Discussion of the implementations of VIRTIO specification
 help / color / mirror / Atom feed
* [virtio-dev] [PATCH] packed-ring: fix example code
@ 2018-11-07  7:18 Tiwei Bie
  0 siblings, 0 replies; only message in thread
From: Tiwei Bie @ 2018-11-07  7:18 UTC (permalink / raw)
  To: mst, virtio-dev; +Cc: tiwei.bie

We also need to check whether AVAIL bit equals to USED bit
when checking whether a descriptor is a used descriptor.
Below is an example:

Assuming ring size is 4, ring's initial state will be:

+----+----+----+----+
| 00 | 00 | 00 | 00 |
+----+----+----+----+

00 means AVAIL=0 USED=0, 01 means AVAIL=0 USED=1
10 means AVAIL=1 USED=0, 11 means AVAIL=1 USED=1

After driver made two descriptor chains available and each
chain consists of two descriptors, the ring could be:

+----+-----------+----+-----------+
| 10 | 10 (id=0) | 10 | 10 (id=1) |
+----+-----------+----+-----------+

After device processed all the available descriptors and made
them used (e.g. in order), the ring could be:

+-----------+----+-----------+----+
| 11 (id=0) | 10 | 11 (id=1) | 10 |
+-----------+----+-----------+----+

After driver processed all the used descriptors and made
one descriptor (not chained, just one descriptor) available,
the ring could be:

+-----------+----+----+----+
| 01 (id=0) | 10 | 11 | 10 |
+-----------+----+----+----+

After device made that descriptor used, the ring will be:

+-----------+----+----+----+
| 00 (id=0) | 10 | 11 | 10 |
+-----------+----+----+----+

If driver doesn't check whether AVAIL bit equals to USED bit
when checking whether a descriptor is a used descriptor, after
checking the first descriptor (whose AVAIL and USED bits are
0 and 0) and advancing the vq->next_used pointer, it will then
also treat the next descriptor, i.e. the second descriptor
(whose AVAIL and USED bits are 1 and 0) as used which is wrong.

Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
---
 packed-ring.tex | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/packed-ring.tex b/packed-ring.tex
index ebbad95..ec116d7 100644
--- a/packed-ring.tex
+++ b/packed-ring.tex
@@ -688,15 +688,17 @@ for (;;) {
         struct pvirtq_desc *d = vq->desc[vq->next_used];
 
         flags = d->flags;
+        bool avail = flags & VIRTQ_DESC_F_AVAIL;
         bool used = flags & VIRTQ_DESC_F_USED;
 
-        if (used != vq->used_wrap_count) {
+        if (avail != used || used != vq->used_wrap_count) {
                 vq->driver_event.flags = RING_EVENT_FLAGS_ENABLE;
                 memory_barrier();
 
                 flags = d->flags;
+                bool avail = flags & VIRTQ_DESC_F_AVAIL;
                 bool used = flags & VIRTQ_DESC_F_USED;
-                if (used != vq->used_wrap_count) {
+                if (avail != used || used != vq->used_wrap_count) {
                         break;
                 }
 
-- 
2.19.1


---------------------------------------------------------------------
To unsubscribe, e-mail: virtio-dev-unsubscribe@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-help@lists.oasis-open.org


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2018-11-07  7:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-11-07  7:18 [virtio-dev] [PATCH] packed-ring: fix example code Tiwei Bie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox