From nobody Mon May 6 22:48:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1564718830; cv=none; d=zoho.com; s=zohoarc; b=FfPR58IPfLguTIzn1s5kJZqLEc9tNKWQ8v5eyzRoMQHtdUra7N+TxEki96xiXmxJEzqGa02npV8/skS0yku8uN2RbzIiq/6wzP04OqoyPpRQPKZT1mf9vFdIH661Zs5AIblJUZW7HoBMPDM0MpPmPKjZA4NzCmjC19KZzYSiG7Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564718830; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=DHeXuKs37jxFJSoa7bZ/jBE62vhI8HZ0cSiOoPlhAr4=; b=bqhq/5Zlk4lmTD6blowaQMg2rieaNSOz4S6bjoBIXfBLv4orzabbMePav5AGp3HvXA/PF9+knvEgDGTI7ckPm74dPgieeJkaqAIiPymiu6WLFPGKd+Uoy472eDrWxRvoObqxxBu4GR7/vgFz7c0movOF1qtRvd/OGGr5ZbXKEt4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1564718830475996.7292951968022; Thu, 1 Aug 2019 21:07:10 -0700 (PDT) Received: from localhost ([::1]:60294 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOqN-0006VY-MJ for importer@patchew.org; Fri, 02 Aug 2019 00:07:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50411) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOpo-0005cw-By for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1htOpn-0001Sa-CK for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41430) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1htOpn-0001Rx-77 for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:27 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8310131E7D0; Fri, 2 Aug 2019 04:06:26 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 45D7A1001B02; Fri, 2 Aug 2019 04:06:21 +0000 (UTC) From: Jason Wang To: mst@redhat.com Date: Fri, 2 Aug 2019 00:06:01 -0400 Message-Id: <20190802040606.22573-2-jasowang@redhat.com> In-Reply-To: <20190802040606.22573-1-jasowang@redhat.com> References: <20190802040606.22573-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 02 Aug 2019 04:06:26 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH V5 1/6] virtio: basic structure for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, tiwei.bie@intel.com, Jason Wang , qemu-devel@nongnu.org, maxime.coquelin@redhat.com, Wei Xu , jfreimann@redhat.com, weiyshay@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Define packed ring structure according to Qemu nomenclature, field data(wrap counter, etc) are also included. Signed-off-by: Wei Xu Signed-off-by: Jason Wang Reviewed-by: Jens Freimann --- hw/virtio/virtio.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index a94ea18a9c..981738fa19 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -39,6 +39,13 @@ typedef struct VRingDesc uint16_t next; } VRingDesc; =20 +typedef struct VRingPackedDesc { + uint64_t addr; + uint32_t len; + uint16_t id; + uint16_t flags; +} VRingPackedDesc; + typedef struct VRingAvail { uint16_t flags; @@ -77,17 +84,25 @@ typedef struct VRing VRingMemoryRegionCaches *caches; } VRing; =20 +typedef struct VRingPackedDescEvent { + uint16_t off_wrap; + uint16_t flags; +} VRingPackedDescEvent ; + struct VirtQueue { VRing vring; =20 /* Next head to pop */ uint16_t last_avail_idx; + bool last_avail_wrap_counter; =20 /* Last avail_idx read from VQ. */ uint16_t shadow_avail_idx; + bool shadow_avail_wrap_counter; =20 uint16_t used_idx; + bool used_wrap_counter; =20 /* Last used index value we have signalled on */ uint16_t signalled_used; --=20 2.18.1 From nobody Mon May 6 22:48:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1564718880; cv=none; d=zoho.com; s=zohoarc; b=f3ALSU0z0q08GcWjPhpymMLeMmmPCPT6R0UA9zp9+DWNG+oSO+GCNbeWySNf6UN2yVYg1j2E30x/UwA/igv63oMMQL2UaPqPQcInEwA90ItE7w8tYqcM5KzD8pOe6GdrlmbRHpzfT2tfAg8uEXZeFvRZ1sf7RSGJvA2HTEJtbyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564718880; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=phQ+VOJMb9Pki+A5gdb3g66ih5MNnh7brn0kFH6rPTY=; b=Z0Idrv6+8OeybpdGQpi5QGJlHhwrgiEiL6dxMT9OgHNPnFw26kXuLJw8ZR8ggJSDCRk8o5vpL0SFZXW1lK8L9Cvk/ZYW7XIf8ArR15WoGvxzpiDrrAIMND3ZyupAnQrUtI+GHYjqPax9vr+PRqYL/lgu/Dwh1EAuWf4m3+EZQeg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 156471888052335.705871270036596; Thu, 1 Aug 2019 21:08:00 -0700 (PDT) Received: from localhost ([::1]:60308 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOrC-0000J8-K4 for importer@patchew.org; Fri, 02 Aug 2019 00:07:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50461) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOpw-0005sY-Gn for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1htOpv-0001bO-HJ for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36214) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1htOpv-0001Zb-C0 for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:35 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A823D309DEE6; Fri, 2 Aug 2019 04:06:34 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5FE621001B02; Fri, 2 Aug 2019 04:06:26 +0000 (UTC) From: Jason Wang To: mst@redhat.com Date: Fri, 2 Aug 2019 00:06:02 -0400 Message-Id: <20190802040606.22573-3-jasowang@redhat.com> In-Reply-To: <20190802040606.22573-1-jasowang@redhat.com> References: <20190802040606.22573-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Fri, 02 Aug 2019 04:06:34 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH V5 2/6] virtio: device/driverr area size calculation refactor for split ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, tiwei.bie@intel.com, Jason Wang , qemu-devel@nongnu.org, maxime.coquelin@redhat.com, Wei Xu , jfreimann@redhat.com, weiyshay@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu There is slight size difference between split/packed rings. This is the refactor of split ring as well as a helper to expanding device and driver area size calculation for packed ring. Signed-off-by: Wei Xu Signed-off-by: Jason Wang Reviewed-by: Jens Freimann --- hw/virtio/virtio.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 981738fa19..ac21ab43e2 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -155,10 +155,8 @@ static void virtio_init_region_cache(VirtIODevice *vde= v, int n) VRingMemoryRegionCaches *old =3D vq->vring.caches; VRingMemoryRegionCaches *new =3D NULL; hwaddr addr, size; - int event_size; int64_t len; =20 - event_size =3D virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_I= DX) ? 2 : 0; =20 addr =3D vq->vring.desc; if (!addr) { @@ -173,7 +171,7 @@ static void virtio_init_region_cache(VirtIODevice *vdev= , int n) goto err_desc; } =20 - size =3D virtio_queue_get_used_size(vdev, n) + event_size; + size =3D virtio_queue_get_used_size(vdev, n); len =3D address_space_cache_init(&new->used, vdev->dma_as, vq->vring.used, size, true); if (len < size) { @@ -181,7 +179,7 @@ static void virtio_init_region_cache(VirtIODevice *vdev= , int n) goto err_used; } =20 - size =3D virtio_queue_get_avail_size(vdev, n) + event_size; + size =3D virtio_queue_get_avail_size(vdev, n); len =3D address_space_cache_init(&new->avail, vdev->dma_as, vq->vring.avail, size, false); if (len < size) { @@ -2410,14 +2408,20 @@ hwaddr virtio_queue_get_desc_size(VirtIODevice *vde= v, int n) =20 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n) { + int s; + + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingAvail, ring) + - sizeof(uint16_t) * vdev->vq[n].vring.num; + sizeof(uint16_t) * vdev->vq[n].vring.num + s; } =20 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n) { + int s; + + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingUsed, ring) + - sizeof(VRingUsedElem) * vdev->vq[n].vring.num; + sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s; } =20 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) --=20 2.18.1 From nobody Mon May 6 22:48:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1564718919; cv=none; d=zoho.com; s=zohoarc; b=cTsyOmQ/goLKVlDGxer2GDOEgFuGYvnfXdCTkhjsIDjrYFV3tWrQRin8N3/WU6g73zi3q4EWqMJU99ACpLXgbzYD3JeOvsFfEwuSyr2QRPZ92RBwwJjk6pEncuN6/XgAM8em8zaQpOt8a7wXMqbAnnmeEq6cANPHzEYWR5rOMrk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564718919; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=073Ti801OiT+r6B2pymHvxugtn2HlhyW4bSdY0fEPw4=; b=i2WvBPZ8y1Jn6QpykpnqX+dF33BeanTTZ34y4ghO4a7j8J0KPscYGP8Yt2xpaA1i/1U3C1n/T2nRktu5mrt6m9HXlm1W+4SvfrWhCmUkm8m+YZ8Fh2rnxMZZooXY2x8cvCnAk45F/igns9Dqda+UWcvpBInx534+25ocdVTMG9M= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1564718919801918.0660326121172; Thu, 1 Aug 2019 21:08:39 -0700 (PDT) Received: from localhost ([::1]:60320 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOru-0001rW-OF for importer@patchew.org; Fri, 02 Aug 2019 00:08:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50499) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOq5-0006Ff-W2 for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1htOq2-0001f5-2D for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43031) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1htOq1-0001er-NU for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:42 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 066AD85545; Fri, 2 Aug 2019 04:06:41 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A9AB1001B02; Fri, 2 Aug 2019 04:06:34 +0000 (UTC) From: Jason Wang To: mst@redhat.com Date: Fri, 2 Aug 2019 00:06:03 -0400 Message-Id: <20190802040606.22573-4-jasowang@redhat.com> In-Reply-To: <20190802040606.22573-1-jasowang@redhat.com> References: <20190802040606.22573-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 02 Aug 2019 04:06:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH V5 3/6] virtio: basic packed virtqueue support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, tiwei.bie@intel.com, Jason Wang , qemu-devel@nongnu.org, maxime.coquelin@redhat.com, Wei Xu , jfreimann@redhat.com, weiyshay@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch implements basic support for the packed virtqueue. Compare the split virtqueue which has three rings, packed virtqueue only have one which is supposed to have better cache utilization and more hardware friendly. Please refer virtio specification for more information. Signed-off-by: Wei Xu Signed-off-by: Jason Wang --- hw/block/virtio-blk.c | 2 +- hw/char/virtio-serial-bus.c | 2 +- hw/scsi/virtio-scsi.c | 3 +- hw/virtio/virtio.c | 900 +++++++++++++++++++++++++++++++++--- include/hw/virtio/virtio.h | 10 +- 5 files changed, 837 insertions(+), 80 deletions(-) diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index cbb3729158..122d9fd6f1 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -1047,7 +1047,7 @@ static void virtio_blk_save_device(VirtIODevice *vdev= , QEMUFile *f) qemu_put_be32(f, virtio_get_queue_index(req->vq)); } =20 - qemu_put_virtqueue_element(f, &req->elem); + qemu_put_virtqueue_element(vdev, f, &req->elem); req =3D req->next; } qemu_put_sbyte(f, 0); diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index f7a54f261b..d0d861386c 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -705,7 +705,7 @@ static void virtio_serial_save_device(VirtIODevice *vde= v, QEMUFile *f) if (elem_popped) { qemu_put_be32s(f, &port->iov_idx); qemu_put_be64s(f, &port->iov_offset); - qemu_put_virtqueue_element(f, port->elem); + qemu_put_virtqueue_element(vdev, f, port->elem); } } } diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 8b9e5e2b49..41ea855e0a 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -188,11 +188,12 @@ static void virtio_scsi_save_request(QEMUFile *f, SCS= IRequest *sreq) { VirtIOSCSIReq *req =3D sreq->hba_private; VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(req->dev); + VirtIODevice *vdev =3D VIRTIO_DEVICE(req->dev); uint32_t n =3D virtio_get_queue_index(req->vq) - 2; =20 assert(n < vs->conf.num_queues); qemu_put_be32s(f, &n); - qemu_put_virtqueue_element(f, &req->elem); + qemu_put_virtqueue_element(vdev, f, &req->elem); } =20 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index ac21ab43e2..4105fe2448 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -92,6 +92,7 @@ typedef struct VRingPackedDescEvent { struct VirtQueue { VRing vring; + VirtQueueElement *used_elems; =20 /* Next head to pop */ uint16_t last_avail_idx; @@ -156,6 +157,7 @@ static void virtio_init_region_cache(VirtIODevice *vdev= , int n) VRingMemoryRegionCaches *new =3D NULL; hwaddr addr, size; int64_t len; + bool packed; =20 =20 addr =3D vq->vring.desc; @@ -164,8 +166,10 @@ static void virtio_init_region_cache(VirtIODevice *vde= v, int n) } new =3D g_new0(VRingMemoryRegionCaches, 1); size =3D virtio_queue_get_desc_size(vdev, n); + packed =3D virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ? + true : false; len =3D address_space_cache_init(&new->desc, vdev->dma_as, - addr, size, false); + addr, size, packed); if (len < size) { virtio_error(vdev, "Cannot map desc"); goto err_desc; @@ -221,8 +225,8 @@ void virtio_queue_update_rings(VirtIODevice *vdev, int = n) } =20 /* Called within rcu_read_lock(). */ -static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc, - MemoryRegionCache *cache, int i) +static void vring_split_desc_read(VirtIODevice *vdev, VRingDesc *desc, + MemoryRegionCache *cache, int i) { address_space_read_cached(cache, i * sizeof(VRingDesc), desc, sizeof(VRingDesc)); @@ -366,6 +370,95 @@ int virtio_queue_ready(VirtQueue *vq) return vq->vring.avail !=3D 0; } =20 +static void vring_packed_desc_read_flags(VirtIODevice *vdev, + uint16_t *flags, + MemoryRegionCache *cache, + int i) +{ + address_space_read_cached(cache, + i * sizeof(VRingPackedDesc) + + offsetof(VRingPackedDesc, flags), + flags, sizeof(*flags)); + virtio_tswap16s(vdev, flags); +} + +static void vring_packed_desc_read(VirtIODevice *vdev, + VRingPackedDesc *desc, + MemoryRegionCache *cache, + int i, bool strict_order) +{ + hwaddr off =3D i * sizeof(VRingPackedDesc); + + vring_packed_desc_read_flags(vdev, &desc->flags, cache, i); + + if (strict_order) { + /* Make sure flags is read before the rest fields. */ + smp_rmb(); + } + + address_space_read_cached(cache, off + offsetof(VRingPackedDesc, addr), + &desc->addr, sizeof(desc->addr)); + address_space_read_cached(cache, off + offsetof(VRingPackedDesc, id), + &desc->id, sizeof(desc->id)); + address_space_read_cached(cache, off + offsetof(VRingPackedDesc, len), + &desc->len, sizeof(desc->len)); + virtio_tswap64s(vdev, &desc->addr); + virtio_tswap16s(vdev, &desc->id); + virtio_tswap32s(vdev, &desc->len); +} + +static void vring_packed_desc_write_data(VirtIODevice *vdev, + VRingPackedDesc *desc, + MemoryRegionCache *cache, + int i) +{ + hwaddr off_id =3D i * sizeof(VRingPackedDesc) + + offsetof(VRingPackedDesc, id); + hwaddr off_len =3D i * sizeof(VRingPackedDesc) + + offsetof(VRingPackedDesc, len); + + virtio_tswap32s(vdev, &desc->len); + virtio_tswap16s(vdev, &desc->id); + address_space_write_cached(cache, off_id, &desc->id, sizeof(desc->id)); + address_space_cache_invalidate(cache, off_id, sizeof(desc->id)); + address_space_write_cached(cache, off_len, &desc->len, sizeof(desc->le= n)); + address_space_cache_invalidate(cache, off_len, sizeof(desc->len)); +} + +static void vring_packed_desc_write_flags(VirtIODevice *vdev, + VRingPackedDesc *desc, + MemoryRegionCache *cache, + int i) +{ + hwaddr off =3D i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc,= flags); + + virtio_tswap16s(vdev, &desc->flags); + address_space_write_cached(cache, off, &desc->flags, sizeof(desc->flag= s)); + address_space_cache_invalidate(cache, off, sizeof(desc->flags)); +} + +static void vring_packed_desc_write(VirtIODevice *vdev, + VRingPackedDesc *desc, + MemoryRegionCache *cache, + int i, bool strict_order) +{ + vring_packed_desc_write_data(vdev, desc, cache, i); + if (strict_order) { + /* Make sure data is wrote before flags. */ + smp_wmb(); + } + vring_packed_desc_write_flags(vdev, desc, cache, i); +} + +static inline bool is_desc_avail(uint16_t flags, bool wrap_counter) +{ + bool avail, used; + + avail =3D !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); + used =3D !!(flags & (1 << VRING_PACKED_DESC_F_USED)); + return (avail !=3D used) && (avail =3D=3D wrap_counter); +} + /* Fetch avail_idx from VQ memory only when we really need to know if * guest has added some buffers. * Called within rcu_read_lock(). */ @@ -386,7 +479,7 @@ static int virtio_queue_empty_rcu(VirtQueue *vq) return vring_avail_idx(vq) =3D=3D vq->last_avail_idx; } =20 -int virtio_queue_empty(VirtQueue *vq) +static int virtio_queue_split_empty(VirtQueue *vq) { bool empty; =20 @@ -408,6 +501,41 @@ int virtio_queue_empty(VirtQueue *vq) return empty; } =20 +static int virtio_queue_packed_empty_rcu(VirtQueue *vq) +{ + struct VRingPackedDesc desc; + VRingMemoryRegionCaches *cache; + + if (unlikely(!vq->vring.desc)) { + return 1; + } + + cache =3D vring_get_region_caches(vq); + vring_packed_desc_read_flags(vq->vdev, &desc.flags, &cache->desc, + vq->last_avail_idx); + + return !is_desc_avail(desc.flags, vq->last_avail_wrap_counter); +} + +static int virtio_queue_packed_empty(VirtQueue *vq) +{ + bool empty; + + rcu_read_lock(); + empty =3D virtio_queue_packed_empty_rcu(vq); + rcu_read_unlock(); + return empty; +} + +int virtio_queue_empty(VirtQueue *vq) +{ + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + return virtio_queue_packed_empty(vq); + } else { + return virtio_queue_split_empty(vq); + } +} + static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, unsigned int len) { @@ -445,10 +573,25 @@ static void virtqueue_unmap_sg(VirtQueue *vq, const V= irtQueueElement *elem, void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem, unsigned int len) { - vq->inuse--; + vq->inuse -=3D elem->ndescs; virtqueue_unmap_sg(vq, elem, len); } =20 +static void virtqueue_split_rewind(VirtQueue *vq, unsigned int num) +{ + vq->last_avail_idx -=3D num; +} + +static void virtqueue_packed_rewind(VirtQueue *vq, unsigned int num) +{ + if (vq->last_avail_idx < num) { + vq->last_avail_idx =3D vq->vring.num + vq->last_avail_idx - num; + vq->last_avail_wrap_counter ^=3D 1; + } else { + vq->last_avail_idx -=3D num; + } +} + /* virtqueue_unpop: * @vq: The #VirtQueue * @elem: The #VirtQueueElement @@ -460,7 +603,13 @@ void virtqueue_detach_element(VirtQueue *vq, const Vir= tQueueElement *elem, void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem, unsigned int len) { - vq->last_avail_idx--; + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_rewind(vq, 1); + } else { + virtqueue_split_rewind(vq, 1); + } + virtqueue_detach_element(vq, elem, len); } =20 @@ -481,25 +630,22 @@ bool virtqueue_rewind(VirtQueue *vq, unsigned int num) if (num > vq->inuse) { return false; } - vq->last_avail_idx -=3D num; + vq->inuse -=3D num; + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_rewind(vq, num); + } else { + virtqueue_split_rewind(vq, num); + } return true; } =20 /* Called within rcu_read_lock(). */ -void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, +static void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *el= em, unsigned int len, unsigned int idx) { VRingUsedElem uelem; =20 - trace_virtqueue_fill(vq, elem, len, idx); - - virtqueue_unmap_sg(vq, elem, len); - - if (unlikely(vq->vdev->broken)) { - return; - } - if (unlikely(!vq->vring.used)) { return; } @@ -511,16 +657,71 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueEle= ment *elem, vring_used_write(vq, &uelem, idx); } =20 -/* Called within rcu_read_lock(). */ -void virtqueue_flush(VirtQueue *vq, unsigned int count) +static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *e= lem, + unsigned int len, unsigned int idx) { - uint16_t old, new; + vq->used_elems[idx].index =3D elem->index; + vq->used_elems[idx].len =3D len; + vq->used_elems[idx].ndescs =3D elem->ndescs; +} + +static void virtqueue_packed_fill_desc(VirtQueue *vq, + const VirtQueueElement *elem, + unsigned int idx, + bool strict_order) +{ + uint16_t head; + VRingMemoryRegionCaches *caches; + VRingPackedDesc desc =3D { + .id =3D elem->index, + .len =3D elem->len, + }; + bool wrap_counter =3D vq->used_wrap_counter; + + if (unlikely(!vq->vring.desc)) { + return; + } + + head =3D vq->used_idx + idx; + if (head >=3D vq->vring.num) { + head -=3D vq->vring.num; + wrap_counter ^=3D 1; + } + if (wrap_counter) { + desc.flags |=3D (1 << VRING_PACKED_DESC_F_AVAIL); + desc.flags |=3D (1 << VRING_PACKED_DESC_F_USED); + } else { + desc.flags &=3D ~(1 << VRING_PACKED_DESC_F_AVAIL); + desc.flags &=3D ~(1 << VRING_PACKED_DESC_F_USED); + } + + caches =3D vring_get_region_caches(vq); + vring_packed_desc_write(vq->vdev, &desc, &caches->desc, head, strict_o= rder); +} + +void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, + unsigned int len, unsigned int idx) +{ + trace_virtqueue_fill(vq, elem, len, idx); + + virtqueue_unmap_sg(vq, elem, len); =20 if (unlikely(vq->vdev->broken)) { - vq->inuse -=3D count; return; } =20 + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_fill(vq, elem, len, idx); + } else { + virtqueue_split_fill(vq, elem, len, idx); + } +} + +/* Called within rcu_read_lock(). */ +static void virtqueue_split_flush(VirtQueue *vq, unsigned int count) +{ + uint16_t old, new; + if (unlikely(!vq->vring.used)) { return; } @@ -536,6 +737,43 @@ void virtqueue_flush(VirtQueue *vq, unsigned int count) vq->signalled_used_valid =3D false; } =20 +static void virtqueue_packed_flush(VirtQueue *vq, unsigned int count) +{ + unsigned int i, ndescs =3D 0; + + if (unlikely(!vq->vring.desc)) { + return; + } + + for (i =3D 1; i < count; i++) { + virtqueue_packed_fill_desc(vq, &vq->used_elems[i], i, false); + ndescs +=3D vq->used_elems[i].ndescs; + } + virtqueue_packed_fill_desc(vq, &vq->used_elems[0], 0, true); + ndescs +=3D vq->used_elems[0].ndescs; + + vq->inuse -=3D ndescs; + vq->used_idx +=3D ndescs; + if (vq->used_idx >=3D vq->vring.num) { + vq->used_idx -=3D vq->vring.num; + vq->used_wrap_counter ^=3D 1; + } +} + +void virtqueue_flush(VirtQueue *vq, unsigned int count) +{ + if (unlikely(vq->vdev->broken)) { + vq->inuse -=3D 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) { @@ -588,9 +826,9 @@ enum { VIRTQUEUE_READ_DESC_MORE =3D 1, /* more buffers in chain */ }; =20 -static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc, - MemoryRegionCache *desc_cache, unsigne= d int max, - unsigned int *next) +static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *d= esc, + MemoryRegionCache *desc_cache, + unsigned int max, unsigned int *= next) { /* If this descriptor says it doesn't chain, we're done. */ if (!(desc->flags & VRING_DESC_F_NEXT)) { @@ -607,13 +845,13 @@ static int virtqueue_read_next_desc(VirtIODevice *vde= v, VRingDesc *desc, return VIRTQUEUE_READ_DESC_ERROR; } =20 - vring_desc_read(vdev, desc, desc_cache, *next); + vring_split_desc_read(vdev, desc, desc_cache, *next); return VIRTQUEUE_READ_DESC_MORE; } =20 -void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, - unsigned int *out_bytes, - unsigned max_in_bytes, unsigned max_out_byt= es) +static void virtqueue_split_get_avail_bytes(VirtQueue *vq, + unsigned int *in_bytes, unsigned int *out_byte= s, + unsigned max_in_bytes, unsigned max_out_bytes) { VirtIODevice *vdev =3D vq->vdev; unsigned int max, idx; @@ -623,27 +861,12 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigne= d int *in_bytes, int64_t len =3D 0; int rc; =20 - if (unlikely(!vq->vring.desc)) { - if (in_bytes) { - *in_bytes =3D 0; - } - if (out_bytes) { - *out_bytes =3D 0; - } - return; - } - rcu_read_lock(); idx =3D vq->last_avail_idx; total_bufs =3D in_total =3D out_total =3D 0; =20 max =3D vq->vring.num; caches =3D vring_get_region_caches(vq); - if (caches->desc.len < max * sizeof(VRingDesc)) { - virtio_error(vdev, "Cannot map descriptor ring"); - goto err; - } - while ((rc =3D virtqueue_num_heads(vq, idx)) > 0) { MemoryRegionCache *desc_cache =3D &caches->desc; unsigned int num_bufs; @@ -656,7 +879,7 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned = int *in_bytes, goto err; } =20 - vring_desc_read(vdev, &desc, desc_cache, i); + vring_split_desc_read(vdev, &desc, desc_cache, i); =20 if (desc.flags & VRING_DESC_F_INDIRECT) { if (!desc.len || (desc.len % sizeof(VRingDesc))) { @@ -682,7 +905,7 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned = int *in_bytes, =20 max =3D desc.len / sizeof(VRingDesc); num_bufs =3D i =3D 0; - vring_desc_read(vdev, &desc, desc_cache, i); + vring_split_desc_read(vdev, &desc, desc_cache, i); } =20 do { @@ -701,7 +924,7 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned = int *in_bytes, goto done; } =20 - rc =3D virtqueue_read_next_desc(vdev, &desc, desc_cache, max, = &i); + rc =3D virtqueue_split_read_next_desc(vdev, &desc, desc_cache,= max, &i); } while (rc =3D=3D VIRTQUEUE_READ_DESC_MORE); =20 if (rc =3D=3D VIRTQUEUE_READ_DESC_ERROR) { @@ -723,17 +946,197 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsign= ed int *in_bytes, done: address_space_cache_destroy(&indirect_desc_cache); if (in_bytes) { - *in_bytes =3D in_total; + *in_bytes =3D in_total; + } + if (out_bytes) { + *out_bytes =3D out_total; + } + rcu_read_unlock(); + return; + +err: + in_total =3D out_total =3D 0; + goto done; +} + +static int virtqueue_packed_read_next_desc(VirtQueue *vq, + VRingPackedDesc *desc, + MemoryRegionCache + *desc_cache, + unsigned int max, + unsigned int *next, + bool indirect) +{ + /* If this descriptor says it doesn't chain, we're done. */ + if (!indirect && !(desc->flags & VRING_DESC_F_NEXT)) { + return VIRTQUEUE_READ_DESC_DONE; + } + + ++*next; + if (*next =3D=3D max) { + if (indirect) { + return VIRTQUEUE_READ_DESC_DONE; + } else { + (*next) -=3D vq->vring.num; + } + } + + vring_packed_desc_read(vq->vdev, desc, desc_cache, *next, false); + return VIRTQUEUE_READ_DESC_MORE; +} + +static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, + unsigned int *in_bytes, + unsigned int *out_bytes, + unsigned max_in_bytes, + unsigned max_out_bytes) +{ + VirtIODevice *vdev =3D vq->vdev; + unsigned int max, idx; + unsigned int total_bufs, in_total, out_total; + MemoryRegionCache *desc_cache; + VRingMemoryRegionCaches *caches; + MemoryRegionCache indirect_desc_cache =3D MEMORY_REGION_CACHE_INVALID; + int64_t len =3D 0; + VRingPackedDesc desc; + bool wrap_counter; + + rcu_read_lock(); + idx =3D vq->last_avail_idx; + wrap_counter =3D vq->last_avail_wrap_counter; + total_bufs =3D in_total =3D out_total =3D 0; + + max =3D vq->vring.num; + caches =3D vring_get_region_caches(vq); + + for (;;) { + unsigned int num_bufs =3D total_bufs; + unsigned int i =3D idx; + int rc; + + desc_cache =3D &caches->desc; + vring_packed_desc_read(vdev, &desc, desc_cache, idx, true); + if (!is_desc_avail(desc.flags, wrap_counter)) { + break; + } + + if (desc.flags & VRING_DESC_F_INDIRECT) { + if (desc.len % sizeof(VRingPackedDesc)) { + virtio_error(vdev, "Invalid size for indirect buffer table= "); + goto err; + } + + /* If we've got too many, that implies a descriptor loop. */ + if (num_bufs >=3D max) { + virtio_error(vdev, "Looped descriptor"); + goto err; + } + + /* loop over the indirect descriptor table */ + len =3D address_space_cache_init(&indirect_desc_cache, + vdev->dma_as, + desc.addr, desc.len, false); + desc_cache =3D &indirect_desc_cache; + if (len < desc.len) { + virtio_error(vdev, "Cannot map indirect buffer"); + goto err; + } + + max =3D desc.len / sizeof(VRingPackedDesc); + num_bufs =3D i =3D 0; + vring_packed_desc_read(vdev, &desc, desc_cache, i, false); + } + + do { + /* If we've got too many, that implies a descriptor loop. */ + if (++num_bufs > max) { + virtio_error(vdev, "Looped descriptor"); + goto err; + } + + if (desc.flags & VRING_DESC_F_WRITE) { + in_total +=3D desc.len; + } else { + out_total +=3D desc.len; + } + if (in_total >=3D max_in_bytes && out_total >=3D max_out_bytes= ) { + goto done; + } + + rc =3D virtqueue_packed_read_next_desc(vq, &desc, desc_cache, = max, + &i, desc_cache =3D=3D + &indirect_desc_cache); + } while (rc =3D=3D VIRTQUEUE_READ_DESC_MORE); + + if (desc_cache =3D=3D &indirect_desc_cache) { + address_space_cache_destroy(&indirect_desc_cache); + total_bufs++; + idx++; + } else { + idx +=3D num_bufs - total_bufs; + total_bufs =3D num_bufs; + } + + if (idx >=3D vq->vring.num) { + idx -=3D vq->vring.num; + wrap_counter ^=3D 1; + } + } + + /* Record the index and wrap counter for a kick we want */ + vq->shadow_avail_idx =3D idx; + vq->shadow_avail_wrap_counter =3D wrap_counter; +done: + address_space_cache_destroy(&indirect_desc_cache); + if (in_bytes) { + *in_bytes =3D in_total; + } + if (out_bytes) { + *out_bytes =3D out_total; + } + rcu_read_unlock(); + return; + +err: + in_total =3D out_total =3D 0; + goto done; +} + +void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, + unsigned int *out_bytes, + unsigned max_in_bytes, unsigned max_out_byt= es) +{ + uint16_t desc_size; + VRingMemoryRegionCaches *caches; + + if (unlikely(!vq->vring.desc)) { + goto err; + } + + caches =3D vring_get_region_caches(vq); + desc_size =3D virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ? + sizeof(VRingPackedDesc) : sizeof(VRingDesc= ); + if (caches->desc.len < vq->vring.num * desc_size) { + virtio_error(vq->vdev, "Cannot map descriptor ring"); + goto err; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes, + max_in_bytes, max_out_bytes); + } else { + virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes, + max_in_bytes, max_out_bytes); + } + + return; +err: + if (in_bytes) { + *in_bytes =3D 0; } if (out_bytes) { - *out_bytes =3D out_total; + *out_bytes =3D 0; } - rcu_read_unlock(); - return; - -err: - in_total =3D out_total =3D 0; - goto done; } =20 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, @@ -860,7 +1263,7 @@ static void *virtqueue_alloc_element(size_t sz, unsign= ed out_num, unsigned in_nu return elem; } =20 -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; @@ -875,9 +1278,6 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) VRingDesc desc; int rc; =20 - if (unlikely(vdev->broken)) { - return NULL; - } rcu_read_lock(); if (virtio_queue_empty_rcu(vq)) { goto done; @@ -913,7 +1313,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) } =20 desc_cache =3D &caches->desc; - vring_desc_read(vdev, &desc, desc_cache, i); + vring_split_desc_read(vdev, &desc, desc_cache, i); if (desc.flags & VRING_DESC_F_INDIRECT) { if (!desc.len || (desc.len % sizeof(VRingDesc))) { virtio_error(vdev, "Invalid size for indirect buffer table"); @@ -931,7 +1331,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) =20 max =3D desc.len / sizeof(VRingDesc); i =3D 0; - vring_desc_read(vdev, &desc, desc_cache, i); + vring_split_desc_read(vdev, &desc, desc_cache, i); } =20 /* Collect all the descriptors */ @@ -962,7 +1362,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) goto err_undo_map; } =20 - rc =3D virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i); + rc =3D virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max= , &i); } while (rc =3D=3D VIRTQUEUE_READ_DESC_MORE); =20 if (rc =3D=3D VIRTQUEUE_READ_DESC_ERROR) { @@ -972,6 +1372,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) /* Now copy what we have collected and mapped */ elem =3D virtqueue_alloc_element(sz, out_num, in_num); elem->index =3D head; + elem->ndescs =3D 1; for (i =3D 0; i < out_num; i++) { elem->out_addr[i] =3D addr[i]; elem->out_sg[i] =3D iov[i]; @@ -995,13 +1396,202 @@ err_undo_map: goto done; } =20 -/* virtqueue_drop_all: - * @vq: The #VirtQueue - * Drops all queued buffers and indicates them to the guest - * as if they are done. Useful when buffers can not be - * processed but must be returned to the guest. - */ -unsigned int virtqueue_drop_all(VirtQueue *vq) +static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) +{ + unsigned int i, max; + VRingMemoryRegionCaches *caches; + MemoryRegionCache indirect_desc_cache =3D MEMORY_REGION_CACHE_INVALID; + MemoryRegionCache *desc_cache; + int64_t len; + VirtIODevice *vdev =3D vq->vdev; + VirtQueueElement *elem =3D NULL; + unsigned out_num, in_num, elem_entries; + hwaddr addr[VIRTQUEUE_MAX_SIZE]; + struct iovec iov[VIRTQUEUE_MAX_SIZE]; + VRingPackedDesc desc; + uint16_t id; + int rc; + + 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 =3D in_num =3D elem_entries =3D 0; + + max =3D vq->vring.num; + + if (vq->inuse >=3D vq->vring.num) { + virtio_error(vdev, "Virtqueue size exceeded"); + goto done; + } + + i =3D vq->last_avail_idx; + + caches =3D vring_get_region_caches(vq); + if (caches->desc.len < max * sizeof(VRingDesc)) { + virtio_error(vdev, "Cannot map descriptor ring"); + goto done; + } + + desc_cache =3D &caches->desc; + vring_packed_desc_read(vdev, &desc, desc_cache, i, true); + id =3D 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 =3D address_space_cache_init(&indirect_desc_cache, vdev->dma_a= s, + desc.addr, desc.len, false); + desc_cache =3D &indirect_desc_cache; + if (len < desc.len) { + virtio_error(vdev, "Cannot map indirect buffer"); + goto done; + } + + max =3D desc.len / sizeof(VRingPackedDesc); + i =3D 0; + vring_packed_desc_read(vdev, &desc, desc_cache, i, false); + } + + /* Collect all the descriptors */ + do { + bool map_ok; + + if (desc.flags & VRING_DESC_F_WRITE) { + map_ok =3D 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 =3D 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; + } + + rc =3D virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,= &i, + desc_cache =3D=3D + &indirect_desc_cache); + } while (rc =3D=3D VIRTQUEUE_READ_DESC_MORE); + + /* Now copy what we have collected and mapped */ + elem =3D virtqueue_alloc_element(sz, out_num, in_num); + for (i =3D 0; i < out_num; i++) { + elem->out_addr[i] =3D addr[i]; + elem->out_sg[i] =3D iov[i]; + } + for (i =3D 0; i < in_num; i++) { + elem->in_addr[i] =3D addr[out_num + i]; + elem->in_sg[i] =3D iov[out_num + i]; + } + + elem->index =3D id; + elem->ndescs =3D (desc_cache =3D=3D &indirect_desc_cache) ? 1 : elem_e= ntries; + vq->last_avail_idx +=3D elem->ndescs; + vq->inuse +=3D elem->ndescs; + + if (vq->last_avail_idx >=3D vq->vring.num) { + vq->last_avail_idx -=3D vq->vring.num; + vq->last_avail_wrap_counter ^=3D 1; + } + + vq->shadow_avail_idx =3D vq->last_avail_idx; + vq->shadow_avail_wrap_counter =3D vq->last_avail_wrap_counter; + + trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); +done: + address_space_cache_destroy(&indirect_desc_cache); + rcu_read_unlock(); + + return elem; + +err_undo_map: + virtqueue_undo_map_desc(out_num, in_num, iov); + goto done; +} + +void *virtqueue_pop(VirtQueue *vq, size_t sz) +{ + if (unlikely(vq->vdev->broken)) { + return NULL; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + return virtqueue_packed_pop(vq, sz); + } else { + return virtqueue_split_pop(vq, sz); + } +} + +static unsigned int virtqueue_packed_drop_all(VirtQueue *vq) +{ + VRingMemoryRegionCaches *caches; + MemoryRegionCache *desc_cache; + unsigned int dropped =3D 0; + VirtQueueElement elem =3D {}; + VirtIODevice *vdev =3D vq->vdev; + VRingPackedDesc desc; + + if (unlikely(vdev->broken)) { + return 0; + } + + caches =3D vring_get_region_caches(vq); + desc_cache =3D &caches->desc; + + virtio_queue_set_notification(vq, 0); + + while (vq->inuse < vq->vring.num) { + unsigned int idx =3D vq->last_avail_idx; + /* + * works similar to virtqueue_pop but does not map buffers + * and does not allocate any memory. + */ + vring_packed_desc_read(vdev, &desc, desc_cache, + vq->last_avail_idx , true); + if (!is_desc_avail(desc.flags, vq->last_avail_wrap_counter)) { + break; + } + elem.index =3D desc.id; + elem.ndescs =3D 1; + while (virtqueue_packed_read_next_desc(vq, &desc, desc_cache, + vq->vring.num, &idx, false)= ) { + ++elem.ndescs; + } + /* + * immediately push the element, nothing to unmap + * as both in_num and out_num are set to 0. + */ + virtqueue_push(vq, &elem, 0); + dropped++; + vq->last_avail_idx +=3D elem.ndescs; + if (vq->last_avail_idx >=3D vq->vring.num) { + vq->last_avail_idx -=3D vq->vring.num; + vq->last_avail_wrap_counter ^=3D 1; + } + } + + return dropped; +} + +static unsigned int virtqueue_split_drop_all(VirtQueue *vq) { unsigned int dropped =3D 0; VirtQueueElement elem =3D {}; @@ -1033,6 +1623,23 @@ unsigned int virtqueue_drop_all(VirtQueue *vq) return dropped; } =20 +/* virtqueue_drop_all: + * @vq: The #VirtQueue + * Drops all queued buffers and indicates them to the guest + * as if they are done. Useful when buffers can not be + * processed but must be returned to the guest. + */ +unsigned int virtqueue_drop_all(VirtQueue *vq) +{ + struct VirtIODevice *vdev =3D vq->vdev; + + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return virtqueue_packed_drop_all(vq); + } else { + return virtqueue_split_drop_all(vq); + } +} + /* Reading and writing a structure directly to QEMUFile is *awful*, but * it is what QEMU has always done by mistake. We can change it sooner * or later by bumping the version number of the affected vm states. @@ -1089,11 +1696,16 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev= , QEMUFile *f, size_t sz) elem->out_sg[i].iov_len =3D data.out_sg[i].iov_len; } =20 + if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + qemu_get_be32s(f, &elem->ndescs); + } + virtqueue_map(vdev, elem); return elem; } =20 -void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem) +void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, + VirtQueueElement *elem) { VirtQueueElementOld data; int i; @@ -1121,6 +1733,11 @@ void qemu_put_virtqueue_element(QEMUFile *f, VirtQue= ueElement *elem) /* Do not save iov_base as above. */ data.out_sg[i].iov_len =3D elem->out_sg[i].iov_len; } + + if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + qemu_put_be32s(f, &elem->ndescs); + } + qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); } =20 @@ -1245,6 +1862,9 @@ void virtio_reset(void *opaque) vdev->vq[i].last_avail_idx =3D 0; vdev->vq[i].shadow_avail_idx =3D 0; vdev->vq[i].used_idx =3D 0; + vdev->vq[i].last_avail_wrap_counter =3D true; + vdev->vq[i].shadow_avail_wrap_counter =3D true; + vdev->vq[i].used_wrap_counter =3D true; virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR); vdev->vq[i].signalled_used =3D 0; vdev->vq[i].signalled_used_valid =3D false; @@ -1635,6 +2255,8 @@ VirtQueue *virtio_add_queue(VirtIODevice *vdev, int q= ueue_size, vdev->vq[i].vring.align =3D VIRTIO_PCI_VRING_ALIGN; vdev->vq[i].handle_output =3D handle_output; vdev->vq[i].handle_aio_output =3D NULL; + vdev->vq[i].used_elems =3D g_malloc0(sizeof(VirtQueueElement) * + queue_size); =20 return &vdev->vq[i]; } @@ -1649,6 +2271,7 @@ void virtio_del_queue(VirtIODevice *vdev, int n) vdev->vq[n].vring.num_default =3D 0; vdev->vq[n].handle_output =3D NULL; vdev->vq[n].handle_aio_output =3D NULL; + g_free(vdev->vq[n].used_elems); } =20 static void virtio_set_isr(VirtIODevice *vdev, int value) @@ -1776,6 +2399,13 @@ static bool virtio_virtqueue_needed(void *opaque) return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1); } =20 +static bool virtio_packed_virtqueue_needed(void *opaque) +{ + VirtIODevice *vdev =3D opaque; + + return virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED); +} + static bool virtio_ringsize_needed(void *opaque) { VirtIODevice *vdev =3D opaque; @@ -1824,6 +2454,20 @@ static const VMStateDescription vmstate_virtqueue = =3D { } }; =20 +static const VMStateDescription vmstate_packed_virtqueue =3D { + .name =3D "packed_virtqueue_state", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT16(last_avail_idx, struct VirtQueue), + VMSTATE_BOOL(last_avail_wrap_counter, struct VirtQueue), + VMSTATE_UINT16(used_idx, struct VirtQueue), + VMSTATE_BOOL(used_wrap_counter, struct VirtQueue), + VMSTATE_UINT32(inuse, struct VirtQueue), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_virtio_virtqueues =3D { .name =3D "virtio/virtqueues", .version_id =3D 1, @@ -1836,6 +2480,18 @@ static const VMStateDescription vmstate_virtio_virtq= ueues =3D { } }; =20 +static const VMStateDescription vmstate_virtio_packed_virtqueues =3D { + .name =3D "virtio/packed_virtqueues", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D &virtio_packed_virtqueue_needed, + .fields =3D (VMStateField[]) { + VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, + VIRTIO_QUEUE_MAX, 0, vmstate_packed_virtqueue, VirtQ= ueue), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_ringsize =3D { .name =3D "ringsize_state", .version_id =3D 1, @@ -1968,6 +2624,7 @@ static const VMStateDescription vmstate_virtio =3D { &vmstate_virtio_broken, &vmstate_virtio_extra_state, &vmstate_virtio_started, + &vmstate_virtio_packed_virtqueues, NULL } }; @@ -2267,6 +2924,13 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int= version_id) virtio_queue_update_rings(vdev, i); } =20 + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + vdev->vq[i].shadow_avail_idx =3D vdev->vq[i].last_avail_id= x; + vdev->vq[i].shadow_avail_wrap_counter =3D + vdev->vq[i].last_avail_wrap_counte= r; + continue; + } + nheads =3D vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_av= ail_idx; /* Check it isn't doing strange things with descriptor numbers= . */ if (nheads > vdev->vq[i].vring.num) { @@ -2410,6 +3074,10 @@ hwaddr virtio_queue_get_avail_size(VirtIODevice *vde= v, int n) { int s; =20 + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return sizeof(struct VRingPackedDescEvent); + } + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingAvail, ring) + sizeof(uint16_t) * vdev->vq[n].vring.num + s; @@ -2419,23 +3087,83 @@ hwaddr virtio_queue_get_used_size(VirtIODevice *vde= v, int n) { int s; =20 + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return sizeof(struct VRingPackedDescEvent); + } + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingUsed, ring) + sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s; } =20 -uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) +static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice *v= dev, + int n) +{ + unsigned int avail, used; + + avail =3D vdev->vq[n].last_avail_idx; + avail |=3D ((uint16_t)vdev->vq[n].last_avail_wrap_counter) << 15; + + used =3D vdev->vq[n].used_idx; + used |=3D ((uint16_t)vdev->vq[n].used_wrap_counter) << 15; + + return avail | used << 16; +} + +static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice *vdev, + int n) { return vdev->vq[n].last_avail_idx; } =20 -void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t i= dx) +unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) { - vdev->vq[n].last_avail_idx =3D idx; - vdev->vq[n].shadow_avail_idx =3D idx; + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return virtio_queue_packed_get_last_avail_idx(vdev, n); + } else { + return virtio_queue_split_get_last_avail_idx(vdev, n); + } } =20 -void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n) +static void virtio_queue_packed_set_last_avail_idx(VirtIODevice *vdev, + int n, unsigned int idx) +{ + struct VirtQueue *vq =3D &vdev->vq[n]; + + vq->last_avail_idx =3D vq->shadow_avail_idx =3D idx & 0x7fff; + vq->last_avail_wrap_counter =3D + vq->shadow_avail_wrap_counter =3D !!(idx & 0x8000); + idx >>=3D 16; + vq->used_idx =3D idx & 0x7ffff; + vq->used_wrap_counter =3D !!(idx & 0x8000); +} + +static void virtio_queue_split_set_last_avail_idx(VirtIODevice *vdev, + int n, unsigned int idx) +{ + vdev->vq[n].last_avail_idx =3D idx; + vdev->vq[n].shadow_avail_idx =3D idx; +} + +void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, + unsigned int idx) +{ + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + virtio_queue_packed_set_last_avail_idx(vdev, n, idx); + } else { + virtio_queue_split_set_last_avail_idx(vdev, n, idx); + } +} + +static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice *vdev, + int n) +{ + /* We don't have a reference like avail idx in shared memory */ + return; +} + +static void virtio_queue_split_restore_last_avail_idx(VirtIODevice *vdev, + int n) { rcu_read_lock(); if (vdev->vq[n].vring.desc) { @@ -2445,7 +3173,22 @@ void virtio_queue_restore_last_avail_idx(VirtIODevic= e *vdev, int n) rcu_read_unlock(); } =20 -void virtio_queue_update_used_idx(VirtIODevice *vdev, int n) +void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n) +{ + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + virtio_queue_packed_restore_last_avail_idx(vdev, n); + } else { + virtio_queue_split_restore_last_avail_idx(vdev, n); + } +} + +static void virtio_queue_packed_update_used_idx(VirtIODevice *vdev, int n) +{ + /* used idx was updated through set_last_avail_idx() */ + return; +} + +static void virtio_split_packed_update_used_idx(VirtIODevice *vdev, int n) { rcu_read_lock(); if (vdev->vq[n].vring.desc) { @@ -2454,6 +3197,15 @@ void virtio_queue_update_used_idx(VirtIODevice *vdev= , int n) rcu_read_unlock(); } =20 +void virtio_queue_update_used_idx(VirtIODevice *vdev, int n) +{ + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return virtio_queue_packed_update_used_idx(vdev, n); + } else { + return virtio_split_packed_update_used_idx(vdev, n); + } +} + void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n) { vdev->vq[n].signalled_used_valid =3D false; diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index b189788cb2..226c94078f 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -59,6 +59,8 @@ typedef struct VirtQueue VirtQueue; typedef struct VirtQueueElement { unsigned int index; + unsigned int len; + unsigned int ndescs; unsigned int out_num; unsigned int in_num; hwaddr *in_addr; @@ -196,7 +198,8 @@ void virtqueue_map(VirtIODevice *vdev, VirtQueueElement= *elem); void *virtqueue_pop(VirtQueue *vq, size_t sz); unsigned int virtqueue_drop_all(VirtQueue *vq); void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t s= z); -void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem); +void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, + VirtQueueElement *elem); int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, unsigned int out_bytes); void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, @@ -291,8 +294,9 @@ hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, i= nt n); hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n); hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n); hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n); -uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n); -void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t i= dx); +unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n); +void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, + unsigned int idx); void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n); void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n); void virtio_queue_update_used_idx(VirtIODevice *vdev, int n); --=20 2.18.1 From nobody Mon May 6 22:48:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1564718879; cv=none; d=zoho.com; s=zohoarc; b=HOBxBoiHKTNWVgyvS9Z0usLCcR+qNarGCizwqL6eeISdMFwiyTrkKhvXQGbk+gtBzufj+IT2AABq2nGVURzHXdWh6pESsJ6dluVyxRETPdACc9chpwpdZRC+LD3w90eWIUP3Q3S1N2uEOf6/WGSOD8kpomPcfWp/5ILRQgyE0sw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564718879; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=MqEE/3+gSdzRjHYZWMI3znh3DOED4+SZ00wvt+TLFn0=; b=e7NOOkKDmIBkUZpX0npTcRZNl2jCxk66anmeNO0nAjQVM0OxAaSC2rhxtVsYrWElhFam4fEOq/dg3nmYsKs71aGiQUTP9IIICklX73XeRz0T6Ajq1tGI6UjDv1S4V/GJDKxz3kKJv+2UkxbkAH3hcpcYBsQlGmL9BEB4AiZU3NE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1564718879336318.04329121218075; Thu, 1 Aug 2019 21:07:59 -0700 (PDT) Received: from localhost ([::1]:60310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOrG-0000V2-Dv for importer@patchew.org; Fri, 02 Aug 2019 00:07:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50510) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOq9-0006N5-9T for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1htOq7-0001gb-VK for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52882) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1htOq7-0001gH-Nf for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:47 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 14F8615EF48; Fri, 2 Aug 2019 04:06:47 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7F0501001959; Fri, 2 Aug 2019 04:06:41 +0000 (UTC) From: Jason Wang To: mst@redhat.com Date: Fri, 2 Aug 2019 00:06:04 -0400 Message-Id: <20190802040606.22573-5-jasowang@redhat.com> In-Reply-To: <20190802040606.22573-1-jasowang@redhat.com> References: <20190802040606.22573-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 02 Aug 2019 04:06:47 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH V5 4/6] virtio: event suppression support for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, tiwei.bie@intel.com, Jason Wang , qemu-devel@nongnu.org, maxime.coquelin@redhat.com, Wei Xu , jfreimann@redhat.com, weiyshay@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch implements event suppression through device/driver area. Please refer virtio specification for more information. Signed-off-by: Wei Xu Signed-off-by: Jason Wang --- hw/virtio/virtio.c | 142 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 9 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 4105fe2448..a70551d26d 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -236,6 +236,44 @@ static void vring_split_desc_read(VirtIODevice *vdev, = VRingDesc *desc, virtio_tswap16s(vdev, &desc->next); } =20 +static void vring_packed_event_read(VirtIODevice *vdev, + MemoryRegionCache *cache, + VRingPackedDescEvent *e) +{ + hwaddr off_off =3D offsetof(VRingPackedDescEvent, off_wrap); + hwaddr off_flags =3D offsetof(VRingPackedDescEvent, flags); + + address_space_read_cached(cache, off_flags, &e->flags, + sizeof(e->flags)); + /* Make sure flags is seen before off_wrap */ + smp_rmb(); + address_space_read_cached(cache, off_off, &e->off_wrap, + sizeof(e->off_wrap)); + virtio_tswap16s(vdev, &e->off_wrap); + virtio_tswap16s(vdev, &e->flags); +} + +static void vring_packed_off_wrap_write(VirtIODevice *vdev, + MemoryRegionCache *cache, + uint16_t off_wrap) +{ + hwaddr off =3D offsetof(VRingPackedDescEvent, off_wrap); + + virtio_tswap16s(vdev, &off_wrap); + address_space_write_cached(cache, off, &off_wrap, sizeof(off_wrap)); + address_space_cache_invalidate(cache, off, sizeof(off_wrap)); +} + +static void vring_packed_flags_write(VirtIODevice *vdev, + MemoryRegionCache *cache, uint16_t fl= ags) +{ + hwaddr off =3D offsetof(VRingPackedDescEvent, flags); + + virtio_tswap16s(vdev, &flags); + address_space_write_cached(cache, off, &flags, sizeof(flags)); + address_space_cache_invalidate(cache, off, sizeof(flags)); +} + static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *= vq) { VRingMemoryRegionCaches *caches =3D atomic_rcu_read(&vq->vring.caches); @@ -342,14 +380,8 @@ static inline void vring_set_avail_event(VirtQueue *vq= , uint16_t val) address_space_cache_invalidate(&caches->used, pa, sizeof(val)); } =20 -void virtio_queue_set_notification(VirtQueue *vq, int enable) +static void virtio_queue_split_set_notification(VirtQueue *vq, int enable) { - vq->notification =3D enable; - - if (!vq->vring.desc) { - return; - } - rcu_read_lock(); if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { vring_set_avail_event(vq, vring_avail_idx(vq)); @@ -365,6 +397,51 @@ void virtio_queue_set_notification(VirtQueue *vq, int = enable) rcu_read_unlock(); } =20 +static void virtio_queue_packed_set_notification(VirtQueue *vq, int enable) +{ + uint16_t off_wrap; + VRingPackedDescEvent e; + VRingMemoryRegionCaches *caches; + + rcu_read_lock(); + caches =3D vring_get_region_caches(vq); + vring_packed_event_read(vq->vdev, &caches->used, &e); + + if (!enable) { + e.flags =3D VRING_PACKED_EVENT_FLAG_DISABLE; + } else if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX))= { + off_wrap =3D vq->shadow_avail_idx | vq->shadow_avail_wrap_counter = << 15; + vring_packed_off_wrap_write(vq->vdev, &caches->used, off_wrap); + /* Make sure off_wrap is wrote before flags */ + smp_wmb(); + e.flags =3D VRING_PACKED_EVENT_FLAG_DESC; + } else { + e.flags =3D VRING_PACKED_EVENT_FLAG_ENABLE; + } + + vring_packed_flags_write(vq->vdev, &caches->used, e.flags); + if (enable) { + /* Expose avail event/used flags before caller checks the avail id= x. */ + smp_mb(); + } + rcu_read_unlock(); +} + +void virtio_queue_set_notification(VirtQueue *vq, int enable) +{ + vq->notification =3D enable; + + if (!vq->vring.desc) { + return; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtio_queue_packed_set_notification(vq, enable); + } else { + virtio_queue_split_set_notification(vq, enable); + } +} + int virtio_queue_ready(VirtQueue *vq) { return vq->vring.avail !=3D 0; @@ -2286,8 +2363,7 @@ static void virtio_set_isr(VirtIODevice *vdev, int va= lue) } } =20 -/* Called within rcu_read_lock(). */ -static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) +static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq) { uint16_t old, new; bool v; @@ -2310,6 +2386,54 @@ static bool virtio_should_notify(VirtIODevice *vdev,= VirtQueue *vq) return !v || vring_need_event(vring_get_used_event(vq), new, old); } =20 +static bool vring_packed_need_event(VirtQueue *vq, bool wrap, + uint16_t off_wrap, uint16_t new, + uint16_t old) +{ + int off =3D off_wrap & ~(1 << 15); + + if (wrap !=3D off_wrap >> 15) { + off -=3D vq->vring.num; + } + + return vring_need_event(off, new, old); +} + +static bool virtio_packed_should_notify(VirtIODevice *vdev, VirtQueue *vq) +{ + VRingPackedDescEvent e; + uint16_t old, new; + bool v; + VRingMemoryRegionCaches *caches; + + caches =3D vring_get_region_caches(vq); + vring_packed_event_read(vdev, &caches->avail, &e); + + old =3D vq->signalled_used; + new =3D vq->signalled_used =3D vq->used_idx; + v =3D vq->signalled_used_valid; + vq->signalled_used_valid =3D true; + + if (e.flags =3D=3D VRING_PACKED_EVENT_FLAG_DISABLE) { + return false; + } else if (e.flags =3D=3D VRING_PACKED_EVENT_FLAG_ENABLE) { + return true; + } + + return !v || vring_packed_need_event(vq, vq->used_wrap_counter, + e.off_wrap, new, old); +} + +/* Called within rcu_read_lock(). */ +static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) +{ + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return virtio_packed_should_notify(vdev, vq); + } else { + return virtio_split_should_notify(vdev, vq); + } +} + void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq) { bool should_notify; --=20 2.18.1 From nobody Mon May 6 22:48:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1564718857; cv=none; d=zoho.com; s=zohoarc; b=KdFbfS4l5iBq/8vzm8EFrIzsa2fFZEAANw14kSjTagjF+urIF3EeQaJTUTWx9HqGkd4zTqa3wab4uOScEkme0Jy+sC1ph1012VRJdZFAAykx4QOZ9oxS01vIB7XYTAOK6L6jYYVO4USsVBRoX7q5kcGeg/RXbGF3hidv/U5gaqs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564718857; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=9YnaZAOQlAvajmkW8v4C7VyMwQ0riUCR0Q1sn2ZBmMg=; b=FTUSoHVZDLWfduG78hIF0XHGy34Lgwop6dAkRe9Ogzz0FnqjgRtFtDimuYSwSBPGnWHnLO73J7oqvchAJx/gT/tezYNIfHIEjb4yQ8T5HQkQ+vkECalV5MsS+V1QEeGtcD6eoOQxM10sj33YcDJrbtC5MNjIwMLs8EMlBGuQlZ4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1564718857510476.77184531952673; Thu, 1 Aug 2019 21:07:37 -0700 (PDT) Received: from localhost ([::1]:60306 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOqu-0007v9-IZ for importer@patchew.org; Fri, 02 Aug 2019 00:07:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50548) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOqG-0006do-Qr for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1htOqF-0001lt-Vv for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51866) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1htOqF-0001kb-Qu for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:06:55 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 16C6EC04959E; Fri, 2 Aug 2019 04:06:55 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DA7E1001B02; Fri, 2 Aug 2019 04:06:47 +0000 (UTC) From: Jason Wang To: mst@redhat.com Date: Fri, 2 Aug 2019 00:06:05 -0400 Message-Id: <20190802040606.22573-6-jasowang@redhat.com> In-Reply-To: <20190802040606.22573-1-jasowang@redhat.com> References: <20190802040606.22573-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 02 Aug 2019 04:06:55 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH V5 5/6] vhost_net: enable packed ring support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, tiwei.bie@intel.com, Jason Wang , qemu-devel@nongnu.org, maxime.coquelin@redhat.com, jfreimann@redhat.com, weiyshay@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Jason Wang Reviewed-by: Jens Freimann --- hw/net/vhost_net.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index a6b719035c..5b97997035 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -49,6 +49,7 @@ static const int kernel_feature_bits[] =3D { VIRTIO_F_VERSION_1, VIRTIO_NET_F_MTU, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_RING_PACKED, VHOST_INVALID_FEATURE_BIT }; =20 @@ -74,6 +75,7 @@ static const int user_feature_bits[] =3D { VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_MTU, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_RING_PACKED, =20 /* This bit implies RARP isn't sent by QEMU out of band */ VIRTIO_NET_F_GUEST_ANNOUNCE, --=20 2.18.1 From nobody Mon May 6 22:48:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1564718954; cv=none; d=zoho.com; s=zohoarc; b=RMV4J1lJGDlUcyLCQmYLNEokYsEUte4LDWIShk2DmPJcyiMEkxqBV5WtFq1OfTyFnrAza7yDeDl2yFV8vf+PSNDEIhFsd+wS/Nua4zgGjPXxH/IDNgmXZR1w/NbEMVXdSYB6M5vB4p2mDnpWBjCRRKevjTUuubw1m49IvoRi8r0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1564718954; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=bGFhfJKig6nQGbwYkDv7JJlaMUeoYDwNODaegtcgO1M=; b=WYuCcnV+GLdtZtPh07uS/pzgk7Vm6/1X3S1tiHn7JHOXdYZ82r7X3ntxQRHtlRmbjlRw3RB8iaDclhLopA5kFkJZGoSN2Pd2lXYZ8fh58EX6Zp+1VKebLXR8Ljr9+SppYCJTjuyYEBThBJn8fpw/+X18D+6Sy+C6jld9n13ARGo= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1564718954397407.8131634760982; Thu, 1 Aug 2019 21:09:14 -0700 (PDT) Received: from localhost ([::1]:60326 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOsT-0002iv-GZ for importer@patchew.org; Fri, 02 Aug 2019 00:09:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50574) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1htOqM-0006uJ-N7 for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:07:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1htOqL-0001nf-Q0 for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:07:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48320) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1htOqL-0001nb-LC for qemu-devel@nongnu.org; Fri, 02 Aug 2019 00:07:01 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E8B903090FC1; Fri, 2 Aug 2019 04:07:00 +0000 (UTC) Received: from hp-dl380pg8-01.lab.eng.pek2.redhat.com (hp-dl380pg8-01.lab.eng.pek2.redhat.com [10.73.8.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 925921001959; Fri, 2 Aug 2019 04:06:55 +0000 (UTC) From: Jason Wang To: mst@redhat.com Date: Fri, 2 Aug 2019 00:06:06 -0400 Message-Id: <20190802040606.22573-7-jasowang@redhat.com> In-Reply-To: <20190802040606.22573-1-jasowang@redhat.com> References: <20190802040606.22573-1-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Fri, 02 Aug 2019 04:07:01 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH V5 6/6] virtio: add property to enable packed virtqueue X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yang.zhong@intel.com, tiwei.bie@intel.com, Jason Wang , qemu-devel@nongnu.org, maxime.coquelin@redhat.com, jfreimann@redhat.com, weiyshay@gmail.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Jason Wang Reviewed-by: Jens Freimann --- include/hw/virtio/virtio.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 226c94078f..3a4ed3ab34 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -285,7 +285,9 @@ typedef struct VirtIORNGConf VirtIORNGConf; DEFINE_PROP_BIT64("any_layout", _state, _field, \ VIRTIO_F_ANY_LAYOUT, true), \ DEFINE_PROP_BIT64("iommu_platform", _state, _field, \ - VIRTIO_F_IOMMU_PLATFORM, false) + VIRTIO_F_IOMMU_PLATFORM, false), \ + DEFINE_PROP_BIT64("packed", _state, _field, \ + VIRTIO_F_RING_PACKED, false) =20 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n); bool virtio_queue_enabled(VirtIODevice *vdev, int n); --=20 2.18.1