From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35360) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zmvmx-0004UI-7n for qemu-devel@nongnu.org; Thu, 15 Oct 2015 23:34:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zmvmw-0006Um-A4 for qemu-devel@nongnu.org; Thu, 15 Oct 2015 23:34:39 -0400 Date: Thu, 15 Oct 2015 23:34:30 -0400 From: Jeff Cody Message-ID: <20151016033430.GA3695@localhost.localdomain> References: <1444924467-26433-1-git-send-email-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1444924467-26433-1-git-send-email-stefanha@redhat.com> Subject: Re: [Qemu-devel] [PATCH] block: fix memory leak in early exit List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: kwolf@redhat.com, Fam Zheng , qemu-devel@nongnu.org, qemu-block@nongnu.org On Thu, Oct 15, 2015 at 05:54:27PM +0200, Stefan Hajnoczi wrote: > The stream block job has two early exit code paths. They do not free > s->backing_file_str. > > Also, the early exits rely on the fact that the coroutine hasn't yielded > yet and was launched from the main thread. Therefore the coroutine is > guaranteed to be running in the main thread where block_job_completed() > may be called safely. This is very subtle so it's nice to eliminate the > assumption by unifying the early exit with the normal exit code path. > > Cc: Fam Zheng > Cc: Jeff Cody > Signed-off-by: Stefan Hajnoczi > --- > block/stream.c | 15 ++++++++++----- > 1 file changed, 10 insertions(+), 5 deletions(-) > > diff --git a/block/stream.c b/block/stream.c > index ab0bd05..1986e9a 100644 > --- a/block/stream.c > +++ b/block/stream.c > @@ -120,16 +120,16 @@ static void coroutine_fn stream_run(void *opaque) > int ret = 0; > int n = 0; > void *buf; > + bool reached_end = false; > > if (!bs->backing_hd) { > - block_job_completed(&s->common, 0); > - return; > + goto out; > } > > s->common.len = bdrv_getlength(bs); > if (s->common.len < 0) { > - block_job_completed(&s->common, s->common.len); > - return; > + ret = s->common.len; > + goto out; > } > > end = s->common.len >> BDRV_SECTOR_BITS; > @@ -207,6 +207,10 @@ wait: > s->common.offset += n * BDRV_SECTOR_SIZE; > } > > + if (sector_num == end) { > + reached_end = true; > + } > + > if (!base) { > bdrv_disable_copy_on_read(bs); > } > @@ -216,10 +220,11 @@ wait: > > qemu_vfree(buf); > > +out: > /* Modify backing chain and close BDSes in main loop */ > data = g_malloc(sizeof(*data)); > data->ret = ret; > - data->reached_end = sector_num == end; > + data->reached_end = reached_end; > block_job_defer_to_main_loop(&s->common, stream_complete, data); > } > > -- > 2.4.3 > Reviewed-by: Jeff Cody