From patchwork Fri Oct 11 07:36:25 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 282542 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id D70782C00CC for ; Fri, 11 Oct 2013 18:37:14 +1100 (EST) Received: from localhost ([::1]:52849 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUXHb-0002N0-OD for incoming@patchwork.ozlabs.org; Fri, 11 Oct 2013 03:37:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50009) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUXH3-0002Bj-17 for qemu-devel@nongnu.org; Fri, 11 Oct 2013 03:36:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VUXGw-0004Ib-VZ for qemu-devel@nongnu.org; Fri, 11 Oct 2013 03:36:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:2654) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUXGw-0004IX-Me for qemu-devel@nongnu.org; Fri, 11 Oct 2013 03:36:30 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r9B7aTir005231 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 11 Oct 2013 03:36:30 -0400 Received: from localhost (dhcp-200-247.str.redhat.com [10.33.200.247]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r9B7aS9E011463 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 11 Oct 2013 03:36:29 -0400 From: Max Reitz To: qemu-devel@nongnu.org Date: Fri, 11 Oct 2013 09:36:25 +0200 Message-Id: <1381476986-3032-2-git-send-email-mreitz@redhat.com> In-Reply-To: <1381476986-3032-1-git-send-email-mreitz@redhat.com> References: <1381476986-3032-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH v2 1/2] qcow2: Undo leaked allocations in co_writev X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org If the write request spans more than one L2 table, qcow2_alloc_cluster_offset cannot handle the required allocations atomically. This results in leaks if it allocated new clusters in any but the last L2 table touched and an error occurs in qcow2_co_writev before having established the L2 link. These non-atomic allocations were, however, indeed successful and are therefore given to the caller in the L2Meta list. If an error occurs in qcow2_co_writev and the L2Meta list is unwound, all its remaining entries are clusters whose L2 links were not yet established. Thus, all allocations in that list should be undone. Signed-off-by: Max Reitz --- block/qcow2.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/block/qcow2.c b/block/qcow2.c index 13e34f0..69d8270 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1078,6 +1078,14 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs, goto fail; } + /* WARNING: If adding a "goto fail" anywhere from here until the + * current QCowL2Meta element is removed from the l2meta list (that + * is, until the end of this loop), mind the qcow2_free_clusters() + * call after the fail label. Always make sure that every list + * element with a non-zero nb_clusters value remaining when reaching + * the while loop after that label indeed corresponds to a leaked + * allocation which has to be freed. */ + /* Take the request off the list of running requests */ if (l2meta->nb_clusters != 0) { QLIST_REMOVE(l2meta, next_in_flight); @@ -1103,6 +1111,13 @@ fail: while (l2meta != NULL) { QCowL2Meta *next; + /* Undo all leaked allocations */ + if (l2meta->nb_clusters != 0) { + qcow2_free_clusters(bs, l2meta->alloc_offset, + l2meta->nb_clusters << s->cluster_bits, + QCOW2_DISCARD_OTHER); + } + if (l2meta->nb_clusters != 0) { QLIST_REMOVE(l2meta, next_in_flight); }