Skip to contentSkip to conversation

virtio: fix vring->inuse recalc after migr

PATCH Superseded Halil Pasic 3 participants 7 messages Dec 15, 2016 – Dec 19, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: fix vring->inuse recalc after migr

Correct recalculation of vring->inuse after migration for the corner case where the avail_idx has already wrapped but used_idx not yet.

Fixes bccdef6b ("virtio: recalculate vq->inuse after migration")

---

I think we could also change the type of inuse to uint16_t. Would this be considered a good idea?

---
 hw/virtio/virtio.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +5 -2
index 1af2de2..089c6f6 100644
@@ -1855,9 +1855,12 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
             /*
              * Some devices migrate VirtQueueElements that have been popped
              * from the avail ring but not yet returned to the used ring.
+             * Cast to uint16_t is OK because max ring size is 0x8000. Thus
+             * no the size of largest array indexable by an integral type
+             * can not be represented by the same type problem.
              */
-            vdev->vq[i].inuse = vdev->vq[i].last_avail_idx -
-                                vdev->vq[i].used_idx;
+            vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
+                                vdev->vq[i].used_idx);
             if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
                 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
                              "used_idx 0x%x",
Signature · 1 line
2.8.4
#1 lore raw
Quoted 11 lines · Halil Pasic

Correct recalculation of vring->inuse after migration for the corner case where the avail_idx has already wrapped but used_idx not yet.

Fixes bccdef6b ("virtio: recalculate vq->inuse after migration")

---

I think we could also change the type of inuse to uint16_t. Would this be considered a good idea?

VRing.num is unsigned int. I would use the same type as num since this is a size, not an index.

Quoted 9 lines
---
 hw/virtio/virtio.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/virtio/virtio.c +0 -0
index 1af2de2..089c6f6 100644

hw/virtio/virtio.c int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 1860

Quoted 6 lines
hw/virtio/virtio.c +3 -0
@@ -1855,9 +1855,12 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
             /*
              * Some devices migrate VirtQueueElements that have been popped
              * from the avail ring but not yet returned to the used ring.
+             * Cast to uint16_t is OK because max ring size is 0x8000. Thus
+             * no the size of largest array indexable by an integral type
+             * can not be represented by the same type problem.

I think it would be safe up to max ring size UINT16_MAX - 1 (we need that extra value to distinguish between empty and full avail rings)?

The last sentence is hard to understand due to the double negative. I think you are saying "Since max ring size < UINT16_MAX it's safe to use uint16_t to represent the size of the ring".

Quoted 12 lines
hw/virtio/virtio.c +2 -2
              */
-            vdev->vq[i].inuse = vdev->vq[i].last_avail_idx -
-                                vdev->vq[i].used_idx;
+            vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
+                                vdev->vq[i].used_idx);
             if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
                 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
                              "used_idx 0x%x",
Signature · 1 line
2.8.4
#2 lore raw
Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Halil Pasic

Correct recalculation of vring->inuse after migration for the corner case where the avail_idx has already wrapped but used_idx not yet.

Fixes bccdef6b ("virtio: recalculate vq->inuse after migration")

---

Quoted 6 lines
Quoted 2 lines

I think we could also change the type of inuse to uint16_t. Would this be considered a good idea?

VRing.num is unsigned int. I would use the same type as num since this is a size, not an index.

Thanks. I asked myself the question why are VRing.num and inuse (at least theoretically arch depended width but at least 16 bit) int while the indexes of the available and used rings uint16_t. Only thing I can think of is some sort of optimization, because the only difference I can see is that the VRing.num is communicated via the transport while the indexes are a part (and/or corresponding to) of the ring layout (that is the shared memory virtio structures and have a fixed width).

Now I'm kind of in doubt: I think having a signed has the benefit of a negative value being more obviously wrong size (for a human looking at it) that a to large positive, but purely theoretically the maximum value of inuse is not guaranteed to fit int (as in C99 MIN_INT is 2^15 - 1).

What is your opinion? Should we keep 'inuse' as is, or should I change it to unsigned with v2 (or prepare a separate patch)?

Quoted 4 lines
Quoted 4 lines
---
 hw/virtio/virtio.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 15 lines

hw/virtio/virtio.c int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 1860

