From nobody Tue Feb 10 20:05:49 2026 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 3894017C66 for ; Tue, 23 Apr 2024 03:24:23 +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=1713842664; cv=none; b=qPFq05vUy1HIwHuzeAv8wI+KoG+B7sQ/zHqL3jL/rkgbVaWVDOIf901iFNMQfl2ms5C24BC2JdZq7oC4zHj5gx5zYzJBWsZxQTBtXIDKdvbzRJN8K+ihxBdL222Vni3rR/eSriOwGvH23DLFGVJL+pA6nPb25HoCy52Vf8JDed8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713842664; c=relaxed/simple; bh=LfYpnyQAsBnDVm5T77C4UR9syKWZT60Wqobuc9Uot60=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CKcrGl7+qvqk47VWWk5ZBEwtmsRfLv+JSBndGu+2f2/tXzssPIU/4WjvR2QNcc95baI+iRBzIvjl/0RAiTBona2ly86t9xzYHuZJw7ywEscv9G+R9WPWHg3pCxrguvEds3C2d/Z0kE/ckRLC1NoUSuQhG0bPTWxq4ebGorE3dOY= 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=gLlgDQeL; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gLlgDQeL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713842662; 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=EdbGfF2EWeWPE3Aari8NoS/LX4R/2UfIijpnrdTbWd0=; b=gLlgDQeLWkTHxa09oG0fESn8IjtktJfAbtH3ii1bnGiuDK9X0k+GmG4TCp1Y1FKTUFHD29 aOr1Yuij78dtw7DOGMDTROgLg1ZW7f4pdQlhs0Zpzeku4rsmU+cEpb1aUVabRQb6S2JScb q21lb7wTYxidbARj3Vp7ASkHCw1ryaM= 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-507-cfbfH8CoNKiFSi8BANP4Gg-1; Mon, 22 Apr 2024 23:24:18 -0400 X-MC-Unique: cfbfH8CoNKiFSi8BANP4Gg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 4B54D80B935; Tue, 23 Apr 2024 03:24:18 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2395A1BDAA; Tue, 23 Apr 2024 03:24:15 +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 1/4] vhost: Drop variable last_avail_idx in vhost_get_vq_desc() Date: Tue, 23 Apr 2024 13:24:04 +1000 Message-ID: <20240423032407.262329-2-gshan@redhat.com> In-Reply-To: <20240423032407.262329-1-gshan@redhat.com> References: <20240423032407.262329-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Content-Type: text/plain; charset="utf-8" The local variable @last_avail_idx is equivalent to vq->last_avail_idx. So the code can be simplified a bit by dropping the local variable @last_avail_idx. No functional change intended. Signed-off-by: Gavin Shan --- drivers/vhost/vhost.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 8995730ce0bf..ef7942103232 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2498,14 +2498,11 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, { struct vring_desc desc; unsigned int i, head, found =3D 0; - u16 last_avail_idx; __virtio16 avail_idx; __virtio16 ring_head; int ret, access; =20 /* Check it isn't doing very strange things with descriptor numbers. */ - last_avail_idx =3D vq->last_avail_idx; - if (vq->avail_idx =3D=3D vq->last_avail_idx) { if (unlikely(vhost_get_avail_idx(vq, &avail_idx))) { vq_err(vq, "Failed to access avail idx at %p\n", @@ -2514,16 +2511,16 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, } vq->avail_idx =3D vhost16_to_cpu(vq, avail_idx); =20 - if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) { + if (unlikely((u16)(vq->avail_idx - vq->last_avail_idx) > vq->num)) { vq_err(vq, "Guest moved avail index from %u to %u", - last_avail_idx, vq->avail_idx); + vq->last_avail_idx, vq->avail_idx); return -EFAULT; } =20 /* If there's nothing new since last we looked, return * invalid. */ - if (vq->avail_idx =3D=3D last_avail_idx) + if (vq->avail_idx =3D=3D vq->last_avail_idx) return vq->num; =20 /* Only get avail ring entries after they have been @@ -2534,10 +2531,10 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, =20 /* Grab the next descriptor number they're advertising, and increment * the index we've seen. */ - if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) { + if (unlikely(vhost_get_avail_head(vq, &ring_head, vq->last_avail_idx))) { vq_err(vq, "Failed to read head: idx %d address %p\n", - last_avail_idx, - &vq->avail->ring[last_avail_idx % vq->num]); + vq->last_avail_idx, + &vq->avail->ring[vq->last_avail_idx % vq->num]); return -EFAULT; } =20 --=20 2.44.0 From nobody Tue Feb 10 20:05:49 2026 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 E4DE61CA81 for ; Tue, 23 Apr 2024 03:24:25 +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=1713842667; cv=none; b=W+LVDytD9nLgkIfUghVEZFb9Zy2TRlQqNbJ8H94Iaa+w0kaTjPpD7bWB8ortUKrCXZEoaYPq/F2A5iSA+c5K7jlWf/c5eg4+vCQZbJXVIAUWn1N9eulBchZ86u1pS7k32BMSAfrGHmuFq7RBB+d5c6eZyajdI4oiXQhBh445JP4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713842667; c=relaxed/simple; bh=LRQl9lv2M8QSOZPByVQiOvj73HlXJF/ZAmTJj/fGtZM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LGi37c1wUxRWyXxUDfNw7kQUwQl4tL6OLSLurP7fH47e/TSJ6+QGTzrA83epfpzdchqTF4Gn5i8Vn3A3RMGkwTA3Qgg/m1PFeJFqX98NX7ckJnINM6zyw0ixe23j9H8orT6JhEXJn+jsvWVKR18eVHTzG+3jyPkH+9rrkQzWpBE= 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=XNPRos7K; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XNPRos7K" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713842664; 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=/BuCY3v8aKDYplSDVVzxpIvd+2taH57MWRDqJV/LGJ4=; b=XNPRos7K5V8ohkM4l1Md0+oWydcxM/C0+uZqAUOPZnUyn4sQEUpsxCWj+ry/G5xYSocm4u QW3rfBiKVAHies/orIlisLdnF51dIkzOHwRvFR7sai0Fw3PN0mHS8qxvsgIwYpCb8N12/E ngzT7J8p3RilDkt0D1g67dqi4i/sdEY= 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-127-O2h3yeDePoGQV3yoIrddFw-1; Mon, 22 Apr 2024 23:24:21 -0400 X-MC-Unique: O2h3yeDePoGQV3yoIrddFw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 0E0F380591B; Tue, 23 Apr 2024 03:24:21 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DD1E51BDAA; Tue, 23 Apr 2024 03:24:18 +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 2/4] vhost: Improve vhost_get_avail_idx() with smp_rmb() Date: Tue, 23 Apr 2024 13:24:05 +1000 Message-ID: <20240423032407.262329-3-gshan@redhat.com> In-Reply-To: <20240423032407.262329-1-gshan@redhat.com> References: <20240423032407.262329-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Content-Type: text/plain; charset="utf-8" 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. Suggested-by: Michael S. Tsirkin Signed-off-by: Gavin Shan --- drivers/vhost/vhost.c | 91 ++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 57 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index ef7942103232..b3adc0bc9e72 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1290,10 +1290,34 @@ static void vhost_dev_unlock_vqs(struct vhost_dev *= d) mutex_unlock(&d->vqs[i]->mutex); } =20 -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 avail_idx; + int r; + + r =3D vhost_get_avail(vq, avail_idx, &vq->avail->idx); + if (unlikely(r)) { + vq_err(vq, "Failed to access avail idx at %p\n", + &vq->avail->idx); + return r; + } + + vq->avail_idx =3D vhost16_to_cpu(vq, avail_idx); + if (vq->avail_idx !=3D vq->last_avail_idx) { + /* Ensure the available ring entry read happens + * before the avail_idx read when the avail_idx + * is advanced. + */ + smp_rmb(); + } + + if (unlikely((u16)(vq->avail_idx - vq->last_avail_idx) > vq->num)) { + vq_err(vq, "Invalid avail index change from %u to %u", + vq->last_avail_idx, vq->avail_idx); + return -EINVAL; + } + + return 0; } =20 static inline int vhost_get_avail_head(struct vhost_virtqueue *vq, @@ -2498,35 +2522,19 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, { struct vring_desc desc; unsigned int i, head, found =3D 0; - __virtio16 avail_idx; __virtio16 ring_head; int ret, access; =20 - /* Check it isn't doing very strange things with descriptor numbers. */ if (vq->avail_idx =3D=3D 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 =3D vhost16_to_cpu(vq, avail_idx); - - if (unlikely((u16)(vq->avail_idx - vq->last_avail_idx) > vq->num)) { - vq_err(vq, "Guest moved avail index from %u to %u", - vq->last_avail_idx, vq->avail_idx); - return -EFAULT; - } + ret =3D vhost_get_avail_idx(vq); + if (unlikely(ret)) + return ret; =20 /* If there's nothing new since last we looked, return * invalid. */ if (vq->avail_idx =3D=3D vq->last_avail_idx) return vq->num; - - /* Only get avail ring entries after they have been - * exposed by guest. - */ - smp_rmb(); } =20 /* Grab the next descriptor number they're advertising, and increment @@ -2787,35 +2795,19 @@ 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 *v= q) { - __virtio16 avail_idx; - int r; - if (vq->avail_idx !=3D vq->last_avail_idx) return false; =20 - r =3D vhost_get_avail_idx(vq, &avail_idx); - if (unlikely(r)) - return false; - - vq->avail_idx =3D vhost16_to_cpu(vq, avail_idx); - if (vq->avail_idx !=3D 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(); + if (unlikely(vhost_get_avail_idx(vq))) return false; - } =20 - return true; + return vq->avail_idx =3D=3D vq->last_avail_idx; } EXPORT_SYMBOL_GPL(vhost_vq_avail_empty); =20 /* 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; =20 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) @@ -2839,25 +2831,10 @@ bool vhost_enable_notify(struct vhost_dev *dev, str= uct 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 =3D 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); + if (unlikely(vhost_get_avail_idx(vq))) return false; - } - - vq->avail_idx =3D vhost16_to_cpu(vq, avail_idx); - if (vq->avail_idx !=3D 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; - } =20 - return false; + return vq->avail_idx !=3D vq->last_avail_idx; } EXPORT_SYMBOL_GPL(vhost_enable_notify); =20 --=20 2.44.0 From nobody Tue Feb 10 20:05:49 2026 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 0EC631CD35 for ; Tue, 23 Apr 2024 03:24:26 +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=1713842668; cv=none; b=iLefBY4lEA2FKIb5PGhqpHUdOvQciIBAtGzgknd1DDUyeIIvMRObGaoGt8pXpBrKrgseDeGudb5LSpQjgtp4hFF/vttpBajLMlAfxniuHagaa40Rvj35HNVomTgiNkwCoVIl1rqejfdH19lNkWEXWF0NDbBxwEg7MrWF/gFsav0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713842668; c=relaxed/simple; bh=VXNuueCqLjer3SK5FWiU+oQ2njUy2Ft9Y+uSyol2th0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VGtJyykGkUs22bfUC+vvHa7h0f6LGufcpA2pYDbfEIOCuAAw4yovZJyMzHcddQitEIQxq8u1lgWlFGQoZe6KInq+Cn1/CVkkOy8gHIuvePSdE2wfamzPmzLn9tE0x5Wmy7kSfZJmQQW3IX/SAyXFi6APfvOgncJkJjh9Df37IoA= 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=E+20g9wQ; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E+20g9wQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713842666; 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=HxWHZLxO01yGdS6uNhVtl2pRAw0NwEn5kPBcPnzmn/Y=; b=E+20g9wQ1k8R5QMRoY32FFUkbkTD2w1AB7/iFjCrQOciUtEqOjUYsYMZhEcZG4dQ/P7j2s Hh+b5rhZtc265ap6S4G+U3aXZeyV2SwlGs524tdMNrAaBiNhlhFSB5gypyklzsg4GnQn/Y LiRU6/VjfsvvOerzDiu8LGbcGOqNesI= 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-655-ZXEZtGiTMAOnPUbEzktHhw-1; Mon, 22 Apr 2024 23:24:23 -0400 X-MC-Unique: ZXEZtGiTMAOnPUbEzktHhw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 8BCE68943A5; Tue, 23 Apr 2024 03:24:23 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8590B1BDAA; Tue, 23 Apr 2024 03:24:21 +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 3/4] vhost: Improve vhost_get_avail_head() Date: Tue, 23 Apr 2024 13:24:06 +1000 Message-ID: <20240423032407.262329-4-gshan@redhat.com> In-Reply-To: <20240423032407.262329-1-gshan@redhat.com> References: <20240423032407.262329-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Content-Type: text/plain; charset="utf-8" Improve vhost_get_avail_head() so that the head or errno is returned. With it, the relevant sanity checks are squeezed to vhost_get_avail_head() and vhost_get_vq_desc() is further simplified. No functional change intended. Signed-off-by: Gavin Shan --- drivers/vhost/vhost.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index b3adc0bc9e72..a3de9325175f 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1320,11 +1320,27 @@ static inline int vhost_get_avail_idx(struct vhost_= virtqueue *vq) return 0; } =20 -static inline int vhost_get_avail_head(struct vhost_virtqueue *vq, - __virtio16 *head, int idx) +static inline int vhost_get_avail_head(struct vhost_virtqueue *vq) { - return vhost_get_avail(vq, *head, - &vq->avail->ring[idx & (vq->num - 1)]); + __virtio16 head; + int r; + + r =3D vhost_get_avail(vq, head, + &vq->avail->ring[vq->last_avail_idx & (vq->num - 1)]); + if (unlikely(r)) { + vq_err(vq, "Failed to read head: idx %u address %p\n", + vq->last_avail_idx, + &vq->avail->ring[vq->last_avail_idx % vq->num]); + return r; + } + + r =3D vhost16_to_cpu(vq, head); + if (unlikely(r >=3D vq->num)) { + vq_err(vq, "Invalid head %d (%u)\n", r, vq->num); + return -EINVAL; + } + + return r; } =20 static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq, @@ -2522,7 +2538,6 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, { struct vring_desc desc; unsigned int i, head, found =3D 0; - __virtio16 ring_head; int ret, access; =20 if (vq->avail_idx =3D=3D vq->last_avail_idx) { @@ -2539,21 +2554,9 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, =20 /* Grab the next descriptor number they're advertising, and increment * the index we've seen. */ - if (unlikely(vhost_get_avail_head(vq, &ring_head, vq->last_avail_idx))) { - vq_err(vq, "Failed to read head: idx %d address %p\n", - vq->last_avail_idx, - &vq->avail->ring[vq->last_avail_idx % vq->num]); - return -EFAULT; - } - - head =3D vhost16_to_cpu(vq, ring_head); - - /* If their number is silly, that's an error. */ - if (unlikely(head >=3D vq->num)) { - vq_err(vq, "Guest says index %u > %u is available", - head, vq->num); - return -EINVAL; - } + head =3D vhost_get_avail_head(vq); + if (unlikely(head < 0)) + return head; =20 /* When we start there are none of either input nor output. */ *out_num =3D *in_num =3D 0; --=20 2.44.0 From nobody Tue Feb 10 20:05:49 2026 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 4AEA0219FC for ; Tue, 23 Apr 2024 03:24:31 +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=1713842672; cv=none; b=ED8I76AdBadF6DcaifjI2u3GKTfIqJHQPNOpQvo+DxUOUqigCfF3BQdhzlVCJcXsDhL+H2H1cLtpOP7xsdSIEfrtGu3PlLNe/78rCDPFX3tWrI0e5B8UmjTLMXzQ9afG79mUiOkUatZ9KOraGgwoPA9oe6I45UNqljcnDL9Rzts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713842672; c=relaxed/simple; bh=5MVVIcDipqgJAk3mCYyyzSHu50ImzAuTrQ9oKbZbq3w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=j2xU5FtZRgY8zieeLaZJlbBXdSKeapFTyfrDdMIGtmbvMmkNtX1hxaLWbVjXpOzjuB8WLqeoyZb+2o0Q1UtJSmbR8qQn/JJ8Lg5DZkhtpSgaoMSb2YVteaU4m/IibIhNlIq4kSex+qTkRV/Vovp0djLHzMCHmz2OkZTth2nP9A0= 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=ByNErQpW; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ByNErQpW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713842670; 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=qtMGrJ3CLLK0RMLaaz/XukLZE22bi0+EJWg0qr8z6dw=; b=ByNErQpWLEuR+y/0VexF/g2/zIX6ZqJlWMFPDXAyxYBA1jIUOpp5JvZ26qgTtSLmbm0yeZ XFL0AX5xg3+krFwJN9Be3N0Gxl9LghLvULgiwAZxyh+AFuV1WN+wSYepjNTS4kbG0XZHhT qWgtlGBflcQdiavq3cM6kwrinyVbMak= 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-653-NjVT6l0HMT-_YCt94dPB7Q-1; Mon, 22 Apr 2024 23:24:26 -0400 X-MC-Unique: NjVT6l0HMT-_YCt94dPB7Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 615C11C068C0; Tue, 23 Apr 2024 03:24:26 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0FA181BDAA; Tue, 23 Apr 2024 03:24:23 +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 4/4] vhost: Reformat vhost_{get, put}_user() Date: Tue, 23 Apr 2024 13:24:07 +1000 Message-ID: <20240423032407.262329-5-gshan@redhat.com> In-Reply-To: <20240423032407.262329-1-gshan@redhat.com> References: <20240423032407.262329-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Content-Type: text/plain; charset="utf-8" Reformat the macros to use tab as the terminator for each line so that it looks clean. No functional change intended. Signed-off-by: Gavin Shan --- drivers/vhost/vhost.c | 60 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index a3de9325175f..3be19877f9df 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1207,21 +1207,22 @@ static inline void __user *__vhost_get_user(struct = vhost_virtqueue *vq, return __vhost_get_user_slow(vq, addr, size, type); } =20 -#define vhost_put_user(vq, x, ptr) \ -({ \ - int ret; \ - if (!vq->iotlb) { \ - ret =3D __put_user(x, ptr); \ - } else { \ - __typeof__(ptr) to =3D \ +#define vhost_put_user(vq, x, ptr) \ +({ \ + int ret; \ + if (!vq->iotlb) { \ + ret =3D __put_user(x, ptr); \ + } else { \ + __typeof__(ptr) to =3D \ (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ - sizeof(*ptr), VHOST_ADDR_USED); \ - if (to !=3D NULL) \ - ret =3D __put_user(x, to); \ - else \ - ret =3D -EFAULT; \ - } \ - ret; \ + sizeof(*ptr), \ + VHOST_ADDR_USED); \ + if (to !=3D NULL) \ + ret =3D __put_user(x, to); \ + else \ + ret =3D -EFAULT; \ + } \ + ret; \ }) =20 static inline int vhost_put_avail_event(struct vhost_virtqueue *vq) @@ -1252,22 +1253,21 @@ static inline int vhost_put_used_idx(struct vhost_v= irtqueue *vq) &vq->used->idx); } =20 -#define vhost_get_user(vq, x, ptr, type) \ -({ \ - int ret; \ - if (!vq->iotlb) { \ - ret =3D __get_user(x, ptr); \ - } else { \ - __typeof__(ptr) from =3D \ - (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ - sizeof(*ptr), \ - type); \ - if (from !=3D NULL) \ - ret =3D __get_user(x, from); \ - else \ - ret =3D -EFAULT; \ - } \ - ret; \ +#define vhost_get_user(vq, x, ptr, type) \ +({ \ + int ret; \ + if (!vq->iotlb) { \ + ret =3D __get_user(x, ptr); \ + } else { \ + __typeof__(ptr) from =3D \ + (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ + sizeof(*ptr), type); \ + if (from !=3D NULL) \ + ret =3D __get_user(x, from); \ + else \ + ret =3D -EFAULT; \ + } \ + ret; \ }) =20 #define vhost_get_avail(vq, x, ptr) \ --=20 2.44.0