From nobody Sat Apr 20 05:24:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1531364142409686.465267266752; Wed, 11 Jul 2018 19:55:42 -0700 (PDT) Received: from localhost ([::1]:57078 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fdRlc-0001oL-UG for importer@patchew.org; Wed, 11 Jul 2018 22:55:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43153) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fdRke-0001Eu-4c for qemu-devel@nongnu.org; Wed, 11 Jul 2018 22:54:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fdRkd-0002Qp-2y for qemu-devel@nongnu.org; Wed, 11 Jul 2018 22:54:40 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36276 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fdRkW-0002JD-Ki; Wed, 11 Jul 2018 22:54:32 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 84D75400ADC2; Thu, 12 Jul 2018 02:54:30 +0000 (UTC) Received: from lemon.usersys.redhat.com (ovpn-12-125.pek2.redhat.com [10.72.12.125]) by smtp.corp.redhat.com (Postfix) with ESMTP id C42267C45; Thu, 12 Jul 2018 02:54:25 +0000 (UTC) From: Fam Zheng To: qemu-devel@nongnu.org Date: Thu, 12 Jul 2018 10:54:20 +0800 Message-Id: <20180712025420.4932-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 12 Jul 2018 02:54:30 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Thu, 12 Jul 2018 02:54:30 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'famz@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2] nvme: Fix nvme_init error handling 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: Kevin Wolf , Fam Zheng , qemu-stable@nongnu.org, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It is wrong to leave this field as 1, as nvme_close() called in the error handling code in nvme_file_open() will use it and try to free s->queues again. Another problem is the cleaning ups are duplicated between the fail* labels of nvme_init() and nvme_file_open(), which calls nvme_close(). A third problem is nvme_close() misses g_free() and event_notifier_cleanup(). Fix all of them. Cc: qemu-stable@nongnu.org Signed-off-by: Fam Zheng Reviewed-by: Stefan Hajnoczi --- v2: Adopt the suggested fix by Kevin. --- block/nvme.c | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/block/nvme.c b/block/nvme.c index 6f71122bf5..37805e8890 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -569,13 +569,13 @@ static int nvme_init(BlockDriverState *bs, const char= *device, int namespace, s->vfio =3D qemu_vfio_open_pci(device, errp); if (!s->vfio) { ret =3D -EINVAL; - goto fail; + goto out; } =20 s->regs =3D qemu_vfio_pci_map_bar(s->vfio, 0, 0, NVME_BAR_SIZE, errp); if (!s->regs) { ret =3D -EINVAL; - goto fail; + goto out; } =20 /* Perform initialize sequence as described in NVMe spec "7.6.1 @@ -585,7 +585,7 @@ static int nvme_init(BlockDriverState *bs, const char *= device, int namespace, if (!(cap & (1ULL << 37))) { error_setg(errp, "Device doesn't support NVMe command set"); ret =3D -EINVAL; - goto fail; + goto out; } =20 s->page_size =3D MAX(4096, 1 << (12 + ((cap >> 48) & 0xF))); @@ -603,7 +603,7 @@ static int nvme_init(BlockDriverState *bs, const char *= device, int namespace, PRId64 " ms)", timeout_ms); ret =3D -ETIMEDOUT; - goto fail; + goto out; } } =20 @@ -613,7 +613,7 @@ static int nvme_init(BlockDriverState *bs, const char *= device, int namespace, s->queues[0] =3D nvme_create_queue_pair(bs, 0, NVME_QUEUE_SIZE, errp); if (!s->queues[0]) { ret =3D -EINVAL; - goto fail; + goto out; } QEMU_BUILD_BUG_ON(NVME_QUEUE_SIZE & 0xF000); s->regs->aqa =3D cpu_to_le32((NVME_QUEUE_SIZE << 16) | NVME_QUEUE_SIZE= ); @@ -633,14 +633,14 @@ static int nvme_init(BlockDriverState *bs, const char= *device, int namespace, PRId64 " ms)", timeout_ms); ret =3D -ETIMEDOUT; - goto fail_queue; + goto out; } } =20 ret =3D qemu_vfio_pci_init_irq(s->vfio, &s->irq_notifier, VFIO_PCI_MSIX_IRQ_INDEX, errp); if (ret) { - goto fail_queue; + goto out; } aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier, false, nvme_handle_event, nvme_poll_cb); @@ -649,30 +649,15 @@ static int nvme_init(BlockDriverState *bs, const char= *device, int namespace, if (local_err) { error_propagate(errp, local_err); ret =3D -EIO; - goto fail_handler; + goto out; } =20 /* Set up command queues. */ if (!nvme_add_io_queue(bs, errp)) { ret =3D -EIO; - goto fail_handler; } - return 0; - -fail_handler: - aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier, - false, NULL, NULL); -fail_queue: - nvme_free_queue_pair(bs, s->queues[0]); -fail: - g_free(s->queues); - if (s->regs) { - qemu_vfio_pci_unmap_bar(s->vfio, 0, (void *)s->regs, 0, NVME_BAR_S= IZE); - } - if (s->vfio) { - qemu_vfio_close(s->vfio); - } - event_notifier_cleanup(&s->irq_notifier); +out: + /* Cleaning up is done in nvme_file_open() upon error. */ return ret; } =20 @@ -739,8 +724,10 @@ static void nvme_close(BlockDriverState *bs) for (i =3D 0; i < s->nr_queues; ++i) { nvme_free_queue_pair(bs, s->queues[i]); } + g_free(s->queues); aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier, false, NULL, NULL); + event_notifier_cleanup(&s->irq_notifier); qemu_vfio_pci_unmap_bar(s->vfio, 0, (void *)s->regs, 0, NVME_BAR_SIZE); qemu_vfio_close(s->vfio); } --=20 2.17.1