Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1257753lqa; Mon, 29 Apr 2024 03:14:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWObxilpouucq8wMhbC4pxd06eb+kiY2NTKdsqhurXKZcAcN9vHmgrYuHoNspHFe9g78UtrGCpnN3tRb3LISdM956f+eR5LZ64olEqO2Q== X-Google-Smtp-Source: AGHT+IFoYlDDycFcXFMgI+0UDNiji/QH2iv2P4eLBMeb5iVHsfyrKPJ2EEEZYSYUfJ2YgaxMhOyd X-Received: by 2002:a05:6a21:4981:b0:1ad:8109:48f2 with SMTP id ax1-20020a056a21498100b001ad810948f2mr8742562pzc.36.1714385692076; Mon, 29 Apr 2024 03:14:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714385692; cv=pass; d=google.com; s=arc-20160816; b=WFiHRM1+98YnPvq2ADaCcH42ybVtl4QuWyPJ5m5b8MEn6o2reRmr7r6wA/jMv+mnjQ Cw6EO2GQ2dqwLxfhl33QdgG87Ik3woLNAX6FY2c1Qdv0EgayHKk8R2GwR1saiJFdrv3i KZMvdj4+ePuRw2vvwftCJjCzBV5PeV6wlezVykaY5WQF2/rdOEPQo+ORf69mfJWrqnE3 aD2SfYWZUYCeJ9Nccq+t9pgOsnIamn8sLREN+XsQKkq1iWQPg2Evu//ox6VabbE1JRG2 G1xFWmtG/LCzUw2HVRve7ggtpZ88m2n1meJiDYeEVO5OOwHSE1HGOvX/Zo/lA0++SJ3a KP6Q== 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=x4Kh6SgnD3DEa96jCeHBWIW1JPmHvCoT4CqiUmflZJE=; fh=J257D8fwb/+62Vcgdn9VfjMZXjRl80a3qN/eoi5IJQE=; b=LAnK/X7vQqe/oAaA0ydhowbjL5ESMqRE7nqZ7JgSbABN1RfMsnbtWvededhXgRJRUz S5JE5JpTnk9MRXzysEXtR0oklhSNL7MAw7qYLi2z5jFS0cNM24zaWAqFvHngaVSb9gPP HaFJ5K57UsGrCZ+L+nucrJKFAntHlh+ZzJNE7xQlZWXvCSb+rb3ZYDuNSB8YB9sDxYpx b55ymqZB/DB/HA3F5ZEtmGPPhAkg46Jc6VPWE0oSyrUeR9gHx2kN8dNehMHQrlSk36N2 1ZMrDtRx0wyBZVO4wjZ6DJ25lxUmIedFhpLFBGzKJI/vjYDJYJz07/ISlg/+hCxsRi65 vlGA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PIQukdBR; 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-162024-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162024-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id r11-20020aa79ecb000000b006f3e30d19f5si5280525pfq.197.2024.04.29.03.14.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 03:14:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162024-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PIQukdBR; 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-162024-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162024-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B103C282BE2 for ; Mon, 29 Apr 2024 10:14:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 799B53A1C2; Mon, 29 Apr 2024 10:14:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PIQukdBR" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 C0BAB36B08 for ; Mon, 29 Apr 2024 10:14:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714385662; cv=none; b=O1k2GyotGmRBExj/JMyhjJ2/0MluBI7U9BFN1koH/8vBR1qqgBjrRlV+USHEpFTtkvCr40ckLNUKPkyhiyx5u/8IigX254muP/9xr+GZNy6Kf/I8UpsMmWAVbXlRguk44F4s1I282w0taIf0vUbRqwg7NfTdfjkTTNo8HP1NlKk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714385662; c=relaxed/simple; bh=6SjZPcbUbLKZ4v3s8EBPZH85hWGE+Q59iUn92olayg4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HiYYARmtkntIW78zzR2i+5LkZ42+XZwb6fFhJ6n+6EOqhSgdusc0HT5dTPHJfypA+4k1G3CjQgLwDDt1HpGp3CDG2LfDaRgl/lXoEptNs3zjg++62vPJS+iNYbeQUd1hbA8r/jatjQGKLeYTpwtrHPGFDsCGUgOXwIPEMY9+Sb0= 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=PIQukdBR; arc=none smtp.client-ip=170.10.133.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=1714385659; 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=x4Kh6SgnD3DEa96jCeHBWIW1JPmHvCoT4CqiUmflZJE=; b=PIQukdBRoUd1MZgEHKPVaOOKlh92zvNf5bv6BJ0DBn4cZL2PtHu9Uji4M3J75f3ACeS1Kr BspZwoAk+SixHWvbNBnLw5EQvrScDGqXZ/sLtbP5w2eVI1Nvo8/yvIaWmICpc65eAJ0+u7 V0PFdNmAOEau80ZRghCadcLwVUuBPzE= 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-682-K6vRAmYKNN66VAVs_T2FBQ-1; Mon, 29 Apr 2024 06:14:16 -0400 X-MC-Unique: K6vRAmYKNN66VAVs_T2FBQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 52372100E77E; Mon, 29 Apr 2024 10:14:16 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DE37C1C060D0; Mon, 29 Apr 2024 10:14:13 +0000 (UTC) From: Gavin Shan To: virtualization@lists.linux.dev Cc: linux-kernel@vger.kernel.org, mst@redhat.com, jasowang@redhat.com, shan.gavin@gmail.com Subject: [PATCH v2 1/4] vhost: Improve vhost_get_avail_idx() with smp_rmb() Date: Mon, 29 Apr 2024 20:13:57 +1000 Message-ID: <20240429101400.617007-2-gshan@redhat.com> In-Reply-To: <20240429101400.617007-1-gshan@redhat.com> References: <20240429101400.617007-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.7 From: "Michael S. Tsirkin" All the callers of vhost_get_avail_idx() are concerned to the memory barrier, imposed by smp_rmb() to ensure the order of the available ring entry read and avail_idx read. Improve vhost_get_avail_idx() so that smp_rmb() is executed when the avail_idx is advanced. With it, the callers needn't to worry about the memory barrier. No functional change intended. Signed-off-by: Michael S. Tsirkin [gshan: repainted vhost_get_avail_idx()] Reviewed-by: Gavin Shan Acked-by: Will Deacon --- drivers/vhost/vhost.c | 106 +++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 64 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 8995730ce0bf..7aa623117aab 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1290,10 +1290,36 @@ static void vhost_dev_unlock_vqs(struct vhost_dev *d) mutex_unlock(&d->vqs[i]->mutex); } -static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq, - __virtio16 *idx) +static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq) { - return vhost_get_avail(vq, *idx, &vq->avail->idx); + __virtio16 idx; + int r; + + r = vhost_get_avail(vq, idx, &vq->avail->idx); + if (unlikely(r < 0)) { + vq_err(vq, "Failed to access available index at %p (%d)\n", + &vq->avail->idx, r); + return r; + } + + /* Check it isn't doing very strange thing with available indexes */ + vq->avail_idx = vhost16_to_cpu(vq, idx); + if (unlikely((u16)(vq->avail_idx - vq->last_avail_idx) > vq->num)) { + vq_err(vq, "Invalid available index change from %u to %u", + vq->last_avail_idx, vq->avail_idx); + return -EINVAL; + } + + /* We're done if there is nothing new */ + if (vq->avail_idx == vq->last_avail_idx) + return 0; + + /* + * We updated vq->avail_idx so we need a memory barrier between + * the index read above and the caller reading avail ring entries. + */ + smp_rmb(); + return 1; } static inline int vhost_get_avail_head(struct vhost_virtqueue *vq, @@ -2498,38 +2524,17 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, { struct vring_desc desc; unsigned int i, head, found = 0; - u16 last_avail_idx; - __virtio16 avail_idx; + u16 last_avail_idx = vq->last_avail_idx; __virtio16 ring_head; int ret, access; - /* Check it isn't doing very strange things with descriptor numbers. */ - last_avail_idx = vq->last_avail_idx; - if (vq->avail_idx == vq->last_avail_idx) { - if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) { - vq_err(vq, "Failed to access avail idx at %p\n", - &vq->avail->idx); - return -EFAULT; - } - vq->avail_idx = vhost16_to_cpu(vq, avail_idx); - - if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { - vq_err(vq, "Guest moved avail index from %u to %u", - last_avail_idx, vq->avail_idx); - return -EFAULT; - } + ret = vhost_get_avail_idx(vq); + if (unlikely(ret < 0)) + return ret; - /* If there's nothing new since last we looked, return - * invalid. - */ - if (vq->avail_idx == last_avail_idx) + if (!ret) return vq->num; - - /* Only get avail ring entries after they have been - * exposed by guest. - */ - smp_rmb(); } /* Grab the next descriptor number they're advertising, and increment @@ -2790,35 +2795,20 @@ EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n); /* return true if we're sure that avaiable ring is empty */ bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) { - __virtio16 avail_idx; int r; if (vq->avail_idx != vq->last_avail_idx) return false; - 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 true; + /* Treat error as non-empty here */ + r = vhost_get_avail_idx(vq); + return r == 0; } EXPORT_SYMBOL_GPL(vhost_vq_avail_empty); /* OK, now we need to know about added descriptors. */ bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) { - __virtio16 avail_idx; int r; if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) @@ -2842,25 +2832,13 @@ bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) /* They could have slipped one in as we were doing that: make * sure it's written, then check again. */ smp_mb(); - r = vhost_get_avail_idx(vq, &avail_idx); - if (r) { - vq_err(vq, "Failed to check avail idx at %p: %d\n", - &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; - } + /* Treat error as empty here */ + r = vhost_get_avail_idx(vq); + if (unlikely(r < 0)) + return false; - return false; + return r; } EXPORT_SYMBOL_GPL(vhost_enable_notify); -- 2.44.0