qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Maxime Coquelin <maxime.coquelin@redhat.com>
To: wexu@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org
Cc: mst@redhat.com, jfreimann@redhat.com, tiwei.bie@intel.com
Subject: Re: [Qemu-devel] [PATCH v1 09/16] virtio: fill/flush/pop for packed ring
Date: Fri, 30 Nov 2018 13:45:19 +0100	[thread overview]
Message-ID: <d0059b46-34c0-86d9-0840-ebe20bbdd561@redhat.com> (raw)
In-Reply-To: <1542895581-10721-10-git-send-email-wexu@redhat.com>

Hi Wei,

On 11/22/18 3:06 PM, wexu@redhat.com wrote:
> +void virtqueue_flush(VirtQueue *vq, unsigned int count)
> +{
> +    if (unlikely(vq->vdev->broken)) {
> +        vq->inuse -= count;
> +        return;
> +    }
> +
> +    if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
> +        virtqueue_packed_flush(vq, count);
> +    } else {
> +        virtqueue_split_flush(vq, count);
> +    }
> +}
> +
>   void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
>                       unsigned int len)
>   {
> @@ -1074,7 +1180,7 @@ static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_nu
>       return elem;
>   }
>   
> -void *virtqueue_pop(VirtQueue *vq, size_t sz)
> +static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)
>   {
>       unsigned int i, head, max;
>       VRingMemoryRegionCaches *caches;
> @@ -1089,9 +1195,6 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
>       VRingDesc desc;
>       int rc;
>   
> -    if (unlikely(vdev->broken)) {
> -        return NULL;
> -    }
>       rcu_read_lock();
>       if (virtio_queue_empty_rcu(vq)) {
>           goto done;
> @@ -1209,6 +1312,159 @@ err_undo_map:
>       goto done;
>   }
>   
> +static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
> +{
> +    unsigned int i, head, max;
> +    VRingMemoryRegionCaches *caches;
> +    MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
> +    MemoryRegionCache *cache;
> +    int64_t len;
> +    VirtIODevice *vdev = vq->vdev;
> +    VirtQueueElement *elem = NULL;
> +    unsigned out_num, in_num, elem_entries;
> +    hwaddr addr[VIRTQUEUE_MAX_SIZE];
> +    struct iovec iov[VIRTQUEUE_MAX_SIZE];
> +    VRingPackedDesc desc;
> +    uint16_t id;
> +
> +    rcu_read_lock();
> +    if (virtio_queue_packed_empty_rcu(vq)) {
> +        goto done;
> +    }
> +
> +    /* When we start there are none of either input nor output. */
> +    out_num = in_num = elem_entries = 0;
> +
> +    max = vq->vring.num;
> +
> +    if (vq->inuse >= vq->vring.num) {
> +        virtio_error(vdev, "Virtqueue size exceeded");
> +        goto done;
> +    }
> +
> +    head = vq->last_avail_idx;
> +    i = head;
> +
> +    caches = vring_get_region_caches(vq);
> +    cache = &caches->desc;
> +
> +    /* Empty check has been done at the beginning, so it is an available
> +     * entry already, make sure all fields has been exposed by guest */
> +    smp_rmb();
> +    vring_packed_desc_read(vdev, &desc, cache, i);
> +
> +    id = desc.id;
> +    if (desc.flags & VRING_DESC_F_INDIRECT) {
> +
> +        if (desc.len % sizeof(VRingPackedDesc)) {
> +            virtio_error(vdev, "Invalid size for indirect buffer table");
> +            goto done;
> +        }
> +
> +        /* loop over the indirect descriptor table */
> +        len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
> +                                       desc.addr, desc.len, false);
> +        cache = &indirect_desc_cache;
> +        if (len < desc.len) {
> +            virtio_error(vdev, "Cannot map indirect buffer");
> +            goto done;
> +        }
> +
> +        max = desc.len / sizeof(VRingPackedDesc);
> +        i = 0;
> +        vring_packed_desc_read(vdev, &desc, cache, i);
> +        /* Make sure we see all the fields*/
> +        smp_rmb();
> +    }
> +
> +    /* Collect all the descriptors */
> +    while (1) {
> +        bool map_ok;
> +
> +        if (desc.flags & VRING_DESC_F_WRITE) {
> +            map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
> +                                        iov + out_num,
> +                                        VIRTQUEUE_MAX_SIZE - out_num, true,
> +                                        desc.addr, desc.len);
> +        } else {
> +            if (in_num) {
> +                virtio_error(vdev, "Incorrect order for descriptors");
> +                goto err_undo_map;
> +            }
> +            map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
> +                                        VIRTQUEUE_MAX_SIZE, false,
> +                                        desc.addr, desc.len);
> +        }
> +        if (!map_ok) {
> +            goto err_undo_map;
> +        }
> +
> +        /* If we've got too many, that implies a descriptor loop. */
> +        if (++elem_entries > max) {
> +            virtio_error(vdev, "Looped descriptor");
> +            goto err_undo_map;
> +        }
> +
> +        if (++i >= vq->vring.num) {
> +            i -= vq->vring.num;
> +        }
> +
> +        if (desc.flags & VRING_DESC_F_NEXT) {
> +            vring_packed_desc_read(vq->vdev, &desc, cache, i);
> +        } else {
> +            break;
> +        }
> +    }
> +
> +    /* Now copy what we have collected and mapped */
> +    elem = virtqueue_alloc_element(sz, out_num, in_num);
> +    elem->index = id;
> +    for (i = 0; i < out_num; i++) {
> +        elem->out_addr[i] = addr[i];
> +        elem->out_sg[i] = iov[i];
> +    }
> +    for (i = 0; i < in_num; i++) {
> +        elem->in_addr[i] = addr[head + out_num + i];
> +        elem->in_sg[i] = iov[out_num + i];
> +    }
> +
> +    vq->last_avail_idx += (cache == &indirect_desc_cache) ?
> +                          1 : out_num + in_num;