Quoted 11 lines
hw/virtio/virtio.c +3 -0
index 1af2de2..089c6f6 100644
@@ -1855,9 +1855,12 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
             /*
              * Some devices migrate VirtQueueElements that have been popped
              * from the avail ring but not yet returned to the used ring.
+             * Cast to uint16_t is OK because max ring size is 0x8000. Thus
+             * no the size of largest array indexable by an integral type
+             * can not be represented by the same type problem.

I think it would be safe up to max ring size UINT16_MAX - 1 (we need that extra value to distinguish between empty and full avail rings)?

Yeah.

Quoted 4 lines

The last sentence is hard to understand due to the double negative. I think you are saying "Since max ring size < UINT16_MAX it's safe to use uint16_t to represent the size of the ring".

You are not the first one complaining, so the sentence is definitively bad. What disturbs me regarding your formulation is that we do not use uint16_t to represent neither the ring size nor inuse.

How about "Since max ring size < UINT16_MAX it's safe to use modulo UINT16_MAX + 1 subtraction."?

Cheers,
Halil

Quoted 12 lines
Quoted 12 lines
hw/virtio/virtio.c +2 -2
              */
-            vdev->vq[i].inuse = vdev->vq[i].last_avail_idx -
-                                vdev->vq[i].used_idx;
+            vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
+                                vdev->vq[i].used_idx);
             if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
                 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
                              "used_idx 0x%x",
Signature · 1 line
2.8.4
#3 lore raw
Quoted 26 lines · Halil Pasic
Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Halil Pasic

Correct recalculation of vring->inuse after migration for the corner case where the avail_idx has already wrapped but used_idx not yet.

Fixes bccdef6b ("virtio: recalculate vq->inuse after migration")

---

Quoted 6 lines
Quoted 2 lines

I think we could also change the type of inuse to uint16_t. Would this be considered a good idea?

VRing.num is unsigned int. I would use the same type as num since this is a size, not an index.

Thanks. I asked myself the question why are VRing.num and inuse (at least theoretically arch depended width but at least 16 bit) int while the indexes of the available and used rings uint16_t. Only thing I can think of is some sort of optimization, because the only difference I can see is that the VRing.num is communicated via the transport while the indexes are a part (and/or corresponding to) of the ring layout (that is the shared memory virtio structures and have a fixed width).

Quoted 7 lines

Now I'm kind of in doubt: I think having a signed has the benefit of a negative value being more obviously wrong size (for a human looking at it) that a to large positive, but purely theoretically the maximum value of inuse is not guaranteed to fit int (as in C99 MIN_INT is 2^15 - 1).

What is your opinion? Should we keep 'inuse' as is, or should I change it to unsigned with v2 (or prepare a separate patch)?

I'm happy with unsigned. I've CCed Michael Tsirkin in case he has a preference.

Quoted 27 lines
Quoted 4 lines
Quoted 4 lines
---
 hw/virtio/virtio.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 15 lines

hw/virtio/virtio.c int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 1860

