From nobody Tue May 21 17:48:38 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1690282033; cv=none; d=zohomail.com; s=zohoarc; b=iEOQO+Ld4pUBmGHgWPQ1TQ7LCgy0GP+0eMLNGPuKqGhKzNVX2qHjJgM1xZoWWZxcaZG5DA/P7Dhv82cdmoJCvGVZr7873uMQIQhkSlI9+81f17A941bRUnelkvAJ7ybu0iWFTE36pioe+4P823kwO70+LNMljQ1YQYLVt5msujw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690282033; h=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=qlztDq/CKb91JnaGmc1JrV/AEfrFJEe1DDcUZ8Ogc1M=; b=H3IBDeRXTngX91JsQggaVEvws+Cp8rT/zxr9nmhHNleiT1/KP2rETTKBwJ9e3qmfXV1Dpd4tGfHw7NTTu3Hp+SsOsdl7OC8nwvGdn4Jn3+l+qA1+UbgTe3i0Dnpj0UvG527oPW0xjXahGBNoio/GleZhJ4uFcNPdFDr+L8xpeuk= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1690282033953188.16701726196504; Tue, 25 Jul 2023 03:47:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOFXj-0008Bg-Oj; Tue, 25 Jul 2023 06:45:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOFXg-00087S-Ew for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:24 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qOFXd-00035C-4Z for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:24 -0400 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-686b643df5dso53399b3a.1 for ; Tue, 25 Jul 2023 03:43:20 -0700 (PDT) Received: from localhost.localdomain.gitgo.cc ([47.75.78.161]) by smtp.gmail.com with ESMTPSA id c20-20020aa78c14000000b0062cf75a9e6bsm9343165pfd.131.2023.07.25.03.43.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 03:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20221208.gappssmtp.com; s=20221208; t=1690281800; x=1690886600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qlztDq/CKb91JnaGmc1JrV/AEfrFJEe1DDcUZ8Ogc1M=; b=4SUZaYatOJt3CpRee7EdKVQiuJic1nfITdBVb2XmYuFgiz0+di+Mg0xcSg8WjJoFGy g1JUGVwi3jPCJDWV3YOjW87vCVqqpVgqI2lJJ2a868/Zu/sOrNZDldcJH9qrnPzomL70 mtMw78KxWRq1igsVdsXjjnvO/FDg6YUnPfsnCxSf4wuVceDWXZkp661ziVGaGA0dERoK irYRfvt0Vm12tmTBAjiruO3goul2F3qwWKO+Hc8Dg1GbrHbFU9uFPH8gGWxFMS8SMoQu p5wx0wBuRi4AEn3EGhOFlRWdnLHDutCo+8mULyYBy/TxD0shZpTl2LY3wcvYMUQvvVNy Ps7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690281800; x=1690886600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qlztDq/CKb91JnaGmc1JrV/AEfrFJEe1DDcUZ8Ogc1M=; b=iA+UJRW1F7CY4yd6hLRy8Qyco/T80gizONoi6pqZ/7QoECo4OPaxM/qqDTi2TyTez8 EYaBfGJSiZqpWitS8EUU5y/pSt5b7r69w4h5qIVzAPJTjSPLsdm5fG2s45MyEpd8AMXL uFbR9pNkANMA2me2QkRHoimGpZlnjldGidAUG4Mx48ZzVOlyAKoh+R6ZaSYMXK02LgMi ZEVR96oYiU9wN4MgUrBIP5J8t9NyIYr3BgkRuxeEOvG6rV5T73tiXhyocPPbIbj+pB9S C7mN+f1781o1AmaSN0JrRy7USNgxwYfC0T7pXTi+sCwm6jNRKoPoESErJrkx1Wh4qqUO QtvQ== X-Gm-Message-State: ABy/qLZQYAlLYSorRhfAJb77Q0uT+tQJqLT1qdpjX35aPpg/2Aasjyg8 UZP8Al9bZtqf2w+QA62IgKC/4t/grbtqz2KuCqHLdaAqTi4= X-Google-Smtp-Source: APBJJlGS+e19bhiDhSICt2X4ntmLuPrtSZcCk8Tc+HPY+hGXJqYL53dGkyhorwmnL8lek4YqdxyzRg== X-Received: by 2002:a05:6a21:66c4:b0:13b:79dc:4538 with SMTP id ze4-20020a056a2166c400b0013b79dc4538mr10284pzb.62.1690281799665; Tue, 25 Jul 2023 03:43:19 -0700 (PDT) From: Li Feng To: "Michael S. Tsirkin" , Raphael Norwitz , Kevin Wolf , Hanna Reitz , Paolo Bonzini , Fam Zheng , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Viresh Kumar , qemu-block@nongnu.org (open list:Block layer core), qemu-devel@nongnu.org (open list:All patches CC here) Cc: Li Feng Subject: [PATCH v2 1/4] vhost: fix the fd leak Date: Tue, 25 Jul 2023 18:42:44 +0800 Message-ID: <20230725104256.4861-2-fengli@smartx.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725104256.4861-1-fengli@smartx.com> References: <20230721105205.1714449-1-fengli@smartx.com> <20230725104256.4861-1-fengli@smartx.com> MIME-Version: 1.0 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: none client-ip=2607:f8b0:4864:20::432; envelope-from=fengli@smartx.com; helo=mail-pf1-x432.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @smartx-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1690282035993100003 Content-Type: text/plain; charset="utf-8" When the vhost-user reconnect to the backend, the notifer should be cleanup. Otherwise, the fd resource will be exhausted. Fixes: f9a09ca3ea ("vhost: add support for configure interrupt") Signed-off-by: Li Feng Reviewed-by: Raphael Norwitz --- hw/virtio/vhost.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index abf0d03c8d..e2f6ffb446 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -2044,6 +2044,8 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODev= ice *vdev, bool vrings) event_notifier_test_and_clear( &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier); event_notifier_test_and_clear(&vdev->config_notifier); + event_notifier_cleanup( + &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier); =20 trace_vhost_dev_stop(hdev, vdev->name, vrings); =20 --=20 2.41.0 From nobody Tue May 21 17:48:38 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1690281967; cv=none; d=zohomail.com; s=zohoarc; b=BxWjnpigzZyyvRiMgTRpd0Yi5FCoEwBuV0s23llbA4qfDpQPOZQEyF+mgGZDm0RWw6YMifviiQeeZKUPqM2HPv/7KC2uIvWXlGGH4ljhpnZgtfkXzB6Qr1H6tWhKA70oHNviXK0Vi1R1+MHwARSh1mS2pN+PYqYD6DPAQFe2Ixw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690281967; h=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=M1cA9e5yzf2hNCWgBUmfES7GtFkzi9v/WbQPtniu6Bs=; b=eHj4PrVgXUp7jK0igerJnfXsHPdaaftsljVusIpnUOle8pSTXzlv2FIZJlOUchfJmfWBwM8/E34QQRLx0HZ2iV4eXvcJenm5H2ObwajQ/I7PKU3SzuH1reVIXKd60jES24RlbEJq7iksgQ0S1X1/z8drtg6x/P0VHZCGRDn2UC0= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1690281967837658.2243910004324; Tue, 25 Jul 2023 03:46:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOFXk-0008By-18; Tue, 25 Jul 2023 06:45:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOFXi-00089f-5i for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:26 -0400 Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qOFXg-00035U-KB for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:25 -0400 Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-34896a1574dso30334435ab.0 for ; Tue, 25 Jul 2023 03:43:24 -0700 (PDT) Received: from localhost.localdomain.gitgo.cc ([47.75.78.161]) by smtp.gmail.com with ESMTPSA id c20-20020aa78c14000000b0062cf75a9e6bsm9343165pfd.131.2023.07.25.03.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 03:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20221208.gappssmtp.com; s=20221208; t=1690281803; x=1690886603; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M1cA9e5yzf2hNCWgBUmfES7GtFkzi9v/WbQPtniu6Bs=; b=Jt8PLWhMFaU9PfcHIz3m78nzNlS0NVISaRN6slY/kAfDySBnZRb+Xq3HjM+JEKVIa7 wut81s25vRaTtib9z7Be3rw/3KuSoSzA2PH3T4eJZmGMftYnpJap2Znv9FdSlf+3yWZQ tJWKxQJVT6JxgCHaopdjz5JC0TnQTqT17Zd2m7D1Poot95+0BbnKOMHeaDRAE6017vG4 Tr3LsBg2L65gg19YLPcuKxLiUI1PRFROqRQBTBZmmqKbijjqKGCNzntAKc1N3ZnUCdKb PTkeQsqEZNT6vwciMrOiLfsHoVXcm+3QPxRNFsFaeg95/0IrEewbA7D9gwa0FSQRylmA 8o3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690281803; x=1690886603; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M1cA9e5yzf2hNCWgBUmfES7GtFkzi9v/WbQPtniu6Bs=; b=DBW20toXYtFMnrOgT4UF84T70AtkBC76OWQ0AGXwlQNTc5AgdSjpW5tOYCcR73EE6z 8VJvAoSMgbF9xGiURqv6ljGjnFGXQveWcfXQzsdD3CGOhBDpXRzsaCe8OO2s2sj+IVZn YY2Vt7PCJZetNZZPnQwgnLAfi1Q14DO6jjmxKMRql8YpbuVA2TGCsSRdRxR+31dsxTLi /tg/YeoQMffTrVMx/F5I5kxYJDeY6C7boxZjhoxwqzUGxBqKvrZ25HsAkHrWQveh6jjs opEaxFnPJfdfGRRoIrGHJo0nBXc8FwyK5zoC8IBjQ2EeJXUn6HKvtp23HN0I05a2Hgw2 NuxA== X-Gm-Message-State: ABy/qLZmUt2b2SgZFqs/5Snvo2SAbXBoB9WUD47gO2Yt0fjjVNN46Vuw of9/UiiN1s0EJ6rOUgfJRRMV2A== X-Google-Smtp-Source: APBJJlECERw41/DD91Jx5irI1BIUW7VDWwh/Uenlyc5Duq422MI9bs/sZwPJoLPbUrkUaCkpXMd/kA== X-Received: by 2002:a92:c711:0:b0:343:c8b1:b7f0 with SMTP id a17-20020a92c711000000b00343c8b1b7f0mr2420127ilp.23.1690281803196; Tue, 25 Jul 2023 03:43:23 -0700 (PDT) From: Li Feng To: "Michael S. Tsirkin" , Raphael Norwitz , Kevin Wolf , Hanna Reitz , Paolo Bonzini , Fam Zheng , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Viresh Kumar , qemu-block@nongnu.org (open list:Block layer core), qemu-devel@nongnu.org (open list:All patches CC here) Cc: Li Feng Subject: [PATCH v2 2/4] vhost-user-common: send get_inflight_fd once Date: Tue, 25 Jul 2023 18:42:45 +0800 Message-ID: <20230725104256.4861-3-fengli@smartx.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725104256.4861-1-fengli@smartx.com> References: <20230721105205.1714449-1-fengli@smartx.com> <20230725104256.4861-1-fengli@smartx.com> MIME-Version: 1.0 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: none client-ip=2607:f8b0:4864:20::131; envelope-from=fengli@smartx.com; helo=mail-il1-x131.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @smartx-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1690281969313100001 Content-Type: text/plain; charset="utf-8" Get_inflight_fd is sent only once. When reconnecting to the backend, qemu sent set_inflight_fd to the backend. Signed-off-by: Li Feng --- hw/scsi/vhost-scsi-common.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/hw/scsi/vhost-scsi-common.c b/hw/scsi/vhost-scsi-common.c index a06f01af26..664adb15b4 100644 --- a/hw/scsi/vhost-scsi-common.c +++ b/hw/scsi/vhost-scsi-common.c @@ -52,20 +52,28 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) =20 vsc->dev.acked_features =3D vdev->guest_features; =20 - assert(vsc->inflight =3D=3D NULL); - vsc->inflight =3D g_new0(struct vhost_inflight, 1); - ret =3D vhost_dev_get_inflight(&vsc->dev, - vs->conf.virtqueue_size, - vsc->inflight); + ret =3D vhost_dev_prepare_inflight(&vsc->dev, vdev); if (ret < 0) { - error_report("Error get inflight: %d", -ret); + error_report("Error setting inflight format: %d", -ret); goto err_guest_notifiers; } =20 - ret =3D vhost_dev_set_inflight(&vsc->dev, vsc->inflight); - if (ret < 0) { - error_report("Error set inflight: %d", -ret); - goto err_guest_notifiers; + if (vsc->inflight) { + if (!vsc->inflight->addr) { + ret =3D vhost_dev_get_inflight(&vsc->dev, + vs->conf.virtqueue_size, + vsc->inflight); + if (ret < 0) { + error_report("Error get inflight: %d", -ret); + goto err_guest_notifiers; + } + } + + ret =3D vhost_dev_set_inflight(&vsc->dev, vsc->inflight); + if (ret < 0) { + error_report("Error set inflight: %d", -ret); + goto err_guest_notifiers; + } } =20 ret =3D vhost_dev_start(&vsc->dev, vdev, true); @@ -85,9 +93,6 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) return ret; =20 err_guest_notifiers: - g_free(vsc->inflight); - vsc->inflight =3D NULL; - k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); err_host_notifiers: vhost_dev_disable_notifiers(&vsc->dev, vdev); @@ -111,12 +116,6 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc) } assert(ret >=3D 0); =20 - if (vsc->inflight) { - vhost_dev_free_inflight(vsc->inflight); - g_free(vsc->inflight); - vsc->inflight =3D NULL; - } - vhost_dev_disable_notifiers(&vsc->dev, vdev); } =20 --=20 2.41.0 From nobody Tue May 21 17:48:38 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1690282037; cv=none; d=zohomail.com; s=zohoarc; b=J+WmWWCSwgvUz4+G2hxo9OBqrujy2AGJ8nDcAGQ1EzH+zkmcSo3mKxjVco/z9eBPmLhsjHKpbWPVWeGiPNpsPUkJJUABGK3YaQKHWmLSjsbqnuKY9wvVTZ32yyQYBzQ6jU31Ez7OH2vDV9QSgA9S8k01YdBv8Sx0IqxGr6iGLLg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690282037; h=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=GOPvppBtU3tERm03xm7z5N4AAn0DHu0QgP1daqx/8gs=; b=aEzWQusTJI+FdyI8F/Yfelvl80jeena1G7YJc9s8saTou0b+DuDa7sVY44Dy0juSn54j/IirWaAYTHehgsVxCkSrCam0G1nFuBfR4mh+ZXf3Yu0/5kGwtone0PbLdqKLbourbTZKlyqHaQ76dy29rLRSPTfo/z8EmDWvfPxaGq4= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1690282037275368.7726420632182; Tue, 25 Jul 2023 03:47:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOFXn-0008D8-1I; Tue, 25 Jul 2023 06:45:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOFXl-0008Ck-Tg for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:29 -0400 Received: from mail-oa1-x32.google.com ([2001:4860:4864:20::32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qOFXk-000372-DY for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:29 -0400 Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-1ba5cda3530so4129902fac.3 for ; Tue, 25 Jul 2023 03:43:28 -0700 (PDT) Received: from localhost.localdomain.gitgo.cc ([47.75.78.161]) by smtp.gmail.com with ESMTPSA id c20-20020aa78c14000000b0062cf75a9e6bsm9343165pfd.131.2023.07.25.03.43.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 03:43:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20221208.gappssmtp.com; s=20221208; t=1690281807; x=1690886607; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GOPvppBtU3tERm03xm7z5N4AAn0DHu0QgP1daqx/8gs=; b=AX93BukBnGkW0S8rcOm55Ve4Oz5kAlu0QhlSQShzfus7rMY5V8/D2cxDejCD/j6+/q Q/s8cSwVlimaPoea7dVCaw/jUvE2xLj800bju5H0tf8vbauN4bvAHz17J9p/5HN0XlFj YUcD/ZJ7ZUroGQBz0wM3SwMThDy69O+mvtC6ncj/Tdw2RHXgIMOd6WASKxhzhihCu6an dYxnIup4qd6iX0U74CqZnTmI9oOlfUkPPBpyUKWcXm+YsHLXp0V5DzJAJjVLcfS0hWZ6 BYYjZc4MRzDLM1j/MYawajSSh7/XkeAwxAID7gj/jXwPz437hNBuc80WaPhQoGl3K7Rd sIFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690281807; x=1690886607; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GOPvppBtU3tERm03xm7z5N4AAn0DHu0QgP1daqx/8gs=; b=jQPkWncrmQQAvYoRX4NHSHeFZNC3NAFfHKn4vm9PvNjRMOw4r+gFcLjSwJUbbBc1+o 11ViVbyLkTcxtQNEKuPWViUJBB2C03ffAr5cvw0EEw3PpqZyClwnfTGngbow2QqoHQkM hOR0LH/6tK4HK+V/oz3ow5pq0vg7DGMR+2epgH8W9oWMYuyIIzub9hHX8vYEbn1OhJlo wHmrT052E5kdi+bYfn3d1HzLy3sr2ceKqlbRTq+euphppARVn3ftS5+bvyxYlT1mVck/ WXgoknSWA9Ujr26w9rIGZCKnQ1mnXeo/XR9BSoh4aAerRHQiZ+IrOPVvi+mQtOq+2g/a qrAg== X-Gm-Message-State: ABy/qLagV7z47SKYXqpI8fJrKuUzS7Ty+E029fxYJSVAnGtCGbuW2i9B Nc6b0nM+2JbiNxBw1lrkpNwCTw== X-Google-Smtp-Source: APBJJlHSq2uBiTm1xQ8MM+BSknk+Ufd69TSpreM89ve71Rzj0TNO3ZpNcBZSEunQeHUE8seKBzIosQ== X-Received: by 2002:a05:6870:9125:b0:1b0:408a:1d14 with SMTP id o37-20020a056870912500b001b0408a1d14mr14525327oae.44.1690281806710; Tue, 25 Jul 2023 03:43:26 -0700 (PDT) From: Li Feng To: Raphael Norwitz , "Michael S. Tsirkin" , Kevin Wolf , Hanna Reitz , Paolo Bonzini , Fam Zheng , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Viresh Kumar , qemu-block@nongnu.org (open list:Block layer core), qemu-devel@nongnu.org (open list:All patches CC here) Cc: Li Feng Subject: [PATCH v2 3/4] vhost: move and rename the conn retry times Date: Tue, 25 Jul 2023 18:42:46 +0800 Message-ID: <20230725104256.4861-4-fengli@smartx.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725104256.4861-1-fengli@smartx.com> References: <20230721105205.1714449-1-fengli@smartx.com> <20230725104256.4861-1-fengli@smartx.com> MIME-Version: 1.0 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: none client-ip=2001:4860:4864:20::32; envelope-from=fengli@smartx.com; helo=mail-oa1-x32.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @smartx-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1690282039287100003 Content-Type: text/plain; charset="utf-8" Multile devices need this macro, move it to a common header. Signed-off-by: Li Feng Reviewed-by: Raphael Norwitz --- hw/block/vhost-user-blk.c | 4 +--- hw/virtio/vhost-user-gpio.c | 3 +-- include/hw/virtio/vhost.h | 2 ++ 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c index eecf3f7a81..3c69fa47d5 100644 --- a/hw/block/vhost-user-blk.c +++ b/hw/block/vhost-user-blk.c @@ -32,8 +32,6 @@ #include "sysemu/sysemu.h" #include "sysemu/runstate.h" =20 -#define REALIZE_CONNECTION_RETRIES 3 - static const int user_feature_bits[] =3D { VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_SEG_MAX, @@ -482,7 +480,7 @@ static void vhost_user_blk_device_realize(DeviceState *= dev, Error **errp) s->inflight =3D g_new0(struct vhost_inflight, 1); s->vhost_vqs =3D g_new0(struct vhost_virtqueue, s->num_queues); =20 - retries =3D REALIZE_CONNECTION_RETRIES; + retries =3D VU_REALIZE_CONN_RETRIES; assert(!*errp); do { if (*errp) { diff --git a/hw/virtio/vhost-user-gpio.c b/hw/virtio/vhost-user-gpio.c index 3b013f2d0f..d9979aa5db 100644 --- a/hw/virtio/vhost-user-gpio.c +++ b/hw/virtio/vhost-user-gpio.c @@ -15,7 +15,6 @@ #include "standard-headers/linux/virtio_ids.h" #include "trace.h" =20 -#define REALIZE_CONNECTION_RETRIES 3 #define VHOST_NVQS 2 =20 /* Features required from VirtIO */ @@ -359,7 +358,7 @@ static void vu_gpio_device_realize(DeviceState *dev, Er= ror **errp) qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, vu_gpio_event, NU= LL, dev, NULL, true); =20 - retries =3D REALIZE_CONNECTION_RETRIES; + retries =3D VU_REALIZE_CONN_RETRIES; g_assert(!*errp); do { if (*errp) { diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 6a173cb9fa..ca3131b1af 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -8,6 +8,8 @@ #define VHOST_F_DEVICE_IOTLB 63 #define VHOST_USER_F_PROTOCOL_FEATURES 30 =20 +#define VU_REALIZE_CONN_RETRIES 3 + /* Generic structures common for any vhost based device. */ =20 struct vhost_inflight { --=20 2.41.0 From nobody Tue May 21 17:48:38 2024 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 ARC-Seal: i=1; a=rsa-sha256; t=1690281983; cv=none; d=zohomail.com; s=zohoarc; b=BLzAX6i+eBCfaiovuxwZ83GYRmcZmPjcT1PEfooMBlH552usDhySCXhVQ+6JhjdnH+oS+5QWXmd3Nc62Bd1eAEsd64fCvQXsij585x7X6ng6fpag8Dc7dlxNfGbzzHzsLdz9AQKUSLG660X5YbuU9UVPuLR6O91paAgmFhKuOgE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690281983; h=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=VOUIT8kGELsnE/AoIXIltAsJSkBIdaBBGe0R8el5eWY=; b=b82PsLDYPlsx3tKBJcL2BatmsLh+hob1GTZQgBzSLNJrOM6/GPEqGAs2GCFzTWdbh05AIiJMFgxLD9emhso50MOtTg1/hYGnDmjZosK33kcOHUMbVafONPuaP3v9QvMQtdDsquxZwYsdo83An/JG3oJHpzPGrsLxk3H4C1u+eNU= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1690281982981191.16363103916547; Tue, 25 Jul 2023 03:46:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOFXr-0008EC-64; Tue, 25 Jul 2023 06:45:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOFXp-0008Dt-VL for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:33 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qOFXn-00037J-Q8 for qemu-devel@nongnu.org; Tue, 25 Jul 2023 06:45:33 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6862842a028so2981032b3a.0 for ; Tue, 25 Jul 2023 03:43:31 -0700 (PDT) Received: from localhost.localdomain.gitgo.cc ([47.75.78.161]) by smtp.gmail.com with ESMTPSA id c20-20020aa78c14000000b0062cf75a9e6bsm9343165pfd.131.2023.07.25.03.43.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 03:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20221208.gappssmtp.com; s=20221208; t=1690281810; x=1690886610; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VOUIT8kGELsnE/AoIXIltAsJSkBIdaBBGe0R8el5eWY=; b=UVsunI7IukB2yvUPsH1Vkr8Ej2h63JJZAS4Qo7sV/Xy8secs/D7/i2BTG3mNYvye7f Ff971PbuwMQspryrm4swJ1Ku39zJEFoxT40dlpQR+NjF/wqAT18KZdL4PK+P+r2li4Xw hZ64NBkW5btuW0+ZH5+RN2BV4HAK69lCptWFqTwf7Yl33kURb60oxGAK/msSiSi+zQXj /KREa8XKphPj9CzxySP3u+6IFPm6MrIVkXFumX/lush3I00LAFAdLvK9hfR3aPcEYcAK 9jAvLfaQxc2wYnLGMhz0p4o8SbBLyfI/jUneAhto6M1I9Wm2B4R2lWeNOgLibiRai4Zd Sdxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690281810; x=1690886610; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VOUIT8kGELsnE/AoIXIltAsJSkBIdaBBGe0R8el5eWY=; b=M7LkNtndmJ9DJwQJqlI0oXTLD/b8wcCskAe4SqRpqiaL9kulupH4MN8y+4Id0UCraz XgpGRV2XzsYeE3vXHfa82+CwKKybD1ryctCVasPBBaSPrfvhlyZOUDq1LKowyU5S2VJl EKoiaa71/pkKNahA0hMBoyhuk4Fnp2is53nvpEDnRrXYBRwjJeE/+T7TzyAqPPxMXiR7 JqdQONKYtT4OzaJXljfigNTW3KvWVa68020qD9jzXN/grEPR/BC8yfv8WmOHR1VQbJng 53X5CMehngwEaec07ldqYmN+th6YAAPmJOmVkAIlf1ybFxcYUNaBXVNo9VMRCCB1uAIz 6R5Q== X-Gm-Message-State: ABy/qLZnQ0oxsaXUX0ARtRknCTjmz1dR2kNQfL0tYJ9lJiKHiMIvuqWp QAr9ges7ehybfIDxhuVkJ9MoVQ== X-Google-Smtp-Source: APBJJlG/WsVOhCa922xgJgr2F3mr+2Llbib3Yh9PVKkz756jGMDsFHqVEDJGmvJPjFX950U6nicriQ== X-Received: by 2002:a05:6a20:7d87:b0:10f:f672:6e88 with SMTP id v7-20020a056a207d8700b0010ff6726e88mr13708080pzj.4.1690281810191; Tue, 25 Jul 2023 03:43:30 -0700 (PDT) From: Li Feng To: Raphael Norwitz , "Michael S. Tsirkin" , Kevin Wolf , Hanna Reitz , Paolo Bonzini , Fam Zheng , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Viresh Kumar , qemu-block@nongnu.org (open list:Block layer core), qemu-devel@nongnu.org (open list:All patches CC here) Cc: Li Feng Subject: [PATCH v2 4/4] vhost-user-scsi: support reconnect to backend Date: Tue, 25 Jul 2023 18:42:47 +0800 Message-ID: <20230725104256.4861-5-fengli@smartx.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725104256.4861-1-fengli@smartx.com> References: <20230721105205.1714449-1-fengli@smartx.com> <20230725104256.4861-1-fengli@smartx.com> MIME-Version: 1.0 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: none client-ip=2607:f8b0:4864:20::430; envelope-from=fengli@smartx.com; helo=mail-pf1-x430.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @smartx-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1690281983387100001 Content-Type: text/plain; charset="utf-8" If the backend crashes and restarts, the device is broken. This patch adds reconnect for vhost-user-scsi. Tested with spdk backend. Signed-off-by: Li Feng --- hw/scsi/vhost-scsi-common.c | 6 + hw/scsi/vhost-user-scsi.c | 220 +++++++++++++++++++++++--- include/hw/virtio/vhost-scsi-common.h | 3 + include/hw/virtio/vhost-user-scsi.h | 3 + 4 files changed, 211 insertions(+), 21 deletions(-) diff --git a/hw/scsi/vhost-scsi-common.c b/hw/scsi/vhost-scsi-common.c index 664adb15b4..3fde477eee 100644 --- a/hw/scsi/vhost-scsi-common.c +++ b/hw/scsi/vhost-scsi-common.c @@ -81,6 +81,7 @@ int vhost_scsi_common_start(VHostSCSICommon *vsc) error_report("Error start vhost dev"); goto err_guest_notifiers; } + vsc->started_vu =3D true; =20 /* guest_notifier_mask/pending not used yet, so just unmask * everything here. virtio-pci will do the right thing by @@ -106,6 +107,11 @@ void vhost_scsi_common_stop(VHostSCSICommon *vsc) VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret =3D 0; =20 + if (!vsc->started_vu) { + return; + } + vsc->started_vu =3D false; + vhost_dev_stop(&vsc->dev, vdev, true); =20 if (k->set_guest_notifiers) { diff --git a/hw/scsi/vhost-user-scsi.c b/hw/scsi/vhost-user-scsi.c index ee99b19e7a..bd32dcf999 100644 --- a/hw/scsi/vhost-user-scsi.c +++ b/hw/scsi/vhost-user-scsi.c @@ -46,20 +46,25 @@ enum VhostUserProtocolFeature { static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) { VHostUserSCSI *s =3D (VHostUserSCSI *)vdev; + DeviceState *dev =3D &s->parent_obj.parent_obj.parent_obj.parent_obj; VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); - bool start =3D (status & VIRTIO_CONFIG_S_DRIVER_OK) && vdev->vm_runnin= g; + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); + bool should_start =3D virtio_device_should_start(vdev, status); + int ret; =20 - if (vhost_dev_is_started(&vsc->dev) =3D=3D start) { + if (!s->connected) { return; } =20 - if (start) { - int ret; + if (vhost_dev_is_started(&vsc->dev) =3D=3D should_start) { + return; + } =20 + if (should_start) { ret =3D vhost_scsi_common_start(vsc); if (ret < 0) { error_report("unable to start vhost-user-scsi: %s", strerror(-= ret)); - exit(1); + qemu_chr_fe_disconnect(&vs->conf.chardev); } } else { vhost_scsi_common_stop(vsc); @@ -85,8 +90,160 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev) } } =20 -static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) +static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *v= q) { + VHostUserSCSI *s =3D (VHostUserSCSI *)vdev; + DeviceState *dev =3D &s->parent_obj.parent_obj.parent_obj.parent_obj; + VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); + + Error *local_err =3D NULL; + int i, ret; + + if (!vdev->start_on_kick) { + return; + } + + if (!s->connected) { + return; + } + + if (vhost_dev_is_started(&vsc->dev)) { + return; + } + + /* + * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start + * vhost here instead of waiting for .set_status(). + */ + ret =3D vhost_scsi_common_start(vsc); + if (ret < 0) { + error_reportf_err(local_err, "vhost-user-blk: vhost start failed: = "); + qemu_chr_fe_disconnect(&vs->conf.chardev); + return; + } + + /* Kick right away to begin processing requests already in vring */ + for (i =3D 0; i < vsc->dev.nvqs; i++) { + VirtQueue *kick_vq =3D virtio_get_queue(vdev, i); + + if (!virtio_queue_get_desc_addr(vdev, i)) { + continue; + } + event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); + } +} + +static int vhost_user_scsi_connect(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostUserSCSI *s =3D VHOST_USER_SCSI(vdev); + VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); + int ret =3D 0; + + if (s->connected) { + return 0; + } + s->connected =3D true; + + vsc->dev.num_queues =3D vs->conf.num_queues; + vsc->dev.nvqs =3D VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; + vsc->dev.vqs =3D s->vhost_vqs; + vsc->dev.vq_index =3D 0; + vsc->dev.backend_features =3D 0; + + ret =3D vhost_dev_init(&vsc->dev, &s->vhost_user, VHOST_BACKEND_TYPE_U= SER, 0, + errp); + if (ret < 0) { + return ret; + } + + /* restore vhost state */ + if (virtio_device_started(vdev, vdev->status)) { + ret =3D vhost_scsi_common_start(vsc); + if (ret < 0) { + return ret; + } + } + + return 0; +} + +static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event); + +static void vhost_user_scsi_disconnect(DeviceState *dev) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostUserSCSI *s =3D VHOST_USER_SCSI(vdev); + VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); + + if (!s->connected) { + return; + } + s->connected =3D false; + + vhost_scsi_common_stop(vsc); + + vhost_dev_cleanup(&vsc->dev); + + /* Re-instate the event handler for new connections */ + qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, + vhost_user_scsi_event, NULL, dev, NULL, true); +} + +static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event) +{ + DeviceState *dev =3D opaque; + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostUserSCSI *s =3D VHOST_USER_SCSI(vdev); + VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); + Error *local_err =3D NULL; + + switch (event) { + case CHR_EVENT_OPENED: + if (vhost_user_scsi_connect(dev, &local_err) < 0) { + error_report_err(local_err); + qemu_chr_fe_disconnect(&vs->conf.chardev); + return; + } + break; + case CHR_EVENT_CLOSED: + /* defer close until later to avoid circular close */ + vhost_user_async_close(dev, &vs->conf.chardev, &vsc->dev, + vhost_user_scsi_disconnect); + break; + case CHR_EVENT_BREAK: + case CHR_EVENT_MUX_IN: + case CHR_EVENT_MUX_OUT: + /* Ignore */ + break; + } +} + +static int vhost_user_scsi_realize_connect(VHostUserSCSI *s, Error **errp) +{ + DeviceState *dev =3D &s->parent_obj.parent_obj.parent_obj.parent_obj; + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); + int ret; + + s->connected =3D false; + + ret =3D qemu_chr_fe_wait_connected(&vs->conf.chardev, errp); + if (ret < 0) { + return ret; + } + + ret =3D vhost_user_scsi_connect(dev, errp); + if (ret < 0) { + qemu_chr_fe_disconnect(&vs->conf.chardev); + return ret; + } + assert(s->connected); + + return 0; } =20 static void vhost_user_scsi_realize(DeviceState *dev, Error **errp) @@ -94,18 +251,18 @@ static void vhost_user_scsi_realize(DeviceState *dev, = Error **errp) VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); VHostUserSCSI *s =3D VHOST_USER_SCSI(dev); VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); - struct vhost_virtqueue *vqs =3D NULL; Error *err =3D NULL; int ret; + int retries =3D VU_REALIZE_CONN_RETRIES; =20 if (!vs->conf.chardev.chr) { error_setg(errp, "vhost-user-scsi: missing chardev"); return; } =20 - virtio_scsi_common_realize(dev, vhost_dummy_handle_output, - vhost_dummy_handle_output, - vhost_dummy_handle_output, &err); + virtio_scsi_common_realize(dev, vhost_user_scsi_handle_output, + vhost_user_scsi_handle_output, + vhost_user_scsi_handle_output, &err); if (err !=3D NULL) { error_propagate(errp, err); return; @@ -115,18 +272,28 @@ static void vhost_user_scsi_realize(DeviceState *dev,= Error **errp) goto free_virtio; } =20 - vsc->dev.nvqs =3D VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues; - vsc->dev.vqs =3D g_new0(struct vhost_virtqueue, vsc->dev.nvqs); - vsc->dev.vq_index =3D 0; - vsc->dev.backend_features =3D 0; - vqs =3D vsc->dev.vqs; + vsc->inflight =3D g_new0(struct vhost_inflight, 1); + s->vhost_vqs =3D g_new0(struct vhost_virtqueue, + VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues); + + assert(!*errp); + do { + if (*errp) { + error_prepend(errp, "Reconnecting after error: "); + error_report_err(*errp); + *errp =3D NULL; + } + ret =3D vhost_user_scsi_realize_connect(s, errp); + } while (ret < 0 && retries--); =20 - ret =3D vhost_dev_init(&vsc->dev, &s->vhost_user, - VHOST_BACKEND_TYPE_USER, 0, errp); if (ret < 0) { goto free_vhost; } =20 + /* we're fully initialized, now we can operate, so add the handler */ + qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, + vhost_user_scsi_event, NULL, (void *)dev, + NULL, true); /* Channel and lun both are 0 for bootable vhost-user-scsi disk */ vsc->channel =3D 0; vsc->lun =3D 0; @@ -135,8 +302,12 @@ static void vhost_user_scsi_realize(DeviceState *dev, = Error **errp) return; =20 free_vhost: + g_free(s->vhost_vqs); + s->vhost_vqs =3D NULL; + g_free(vsc->inflight); + vsc->inflight =3D NULL; vhost_user_cleanup(&s->vhost_user); - g_free(vqs); + free_virtio: virtio_scsi_common_unrealize(dev); } @@ -146,16 +317,23 @@ static void vhost_user_scsi_unrealize(DeviceState *de= v) VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VHostUserSCSI *s =3D VHOST_USER_SCSI(dev); VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); - struct vhost_virtqueue *vqs =3D vsc->dev.vqs; + VirtIOSCSICommon *vs =3D VIRTIO_SCSI_COMMON(dev); =20 /* This will stop the vhost backend. */ vhost_user_scsi_set_status(vdev, 0); + qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, NULL, NULL, NU= LL, + NULL, false); =20 vhost_dev_cleanup(&vsc->dev); - g_free(vqs); + g_free(s->vhost_vqs); + s->vhost_vqs =3D NULL; + + vhost_dev_free_inflight(vsc->inflight); + g_free(vsc->inflight); + vsc->inflight =3D NULL; =20 - virtio_scsi_common_unrealize(dev); vhost_user_cleanup(&s->vhost_user); + virtio_scsi_common_unrealize(dev); } =20 static Property vhost_user_scsi_properties[] =3D { diff --git a/include/hw/virtio/vhost-scsi-common.h b/include/hw/virtio/vhos= t-scsi-common.h index 18f115527c..daa183018c 100644 --- a/include/hw/virtio/vhost-scsi-common.h +++ b/include/hw/virtio/vhost-scsi-common.h @@ -37,6 +37,9 @@ struct VHostSCSICommon { bool migratable; =20 struct vhost_inflight *inflight; + + /* vhost_scsi_common_start/vhost_scsi_common_stop */ + bool started_vu; }; =20 int vhost_scsi_common_start(VHostSCSICommon *vsc); diff --git a/include/hw/virtio/vhost-user-scsi.h b/include/hw/virtio/vhost-= user-scsi.h index 521b08e559..c66acc68b7 100644 --- a/include/hw/virtio/vhost-user-scsi.h +++ b/include/hw/virtio/vhost-user-scsi.h @@ -29,6 +29,9 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserSCSI, VHOST_USER_SCSI) struct VHostUserSCSI { VHostSCSICommon parent_obj; VhostUserState vhost_user; + bool connected; + + struct vhost_virtqueue *vhost_vqs; }; =20 #endif /* VHOST_USER_SCSI_H */ --=20 2.41.0