Skip to contentSkip to conversation

vhost: flush used->idx update before reading avail->flags

PATCH Superseded Huawei Xie 9 participants 17 messages Apr 22, 2015 – Jun 11, 2015 2 messages not in the archive

lore
Share & tools
lore

Huawei Xie

author
#0 lore raw

PATCH 1/1 vhost: flush used->idx update before reading avail->flags

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.

---
 lib/librte_vhost/vhost_rxtx.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/vhost_rxtx.c +3 -0
index 510ffe8..6afba35 100644
@@ -178,6 +178,9 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 	*(volatile uint16_t *)&vq->used->idx += count;
 	vq->last_used_idx = res_end_idx;
 
+	/* flush used->idx update before we read avail->flags. */
+	rte_mb();
+
 	/* Kick the guest if necessary. */
 	if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
 		eventfd_write((int)vq->callfd, 1);
Signature · 1 line
1.8.1.4
#2 lore raw
In reply to a message not in the archive
Quoted 4 lines · Huawei Xie

update of used->idx and read of avail->flags could be reordered. memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.

This patch looks right to me.

#3 lore raw
In reply to a message not in the archive
Quoted 14 lines · Huawei Xie

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.

---
 lib/librte_vhost/vhost_rxtx.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/vhost_rxtx.c +0 -0
index 510ffe8..6afba35 100644

lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 186

Quoted 9 lines
lib/librte_vhost/vhost_rxtx.c +3 -0
@@ -178,6 +178,9 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 	*(volatile uint16_t *)&vq->used->idx += count;
 	vq->last_used_idx = res_end_idx;
 
+	/* flush used->idx update before we read avail->flags. */
+	rte_mb();
+
 	/* Kick the guest if necessary. */
 	if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
 		eventfd_write((int)vq->callfd, 1);

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

#5 lore raw
In reply to a message not in the archive
Quoted 31 lines

-----Original Message-----
From: Linhaifeng [mailto:[email protected]]
Sent: Friday, April 24, 2015 9:01 AM
To: Xie, Huawei; [email protected]
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] vhost: flush used->idx update before reading avail-

Quoted 1 line

flags

Quoted 2 lines · Huawei Xie

update of used->idx and read of avail->flags could be reordered. memory fence should be used to ensure the order, otherwise guest could

see a stale used->idx value after it toggles the interrupt suppression flag.

lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t

Quoted 11 lines
---
 lib/librte_vhost/vhost_rxtx.c | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_vhost/vhost_rxtx.c +0 -0
index 510ffe8..6afba35 100644
@@ -178,6 +178,9 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t

queue_id,

Quoted 3 lines
lib/librte_vhost/vhost_rxtx.c +0 -0
 	*(volatile uint16_t *)&vq->used->idx += count;
 	vq->last_used_idx = res_end_idx;
 
Quoted 10 lines

lib/librte_vhost/vhost_rxtx.c virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, · line 186

Quoted 7 lines
lib/librte_vhost/vhost_rxtx.c +3 -0
+	/* flush used->idx update before we read avail->flags. */
+	rte_mb();
+
 	/* Kick the guest if necessary. */
 	if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
 		eventfd_write((int)vq->callfd, 1);

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

If those two store and load are reordered, guest toggles the interrupt suppression flag, then it checks that there is no more work to do. Actually there is.

#6 lore raw
In reply to a message not in the archive
Quoted 3 lines · Linhaifeng

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

You should be able to test it like this:

1. Boot two Linux kernel (e.g. 3.13) guests.
2. Connect them via vhost switch.
3. Run continuous traffic between them (e.g. iperf).

I would expect that within a reasonable timeframe (< 1 hour) one of the guests' network interfaces will hang indefinitely due to a missed interrupt.

You won't be able to reproduce this using DPDK guests because they are not using the same interrupt suppression method.

This is a serious real-world problem. I wouldn't deploy the vhost implementation without this fix.

Cheers,
-Luke

author
#14 lore raw

PATCH v2 1/1 vhost: flush used->idx update before reading avail->flags

In reply to a message not in the archive

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.
After guest sets the interrupt suppression flag, it will check if there is more buffer to process through used->idx. If it sees a stale value, it will exit the processing while host willn't send interrupt to guest.

---
 lib/librte_vhost/vhost_rxtx.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/librte_vhost/vhost_rxtx.c +6 -0
index 510ffe8..4809d32 100644
@@ -178,6 +178,9 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 	*(volatile uint16_t *)&vq->used->idx += count;
 	vq->last_used_idx = res_end_idx;
 
