Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 065a13c

Browse files
paravmellanoxgregkh
authored andcommittedSep 3, 2021
virtio: Improve vq->broken access to avoid any compiler optimization
[ Upstream commit 60f0779 ] Currently vq->broken field is read by virtqueue_is_broken() in busy loop in one context by virtnet_send_command(). vq->broken is set to true in other process context by virtio_break_device(). Reader and writer are accessing it without any synchronization. This may lead to a compiler optimization which may result to optimize reading vq->broken only once. Hence, force reading vq->broken on each invocation of virtqueue_is_broken() and also force writing it so that such update is visible to the readers. It is a theoretical fix that isn't yet encountered in the field. Signed-off-by: Parav Pandit <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent f41c746 commit 065a13c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed
 

‎drivers/virtio/virtio_ring.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ bool virtqueue_is_broken(struct virtqueue *_vq)
22682268
{
22692269
struct vring_virtqueue *vq = to_vvq(_vq);
22702270

2271-
return vq->broken;
2271+
return READ_ONCE(vq->broken);
22722272
}
22732273
EXPORT_SYMBOL_GPL(virtqueue_is_broken);
22742274

@@ -2283,7 +2283,9 @@ void virtio_break_device(struct virtio_device *dev)
22832283
spin_lock(&dev->vqs_list_lock);
22842284
list_for_each_entry(_vq, &dev->vqs, list) {
22852285
struct vring_virtqueue *vq = to_vvq(_vq);
2286-
vq->broken = true;
2286+
2287+
/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2288+
WRITE_ONCE(vq->broken, true);
22872289
}
22882290
spin_unlock(&dev->vqs_list_lock);
22892291
}

0 commit comments

Comments
 (0)
Please sign in to comment.