From patchwork Tue Apr 3 16:13:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 894687 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com 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 40FvLw14Tjz9s0q for ; Wed, 4 Apr 2018 02:15:03 +1000 (AEST) Received: from localhost ([::1]:46081 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f3OaM-0004ZT-0U for incoming@patchwork.ozlabs.org; Tue, 03 Apr 2018 12:15:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50260) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f3OZ5-00044J-Vp for qemu-devel@nongnu.org; Tue, 03 Apr 2018 12:13:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f3OZ4-0005Fe-Va for qemu-devel@nongnu.org; Tue, 03 Apr 2018 12:13:43 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:45748 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 1f3OZ0-0005BZ-8F; Tue, 03 Apr 2018 12:13:38 -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 2B118EB700; Tue, 3 Apr 2018 16:13:37 +0000 (UTC) Received: from localhost (ovpn-121-230.rdu2.redhat.com [10.10.121.230]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8D08C7C52; Tue, 3 Apr 2018 16:13:34 +0000 (UTC) From: Jeff Cody To: qemu-block@nongnu.org Date: Tue, 3 Apr 2018 12:13:22 -0400 Message-Id: <20180403161324.24514-2-jcody@redhat.com> In-Reply-To: <20180403161324.24514-1-jcody@redhat.com> References: <20180403161324.24514-1-jcody@redhat.com> MIME-Version: 1.0 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.1]); Tue, 03 Apr 2018 16:13:37 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Tue, 03 Apr 2018 16:13:37 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jcody@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] [PULL 1/3] blockjob: leak fix, remove from txn when failing early 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 , peter.maydell@linaro.org, jcody@redhat.com, qemu-devel@nongnu.org, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Marc-André Lureau This fixes leaks found by ASAN such as: GTESTER tests/test-blockjob ================================================================= ==31442==ERROR: LeakSanitizer: detected memory leaks Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x7f88483cba38 in __interceptor_calloc (/lib64/libasan.so.4+0xdea38) #1 0x7f8845e1bd77 in g_malloc0 ../glib/gmem.c:129 #2 0x7f8845e1c04b in g_malloc0_n ../glib/gmem.c:360 #3 0x5584d2732498 in block_job_txn_new /home/elmarco/src/qemu/blockjob.c:172 #4 0x5584d2739b28 in block_job_create /home/elmarco/src/qemu/blockjob.c:973 #5 0x5584d270ae31 in mk_job /home/elmarco/src/qemu/tests/test-blockjob.c:34 #6 0x5584d270b1c1 in do_test_id /home/elmarco/src/qemu/tests/test-blockjob.c:57 #7 0x5584d270b65c in test_job_ids /home/elmarco/src/qemu/tests/test-blockjob.c:118 #8 0x7f8845e40b69 in test_case_run ../glib/gtestutils.c:2255 #9 0x7f8845e40f29 in g_test_run_suite_internal ../glib/gtestutils.c:2339 #10 0x7f8845e40fd2 in g_test_run_suite_internal ../glib/gtestutils.c:2351 #11 0x7f8845e411e9 in g_test_run_suite ../glib/gtestutils.c:2426 #12 0x7f8845e3fe72 in g_test_run ../glib/gtestutils.c:1692 #13 0x5584d270d6e2 in main /home/elmarco/src/qemu/tests/test-blockjob.c:377 #14 0x7f8843641f29 in __libc_start_main (/lib64/libc.so.6+0x20f29) Add an assert to make sure that the job doesn't have associated txn before free(). [Jeff Cody: N.B., used updated patch provided by John Snow] Signed-off-by: Marc-André Lureau Signed-off-by: Jeff Cody --- blockjob.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/blockjob.c b/blockjob.c index ef3ed69ff1..c510a9fde5 100644 --- a/blockjob.c +++ b/blockjob.c @@ -204,6 +204,15 @@ void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job) block_job_txn_ref(txn); } +static void block_job_txn_del_job(BlockJob *job) +{ + if (job->txn) { + QLIST_REMOVE(job, txn_list); + block_job_txn_unref(job->txn); + job->txn = NULL; + } +} + static void block_job_pause(BlockJob *job) { job->pause_count++; @@ -232,6 +241,7 @@ void block_job_unref(BlockJob *job) { if (--job->refcnt == 0) { assert(job->status == BLOCK_JOB_STATUS_NULL); + assert(!job->txn); BlockDriverState *bs = blk_bs(job->blk); QLIST_REMOVE(job, job_list); bs->job = NULL; @@ -392,6 +402,7 @@ static void block_job_decommission(BlockJob *job) job->busy = false; job->paused = false; job->deferred_to_main_loop = true; + block_job_txn_del_job(job); block_job_state_transition(job, BLOCK_JOB_STATUS_NULL); block_job_unref(job); } @@ -481,8 +492,7 @@ static int block_job_finalize_single(BlockJob *job) } } - QLIST_REMOVE(job, txn_list); - block_job_txn_unref(job->txn); + block_job_txn_del_job(job); block_job_conclude(job); return 0; }