Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1781949ybt; Mon, 15 Jun 2020 09:14:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxdJwRZOIDZ106tzfDE6TCjEIKTDqtA6sBn8eQUVUoG4sDdknf/+GZmThgn+tIsyYdJvwQI X-Received: by 2002:a17:906:840d:: with SMTP id n13mr25968250ejx.49.1592237691759; Mon, 15 Jun 2020 09:14:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592237691; cv=none; d=google.com; s=arc-20160816; b=z3T2piWcxRrGO8c+hRnYjm5WPdp+r4ztkyLDWzKt+tx+B4l+/I189AQ9zSdjiWzdgC qx6IOtGu/TEiqRCFkzt5Z5QWcRzyvoix0ziKDbKmRsKvmMWkhqdQHi0gTfRrvwlGZTaj Q+gechCKTIq1IQhzf6OgWV/4lJ51nthzHuWs0b4lEhy/7gcc+4uuNjW2YXQvlpcjyd+u kVhjzR9pre3VmnUoQyd3Xg0QHNwKwyY7TbY88GyMOZjPb26X5xpsM8nTsFW8ryonRfhx omvWm7onO1tusFZx5QnE4vIyImAFFHwxkLFjQ42VqSQRCY3IZcDW5f+ioLlVu7Ajdxl5 1QhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=s3Y9+xXDyttebs3/fVIigjv1aP9MsZuTcGKyOjbOlrk=; b=uiIH/0UiDF2bz+J9uoWYjpcETv42CdVI2ayju2m0IUJAAxACVIHsOTDx46Bv0pibLF B27+Nt9tDN86yXQJYM/y3fNV/TQujmJYUl9nXpwxQAUflUTLHNUooljxQph0Zkeoee06 D8VTCYfDMlpO/Nf1aP+/LYBlSdsSeej9i58ganCtARaK+SpodacJ3Ft0YYFn55dPMMBM l6vCekT48JMgzeV1ulST8wMHBNwVhnP+q4cKmH4juBQER45l8rR6cboLgpNHP9jSLuBl vgthaU8GKCENPeQhq7YVtwhYj/vod5xaPTK6/5byJRD12wzxC739uSIbVCV4o1A83AcO VJUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RUlyA9C1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id v11si9302395ejb.677.2020.06.15.09.14.28; Mon, 15 Jun 2020 09:14:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RUlyA9C1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1730797AbgFOQLu (ORCPT + 99 others); Mon, 15 Jun 2020 12:11:50 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:30877 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729949AbgFOQLs (ORCPT ); Mon, 15 Jun 2020 12:11:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592237506; 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: in-reply-to:in-reply-to:references:references; bh=s3Y9+xXDyttebs3/fVIigjv1aP9MsZuTcGKyOjbOlrk=; b=RUlyA9C1yldMWgNtIwDeFLL/ywlWYyEUuz5T7NXXQKJ2EQy2p3f9siYTgXxAcqm4l51chG jmoxDb23K626sUkr6tkAo7RsZtBZlXGj6kZET4EqdHOYIPRwj5AGYhV67ANutZ3S5TQPRt HQKiJ5T7WjaPsrl/B7N7hg5jF3NQgwU= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-44-dTb3WIONMtuKgZwd7-NMdg-1; Mon, 15 Jun 2020 12:11:43 -0400 X-MC-Unique: dTb3WIONMtuKgZwd7-NMdg-1 Received: by mail-qv1-f72.google.com with SMTP id ba13so13402708qvb.15 for ; Mon, 15 Jun 2020 09:11:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=s3Y9+xXDyttebs3/fVIigjv1aP9MsZuTcGKyOjbOlrk=; b=XUCCFahAMfay3AMvEShDXWSA3zC6vg/e6ovgYfgFaOk3gYC0KQ0t8gukbZB9L6Giz3 Iy/aRmJVvn7VuSgBx0gAVyjOHPrRiC7XC8Mi/wXmqVgsqNx/K8zsrAaYWn0C1v5uejLT KZ8rjbz4N7M60pty6OlriAGX5Kx+CK6mc0LLEq0z5GNjUgyjIN4nAxA0/zR2tRHc1RJO MHwjNNgP/mlzOjXbDCNOivPkDUjgODmUJvgKYWrTK+TrrObK94BXlAR29gqxPHmIEAgQ 4Wx0GQAS8SuwlbIsMl3+v6XDaFL+Hoix9vUVGnEAv5VN//5veUsb/1XoWOnPSsnbcFWN qAIw== X-Gm-Message-State: AOAM532X4O+H9xC5eya2VAse1eUQTBVUmD9H0PF3np30P5X8vqcVo48C XO06XQvNdUdPZL7xqo/Lfj8xYeftg41V+QGq6/y39wwizriO8lH1sr7avDRvRV5SoHQY/AxgX9c y9Yx3v7BkMCvt6vtlkWwqtU3Bl7epjiz9PCsBFiKr X-Received: by 2002:ac8:312e:: with SMTP id g43mr16444501qtb.308.1592237502358; Mon, 15 Jun 2020 09:11:42 -0700 (PDT) X-Received: by 2002:ac8:312e:: with SMTP id g43mr16444416qtb.308.1592237501400; Mon, 15 Jun 2020 09:11:41 -0700 (PDT) MIME-Version: 1.0 References: <20200611113404.17810-1-mst@redhat.com> <20200611113404.17810-6-mst@redhat.com> In-Reply-To: <20200611113404.17810-6-mst@redhat.com> From: Eugenio Perez Martin Date: Mon, 15 Jun 2020 18:11:05 +0200 Message-ID: Subject: Re: [PATCH RFC v8 05/11] vhost: format-independent API for used buffers To: "Michael S. Tsirkin" Cc: linux-kernel@vger.kernel.org, kvm list , virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, Jason Wang Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 11, 2020 at 1:34 PM Michael S. Tsirkin wrote: > > Add a new API that doesn't assume used ring, heads, etc. > For now, we keep the old APIs around to make it easier > to convert drivers. > > Signed-off-by: Michael S. Tsirkin > --- > drivers/vhost/vhost.c | 73 +++++++++++++++++++++++++++++++++++++------ > drivers/vhost/vhost.h | 17 +++++++++- > 2 files changed, 79 insertions(+), 11 deletions(-) > > diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c > index c38605b01080..03e6bca02288 100644 > --- a/drivers/vhost/vhost.c > +++ b/drivers/vhost/vhost.c > @@ -2335,13 +2335,12 @@ static void unfetch_descs(struct vhost_virtqueue *vq) > * 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 were. > * > - * 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) > + * This function returns a value > 0 if a descriptor was found, or 0 if none were found. > + * A negative code is returned on error. */ > +int vhost_get_avail_buf(struct vhost_virtqueue *vq, struct vhost_buf *buf, > + struct iovec iov[], unsigned int iov_size, > + unsigned int *out_num, unsigned int *in_num, > + struct vhost_log *log, unsigned int *log_num) > { > int ret = fetch_descs(vq); > int i; > @@ -2354,6 +2353,8 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, > *out_num = *in_num = 0; > if (unlikely(log)) > *log_num = 0; > + buf->in_len = buf->out_len = 0; > + buf->descs = 0; > > for (i = vq->first_desc; i < vq->ndescs; ++i) { > unsigned iov_count = *in_num + *out_num; > @@ -2383,6 +2384,7 @@ int (struct vhost_virtqueue *vq, > /* If this is an input descriptor, > * increment that count. */ > *in_num += ret; > + buf->in_len += desc->len; > if (unlikely(log && ret)) { > log[*log_num].addr = desc->addr; > log[*log_num].len = desc->len; > @@ -2398,9 +2400,11 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, > goto err; > } > *out_num += ret; > + buf->out_len += desc->len; > } > > - ret = desc->id; > + buf->id = desc->id; > + ++buf->descs; > > if (!(desc->flags & VRING_DESC_F_NEXT)) > break; > @@ -2408,12 +2412,41 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, > > vq->first_desc = i + 1; > > - return ret; > + return 1; > > err: > unfetch_descs(vq); > > - return ret ? ret : vq->num; > + return ret; > +} > +EXPORT_SYMBOL_GPL(vhost_get_avail_buf); > + > +/* Reverse the effect of vhost_get_avail_buf. Useful for error handling. */ > +void vhost_discard_avail_bufs(struct vhost_virtqueue *vq, > + struct vhost_buf *buf, unsigned count) > +{ > + vhost_discard_vq_desc(vq, count); > +} > +EXPORT_SYMBOL_GPL(vhost_discard_avail_bufs); > + > +/* 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 vhost_buf buf; > + int ret = vhost_get_avail_buf(vq, &buf, > + iov, iov_size, out_num, in_num, > + log, log_num); > + > + if (likely(ret > 0)) > + return buf->id; This should be buf.id, isn't it? > + if (likely(!ret)) > + return vq->num; > + return ret; > } > EXPORT_SYMBOL_GPL(vhost_get_vq_desc); > > @@ -2507,6 +2540,26 @@ int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) > } > EXPORT_SYMBOL_GPL(vhost_add_used); > > +int vhost_put_used_buf(struct vhost_virtqueue *vq, struct vhost_buf *buf) > +{ > + return vhost_add_used(vq, buf->id, buf->in_len); > +} > +EXPORT_SYMBOL_GPL(vhost_put_used_buf); > + > +int vhost_put_used_n_bufs(struct vhost_virtqueue *vq, > + struct vhost_buf *bufs, unsigned count) > +{ > + unsigned i; > + > + for (i = 0; i < count; ++i) { > + vq->heads[i].id = cpu_to_vhost32(vq, bufs[i].id); > + vq->heads[i].len = cpu_to_vhost32(vq, bufs[i].in_len); > + } > + > + return vhost_add_used_n(vq, vq->heads, count); > +} > +EXPORT_SYMBOL_GPL(vhost_put_used_n_bufs); > + > static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) > { > __u16 old, new; > diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h > index fed36af5c444..28eea0155efb 100644 > --- a/drivers/vhost/vhost.h > +++ b/drivers/vhost/vhost.h > @@ -67,6 +67,13 @@ struct vhost_desc { > u16 id; > }; > > +struct vhost_buf { > + u32 out_len; > + u32 in_len; > + u16 descs; > + u16 id; > +}; > + > /* The virtqueue structure describes a queue attached to a device. */ > struct vhost_virtqueue { > struct vhost_dev *dev; > @@ -195,7 +202,12 @@ int vhost_get_vq_desc(struct vhost_virtqueue *, > unsigned int *out_num, unsigned int *in_num, > struct vhost_log *log, unsigned int *log_num); > void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); > - > +int vhost_get_avail_buf(struct vhost_virtqueue *, struct vhost_buf *buf, > + struct iovec iov[], unsigned int iov_count, > + unsigned int *out_num, unsigned int *in_num, > + struct vhost_log *log, unsigned int *log_num); > +void vhost_discard_avail_bufs(struct vhost_virtqueue *, > + struct vhost_buf *, unsigned count); > int vhost_vq_init_access(struct vhost_virtqueue *); > int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); > int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, > @@ -204,6 +216,9 @@ void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, > unsigned int id, int len); > void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, > struct vring_used_elem *heads, unsigned count); > +int vhost_put_used_buf(struct vhost_virtqueue *, struct vhost_buf *buf); > +int vhost_put_used_n_bufs(struct vhost_virtqueue *, > + struct vhost_buf *bufs, unsigned count); > void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); > void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); > bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); > -- > MST >