+	/* flush used->idx update before we read avail->flags. */
+	rte_mb();
+
 	/* Kick the guest if necessary. */
 	if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
 		eventfd_write((int)vq->callfd, 1);
@@ -505,6 +508,9 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 		*(volatile uint16_t *)&vq->used->idx += entry_success;
 		vq->last_used_idx = res_end_idx;
 
+		/* flush used->idx update before we read avail->flags. */
+		rte_mb();
+
 		/* Kick the guest if necessary. */
 		if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
 			eventfd_write((int)vq->callfd, 1);
Signature · 1 line
1.8.1.4
#15 lore raw

2015-04-29 19:11, Huawei Xie:

Quoted 5 lines

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.
After guest sets the interrupt suppression flag, it will check if there is more buffer to process through used->idx. If it sees a stale value, it will exit the processing while host willn't send interrupt to guest.

Applied with following title, thanks
vhost: fix virtio freeze due to missed interrupt

#16 lore raw

Hi,

Maybe I missed a part of the discussion, but is there any special purpose for using rte_mb (both read and write fence) here rather than rte_wmb (write fence only)?

Signature · 12 lines
Best regards,

Nikita Kalyazin,
[email protected]

Software Engineer
CE OS Group
Samsung R&D Institute Russia
Tel: +7 (495) 797-25-00 #3816
Tel: +7 (495) 797-25-03
Office #1501, 12-1, Dvintsev str.,
Moscow, 127018, Russia
Quoted 10 lines · Thomas Monjalon

2015-04-29 19:11, Huawei Xie:

Quoted 5 lines

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.
After guest sets the interrupt suppression flag, it will check if there is more buffer to process through used->idx. If it sees a stale value, it will exit the processing while host willn't send interrupt to guest.

Applied with following title, thanks
vhost: fix virtio freeze due to missed interrupt

#17 lore raw
Quoted 4 lines · Nikita Kalyazin

Hi,

Maybe I missed a part of the discussion, but is there any special purpose for using rte_mb (both read and write fence) here rather than rte_wmb (write fence only)?

The fence is between write of used->idx and read of avail->flags, so rte_wmb won't do anything useful.

Quoted 26 lines
Signature · 12 lines
Best regards,

Nikita Kalyazin,
[email protected]

Software Engineer
CE OS Group
Samsung R&D Institute Russia
Tel: +7 (495) 797-25-00 #3816
Tel: +7 (495) 797-25-03
Office #1501, 12-1, Dvintsev str.,
Moscow, 127018, Russia
Quoted 10 lines · Thomas Monjalon

2015-04-29 19:11, Huawei Xie:

Quoted 5 lines

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.
After guest sets the interrupt suppression flag, it will check if there is more buffer to process through used->idx. If it sees a stale value, it will exit the processing while host willn't send interrupt to guest.

Applied with following title, thanks
vhost: fix virtio freeze due to missed interrupt

#18 lore raw

Ah, sorry. I looked at it without the context. Thanks.

Signature · 12 lines
Best regards,

Nikita Kalyazin,
[email protected]

Software Engineer
CE OS Group
Samsung R&D Institute Russia
Tel: +7 (495) 797-25-00 #3816
Tel: +7 (495) 797-25-03
Office #1501, 12-1, Dvintsev str.,
Moscow, 127018, Russia
Quoted 35 lines · Michael S. Tsirkin
Quoted 4 lines · Nikita Kalyazin

Hi,

Maybe I missed a part of the discussion, but is there any special purpose for using rte_mb (both read and write fence) here rather than rte_wmb (write fence only)?

The fence is between write of used->idx and read of avail->flags, so rte_wmb won't do anything useful.

Quoted 26 lines
Signature · 12 lines
Best regards,

Nikita Kalyazin,
[email protected]

Software Engineer
CE OS Group
Samsung R&D Institute Russia
Tel: +7 (495) 797-25-00 #3816
Tel: +7 (495) 797-25-03
Office #1501, 12-1, Dvintsev str.,
Moscow, 127018, Russia
Quoted 10 lines · Thomas Monjalon

2015-04-29 19:11, Huawei Xie:

Quoted 5 lines

update of used->idx and read of avail->flags could be reordered.
memory fence should be used to ensure the order, otherwise guest could see a stale used->idx value after it toggles the interrupt suppression flag.
After guest sets the interrupt suppression flag, it will check if there is more buffer to process through used->idx. If it sees a stale value, it will exit the processing while host willn't send interrupt to guest.

