From patchwork Sun Jan 31 10:29:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 8173251 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 7AECE9F96D for ; Sun, 31 Jan 2016 10:31:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 136F8202FE for ; Sun, 31 Jan 2016 10:31:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 30E59202F0 for ; Sun, 31 Jan 2016 10:31:13 +0000 (UTC) Received: from localhost ([::1]:41113 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPpHk-00029F-7A for patchwork-qemu-devel@patchwork.kernel.org; Sun, 31 Jan 2016 05:31:12 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45039) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPpFv-0007Qy-1T for qemu-devel@nongnu.org; Sun, 31 Jan 2016 05:29:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aPpFt-0007mm-Rh for qemu-devel@nongnu.org; Sun, 31 Jan 2016 05:29:18 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:33564) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPpFt-0007mQ-4Z for qemu-devel@nongnu.org; Sun, 31 Jan 2016 05:29:17 -0500 Received: by mail-wm0-x242.google.com with SMTP id r129so4665347wmr.0 for ; Sun, 31 Jan 2016 02:29:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=K7onzpmHoO+xBC2E+jvm0hFDxe+cAM2TTBWQlxD/yys=; b=t2ewgb36z+OCH5Wy0SRcjXCstXVydfv9oFktup76SupvXH457bm0issh++Prb76ya3 G/yGQDOKNr0hcx+YghFu+HyzpP7ROeNuEHYOgdJaj/aivz2Zr9ZzOS43uZt/MBbbTkC+ ZwE46OiYTpaFimmAEj2mNnWhZd5uRpIVLoYTTo72D7lB2BQFh1C99axq5iT4FtVDH2gH zHemwq0SsACTL+BoKCv7HaR4h5shdioI5/g6JKj+LKJUc2i61j0VpaeGOoSXeMsQx2Tu Ga0OyMobBghpQywlQ/dnMOuQC3V34mB4QwxMaCOD0olOHDjzuhLcpWGzp4y6L6okjGFD D/MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=K7onzpmHoO+xBC2E+jvm0hFDxe+cAM2TTBWQlxD/yys=; b=YbspNhdwwFH8i2fxP6F2AWFLSn/kreuXYT/DP5rOANZnDSuBd+k4R7/W9E7o2UThDA 3QYNn2ExcN09Kq+dlfDCv3fHWGj58Ol8aFJJqYBUKuL3lD3zccybqIubj9fEA+w8IArs t2hMSsWascyDZN/JBHUC/rNBmWUQHWuUdu1eyr90M/GpWwvfRVK4xtTfBZdkWGN6L6Ep ZWEO2dTVlFmr3fYVBhHiRifbBQn4E3thZSSW1CwCBFlFYiUWb9Kh/esLzwps2PO+zEdx VaV8+olQDTWg96QRM4GfnOhX0PKOHD7wMuI/xrveSkfPjPYPW77BSbu92V+Hn2cxxyTS MXSA== X-Gm-Message-State: AG10YOTOKDBd5j284fZxRV4P0qnDZXavw1TKiPVgFPQolBXpZbJPhbm6j9REOnKD9TpcNA== X-Received: by 10.194.175.198 with SMTP id cc6mr17414429wjc.24.1454236156631; Sun, 31 Jan 2016 02:29:16 -0800 (PST) Received: from donizetti.fosdem.net ([2001:67c:1810:f051:7105:9465:633c:859]) by smtp.gmail.com with ESMTPSA id i79sm2349430wmf.24.2016.01.31.02.29.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Jan 2016 02:29:15 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sun, 31 Jan 2016 11:29:02 +0100 Message-Id: <1454236146-23293-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1454236146-23293-1-git-send-email-pbonzini@redhat.com> References: <1454236146-23293-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::242 Cc: cornelia.huck@de.ibm.com, mst@redhat.com Subject: [Qemu-devel] [PATCH 06/10] vring: slim down allocation of VirtQueueElements X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Build the addresses and s/g lists on the stack, and then copy them to a VirtQueueElement that is just as big as required to contain this particular s/g list. The cost of the copy is minimal compared to that of a large malloc. Reviewed-by: Cornelia Huck Signed-off-by: Paolo Bonzini --- hw/virtio/dataplane/vring.c | 53 ++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/hw/virtio/dataplane/vring.c b/hw/virtio/dataplane/vring.c index c950caa..d6b8ba9 100644 --- a/hw/virtio/dataplane/vring.c +++ b/hw/virtio/dataplane/vring.c @@ -217,8 +217,14 @@ bool vring_should_notify(VirtIODevice *vdev, Vring *vring) new, old); } - -static int get_desc(Vring *vring, VirtQueueElement *elem, +typedef struct VirtQueueCurrentElement { + unsigned in_num; + unsigned out_num; + hwaddr addr[VIRTQUEUE_MAX_SIZE]; + struct iovec iov[VIRTQUEUE_MAX_SIZE]; +} VirtQueueCurrentElement; + +static int get_desc(Vring *vring, VirtQueueCurrentElement *elem, struct vring_desc *desc) { unsigned *num; @@ -229,12 +235,12 @@ static int get_desc(Vring *vring, VirtQueueElement *elem, if (desc->flags & VRING_DESC_F_WRITE) { num = &elem->in_num; - iov = &elem->in_sg[*num]; - addr = &elem->in_addr[*num]; + iov = &elem->iov[elem->out_num + *num]; + addr = &elem->addr[elem->out_num + *num]; } else { num = &elem->out_num; - iov = &elem->out_sg[*num]; - addr = &elem->out_addr[*num]; + iov = &elem->iov[*num]; + addr = &elem->addr[*num]; /* If it's an output descriptor, they're all supposed * to come before any input descriptors. */ @@ -298,7 +304,8 @@ static bool read_vring_desc(VirtIODevice *vdev, /* This is stolen from linux/drivers/vhost/vhost.c. */ static int get_indirect(VirtIODevice *vdev, Vring *vring, - VirtQueueElement *elem, struct vring_desc *indirect) + VirtQueueCurrentElement *cur_elem, + struct vring_desc *indirect) { struct vring_desc desc; unsigned int i = 0, count, found = 0; @@ -350,7 +357,7 @@ static int get_indirect(VirtIODevice *vdev, Vring *vring, return -EFAULT; } - ret = get_desc(vring, elem, &desc); + ret = get_desc(vring, cur_elem, &desc); if (ret < 0) { vring->broken |= (ret == -EFAULT); return ret; @@ -393,6 +400,7 @@ void *vring_pop(VirtIODevice *vdev, Vring *vring, size_t sz) struct vring_desc desc; unsigned int i, head, found = 0, num = vring->vr.num; uint16_t avail_idx, last_avail_idx; + VirtQueueCurrentElement cur_elem; VirtQueueElement *elem = NULL; int ret; @@ -402,10 +410,7 @@ void *vring_pop(VirtIODevice *vdev, Vring *vring, size_t sz) goto out; } - elem = virtqueue_alloc_element(sz, VIRTQUEUE_MAX_SIZE, VIRTQUEUE_MAX_SIZE); - - /* Initialize elem so it can be safely unmapped */ - elem->in_num = elem->out_num = 0; + cur_elem.in_num = cur_elem.out_num = 0; /* Check it isn't doing very strange things with descriptor numbers. */ last_avail_idx = vring->last_avail_idx; @@ -432,8 +437,6 @@ void *vring_pop(VirtIODevice *vdev, Vring *vring, size_t sz) * the index we've seen. */ head = vring_get_avail_ring(vdev, vring, last_avail_idx % num); - elem->index = head; - /* If their number is silly, that's an error. */ if (unlikely(head >= num)) { error_report("Guest says index %u > %u is available", head, num); @@ -460,14 +463,14 @@ void *vring_pop(VirtIODevice *vdev, Vring *vring, size_t sz) barrier(); if (desc.flags & VRING_DESC_F_INDIRECT) { - ret = get_indirect(vdev, vring, elem, &desc); + ret = get_indirect(vdev, vring, &cur_elem, &desc); if (ret < 0) { goto out; } continue; } - ret = get_desc(vring, elem, &desc); + ret = get_desc(vring, &cur_elem, &desc); if (ret < 0) { goto out; } @@ -482,6 +485,18 @@ void *vring_pop(VirtIODevice *vdev, Vring *vring, size_t sz) virtio_tswap16(vdev, vring->last_avail_idx); } + /* Now copy what we have collected and mapped */ + elem = virtqueue_alloc_element(sz, cur_elem.out_num, cur_elem.in_num); + elem->index = head; + for (i = 0; i < cur_elem.out_num; i++) { + elem->out_addr[i] = cur_elem.addr[i]; + elem->out_sg[i] = cur_elem.iov[i]; + } + for (i = 0; i < cur_elem.in_num; i++) { + elem->in_addr[i] = cur_elem.addr[cur_elem.out_num + i]; + elem->in_sg[i] = cur_elem.iov[cur_elem.out_num + i]; + } + return elem; out: @@ -489,7 +504,11 @@ out: if (ret == -EFAULT) { vring->broken = true; } - vring_unmap_element(elem); + + for (i = 0; i < cur_elem.out_num + cur_elem.in_num; i++) { + vring_unmap(cur_elem.iov[i].iov_base, false); + } + g_free(elem); return NULL; }