Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2689572lqt; Mon, 22 Apr 2024 20:25:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXxY2nZGtPqwtAdCN7Y5gM3t2AUqnByvoCUH8q1DYatZPvdYNAzKQXpESYyer7YCPHS9TTFWd5MKON79g+hHHOch/jATEP0d2tgTfN1JA== X-Google-Smtp-Source: AGHT+IGug3pBWcQqKPIJITWlcO0cXALdS97KwJOODi6hhFGRxnKAvQS5ZVUYfUV+HkaNKX294SF4 X-Received: by 2002:a17:906:ccc2:b0:a51:d23e:c53 with SMTP id ot2-20020a170906ccc200b00a51d23e0c53mr6946948ejb.31.1713842713562; Mon, 22 Apr 2024 20:25:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713842713; cv=pass; d=google.com; s=arc-20160816; b=V6RM0FfDsO2YOoYMHnRS9bIiK8qV98Kgs53x9HqYtiyVWtRg7sTSpF8saHFLUAPM1e FoTOJ0dL+LHQ9ZWmZDfidR0gBYRFG1b72wNRQ21r8m7csyXx5CdAyTqIiPXfmYCAv+1V zCpmdb086CPcMi5sNqbwE1LiKvksapNvqDpzX2DwsOBlbEBQerNvHFM/AWOQaVMKddVI uQkHiDRofWmBaM13K8fakHsbEUJ+0h40qKMSLyi8vObvj7kk1p/AMUQZa8CKoJmcYVAF 0dX2RylTQ18e87B/PCIMaT1lQLIRaT39ZHQ17s+6SY5Z0bOte7/1eJGsaPnVbFtFG6Dt axTQ== 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=HxWHZLxO01yGdS6uNhVtl2pRAw0NwEn5kPBcPnzmn/Y=; fh=J257D8fwb/+62Vcgdn9VfjMZXjRl80a3qN/eoi5IJQE=; b=AmJA7cSpCLtPagFGqH/r3pcR/oQe/57jaAyUiM/HwOxpK5WJqBUbuPgAK2oz8I/7TH xs7jXblxZSAGTJzgWX6TSqYKZsgKAzodlUUZcRkZPhTRVsylPSMWah7o8HCz1t47UMg2 ZOH5v28bkC0y2wG2aZbcLMustlkNTnM8Dg3X4tpLLffEwtp45z4QQ9hQLowekfxMzPJ9 4X0bEQR4SNWYBPJBxsyhCLphux52bvVvsSwxNSsZU+Gy75FT654kyswiSCkoZwhlPYFV ORTsWf+E5T5zYc/019iHA2NbOL6aj0zOCfCB4ZStMyRt8YHPKWntLm27SLVqmJ2p5uVN qUBw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=E+20g9wQ; 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-154455-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154455-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id m13-20020a1709061ecd00b00a520ce94de8si6223022ejj.1009.2024.04.22.20.25.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 20:25:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154455-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=E+20g9wQ; 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-154455-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154455-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 4E66D1F22FEB for ; Tue, 23 Apr 2024 03:25:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 479CA200CB; Tue, 23 Apr 2024 03:24:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E+20g9wQ" 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 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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 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; } -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 = 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 = vhost16_to_cpu(vq, head); + if (unlikely(r >= vq->num)) { + vq_err(vq, "Invalid head %d (%u)\n", r, vq->num); + return -EINVAL; + } + + return r; } 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 = 0; - __virtio16 ring_head; int ret, access; if (vq->avail_idx == vq->last_avail_idx) { @@ -2539,21 +2554,9 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, /* 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 = vhost16_to_cpu(vq, ring_head); - - /* If their number is silly, that's an error. */ - if (unlikely(head >= vq->num)) { - vq_err(vq, "Guest says index %u > %u is available", - head, vq->num); - return -EINVAL; - } + head = vhost_get_avail_head(vq); + if (unlikely(head < 0)) + return head; /* When we start there are none of either input nor output. */ *out_num = *in_num = 0; -- 2.44.0