virtio_ring: aovid reading flag from the descriptor ring
PATCH 1/1 virtio_ring: aovid reading flag from the descriptor ring
Commit 72b5e8958738 ("virtio-ring: store DMA metadata in desc_extra for split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.
This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +2 -2
@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;
@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++;
Signature · 6 lines
2.25.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 7 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.
Quoted 6 lines
This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
Signed-off-by Jason Wang <[email protected]>
Michael, any comment for this?
Thanks
Quoted 29 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +2
-3@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1
Michael S. Tsirkin
maintainerQuoted 13 lines · Jason Wang
Quoted 12 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
Quoted 5 lines
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>Michael, any comment for this?
Thanks
I don't exactly see why we should care without DMA API, it seems cleaner not to poke at the array one extra time.
Quoted 21 lines
Quoted 21 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +1
-1@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head;
Quoted 8 lines
Quoted 8 lines
drivers/virtio/virtio_ring.c +1
-2-while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 15 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
Quoted 12 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>Michael, any comment for this?
Thanks
I don't exactly see why we should care without DMA API, it seems cleaner not to poke at the array one extra time.
I think the answer is that we have any special care about the DMA API for all other places that are using desc_extra.
Thanks
Quoted 31 lines
Quoted 30 lines
Quoted 30 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +2
-3@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1
Quoted 19 lines · Jason Wang
Quoted 17 lines · Michael S. Tsirkin
Quoted 15 lines · Jason Wang
Quoted 13 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
Signed-off-by Jason Wang <[email protected]>
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Michael, any comment for this?
Thanks
I don't exactly see why we should care without DMA API, it seems cleaner not to poke at the array one extra time.
I think the answer is that we have any special care about the DMA API
I meant "we haven't had" actually.
Thanks
Quoted 36 lines
for all other places that are using
desc_extra.Thanks
Quoted 31 lines
Quoted 30 lines
Quoted 30 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +2
-3@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1
Michael S. Tsirkin
maintainerQuoted 24 lines · Jason Wang
Quoted 23 lines · Jason Wang
Quoted 21 lines · Michael S. Tsirkin
Quoted 18 lines · Jason Wang
Quoted 7 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.Quoted 6 lines
This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
Signed-off-by Jason Wang <[email protected]>Michael, any comment for this?
Thanks
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I don't exactly see why we should care without DMA API, it seems cleaner not to poke at the array one extra time.
I think the answer is that we have any special care about the DMA API
I meant "we haven't had" actually.
Thanks
I'm just asking what's better for performance. An extra write in the first chunk has a cost. Want to test and see?
Quoted 27 lines
Quoted 27 lines
for all other places that are using
desc_extra.Thanks
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +1
-1@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head;
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
drivers/virtio/virtio_ring.c +1
-2-while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 29 lines · Michael S. Tsirkin
Quoted 27 lines · Jason Wang
Quoted 26 lines · Jason Wang
Quoted 23 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang
Quoted 12 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read the descriptor flag from the descriptor ring during buffer detach.This patch fixes by always store the descriptor flag no matter whether DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).
Quoted 5 lines
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>Michael, any comment for this?
Thanks
I don't exactly see why we should care without DMA API, it seems cleaner not to poke at the array one extra time.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
I think the answer is that we have any special care about the DMA API
I meant "we haven't had" actually.
Thanks
I'm just asking what's better for performance. An extra write in the first chunk has a cost. Want to test and see?
I will do it.
Thanks
Quoted 38 lines
Quoted 37 lines
Quoted 37 lines
for all other places that are using
desc_extra.Thanks
Quoted 31 lines
Quoted 30 lines
Quoted 30 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +2
-3@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1
Michael S. Tsirkin
maintainerTypo in the subject btw.
minor tweaks to commit log below
Quoted 4 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still read
reads
Quoted 4 lines
the descriptor flag from the
descriptor ring during buffer detach.This patch fixes
fixes this
Quoted 1 line
by always store
storing
Quoted 6 lines
the descriptor flag no matter whether
DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).Signed-off-by Jason Wang <[email protected]>
I'd also like the commit log to document what the issue is in a bit more depth. I think the main reason we are checking the dma API is this
static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
unsigned int i)
{
struct vring_desc_extra *extra = vq->split.desc_extra;
u16 flags;
if (!vq->use_dma_api)
goto out;
...
}so I guess with a bad flag, what will happen is num_free will become too big is that right?
Quoted 9 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +0
-0
drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 589
Quoted 8 lines
drivers/virtio/virtio_ring.c +1
-1@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;
BTW I'm a bit confused why we need the & (vq->split.vring.num - 1) logic. Maybe it's time we stopped writing out descriptor then overwriting it - e.g. return the desc_extra pointer from virtqueue_add_desc_split instead of an index. Worth checking what this does to performance.
Quoted 11 lines
drivers/virtio/virtio_ring.c +1
-1@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++;Signature · 1 line
2.25.1
Quoted 16 lines · Michael S. Tsirkin
Typo in the subject btw.
minor tweaks to commit log below
Quoted 4 lines · Jason Wang
Commit
72b5e8958738("virtio-ring: store DMA metadata indesc_extrafor split virtqueue") tries to make it possible for the driver to not read from the descriptor ring to prevent the device from corrupting the descriptor ring. But it still readreads
Quoted 3 lines
the descriptor flag from the
descriptor ring during buffer detach.
Quoted 8 lines
Quoted 1 line
This patch fixes
fixes this
Quoted 1 line
by always store
storing
Will fix.
Quoted 25 lines
Quoted 6 lines
the descriptor flag no matter whether
DMA API is used and then we can avoid reading descriptor flag from the descriptor ring. This eliminates the possibly of unexpected next descriptor caused by the wrong flag (e.g the next flag).Signed-off-by Jason Wang <[email protected]>I'd also like the commit log to document what the issue is in a bit more depth. I think the main reason we are checking the dma API is this
static unsigned int vring_unmap_one_split(const struct
vring_virtqueue*vq,
unsigned int i){ struct vring_desc_extra *extra = vq->split.desc_extra; u16 flags; if (!vq->use_dma_api) goto out; ... }
Quoted 2 lines
so I guess with a bad flag, what will happen is
num_freewill become too big is that right?
Yes, and it may have other implications and this is not easy to answer since with the current code, next/flag is the only one that can be controlled by the device. From what I've seen so far, it can cause an early unmap for the descriptors in the free list. This may cause several issues when using DMA API (e.g IOTLB for bouncing). Indeed software IOMMU/IOTLB has done a lot of checks for this but we can't solely depend on IOMMU/IOTLB to handle malicious inputs.
So my understanding is that, instead of trying to figuring out what may happens if some flag or descriptor is modified by the malicious device/hypervisor, we can simply eliminate all those possibilities with minimal efforts and this is why we try not read anything from shared memory area (e.g the descriptor ring etc) if possible.
Quoted 8 lines
Quoted 8 lines
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 16 lines
drivers/virtio/virtio_ring.c static inline int virtqueue_add_split(struct virtqueue *_vq, · line 589
Quoted 13 lines
drivers/virtio/virtio_ring.c +1
-1@@ -583,7 +583,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, }/* Last one doesn't continue. */ desc[prev].flags &=cpu_to_virtio16 (_vq->vdev, ~VRING_DESC_F_NEXT ); -if (!indirect && vq->use_dma_api) +if (!indirect) vq->split.desc_extra[prev & (vq->split.vring.num -1 )].flags &= ~VRING_DESC_F_NEXT ;BTW I'm a bit confused why we need the & (
vq->split.vring.num- 1) logic. Maybe it's time we stopped writing out descriptor then overwriting it -
Right since the next can be read from the descriptor ring directly (as you said below, this needs to be fixed as well).
Quoted 2 lines
e.g. return the
desc_extrapointer fromvirtqueue_add_desc_splitinstead of an index. Worth checking what this does to performance.
Right, let me try to do that in the next version.
Thanks
Quoted 14 lines
Quoted 13 lines
drivers/virtio/virtio_ring.c +1
-2@@ -713,7 +713,7 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,/* Put back on free list: unmap first-level descriptors and find end */ i = head; -while (vq->split.vring.desc[i].flags & nextflag) { +while (vq->split.desc_extra[i].flags & nextflag) {vring_unmap_one_split (vq, i); i = vq->split.desc_extra[i].next; vq->vq.num_free++; --2.25.1