Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2341390pxb; Sun, 30 Jan 2022 12:51:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJxc/UgkMMKuLyXA0ZwMlb/hr2354/fDw07g+lMg8Rcc0hdiQInp2IRsvqgid5mpTnGslojq X-Received: by 2002:a17:907:7f92:: with SMTP id qk18mr14667029ejc.766.1643575887927; Sun, 30 Jan 2022 12:51:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643575887; cv=none; d=google.com; s=arc-20160816; b=YifGuZshJ+NB8zElgSz1e0llzJCx6f/ABwyEd9UKXydr2FbOuZQcl0vFgMSaHbyJGy e9cO3Yv+2o9TTxU1ugC7VHVN8fpAVBPLHFlmvPAcbOlkRBIvw5u70OR2+0CsPHKh+t53 WcC/x+fVYrwbat9WHeFXGVp1Uefr0jkEdGawBMfwEKpM0YXqFTbk1TglV3CM95WfrxuN nbvjkq0hWHsSzOE6chWoccTp8cTVSIT+JwkzWt6fD0y186/Uh0DUFaeyhfEnitZHq1cZ accZZk1rKDDuLdDGM1e/tOcfEKAL1WlAg3gMyPXGhPQf4GuKbmmq20LfHL7/Sxt3DGnt Zd0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=gm/zIaqxAUJ7weu5vueVLpk6KOEoe9UgJ0ELP0kscoU=; b=DjLs9Xbx3RWDlgfIHIPW0XfAHk1BHnm2iTarti5L1POk3xSglOmi4EaRBaqw2gzd3w LsAULwyzNC+QcpHuFMe42rZ6pKBKY9fIushvOi/bzszu1/TUPWtl8lyMzoWRGvPHt3XI 4pyxKh+NFgIuQFLvupAwg5gr4NCf8Ad9xRJQF7/efsD8BmLE8flQuTvE7qa2cfx+NIOg wSuXBW/twWRErOdO5bkOiitewCqKu7GaJbfgEmg6vbPDpSU3kla1lx2Qz4+e/EGej55t OOVywH8ZaNSCDy8WSCF8+z77gSTsOiFs/2cC4YoKgTvJiZi7kLcyNuPz7WGyjoXrZmBI lulA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ebdnjtyr; 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 ds8si7531222ejc.1002.2022.01.30.12.51.03; Sun, 30 Jan 2022 12:51:27 -0800 (PST) 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=ebdnjtyr; 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 S1347701AbiA1Jlg (ORCPT + 99 others); Fri, 28 Jan 2022 04:41:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:57956 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243797AbiA1Jle (ORCPT ); Fri, 28 Jan 2022 04:41:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643362894; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=gm/zIaqxAUJ7weu5vueVLpk6KOEoe9UgJ0ELP0kscoU=; b=ebdnjtyrBbFVDM8A59XDWW4FHeYME03yT5YEb2C5ay0gkoXJOr12xRvBG7Ud2xmOaHLC/z XVKRgifivUJoyQgeP06V0ZSvs8LfzfLZT88MwrGZ7frsCRPO2/jRictzgjr+WxJMqt8ONd i/Ax2zL5HLnhegQrABEPndEo2h/GvLs= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-544-ukQM9CArMjOzul-vEly3QA-1; Fri, 28 Jan 2022 04:41:33 -0500 X-MC-Unique: ukQM9CArMjOzul-vEly3QA-1 Received: by mail-wr1-f69.google.com with SMTP id j21-20020adfa555000000b001db55dd5a1dso2040124wrb.15 for ; Fri, 28 Jan 2022 01:41:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gm/zIaqxAUJ7weu5vueVLpk6KOEoe9UgJ0ELP0kscoU=; b=I+DxT2OY/sCQ+reSj6SKwKYy8g6VuPNBt/cdF/SluuuOiibNwxfNEVo+CLJmpKRBTu m9AUo3heQrr4NuW37wmZJX9hIrvKthBTxRLXvBI2a+UVQqaWfl14CgFoj5dScHx6rOj/ SehoHzSN4pkGBn7w9xcYuTu+v9CR1H+O+nZrcPeTMlEtqy3dgRSo1HthSwU+Pj3KYux+ 0N5XjzPQeVkEt2jSPLr+unHSFNRUQUGe7GoEVAC3+j2InnThvVtbFbm0uKHVGVIiK0dL JAEXkVroU5ml5FFjQzCQRKzr25KK4arTicFVbc1UVkD/pPjqro/0jcPgHLjwwa4C2Sl1 bKVQ== X-Gm-Message-State: AOAM530XDDc+hfesywi++b6sAbkpY7SNfBwq7yiuOs8pZKuqDoAFnAI8 rNw4nyMJ5jTLnLsIwoar9DrIJa3thVnDMWNPI7Iu0VFO194foPIaGGXA5qnZtdgv6UTP8DV/LtZ Z1HcLHElL+VG/lfbqCmFY10Dc X-Received: by 2002:a5d:554b:: with SMTP id g11mr6278084wrw.168.1643362891423; Fri, 28 Jan 2022 01:41:31 -0800 (PST) X-Received: by 2002:a5d:554b:: with SMTP id g11mr6278075wrw.168.1643362891224; Fri, 28 Jan 2022 01:41:31 -0800 (PST) Received: from steredhat.redhat.com (host-95-238-125-214.retail.telecomitalia.it. [95.238.125.214]) by smtp.gmail.com with ESMTPSA id o1sm683206wri.108.2022.01.28.01.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 01:41:30 -0800 (PST) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: "Michael S. Tsirkin" , netdev@vger.kernel.org, stefanha@redhat.com, Jason Wang , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v3] vhost: cache avail index in vhost_enable_notify() Date: Fri, 28 Jan 2022 10:41:29 +0100 Message-Id: <20220128094129.40809-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In vhost_enable_notify() we enable the notifications and we read the avail index to check if new buffers have become available in the meantime. We do not update the cached avail index value, so when the device will call vhost_get_vq_desc(), it will find the old value in the cache and it will read the avail index again. It would be better to refresh the cache every time we read avail index, so let's change vhost_enable_notify() caching the value in `avail_idx` and compare it with `last_avail_idx` to check if there are new buffers available. We don't expect a significant performance boost because the above path is not very common, indeed vhost_enable_notify() is often called with unlikely(), expecting that avail index has not been updated. We ran virtio-test/vhost-test and noticed minimal improvement as expected. To stress the patch more, we modified vhost_test.ko to call vhost_enable_notify()/vhost_disable_notify() on every cycle when calling vhost_get_vq_desc(); in this case we observed a more evident improvement, with a reduction of the test execution time of about 3.7%. Signed-off-by: Stefano Garzarella --- v3 - reworded commit description [Stefan] --- drivers/vhost/vhost.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 59edb5a1ffe2..07363dff559e 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2543,8 +2543,9 @@ bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) &vq->avail->idx, r); return false; } + vq->avail_idx = vhost16_to_cpu(vq, avail_idx); - return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx; + return vq->avail_idx != vq->last_avail_idx; } EXPORT_SYMBOL_GPL(vhost_enable_notify); -- 2.34.1