From patchwork Wed May 21 16:28:15 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 351256 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)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 576FC14008E for ; Thu, 22 May 2014 02:38:37 +1000 (EST) Received: from localhost ([::1]:60924 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn9XH-0006QF-7q for incoming@patchwork.ozlabs.org; Wed, 21 May 2014 12:38:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55150) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn9Nu-0001NN-ET for qemu-devel@nongnu.org; Wed, 21 May 2014 12:29:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn9Nn-0002JY-SF for qemu-devel@nongnu.org; Wed, 21 May 2014 12:28:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:28479) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn9Nn-0002JM-Jd for qemu-devel@nongnu.org; Wed, 21 May 2014 12:28:47 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4LGSlMU006985 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 21 May 2014 12:28:47 -0400 Received: from noname.redhat.com (ovpn-116-42.ams2.redhat.com [10.36.116.42]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s4LGSJZx017108; Wed, 21 May 2014 12:28:45 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Wed, 21 May 2014 18:28:15 +0200 Message-Id: <1400689698-3096-18-git-send-email-kwolf@redhat.com> In-Reply-To: <1400689698-3096-1-git-send-email-kwolf@redhat.com> References: <1400689698-3096-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH 17/20] vhdx: Handle failure for potentially large allocations 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 Some code in the block layer makes potentially huge allocations. Failure is not completely unexpected there, so avoid aborting qemu and handle out-of-memory situations gracefully. This patch addresses the allocations in the vhdx block driver. Signed-off-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- block/vhdx-log.c | 6 +++++- block/vhdx.c | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/block/vhdx-log.c b/block/vhdx-log.c index a77c040..3eb7e68 100644 --- a/block/vhdx-log.c +++ b/block/vhdx-log.c @@ -349,7 +349,11 @@ static int vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s, } desc_sectors = vhdx_compute_desc_sectors(hdr.descriptor_count); - desc_entries = qemu_blockalign(bs, desc_sectors * VHDX_LOG_SECTOR_SIZE); + desc_entries = qemu_try_blockalign(bs, desc_sectors * VHDX_LOG_SECTOR_SIZE); + if (desc_entries == NULL) { + ret = -ENOMEM; + goto exit; + } ret = vhdx_log_read_sectors(bs, log, §ors_read, desc_entries, desc_sectors, false); diff --git a/block/vhdx.c b/block/vhdx.c index 353c74d..0922f55 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -950,7 +950,11 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags, } /* s->bat is freed in vhdx_close() */ - s->bat = qemu_blockalign(bs, s->bat_rt.length); + s->bat = qemu_try_blockalign(bs, s->bat_rt.length); + if (s->bat == NULL) { + ret = -ENOMEM; + goto fail; + } ret = bdrv_pread(bs->file, s->bat_offset, s->bat, s->bat_rt.length); if (ret < 0) { @@ -1579,7 +1583,11 @@ static int vhdx_create_bat(BlockDriverState *bs, BDRVVHDXState *s, use_zero_blocks || bdrv_has_zero_init(bs) == 0) { /* for a fixed file, the default BAT entry is not zero */ - s->bat = g_malloc0(rt_bat->length); + s->bat = g_try_malloc0(rt_bat->length); + if (rt_bat->length && s->bat != NULL) { + ret = -ENOMEM; + goto exit; + } block_state = type == VHDX_TYPE_FIXED ? PAYLOAD_BLOCK_FULLY_PRESENT : PAYLOAD_BLOCK_NOT_PRESENT; block_state = use_zero_blocks ? PAYLOAD_BLOCK_ZERO : block_state;