Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp3678523ybg; Sun, 7 Jun 2020 07:16:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwj2phuLJl81FJ3gwlIeIGFAQSf99jU+niu4ubSuM5IOo3AFXADvng2ivnazs5rpSkZoJ0A X-Received: by 2002:a50:fb0b:: with SMTP id d11mr18528662edq.118.1591539369526; Sun, 07 Jun 2020 07:16:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591539369; cv=none; d=google.com; s=arc-20160816; b=KhQJBXQHnHOr57QP8hhfYQTi6DvxrXb8aXQ94bbLuBeOR2yUVTIB/ZZHxbdpe6foHS 38W+DvBt3HQya4r9GoTNdiWuFZYYhy0zel9B0h2X6HLIWfx3nv6UUV+Jozh2DKac3skX p0468+mWdzXUiXJ5tW2ZYazWupl1PHE91MGwvn0dDp/vdnUHKGB8rf9r5OEBNgru+k0D /c+bytVCLYjKJChAboEYQUolzLCDELfp/LOFkCzGh88aWCcxALjGwu3p2GyG8ad3d4H/ AK8unES4TSAKiWYQBSYLS+3ZR0r2bJeJD+XFac32aDKM+ZTZvpEU6+MZRQr2KxhxZ1JY fShg== 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=JQR7Mpm9OBikwgtvyKeD/N9hz5sz6YikG7pS21JFzQo=; b=fxS6ZVkQXErWeFZSjfbh8eSO88lXuY4MO4VievYlx4YMexX66IpyKayVHBNnR/g2vA yjAfdXDXFKKX6RoY5t24gSKZw/y4k6EVOHLMMghLqgTydJeH4yN03tBuLasTFZq5HHYx M9DBj5K3HhZIt+Hh22VWw41opxXscML6NYSts4p/2gPfg/XZAqOKYC7SLmKRxOnGF6QO cWvaZTbxstTo73K++WJPPPqltbmwnz4r6NeVi/1EPo3GK90vm6I4McBuEVR+vsGHJesk Xz7q3nISD+2ZAD9BSiTq56+euLbNWxcgcWm7iypJsiCaBzvzczOPvkTgiJkmysP5toa3 ZTJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bpROrwDo; 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 q16si6681441eju.407.2020.06.07.07.15.47; Sun, 07 Jun 2020 07:16:09 -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=bpROrwDo; 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 S1728045AbgFGONL (ORCPT + 99 others); Sun, 7 Jun 2020 10:13:11 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:31231 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726697AbgFGOLj (ORCPT ); Sun, 7 Jun 2020 10:11:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1591539097; 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=JQR7Mpm9OBikwgtvyKeD/N9hz5sz6YikG7pS21JFzQo=; b=bpROrwDoGrfk/f22tIGoDnQQdwnk3dxKKY6ASNFC9bLdHKR2BIsIV9HUP8aHfl+lxSLj8p RNxpjQgPKpXmXnmplaF0TBCaL6KgC7a7hxanY8Z4wYpF2oVs30+c7d3GZGckHKsPGvSBBD ChUEd8+Sy3PtHlMiEzeVo3I6YPrUO18= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-189-HUIlMPsxNNSvG-o4ClhAXg-1; Sun, 07 Jun 2020 10:11:35 -0400 X-MC-Unique: HUIlMPsxNNSvG-o4ClhAXg-1 Received: by mail-wm1-f72.google.com with SMTP id b65so4289265wmb.5 for ; Sun, 07 Jun 2020 07:11:35 -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=JQR7Mpm9OBikwgtvyKeD/N9hz5sz6YikG7pS21JFzQo=; b=m+liDo2zzvz3dfovpkdTId8j62Na1kiSiNlfjuRzbmFK5iqYzAIxyAo6NcCmjtIiqO mdyf1lh64MQmk7GCC1EWPlqJPFsrwawELcV4aytxqoXf6APMrHO1DyPrKK8ZmKF15ZjG o4Fqf80Kv5N5h5zjpT/xd1OfpMf8tE4TaAtiws3jjtDj4Opn8n7qatydfh8ZoIWnf2jf WqWwjB24ALQlz8epUE9S0kz7svLuTQj30SqsB5TPS16LguKwsD2XKrrmhml0qlETZkJc SjEXq3ai4fMUYuGuI21zy2RA9ohjWF1V/sQsvijCR0i0jOVuKVTpWkTSgTq8bSlIDgOr hGOQ== X-Gm-Message-State: AOAM530BMJi9ZWXx8PISKahy4UZ/HTR9bL6u1Z3lyXAS+yOGf6YB1TGB 7Sf0/RY6fDh16eue/zEhwUTLhKoZl2VcvLi/d3Xqqt15vGjBl1zh/XVtYVdgomaPtXARO2zlKNC 5sGAxK0tXUjHeJP3/eGKYmTi1 X-Received: by 2002:adf:dc8e:: with SMTP id r14mr18094716wrj.333.1591539094201; Sun, 07 Jun 2020 07:11:34 -0700 (PDT) X-Received: by 2002:adf:dc8e:: with SMTP id r14mr18094706wrj.333.1591539094004; Sun, 07 Jun 2020 07:11:34 -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 k17sm18626832wmj.15.2020.06.07.07.11.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jun 2020 07:11:33 -0700 (PDT) Date: Sun, 7 Jun 2020 10:11:30 -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 04/13] vhost: cleanup fetch_buf return code handling Message-ID: <20200607141057.704085-5-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 Return code of fetch_buf is confusing, so callers resort to tricks to get to sane values. Let's switch to something standard: 0 empty, >0 non-empty, <0 error. Signed-off-by: Michael S. Tsirkin --- drivers/vhost/vhost.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 3b0609801381..5075505cfe55 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2189,6 +2189,8 @@ static int fetch_indirect_descs(struct vhost_virtqueue *vq, return 0; } +/* This function returns a value > 0 if a descriptor was found, or 0 if none were found. + * A negative code is returned on error. */ static int fetch_buf(struct vhost_virtqueue *vq) { unsigned int i, head, found = 0; @@ -2205,7 +2207,7 @@ static int fetch_buf(struct vhost_virtqueue *vq) if (unlikely(vq->avail_idx == vq->last_avail_idx)) { /* If we already have work to do, don't bother re-checking. */ if (likely(vq->ndescs)) - return vq->num; + return 0; if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) { vq_err(vq, "Failed to access avail idx at %p\n", @@ -2224,7 +2226,7 @@ static int fetch_buf(struct vhost_virtqueue *vq) * invalid. */ if (vq->avail_idx == last_avail_idx) - return vq->num; + return 0; /* Only get avail ring entries after they have been * exposed by guest. @@ -2294,12 +2296,14 @@ static int fetch_buf(struct vhost_virtqueue *vq) /* On success, increment avail index. */ vq->last_avail_idx++; - return 0; + return 1; } +/* This function returns a value > 0 if a descriptor was found, or 0 if none were found. + * A negative code is returned on error. */ static int fetch_descs(struct vhost_virtqueue *vq) { - int ret = 0; + int ret; if (unlikely(vq->first_desc >= vq->ndescs)) { vq->first_desc = 0; @@ -2309,10 +2313,14 @@ static int fetch_descs(struct vhost_virtqueue *vq) if (vq->ndescs) return 0; - while (!ret && vq->ndescs <= vhost_vq_num_batch_descs(vq)) - ret = fetch_buf(vq); + for (ret = 1; + ret > 0 && vq->ndescs <= vhost_vq_num_batch_descs(vq); + ret = fetch_buf(vq)) + ; - return vq->ndescs ? 0 : ret; + /* On success we expect some descs */ + BUG_ON(ret > 0 && !vq->ndescs); + return ret; } /* This looks in the virtqueue and for the first available buffer, and converts @@ -2331,7 +2339,7 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, int ret = fetch_descs(vq); int i; - if (ret) + if (ret <= 0) return ret; /* Now convert to IOV */ -- MST