From patchwork Mon Jul 25 12:33:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ilya Maximets X-Patchwork-Id: 652245 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ryggY5r0dz9rxl for ; Mon, 25 Jul 2016 22:34:49 +1000 (AEST) Received: from localhost ([::1]:60369 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bRf5o-0004EJ-FT for incoming@patchwork.ozlabs.org; Mon, 25 Jul 2016 08:34:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54932) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bRf5A-0003wS-8E for qemu-devel@nongnu.org; Mon, 25 Jul 2016 08:34:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bRf55-0002zs-WF for qemu-devel@nongnu.org; Mon, 25 Jul 2016 08:34:03 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:34463) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bRf55-0002yC-Nl for qemu-devel@nongnu.org; Mon, 25 Jul 2016 08:33:59 -0400 Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OAV008REEWH9B50@mailout1.w1.samsung.com> for qemu-devel@nongnu.org; Mon, 25 Jul 2016 13:33:53 +0100 (BST) Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id C4.DF.05254.13706975; Mon, 25 Jul 2016 13:33:53 +0100 (BST) Received: from [106.109.129.180] by eusync2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OAV00MJEEWAGB20@eusync2.samsung.com>; Mon, 25 Jul 2016 13:33:53 +0100 (BST) Date: Mon, 25 Jul 2016 15:33:46 +0300 From: Ilya Maximets In-reply-to: <20160721085750.30008-10-marcandre.lureau@redhat.com> To: =?UTF-8?Q?Marc-Andr=c3=a9_Lureau?= , qemu-devel@nongnu.org Message-id: <5796072A.8080208@samsung.com> MIME-version: 1.0 Content-type: text/plain; charset=utf-8 Content-transfer-encoding: QUOTED-PRINTABLE X-AuditID: cbfec7f4-f796c6d000001486-22-57960731e4db X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrFLMWRmVeSWpSXmKPExsVy+t/xK7qG7NPCDRb/lrCY9vk2u8WV9p/s Fk/Wr2G3WNDWzmrx/9crVotjPZ9YLY737mCxmDxbymLxgcPMFtcnXGB14PKY8nsjq0fjcwmP eScDPZ5c28zk8X7fVTaPvi2rGAPYorhsUlJzMstSi/TtErgyZjy8xV7wRKri8ZkW1gbGrSJd jJwcEgImEiemdbJA2GISF+6tZ+ti5OIQEljKKHH98WJWkASvgKDEj8n3gIo4OJgF5CWOXMqG MNUlpkzJhSh/wShxbu9tsDnCAhES0x4fZAexRQRiJZrmLWACsYUEHCV2HDnJAtLALLCLUeL3 2sVgDWwCOhKnVh9hhNilJTH37k1mEJtFQFXi4tnDYINEgYbO2v6DCWQxp4CTxNlHXhMYBWYh uW4WwnWzEK5bwMi8ilE0tTS5oDgpPddQrzgxt7g0L10vOT93EyMkBr7sYFx8zOoQowAHoxIP rwLT1HAh1sSy4srcQ4wSHMxKIryWrNPChXhTEiurUovy44tKc1KLDzFKc7AoifPO3fU+REgg PbEkNTs1tSC1CCbLxMEp1cA4beP7/PdTHDvqWCf6KF5obZj/7dw6/kDv1mTW7Z02Dsfmfglb +OdV6WqjjpCTZ59OmC2+ec+TFJtf11Uru/4Epxx60Bn8jLFP/C7H9btq8YrF+t72iq8f7lhw b8qfyPd2m81sk8JUzxw79lZRf69/5qLf064IKDasCGT46FW5k8vbqU7rVaWXEktxRqKhFnNR cSIA5RpafH0CAAA= References: <20160721085750.30008-10-marcandre.lureau@redhat.com> User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 210.118.77.11 Subject: Re: [Qemu-devel] [v5, 09/31] vhost: fix calling vhost_dev_cleanup() after vhost_dev_init() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yuanhan.liu@linux.intel.com, victork@redhat.com, Heetae Ahn , Dyasly Sergey , mst@redhat.com, jonshin@cisco.com, mukawa@igel.co.jp Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" On 21.07.2016 11:57, Marc-André Lureau wrote: > From: Marc-André Lureau > > vhost_net_init() calls vhost_dev_init() and in case of failure, calls > vhost_dev_cleanup() directly. However, the structure is already > partially cleaned on error. Calling vhost_dev_cleanup() again will call > vhost_virtqueue_cleanup() on already clean queues, and causing potential > double-close. Instead, adjust dev->nvqs and simplify vhost_dev_init() > code to not call vhost_virtqueue_cleanup() but vhost_dev_cleanup() > instead. > > Signed-off-by: Marc-André Lureau > --- > hw/virtio/vhost.c | 13 ++++--------- > 1 file changed, 4 insertions(+), 9 deletions(-) > > diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c > index 9400b47..c61302a 100644 > --- a/hw/virtio/vhost.c > +++ b/hw/virtio/vhost.c > @@ -1047,7 +1047,8 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, > for (i = 0; i < hdev->nvqs; ++i) { > r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i); > if (r < 0) { > - goto fail_vq; > + hdev->nvqs = i; > + goto fail; > } > } > > @@ -1104,19 +1105,13 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, > memory_listener_register(&hdev->memory_listener, &address_space_memory); > QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); > return 0; > + > fail_busyloop: > while (--i >= 0) { > vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0); > } > - i = hdev->nvqs; > -fail_vq: > - while (--i >= 0) { > - vhost_virtqueue_cleanup(hdev->vqs + i); > - } > fail: > - r = -errno; > - hdev->vhost_ops->vhost_backend_cleanup(hdev); > - QLIST_REMOVE(hdev, entry); > + vhost_dev_cleanup(hdev); > return r; > } > > This patch introduces closing of zero fd on backend init failure or any other error before virtqueue_init loop because of calling 'vhost_virtqueue_cleanup()' on not initialized virtqueues. I'm suggesting following fixup: ---------------------------------------------------------------------- @@ -1069,10 +1071,9 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, goto fail; } - for (i = 0; i < hdev->nvqs; ++i) { + for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) { r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i); if (r < 0) { - hdev->nvqs = i; goto fail; } } @@ -1136,6 +1137,7 @@ fail_busyloop: vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0); } fail: + hdev->nvqs = n_initialized_vqs; vhost_dev_cleanup(hdev); return r; } ---------------------------------------------------------------------- Best regards, Ilya Maximets. diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 6175d8b..d7428c5 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1038,8 +1038,9 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, VhostBackendType backend_type, uint32_t busyloop_timeout) { uint64_t features; - int i, r; + int i, r, n_initialized_vqs; + n_initialized_vqs = 0; hdev->migration_blocker = NULL; r = vhost_set_backend_type(hdev, backend_type);