Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1258101lqa; Mon, 29 Apr 2024 03:15:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhJ7aO0yX0gaYw9HAw5TUntTSajSJQz8pCySLy4GvuZeFVcMJ1BFT/9VPTtslK7imB63pBODrYj+ZVhbzWqZkAMgGgthYCqC7NSC+Hpw== X-Google-Smtp-Source: AGHT+IFb9WhE7xcHLG1/p+49jGQtGBF4DTPq4VJCM9P+MXhzvF4kXUuPCpgmu1FV5r63ETPXeOja X-Received: by 2002:a17:902:ce12:b0:1eb:5344:6a01 with SMTP id k18-20020a170902ce1200b001eb53446a01mr7115976plg.44.1714385733768; Mon, 29 Apr 2024 03:15:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714385733; cv=pass; d=google.com; s=arc-20160816; b=eXnpt1uzPnvfM8c0SjT+gFCaXIonKiHR+y4e3mcDUWuLhTWSUZ7d+Gsv+xrPvfOfpa idihDAYbkdUeRQN6Sg67gft6GJgVNFoXFIyhFl3OH+2Oqh3ZoaUtOZY9+Z5cRYBN44kX pkppRAKGIdyQlCPg23rEHtA3deGpf9GmXgbHrW9WsDBnWjrOXV2gXuHSQw+AXEmOj9J3 EkRTgiSGSaPvC7ys8FwmnSJdsVSbBFVKscyMf56JFq8lcWRC1jpyF5XlvhwobRDwrpAs ire2aNCjti50bpZ1hf9udmx5suBmYE9CO2JY9qolWY9ragxqtaKKhIm9nfO9t1Oz93Lm g4ww== 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=A9NTPnuJF9Q3tKbWrrHkgNuzaaSPtWXInMPFcIIlGPM=; fh=J257D8fwb/+62Vcgdn9VfjMZXjRl80a3qN/eoi5IJQE=; b=ILvpowzMv8ePkZ7imKxROdNr5XQOfw1jUkpmoKaRG77R2Q7ImTF9mY9WDodhLa1S/4 ayXI3fo4uhevO034ZSrEKSyu4yCf9/KH/YUhFyBZ6Y64rfsiNnsBfo6AOtD/RqPTqHzL X6gDOCXJcS6G22tFtrElni+hmq1fxW5+dJi2RU3gvD89rvknFIQ15n7qmdlTy5Q2ewnC wDqwe/AYfqSpso2rjSQiAdJLjRdUt2CZEQ4O2XP+MiUIphfwptiRSQf+igYBDAQiE52B XD7uPwnpm/nO4qAVb+RtUmfCdfGi4sa/j+95JDqDshM8ygQr+TWVqQEAuSKNzyjFqwt/ yoxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VnrlGBdL; 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-162026-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162026-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id e1-20020a170902784100b001ea26bf0919si10549878pln.467.2024.04.29.03.15.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 03:15:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162026-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VnrlGBdL; 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-162026-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162026-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 6FA0AB21EC5 for ; Mon, 29 Apr 2024 10:15:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE1163C06B; Mon, 29 Apr 2024 10:14:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VnrlGBdL" 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 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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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; } -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: index %u address %p\n", + vq->last_avail_idx, + &vq->avail->ring[vq->last_avail_idx & (vq->num - 1)]); + 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, @@ -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 = 0; - __virtio16 ring_head; - int ret, access; + unsigned int i, found = 0; + int head, ret, access; if (vq->avail_idx == vq->last_avail_idx) { ret = vhost_get_avail_idx(vq); @@ -2536,23 +2551,10 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq, return vq->num; } - /* 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; - } + /* Grab the next descriptor number they're advertising */ + 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