Quoted 11 lines
hw/virtio/virtio.c +3 -0
index 1af2de2..089c6f6 100644
@@ -1855,9 +1855,12 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
             /*
              * Some devices migrate VirtQueueElements that have been popped
              * from the avail ring but not yet returned to the used ring.
+             * Cast to uint16_t is OK because max ring size is 0x8000. Thus
+             * no the size of largest array indexable by an integral type
+             * can not be represented by the same type problem.

I think it would be safe up to max ring size UINT16_MAX - 1 (we need that extra value to distinguish between empty and full avail rings)?

Yeah.

Quoted 4 lines

The last sentence is hard to understand due to the double negative. I think you are saying "Since max ring size < UINT16_MAX it's safe to use uint16_t to represent the size of the ring".

Quoted 6 lines

You are not the first one complaining, so the sentence is definitively bad. What disturbs me regarding your formulation is that we do not use uint16_t to represent neither the ring size nor inuse.

How about "Since max ring size < UINT16_MAX it's safe to use modulo UINT16_MAX + 1 subtraction."?

That doesn't mention "representing the size of the ring" so it's unclear what "safe" means.

Stefan

#4 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 6 lines

You are not the first one complaining, so the sentence is definitively bad. What disturbs me regarding your formulation is that we do not use uint16_t to represent neither the ring size nor inuse.

How about "Since max ring size < UINT16_MAX it's safe to use modulo UINT16_MAX + 1 subtraction."?

That doesn't mention "representing the size of the ring" so it's unclear what "safe" means.

Stefan

IMHO it is not about representation but about correct arithmetic. We introduce the cast, not because representing the ring size as int is necessarily an issue, but because we ended up with a wrong result. In my opinion how can 'inuse' be represented correctly and efficiently concerns the member of struct VirtQueue.

Here the important point is how conversions between signed unsigned integer types work in C.

"""
6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
"""

That is we get mod UINT16_MAX + 1 subtraction which is what we need if we want to calculate the difference between the two counters under the assumption that the actual conceptual difference (that is if the counters where of type arbitrary natural) is less or equal that queue size which is less than UINT16_MAX.

But no strong opinion here. If I did not manage to convince you now, just tell, and I will be happy to just go with your formulation.

Thanks,

Halil

#6 lore raw
In reply to Stefan Hajnoczi
Quoted 32 lines · Stefan Hajnoczi
Quoted 31 lines · Halil Pasic
Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Halil Pasic

Correct recalculation of vring->inuse after migration for the corner case where the avail_idx has already wrapped but used_idx not yet.

Fixes bccdef6b ("virtio: recalculate vq->inuse after migration")

---

Quoted 6 lines
Quoted 2 lines

I think we could also change the type of inuse to uint16_t. Would this be considered a good idea?

VRing.num is unsigned int. I would use the same type as num since this is a size, not an index.

Thanks. I asked myself the question why are VRing.num and inuse (at least theoretically arch depended width but at least 16 bit) int while the indexes of the available and used rings uint16_t. Only thing I can think of is some sort of optimization, because the only difference I can see is that the VRing.num is communicated via the transport while the indexes are a part (and/or corresponding to) of the ring layout (that is the shared memory virtio structures and have a fixed width).

Now I'm kind of in doubt: I think having a signed has the benefit of a negative value being more obviously wrong size (for a human looking at it) that a to large positive, but purely theoretically the maximum value of inuse is not guaranteed to fit int (as in C99 MIN_INT is 2^15 - 1).

Quoted 4 lines
Quoted 2 lines

What is your opinion? Should we keep 'inuse' as is, or should I change it to unsigned with v2 (or prepare a separate patch)?

I'm happy with unsigned. I've CCed Michael Tsirkin in case he has a preference.

I don't mind.

Quoted 38 lines
Quoted 27 lines
Quoted 4 lines
Quoted 4 lines
---
 hw/virtio/virtio.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 15 lines

hw/virtio/virtio.c int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) · line 1860

Quoted 11 lines
hw/virtio/virtio.c +3 -0
index 1af2de2..089c6f6 100644
@@ -1855,9 +1855,12 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
             /*
              * Some devices migrate VirtQueueElements that have been popped
              * from the avail ring but not yet returned to the used ring.
+             * Cast to uint16_t is OK because max ring size is 0x8000. Thus
+             * no the size of largest array indexable by an integral type
+             * can not be represented by the same type problem.

I think it would be safe up to max ring size UINT16_MAX - 1 (we need that extra value to distinguish between empty and full avail rings)?

Yeah.

Quoted 4 lines

The last sentence is hard to understand due to the double negative. I think you are saying "Since max ring size < UINT16_MAX it's safe to use uint16_t to represent the size of the ring".

Quoted 6 lines

You are not the first one complaining, so the sentence is definitively bad. What disturbs me regarding your formulation is that we do not use uint16_t to represent neither the ring size nor inuse.

How about "Since max ring size < UINT16_MAX it's safe to use modulo UINT16_MAX + 1 subtraction."?

That doesn't mention "representing the size of the ring" so it's unclear what "safe" means.

Stefan

#5 lore raw
In reply to Halil Pasic
Quoted 12 lines · Halil Pasic
Quoted 9 lines · Stefan Hajnoczi
Quoted 6 lines

You are not the first one complaining, so the sentence is definitively bad. What disturbs me regarding your formulation is that we do not use uint16_t to represent neither the ring size nor inuse.

How about "Since max ring size < UINT16_MAX it's safe to use modulo UINT16_MAX + 1 subtraction."?

That doesn't mention "representing the size of the ring" so it's unclear what "safe" means.

Quoted 8 lines
Quoted 2 lines

Stefan

IMHO it is not about representation but about correct arithmetic. We introduce the cast, not because representing the ring size as int is necessarily an issue, but because we ended up with a wrong result. In my opinion how can 'inuse' be represented correctly and efficiently concerns the member of struct VirtQueue.

Fair enough, the type of VirtQueue.inuse doesn't need to be justified at this point in the code.

Quoted 14 lines

Here the important point is how conversions between signed unsigned integer types work in C.

"""
6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
"""

Quoted 5 lines

That is we get mod UINT16_MAX + 1 subtraction which is what we need if we want to calculate the difference between the two counters under the assumption that the actual conceptual difference (that is if the counters where of type arbitrary natural) is less or equal that queue size which is less than UINT16_MAX.

- vdev->vq[i].inuse = vdev->vq[i].last_avail_idx -
- vdev->vq[i].used_idx;
+ vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
+ vdev->vq[i].used_idx);

Looking at C99 I learnt the cause of the bug is the integer promotion performed on the uint16_t subtraction operands. Previously I was only aware of integer promotion on varargs and function arguments where no prototype is declared.

So the original statement behaves like:

vdev->vq[i].inuse = (int)vdev->vq[i].last_avail_idx -

                    (int)vdev->vq[i].used_idx;

This is the real gotcha for me.

If you feel it helps to explain the signed -> unsigned cast behavior, feel free. I don't think it's necessary.

Stefan