Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp122994lqh; Wed, 27 Mar 2024 17:31:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU+uwmaiLGmRrdU+klIt88M+RU40NHRjEq30h2pJzbM6pOliZ/cdUXZnpNmbsEUhklWk+Q7QP4L/YEFj97iqt/+raHd5LY55zsGKkpLsw== X-Google-Smtp-Source: AGHT+IHd4Cs+5AidTlv7tP8IP++G9AB5tVipBVYbSgK+SLWItzCWkOJeurpChnHHvVkASegGEV1U X-Received: by 2002:a2e:310a:0:b0:2d6:f68b:16d8 with SMTP id x10-20020a2e310a000000b002d6f68b16d8mr959744ljx.26.1711585919716; Wed, 27 Mar 2024 17:31:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711585919; cv=pass; d=google.com; s=arc-20160816; b=Oa+Xu+FrF4AGOcpWR1aZ1MWlnd4H42zw4sxbMfb2XL3v+G2oT89OMsudx0t8bj1ssF 8+1zQNG/K1vwoI9m+T/P04S6gxwIYR7+nEJP/vMqPA+i4daKimHpXHxKxVrlB3Kll10A kPyfSp+yxpOCU1IbNRL+R/QRDZLIlh1n55KBBtJon67cFalqodqy8bN9IlvrVFz8mpD/ wGyzm39PJ4J1kRLh14GQPkv4t2FLRBrWvWvyh1qFcZy5dlEIiKq8V+/FLDxQ4ZczaP5G lDFRUhAvPntxjcdLNX67BLc2D3kzMw7zadOgkXbOKNBVAy1oJTh7eLt85epiQs/yIS3v Sjxg== 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=+dym63xirAqdqlrrfe7SLDQLu/P+E/eWefAsaqG2hIU=; fh=i77RcuNoT/gRtIsYcB/vTAKLoPTDpS8M3Squ2zaUGRY=; b=tiZgpWRU7f33Q9WR9g2pO7gPEz9A2QlYEt8Ec2OCMj/RIkC2jgo+q3nVDNMW5l5htf BA+Ue2XN/7xnpZto8fcTcgr2yywj0wzoC9OrD6m1M4Ch15obeuNEn+okS5X6iCfUGm5E H3AWXvMrEFjRFvgv7BSFbeD6WyWjd6t5KbaZ3ux6M03eXX7vja6A5LgPOT9eOmYQ641G wuRVqXt4O4BGOryeJOxLgXYWDk5yd14u5pS5hrBswjopHFv50oC9b5zYkWVxkNxDikKE /xWJHSFLnDW+oXZgvasAOVTpsXBiQuCttOcdGlZq34kMDMoCNTQ1A2zKCZripBcwVHqn RvZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EW6INL8J; 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-122212-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122212-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r11-20020a50d68b000000b0056c364eec14si154837edi.653.2024.03.27.17.31.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 17:31:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122212-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EW6INL8J; 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-122212-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122212-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 am.mirrors.kernel.org (Postfix) with ESMTPS id A43D01F2D747 for ; Thu, 28 Mar 2024 00:22:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE0A9EEC5; Thu, 28 Mar 2024 00:22:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EW6INL8J" 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 6B9C118646 for ; Thu, 28 Mar 2024 00:22:21 +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=1711585342; cv=none; b=HwRCPHixUuhtL4dhtF/5e53dUr5wvd8X1SeaVR4+vWZL9f25jB9H96vSPG1RxFTrivXxrQZOytMc4RIZVUHgC1OFseTD4l/yUpxPBYD+r8JX0zhmpjecWu6IQjQH+Zy6yfxoV1vDOxCs15y4QSSNi1A5U8pui27jqESvbjbSQDg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585342; c=relaxed/simple; bh=cU6kIJkiS3Tzc0gfxh5Vp6FcxfNzGBGQ0y/wawqvXsw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BYRNSqAmS4DBLEgBFxtVPYAcJIU+nttOmIcVNLcRM0eDbXatMJw84wE588RvVGw2SHPfx5fyw7zqkYLwQlAkxZF78+p5XNT89T5LNS7w4islbWtbTepFYieGxWcWT0OkWnmLnZWeI91t3B5r56yRb/VwFJFmJYE7PCF/LPlYCJI= 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=EW6INL8J; 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=1711585340; 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=+dym63xirAqdqlrrfe7SLDQLu/P+E/eWefAsaqG2hIU=; b=EW6INL8Jfn3mUBPAxysT4nHnFEQbrLqU+zjEkKWHO+rlt+HEgkfm9mSVcWsQquZzF9boPa ZSctlSJqxmvnEZwDdAIqeeNRXYAHWoeCGcxzHtOir/VWtyQkVPcbB1HN/7LQPIWjug1AJK qPb+lrtr+66pJP0LwehQ8CZFL3wNedk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-fAi4iPYcOTaibOqMrTCOTw-1; Wed, 27 Mar 2024 20:22:15 -0400 X-MC-Unique: fAi4iPYcOTaibOqMrTCOTw-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 44407800264; Thu, 28 Mar 2024 00:22:15 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.81]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4D3792166B34; Thu, 28 Mar 2024 00:22:11 +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 2/3] vhost: Add smp_rmb() in vhost_enable_notify() Date: Thu, 28 Mar 2024 10:21:48 +1000 Message-ID: <20240328002149.1141302-3-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_enable_notify(), inspired 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_enable_notify(). When it 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 d3bb267bbdcb ("vhost: cache avail index in vhost_enable_notify()"). Fixes: d3bb267bbdcb ("vhost: cache avail index in vhost_enable_notify()") Cc: # v5.18+ 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 29df65b2ebf2..32686c79c41d 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2848,9 +2848,19 @@ 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); + 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 true; + } - return vq->avail_idx != vq->last_avail_idx; + return false; } EXPORT_SYMBOL_GPL(vhost_enable_notify); -- 2.44.0