From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54140) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WrqmK-0007OL-Fm for qemu-devel@nongnu.org; Tue, 03 Jun 2014 11:37:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WrqmF-0000Vk-Dc for qemu-devel@nongnu.org; Tue, 03 Jun 2014 11:37:32 -0400 Received: from lputeaux-656-01-25-125.w80-12.abo.wanadoo.fr ([80.12.84.125]:38749 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WrqmF-0000Vg-7h for qemu-devel@nongnu.org; Tue, 03 Jun 2014 11:37:27 -0400 Date: Tue, 3 Jun 2014 17:37:26 +0200 From: =?iso-8859-1?Q?Beno=EEt?= Canet Message-ID: <20140603153726.GJ21314@irqsave.net> References: <1401801062-9154-1-git-send-email-kwolf@redhat.com> <1401801062-9154-17-git-send-email-kwolf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1401801062-9154-17-git-send-email-kwolf@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v3 16/21] vdi: Handle failure for potentially large allocations List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: benoit.canet@irqsave.net, mreitz@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com, armbru@redhat.com The Tuesday 03 Jun 2014 =E0 15:10:57 (+0200), Kevin Wolf wrote : > Some code in the block layer makes potentially huge allocations. Failur= e > is not completely unexpected there, so avoid aborting qemu and handle > out-of-memory situations gracefully. >=20 > This patch addresses the allocations in the vdi block driver. >=20 > Signed-off-by: Kevin Wolf > Reviewed-by: Stefan Hajnoczi > --- > block/vdi.c | 24 +++++++++++++++++++----- > 1 file changed, 19 insertions(+), 5 deletions(-) >=20 > diff --git a/block/vdi.c b/block/vdi.c > index 27737af..1f76441 100644 > --- a/block/vdi.c > +++ b/block/vdi.c > @@ -293,7 +293,12 @@ static int vdi_check(BlockDriverState *bs, BdrvChe= ckResult *res, > return -ENOTSUP; > } > =20 > - bmap =3D g_malloc(s->header.blocks_in_image * sizeof(uint32_t)); > + bmap =3D g_try_malloc(s->header.blocks_in_image * sizeof(uint32_t)= ); > + if (s->header.blocks_in_image && bmap =3D=3D NULL) { > + res->check_errors++; > + return -ENOMEM; > + } > + > memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t)); > =20 > /* Check block map and value of blocks_allocated. */ > @@ -472,7 +477,12 @@ static int vdi_open(BlockDriverState *bs, QDict *o= ptions, int flags, > =20 > bmap_size =3D header.blocks_in_image * sizeof(uint32_t); > bmap_size =3D (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE; > - s->bmap =3D g_malloc(bmap_size * SECTOR_SIZE); > + s->bmap =3D qemu_try_blockalign(bs->file, bmap_size * SECTOR_SIZE)= ; > + if (s->bmap =3D=3D NULL) { > + ret =3D -ENOMEM; > + goto fail; > + } > + > ret =3D bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bm= ap_size); > if (ret < 0) { > goto fail_free_bmap; > @@ -487,7 +497,7 @@ static int vdi_open(BlockDriverState *bs, QDict *op= tions, int flags, > return 0; > =20 > fail_free_bmap: > - g_free(s->bmap); > + qemu_vfree(s->bmap); > =20 > fail: > return ret; > @@ -760,7 +770,11 @@ static int vdi_create(const char *filename, QEMUOp= tionParameter *options, > } > =20 > if (bmap_size > 0) { > - uint32_t *bmap =3D g_malloc0(bmap_size); > + uint32_t *bmap =3D g_try_malloc0(bmap_size); > + if (bmap =3D=3D NULL) { > + result =3D -ENOMEM; > + goto close_and_exit; > + } > for (i =3D 0; i < blocks; i++) { > if (image_type =3D=3D VDI_TYPE_STATIC) { > bmap[i] =3D i; > @@ -796,7 +810,7 @@ static void vdi_close(BlockDriverState *bs) > { > BDRVVdiState *s =3D bs->opaque; > =20 > - g_free(s->bmap); > + qemu_vfree(s->bmap); > =20 > migrate_del_blocker(s->migration_blocker); > error_free(s->migration_blocker); > --=20 > 1.8.3.1 >=20 >=20 Reviewed-by: Benoit Canet