Skip to contentSkip to conversation

virtio_ring: Fix the stale index in available ring

PATCH In review Gavin Shan 4 participants 44 messages Mar 14, 2024 – Mar 27, 2024

Subsystems (2)
lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH 1/1 virtio_ring: Fix the stale index in available ring

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

  /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
  -accel kvm -machine virt,gic-version=host                            \
     :                                                                 \
  -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

  [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
 drivers/virtio/virtio_ring.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +9 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
 	vq->split.avail_idx_shadow++;
 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
 						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0
#1 lore raw
Quoted 45 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

  /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
  -accel kvm -machine virt,gic-version=host                            \
     :                                                                 \
  -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

  [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
 drivers/virtio/virtio_ring.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +0 -2
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 698

Quoted 10 lines
drivers/virtio/virtio_ring.c +9 -1
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
 	vq->split.avail_idx_shadow++;
 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
 						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0
author
#2 lore raw
Quoted 44 lines · Michael S. Tsirkin
Quoted 43 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

   /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
   -accel kvm -machine virt,gic-version=host                            \
      :                                                                 \
   -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

   [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
  drivers/virtio/virtio_ring.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
Quoted 17 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 698

Quoted 12 lines
drivers/virtio/virtio_ring.c +9 -3
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

The change is replacing instruction "dmb" with "dsb". "dsb" is stronger barrier than "dmb" because "dsb" ensures that all memory accesses raised before this instruction is completed when the 'dsb' instruction completes. However, "dmb" doesn't guarantee the order of completion of the memory accesses.

So 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' can be completed before 'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'. The stronger barrier 'dsb' ensures the completion order as we expected.

virtio_wmb(true) virt_mb(false)
virt_wmb mb
__smp_wmb __mb
dmb(ishst) dsb(sy)

Extraced from ARMv9 specificaton
================================
The DMB instruction is a memory barrier instruction that ensures the relative order of memory accesses before the barrier with memory accesses after the barrier. The DMB instruction _does not_ ensure the completion of any of the memory accesses for which it ensures relative order.

A DSB instruction is a memory barrier that ensures that memory accesses that occur before the DSB instruction have __completed__ before the completion of the DSB instruction. In doing this, it acts as a stronger barrier than a DMB and all ordering that is created by a DMB with specific options is also generated by a DSB with the same options.

Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
  	vq->split.avail_idx_shadow++;
  	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0

Thanks,
Gavin

#3 lore raw
Quoted 62 lines · Gavin Shan
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

   /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
   -accel kvm -machine virt,gic-version=host                            \
      :                                                                 \
   -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

   [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
  drivers/virtio/virtio_ring.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
Quoted 9 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 698

Quoted 4 lines
drivers/virtio/virtio_ring.c +4 -0
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

Quoted 7 lines

The change is replacing instruction "dmb" with "dsb". "dsb" is stronger barrier than "dmb" because "dsb" ensures that all memory accesses raised before this instruction is completed when the 'dsb' instruction completes. However, "dmb" doesn't guarantee the order of completion of the memory accesses.

So 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' can be completed before 'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by which CPU?
We have 2 writes that we want observed by another CPU in order. So if CPU observes a new value of idx we want it to see new value in ring.
This is standard use of smp_wmb()
How are these 2 writes different?

What DMB does, is that is seems to ensure that effects
of 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' are observed after effects of
'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Quoted 6 lines

The stronger barrier 'dsb' ensures the completion order as we expected.

virtio_wmb(true) virt_mb(false)
virt_wmb mb
__smp_wmb __mb
dmb(ishst) dsb(sy)

First, why would you want a non smp barrier when you are synchronizing with another CPU? This is what smp_ means ...

Quoted 6 lines

Extraced from ARMv9 specificaton
================================
The DMB instruction is a memory barrier instruction that ensures the relative order of memory accesses before the barrier with memory accesses after the barrier. The DMB instruction _does not_ ensure the completion of any of the memory accesses for which it ensures relative order.

Isn't this exactly what we need?

Quoted 3 lines

A DSB instruction is a memory barrier that ensures that memory accesses that occur before the DSB instruction have __completed__ before the completion of the DSB instruction.

This seems appropriate for when you want to order things more strongly. I do not get why it's necessary here.

Quoted 13 lines

In doing this, it acts as a stronger barrier than a DMB
and all ordering that is created by a DMB with specific options is also generated by a DSB with the same options.

Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
  	vq->split.avail_idx_shadow++;
  	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0

Thanks,
Gavin

author
#4 lore raw
Quoted 78 lines · Michael S. Tsirkin
Quoted 62 lines · Gavin Shan
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

    /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
    -accel kvm -machine virt,gic-version=host                            \
       :                                                                 \
    -netdev tap,id=vnet0,vhost=on                                        \
    -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

    [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
   drivers/virtio/virtio_ring.c | 12 +++++++++---
   1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
Quoted 9 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 698

Quoted 4 lines
drivers/virtio/virtio_ring.c +4 -0
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

Quoted 7 lines

The change is replacing instruction "dmb" with "dsb". "dsb" is stronger barrier than "dmb" because "dsb" ensures that all memory accesses raised before this instruction is completed when the 'dsb' instruction completes. However, "dmb" doesn't guarantee the order of completion of the memory accesses.

So 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' can be completed before 'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by which CPU?
We have 2 writes that we want observed by another CPU in order. So if CPU observes a new value of idx we want it to see new value in ring.
This is standard use of smp_wmb()
How are these 2 writes different?

Quoted 6 lines

What DMB does, is that is seems to ensure that effects
of 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' are observed after effects of
'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by the CPU where vhost worker is running. I don't think DMB does the work here. If I'm understanding correctly, DMB ensures the order of these two writes from the local CPU's standpoint. The written data can be stored in local CPU's cache, not flushed to DRAM and propogated to the cache of the far CPU where vhost worker is running. So DMB isn't ensuring the write data is observed from the far CPU.

DSB ensures that the written data is observable from the far CPU immediately.

Quoted 13 lines
Quoted 8 lines

The stronger barrier 'dsb' ensures the completion order as we expected.

virtio_wmb(true) virt_mb(false)
virt_wmb mb
__smp_wmb __mb
dmb(ishst) dsb(sy)

First, why would you want a non smp barrier when you are synchronizing with another CPU? This is what smp_ means ...

Quoted 9 lines
Quoted 6 lines

Extraced from ARMv9 specificaton
================================
The DMB instruction is a memory barrier instruction that ensures the relative order of memory accesses before the barrier with memory accesses after the barrier. The DMB instruction _does not_ ensure the completion of any of the memory accesses for which it ensures relative order.

Isn't this exactly what we need?

I don't think so. DMB gurantees the order of EXECUTION, but DSB gurantees the order of COMPLETION. After the data store is executed, the written data can be stored in local CPU's cache, far from completion. Only the instruction is completed, the written data is synchronized to the far CPU where vhost worker is running.

Here is another paper talking about the difference between DMB and DSB. It's explaining the difference between DMB/DSB better than ARMv9 specification. However, it's hard for us to understand DMB/DSB work from the hardware level based on the specification and paper.

https://ipads.se.sjtu.edu.cn/_media/publications/liuppopp20.pdf
(section 2.2 Order-preserving Options)
(I'm extracting the relevant part as below)

Data Memory Barrier (DMB) prevents reordering of memory accesses across the barrier. Instead of waiting for previous accesses to become observable in the specified domain, DMB only maintains the relative order between memory accesses. Meanwhile, DMB does not block any non-memory access operations.

Data Synchronization Barrier (DSB) prevents reordering of any instructions across the barrier. DSB will make sure that all masters in the specified domain can observe the previous operations before issuing any subsequent instructions.

Quoted 7 lines
Quoted 3 lines

A DSB instruction is a memory barrier that ensures that memory accesses that occur before the DSB instruction have __completed__ before the completion of the DSB instruction.

This seems appropriate for when you want to order things more strongly. I do not get why it's necessary here.

Please refer to above explanations.

Quoted 10 lines
Quoted 10 lines

In doing this, it acts as a stronger barrier than a DMB
and all ordering that is created by a DMB with specific options is also generated by a DSB with the same options.

Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0

Thanks,
Gavin

#5 lore raw
Quoted 86 lines · Gavin Shan
Quoted 78 lines · Michael S. Tsirkin
Quoted 62 lines · Gavin Shan
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

    /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
    -accel kvm -machine virt,gic-version=host                            \
       :                                                                 \
    -netdev tap,id=vnet0,vhost=on                                        \
    -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

    [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
   drivers/virtio/virtio_ring.c | 12 +++++++++---
   1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
Quoted 9 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 698

Quoted 4 lines
drivers/virtio/virtio_ring.c +4 -0
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

Quoted 7 lines

The change is replacing instruction "dmb" with "dsb". "dsb" is stronger barrier than "dmb" because "dsb" ensures that all memory accesses raised before this instruction is completed when the 'dsb' instruction completes. However, "dmb" doesn't guarantee the order of completion of the memory accesses.

So 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' can be completed before 'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by which CPU?
We have 2 writes that we want observed by another CPU in order. So if CPU observes a new value of idx we want it to see new value in ring.
This is standard use of smp_wmb()
How are these 2 writes different?

Quoted 6 lines

What DMB does, is that is seems to ensure that effects
of 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' are observed after effects of
'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Quoted 3 lines

Completed as observed by the CPU where vhost worker is running. I don't think DMB does the work here. If I'm understanding correctly, DMB ensures the order of these two writes from the local CPU's standpoint.

No this makes no sense at all. All memory accesses are in order from local CPU standpoint.

Quoted 4 lines

The written data can be stored in local
CPU's cache, not flushed to DRAM and propogated to the cache of the far CPU where vhost worker is running. So DMB isn't ensuring the write data is observed from the far CPU.

No but it has to ensure *ordering*. So if index is oberved then ring is observed too because there is a MB there when reading.

Quoted 43 lines

DSB ensures that the written data is observable from the far CPU immediately.

Quoted 13 lines
Quoted 8 lines

The stronger barrier 'dsb' ensures the completion order as we expected.

virtio_wmb(true) virt_mb(false)
virt_wmb mb
__smp_wmb __mb
dmb(ishst) dsb(sy)

First, why would you want a non smp barrier when you are synchronizing with another CPU? This is what smp_ means ...

Quoted 9 lines
Quoted 6 lines

Extraced from ARMv9 specificaton
================================
The DMB instruction is a memory barrier instruction that ensures the relative order of memory accesses before the barrier with memory accesses after the barrier. The DMB instruction _does not_ ensure the completion of any of the memory accesses for which it ensures relative order.

Isn't this exactly what we need?

I don't think so. DMB gurantees the order of EXECUTION, but DSB gurantees the order of COMPLETION. After the data store is executed, the written data can be stored in local CPU's cache, far from completion. Only the instruction is completed, the written data is synchronized to the far CPU where vhost worker is running.

Here is another paper talking about the difference between DMB and DSB. It's explaining the difference between DMB/DSB better than ARMv9 specification. However, it's hard for us to understand DMB/DSB work from the hardware level based on the specification and paper.

https://ipads.se.sjtu.edu.cn/_media/publications/liuppopp20.pdf
(section 2.2 Order-preserving Options)
(I'm extracting the relevant part as below)

Quoted 9 lines

Data Memory Barrier (DMB) prevents reordering of memory accesses across the barrier. Instead of waiting for previous accesses to become observable in the specified domain, DMB only maintains the relative order between memory accesses. Meanwhile, DMB does not block any non-memory access operations.

Data Synchronization Barrier (DSB) prevents reordering of any instructions across the barrier. DSB will make sure that all masters in the specified domain can observe the previous operations before issuing any subsequent instructions.

What you are describing is that smp_wmb is buggy on this platform. Poossible but are you sure?
Write a small test and check.
Leave virtio alone.

Quoted 23 lines
Quoted 7 lines
Quoted 3 lines

A DSB instruction is a memory barrier that ensures that memory accesses that occur before the DSB instruction have __completed__ before the completion of the DSB instruction.

This seems appropriate for when you want to order things more strongly. I do not get why it's necessary here.

Please refer to above explanations.

Quoted 10 lines
Quoted 10 lines

In doing this, it acts as a stronger barrier than a DMB
and all ordering that is created by a DMB with specific options is also generated by a DSB with the same options.

Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0

Thanks,
Gavin

author
#6 lore raw

+ Will, Catalin and Matt from Nvidia

Quoted 87 lines · Michael S. Tsirkin
Quoted 86 lines · Gavin Shan
Quoted 84 lines · Michael S. Tsirkin
Quoted 62 lines · Gavin Shan
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

     /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
     -accel kvm -machine virt,gic-version=host                            \
        :                                                                 \
     -netdev tap,id=vnet0,vhost=on                                        \
     -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

     [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
    drivers/virtio/virtio_ring.c | 12 +++++++++---
    1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
    	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
    	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
Quoted 9 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 698

Quoted 4 lines
drivers/virtio/virtio_ring.c +4 -0
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

Quoted 7 lines

The change is replacing instruction "dmb" with "dsb". "dsb" is stronger barrier than "dmb" because "dsb" ensures that all memory accesses raised before this instruction is completed when the 'dsb' instruction completes. However, "dmb" doesn't guarantee the order of completion of the memory accesses.

So 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' can be completed before 'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by which CPU?
We have 2 writes that we want observed by another CPU in order. So if CPU observes a new value of idx we want it to see new value in ring.
This is standard use of smp_wmb()
How are these 2 writes different?

What DMB does, is that is seems to ensure that effects
of 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' are observed after effects of
'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Quoted 7 lines
Quoted 3 lines

Completed as observed by the CPU where vhost worker is running. I don't think DMB does the work here. If I'm understanding correctly, DMB ensures the order of these two writes from the local CPU's standpoint.

No this makes no sense at all. All memory accesses are in order from local CPU standpoint.

It's true if compiler doesn't reorder the accesses, and light-weight barrier like 'dmb' and 'isb' is used. Otherwise, the accesses still can be disordered on the local CPU, correct?

Quoted 8 lines
Quoted 4 lines

The written data can be stored in local
CPU's cache, not flushed to DRAM and propogated to the cache of the far CPU where vhost worker is running. So DMB isn't ensuring the write data is observed from the far CPU.

No but it has to ensure *ordering*. So if index is oberved then ring is observed too because there is a MB there when reading.

Right. It should work like this way theoretically. I don't know the difference between 'dmb' and 'dsb' from the hardware level. The description in ARMv9 spec is also ambiguous. Lets see if Matt will have comments. Besides, this issue is only reproducible on NVidia's grace-hopper. We don't hit the issue on Ampere's CPUs.

Quoted 51 lines
Quoted 41 lines

DSB ensures that the written data is observable from the far CPU immediately.

Quoted 13 lines
Quoted 8 lines

The stronger barrier 'dsb' ensures the completion order as we expected.

virtio_wmb(true) virt_mb(false)
virt_wmb mb
__smp_wmb __mb
dmb(ishst) dsb(sy)

First, why would you want a non smp barrier when you are synchronizing with another CPU? This is what smp_ means ...

Quoted 9 lines
Quoted 6 lines

Extraced from ARMv9 specificaton
================================
The DMB instruction is a memory barrier instruction that ensures the relative order of memory accesses before the barrier with memory accesses after the barrier. The DMB instruction _does not_ ensure the completion of any of the memory accesses for which it ensures relative order.

Isn't this exactly what we need?

I don't think so. DMB gurantees the order of EXECUTION, but DSB gurantees the order of COMPLETION. After the data store is executed, the written data can be stored in local CPU's cache, far from completion. Only the instruction is completed, the written data is synchronized to the far CPU where vhost worker is running.

Here is another paper talking about the difference between DMB and DSB. It's explaining the difference between DMB/DSB better than ARMv9 specification. However, it's hard for us to understand DMB/DSB work from the hardware level based on the specification and paper.

https://ipads.se.sjtu.edu.cn/_media/publications/liuppopp20.pdf
(section 2.2 Order-preserving Options)
(I'm extracting the relevant part as below)

Quoted 9 lines

Data Memory Barrier (DMB) prevents reordering of memory accesses across the barrier. Instead of waiting for previous accesses to become observable in the specified domain, DMB only maintains the relative order between memory accesses. Meanwhile, DMB does not block any non-memory access operations.

Data Synchronization Barrier (DSB) prevents reordering of any instructions across the barrier. DSB will make sure that all masters in the specified domain can observe the previous operations before issuing any subsequent instructions.

Quoted 5 lines

What you are describing is that smp_wmb is buggy on this platform. Poossible but are you sure?
Write a small test and check.
Leave virtio alone.

Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried to reproduce it with my own driver where one thread writes to the shared buffer and another thread reads from the buffer. I don't hit the out-of-order issue so far. My driver may be not correct somewhere and I will update if I can reproduce the issue with my driver in the future.

Quoted 15 lines
Quoted 15 lines
Quoted 8 lines
Quoted 4 lines

A DSB instruction is a memory barrier that ensures that memory accesses that occur before the DSB instruction have __completed__ before the completion of the DSB instruction.

This seems appropriate for when you want to order things more strongly. I do not get why it's necessary here.

Please refer to above explanations.

Quoted 4 lines
Quoted 4 lines

In doing this, it acts as a stronger barrier than a DMB
and all ordering that is created by a DMB with specific options is also generated by a DSB with the same options.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
    	vq->split.avail_idx_shadow++;
    	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
    						vq->split.avail_idx_shadow);
Signature · 1 line
2.44.0

Thanks,
Gavin

#7 lore raw
Quoted 95 lines · Gavin Shan

+ Will, Catalin and Matt from Nvidia

Quoted 91 lines · Michael S. Tsirkin
Quoted 89 lines · Gavin Shan
Quoted 78 lines · Michael S. Tsirkin
Quoted 62 lines · Gavin Shan
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

     /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
     -accel kvm -machine virt,gic-version=host                            \
        :                                                                 \
     -netdev tap,id=vnet0,vhost=on                                        \
     -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

     [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
    drivers/virtio/virtio_ring.c | 12 +++++++++---
    1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +5 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
    	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
    	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
Quoted 9 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 697

Quoted 4 lines
drivers/virtio/virtio_ring.c +4 -0
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);

I don't get what is going on here. Any explanation why virtio_wmb is not enough besides "it does not work"?

Quoted 7 lines

The change is replacing instruction "dmb" with "dsb". "dsb" is stronger barrier than "dmb" because "dsb" ensures that all memory accesses raised before this instruction is completed when the 'dsb' instruction completes. However, "dmb" doesn't guarantee the order of completion of the memory accesses.

So 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' can be completed before 'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by which CPU?
We have 2 writes that we want observed by another CPU in order. So if CPU observes a new value of idx we want it to see new value in ring.
This is standard use of smp_wmb()
How are these 2 writes different?

Quoted 6 lines

What DMB does, is that is seems to ensure that effects
of 'vq->split.vring.avail->idx = cpu_to_virtio(_vq->vdev, vq->split.avail_idx_shadow)' are observed after effects of
'vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head)'.

Completed as observed by the CPU where vhost worker is running. I don't think DMB does the work here. If I'm understanding correctly, DMB ensures the order of these two writes from the local CPU's standpoint.

Quoted 7 lines
Quoted 3 lines

No this makes no sense at all. All memory accesses are in order from local CPU standpoint.

It's true if compiler doesn't reorder the accesses, and light-weight barrier like 'dmb' and 'isb' is used. Otherwise, the accesses still can be disordered on the local CPU, correct?

Compiler issues aside (you can look at binary and see if they got reordered) no, the local CPU always observes all writes in the same way they were initiated, and on all architectures.

Quoted 71 lines
Quoted 8 lines
Quoted 4 lines

The written data can be stored in local
CPU's cache, not flushed to DRAM and propogated to the cache of the far CPU where vhost worker is running. So DMB isn't ensuring the write data is observed from the far CPU.

No but it has to ensure *ordering*. So if index is oberved then ring is observed too because there is a MB there when reading.

Right. It should work like this way theoretically. I don't know the difference between 'dmb' and 'dsb' from the hardware level. The description in ARMv9 spec is also ambiguous. Lets see if Matt will have comments. Besides, this issue is only reproducible on NVidia's grace-hopper. We don't hit the issue on Ampere's CPUs.

Quoted 50 lines
Quoted 40 lines

DSB ensures that the written data is observable from the far CPU immediately.

Quoted 13 lines
Quoted 8 lines

The stronger barrier 'dsb' ensures the completion order as we expected.

virtio_wmb(true) virt_mb(false)
virt_wmb mb
__smp_wmb __mb
dmb(ishst) dsb(sy)

First, why would you want a non smp barrier when you are synchronizing with another CPU? This is what smp_ means ...

Quoted 9 lines
Quoted 6 lines

Extraced from ARMv9 specificaton
================================
The DMB instruction is a memory barrier instruction that ensures the relative order of memory accesses before the barrier with memory accesses after the barrier. The DMB instruction _does not_ ensure the completion of any of the memory accesses for which it ensures relative order.

Isn't this exactly what we need?

I don't think so. DMB gurantees the order of EXECUTION, but DSB gurantees the order of COMPLETION. After the data store is executed, the written data can be stored in local CPU's cache, far from completion. Only the instruction is completed, the written data is synchronized to the far CPU where vhost worker is running.

Here is another paper talking about the difference between DMB and DSB. It's explaining the difference between DMB/DSB better than ARMv9 specification. However, it's hard for us to understand DMB/DSB work from the hardware level based on the specification and paper.

https://ipads.se.sjtu.edu.cn/_media/publications/liuppopp20.pdf
(section 2.2 Order-preserving Options)
(I'm extracting the relevant part as below)

Quoted 9 lines

Data Memory Barrier (DMB) prevents reordering of memory accesses across the barrier. Instead of waiting for previous accesses to become observable in the specified domain, DMB only maintains the relative order between memory accesses. Meanwhile, DMB does not block any non-memory access operations.

Data Synchronization Barrier (DSB) prevents reordering of any instructions across the barrier. DSB will make sure that all masters in the specified domain can observe the previous operations before issuing any subsequent instructions.

Quoted 5 lines

What you are describing is that smp_wmb is buggy on this platform. Poossible but are you sure?
Write a small test and check.
Leave virtio alone.

Quoted 4 lines

Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried to reproduce it with my own driver where one thread writes to the shared buffer and another thread reads from the buffer. I don't hit the out-of-order issue so far.

Make sure the 2 areas you are accessing are in different cache lines.

Quoted 2 lines

My driver may be not correct somewhere and I will update if I can reproduce the issue with my driver in the future.

Then maybe your change is just making virtio slower and masks the bug that is actually elsewhere?

You don't really need a driver. Here's a simple test: without barriers assertion will fail. With barriers it will not.
(Warning: didn't bother testing too much, could be buggy.

---

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define FIRST values[0]
#define SECOND values[64]

volatile int values[100] = {};

void* writer_thread(void* arg) {
	while (1) {
	FIRST++;
	// NEED smp_wmb here
	SECOND++;
	}
}
void* reader_thread(void* arg) {
    while (1) {
	int first = FIRST;
	// NEED smp_rmb here
	int second = SECOND;
	assert(first - second == 1 || first - second == 0);
    }
}
int main() {
    pthread_t writer, reader;

    pthread_create(&writer, NULL, writer_thread, NULL);
    pthread_create(&reader, NULL, reader_thread, NULL);

    pthread_join(writer, NULL);
    pthread_join(reader, NULL);

    return 0;
}
Signature · 1 line
MST
author
#8 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 4 lines · Gavin Shan
Quoted 1 line
Quoted 1 line

Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried

to reproduce it with my own driver where one thread writes to the shared buffer and another thread reads from the buffer. I don't hit the out-of-order issue so far.

Make sure the 2 areas you are accessing are in different cache lines.

Yes, I already put those 2 areas to separate cache lines.

Quoted 21 lines
Quoted 3 lines

My driver may be not correct somewhere and I will update if I can reproduce the issue with my driver in the future.

Then maybe your change is just making virtio slower and masks the bug that is actually elsewhere?

You don't really need a driver. Here's a simple test: without barriers assertion will fail. With barriers it will not.
(Warning: didn't bother testing too much, could be buggy.

---

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define FIRST values[0]
#define SECOND values[64]

Quoted 6 lines

volatile int values[100] = {};

void* writer_thread(void* arg) {
	while (1) {
	FIRST++;
	// NEED smp_wmb here
        __asm__ volatile("dmb ishst" : : : "memory");
Quoted 8 lines
	SECOND++;
	}
}
void* reader_thread(void* arg) {
     while (1) {
	int first = FIRST;
	// NEED smp_rmb here
        __asm__ volatile("dmb ishld" : : : "memory");
Quoted 5 lines
	int second = SECOND;
	assert(first - second == 1 || first - second == 0);
     }
}
Quoted 12 lines
int main() {
     pthread_t writer, reader;

     pthread_create(&writer, NULL, writer_thread, NULL);
     pthread_create(&reader, NULL, reader_thread, NULL);

     pthread_join(writer, NULL);
     pthread_join(reader, NULL);

     return 0;
}

Had a quick test on NVidia's grace-hopper and Ampere's CPUs. I hit the assert on both of them. After replacing 'dmb' with 'dsb', I can hit assert on both of them too. I need to look at the code closely.

[root@virt-mtcollins-02 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

[root@nvidia-grace-hopper-05 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Thanks,
Gavin

#9 lore raw
Quoted 76 lines · Gavin Shan
Quoted 8 lines · Michael S. Tsirkin
Quoted 4 lines · Gavin Shan
Quoted 1 line
Quoted 1 line

Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried

to reproduce it with my own driver where one thread writes to the shared buffer and another thread reads from the buffer. I don't hit the out-of-order issue so far.

Make sure the 2 areas you are accessing are in different cache lines.

Yes, I already put those 2 areas to separate cache lines.

Quoted 21 lines
Quoted 3 lines

My driver may be not correct somewhere and I will update if I can reproduce the issue with my driver in the future.

Then maybe your change is just making virtio slower and masks the bug that is actually elsewhere?

You don't really need a driver. Here's a simple test: without barriers assertion will fail. With barriers it will not.
(Warning: didn't bother testing too much, could be buggy.

---

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define FIRST values[0]
#define SECOND values[64]

Quoted 6 lines

volatile int values[100] = {};

void* writer_thread(void* arg) {
	while (1) {
	FIRST++;
	// NEED smp_wmb here
        __asm__ volatile("dmb ishst" : : : "memory");
Quoted 8 lines
	SECOND++;
	}
}
void* reader_thread(void* arg) {
     while (1) {
	int first = FIRST;
	// NEED smp_rmb here
        __asm__ volatile("dmb ishld" : : : "memory");
Quoted 5 lines
	int second = SECOND;
	assert(first - second == 1 || first - second == 0);
     }
}
Quoted 12 lines
int main() {
     pthread_t writer, reader;

     pthread_create(&writer, NULL, writer_thread, NULL);
     pthread_create(&reader, NULL, reader_thread, NULL);

     pthread_join(writer, NULL);
     pthread_join(reader, NULL);

     return 0;
}

Had a quick test on NVidia's grace-hopper and Ampere's CPUs. I hit the assert on both of them. After replacing 'dmb' with 'dsb', I can hit assert on both of them too. I need to look at the code closely.

[root@virt-mtcollins-02 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Quoted 6 lines

[root@nvidia-grace-hopper-05 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Thanks,
Gavin

Actually this test is broken. No need for ordering it's a simple race. The following works on x86 though (x86 does not need barriers though).

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#if 0
#define x86_rmb() asm volatile("lfence":::"memory") #define x86_mb() asm volatile("mfence":::"memory") #define x86_smb() asm volatile("sfence":::"memory") #else
#define x86_rmb() asm volatile("":::"memory") #define x86_mb() asm volatile("":::"memory") #define x86_smb() asm volatile("":::"memory") #endif

#define FIRST values[0]
#define SECOND values[640]
#define FLAG values[1280]

volatile unsigned values[2000] = {};

void* writer_thread(void* arg) {
	while (1) {
	/* Now synchronize with reader */
	while(FLAG);
	FIRST++;
	x86_smb();
	SECOND++;
	x86_smb();
	FLAG = 1;
	}
}
void* reader_thread(void* arg) {
    while (1) {
	/* Now synchronize with writer */
	while(!FLAG);
	x86_rmb();
	unsigned first = FIRST;
	x86_rmb();
	unsigned second = SECOND;
	assert(first - second == 1 || first - second == 0);
	FLAG = 0;

	if (!(first %1000000))
		printf("%d\n", first);

}
}

int main() {
    pthread_t writer, reader;

    pthread_create(&writer, NULL, writer_thread, NULL);
    pthread_create(&reader, NULL, reader_thread, NULL);

    pthread_join(writer, NULL);
    pthread_join(reader, NULL);

    return 0;
}
author
#10 lore raw
Quoted 139 lines · Michael S. Tsirkin
Quoted 76 lines · Gavin Shan
Quoted 8 lines · Michael S. Tsirkin
Quoted 4 lines · Gavin Shan
Quoted 1 line
Quoted 1 line

Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried

to reproduce it with my own driver where one thread writes to the shared buffer and another thread reads from the buffer. I don't hit the out-of-order issue so far.

Make sure the 2 areas you are accessing are in different cache lines.

Yes, I already put those 2 areas to separate cache lines.

Quoted 21 lines
Quoted 3 lines

My driver may be not correct somewhere and I will update if I can reproduce the issue with my driver in the future.

Then maybe your change is just making virtio slower and masks the bug that is actually elsewhere?

You don't really need a driver. Here's a simple test: without barriers assertion will fail. With barriers it will not.
(Warning: didn't bother testing too much, could be buggy.

---

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define FIRST values[0]
#define SECOND values[64]

Quoted 6 lines

volatile int values[100] = {};

void* writer_thread(void* arg) {
	while (1) {
	FIRST++;
	// NEED smp_wmb here
         __asm__ volatile("dmb ishst" : : : "memory");
Quoted 8 lines
	SECOND++;
	}
}
void* reader_thread(void* arg) {
      while (1) {
	int first = FIRST;
	// NEED smp_rmb here
         __asm__ volatile("dmb ishld" : : : "memory");
Quoted 5 lines
	int second = SECOND;
	assert(first - second == 1 || first - second == 0);
      }
}
Quoted 12 lines
int main() {
      pthread_t writer, reader;

      pthread_create(&writer, NULL, writer_thread, NULL);
      pthread_create(&reader, NULL, reader_thread, NULL);

      pthread_join(writer, NULL);
      pthread_join(reader, NULL);

      return 0;
}

Had a quick test on NVidia's grace-hopper and Ampere's CPUs. I hit the assert on both of them. After replacing 'dmb' with 'dsb', I can hit assert on both of them too. I need to look at the code closely.

[root@virt-mtcollins-02 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Quoted 6 lines

[root@nvidia-grace-hopper-05 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Thanks,
Gavin

Actually this test is broken. No need for ordering it's a simple race. The following works on x86 though (x86 does not need barriers though).

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#if 0
#define x86_rmb() asm volatile("lfence":::"memory") #define x86_mb() asm volatile("mfence":::"memory") #define x86_smb() asm volatile("sfence":::"memory") #else
#define x86_rmb() asm volatile("":::"memory") #define x86_mb() asm volatile("":::"memory") #define x86_smb() asm volatile("":::"memory") #endif

#define FIRST values[0]
#define SECOND values[640]
#define FLAG values[1280]

volatile unsigned values[2000] = {};

void* writer_thread(void* arg) {
	while (1) {
	/* Now synchronize with reader */
	while(FLAG);
	FIRST++;
	x86_smb();
	SECOND++;
	x86_smb();
	FLAG = 1;
	}
}
void* reader_thread(void* arg) {
     while (1) {
	/* Now synchronize with writer */
	while(!FLAG);
	x86_rmb();
	unsigned first = FIRST;
	x86_rmb();
	unsigned second = SECOND;
	assert(first - second == 1 || first - second == 0);
	FLAG = 0;

	if (!(first %1000000))
		printf("%d\n", first);
    }
}
Quoted 12 lines
int main() {
     pthread_t writer, reader;

     pthread_create(&writer, NULL, writer_thread, NULL);
     pthread_create(&reader, NULL, reader_thread, NULL);

     pthread_join(writer, NULL);
     pthread_join(reader, NULL);

     return 0;
}

I tried it on host and VM of NVidia's grace-hopper. Without the barriers, I can hit assert. With the barriers, it's working fine without hitting the assert.

I also had some code to mimic virtio vring last weekend, and it's just working well. Back to our original issue, __smb_wmb() is issued by guest while __smb_rmb() is executed on host. The VM and host are running at different exception level: EL2 vs EL1. I'm not sure it's the cause. I need to modify my code so that __smb_wmb() and __smb_rmb() can be executed from guest and host.

[gshan@gshan code]$ cat test.h
#ifndef __TEST_H
#define __TEST_H

struct vring_desc {
	uint64_t	addr;
	uint32_t	len;
	uint16_t	flags;
	uint16_t	next;
} __attribute__((aligned(4)));
struct vring_avail {
	uint16_t	flags;
	uint16_t	idx;
	uint16_t	ring[];
} __attribute__((aligned(4)));
struct vring_used_elem {
	uint32_t	id;
	uint32_t	len;
} __attribute__((aligned(4)));
struct vring_used {
	uint16_t 		flags;
        uint16_t 		idx;
        struct vring_used_elem	ring[];
} __attribute__((aligned(4)));
struct vring {
	struct vring_desc	*desc;
	struct vring_avail	*avail;
	struct vring_used	*used;
	uint8_t			pad0[64];

	/* Writer */
	uint32_t		num;
	uint32_t		w_num_free;
	uint32_t		w_free_head;
	uint16_t		w_avail_idx;
	uint16_t		w_last_used_idx;
	uint16_t		*w_extra_data;
	uint16_t		*w_extra_next;
	uint8_t			pad1[64];

	/* Reader */
	uint16_t		r_avail_idx;
	uint16_t		r_last_avail_idx;
	uint16_t		r_last_used_idx;
	uint8_t			pad2[64];
};

static inline unsigned int vring_size(unsigned int num, unsigned long align)

{
	return ((sizeof(struct vring_desc) * num +
		 sizeof(uint16_t) * (3 + num) + (align - 1)) & ~(align - 1)) +
	       sizeof(uint16_t) * 3 + sizeof(struct vring_used_elem) * num;

}

static inline void __smp_rmb(void)
{

#ifdef WEAK_BARRIER
	__asm__ volatile("dmb ishld" : : : "memory");
#else
	__asm__ volatile("dsb sy"    : : : "memory");

#endif
}

static inline void __smp_wmb(void)
{

#ifdef WEAK_BARRIER
	__asm__ volatile("dmb ishst" : : : "memory");
#else
	__asm__ volatile("dsb sy"    : : : "memory");

#endif
}

#endif /* __TEST_H */

[gshan@gshan code]$ cat test.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <assert.h>
#include <sched.h>
#include <pthread.h>

#include "test.h"

static struct vring *vring;

static int bind_cpu(int cpuid)

{
	cpu_set_t cpuset;

	CPU_ZERO(&cpuset);
	CPU_SET(cpuid, &cpuset);

	return pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
}

static void write_free_used_desc(void)

{
	uint16_t last_used;
	uint32_t idx;

	if ((uint16_t)(vring->used->idx - vring->w_last_used_idx) < 64)
		return;

	while (true) {
		if (vring->w_last_used_idx == vring->used->idx)
			return;

		__smp_rmb();

		/* Retrieve the head */
		last_used = vring->w_last_used_idx & (vring->num - 1);
		idx = vring->used->ring[last_used].id;
		assert(idx < vring->num);
		assert(vring->w_extra_data[idx]);

		/* Reclaim the descriptor */
		vring->w_extra_data[idx] = 0;
		vring->w_extra_next[idx] = vring->w_free_head;
		vring->w_free_head = idx;

		/* Update statistics */
		vring->w_num_free++;
		vring->w_last_used_idx++;
	}
}

static void write_push_desc(void)

{
	uint32_t head = vring->w_free_head;
	uint32_t avail_idx;

	if (vring->w_num_free < 1)
		return;

	/*
	 * The data in the descriptor doesn't matter. The idea here
	 * is to dirty the cache line.
	 */
	vring->desc[head].flags = 1;
	vring->desc[head].addr  = 0xffffffffffffffff;
	vring->desc[head].len   = 0xffffffff;
	vring->desc[head].next  = vring->w_extra_next[head];
	vring->desc[head].flags = 0;

	vring->w_num_free--;
	vring->w_free_head = vring->w_extra_next[head];
	vring->w_extra_data[head] = 1;

	avail_idx = vring->w_avail_idx & (vring->num - 1);
	vring->avail->ring[avail_idx] = head;

	__smp_wmb();

	vring->w_avail_idx++;
	vring->avail->idx = vring->w_avail_idx;
}

static void *write_worker(void *arg)

{
	assert(!bind_cpu(10));

	while (true) {
		write_free_used_desc();
		write_push_desc();
	}

	return NULL;
}

static void read_pull_desc(void)

{
	uint16_t avail_idx, last_avail_idx;
	uint32_t head;

	last_avail_idx = vring->r_last_avail_idx;
	if (vring->r_avail_idx == vring->r_last_avail_idx) {
		vring->r_avail_idx = vring->avail->idx;
		if (vring->r_avail_idx == last_avail_idx)
			return;

		__smp_rmb();
	}

	head = vring->avail->ring[last_avail_idx & (vring->num - 1)];
	assert(head < vring->num);
	vring->r_last_avail_idx++;

	vring->used->ring[vring->r_last_used_idx & (vring->num - 1)].id  = head;
	vring->used->ring[vring->r_last_used_idx & (vring->num - 1)].len = 0;
	vring->r_last_used_idx++;

	__smp_wmb();

	vring->used->idx = vring->r_last_used_idx;
}

static void *read_worker(void *arg)

{
	assert(!bind_cpu(60));

	while (true) {
		read_pull_desc();
	}

	return NULL;
}

static void init_vring(unsigned int num, unsigned long align)

{
	unsigned int size, i;

	/* vring */
	vring = malloc(sizeof(*vring));
	assert(vring);
	memset(vring, 0, sizeof(*vring));

	/* Descriptors */
	size = vring_size(num, align);
	vring->desc = (struct vring_desc *)malloc(size);
	assert(vring->desc);
	memset(vring->desc, 0, size);
	vring->avail = (struct vring_avail *)((void *)vring->desc +
					      num * sizeof(struct vring_desc));
	vring->used = (struct vring_used *)(((unsigned long)&vring->avail->ring[num] +
					    sizeof(uint16_t) + (align - 1)) & ~(align - 1));

	/* Writer's extra data */
	vring->w_extra_data = malloc(sizeof(uint16_t) * num);
	assert(vring->w_extra_data);
	memset(vring->w_extra_data, 0, sizeof(uint16_t) * num);
	vring->w_extra_next = malloc(sizeof(uint16_t) * num);
	assert(vring->w_extra_next);
	memset(vring->w_extra_next, 0, sizeof(uint16_t) * num);
	for (i = 0; i < num - 1; i++)
		vring->w_extra_next[i] = i + 1;

	/* Statistics */
	vring->num = num;
	vring->w_num_free = num;
	vring->w_free_head = 0;
	vring->w_avail_idx = 0;
	vring->w_last_used_idx = 0;
	vring->r_avail_idx = 0;
	vring->r_last_avail_idx = 0;
	vring->r_last_used_idx = 0;
}

int main(int argc, char **argv)

{
	pthread_t w_tid, r_tid;
	int ret;

	assert(!bind_cpu(0));

	init_vring(256, 64);

	ret = pthread_create(&w_tid, NULL, write_worker, NULL);
	assert(!ret);
	ret = pthread_create(&r_tid, NULL, read_worker, NULL);
	assert(!ret);
	ret = pthread_join(w_tid, NULL);
	assert(!ret);
	ret = pthread_join(r_tid, NULL);
	assert(!ret);

	while (true) {
		sleep(1);
	}

	return 0;
}

Thanks,
Gavin

#11 lore raw
Quoted 157 lines · Gavin Shan
Quoted 139 lines · Michael S. Tsirkin
Quoted 76 lines · Gavin Shan
Quoted 8 lines · Michael S. Tsirkin
Quoted 4 lines · Gavin Shan
Quoted 1 line
Quoted 1 line

Yes, I guess smp_wmb() ('dmb') is buggy on NVidia's grace-hopper platform. I tried

to reproduce it with my own driver where one thread writes to the shared buffer and another thread reads from the buffer. I don't hit the out-of-order issue so far.

Make sure the 2 areas you are accessing are in different cache lines.

Yes, I already put those 2 areas to separate cache lines.

Quoted 21 lines
Quoted 3 lines

My driver may be not correct somewhere and I will update if I can reproduce the issue with my driver in the future.

Then maybe your change is just making virtio slower and masks the bug that is actually elsewhere?

You don't really need a driver. Here's a simple test: without barriers assertion will fail. With barriers it will not.
(Warning: didn't bother testing too much, could be buggy.

---

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define FIRST values[0]
#define SECOND values[64]

Quoted 6 lines

volatile int values[100] = {};

void* writer_thread(void* arg) {
	while (1) {
	FIRST++;
	// NEED smp_wmb here
         __asm__ volatile("dmb ishst" : : : "memory");
Quoted 8 lines
	SECOND++;
	}
}
void* reader_thread(void* arg) {
      while (1) {
	int first = FIRST;
	// NEED smp_rmb here
         __asm__ volatile("dmb ishld" : : : "memory");
Quoted 5 lines
	int second = SECOND;
	assert(first - second == 1 || first - second == 0);
      }
}
Quoted 12 lines
int main() {
      pthread_t writer, reader;

      pthread_create(&writer, NULL, writer_thread, NULL);
      pthread_create(&reader, NULL, reader_thread, NULL);

      pthread_join(writer, NULL);
      pthread_join(reader, NULL);

      return 0;
}

Had a quick test on NVidia's grace-hopper and Ampere's CPUs. I hit the assert on both of them. After replacing 'dmb' with 'dsb', I can hit assert on both of them too. I need to look at the code closely.

[root@virt-mtcollins-02 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Quoted 6 lines

[root@nvidia-grace-hopper-05 test]# ./a
a: a.c:26: reader_thread: Assertion `first - second == 1 || first - second == 0' failed. Aborted (core dumped)

Thanks,
Gavin

Actually this test is broken. No need for ordering it's a simple race. The following works on x86 though (x86 does not need barriers though).

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#if 0
#define x86_rmb() asm volatile("lfence":::"memory") #define x86_mb() asm volatile("mfence":::"memory") #define x86_smb() asm volatile("sfence":::"memory") #else
#define x86_rmb() asm volatile("":::"memory") #define x86_mb() asm volatile("":::"memory") #define x86_smb() asm volatile("":::"memory") #endif

#define FIRST values[0]
#define SECOND values[640]
#define FLAG values[1280]

volatile unsigned values[2000] = {};

void* writer_thread(void* arg) {
	while (1) {
	/* Now synchronize with reader */
	while(FLAG);
	FIRST++;
	x86_smb();
	SECOND++;
	x86_smb();
	FLAG = 1;
	}
}
void* reader_thread(void* arg) {
     while (1) {
	/* Now synchronize with writer */
	while(!FLAG);
	x86_rmb();
	unsigned first = FIRST;
	x86_rmb();
	unsigned second = SECOND;
	assert(first - second == 1 || first - second == 0);
	FLAG = 0;

	if (!(first %1000000))
		printf("%d\n", first);
    }
}
Quoted 12 lines
int main() {
     pthread_t writer, reader;

     pthread_create(&writer, NULL, writer_thread, NULL);
     pthread_create(&reader, NULL, reader_thread, NULL);

     pthread_join(writer, NULL);
     pthread_join(reader, NULL);

     return 0;
}

I tried it on host and VM of NVidia's grace-hopper. Without the barriers, I can hit assert. With the barriers, it's working fine without hitting the assert.

Quoted 6 lines

I also had some code to mimic virtio vring last weekend, and it's just working well. Back to our original issue, __smb_wmb() is issued by guest while __smb_rmb() is executed on host. The VM and host are running at different exception level: EL2 vs EL1. I'm not sure it's the cause. I need to modify my code so that __smb_wmb() and __smb_rmb() can be executed from guest and host.

It is thinkably possible that on grace-hopper barriers work differently somehow. We need to find out more though. Anyone from Nvidia can chime in?

Signature · 1 line
MST
#12 lore raw
In reply to Gavin Shan
Quoted 48 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

  /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
  -accel kvm -machine virt,gic-version=host                            \
     :                                                                 \
  -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

  [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
 drivers/virtio/virtio_ring.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 701

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
 	vq->split.avail_idx_shadow++;
 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
 						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Will

author
#13 lore raw
Quoted 63 lines · Will Deacon
Quoted 48 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

   /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
   -accel kvm -machine virt,gic-version=host                            \
      :                                                                 \
   -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

   [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
  drivers/virtio/virtio_ring.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 701

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
  	vq->split.avail_idx_shadow++;
  	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
    :
        /* Put entry in available array (but don't update avail->idx until they
         * do sync). */
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

        /* Descriptors and available array need to be set before we expose the
         * new available array entries. */
        // Broken: virtio_wmb(vq->weak_barriers);
        // Broken: __dma_mb();
        // Work:   __mb();
        // Work:   __smp_mb();
        // Work:   __ndelay(100);
        // Work:   __ndelay(10);
        // Broken: __ndelay(9);

       vq->split.avail_idx_shadow++;
        vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                vq->split.avail_idx_shadow);
        vq->num_added++;

        pr_debug("Added buffer head %i to %p\n", head, vq);
        END_USE(vq);
        :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

    Instruction           Range of used time in ns
    ----------------------------------------------
    __smp_wmb()           [32  1128032]
    __smp_mb()            [32  1160096]
    __mb()                [32  1162496]

Thanks,
Gavin

#14 lore raw
Quoted 95 lines · Gavin Shan
Quoted 62 lines · Will Deacon
Quoted 47 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

   /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
   -accel kvm -machine virt,gic-version=host                            \
      :                                                                 \
   -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

   [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
  drivers/virtio/virtio_ring.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
  	vq->split.avail_idx_shadow++;
  	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
    :
        /* Put entry in available array (but don't update avail->idx until they
         * do sync). */
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

        /* Descriptors and available array need to be set before we expose the
         * new available array entries. */
        // Broken: virtio_wmb(vq->weak_barriers);
        // Broken: __dma_mb();
        // Work:   __mb();
Quoted 8 lines
        // Work:   __smp_mb();
        // Work:   __ndelay(100);
        // Work:   __ndelay(10);
        // Broken: __ndelay(9);

       vq->split.avail_idx_shadow++;
        vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                vq->split.avail_idx_shadow);

What if you stick __ndelay here?

Quoted 19 lines
        vq->num_added++;

        pr_debug("Added buffer head %i to %p\n", head, vq);
        END_USE(vq);
        :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

    Instruction           Range of used time in ns
    ----------------------------------------------
    __smp_wmb()           [32  1128032]
    __smp_mb()            [32  1160096]
    __mb()                [32  1162496]

Thanks,
Gavin

#15 lore raw
Quoted 91 lines · Michael S. Tsirkin
Quoted 90 lines · Gavin Shan
Quoted 62 lines · Will Deacon
Quoted 47 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

   /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
   -accel kvm -machine virt,gic-version=host                            \
      :                                                                 \
   -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

   [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
  drivers/virtio/virtio_ring.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
  	vq->split.avail_idx_shadow++;
  	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
    :
        /* Put entry in available array (but don't update avail->idx until they
         * do sync). */
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
Quoted 6 lines
Quoted 6 lines
        /* Descriptors and available array need to be set before we expose the
         * new available array entries. */
        // Broken: virtio_wmb(vq->weak_barriers);
        // Broken: __dma_mb();
        // Work:   __mb();
        // Work:   __smp_mb();

Did you try __smp_wmb ? And wmb?

Quoted 9 lines
Quoted 7 lines
        // Work:   __ndelay(100);
        // Work:   __ndelay(10);
        // Broken: __ndelay(9);

       vq->split.avail_idx_shadow++;
        vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                vq->split.avail_idx_shadow);

What if you stick __ndelay here?

And keep virtio_wmb above?

Quoted 20 lines
Quoted 20 lines
        vq->num_added++;

        pr_debug("Added buffer head %i to %p\n", head, vq);
        END_USE(vq);
        :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

    Instruction           Range of used time in ns
    ----------------------------------------------
    __smp_wmb()           [32  1128032]
    __smp_mb()            [32  1160096]
    __mb()                [32  1162496]

Thanks,
Gavin

author
#16 lore raw
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Gavin Shan

On 3/19/24 02:59, Will Deacon wrote:

[...]

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();

Did you try __smp_wmb ? And wmb?

virtio_wmb(false) is equivalent to __smb_wmb(), which is broken.

__wmb() works either. No issue found with it.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
         // Work:   __ndelay(100);
         // Work:   __ndelay(10);
         // Broken: __ndelay(9);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
        vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);

What if you stick __ndelay here?

And keep virtio_wmb above?

The result has been shared through a separate reply.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
         vq->num_added++;

         pr_debug("Added buffer head %i to %p\n", head, vq);
         END_USE(vq);
         :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
     Instruction           Range of used time in ns
     ----------------------------------------------
     __smp_wmb()           [32  1128032]
     __smp_mb()            [32  1160096]
     __mb()                [32  1162496]

Thanks,
Gavin

#17 lore raw
Quoted 65 lines · Gavin Shan
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Gavin Shan

On 3/19/24 02:59, Will Deacon wrote:

[...]

Quoted 60 lines
Quoted 59 lines
Quoted 59 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();
Quoted 6 lines
Quoted 2 lines

Did you try __smp_wmb ? And wmb?

virtio_wmb(false) is equivalent to __smb_wmb(), which is broken.

__wmb() works either. No issue found with it.

Oh interesting. So how do smp_mb() and wmb() disassemble on this platform? Can you please check?

Quoted 36 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
         // Work:   __ndelay(100);
         // Work:   __ndelay(10);
         // Broken: __ndelay(9);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
        vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);

What if you stick __ndelay here?

And keep virtio_wmb above?

The result has been shared through a separate reply.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
         vq->num_added++;

         pr_debug("Added buffer head %i to %p\n", head, vq);
         END_USE(vq);
         :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
     Instruction           Range of used time in ns
     ----------------------------------------------
     __smp_wmb()           [32  1128032]
     __smp_mb()            [32  1160096]
     __mb()                [32  1162496]

Thanks,
Gavin

author
#18 lore raw
Quoted 69 lines · Michael S. Tsirkin
Quoted 68 lines · Gavin Shan
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Gavin Shan

On 3/19/24 02:59, Will Deacon wrote:

[...]

Quoted 62 lines
Quoted 53 lines
Quoted 53 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
    	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
    	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
    	vq->split.avail_idx_shadow++;
    	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
    						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
      :
          /* Put entry in available array (but don't update avail->idx until they
           * do sync). */
          avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
          vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
Quoted 6 lines
Quoted 6 lines
          /* Descriptors and available array need to be set before we expose the
           * new available array entries. */
          // Broken: virtio_wmb(vq->weak_barriers);
          // Broken: __dma_mb();
          // Work:   __mb();
          // Work:   __smp_mb();

Did you try __smp_wmb ? And wmb?

Quoted 7 lines
Quoted 3 lines

virtio_wmb(false) is equivalent to __smb_wmb(), which is broken.

__wmb() works either. No issue found with it.

Oh interesting. So how do smp_mb() and wmb() disassemble on this platform? Can you please check?

I don't see they have been translated wrongly on Nvidia's grace-hopper:

===> virtio_wmb(vq->weak_barriers)

0xffff8000807b07c8 <+1168>:  ldrb    w0, [x20, #66]
0xffff8000807b07cc <+1172>:  cbz     w0, 0xffff8000807b089c <virtqueue_add_split+1380>
0xffff8000807b07d0 <+1176>:  dmb     ishst     // same to __smp_wmb()
    :
0xffff8000807b089c <+1380>:  dmb     oshst     // same to __dma_wmb()
0xffff8000807b08a0 <+1384>:  b       0xffff8000807b07d4 <virtqueue_add_split+1180>

===> wmb()

0xffff8000807b07c8 <+1168>: dsb st

Quoted 28 lines
Quoted 28 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
          // Work:   __ndelay(100);
          // Work:   __ndelay(10);
          // Broken: __ndelay(9);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
         vq->split.avail_idx_shadow++;
          vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                  vq->split.avail_idx_shadow);

What if you stick __ndelay here?

And keep virtio_wmb above?

The result has been shared through a separate reply.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
          vq->num_added++;

          pr_debug("Added buffer head %i to %p\n", head, vq);
          END_USE(vq);
          :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
      Instruction           Range of used time in ns
      ----------------------------------------------
      __smp_wmb()           [32  1128032]
      __smp_mb()            [32  1160096]
      __mb()                [32  1162496]

Thanks,
Gavin

#19 lore raw
In reply to Gavin Shan
Quoted 65 lines · Gavin Shan
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Gavin Shan

On 3/19/24 02:59, Will Deacon wrote:

[...]

Quoted 60 lines
Quoted 59 lines
Quoted 59 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();
Quoted 7 lines
Quoted 2 lines

Did you try __smp_wmb ? And wmb?

virtio_wmb(false) is equivalent to __smb_wmb(), which is broken.

__wmb() works either. No issue found with it.

So this is
arch/arm64/include/asm/barrier.h:#define __smp_wmb() dmb(ishst)

versus

arch/arm64/include/asm/barrier.h:#define __wmb() dsb(st)

right?

Really interesting. And you are saying dma_wmb does not work either:

arch/arm64/include/asm/barrier.h:#define __dma_wmb() dmb(oshst)

Really strange.

However I found this:
https://developer.arm.com/documentation/den0024/a/Memory-Ordering/Memory-attributes/Cacheable-and-shareable-memory-attributes

Going by this picture, all CPUs are in the innner shareable domain so ishst should be enough to synchronize, right?

However, there are two points that give me pause here:

Inner shareable
This represents a shareability domain that can be shared by multiple processors, but not necessarily all of the agents in the system. A system might have multiple Inner Shareable domains. An operation that affects one Inner Shareable domain does not affect other Inner Shareable domains in the system. An example of such a domain might be a quad-core Cortex-A57 cluster.

Point 1 - so is it possible that there are multiple inner shareable domains in this system? With vhost running inside one and guest inside another? Anyone knows if that is the case on nvidia grace hopper and how to find out?

Outer shareable
An outer shareable (OSH) domain re-order is shared by multiple agents and can consist of one or more inner shareable domains. An operation that affects an outer shareable domain also implicitly affects all inner shareable domains inside it. However, it does not otherwise behave as an inner shareable operation.

I do not get this last sentence. If it affects all inner domains then how it "does not behave as an inner shareable operation"?

Quoted 37 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
         // Work:   __ndelay(100);
         // Work:   __ndelay(10);
         // Broken: __ndelay(9);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
        vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);

What if you stick __ndelay here?

And keep virtio_wmb above?

The result has been shared through a separate reply.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
         vq->num_added++;

         pr_debug("Added buffer head %i to %p\n", head, vq);
         END_USE(vq);
         :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
     Instruction           Range of used time in ns
     ----------------------------------------------
     __smp_wmb()           [32  1128032]
     __smp_mb()            [32  1160096]
     __mb()                [32  1162496]

Thanks,
Gavin

author
#20 lore raw
In reply to Michael S. Tsirkin
Quoted 63 lines · Michael S. Tsirkin
Quoted 63 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();
         // Work:   __ndelay(100);
         // Work:   __ndelay(10);
         // Broken: __ndelay(9);
Quoted 6 lines
Quoted 3 lines
        vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);

What if you stick __ndelay here?

       /* Put entry in available array (but don't update avail->idx until they
         * do sync). */
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

        /* Descriptors and available array need to be set before we expose the
         * new available array entries. */
        virtio_wmb(vq->weak_barriers);
        vq->split.avail_idx_shadow++;
        vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                vq->split.avail_idx_shadow);
        /* Try __ndelay(x) here as Michael suggested
         *
         * Work:      __ndelay(200);    possiblly make it hard to reproduce
         * Broken:    __ndelay(100);
         * Broken:    __ndelay(20);
         * Broken:    __ndelay(10);
         */
        __ndelay(200);
Quoted 12 lines
Quoted 12 lines
         vq->num_added++;

         pr_debug("Added buffer head %i to %p\n", head, vq);
         END_USE(vq);
         :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
     Instruction           Range of used time in ns
     ----------------------------------------------
     __smp_wmb()           [32  1128032]
     __smp_mb()            [32  1160096]
     __mb()                [32  1162496]

Thanks,
Gavin

#21 lore raw
Quoted 83 lines · Gavin Shan
Quoted 63 lines · Michael S. Tsirkin
Quoted 63 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();
         // Work:   __ndelay(100);
         // Work:   __ndelay(10);
         // Broken: __ndelay(9);
Quoted 6 lines
Quoted 3 lines
        vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);

What if you stick __ndelay here?

       /* Put entry in available array (but don't update avail->idx until they
         * do sync). */
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

        /* Descriptors and available array need to be set before we expose the
         * new available array entries. */
        virtio_wmb(vq->weak_barriers);
        vq->split.avail_idx_shadow++;
        vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                vq->split.avail_idx_shadow);
Quoted 8 lines
        /* Try __ndelay(x) here as Michael suggested
         *
         * Work:      __ndelay(200);    possiblly make it hard to reproduce
         * Broken:    __ndelay(100);
         * Broken:    __ndelay(20);
         * Broken:    __ndelay(10);
         */
        __ndelay(200);

So we see that just changing the timing masks the race. What are you using on the host side? vhost or qemu?

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines

         vq->num_added++;

         pr_debug("Added buffer head %i to %p\n", head, vq);
         END_USE(vq);
         :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
     Instruction           Range of used time in ns
     ----------------------------------------------
     __smp_wmb()           [32  1128032]
     __smp_mb()            [32  1160096]
     __mb()                [32  1162496]

Thanks,
Gavin

author
#22 lore raw
Quoted 93 lines · Michael S. Tsirkin
Quoted 83 lines · Gavin Shan
Quoted 63 lines · Michael S. Tsirkin
Quoted 63 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
    	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
    	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
    	vq->split.avail_idx_shadow++;
    	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
    						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
      :
          /* Put entry in available array (but don't update avail->idx until they
           * do sync). */
          avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
          vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

          /* Descriptors and available array need to be set before we expose the
           * new available array entries. */
          // Broken: virtio_wmb(vq->weak_barriers);
          // Broken: __dma_mb();
          // Work:   __mb();
          // Work:   __smp_mb();
          // Work:   __ndelay(100);
          // Work:   __ndelay(10);
          // Broken: __ndelay(9);
Quoted 6 lines
Quoted 3 lines
         vq->split.avail_idx_shadow++;
          vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                  vq->split.avail_idx_shadow);

What if you stick __ndelay here?

        /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         virtio_wmb(vq->weak_barriers);
         vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);
Quoted 8 lines
         /* Try __ndelay(x) here as Michael suggested
          *
          * Work:      __ndelay(200);    possiblly make it hard to reproduce
          * Broken:    __ndelay(100);
          * Broken:    __ndelay(20);
          * Broken:    __ndelay(10);
          */
         __ndelay(200);
Quoted 3 lines

So we see that just changing the timing masks the race. What are you using on the host side? vhost or qemu?

__ndelay(200) may make the issue harder to be reproduce as I understand. More delays here will give vhost relief, reducing the race.

The issue is only reproducible when vhost is turned on. Otherwise, we aren't able to hit the issue.

   -netdev tap,id=vnet0,vhost=true,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown \
   -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

          vq->num_added++;

          pr_debug("Added buffer head %i to %p\n", head, vq);
          END_USE(vq);
          :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
      Instruction           Range of used time in ns
      ----------------------------------------------
      __smp_wmb()           [32  1128032]
      __smp_mb()            [32  1160096]
      __mb()                [32  1162496]

Thanks,
Gavin

#23 lore raw
Quoted 99 lines · Gavin Shan
Quoted 93 lines · Michael S. Tsirkin
Quoted 83 lines · Gavin Shan
Quoted 63 lines · Michael S. Tsirkin
Quoted 63 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
    	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
    	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
    	vq->split.avail_idx_shadow++;
    	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
    						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
      :
          /* Put entry in available array (but don't update avail->idx until they
           * do sync). */
          avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
          vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

          /* Descriptors and available array need to be set before we expose the
           * new available array entries. */
          // Broken: virtio_wmb(vq->weak_barriers);
          // Broken: __dma_mb();
          // Work:   __mb();
          // Work:   __smp_mb();
          // Work:   __ndelay(100);
          // Work:   __ndelay(10);
          // Broken: __ndelay(9);
Quoted 6 lines
Quoted 3 lines
         vq->split.avail_idx_shadow++;
          vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                  vq->split.avail_idx_shadow);

What if you stick __ndelay here?

        /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         virtio_wmb(vq->weak_barriers);
         vq->split.avail_idx_shadow++;
         vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                 vq->split.avail_idx_shadow);
Quoted 8 lines
         /* Try __ndelay(x) here as Michael suggested
          *
          * Work:      __ndelay(200);    possiblly make it hard to reproduce
          * Broken:    __ndelay(100);
          * Broken:    __ndelay(20);
          * Broken:    __ndelay(10);
          */
         __ndelay(200);
Quoted 3 lines

So we see that just changing the timing masks the race. What are you using on the host side? vhost or qemu?

Quoted 8 lines

__ndelay(200) may make the issue harder to be reproduce as I understand. More delays here will give vhost relief, reducing the race.

The issue is only reproducible when vhost is turned on. Otherwise, we aren't able to hit the issue.

   -netdev tap,id=vnet0,vhost=true,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown \
   -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0

Given it's vhost, it's also possible that the issue is host side. I wonder what happens if we stick a delay or a stronger barrier in vhost.c - either here:

        /* Make sure buffer is written before we update index. */
        smp_wmb();      

or here:

                /* Only get avail ring entries after they have been
                 * exposed by guest.
                 */
                smp_rmb();

?

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

          vq->num_added++;

          pr_debug("Added buffer head %i to %p\n", head, vq);
          END_USE(vq);
          :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
      Instruction           Range of used time in ns
      ----------------------------------------------
      __smp_wmb()           [32  1128032]
      __smp_mb()            [32  1160096]
      __mb()                [32  1162496]

Thanks,
Gavin

author
#24 lore raw
Quoted 118 lines · Michael S. Tsirkin
Quoted 99 lines · Gavin Shan
Quoted 93 lines · Michael S. Tsirkin
Quoted 83 lines · Gavin Shan
Quoted 63 lines · Michael S. Tsirkin
Quoted 63 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
     	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
     	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
     	vq->split.avail_idx_shadow++;
     	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
     						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
       :
           /* Put entry in available array (but don't update avail->idx until they
            * do sync). */
           avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
           vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

           /* Descriptors and available array need to be set before we expose the
            * new available array entries. */
           // Broken: virtio_wmb(vq->weak_barriers);
           // Broken: __dma_mb();
           // Work:   __mb();
           // Work:   __smp_mb();
           // Work:   __ndelay(100);
           // Work:   __ndelay(10);
           // Broken: __ndelay(9);
Quoted 6 lines
Quoted 3 lines
          vq->split.avail_idx_shadow++;
           vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                   vq->split.avail_idx_shadow);

What if you stick __ndelay here?

         /* Put entry in available array (but don't update avail->idx until they
           * do sync). */
          avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
          vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

          /* Descriptors and available array need to be set before we expose the
           * new available array entries. */
          virtio_wmb(vq->weak_barriers);
          vq->split.avail_idx_shadow++;
          vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                  vq->split.avail_idx_shadow);
Quoted 8 lines
          /* Try __ndelay(x) here as Michael suggested
           *
           * Work:      __ndelay(200);    possiblly make it hard to reproduce
           * Broken:    __ndelay(100);
           * Broken:    __ndelay(20);
           * Broken:    __ndelay(10);
           */
          __ndelay(200);
Quoted 3 lines

So we see that just changing the timing masks the race. What are you using on the host side? vhost or qemu?

Quoted 8 lines

__ndelay(200) may make the issue harder to be reproduce as I understand. More delays here will give vhost relief, reducing the race.

The issue is only reproducible when vhost is turned on. Otherwise, we aren't able to hit the issue.

    -netdev tap,id=vnet0,vhost=true,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown \
    -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0

Given it's vhost, it's also possible that the issue is host side. I wonder what happens if we stick a delay or a stronger barrier in vhost.c - either here:

         /* Make sure buffer is written before we update index. */
         smp_wmb();
Quoted 9 lines

or here:

                 /* Only get avail ring entries after they have been
                  * exposed by guest.
                  */
                 smp_rmb();

?

It's possible. However, I still can hit the issue after both of them are replaed with '__mb()'. So the issue seems on the guest side, where the written data isn't observed in time by the CPU on far end (for vhost worker).

drivers/vhost/vhost.c +5 -2
index 045f666b4f12..327b68d21b02 100644
@@ -2529,7 +2529,8 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
                /* Only get avail ring entries after they have been
                 * exposed by guest.
                 */
-               smp_rmb();
+               // smp_rmb();
+               __mb();
        }
 
        /* Grab the next descriptor number they're advertising, and increment
@@ -2703,7 +2704,9 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
        r = __vhost_add_used_n(vq, heads, count);
 
        /* Make sure buffer is written before we update index. */
-       smp_wmb();
+       // smp_wmb();
+       __mb();
+
 

From the guest:

[   14.102608] virtio_net virtio0: output.0:id 80 is not a head!
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

           vq->num_added++;

           pr_debug("Added buffer head %i to %p\n", head, vq);
           END_USE(vq);
           :
}

I also tried to measure the consumed time for various barrier-relative instructions using ktime_get_ns() which should have consumed most of the time. __smb_mb() is slower than __smp_wmb() but faster than __mb()

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
       Instruction           Range of used time in ns
       ----------------------------------------------
       __smp_wmb()           [32  1128032]
       __smp_mb()            [32  1160096]
       __mb()                [32  1162496]

Thanks,
Gavin

#25 lore raw
Quoted 156 lines · Gavin Shan
Quoted 118 lines · Michael S. Tsirkin
Quoted 99 lines · Gavin Shan
Quoted 93 lines · Michael S. Tsirkin
Quoted 83 lines · Gavin Shan
Quoted 63 lines · Michael S. Tsirkin
Quoted 63 lines
Quoted 26 lines
Quoted 12 lines
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
     	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
     	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
     	vq->split.avail_idx_shadow++;
     	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
     						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

{
       :
           /* Put entry in available array (but don't update avail->idx until they
            * do sync). */
           avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
           vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

           /* Descriptors and available array need to be set before we expose the
            * new available array entries. */
           // Broken: virtio_wmb(vq->weak_barriers);
           // Broken: __dma_mb();
           // Work:   __mb();
           // Work:   __smp_mb();
           // Work:   __ndelay(100);
           // Work:   __ndelay(10);
           // Broken: __ndelay(9);
Quoted 6 lines
Quoted 3 lines
          vq->split.avail_idx_shadow++;
           vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                   vq->split.avail_idx_shadow);

What if you stick __ndelay here?

         /* Put entry in available array (but don't update avail->idx until they
           * do sync). */
          avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
          vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

          /* Descriptors and available array need to be set before we expose the
           * new available array entries. */
          virtio_wmb(vq->weak_barriers);
          vq->split.avail_idx_shadow++;
          vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
                                                  vq->split.avail_idx_shadow);
Quoted 8 lines
          /* Try __ndelay(x) here as Michael suggested
           *
           * Work:      __ndelay(200);    possiblly make it hard to reproduce
           * Broken:    __ndelay(100);
           * Broken:    __ndelay(20);
           * Broken:    __ndelay(10);
           */
          __ndelay(200);
Quoted 3 lines

So we see that just changing the timing masks the race. What are you using on the host side? vhost or qemu?

Quoted 8 lines

__ndelay(200) may make the issue harder to be reproduce as I understand. More delays here will give vhost relief, reducing the race.

The issue is only reproducible when vhost is turned on. Otherwise, we aren't able to hit the issue.

    -netdev tap,id=vnet0,vhost=true,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown \
    -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0

Given it's vhost, it's also possible that the issue is host side. I wonder what happens if we stick a delay or a stronger barrier in vhost.c - either here:

         /* Make sure buffer is written before we update index. */
         smp_wmb();
Quoted 9 lines

or here:

                 /* Only get avail ring entries after they have been
                  * exposed by guest.
                  */
                 smp_rmb();

?

It's possible. However, I still can hit the issue after both of them are replaed with '__mb()'. So the issue seems on the guest side, where the written data isn't observed in time by the CPU on far end (for vhost worker).

drivers/vhost/vhost.c +5 -2
index 045f666b4f12..327b68d21b02 100644
@@ -2529,7 +2529,8 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
                /* Only get avail ring entries after they have been
                 * exposed by guest.
                 */
-               smp_rmb();
+               // smp_rmb();
+               __mb();
        }
        /* Grab the next descriptor number they're advertising, and increment
@@ -2703,7 +2704,9 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
        r = __vhost_add_used_n(vq, heads, count);
        /* Make sure buffer is written before we update index. */
-       smp_wmb();
+       // smp_wmb();
+       __mb();
+
 

From the guest:

Quoted 1 line
[   14.102608] virtio_net virtio0: output.0:id 80 is not a head!

You realize however that for that to happen, first vhost has to use the wrong index?
Again the fact tweaking code helps is suggestive but does not lead us to the root cause.

So you should be able to detect the bad index when vhost gets it. Here's a suggestion: stick a valid flag in desc.
then vhost should notice if it gets a desc with that flag clear. Like this maybe?

drivers/virtio/virtio_ring.c +4 -1
index 6f7e5010a673..2789e51b57e6 100644
@@ -524,7 +524,7 @@ static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
 	struct vring_desc_extra *extra = vring->split.desc_extra;
 	u16 next;
 
-	desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
+	desc[i].flags = cpu_to_virtio16(vq->vdev, flags | (0x1 << 5)); /* Mark it valid */
 	desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
 	desc[i].len = cpu_to_virtio32(vq->vdev, len);
 
@@ -721,6 +721,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
 		} else
 			i = vring_unmap_one_split(vq, i);
+		desc[i].flags = 0x0;
 	}
 
 free_indirect:
@@ -775,11 +776,13 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
 	i = head;
 
 	while (vq->split.vring.desc[i].flags & nextflag) {
+		vq->split.vring.desc[i].flags = 0x0;
 		vring_unmap_one_split(vq, i);
 		i = vq->split.desc_extra[i].next;
 		vq->vq.num_free++;
 	}
 
+	vq->split.vring.desc[i].flags = 0x0;
 	vring_unmap_one_split(vq, i);
 	vq->split.desc_extra[i].next = vq->free_head;
 	vq->free_head = head;
#26 lore raw
In reply to Gavin Shan
Quoted 83 lines · Gavin Shan
Quoted 62 lines · Will Deacon
Quoted 47 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

   /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
   -accel kvm -machine virt,gic-version=host                            \
      :                                                                 \
   -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

   [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
  drivers/virtio/virtio_ring.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
  	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
  	vq->split.avail_idx_shadow++;
  	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
  						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

Quoted 13 lines
{
    :
        /* Put entry in available array (but don't update avail->idx until they
         * do sync). */
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

        /* Descriptors and available array need to be set before we expose the
         * new available array entries. */
        // Broken: virtio_wmb(vq->weak_barriers);
        // Broken: __dma_mb();
        // Work:   __mb();
        // Work:   __smp_mb();

It's pretty weird that __dma_mb() is "broken" but __smp_mb() "works". How confident are you in that result?

Will

author
#27 lore raw
Quoted 65 lines · Will Deacon
Quoted 50 lines · Gavin Shan
Quoted 29 lines · Will Deacon
Quoted 15 lines
   drivers/virtio/virtio_ring.c | 12 +++++++++---
   1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

Quoted 13 lines
{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();
Quoted 3 lines

It's pretty weird that __dma_mb() is "broken" but __smp_mb() "works". How confident are you in that result?

Yes, __dma_mb() is even stronger than __smp_mb(). I retried the test, showing that both __dma_mb() and __smp_mb() work for us. I had too many tests yesterday and something may have been messed up.

Instruction Hitting times in 10 tests
---------------------------------------------

__smp_wmb()         8
__smp_mb()          0
__dma_wmb()         7
__dma_mb()          0
__mb()              0
__wmb()             0

It's strange that __smp_mb() works, but __smp_wmb() fails. It seems we need a read barrier here. I will try WRITE_ONCE() + __smp_wmb() as suggested by Michael in another reply. Will update the result soon.

Thanks,
Gavin

#28 lore raw
Quoted 83 lines · Gavin Shan
Quoted 65 lines · Will Deacon
Quoted 50 lines · Gavin Shan
Quoted 29 lines · Will Deacon
Quoted 15 lines
   drivers/virtio/virtio_ring.c | 12 +++++++++---
   1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
   	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 700

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
   	vq->split.avail_idx_shadow++;
   	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
   						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 8 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Thanks for your comments, Will.

Yes, DMB should work for us. However, it seems this instruction has issues on NVidia's grace-hopper. It's hard for me to understand how DMB and DSB works from hardware level. I agree it's not the solution to replace DMB with DSB before we fully understand the root cause.

I tried the possible replacement like below. __smp_mb() can avoid the issue like __mb() does. __ndelay(10) can avoid the issue, but __ndelay(9) doesn't.

static inline int virtqueue_add_split(struct virtqueue *_vq, ...)

Quoted 13 lines
{
     :
         /* Put entry in available array (but don't update avail->idx until they
          * do sync). */
         avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
         vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);

         /* Descriptors and available array need to be set before we expose the
          * new available array entries. */
         // Broken: virtio_wmb(vq->weak_barriers);
         // Broken: __dma_mb();
         // Work:   __mb();
         // Work:   __smp_mb();
Quoted 3 lines

It's pretty weird that __dma_mb() is "broken" but __smp_mb() "works". How confident are you in that result?

Yes, __dma_mb() is even stronger than __smp_mb(). I retried the test, showing that both __dma_mb() and __smp_mb() work for us. I had too many tests yesterday and something may have been messed up.

Instruction Hitting times in 10 tests
---------------------------------------------

__smp_wmb()         8
__smp_mb()          0
__dma_wmb()         7
__dma_mb()          0
__mb()              0
__wmb()             0
Quoted 6 lines

It's strange that __smp_mb() works, but __smp_wmb() fails. It seems we need a read barrier here. I will try WRITE_ONCE() + __smp_wmb() as suggested by Michael in another reply. Will update the result soon.

Thanks,
Gavin

I think you are wasting the time with these tests. Even if it helps what does this tell us? Try setting a flag as I suggested elsewhere. Then check it in vhost.
Or here's another idea - possibly easier. Copy the high bits from index into ring itself. Then vhost can check that head is synchronized with index.

Warning: completely untested, not even compiled. But should give you the idea. If this works btw we should consider making this official in the spec.

static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq,

drivers/virtio/virtio_ring.c +2 -1
index 6f7e5010a673..79456706d0bd 100644
@@ -685,7 +685,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	/* Put entry in available array (but don't update avail->idx until they
 	 * do sync). */
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
-	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
+	u16 headwithflag = head | (q->split.avail_idx_shadow & ~(vq->split.vring.num - 1));
+	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, headwithflag);
 
 	/* Descriptors and available array need to be set before we expose the
 	 * new available array entries. */
drivers/vhost/vhost.c +8 -2
index 045f666b4f12..bd8f7c763caa 100644
@@ -1299,8 +1299,15 @@ static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq,
 static inline int vhost_get_avail_head(struct vhost_virtqueue *vq,
 				       __virtio16 *head, int idx)
 {
-	return vhost_get_avail(vq, *head,
+	unsigned i = idx;
+	unsigned flag = i & ~(vq->num - 1);
+	unsigned val = vhost_get_avail(vq, *head,
 			       &vq->avail->ring[idx & (vq->num - 1)]);
+	unsigned valflag = val & ~(vq->num - 1);
+
+	WARN_ON(valflag != flag);
+
+	return val & (vq->num - 1);
 }
 
-- 

MST

author
#29 lore raw

On 3/20/24 10:49, Michael S. Tsirkin wrote:>

Quoted 38 lines

I think you are wasting the time with these tests. Even if it helps what does this tell us? Try setting a flag as I suggested elsewhere. Then check it in vhost.
Or here's another idea - possibly easier. Copy the high bits from index into ring itself. Then vhost can check that head is synchronized with index.

Warning: completely untested, not even compiled. But should give you the idea. If this works btw we should consider making this official in the spec.

static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq,

drivers/virtio/virtio_ring.c +2 -1
index 6f7e5010a673..79456706d0bd 100644
@@ -685,7 +685,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	/* Put entry in available array (but don't update avail->idx until they
  	 * do sync). */
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
-	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
+	u16 headwithflag = head | (q->split.avail_idx_shadow & ~(vq->split.vring.num - 1));
+	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, headwithflag);
 
  	/* Descriptors and available array need to be set before we expose the
  	 * new available array entries. */
drivers/vhost/vhost.c +1 -1
index 045f666b4f12..bd8f7c763caa 100644
@@ -1299,8 +1299,15 @@ static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq,
  static inline int vhost_get_avail_head(struct vhost_virtqueue *vq,
  				       __virtio16 *head, int idx)
  {
-	return vhost_get_avail(vq, *head,
+	unsigned i = idx;

drivers/vhost/vhost.c static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq, · line 1312

Quoted 10 lines
drivers/vhost/vhost.c +7 -0
+	unsigned flag = i & ~(vq->num - 1);
+	unsigned val = vhost_get_avail(vq, *head,
  			       &vq->avail->ring[idx & (vq->num - 1)]);
+	unsigned valflag = val & ~(vq->num - 1);
+
+	WARN_ON(valflag != flag);
+
+	return val & (vq->num - 1);
  }
 

Thanks, Michael. The code is already self-explanatory. Since vq->num is 256, I just squeezed the last_avail_idx to the high byte. Unfortunately, I'm unable to hit the WARN_ON(). Does it mean the low byte is stale (or corrupted) while the high byte is still correct and valid?

        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] =
                cpu_to_virtio16(_vq->vdev, head | (avail << 8));


        head = vhost16_to_cpu(vq, ring_head);
        WARN_ON((head >> 8) != (vq->last_avail_idx % vq->num));
        head = head & 0xff;

One question: Does QEMU has any chance writing data to the available queue when vhost is enabled? My previous understanding is no, the queue is totally owned by vhost instead of QEMU.

Before this patch was posted, I had debugging code to record last 16 transactions to the available and used queue from guest and host side. It did reveal the wrong head was fetched from the available queue.

[   11.785745] ================ virtqueue_get_buf_ctx_split ================
[   11.786238] virtio_net virtio0: output.0:id 74 is not a head!
[   11.786655] head to be released: 036 077
[   11.786952]
[   11.786952] avail_idx:
[   11.787234] 000  63985  <--
[   11.787237] 001  63986
[   11.787444] 002  63987
[   11.787632] 003  63988
[   11.787821] 004  63989
[   11.788006] 005  63990
[   11.788194] 006  63991
[   11.788381] 007  63992
[   11.788567] 008  63993
[   11.788772] 009  63994
[   11.788957] 010  63995
[   11.789141] 011  63996
[   11.789327] 012  63997
[   11.789515] 013  63998
[   11.789701] 014  63999
[   11.789886] 015  64000
[   11.790068]
[   11.790068] avail_head:
[   11.790529] 000  075  <--
[   11.790718] 001  036
[   11.790890] 002  077
[   11.791061] 003  129
[   11.791231] 004  072
[   11.791400] 005  130
[   11.791574] 006  015
[   11.791748] 007  074
[   11.791918] 008  130
[   11.792094] 009  130
[   11.792263] 010  074
[   11.792437] 011  015
[   11.792617] 012  072
[   11.792788] 013  129
[   11.792961] 014  077    // The last two heads from guest to host: 077, 036
[   11.793134] 015  036

[root@nvidia-grace-hopper-05 qemu.main]# cat /proc/vhost

avail_idx

000  63998
001  64000
002  63954  <---
003  63955
004  63956
005  63974
006  63981
007  63984
008  63986
009  63987
010  63988
011  63989
012  63992
013  63993
014  63995
015  63997

avail_head

000  074
001  015
002  072
003  129
004  074            // The last two heads seen by vhost is: 074, 036
005  036
006  075  <---
007  036
008  077
009  129
010  072
011  130
012  015
013  074
014  130
015  130

used_idx

000  64000
001  63882  <---
002  63889
003  63891
004  63898
005  63936
006  63942
007  63946
008  63949
009  63953
010  63957
011  63981
012  63990
013  63992
014  63993
015  63999

used_head

000  072
001  129
002  074          // The last two heads published to guest is: 074, 036
003  036
004  075  <---
005  036
006  077
007  129
008  072
009  130
010  015
011  074
012  130
013  130
014  074
015  015

Thanks,
Gavin

#30 lore raw
Quoted 41 lines · Gavin Shan

On 3/20/24 10:49, Michael S. Tsirkin wrote:>

Quoted 40 lines

I think you are wasting the time with these tests. Even if it helps what does this tell us? Try setting a flag as I suggested elsewhere. Then check it in vhost.
Or here's another idea - possibly easier. Copy the high bits from index into ring itself. Then vhost can check that head is synchronized with index.

Warning: completely untested, not even compiled. But should give you the idea. If this works btw we should consider making this official in the spec.

static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq,

drivers/virtio/virtio_ring.c +2 -1
index 6f7e5010a673..79456706d0bd 100644
@@ -685,7 +685,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
  	/* Put entry in available array (but don't update avail->idx until they
  	 * do sync). */
  	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
-	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
+	u16 headwithflag = head | (q->split.avail_idx_shadow & ~(vq->split.vring.num - 1));
+	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, headwithflag);
  	/* Descriptors and available array need to be set before we expose the
  	 * new available array entries. */
 
drivers/vhost/vhost.c +3 -1
index 045f666b4f12..bd8f7c763caa 100644
@@ -1299,8 +1299,15 @@ static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq,
  static inline int vhost_get_avail_head(struct vhost_virtqueue *vq,
  				       __virtio16 *head, int idx)
  {
-	return vhost_get_avail(vq, *head,
+	unsigned i = idx;
+	unsigned flag = i & ~(vq->num - 1);
+	unsigned val = vhost_get_avail(vq, *head,
  			       &vq->avail->ring[idx & (vq->num - 1)]);
Quoted 8 lines

drivers/vhost/vhost.c static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq, · line 1311

Quoted 6 lines
drivers/vhost/vhost.c +5 -0
+	unsigned valflag = val & ~(vq->num - 1);
+
+	WARN_ON(valflag != flag);
+
+	return val & (vq->num - 1);
  }

Thanks, Michael. The code is already self-explanatory.

Apparently not. See below.

Quoted 4 lines

Since vq->num is 256, I just
squeezed the last_avail_idx to the high byte. Unfortunately, I'm unable to hit the WARN_ON(). Does it mean the low byte is stale (or corrupted) while the high byte is still correct and valid?

I would find this very surprising.

Quoted 8 lines
        avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
        vq->split.vring.avail->ring[avail] =
                cpu_to_virtio16(_vq->vdev, head | (avail << 8));


        head = vhost16_to_cpu(vq, ring_head);
        WARN_ON((head >> 8) != (vq->last_avail_idx % vq->num));
        head = head & 0xff;

This code misses the point of the test.
The high value you store now is exactly the same each time you go around the ring. E.g. at beginning of ring you now always store 0 as high byte. So a stale value will not be detected/ The high value you store now is exactly the same each time you go around the ring. E.g. at beginning of ring you now always store 0 as high byte. So a stale value will not be detected.

The value you are interested in should change each time you go around the ring a full circle. Thus you want exactly the *high byte* of avail idx - this is what my patch did - your patch instead stored and compared the low byte.

The advantage of this debugging patch is that it will detect the issue immediately not after guest detected the problem in the used ring.
For example, you can add code to re-read the value, or dump the whole ring.

Quoted 3 lines

One question: Does QEMU has any chance writing data to the available queue when vhost is enabled? My previous understanding is no, the queue is totally owned by vhost instead of QEMU.

It shouldn't do it normally.

Quoted 3 lines

Before this patch was posted, I had debugging code to record last 16 transactions to the available and used queue from guest and host side. It did reveal the wrong head was fetched from the available queue.

Oh nice that's a very good hint. And is this still reproducible?

Quoted 31 lines
[   11.785745] ================ virtqueue_get_buf_ctx_split ================
[   11.786238] virtio_net virtio0: output.0:id 74 is not a head!
[   11.786655] head to be released: 036 077
[   11.786952]
[   11.786952] avail_idx:
[   11.787234] 000  63985  <--
[   11.787237] 001  63986
[   11.787444] 002  63987
[   11.787632] 003  63988
[   11.787821] 004  63989
[   11.788006] 005  63990
[   11.788194] 006  63991
[   11.788381] 007  63992
[   11.788567] 008  63993
[   11.788772] 009  63994
[   11.788957] 010  63995
[   11.789141] 011  63996
[   11.789327] 012  63997
[   11.789515] 013  63998
[   11.789701] 014  63999
[   11.789886] 015  64000
[   11.790068]
[   11.790068] avail_head:
[   11.790529] 000  075  <--
[   11.790718] 001  036
[   11.790890] 002  077
[   11.791061] 003  129
[   11.791231] 004  072
[   11.791400] 005  130
[   11.791574] 006  015
[   11.791748] 007  074
Quoted 8 lines
[   11.791918] 008  130
[   11.792094] 009  130
[   11.792263] 010  074
[   11.792437] 011  015
[   11.792617] 012  072
[   11.792788] 013  129
[   11.792961] 014  077    // The last two heads from guest to host: 077, 036
[   11.793134] 015  036

Maybe dump the avail ring from guest to make sure
it matches the expected contents?

Quoted 20 lines

[root@nvidia-grace-hopper-05 qemu.main]# cat /proc/vhost

avail_idx

000  63998
001  64000
002  63954  <---
003  63955
004  63956
005  63974
006  63981
007  63984
008  63986
009  63987
010  63988
011  63989
012  63992
013  63993
014  63995
015  63997
Quoted 8 lines

avail_head

000  074
001  015
002  072
003  129
004  074            // The last two heads seen by vhost is: 074, 036
005  036
006  075  <---

And is 074 the previous (stale) value in the ring?

Quoted 48 lines
007  036
008  077
009  129
010  072
011  130
012  015
013  074
014  130
015  130

used_idx

000  64000
001  63882  <---
002  63889
003  63891
004  63898
005  63936
006  63942
007  63946
008  63949
009  63953
010  63957
011  63981
012  63990
013  63992
014  63993
015  63999

used_head

000  072
001  129
002  074          // The last two heads published to guest is: 074, 036
003  036
004  075  <---
005  036
006  077
007  129
008  072
009  130
010  015
011  074
012  130
013  130
014  074
015  015
Quoted 2 lines

Thanks,
Gavin

I like this debugging patch, it might make sense to polish it up and include in production. We'll want it in debugfs naturally not /proc/vhost.

But all in good time.

Quoted 2 lines
author
#31 lore raw
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Gavin Shan

On 3/20/24 10:49, Michael S. Tsirkin wrote:>

Quoted 5 lines
drivers/virtio/virtio_ring.c +0 -0
index 6f7e5010a673..79456706d0bd 100644

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 692

Quoted 9 lines
drivers/virtio/virtio_ring.c +2 -1
@@ -685,7 +685,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	/* Put entry in available array (but don't update avail->idx until they
   	 * do sync). */
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
-	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
+	u16 headwithflag = head | (q->split.avail_idx_shadow & ~(vq->split.vring.num - 1));
+	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, headwithflag);
   	/* Descriptors and available array need to be set before we expose the
   	 * new available array entries. */
 

Ok, Michael. I continued with my debugging code. It still looks like a hardware bug on NVidia's grace-hopper. I really think NVidia needs to be involved for the discussion, as suggested by you.

Firstly, I bind the vhost process and vCPU thread to CPU#71 and CPU#70. Note that I have only one vCPU in my configuration.

Secondly, the debugging code is enhanced so that the available head for (last_avail_idx - 1) is read for twice and recorded. It means the available head for one specific available index is read for twice. I do see the available heads are different from the consecutive reads. More details are shared as below.

From the guest side
===================

virtio_net virtio0: output.0:id 86 is not a head!
head to be released: 047 062 112

avail_idx:
000 49665
001 49666 <--
:
015 49664

avail_head:
000 062
001 047 <--
:
015 112

From the host side
==================

avail_idx
000 49663
001 49666 <---
:

avail_head

000  062  (062)
001  047  (047)  <---
 :
015  086  (112)          // head 086 is returned from the first read,
                         // but head 112 is returned from the second read

vhost_get_vq_desc: Inconsistent head in two read (86 -> 112) for avail_idx 49664

Thanks,
Gavin

#32 lore raw
Quoted 14 lines · Gavin Shan
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Gavin Shan

On 3/20/24 10:49, Michael S. Tsirkin wrote:>

Quoted 10 lines
drivers/virtio/virtio_ring.c +1 -1
index 6f7e5010a673..79456706d0bd 100644
@@ -685,7 +685,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	/* Put entry in available array (but don't update avail->idx until they
   	 * do sync). */
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
-	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
+	u16 headwithflag = head | (q->split.avail_idx_shadow & ~(vq->split.vring.num - 1));
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq,

Quoted 4 lines
drivers/virtio/virtio_ring.c +1 -0
+	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, headwithflag);
   	/* Descriptors and available array need to be set before we expose the
   	 * new available array entries. */
 

Ok, Michael. I continued with my debugging code. It still looks like a hardware bug on NVidia's grace-hopper. I really think NVidia needs to be involved for the discussion, as suggested by you.

Do you have a support contact at Nvidia to report this?

Quoted 2 lines

Firstly, I bind the vhost process and vCPU thread to CPU#71 and CPU#70. Note that I have only one vCPU in my configuration.

Interesting but is guest built with CONFIG_SMP set?

Quoted 9 lines

Secondly, the debugging code is enhanced so that the available head for (last_avail_idx - 1) is read for twice and recorded. It means the available head for one specific available index is read for twice. I do see the available heads are different from the consecutive reads. More details are shared as below.

From the guest side
===================

Quoted 8 lines

virtio_net virtio0: output.0:id 86 is not a head!
head to be released: 047 062 112

avail_idx:
000 49665
001 49666 <--
:
015 49664

what are these #s 49665 and so on?
and how large is the ring?
I am guessing 49664 is the index ring size is 16 and 49664 % 16 == 0

Quoted 1 line

avail_head:

is this the avail ring contents?

Quoted 4 lines

000 062
001 047 <--
:
015 112

What are these arrows pointing at, btw?

Quoted 15 lines

From the host side
==================

avail_idx
000 49663
001 49666 <---
:

avail_head

000  062  (062)
001  047  (047)  <---
 :
015  086  (112)          // head 086 is returned from the first read,
                         // but head 112 is returned from the second read
Quoted 4 lines

vhost_get_vq_desc: Inconsistent head in two read (86 -> 112) for avail_idx 49664

Thanks,
Gavin

OK thanks so this proves it is actually the avail ring value.

Signature · 1 line
MST
#33 lore raw
Quoted 24 lines · Michael S. Tsirkin
Quoted 14 lines · Gavin Shan
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Gavin Shan

On 3/20/24 10:49, Michael S. Tsirkin wrote:>

Quoted 10 lines
drivers/virtio/virtio_ring.c +1 -1
index 6f7e5010a673..79456706d0bd 100644
@@ -685,7 +685,8 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
   	/* Put entry in available array (but don't update avail->idx until they
   	 * do sync). */
   	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
-	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
+	u16 headwithflag = head | (q->split.avail_idx_shadow & ~(vq->split.vring.num - 1));
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq,

Quoted 4 lines
drivers/virtio/virtio_ring.c +1 -0
+	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, headwithflag);
   	/* Descriptors and available array need to be set before we expose the
   	 * new available array entries. */
 

Ok, Michael. I continued with my debugging code. It still looks like a hardware bug on NVidia's grace-hopper. I really think NVidia needs to be involved for the discussion, as suggested by you.

Quoted 6 lines

Do you have a support contact at Nvidia to report this?

Quoted 2 lines

Firstly, I bind the vhost process and vCPU thread to CPU#71 and CPU#70. Note that I have only one vCPU in my configuration.

Interesting but is guest built with CONFIG_SMP set?

arm64 is always built CONFIG_SMP.

Quoted 18 lines
Quoted 9 lines

Secondly, the debugging code is enhanced so that the available head for (last_avail_idx - 1) is read for twice and recorded. It means the available head for one specific available index is read for twice. I do see the available heads are different from the consecutive reads. More details are shared as below.

From the guest side
===================

Quoted 8 lines

virtio_net virtio0: output.0:id 86 is not a head!
head to be released: 047 062 112

avail_idx:
000 49665
001 49666 <--
:
015 49664

Quoted 4 lines

what are these #s 49665 and so on?
and how large is the ring?
I am guessing 49664 is the index ring size is 16 and 49664 % 16 == 0

More than that, 49664 % 256 == 0

So again there seems to be an error in the vicinity of roll-over of the idx low byte, as I observed in the earlier log. Surely this is more than coincidence?

-- Keir

Quoted 39 lines
Quoted 1 line

avail_head:

is this the avail ring contents?

Quoted 4 lines

000 062
001 047 <--
:
015 112

What are these arrows pointing at, btw?

Quoted 15 lines

From the host side
==================

avail_idx
000 49663
001 49666 <---
:

avail_head

000  062  (062)
001  047  (047)  <---
 :
015  086  (112)          // head 086 is returned from the first read,
                         // but head 112 is returned from the second read
Quoted 4 lines

vhost_get_vq_desc: Inconsistent head in two read (86 -> 112) for avail_idx 49664

Thanks,
Gavin

OK thanks so this proves it is actually the avail ring value.

Signature · 1 line
MST
#34 lore raw
Quoted 24 lines · Keir Fraser
Quoted 18 lines · Michael S. Tsirkin
Quoted 9 lines

Secondly, the debugging code is enhanced so that the available head for (last_avail_idx - 1) is read for twice and recorded. It means the available head for one specific available index is read for twice. I do see the available heads are different from the consecutive reads. More details are shared as below.

From the guest side
===================

Quoted 8 lines

virtio_net virtio0: output.0:id 86 is not a head!
head to be released: 047 062 112

avail_idx:
000 49665
001 49666 <--
:
015 49664

Quoted 4 lines

what are these #s 49665 and so on?
and how large is the ring?
I am guessing 49664 is the index ring size is 16 and 49664 % 16 == 0

Quoted 5 lines

More than that, 49664 % 256 == 0

So again there seems to be an error in the vicinity of roll-over of the idx low byte, as I observed in the earlier log. Surely this is more than coincidence?

Yeah, I'd still really like to see the disassembly for both sides of the protocol here. Gavin, is that something you're able to provide? Worst case, the host and guest vmlinux objects would be a starting point.

Personally, I'd be fairly surprised if this was a hardware issue.

Will

#35 lore raw
Quoted 31 lines · Will Deacon
Quoted 24 lines · Keir Fraser
Quoted 18 lines · Michael S. Tsirkin
Quoted 9 lines

Secondly, the debugging code is enhanced so that the available head for (last_avail_idx - 1) is read for twice and recorded. It means the available head for one specific available index is read for twice. I do see the available heads are different from the consecutive reads. More details are shared as below.

From the guest side
===================

Quoted 8 lines

virtio_net virtio0: output.0:id 86 is not a head!
head to be released: 047 062 112

avail_idx:
000 49665
001 49666 <--
:
015 49664

Quoted 4 lines

what are these #s 49665 and so on?
and how large is the ring?
I am guessing 49664 is the index ring size is 16 and 49664 % 16 == 0

Quoted 5 lines

More than that, 49664 % 256 == 0

So again there seems to be an error in the vicinity of roll-over of the idx low byte, as I observed in the earlier log. Surely this is more than coincidence?

Quoted 5 lines

Yeah, I'd still really like to see the disassembly for both sides of the protocol here. Gavin, is that something you're able to provide? Worst case, the host and guest vmlinux objects would be a starting point.

Personally, I'd be fairly surprised if this was a hardware issue.

Ok, long shot after eyeballing the vhost code, but does the diff below help at all? It looks like vhost_vq_avail_empty() can advance the value saved in 'vq->avail_idx' but without the read barrier, possibly confusing vhost_get_vq_desc() in polling mode.

Will

--->8

drivers/vhost/vhost.c +1 -0
index 045f666b4f12..87bff710331a 100644
@@ -2801,6 +2801,7 @@ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
                return false;
        vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
 
+       smp_rmb();
        return vq->avail_idx == vq->last_avail_idx;
 }
 EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
author
#36 lore raw
Quoted 13 lines · Will Deacon

On Tue, Mar 26, 2024 at 11:43:13AM +0000, Will Deacon wrote:

Ok, long shot after eyeballing the vhost code, but does the diff below help at all? It looks like vhost_vq_avail_empty() can advance the value saved in 'vq->avail_idx' but without the read barrier, possibly confusing vhost_get_vq_desc() in polling mode.

drivers/vhost/vhost.c +0 -0
index 045f666b4f12..87bff710331a 100644

drivers/vhost/vhost.c bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) · line 2807

Quoted 7 lines
drivers/vhost/vhost.c +1 -0
@@ -2801,6 +2801,7 @@ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
                 return false;
         vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
 
+       smp_rmb();
         return vq->avail_idx == vq->last_avail_idx;
  }
  EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);

Thanks, Will. I already noticed smp_rmb() has been missed in vhost_vq_avail_empty(). The issue still exists after smp_rmb() is added here. However, I'm inspired by your suggestion and recheck the code again. It seems another smp_rmb() has been missed in vhost_enable_notify().

With smp_rmb() added to vhost_vq_avail_empty() and vhost_enable_notify(), I'm unable to hit the issue. I will try for more times to make sure the issue is really resolved. After that, I will post formal patches for review.

Thanks,
Gavin

author
#37 lore raw
Quoted 26 lines · Gavin Shan
Quoted 12 lines · Will Deacon

On Tue, Mar 26, 2024 at 11:43:13AM +0000, Will Deacon wrote:

Ok, long shot after eyeballing the vhost code, but does the diff below help at all? It looks like vhost_vq_avail_empty() can advance the value saved in 'vq->avail_idx' but without the read barrier, possibly confusing vhost_get_vq_desc() in polling mode.

drivers/vhost/vhost.c +0 -0
index 045f666b4f12..87bff710331a 100644

drivers/vhost/vhost.c bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) · line 2807

Quoted 7 lines
drivers/vhost/vhost.c +1 -0
@@ -2801,6 +2801,7 @@ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
                  return false;
          vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
+       smp_rmb();
          return vq->avail_idx == vq->last_avail_idx;
   }
   EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
 

Thanks, Will. I already noticed smp_rmb() has been missed in vhost_vq_avail_empty(). The issue still exists after smp_rmb() is added here. However, I'm inspired by your suggestion and recheck the code again. It seems another smp_rmb() has been missed in vhost_enable_notify().

Quoted 4 lines

With smp_rmb() added to vhost_vq_avail_empty() and vhost_enable_notify(), I'm unable to hit the issue. I will try for more times to make sure the issue is really resolved. After that, I will post formal patches for review.

Thanks again, Will. The formal patches have been sent for review.

https://lkml.org/lkml/2024/3/27/40

Thanks,
Gavin

#38 lore raw
In reply to Will Deacon
Quoted 52 lines · Will Deacon
Quoted 31 lines · Will Deacon
Quoted 24 lines · Keir Fraser
Quoted 18 lines · Michael S. Tsirkin
Quoted 9 lines

Secondly, the debugging code is enhanced so that the available head for (last_avail_idx - 1) is read for twice and recorded. It means the available head for one specific available index is read for twice. I do see the available heads are different from the consecutive reads. More details are shared as below.

From the guest side
===================

Quoted 8 lines

virtio_net virtio0: output.0:id 86 is not a head!
head to be released: 047 062 112

avail_idx:
000 49665
001 49666 <--
:
015 49664

Quoted 4 lines

what are these #s 49665 and so on?
and how large is the ring?
I am guessing 49664 is the index ring size is 16 and 49664 % 16 == 0

Quoted 5 lines

More than that, 49664 % 256 == 0

So again there seems to be an error in the vicinity of roll-over of the idx low byte, as I observed in the earlier log. Surely this is more than coincidence?

Quoted 5 lines

Yeah, I'd still really like to see the disassembly for both sides of the protocol here. Gavin, is that something you're able to provide? Worst case, the host and guest vmlinux objects would be a starting point.

Personally, I'd be fairly surprised if this was a hardware issue.

Ok, long shot after eyeballing the vhost code, but does the diff below help at all? It looks like vhost_vq_avail_empty() can advance the value saved in 'vq->avail_idx' but without the read barrier, possibly confusing vhost_get_vq_desc() in polling mode.

Will

--->8

drivers/vhost/vhost.c +0 -0
index 045f666b4f12..87bff710331a 100644

drivers/vhost/vhost.c bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) · line 2807

Quoted 7 lines
drivers/vhost/vhost.c +1 -0
@@ -2801,6 +2801,7 @@ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
                return false;
        vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
 
+       smp_rmb();
        return vq->avail_idx == vq->last_avail_idx;
 }
 EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);

Oh wow you are right.

We have:

        if (vq->avail_idx == vq->last_avail_idx) {
                if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) {
                        vq_err(vq, "Failed to access avail idx at %p\n",
                                &vq->avail->idx);
                        return -EFAULT;
                }
                vq->avail_idx = vhost16_to_cpu(vq, avail_idx);

                if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
                        vq_err(vq, "Guest moved used index from %u to %u",
                                last_avail_idx, vq->avail_idx);
                        return -EFAULT;
                }

                /* If there's nothing new since last we looked, return
                 * invalid.
                 */
                if (vq->avail_idx == last_avail_idx)
                        return vq->num;

                /* Only get avail ring entries after they have been
                 * exposed by guest.
                 */
                smp_rmb();
        }

and so the rmb only happens if avail_idx is not advanced.

Actually there is a bunch of code duplication where we assign to avail_idx, too.

Will thanks a lot for looking into this! I kept looking into the virtio side for some reason, the fact that it did not trigger with qemu should have been a big hint!

Signature · 1 line
MST
#39 lore raw
In reply to Gavin Shan
Quoted 18 lines · Gavin Shan

Before this patch was posted, I had debugging code to record last 16 transactions to the available and used queue from guest and host side. It did reveal the wrong head was fetched from the available queue.

[   11.785745] ================ virtqueue_get_buf_ctx_split ================
[   11.786238] virtio_net virtio0: output.0:id 74 is not a head!
[   11.786655] head to be released: 036 077
[   11.786952]
[   11.786952] avail_idx:
[   11.787234] 000  63985  <--
[   11.787237] 001  63986
[   11.787444] 002  63987
[   11.787632] 003  63988
[   11.787821] 004  63989
[   11.788006] 005  63990
[   11.788194] 006  63991
[   11.788381] 007  63992
Quoted 8 lines
[   11.788567] 008  63993
[   11.788772] 009  63994
[   11.788957] 010  63995
[   11.789141] 011  63996
[   11.789327] 012  63997
[   11.789515] 013  63998
[   11.789701] 014  63999
[   11.789886] 015  64000

Does the error always occur at such a round idx value?

Here, 64000 == 0xFA00. Maybe coincidence but it's improbable enough to be interesting.

This debug code seems rather useful!

-- Keir

Quoted 99 lines
[   11.790068]
[   11.790068] avail_head:
[   11.790529] 000  075  <--
[   11.790718] 001  036
[   11.790890] 002  077
[   11.791061] 003  129
[   11.791231] 004  072
[   11.791400] 005  130
[   11.791574] 006  015
[   11.791748] 007  074
[   11.791918] 008  130
[   11.792094] 009  130
[   11.792263] 010  074
[   11.792437] 011  015
[   11.792617] 012  072
[   11.792788] 013  129
[   11.792961] 014  077    // The last two heads from guest to host: 077, 036
[   11.793134] 015  036

[root@nvidia-grace-hopper-05 qemu.main]# cat /proc/vhost

avail_idx

000  63998
001  64000
002  63954  <---
003  63955
004  63956
005  63974
006  63981
007  63984
008  63986
009  63987
010  63988
011  63989
012  63992
013  63993
014  63995
015  63997

avail_head

000  074
001  015
002  072
003  129
004  074            // The last two heads seen by vhost is: 074, 036
005  036
006  075  <---
007  036
008  077
009  129
010  072
011  130
012  015
013  074
014  130
015  130

used_idx

000  64000
001  63882  <---
002  63889
003  63891
004  63898
005  63936
006  63942
007  63946
008  63949
009  63953
010  63957
011  63981
012  63990
013  63992
014  63993
015  63999

used_head

000  072
001  129
002  074          // The last two heads published to guest is: 074, 036
003  036
004  075  <---
005  036
006  077
007  129
008  072
009  130
010  015
011  074
012  130
013  130
014  074
015  015

Thanks,
Gavin

author
#40 lore raw
Quoted 28 lines · Keir Fraser
Quoted 18 lines · Gavin Shan

Before this patch was posted, I had debugging code to record last 16 transactions to the available and used queue from guest and host side. It did reveal the wrong head was fetched from the available queue.

[   11.785745] ================ virtqueue_get_buf_ctx_split ================
[   11.786238] virtio_net virtio0: output.0:id 74 is not a head!
[   11.786655] head to be released: 036 077
[   11.786952]
[   11.786952] avail_idx:
[   11.787234] 000  63985  <--
[   11.787237] 001  63986
[   11.787444] 002  63987
[   11.787632] 003  63988
[   11.787821] 004  63989
[   11.788006] 005  63990
[   11.788194] 006  63991
[   11.788381] 007  63992
Quoted 8 lines
[   11.788567] 008  63993
[   11.788772] 009  63994
[   11.788957] 010  63995
[   11.789141] 011  63996
[   11.789327] 012  63997
[   11.789515] 013  63998
[   11.789701] 014  63999
[   11.789886] 015  64000
Quoted 6 lines

Does the error always occur at such a round idx value?

Here, 64000 == 0xFA00. Maybe coincidence but it's improbable enough to be interesting.

This debug code seems rather useful!

Keir, Nope, it's just coincidence. We don't have such kind of pattern.

Thanks,
Gavin

Quoted 102 lines
Quoted 101 lines

[   11.790068]
[   11.790068] avail_head:
[   11.790529] 000  075  <--
[   11.790718] 001  036
[   11.790890] 002  077
[   11.791061] 003  129
[   11.791231] 004  072
[   11.791400] 005  130
[   11.791574] 006  015
[   11.791748] 007  074
[   11.791918] 008  130
[   11.792094] 009  130
[   11.792263] 010  074
[   11.792437] 011  015
[   11.792617] 012  072
[   11.792788] 013  129
[   11.792961] 014  077    // The last two heads from guest to host: 077, 036
[   11.793134] 015  036

[root@nvidia-grace-hopper-05 qemu.main]# cat /proc/vhost

avail_idx

000  63998
001  64000
002  63954  <---
003  63955
004  63956
005  63974
006  63981
007  63984
008  63986
009  63987
010  63988
011  63989
012  63992
013  63993
014  63995
015  63997

avail_head

000  074
001  015
002  072
003  129
004  074            // The last two heads seen by vhost is: 074, 036
005  036
006  075  <---
007  036
008  077
009  129
010  072
011  130
012  015
013  074
014  130
015  130

used_idx

000  64000
001  63882  <---
002  63889
003  63891
004  63898
005  63936
006  63942
007  63946
008  63949
009  63953
010  63957
011  63981
012  63990
013  63992
014  63993
015  63999

used_head

000  072
001  129
002  074          // The last two heads published to guest is: 074, 036
003  036
004  075  <---
005  036
006  077
007  129
008  072
009  130
010  015
011  074
012  130
013  130
014  074
015  015

Thanks,
Gavin

#41 lore raw
In reply to Will Deacon
Quoted 68 lines · Will Deacon
Quoted 48 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

  /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
  -accel kvm -machine virt,gic-version=host                            \
     :                                                                 \
  -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

  [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
 drivers/virtio/virtio_ring.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 701

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
 	vq->split.avail_idx_shadow++;
 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
 						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

Quoted 4 lines

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Will

Just making sure: so on this system, how do
smp_wmb() and wmb() differ? smb_wmb is normally for synchronizing with kernel running on another CPU and we are doing something unusual in virtio when we use it to synchronize with host as opposed to the guest - e.g. CONFIG_SMP is special cased because of this:

#define virt_wmb() do { kcsan_wmb(); __smp_wmb(); } while (0)

Note __smp_wmb not smp_wmb which would be a NOP on UP.

Signature · 1 line
MST
#42 lore raw
Quoted 44 lines · Michael S. Tsirkin
Quoted 28 lines · Will Deacon
Quoted 13 lines · Gavin Shan
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 701

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
 	vq->split.avail_idx_shadow++;
 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
 						vq->split.avail_idx_shadow);

Replacing a DMB with a DSB is _very_ unlikely to be the correct solution here, especially when ordering accesses to coherent memory.

Quoted 7 lines

In practice, either the larger timing different from the DSB or the fact that you're going from a Store->Store barrier to a full barrier is what makes things "work" for you. Have you tried, for example, a DMB SY (e.g. via __smb_mb()).

We definitely shouldn't take changes like this without a proper explanation of what is going on.

Just making sure: so on this system, how do
smp_wmb() and wmb() differ? smb_wmb is normally for synchronizing with kernel running on another CPU and we are doing something unusual in virtio when we use it to synchronize with host as opposed to the guest - e.g. CONFIG_SMP is special cased because of this:

Quoted 3 lines

#define virt_wmb() do { kcsan_wmb(); __smp_wmb(); } while (0)

Note __smp_wmb not smp_wmb which would be a NOP on UP.

I think that should be fine (as long as the NOP is a barrier() for the compiler).

wmb() uses a DSB, but that is only really relevant to endpoint ordering with real I/O devices, e.g. if for some reason you need to guarantee that a write has made it all the way to the device before proceeding. Even then you're slightly at the mercy of the memory type and the interconnect not giving an early acknowledgement, so the extra ordering is rarely needed in practice and we don't even provide it for our I/O accessors (e.g. writel() and readl()).

So for virtio between two CPUs using coherent memory, DSB is a red herring.

Will

#43 lore raw
In reply to Gavin Shan
Quoted 48 lines · Gavin Shan

The issue is reported by Yihuang Yu who have 'netperf' test on NVidia's grace-grace and grace-hopper machines. The 'netperf' client is started in the VM hosted by grace-hopper machine, while the 'netperf' server is running on grace-grace machine.

The VM is started with virtio-net and vhost has been enabled. We observe a error message spew from VM and then soft-lockup report. The error message indicates the data associated with the descriptor (index: 135) has been released, and the queue is marked as broken. It eventually leads to the endless effort to fetch free buffer (skb) in drivers/net/virtio_net.c::start_xmit() and soft-lockup. The stale index 135 is fetched from the available ring and published to the used ring by vhost, meaning we have disordred write to the available ring element and available index.

  /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64              \
  -accel kvm -machine virt,gic-version=host                            \
     :                                                                 \
  -netdev tap,id=vnet0,vhost=on                                        \

-device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 \

  [   19.993158] virtio_net virtio1: output.0:id 135 is not a head!

Fix the issue by replacing virtio_wmb(vq->weak_barriers) with stronger virtio_mb(false), equivalent to replaced 'dmb' by 'dsb' instruction on ARM64. It should work for other architectures, but performance loss is expected.

---
 drivers/virtio/virtio_ring.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_ring.c +2 -3
index 49299b1f9ec7..7d852811c912 100644
@@ -687,9 +687,15 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
 
-	/* Descriptors and available array need to be set before we expose the
-	 * new available array entries. */
-	virtio_wmb(vq->weak_barriers);
+	/*
+	 * Descriptors and available array need to be set before we expose

drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 701

Quoted 10 lines
drivers/virtio/virtio_ring.c +7 -0
+	 * the new available array entries. virtio_wmb() should be enough
+	 * to ensuere the order theoretically. However, a stronger barrier
+	 * is needed by ARM64. Otherwise, the stale data can be observed
+	 * by the host (vhost). A stronger barrier should work for other
+	 * architectures, but performance loss is expected.
+	 */
+	virtio_mb(false);
 	vq->split.avail_idx_shadow++;
 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
 						vq->split.avail_idx_shadow);

Something else to try, is to disassemble the code and check the compiler is not broken.

It also might help to replace assigment above with WRITE_ONCE - it's technically always has been the right thing to do, it's just a big change (has to be done everywhere if done at all) so we never bothered and we never hit a compiler that would split or speculate stores ...

Quoted 2 lines
Signature · 1 line
2.44.0