From nobody Fri Dec 19 02:49:50 2025 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PIQukdBR" 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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" 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); } =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 idx; + int r; + + r =3D 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 =3D 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 =3D=3D 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; } =20 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 =3D 0; - u16 last_avail_idx; - __virtio16 avail_idx; + u16 last_avail_idx =3D vq->last_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", - &vq->avail->idx); - return -EFAULT; - } - vq->avail_idx =3D 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 =3D vhost_get_avail_idx(vq); + if (unlikely(ret < 0)) + return ret; =20 - /* If there's nothing new since last we looked, return - * invalid. - */ - if (vq->avail_idx =3D=3D last_avail_idx) + if (!ret) 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 @@ -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 *v= q) { - __virtio16 avail_idx; int r; =20 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(); - return false; - } - - return true; + /* Treat error as non-empty here */ + r =3D vhost_get_avail_idx(vq); + return r =3D=3D 0; } 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)) @@ -2842,25 +2832,13 @@ 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); - return false; - } =20 - 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; - } + /* Treat error as empty here */ + r =3D vhost_get_avail_idx(vq); + if (unlikely(r < 0)) + return false; =20 - return false; + return r; } EXPORT_SYMBOL_GPL(vhost_enable_notify); =20 --=20 2.44.0 From nobody Fri Dec 19 02:49:50 2025 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 AAF0C3B29A for ; Mon, 29 Apr 2024 10:14:24 +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=1714385667; cv=none; b=VVo536m0RlRRjbyc67js9E3GbzMhOSEVWjpY2wJmUED0G2LUp316RHdwPoipTubPPVTxAtk8ogtZ3+hdK4tW1il3hMr+ziDn+pEgthSoIaW76IwGQSfFHnWbKJIxdkLV4gBmwR4WTY8zjJe2ekynqO5qyhLqi3pDoRPCxMpFdB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714385667; c=relaxed/simple; bh=wFFVcm/k79HiZBQXjR6hHvK8Eq3yzCmKghIT42hEDwQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gabo9m2p2wTOYAWHEU6lPKqwQCbhEY7mywNJTvbNVsM0F8vTzNaZn4jPBCClTF/Pu5pyIGpjakYKBezdKNxqXAZUFtdKuvbKb2piDcPZi6p27YubEiiHD8sVJeIg3tfSHZfgfJ+ERd3dBnHZq1RnPYbUhrV5htPjKbUp7iywuOM= 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=VXqo3idc; 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="VXqo3idc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714385663; 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=pQ+t0av/EOP1urgumrrp1t7Px6sEa+YzHYZfwcovyAc=; b=VXqo3idcXUV5Q4coeNFgm/6h0cjL7DqJF6ECVI382pZAPIeh5gYX5St3gNHLQU1KzxhC7O 91zpblyH9UVgW6DVfNZACG33tZWCorZfu3giMTlbdv00fv4mHCnzz5Al2tN88g2dvdtyIP L0nt3kDRH6hrD4vFDkGDjsYzr4etiuU= 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-59-IldMwNX_M8WvfhBrYQeNAw-1; Mon, 29 Apr 2024 06:14:19 -0400 X-MC-Unique: IldMwNX_M8WvfhBrYQeNAw-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 210513C025D0; Mon, 29 Apr 2024 10:14:19 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E42A11C060D1; Mon, 29 Apr 2024 10:14:16 +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 2/4] vhost: Drop variable last_avail_idx in vhost_get_vq_desc() Date: Mon, 29 Apr 2024 20:13:58 +1000 Message-ID: <20240429101400.617007-3-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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 | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 7aa623117aab..b278c0333a66 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2524,7 +2524,6 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, { struct vring_desc desc; unsigned int i, head, found =3D 0; - u16 last_avail_idx =3D vq->last_avail_idx; __virtio16 ring_head; int ret, access; =20 @@ -2539,10 +2538,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 Fri Dec 19 02:49:50 2025 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 CD5B13B798 for ; Mon, 29 Apr 2024 10:14:24 +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=1714385667; cv=none; b=UYl9+Ks/NA/M8mnxHX57WSBsvDSq+Kr0onU5FfjhOdHlQoBJOZszuy/A4VrSyTxtJpE3cn+Qb4mJmKYkX384/fTOZHsmIyk8pjeQ/2cP7v86hn69xiEiYIEUSaBFbTXeoAN6oFW8KbrODur7QXRlmt5IytiHvPI82M97ZBKR0hc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714385667; c=relaxed/simple; bh=91YU68g5/Rx6XCivSIXx5FuQeNsrW8rUkwDzyohzLvE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GZKROJaugud0EWrRRkbAVUWGxYS7l3c01TtmP14pQ391pdTU6VVEVPsRv5XkpQikjUwGE2VTTfHLwy5mT9V/WSJiXJJMCCIXggT7Nl1I2jpIKEy1QozdDMYrUJaLB0/UK3TiJcc4ZE/M8A4BR+wSiwdWQg3vafjqEoUGWwg70wI= 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=VnrlGBdL; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VnrlGBdL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714385663; 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=A9NTPnuJF9Q3tKbWrrHkgNuzaaSPtWXInMPFcIIlGPM=; b=VnrlGBdLelgELGjj/2Z0STzk1v9dAmkhHImQEca4czD3vnYfc1/qxED17SxBscIpF5+kaM bCFC07TU+sTF2ACX4cXCFBAPZZwnXK27Z1+nKMBj3khqHNjzVTKl5OwF2jPyqPTDSm8gTR z7RJMIWJEEG9pjrrvLUl0kvKJk0fS50= 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-470-0qXNvE84N4Gy1o1ZFEXugg-1; Mon, 29 Apr 2024 06:14:22 -0400 X-MC-Unique: 0qXNvE84N4Gy1o1ZFEXugg-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 02D301E441CE; Mon, 29 Apr 2024 10:14:22 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AAA671C060D0; Mon, 29 Apr 2024 10:14:19 +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 3/4] vhost: Improve vhost_get_avail_head() Date: Mon, 29 Apr 2024 20:13:59 +1000 Message-ID: <20240429101400.617007-4-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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 | 50 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index b278c0333a66..4ddb9ec2fe46 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1322,11 +1322,27 @@ static inline int vhost_get_avail_idx(struct vhost_= virtqueue *vq) return 1; } =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: index %u address %p\n", + vq->last_avail_idx, + &vq->avail->ring[vq->last_avail_idx & (vq->num - 1)]); + 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, @@ -2523,9 +2539,8 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, struct vhost_log *log, unsigned int *log_num) { struct vring_desc desc; - unsigned int i, head, found =3D 0; - __virtio16 ring_head; - int ret, access; + unsigned int i, found =3D 0; + int head, ret, access; =20 if (vq->avail_idx =3D=3D vq->last_avail_idx) { ret =3D vhost_get_avail_idx(vq); @@ -2536,23 +2551,10 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, return vq->num; } =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; - } + /* Grab the next descriptor number they're advertising */ + 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 Fri Dec 19 02:49:50 2025 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 959413BBFA for ; Mon, 29 Apr 2024 10:14:27 +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=1714385669; cv=none; b=q3fa/4RORAq2UUyhwUZ4hsM/OqyNfNAKiT1o6KIckYE7XqbfFueswAUUvTT0NZdwveGyEWBgBbel5XJQebvrSJJDv7Qb4QcgK5h5iEnnN/kb7cNU4od7/GWxoCLJzTD1NkBOYA4nF5JkXct2QY6nsVM2pawgS4KjpbCZt/ENWx0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714385669; c=relaxed/simple; bh=wPCxHih6c1vg4MIhCKlt1UClyyDOfohtXqvQUkMZ+Pk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dKggz63TADapmTzuE5O0yDbW5B5CCXfkKEvtrVmHq6J1TXTxHqkiUjZAPoVuYOmR1Qh88Hss26VoieVp6L5GEvniFxuk52gAxv13gbki5etX0uYZ1jsD0RRP3e7e0m2AAy0yQJNqWZY4DuQbfCWCaf4wFApIxY9JTj9KqM2HsKw= 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=PC6N8eHw; 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="PC6N8eHw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714385666; 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=UVxg8KMA7vvgaacJi4UzbU3rBDHOjF9aejTpYpkSnrY=; b=PC6N8eHwBdmfvUKIZ35oM+mWK5CNsHqfFHdH9Esincmu6zttsp1Hlabxycot+POCBR+jt0 VpBJMfNiQLl8ogO+SMZijOlMlJQS/c8HKFJU50pHsBnUeLcD7ADccrxv7EkJOV1r8GKQRi aH+kTrmTNZ8qVJYQvZLnJQIaZH1AVos= 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-17-1Oyf9mrDPxefgbDRlG6VsQ-1; Mon, 29 Apr 2024 06:14:24 -0400 X-MC-Unique: 1Oyf9mrDPxefgbDRlG6VsQ-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 75E8F8032FA; Mon, 29 Apr 2024 10:14:24 +0000 (UTC) Received: from gshan-thinkpadx1nanogen2.remote.csb (unknown [10.64.136.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7A6111C060D0; Mon, 29 Apr 2024 10:14:22 +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 4/4] vhost: Reformat vhost_{get, put}_user() Date: Mon, 29 Apr 2024 20:14:00 +1000 Message-ID: <20240429101400.617007-5-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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 4ddb9ec2fe46..c1ed5e750521 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