I think you cannot rely on out_num and in_num to deduce the number of 
descriptors.

Indeed, in virtqueue_map_desc(), in_num and out_num can be incremented
by more than one for a single descriptor, when the desc buffer is not
contiguous in the QEMU address space.

  reply	other threads:[~2018-11-30 12:50 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-22 14:06 [Qemu-devel] [PATCH v1 00/16] packed ring virtio-net backend support wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 01/16] Update version for v3.1.0-rc2 release wexu
2018-11-22 14:31   ` Wei Xu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 02/16] virtio: introduce packed ring definitions wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 03/16] virtio: redefine structure & memory cache for packed ring wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 04/16] virtio: expand offset calculation " wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 05/16] virtio: add memory region init " wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 06/16] virtio: init wrap counter " wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 07/16] virtio: init and desc empty check " wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 08/16] virtio: get avail bytes " wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 09/16] virtio: fill/flush/pop " wexu
2018-11-30 12:45   ` Maxime Coquelin [this message]
2019-01-16 17:46     ` Wei Xu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 10/16] virtio: event suppression support " wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 11/16] virtio-net: fill head desc after done all in a chain wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 12/16] virtio: add userspace migration of packed ring wexu
2018-11-22 14:45   ` Jason Wang
2019-01-16 17:50     ` Wei Xu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 13/16] virtio: add vhost-net " wexu
2018-11-28 10:05   ` Maxime Coquelin
2018-11-28 10:34   ` Maxime Coquelin
2019-01-16 17:48     ` Wei Xu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 14/16] virtio: packed ring feature bit for userspace backend wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 15/16] vhost: enable packed ring wexu
2018-11-22 14:06 ` [Qemu-devel] [PATCH v1 16/16] virtio: enable packed ring via a new command line wexu
2018-11-22 17:57 ` [Qemu-devel] [PATCH v1 00/16] packed ring virtio-net backend support Maxime Coquelin
2018-11-23  5:57   ` Wei Xu
2018-11-25 13:59     ` Wei Xu
2018-11-23  6:45 ` no-reply
2018-11-23  6:45 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d0059b46-34c0-86d9-0840-ebe20bbdd561@redhat.com \
    --to=maxime.coquelin@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=jfreimann@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=tiwei.bie@intel.com \
    --cc=wexu@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).