Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3813436ybb; Tue, 31 Mar 2020 12:29:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsgzvSHQvF0rM5145L7uyQjTuzj6rCrilsQAxBNJtesxV/jQMPii0u0/dzwqui/21ZcEq/n X-Received: by 2002:a05:6830:148d:: with SMTP id s13mr13873072otq.342.1585682990904; Tue, 31 Mar 2020 12:29:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585682990; cv=none; d=google.com; s=arc-20160816; b=bB4BVDxJOrUddimpgvyxID64Afz5aLemRHzhyABYdvUQn9QQKBFXNabJBInUgURzOQ 0BjKbhdc/h1jGgpbjP8XSOorGWsmUWHnxYk7kn6Dbxgkp2dtKEpxcbsYGi5h1a7w/10J foDf/fAmRIfCM3Fzp1cCnRrp3kRFDT8IqY8c4QJxvtwB7ya204kel/s3rywLJeF0zrve 2l4894xvj0Di/VTVmmEzezDM/LYtzn9KwrGnOjscNJ3pJQIHTUO0pmcwWQ8aQJUf/3nC je46WPubOMJuya4FsOGPdMbQKJwO4Upppr7gkwA4QeGcvr4tcdTTm+KaauOF+5QMZWSY kYvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1EtrP69P9x99nL17zc2WKV5FEgGlPObPqDaYjk2gMbc=; b=N35RH5RCV/aU5RLIWQqrxoiq8EgnD+j0/hNYYTQlfYcFtVLsHiJq8euBuVwWoOLy91 r1Q93r+S36kfj23hfotHMrFNzM216mTUJ0E/Yo5rjthcxp0kpNaNS70xp9ikkQpX1skq 0+w3tVWBTxe46bSGEovQ3boqFGdN77z7i0bFfoXYqRRHD/vT6HaqjyUqwO1KMAacNoD+ wD7XogCJhExQVTydcRFuvXivUAjMVrIOdrYNk8V2UBHIZWq2GrCaQdWKR6gOzpKqenv2 ifde54ugIOgjhG0zvvbU/dAzHkX1bSqQiHNx0J3vJEdRfESsQRVxWKPGfC/MM+fkHDOm ZQbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KMUIVcsJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l20si7114388otk.242.2020.03.31.12.29.38; Tue, 31 Mar 2020 12:29:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KMUIVcsJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731384AbgCaT25 (ORCPT + 99 others); Tue, 31 Mar 2020 15:28:57 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:49076 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730413AbgCaT21 (ORCPT ); Tue, 31 Mar 2020 15:28:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585682906; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1EtrP69P9x99nL17zc2WKV5FEgGlPObPqDaYjk2gMbc=; b=KMUIVcsJ6xWUABl1q3pFRGNPeXuyA15V08SGe6DB0MFVFsR8lWp7CMCenrJKA03EjKVpP4 9ukZ9IZb4l4BG1iOKDkSkbsOmIinQNhI5AznhcTQ0gyFAEc1sGdauRFwBw9zXzRcvn4U0r QrPFJRj3knfuu/43hJREDA+aW2g+hf4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-393-AnYolck1OumgolGyzYnitA-1; Tue, 31 Mar 2020 15:28:22 -0400 X-MC-Unique: AnYolck1OumgolGyzYnitA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D14C418A6EC1; Tue, 31 Mar 2020 19:28:20 +0000 (UTC) Received: from eperezma.remote.csb (ovpn-112-92.ams2.redhat.com [10.36.112.92]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8E08298A52; Tue, 31 Mar 2020 19:28:18 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S. Tsirkin" Cc: "linux-kernel@vger.kernel.org" , Stephen Rothwell , kvm list , Linux Next Mailing List , "virtualization@lists.linux-foundation.org" , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Christian Borntraeger , Halil Pasic , Cornelia Huck Subject: [PATCH v3 3/8] vhost: use batched version by default Date: Tue, 31 Mar 2020 21:27:59 +0200 Message-Id: <20200331192804.6019-4-eperezma@redhat.com> In-Reply-To: <20200331192804.6019-1-eperezma@redhat.com> References: <20200331192804.6019-1-eperezma@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Michael S. Tsirkin" As testing shows no performance change, switch to that now. Signed-off-by: Michael S. Tsirkin Signed-off-by: Eugenio P=C3=A9rez --- drivers/vhost/vhost.c | 251 +----------------------------------------- drivers/vhost/vhost.h | 4 - 2 files changed, 2 insertions(+), 253 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 38e65a36465b..56c5253056ee 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2113,253 +2113,6 @@ static unsigned next_desc(struct vhost_virtqueue = *vq, struct vring_desc *desc) return next; } =20 -static int get_indirect(struct vhost_virtqueue *vq, - struct iovec iov[], unsigned int iov_size, - unsigned int *out_num, unsigned int *in_num, - struct vhost_log *log, unsigned int *log_num, - struct vring_desc *indirect) -{ - struct vring_desc desc; - unsigned int i =3D 0, count, found =3D 0; - u32 len =3D vhost32_to_cpu(vq, indirect->len); - struct iov_iter from; - int ret, access; - - /* Sanity check */ - if (unlikely(len % sizeof desc)) { - vq_err(vq, "Invalid length in indirect descriptor: " - "len 0x%llx not multiple of 0x%zx\n", - (unsigned long long)len, - sizeof desc); - return -EINVAL; - } - - ret =3D translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq-= >indirect, - UIO_MAXIOV, VHOST_ACCESS_RO); - if (unlikely(ret < 0)) { - if (ret !=3D -EAGAIN) - vq_err(vq, "Translation failure %d in indirect.\n", ret); - return ret; - } - iov_iter_init(&from, READ, vq->indirect, ret, len); - - /* We will use the result as an address to read from, so most - * architectures only need a compiler barrier here. */ - read_barrier_depends(); - - count =3D len / sizeof desc; - /* Buffers are chained via a 16 bit next field, so - * we can have at most 2^16 of these. */ - if (unlikely(count > USHRT_MAX + 1)) { - vq_err(vq, "Indirect buffer length too big: %d\n", - indirect->len); - return -E2BIG; - } - - do { - unsigned iov_count =3D *in_num + *out_num; - if (unlikely(++found > count)) { - vq_err(vq, "Loop detected: last one at %u " - "indirect size %u\n", - i, count); - return -EINVAL; - } - if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) { - vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", - i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof des= c); - return -EINVAL; - } - if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) = { - vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", - i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof des= c); - return -EINVAL; - } - - if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) - access =3D VHOST_ACCESS_WO; - else - access =3D VHOST_ACCESS_RO; - - ret =3D translate_desc(vq, vhost64_to_cpu(vq, desc.addr), - vhost32_to_cpu(vq, desc.len), iov + iov_count, - iov_size - iov_count, access); - if (unlikely(ret < 0)) { - if (ret !=3D -EAGAIN) - vq_err(vq, "Translation failure %d indirect idx %d\n", - ret, i); - return ret; - } - /* If this is an input descriptor, increment that count. */ - if (access =3D=3D VHOST_ACCESS_WO) { - *in_num +=3D ret; - if (unlikely(log && ret)) { - log[*log_num].addr =3D vhost64_to_cpu(vq, desc.addr); - log[*log_num].len =3D vhost32_to_cpu(vq, desc.len); - ++*log_num; - } - } else { - /* If it's an output descriptor, they're all supposed - * to come before any input descriptors. */ - if (unlikely(*in_num)) { - vq_err(vq, "Indirect descriptor " - "has out after in: idx %d\n", i); - return -EINVAL; - } - *out_num +=3D ret; - } - } while ((i =3D next_desc(vq, &desc)) !=3D -1); - return 0; -} - -/* This looks in the virtqueue and for the first available buffer, and c= onverts - * it to an iovec for convenient access. Since descriptors consist of s= ome - * number of output then some number of input descriptors, it's actually= two - * iovecs, but we pack them into one and note how many of each there wer= e. - * - * This function returns the descriptor number found, or vq->num (which = is - * never a valid descriptor number) if none was found. A negative code = is - * returned on error. */ -int vhost_get_vq_desc(struct vhost_virtqueue *vq, - struct iovec iov[], unsigned int iov_size, - unsigned int *out_num, unsigned int *in_num, - struct vhost_log *log, unsigned int *log_num) -{ - struct vring_desc desc; - unsigned int i, head, found =3D 0; - u16 last_avail_idx; - __virtio16 avail_idx; - __virtio16 ring_head; - int ret, access; - - /* Check it isn't doing very strange things with descriptor numbers. */ - last_avail_idx =3D vq->last_avail_idx; - - if (vq->avail_idx =3D=3D vq->last_avail_idx) { - if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) { - vq_err(vq, "Failed to access avail idx at %p\n", - &vq->avail->idx); - return -EFAULT; - } - vq->avail_idx =3D vhost16_to_cpu(vq, avail_idx); - - if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { - vq_err(vq, "Guest moved used index from %u to %u", - last_avail_idx, vq->avail_idx); - return -EFAULT; - } - - /* If there's nothing new since last we looked, return - * invalid. - */ - if (vq->avail_idx =3D=3D last_avail_idx) - return vq->num; - - /* Only get avail ring entries after they have been - * exposed by guest. - */ - smp_rmb(); - } - - /* Grab the next descriptor number they're advertising, and increment - * the index we've seen. */ - if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) { - vq_err(vq, "Failed to read head: idx %d address %p\n", - last_avail_idx, - &vq->avail->ring[last_avail_idx % vq->num]); - return -EFAULT; - } - - head =3D vhost16_to_cpu(vq, ring_head); - - /* If their number is silly, that's an error. */ - if (unlikely(head >=3D vq->num)) { - vq_err(vq, "Guest says index %u > %u is available", - head, vq->num); - return -EINVAL; - } - - /* When we start there are none of either input nor output. */ - *out_num =3D *in_num =3D 0; - if (unlikely(log)) - *log_num =3D 0; - - i =3D head; - do { - unsigned iov_count =3D *in_num + *out_num; - if (unlikely(i >=3D vq->num)) { - vq_err(vq, "Desc index is %u > %u, head =3D %u", - i, vq->num, head); - return -EINVAL; - } - if (unlikely(++found > vq->num)) { - vq_err(vq, "Loop detected: last one at %u " - "vq size %u head %u\n", - i, vq->num, head); - return -EINVAL; - } - ret =3D vhost_get_desc(vq, &desc, i); - if (unlikely(ret)) { - vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", - i, vq->desc + i); - return -EFAULT; - } - if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) { - ret =3D get_indirect(vq, iov, iov_size, - out_num, in_num, - log, log_num, &desc); - if (unlikely(ret < 0)) { - if (ret !=3D -EAGAIN) - vq_err(vq, "Failure detected " - "in indirect descriptor at idx %d\n", i); - return ret; - } - continue; - } - - if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) - access =3D VHOST_ACCESS_WO; - else - access =3D VHOST_ACCESS_RO; - ret =3D translate_desc(vq, vhost64_to_cpu(vq, desc.addr), - vhost32_to_cpu(vq, desc.len), iov + iov_count, - iov_size - iov_count, access); - if (unlikely(ret < 0)) { - if (ret !=3D -EAGAIN) - vq_err(vq, "Translation failure %d descriptor idx %d\n", - ret, i); - return ret; - } - if (access =3D=3D VHOST_ACCESS_WO) { - /* If this is an input descriptor, - * increment that count. */ - *in_num +=3D ret; - if (unlikely(log && ret)) { - log[*log_num].addr =3D vhost64_to_cpu(vq, desc.addr); - log[*log_num].len =3D vhost32_to_cpu(vq, desc.len); - ++*log_num; - } - } else { - /* If it's an output descriptor, they're all supposed - * to come before any input descriptors. */ - if (unlikely(*in_num)) { - vq_err(vq, "Descriptor has out after in: " - "idx %d\n", i); - return -EINVAL; - } - *out_num +=3D ret; - } - } while ((i =3D next_desc(vq, &desc)) !=3D -1); - - /* On success, increment avail index. */ - vq->last_avail_idx++; - - /* Assume notifications from guest are disabled at this point, - * if they aren't we would need to update avail_event index. */ - BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY)); - return head; -} -EXPORT_SYMBOL_GPL(vhost_get_vq_desc); - static struct vhost_desc *peek_split_desc(struct vhost_virtqueue *vq) { BUG_ON(!vq->ndescs); @@ -2570,7 +2323,7 @@ static int fetch_descs(struct vhost_virtqueue *vq) * This function returns the descriptor number found, or vq->num (which = is * never a valid descriptor number) if none was found. A negative code = is * returned on error. */ -int vhost_get_vq_desc_batch(struct vhost_virtqueue *vq, +int vhost_get_vq_desc(struct vhost_virtqueue *vq, struct iovec iov[], unsigned int iov_size, unsigned int *out_num, unsigned int *in_num, struct vhost_log *log, unsigned int *log_num) @@ -2645,7 +2398,7 @@ int vhost_get_vq_desc_batch(struct vhost_virtqueue = *vq, =20 return ret; } -EXPORT_SYMBOL_GPL(vhost_get_vq_desc_batch); +EXPORT_SYMBOL_GPL(vhost_get_vq_desc); =20 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. *= / void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n) diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index a6fc11797a8a..1dbb5e44fba4 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -203,10 +203,6 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned= int ioctl, void __user *arg bool vhost_vq_access_ok(struct vhost_virtqueue *vq); bool vhost_log_access_ok(struct vhost_dev *); =20 -int vhost_get_vq_desc_batch(struct vhost_virtqueue *, - struct iovec iov[], unsigned int iov_count, - unsigned int *out_num, unsigned int *in_num, - struct vhost_log *log, unsigned int *log_num); int vhost_get_vq_desc(struct vhost_virtqueue *, struct iovec iov[], unsigned int iov_count, unsigned int *out_num, unsigned int *in_num, --=20 2.18.1