Applied with following title, thanks
vhost: fix virtio freeze due to missed interrupt

#7 lore raw
In reply to Luke Gorrie
Quoted 15 lines · Luke Gorrie
Quoted 3 lines · Linhaifeng

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

You should be able to test it like this:

1. Boot two Linux kernel (e.g. 3.13) guests.
2. Connect them via vhost switch.
3. Run continuous traffic between them (e.g. iperf).

I would expect that within a reasonable timeframe (< 1 hour) one of the guests' network interfaces will hang indefinitely due to a missed interrupt.

Quoted 9 lines

You won't be able to reproduce this using DPDK guests because they are not using the same interrupt suppression method.

This is a serious real-world problem. I wouldn't deploy the vhost implementation without this fix.

Cheers,
-Luke

I think this patch can't resole this problem. On the other hand we still would miss interrupt.

After add rte_mb() function the we want the case is :
1.write used->idx. ring is full or empty. 2.virtio_net open interrupt.
3.read avail->flags.

but this case(miss interrupt) would happen too:
1.write used->idx. ring is full or empty.
2.read avail->flags.
3.virtio_net open interrupt.

#8 lore raw
Quoted 9 lines · Linhaifeng
Quoted 8 lines · Luke Gorrie

You should be able to test it like this:

1. Boot two Linux kernel (e.g. 3.13) guests.
2. Connect them via vhost switch.
3. Run continuous traffic between them (e.g. iperf).

I would expect that within a reasonable timeframe (< 1 hour) one of the guests' network interfaces will hang indefinitely due to a missed

Quoted 9 lines

interrupt.

Quoted 2 lines

You won't be able to reproduce this using DPDK guests because they are

not

Quoted 1 line

using the same interrupt suppression method.

I think this patch can't resole this problem. On the other hand we still would miss interrupt.

For what it is worth, we were able to reproduce the problem as described above with older Snabb Switch releases and we were also able to verify that inserting a memory barrier fixes this problem.

This is the relevant commit in the snabbswitch repo for reference:
https://github.com/SnabbCo/snabbswitch/commit/c33cdd8704246887e11d7c353f773f7b488a47f2

In a nutshell, we added an MFENCE instruction after writing used->idx and before checking VRING_F_NO_INTERRUPT.

I have not tested this case under DPDK myself and so I am not really certain which memory barrier operations are sufficient/insufficient in that context. I hope that our experience is relevant/helpful though and I am happy to explain more about that if I have missed any important details.

Cheers,
-Luke

#9 lore raw
In reply to Linhaifeng
Quoted 35 lines · Linhaifeng
Quoted 15 lines · Luke Gorrie
Quoted 3 lines · Linhaifeng

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

You should be able to test it like this:

1. Boot two Linux kernel (e.g. 3.13) guests.
2. Connect them via vhost switch.
3. Run continuous traffic between them (e.g. iperf).

I would expect that within a reasonable timeframe (< 1 hour) one of the guests' network interfaces will hang indefinitely due to a missed interrupt.

Quoted 9 lines

You won't be able to reproduce this using DPDK guests because they are not using the same interrupt suppression method.

This is a serious real-world problem. I wouldn't deploy the vhost implementation without this fix.

Cheers,
-Luke

I think this patch can't resole this problem. On the other hand we still would miss interrupt.

After add rte_mb() function the we want the case is :
1.write used->idx. ring is full or empty. 2.virtio_net open interrupt.
3.read avail->flags.

Quoted 5 lines

but this case(miss interrupt) would happen too:
1.write used->idx. ring is full or empty.
2.read avail->flags.
3.virtio_net open interrupt.

That's why a correct guest, after detecting an empty used ring, must always re-check used idx at least once after writing avail->flags.

By the way, similarly, host side must re-check avail idx after writing used flags. I don't see where snabbswitch does it - is that a bug in snabbswitch?

Signature · 1 line
MST
#10 lore raw
Quoted 38 lines · Michael S. Tsirkin
Quoted 32 lines · Linhaifeng
Quoted 14 lines · Luke Gorrie
Quoted 3 lines · Linhaifeng

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

You should be able to test it like this:

1. Boot two Linux kernel (e.g. 3.13) guests.
2. Connect them via vhost switch.
3. Run continuous traffic between them (e.g. iperf).

I would expect that within a reasonable timeframe (< 1 hour) one of the guests' network interfaces will hang indefinitely due to a missed interrupt.

Quoted 9 lines

You won't be able to reproduce this using DPDK guests because they are not using the same interrupt suppression method.

This is a serious real-world problem. I wouldn't deploy the vhost implementation without this fix.

