From nobody Wed Dec 17 21:48:17 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1625070379; cv=none; d=zohomail.com; s=zohoarc; b=LrmhoxHeUoFqW37e5y06sWCB0WMllyb5SVJxK7JzWcDLFDtL4suTQJgvQVAC1NNIZZl5Vu7WGMEiRO9tDNwq1+KBLMtpVc2Jelk/0feqWsJKOsSx39V+P0tz9/alq0XD6axjwNGBWIZofZzbMOJpOPc8q1byNSM771CjAUQ2I7g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625070379; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FvGmCYj7ykVpCCWeIiMBmiblyJ4fnn6QIsHl9PyEB5w=; b=mhowKg//FUAEytzD7dg87P1meogWkMpFJf6Z+kUM0bxPEjGHILxjgQdDaatRV2Cd9FxtvVpY2mK2XbTgAV3ccnd2rimluHz9WJPX3YozSfJPd2nm0D3RXpiBbiSISlOAsHpnNgJRl6btQt2ys+0d7sJgSAypGke9Ls2fjQtCg6A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1625070379654637.7876320117282; Wed, 30 Jun 2021 09:26:19 -0700 (PDT) Received: from localhost ([::1]:45990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lyd2X-0004rJ-Mp for importer@patchew.org; Wed, 30 Jun 2021 12:26:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44370) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lycg8-000365-Pw for qemu-devel@nongnu.org; Wed, 30 Jun 2021 12:03:08 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:36277) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lycfj-00078y-Iv for qemu-devel@nongnu.org; Wed, 30 Jun 2021 12:03:08 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-361-Nu7390f_PpybcAjqiB_8HA-1; Wed, 30 Jun 2021 12:02:39 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C72131835AC4; Wed, 30 Jun 2021 16:02:38 +0000 (UTC) Received: from merkur.fritz.box (ovpn-114-114.ams2.redhat.com [10.36.114.114]) by smtp.corp.redhat.com (Postfix) with ESMTP id D5278179E6; Wed, 30 Jun 2021 16:02:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625068961; 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=FvGmCYj7ykVpCCWeIiMBmiblyJ4fnn6QIsHl9PyEB5w=; b=cU1u1iRcE+91PaBFU+UffZyuNzlOWV9L2KVkbJa035l4bBKBmqkl7UGNuDMuDRNX3GMn3V g2QvnxkMqlri/LWlURIsVuuyWcveRlEuNP0cUup3kHnVRvZ+vqjEC3fQtMsV3TC0ffXDy7 ea4jJsQ6vWC9BBCsyxQ+5zrbyH4F52U= X-MC-Unique: Nu7390f_PpybcAjqiB_8HA-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PULL 22/24] vhost: Distinguish errors in vhost_dev_get_config() Date: Wed, 30 Jun 2021 18:02:04 +0200 Message-Id: <20210630160206.276439-23-kwolf@redhat.com> In-Reply-To: <20210630160206.276439-1-kwolf@redhat.com> References: <20210630160206.276439-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kwolf@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.435, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Instead of just returning 0/-1 and letting the caller make up a meaningless error message, add an Error parameter to allow reporting the real error and switch to 0/-errno so that different kind of errors can be distinguished in the caller. config_len in vhost_user_get_config() is defined by the device, so if it's larger than VHOST_USER_MAX_CONFIG_SIZE, this is a programming error. Turn the corresponding check into an assertion. Signed-off-by: Kevin Wolf Message-Id: <20210609154658.350308-6-kwolf@redhat.com> Reviewed-by: Stefano Garzarella Reviewed-by: Raphael Norwitz Signed-off-by: Kevin Wolf --- include/hw/virtio/vhost-backend.h | 2 +- include/hw/virtio/vhost.h | 4 ++-- hw/block/vhost-user-blk.c | 9 +++++---- hw/display/vhost-user-gpu.c | 6 ++++-- hw/input/vhost-user-input.c | 6 ++++-- hw/net/vhost_net.c | 2 +- hw/virtio/vhost-user-vsock.c | 9 +++++---- hw/virtio/vhost-user.c | 24 ++++++++++++------------ hw/virtio/vhost-vdpa.c | 2 +- hw/virtio/vhost.c | 14 +++++++++++--- 10 files changed, 46 insertions(+), 32 deletions(-) diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index 728ebb0ed9..8475c5a29d 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -98,7 +98,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev,= const uint8_t *data, uint32_t offset, uint32_t size, uint32_t flags); typedef int (*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config, - uint32_t config_len); + uint32_t config_len, Error **errp); =20 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev, void *session_info, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 2d7aaad67b..045d0fd9f2 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -130,8 +130,8 @@ int vhost_net_set_backend(struct vhost_dev *hdev, struct vhost_vring_file *file); =20 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int writ= e); -int vhost_dev_get_config(struct vhost_dev *dev, uint8_t *config, - uint32_t config_len); +int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, + uint32_t config_len, Error **errp); int vhost_dev_set_config(struct vhost_dev *dev, const uint8_t *data, uint32_t offset, uint32_t size, uint32_t flags); /* notifier callback in case vhost device config space changed diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c index e9382e152a..3770f715da 100644 --- a/hw/block/vhost-user-blk.c +++ b/hw/block/vhost-user-blk.c @@ -91,11 +91,13 @@ static int vhost_user_blk_handle_config_change(struct v= host_dev *dev) int ret; struct virtio_blk_config blkcfg; VHostUserBlk *s =3D VHOST_USER_BLK(dev->vdev); + Error *local_err =3D NULL; =20 ret =3D vhost_dev_get_config(dev, (uint8_t *)&blkcfg, - sizeof(struct virtio_blk_config)); + sizeof(struct virtio_blk_config), + &local_err); if (ret < 0) { - error_report("get config space failed"); + error_report_err(local_err); return -1; } =20 @@ -478,9 +480,8 @@ static void vhost_user_blk_device_realize(DeviceState *= dev, Error **errp) assert(s->connected); =20 ret =3D vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, - sizeof(struct virtio_blk_config)); + sizeof(struct virtio_blk_config), errp); if (ret < 0) { - error_setg(errp, "vhost-user-blk: get block config failed"); goto vhost_err; } =20 diff --git a/hw/display/vhost-user-gpu.c b/hw/display/vhost-user-gpu.c index 6cdaa1c73b..389199e6ca 100644 --- a/hw/display/vhost-user-gpu.c +++ b/hw/display/vhost-user-gpu.c @@ -415,14 +415,16 @@ vhost_user_gpu_get_config(VirtIODevice *vdev, uint8_t= *config_data) VirtIOGPUBase *b =3D VIRTIO_GPU_BASE(vdev); struct virtio_gpu_config *vgconfig =3D (struct virtio_gpu_config *)config_data; + Error *local_err =3D NULL; int ret; =20 memset(config_data, 0, sizeof(struct virtio_gpu_config)); =20 ret =3D vhost_dev_get_config(&g->vhost->dev, - config_data, sizeof(struct virtio_gpu_confi= g)); + config_data, sizeof(struct virtio_gpu_confi= g), + &local_err); if (ret) { - error_report("vhost-user-gpu: get device config space failed"); + error_report_err(local_err); return; } =20 diff --git a/hw/input/vhost-user-input.c b/hw/input/vhost-user-input.c index 63984a8ba7..273e96a7b1 100644 --- a/hw/input/vhost-user-input.c +++ b/hw/input/vhost-user-input.c @@ -49,13 +49,15 @@ static void vhost_input_get_config(VirtIODevice *vdev, = uint8_t *config_data) { VirtIOInput *vinput =3D VIRTIO_INPUT(vdev); VHostUserInput *vhi =3D VHOST_USER_INPUT(vdev); + Error *local_err =3D NULL; int ret; =20 memset(config_data, 0, vinput->cfg_size); =20 - ret =3D vhost_dev_get_config(&vhi->vhost->dev, config_data, vinput->cf= g_size); + ret =3D vhost_dev_get_config(&vhi->vhost->dev, config_data, vinput->cf= g_size, + &local_err); if (ret) { - error_report("vhost-user-input: get device config space failed"); + error_report_err(local_err); return; } } diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index 447b119f85..10a7780a13 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -117,7 +117,7 @@ uint64_t vhost_net_get_features(struct vhost_net *net, = uint64_t features) int vhost_net_get_config(struct vhost_net *net, uint8_t *config, uint32_t config_len) { - return vhost_dev_get_config(&net->dev, config, config_len); + return vhost_dev_get_config(&net->dev, config, config_len, NULL); } int vhost_net_set_config(struct vhost_net *net, const uint8_t *data, uint32_t offset, uint32_t size, uint32_t flags) diff --git a/hw/virtio/vhost-user-vsock.c b/hw/virtio/vhost-user-vsock.c index b6a4a25ea1..6095ed7349 100644 --- a/hw/virtio/vhost-user-vsock.c +++ b/hw/virtio/vhost-user-vsock.c @@ -34,10 +34,12 @@ static void vuv_get_config(VirtIODevice *vdev, uint8_t = *config) static int vuv_handle_config_change(struct vhost_dev *dev) { VHostUserVSock *vsock =3D VHOST_USER_VSOCK(dev->vdev); + Error *local_err =3D NULL; int ret =3D vhost_dev_get_config(dev, (uint8_t *)&vsock->vsockcfg, - sizeof(struct virtio_vsock_config)); + sizeof(struct virtio_vsock_config), + &local_err); if (ret < 0) { - error_report("get config space failed"); + error_report_err(local_err); return -1; } =20 @@ -114,9 +116,8 @@ static void vuv_device_realize(DeviceState *dev, Error = **errp) } =20 ret =3D vhost_dev_get_config(&vvc->vhost_dev, (uint8_t *)&vsock->vsock= cfg, - sizeof(struct virtio_vsock_config)); + sizeof(struct virtio_vsock_config), errp); if (ret < 0) { - error_setg_errno(errp, -ret, "get config space failed"); goto err_vhost_dev; } =20 diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 889559d86a..1ac4a2ebec 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -2117,7 +2117,7 @@ static void vhost_user_set_iotlb_callback(struct vhos= t_dev *dev, int enabled) } =20 static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config, - uint32_t config_len) + uint32_t config_len, Error **errp) { VhostUserMsg msg =3D { .hdr.request =3D VHOST_USER_GET_CONFIG, @@ -2127,32 +2127,32 @@ static int vhost_user_get_config(struct vhost_dev *= dev, uint8_t *config, =20 if (!virtio_has_feature(dev->protocol_features, VHOST_USER_PROTOCOL_F_CONFIG)) { - return -1; + error_setg(errp, "VHOST_USER_PROTOCOL_F_CONFIG not supported"); + return -EINVAL; } =20 - if (config_len > VHOST_USER_MAX_CONFIG_SIZE) { - return -1; - } + assert(config_len <=3D VHOST_USER_MAX_CONFIG_SIZE); =20 msg.payload.config.offset =3D 0; msg.payload.config.size =3D config_len; if (vhost_user_write(dev, &msg, NULL, 0) < 0) { - return -1; + return -EPROTO; } =20 if (vhost_user_read(dev, &msg) < 0) { - return -1; + return -EPROTO; } =20 if (msg.hdr.request !=3D VHOST_USER_GET_CONFIG) { - error_report("Received unexpected msg type. Expected %d received %= d", - VHOST_USER_GET_CONFIG, msg.hdr.request); - return -1; + error_setg(errp, + "Received unexpected msg type. Expected %d received %d", + VHOST_USER_GET_CONFIG, msg.hdr.request); + return -EINVAL; } =20 if (msg.hdr.size !=3D VHOST_USER_CONFIG_HDR_SIZE + config_len) { - error_report("Received bad msg size."); - return -1; + error_setg(errp, "Received bad msg size."); + return -EINVAL; } =20 memcpy(config, msg.payload.config.region, config_len); diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 0f469f1823..4fa414feea 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -523,7 +523,7 @@ static int vhost_vdpa_set_config(struct vhost_dev *dev,= const uint8_t *data, } =20 static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config, - uint32_t config_len) + uint32_t config_len, Error **errp) { struct vhost_vdpa_config *v_config; unsigned long config_size =3D offsetof(struct vhost_vdpa_config, buf); diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index c7f9d8bb06..e8f85a5d2d 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1562,15 +1562,23 @@ void vhost_ack_features(struct vhost_dev *hdev, con= st int *feature_bits, } =20 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, - uint32_t config_len) + uint32_t config_len, Error **errp) { + ERRP_GUARD(); + int ret; + assert(hdev->vhost_ops); =20 if (hdev->vhost_ops->vhost_get_config) { - return hdev->vhost_ops->vhost_get_config(hdev, config, config_len); + ret =3D hdev->vhost_ops->vhost_get_config(hdev, config, config_len= , errp); + if (ret < 0 && !*errp) { + error_setg_errno(errp, -ret, "vhost_get_config failed"); + } + return ret; } =20 - return -1; + error_setg(errp, "vhost_get_config not implemented"); + return -ENOTSUP; } =20 int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data, --=20 2.31.1