Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp354648imi; Thu, 21 Jul 2022 02:11:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tUsTZW3tKtjyot6zvv+2SGRsuDkH7e/EvySWDjozKlcnycXJMvcBxsIoPsXcMokCwu18j7 X-Received: by 2002:a17:906:6a14:b0:72b:64bd:ea2b with SMTP id qw20-20020a1709066a1400b0072b64bdea2bmr37553490ejc.680.1658394705500; Thu, 21 Jul 2022 02:11:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658394705; cv=none; d=google.com; s=arc-20160816; b=L5Lif2w8ZYKx1U5ZCK02n5a/KPTfkP4yUkNMEEQd53zpwkL3xINj2+W/eiK1Shb6E9 44b3fzVaRc1JOJbHdnS31G4lsv7zRcgBoLTFZlfFseSabV1K1Iu0St1TuYCv+O5b89Cd 6IHmYxT9pn11TRfhOVTAnPVvLEDx4lww7lWrwnJIglrjUtOexRFzuodq8TBQq67HRFIp +HAuf4OoZtvgMe1KJ61QXDN0ibjFNnc3b9NRNRKeS60RVmsDXkiEDWJtMoy2wl1Qn9fG pTKy4yKw3tbtmD3IVxGLXW8Nle1FrKu6ZeKLOcdCUhp5FiQqAI7m6qHkerGf8wO1/2dw c8Qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=WdYfVdlMXm7klKjrUy4+fGsoAONCGN3b2p/ahbKSCPU=; b=WWQZks8hf1lN4giYp5sWsdz+VY24MZXFj9Y8ZYfT9rf/WfYZb/2XrdrUhwnbTc+toO 2yiv6EdZDkOhNQyNUTNX9jWi2tKfBASn9lQRl0hUh0LN8s7GVyIkeUM1JQMr62kTbe+Z La6rHv4Ro/0PFHsW5eY+303RJHaUXB7gIsbsyNfVGkRIQeXXrk9U67cbb0mgy8IONHlm bXfSXB7j117JYtohgILMzd7bkmczWBFtI8ZSeojFD70fLQBn9b5iXZF4EGyVKrUp5tDT MDlDNK+15rlv4o2cQL86alpFH06HUDpjoF4z6rNGZwypIgi51zqU4ANpOEuFskugJkNz eSEA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m19-20020a056402431300b00435d8d8821bsi2058085edc.2.2022.07.21.02.11.20; Thu, 21 Jul 2022 02:11:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232501AbiGUIoT (ORCPT + 99 others); Thu, 21 Jul 2022 04:44:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232508AbiGUIoR (ORCPT ); Thu, 21 Jul 2022 04:44:17 -0400 Received: from smtp236.sjtu.edu.cn (smtp236.sjtu.edu.cn [202.120.2.236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F26E07E833 for ; Thu, 21 Jul 2022 01:44:15 -0700 (PDT) Received: from proxy02.sjtu.edu.cn (smtp188.sjtu.edu.cn [202.120.2.188]) by smtp236.sjtu.edu.cn (Postfix) with ESMTPS id 90FF41008B38F; Thu, 21 Jul 2022 16:44:13 +0800 (CST) Received: from localhost (localhost.localdomain [127.0.0.1]) by proxy02.sjtu.edu.cn (Postfix) with ESMTP id 7483E200A4ED3; Thu, 21 Jul 2022 16:44:13 +0800 (CST) X-Virus-Scanned: amavisd-new at Received: from proxy02.sjtu.edu.cn ([127.0.0.1]) by localhost (proxy02.sjtu.edu.cn [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id L5Z_9Dui9VVx; Thu, 21 Jul 2022 16:44:10 +0800 (CST) Received: from localhost.localdomain (unknown [202.120.40.82]) (Authenticated sender: qtxuning1999@sjtu.edu.cn) by proxy02.sjtu.edu.cn (Postfix) with ESMTPSA id 7A4A0200BFDA8; Thu, 21 Jul 2022 16:43:54 +0800 (CST) From: Guo Zhi To: eperezma@redhat.com, jasowang@redhat.com, sgarzare@redhat.com, mst@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, Guo Zhi Subject: [RFC 1/5] vhost: reorder used descriptors in a batch Date: Thu, 21 Jul 2022 16:43:37 +0800 Message-Id: <20220721084341.24183-2-qtxuning1999@sjtu.edu.cn> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220721084341.24183-1-qtxuning1999@sjtu.edu.cn> References: <20220721084341.24183-1-qtxuning1999@sjtu.edu.cn> X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,RCVD_IN_SORBS_WEB, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Device may not use descriptors in order, for example, NIC and SCSI may not call __vhost_add_used_n with buffers in order. It's the task of __vhost_add_used_n to order them. This commit reorder the buffers using vq->heads, only the batch is begin from the expected start point and is continuous can the batch be exposed to driver. And only writing out a single used ring for a batch of descriptors, according to VIRTIO 1.1 spec. Signed-off-by: Guo Zhi --- drivers/vhost/vhost.c | 44 +++++++++++++++++++++++++++++++++++++++++-- drivers/vhost/vhost.h | 3 +++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 40097826c..e2e77e29f 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -317,6 +317,7 @@ static void vhost_vq_reset(struct vhost_dev *dev, vq->used_flags = 0; vq->log_used = false; vq->log_addr = -1ull; + vq->next_used_head_idx = 0; vq->private_data = NULL; vq->acked_features = 0; vq->acked_backend_features = 0; @@ -398,6 +399,8 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) GFP_KERNEL); if (!vq->indirect || !vq->log || !vq->heads) goto err_nomem; + + memset(vq->heads, 0, sizeof(*vq->heads) * dev->iov_limit); } return 0; @@ -2374,12 +2377,49 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq, unsigned count) { vring_used_elem_t __user *used; + struct vring_desc desc; u16 old, new; int start; + int begin, end, i; + int copy_n = count; + + if (vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) { + /* calculate descriptor chain length for each used buffer */ + for (i = 0; i < count; i++) { + begin = heads[i].id; + end = begin; + vq->heads[begin].len = 0; + do { + vq->heads[begin].len += 1; + if (unlikely(vhost_get_desc(vq, &desc, end))) { + vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", + end, vq->desc + end); + return -EFAULT; + } + } while ((end = next_desc(vq, &desc)) != -1); + } + + count = 0; + /* sort and batch continuous used ring entry */ + while (vq->heads[vq->next_used_head_idx].len != 0) { + count++; + i = vq->next_used_head_idx; + vq->next_used_head_idx = (vq->next_used_head_idx + + vq->heads[vq->next_used_head_idx].len) + % vq->num; + vq->heads[i].len = 0; + } + /* only write out a single used ring entry with the id corresponding + * to the head entry of the descriptor chain describing the last buffer + * in the batch. + */ + heads[0].id = i; + copy_n = 1; + } start = vq->last_used_idx & (vq->num - 1); used = vq->used->ring + start; - if (vhost_put_used(vq, heads, start, count)) { + if (vhost_put_used(vq, heads, start, copy_n)) { vq_err(vq, "Failed to write used"); return -EFAULT; } @@ -2410,7 +2450,7 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, start = vq->last_used_idx & (vq->num - 1); n = vq->num - start; - if (n < count) { + if (n < count && !vhost_has_feature(vq, VIRTIO_F_IN_ORDER)) { r = __vhost_add_used_n(vq, heads, n); if (r < 0) return r; diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index d9109107a..7b2c0fbb5 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -107,6 +107,9 @@ struct vhost_virtqueue { bool log_used; u64 log_addr; + /* Sort heads in order */ + u16 next_used_head_idx; + struct iovec iov[UIO_MAXIOV]; struct iovec iotlb_iov[64]; struct iovec *indirect; -- 2.17.1