Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp119666lqh; Wed, 27 Mar 2024 17:22:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWEcnr7sTUxDa98zn7V9wFSj0h66hq88mS7/YI3o7lMDJuWLEHEFAZiDcvdzfeFjEu7xa+zUvCCaam2hW9fdBhbW8Eloi9SAPi98f175Q== X-Google-Smtp-Source: AGHT+IH6NZCFGh8WcTtVDphVLPcplzIycWze8trbVYd/9eU1IPL2UqR+XAM+rWByU+g4Zxr1IEhL X-Received: by 2002:a05:620a:31a3:b0:789:ec9a:11de with SMTP id bi35-20020a05620a31a300b00789ec9a11demr1326988qkb.61.1711585352368; Wed, 27 Mar 2024 17:22:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711585352; cv=pass; d=google.com; s=arc-20160816; b=garRTsIDHUIMTn5bvpEkcfnH/0wWfUTL3zJYYzQfeIGHt5AxYuhbWcUjWhN/n1MsgN 8HnS1X3XGBfd9lD8UBXFHwV+f/PLBu1vfNtgtrr9ktIruNXGAZJFHWI1OgJL+UlsHjwl pwsWvfMPouB3klJPCbZVlMr/pJQ7jdCC2eB6YiTYU9xzSkMdALm3xB9up6QOROVy1E+w +VUc7VCt6XYdR24YLc6liCcYXK7jgb06zwPRXWBmRD4fxBV/1ZSr3Vb1A09vv1siysS5 DoxeyZkvTeYx/Z8dejZ/YFfcDe8+b0UM4qf9tuPyu+YWwUDfTpjjrxUtv4tlHydb1ZGC 865A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=WvpBOxCM54Ha0O04EGrQoRIE4s015ZHfzJODI+uMiUU=; fh=i77RcuNoT/gRtIsYcB/vTAKLoPTDpS8M3Squ2zaUGRY=; b=SIYqr44FyDg9hEN6flWjdGE6oErmZkWKpVmT3jjXmSliXa02vpXG6pyhxV3PnAkwCu t8KcMPKmuBx9/k1wiFnFtP+mgvlWyPAUApX7AnPKtz9TWDnHHxmKnfphbQ+aY1IlzjLo /D9fG55RjwL5/lo0TFDp0ktwTClBTh4Nxl/Tw/9B5VJJfsJJNawVpyXxskVUqG71S1cS QLk+CNOPcVRLS6o4lmMRmiraffA6ZHWq5GjASie6RudTCmaNqhnlre6502P2UU7QIoki 3lBbUusBU2B6u7Yl/61oRIjSuxjEqW2HY7YtJQF93QXZOqYjtGfCAEaB/Xk3hIMAU4cw EIyg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MzQNU2JR; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-122211-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122211-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id m5-20020a05620a290500b00789e9b091absi370379qkp.744.2024.03.27.17.22.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 17:22:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122211-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MzQNU2JR; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-122211-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122211-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1F8281C261BA for ; Thu, 28 Mar 2024 00:22:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 204D04A18; Thu, 28 Mar 2024 00:22:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MzQNU2JR" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A192F107B2 for ; Thu, 28 Mar 2024 00:22:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585337; cv=none; b=eK2r4fc9iuEi/aHXPfSviNtTnGT7tzCRUyhO49BAkhDUz9D3EgBiDCDYmomdWaXafq+XRyRBYqBsJCUC9ZwiovFCJD+QsBAGu8nBC0iwbTXpGsTO3I1UVqTiqqGB1Fyb4Rn1oPAFNMK5oXnSFc+rdG1+hpBlnrsjUl4HfFOzyDo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585337; c=relaxed/simple; bh=TpeLSXi6OQkQnOEw3Yi3HiVxO9pqlJkPXi+oAqf40MY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=utLXeY/rikFKUL6nrh6F5V51Ppa8ot9mUgEfbNB9zQ70fP2naJNbAeJ/4TVaLPBrW8Unew9sU5CtIqsYk/Q8ZOxucXUYEhm0mb3kQs/LjnoBI6RkMWJu02r3VZBSU2ZnZVQlnrKP3b+XV6HW1YIbVbTneSISFPOgqXiZ+f3FvCQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MzQNU2JR; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711585334; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WvpBOxCM54Ha0O04EGrQoRIE4s015ZHfzJODI+uMiUU=; b=MzQNU2JRUrVXisjn7lFLej/K/NdiCCBCaOznYOSXMQu/nkz4AB4y3bkNWEduACL3y9MgH/ 1ntJqWarDTtQngKAOxhMHCEtnwnDzuOTrt1OChPVdYXxh9Z/Hc/op3DcvF+JxOlrzcD4w4 91ZIvu066IgLvUzsA/8wyMFC11K1iMM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-357-UV_cAuXxPYu-HH8KBHS8nQ-1; Wed, 27 Mar 2024 20:22:11 -0400 X-MC-Unique: UV_cAuXxPYu-HH8KBHS8nQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B172B1C05AA3; Thu, 28 Mar 2024 00:22:10 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.81]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6F9502166B31; Thu, 28 Mar 2024 00:22:06 +0000 (UTC) From: Gavin Shan To: virtualization@lists.linux.dev Cc: linux-kernel@vger.kernel.org, mst@redhat.com, jasowang@redhat.com, will@kernel.org, davem@davemloft.net, stefanha@redhat.com, sgarzare@redhat.com, keirf@google.com, yihyu@redhat.com, shan.gavin@gmail.com Subject: [PATCH v3 1/3] vhost: Add smp_rmb() in vhost_vq_avail_empty() Date: Thu, 28 Mar 2024 10:21:47 +1000 Message-ID: <20240328002149.1141302-2-gshan@redhat.com> In-Reply-To: <20240328002149.1141302-1-gshan@redhat.com> References: <20240328002149.1141302-1-gshan@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 A smp_rmb() has been missed in vhost_vq_avail_empty(), spotted by Will. Otherwise, it's not ensured the available ring entries pushed by guest can be observed by vhost in time, leading to stale available ring entries fetched by vhost in vhost_get_vq_desc(), as reported by Yihuang Yu on NVidia's grace-hopper (ARM64) platform. /home/gavin/sandbox/qemu.main/build/qemu-system-aarch64 \ -accel kvm -machine virt,gic-version=host -cpu host \ -smp maxcpus=1,cpus=1,sockets=1,clusters=1,cores=1,threads=1 \ -m 4096M,slots=16,maxmem=64G \ -object memory-backend-ram,id=mem0,size=4096M \ : \ -netdev tap,id=vnet0,vhost=true \ -device virtio-net-pci,bus=pcie.8,netdev=vnet0,mac=52:54:00:f1:26:b0 : guest# netperf -H 10.26.1.81 -l 60 -C -c -t UDP_STREAM virtio_net virtio0: output.0:id 100 is not a head! Add the missed smp_rmb() in vhost_vq_avail_empty(). When tx_can_batch() returns true, it means there's still pending tx buffers. Since it might read indices, so it still can bypass the smp_rmb() in vhost_get_vq_desc(). Note that it should be safe until vq->avail_idx is changed by commit 275bf960ac697 ("vhost: better detection of available buffers"). Fixes: 275bf960ac69 ("vhost: better detection of available buffers") Cc: # v4.11+ Reported-by: Yihuang Yu Suggested-by: Will Deacon Signed-off-by: Gavin Shan Acked-by: Jason Wang --- drivers/vhost/vhost.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 045f666b4f12..29df65b2ebf2 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2799,9 +2799,19 @@ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) r = vhost_get_avail_idx(vq, &avail_idx); if (unlikely(r)) return false; + vq->avail_idx = vhost16_to_cpu(vq, avail_idx); + if (vq->avail_idx != vq->last_avail_idx) { + /* Since we have updated avail_idx, the following + * call to vhost_get_vq_desc() will read available + * ring entries. Make sure that read happens after + * the avail_idx read. + */ + smp_rmb(); + return false; + } - return vq->avail_idx == vq->last_avail_idx; + return true; } EXPORT_SYMBOL_GPL(vhost_vq_avail_empty); -- 2.44.0