Skip to contentSkip to conversation

vhost: fix read vs write lock mismatch

PATCH Applied Stephen Hemminger 3 participants 7 messages Nov 18, 2024 – Nov 26, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: fix read vs write lock mismatch

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
 lib/vhost/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +1 -1
index 298a5dae74..d764d4bc6a 100644
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
 	if (unlikely(!vq->access_ok)) {
 		vhost_user_iotlb_rd_unlock(vq);
-		rte_rwlock_read_unlock(&vq->access_lock);
+		rte_rwlock_write_unlock(&vq->access_lock);
 
 		virtio_dev_vring_translate(dev, vq);
 		goto out_no_unlock;
Signature · 1 line
2.45.2
#1 lore raw
Quoted 19 lines · Stephen Hemminger

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
 lib/vhost/virtio_net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +0 -0
index 298a5dae74..d764d4bc6a 100644

lib/vhost/virtio_net.c virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 2541

Quoted 5 lines
lib/vhost/virtio_net.c +1 -1
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
        if (unlikely(!vq->access_ok)) {
                vhost_user_iotlb_rd_unlock(vq);
-               rte_rwlock_read_unlock(&vq->access_lock);
+               rte_rwlock_write_unlock(&vq->access_lock);

A write lock is taken earlier, because virtio_dev_rx_async_submit_* need it for access to vq->async (as opposed to the sync code that only takes read lock).

Here, no need to release/take again all locks.
A simpler fix is to directly call vring_translate(dev, vq).

Signature · 1 line
David Marchand
Reviewed-by
#2 lore raw
Quoted 31 lines · David Marchand
Quoted 19 lines · Stephen Hemminger

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
  lib/vhost/virtio_net.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +0 -0
index 298a5dae74..d764d4bc6a 100644

lib/vhost/virtio_net.c virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 2541

Quoted 5 lines
lib/vhost/virtio_net.c +1 -1
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
         if (unlikely(!vq->access_ok)) {
                 vhost_user_iotlb_rd_unlock(vq);
-               rte_rwlock_read_unlock(&vq->access_lock);
+               rte_rwlock_write_unlock(&vq->access_lock);

A write lock is taken earlier, because virtio_dev_rx_async_submit_* need it for access to vq->async (as opposed to the sync code that only takes read lock).

Quoted 4 lines

Here, no need to release/take again all locks.
A simpler fix is to directly call vring_translate(dev, vq).

Ok, so both solutions are correct.

David's one is more optimized, but this is a corner case in the async datapath so not really critical.

On the other hand, Stephen's solution keeps the same pattern as the other datapaths.

I'd go with Stephen's solution, but would change the commit title to:

vhost: fix deadlock in Rx async path

With this change:

Thanks,
Maxime

#3 lore raw
Quoted 37 lines · Maxime Coquelin
Quoted 31 lines · David Marchand
Quoted 19 lines · Stephen Hemminger

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
  lib/vhost/virtio_net.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +0 -0
index 298a5dae74..d764d4bc6a 100644

lib/vhost/virtio_net.c virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 2541

Quoted 5 lines
lib/vhost/virtio_net.c +1 -1
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
         if (unlikely(!vq->access_ok)) {
                 vhost_user_iotlb_rd_unlock(vq);
-               rte_rwlock_read_unlock(&vq->access_lock);
+               rte_rwlock_write_unlock(&vq->access_lock);

A write lock is taken earlier, because virtio_dev_rx_async_submit_* need it for access to vq->async (as opposed to the sync code that only takes read lock).

Quoted 4 lines

Here, no need to release/take again all locks.
A simpler fix is to directly call vring_translate(dev, vq).

Quoted 7 lines

Ok, so both solutions are correct.

David's one is more optimized, but this is a corner case in the async datapath so not really critical.

On the other hand, Stephen's solution keeps the same pattern as the other datapaths.

Ok, it avoids having a special case, and I prefer limiting special cases too.

I'll send a RFC for new lock annotations (for catching such bug) for the next release.

Quoted 8 lines

I'd go with Stephen's solution, but would change the commit title to:

vhost: fix deadlock in Rx async path

With this change:

Yes, the title needed some work.

Signature · 1 line
David Marchand
#4 lore raw
Quoted 44 lines · David Marchand
Quoted 42 lines · Maxime Coquelin
Quoted 31 lines · David Marchand
Quoted 19 lines · Stephen Hemminger

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
   lib/vhost/virtio_net.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +0 -0
index 298a5dae74..d764d4bc6a 100644

lib/vhost/virtio_net.c virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 2541

Quoted 5 lines
lib/vhost/virtio_net.c +1 -1
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
          if (unlikely(!vq->access_ok)) {
                  vhost_user_iotlb_rd_unlock(vq);
-               rte_rwlock_read_unlock(&vq->access_lock);
+               rte_rwlock_write_unlock(&vq->access_lock);

A write lock is taken earlier, because virtio_dev_rx_async_submit_* need it for access to vq->async (as opposed to the sync code that only takes read lock).

Quoted 4 lines

Here, no need to release/take again all locks.
A simpler fix is to directly call vring_translate(dev, vq).

Ok, so both solutions are correct.

David's one is more optimized, but this is a corner case in the async datapath so not really critical.

Quoted 7 lines
Quoted 2 lines

On the other hand, Stephen's solution keeps the same pattern as the other datapaths.

Ok, it avoids having a special case, and I prefer limiting special cases too.

I'll send a RFC for new lock annotations (for catching such bug) for the next release.

Great, thanks for that!

Quoted 4 lines
Quoted 4 lines

I'd go with Stephen's solution, but would change the commit title to:

Quoted 9 lines
Quoted 5 lines

vhost: fix deadlock in Rx async path

With this change:

Yes, the title needed some work.

Ack, will change while applying.

Thanks,
Maxime

#5 lore raw
Quoted 63 lines · On Mon, 25 Nov 2024 17:31:26 +0100 Maxime Coquelin…
Quoted 44 lines · David Marchand
Quoted 42 lines · Maxime Coquelin
Quoted 31 lines · David Marchand
Quoted 19 lines · Stephen Hemminger

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
   lib/vhost/virtio_net.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +0 -0
index 298a5dae74..d764d4bc6a 100644

lib/vhost/virtio_net.c virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 2541

Quoted 5 lines
lib/vhost/virtio_net.c +1 -1
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
          if (unlikely(!vq->access_ok)) {
                  vhost_user_iotlb_rd_unlock(vq);
-               rte_rwlock_read_unlock(&vq->access_lock);
+               rte_rwlock_write_unlock(&vq->access_lock);  

A write lock is taken earlier, because virtio_dev_rx_async_submit_* need it for access to vq->async (as opposed to the sync code that only takes read lock).

Quoted 4 lines

Here, no need to release/take again all locks.
A simpler fix is to directly call vring_translate(dev, vq).

Ok, so both solutions are correct.

David's one is more optimized, but this is a corner case in the async datapath so not really critical.

Quoted 7 lines
Quoted 2 lines

On the other hand, Stephen's solution keeps the same pattern as the other datapaths.

Ok, it avoids having a special case, and I prefer limiting special cases too.

I'll send a RFC for new lock annotations (for catching such bug) for the next release.

Great, thanks for that!

Quoted 8 lines
Quoted 8 lines

I'd go with Stephen's solution, but would change the commit title to:

vhost: fix deadlock in Rx async path

With this change:

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Yes, the title needed some work.

Ack, will change while applying.

Yes better title would be great.

#6 lore raw
In reply to Stephen Hemminger
Quoted 18 lines · Stephen Hemminger

If lock is acquired for write, it must be released for write or a deadlock is likely.

Bugzilla ID: 1582

Fixes 9fc93a1e2320 ("vhost: fix virtqueue access check in datapath")
---
  lib/vhost/virtio_net.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/vhost/virtio_net.c +0 -0
index 298a5dae74..d764d4bc6a 100644

lib/vhost/virtio_net.c virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 2544

Quoted 8 lines
lib/vhost/virtio_net.c +1 -1
@@ -2538,7 +2538,7 @@ virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
  	if (unlikely(!vq->access_ok)) {
  		vhost_user_iotlb_rd_unlock(vq);
-		rte_rwlock_read_unlock(&vq->access_lock);
+		rte_rwlock_write_unlock(&vq->access_lock);
 
  		virtio_dev_vring_translate(dev, vq);
  		goto out_no_unlock;

Applied to dpdk-next-virtio/for-main with suggested title change.

Thanks,
Maxime