From patchwork Sat Jul 1 15:39:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 9820915 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9F24C602CC for ; Sat, 1 Jul 2017 15:40:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 88C6E28521 for ; Sat, 1 Jul 2017 15:40:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7CD9028535; Sat, 1 Jul 2017 15:40:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 335EB28521 for ; Sat, 1 Jul 2017 15:40:43 +0000 (UTC) Received: from localhost ([::1]:55094 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRKVl-0004tV-ND for patchwork-qemu-devel@patchwork.kernel.org; Sat, 01 Jul 2017 11:40:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42949) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRKUy-0004tD-HQ for qemu-devel@nongnu.org; Sat, 01 Jul 2017 11:39:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dRKUv-0003Zr-Fv for qemu-devel@nongnu.org; Sat, 01 Jul 2017 11:39:52 -0400 Received: from smtp1.ntua.gr ([2001:648:2000:de::183]:61670) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRKUv-00039L-3e for qemu-devel@nongnu.org; Sat, 01 Jul 2017 11:39:49 -0400 Received: from mail.ntua.gr (ppp005055127131.access.hol.gr [5.55.127.131]) (authenticated bits=0) by smtp1.ntua.gr (8.15.2/8.15.2) with ESMTPSA id v61FdBZX052297 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 1 Jul 2017 18:39:11 +0300 (EEST) (envelope-from el13635@mail.ntua.gr) X-Authentication-Warning: smtp1.ntua.gr: Host ppp005055127131.access.hol.gr [5.55.127.131] claimed to be mail.ntua.gr From: Manos Pitsidianakis To: qemu-devel Date: Sat, 1 Jul 2017 18:39:06 +0300 Message-Id: <20170701153906.16588-1-el13635@mail.ntua.gr> X-Mailer: git-send-email 2.11.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:648:2000:de::183 Subject: [Qemu-devel] [PATCH v2] block: fix leaks in bdrv_open_driver() 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 , Alberto Garcia , Stefan Hajnoczi , qemu-block , Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP bdrv_open_driver() is called in two places, bdrv_new_open_driver() and bdrv_open_common(). In the latter, failure cleanup in is in its caller, bdrv_open_inherit(), which unrefs the bs->file of the failed driver open if it exists. Let's move the bs->file cleanup to bdrv_open_driver() to take care of all callers and do not set bs->drv to NULL unless the driver's open function failed. When bs is destroyed by removing its last reference, bdrv_close() checks bs->drv to perform the needed cleanups and also call the driver's close function. Signed-off-by: Manos Pitsidianakis --- v2: move bdrv_unref_child(bs, bs->file) to bdrv_open_driver do not set bs->drv to NULL if open succeeds block.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index 694396281b..df2a46990c 100644 --- a/block.c +++ b/block.c @@ -1091,6 +1091,7 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, { Error *local_err = NULL; int ret; + bool open_failed; bdrv_assign_node_name(bs, node_name, &local_err); if (local_err) { @@ -1111,7 +1112,9 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, ret = 0; } - if (ret < 0) { + open_failed = ret < 0; + + if (open_failed) { if (local_err) { error_propagate(errp, local_err); } else if (bs->filename[0]) { @@ -1142,10 +1145,15 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, return 0; free_and_fail: - /* FIXME Close bs first if already opened*/ - g_free(bs->opaque); - bs->opaque = NULL; - bs->drv = NULL; + if (open_failed) { + g_free(bs->opaque); + bs->opaque = NULL; + bs->drv = NULL; + } + if (bs->file != NULL) { + bdrv_unref_child(bs, bs->file); + bs->file = NULL; + } return ret; } @@ -2607,9 +2615,6 @@ static BlockDriverState *bdrv_open_inherit(const char *filename, fail: blk_unref(file); - if (bs->file != NULL) { - bdrv_unref_child(bs, bs->file); - } QDECREF(snapshot_options); QDECREF(bs->explicit_options); QDECREF(bs->options);