Cheers,
-Luke

I think this patch can't resole this problem. On the other hand we still would miss interrupt.

After add rte_mb() function the we want the case is :
1.write used->idx. ring is full or empty. 2.virtio_net open interrupt.
3.read avail->flags.

Quoted 5 lines

but this case(miss interrupt) would happen too:
1.write used->idx. ring is full or empty.
2.read avail->flags.
3.virtio_net open interrupt.

Quoted 7 lines

That's why a correct guest, after detecting an empty used ring, must always re-check used idx at least once after writing avail->flags.

By the way, similarly, host side must re-check avail idx after writing used flags. I don't see where snabbswitch does it - is that a bug in snabbswitch?

yes, both host and guest should recheck if there is more work added after they toggle the flag.
For DPDK vHost, as it runs in polling mode, we will recheck avail idx soon, so we don't need recheck.

#11 lore raw
Quoted 42 lines · Huawei
Quoted 41 lines · Michael S. Tsirkin
Quoted 32 lines · Linhaifeng
Quoted 14 lines · Luke Gorrie
Quoted 3 lines · Linhaifeng

If not add memory fence what would happen? Packets loss or interrupt loss?How to test it ?

You should be able to test it like this:

1. Boot two Linux kernel (e.g. 3.13) guests.
2. Connect them via vhost switch.
3. Run continuous traffic between them (e.g. iperf).

I would expect that within a reasonable timeframe (< 1 hour) one of the guests' network interfaces will hang indefinitely due to a missed interrupt.

Quoted 9 lines

You won't be able to reproduce this using DPDK guests because they are not using the same interrupt suppression method.

This is a serious real-world problem. I wouldn't deploy the vhost implementation without this fix.

Cheers,
-Luke

I think this patch can't resole this problem. On the other hand we still would miss interrupt.

After add rte_mb() function the we want the case is :
1.write used->idx. ring is full or empty. 2.virtio_net open interrupt.
3.read avail->flags.

Quoted 5 lines

but this case(miss interrupt) would happen too:
1.write used->idx. ring is full or empty.
2.read avail->flags.
3.virtio_net open interrupt.

That's why a correct guest, after detecting an empty used ring, must always re-check used idx at least once after writing avail->flags.

Quoted 10 lines
Quoted 4 lines

By the way, similarly, host side must re-check avail idx after writing used flags. I don't see where snabbswitch does it - is that a bug in snabbswitch?

yes, both host and guest should recheck if there is more work added after they toggle the flag.
For DPDK vHost, as it runs in polling mode, we will recheck avail idx soon, so we don't need recheck.

DPDK does check the avail idx but does nothing like this:

if (vq->last_used_idx == avail_idx) {
	return;
}

If we miss an interrupt after calling rte_mb(), (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) is False; while (vq->last_used_idx == avail_idx) is True,

then the guest will miss the interrupt forever and virtio-net would stop working.

Would this case happen?

#12 lore raw
In reply to Michael S. Tsirkin
Quoted 3 lines · Michael S. Tsirkin

By the way, similarly, host side must re-check avail idx after writing used flags. I don't see where snabbswitch does it - is that a bug in snabbswitch?

Good question.

Snabb Switch does not use interrupts from the guest. We always set VRING_F_NO_NOTIFY to tell the guest that it need not interrupt us. Then we run in poll mode and in practice check the avail ring for new descriptors every 20us or so.

So the argument for not needing this check in both Snabb Switch and DPDK is that we are running poll mode and don't notice whether interrupts are being sent or not.

Is that a solid argument or do I misunderstand what the race condition is?

Cheers,
-Luke

#13 lore raw
Quoted 7 lines · Luke Gorrie
Quoted 3 lines · Michael S. Tsirkin

By the way, similarly, host side must re-check avail idx after writing used flags. I don't see where snabbswitch does it - is that a bug in snabbswitch?

Quoted 6 lines

Good question.

Snabb Switch does not use interrupts from the guest. We always set VRING_F_NO_NOTIFY to tell the guest that it need not interrupt us. Then we run in poll mode and in practice check the avail ring for new descriptors every 20us or so.

Yes, host not need guest to notify when in poll mode but host also need to notify guest who use virtio_net when vring is full or emtpy.If host loss this notification guest would stop working.

Quoted 10 lines

So the argument for not needing this check in both Snabb Switch and DPDK is that we are running poll mode and don't notice whether interrupts are being sent or not.

Is that a solid argument or do I misunderstand what the race condition is?

Cheers,
-Luke