Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp3678012ybg; Sun, 7 Jun 2020 07:15:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxUs06OkUc8k1jhnDXJGSjfPle+p9KaYSf8ZfyBJq1gpRKZByzulf7NLHSz5XPByDCRSOl X-Received: by 2002:a17:906:838a:: with SMTP id p10mr16784895ejx.243.1591539320953; Sun, 07 Jun 2020 07:15:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591539320; cv=none; d=google.com; s=arc-20160816; b=vILN9WyjsR5IdyupGETrRl9D69rh/TiFvqM6Il5tFEDICOjmsvZXbdnn7JKUkDSqdZ v8xghau9aG3tvC3PrtgrToSYWtiAU/wxrp2o7mmG8f1U4x+IXEj6SiAamYnxjUQNR6sG Z4EiqKqR3vjGz3mfds3ncwAtKAIjESbwzWcB4J1VxTLrbNTyKf/om7SbZwLpp4AdzaJQ hRgfpsYs94c3lJb0EeYZu4qDMCUUYYT20W4Jai6CK2rZfZwNFXXbQ1FBFngo+e6G18ZM aJXwK798h4zoDfQMRfiTU1NIDEkjPMXbP2h4Q4eR6o/n7dmLaeVCbZ6sDYWpBJJvtGGC IwyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=shQxXwfF+1TlqdtG761ShfAvh9dKPfv2uFBe9eGr/Ow=; b=fA95wKxtDuoEwufmjdaYZx/MXusbhyjC1MdjnZNB/yGtVjofZMSR1sRgae5qfeivBo FyXEb67R2zPZ8YIbtdrGjLHzsG0ugsenIOusXukkHBUXwKbPzPc7F+VEC3jXdgNEnx46 GMq36GesZ1UJTEgIO5iBQNKNBeAs9KomDPVef5VG5KexpdyV+/RfokVjQY7aQq+9mvsL OyRljr+LGROzBswjUJKKPP6bkprX2V4+Ds/r5amavb/4OYNmlrjgf0N0YrtCd81TrWgW Z7ZY4dgZm78eJMF4PuuEzwjfuiL3eyKmIXG+dMGiGAz6om8MtMvJL3qlVtwtB/voKc/Z CN3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aabc989f; 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 lv27si7784516ejb.651.2020.06.07.07.14.58; Sun, 07 Jun 2020 07:15:20 -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=aabc989f; 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 S1726566AbgFGOLv (ORCPT + 99 others); Sun, 7 Jun 2020 10:11:51 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:26144 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726772AbgFGOLq (ORCPT ); Sun, 7 Jun 2020 10:11:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591539103; 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=shQxXwfF+1TlqdtG761ShfAvh9dKPfv2uFBe9eGr/Ow=; b=aabc989fDEMCQNeDT3hRn07Wr2+CRtRAyHXKBvcKFltn0+N1VPCeGZidp8SxsSoFkejtO7 J8d528iskIctOsypalD5jqQ3phpGbloHxlR4ltxK97wa3IAKdhvjdIW59Bif+5qvL/9VTa 9g3tHmnb3oNzlXrS2toyRsJlc4Z4lTc= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-316-cKFB5D7VMveQrLB5oxK4Pg-1; Sun, 07 Jun 2020 10:11:41 -0400 X-MC-Unique: cKFB5D7VMveQrLB5oxK4Pg-1 Received: by mail-wm1-f71.google.com with SMTP id c4so453254wmd.0 for ; Sun, 07 Jun 2020 07:11:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=shQxXwfF+1TlqdtG761ShfAvh9dKPfv2uFBe9eGr/Ow=; b=JVbybEg0rsFl93SACzAIs+wSi3rHoaXRgbW65+hCqFP5FZxg5wEUZQu0oe+S991ujk p+rux4rICQC0WQ2v3By2TrUVx0aD9obU/WjGV15D97qflbD3O4LvGeaID3vcVTNWXam3 cDjC2EMVs3j9K79ZIJp66rKp3BCPF/c9oW81dDsYAqt+jEu72Gr04yX7NkYeZbpv6G63 4GZzzbtHRwZlWDBzBw1YQTTW5ewLTOHeBZmFkW0E5OUqLGhFF7VY46fy9qclfvNOsP4c Q0mfzZpTmhYs+jdXOt2JhrZyP+X3XXkyZwsGcLSrt5gV3odhZFpE94K9eLhmw9UbZCft jL0A== X-Gm-Message-State: AOAM533/9e1ES3VfYCoBiCqfZXpgnFWTknK0+Epwsav7iBMmPCCZyumA WRMmH/PMONGKBqqnxGqaAt2oeHSVdo68liEYMEsbTm0hlPfQzXIoI5L9s4tCqk3CISesHNL7X+U df+wWFJgNlmZcIsKMJ4ONKeRY X-Received: by 2002:a5d:518b:: with SMTP id k11mr20431201wrv.58.1591539100369; Sun, 07 Jun 2020 07:11:40 -0700 (PDT) X-Received: by 2002:a5d:518b:: with SMTP id k11mr20431182wrv.58.1591539100132; Sun, 07 Jun 2020 07:11:40 -0700 (PDT) Received: from redhat.com (bzq-82-81-31-23.red.bezeqint.net. [82.81.31.23]) by smtp.gmail.com with ESMTPSA id f11sm20387998wrj.2.2020.06.07.07.11.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jun 2020 07:11:39 -0700 (PDT) Date: Sun, 7 Jun 2020 10:11:38 -0400 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, Jason Wang , eperezma@redhat.com Subject: [PATCH RFC v5 07/13] vhost: format-independent API for used buffers Message-ID: <20200607141057.704085-8-mst@redhat.com> References: <20200607141057.704085-1-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200607141057.704085-1-mst@redhat.com> X-Mailer: git-send-email 2.24.1.751.gd10ce2899c X-Mutt-Fcc: =sent Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 52 ++++++++++++++++++++++++++++++++++--------- drivers/vhost/vhost.h | 17 +++++++++++++- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 3ffcba4e27e9..72ee55c810c4 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2335,13 +2335,12 @@ static int fetch_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 vhost_get_vq_desc(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,7 +2412,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, vq->first_desc = i + 1; - return ret; + return 1; err: for (i = vq->first_desc; i < vq->ndescs; ++i) @@ -2418,7 +2422,15 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, return ret; } -EXPORT_SYMBOL_GPL(vhost_get_vq_desc); +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); static int __vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, @@ -2502,6 +2514,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