block: Handle failure for potentially large allocations
cover block: Handle failure for potentially large allocations
A not too small part of the recent CVEs were DoS scenarios by letting qemu abort with too large memory allocations. We generally "fixed" these cases by setting some limits on values read from image files that influence the size of allocations.
Because we still need to allow reading large images, this works only to a certain degree and we still can get fairly large allocations, which are not unthinkable to fail on some machines.
This series converts potentially large allocations to g_try_malloc() and friends and handles failure gracefully e.g. by returning -ENOMEM. This may cause hot-plug of a new disk or individual requests to fail, but the VM as a whole can keep running.
Kevin Wolf (20):
block: Introduce qemu_try_blockalign()
block: Handle failure for potentially large allocations
bochs: Handle failure for potentially large allocations
cloop: Handle failure for potentially large allocations
curl: Handle failure for potentially large allocations
dmg: Handle failure for potentially large allocations
iscsi: Handle failure for potentially large allocations
nfs: Handle failure for potentially large allocations
parallels: Handle failure for potentially large allocations
qcow1: Handle failure for potentially large allocations
qcow2: Handle failure for potentially large allocations
qed: Handle failure for potentially large allocations
raw-posix: Handle failure for potentially large allocations
raw-win32: Handle failure for potentially large allocations
rbd: Handle failure for potentially large allocations
vdi: Handle failure for potentially large allocations
vhdx: Handle failure for potentially large allocations
vmdk: Handle failure for potentially large allocations
vpc: Handle failure for potentially large allocations
mirror: Handle failure for potentially large allocations
block.c | 33 +++++++++++++++++++++++++++------ block/bochs.c | 6 +++++- block/cloop.c | 23 ++++++++++++++++++++--- block/curl.c | 8 +++++++- block/dmg.c | 11 +++++++++-- block/iscsi.c | 16 +++++++++++++--- block/mirror.c | 7 ++++++- block/nfs.c | 6 +++++- block/parallels.c | 6 +++++- block/qcow.c | 29 ++++++++++++++++++++++++----- block/qcow2-cache.c | 12 +++++++++++- block/qcow2-cluster.c | 28 +++++++++++++++++++++++----- block/qcow2-refcount.c | 46 ++++++++++++++++++++++++++++++++++++---------- block/qcow2-snapshot.c | 18 +++++++++++++++--- block/qcow2.c | 37 +++++++++++++++++++++++++++++++------ block/qed-check.c | 7 +++++-- block/qed.c | 6 +++++- block/raw-posix.c | 6 +++++- block/rbd.c | 5 ++++- block/vdi.c | 20 +++++++++++++++++--- block/vhdx-log.c | 6 +++++- block/vhdx.c | 12 ++++++++++-- block/vmdk.c | 12 ++++++++++-- block/vpc.c | 6 +++++- block/win32-aio.c | 6 +++++- include/block/block.h | 1 + include/qemu/osdep.h | 1 + util/oslib-posix.c | 16 ++++++++++------ util/oslib-win32.c | 9 +++++++-- 29 files changed, 327 insertions(+), 72 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/20 block: Introduce qemu_try_blockalign()
This function returns NULL instead of aborting when an allocation fails.
--- block.c | 5 +++++ include/block/block.h | 1 + include/qemu/osdep.h | 1 + util/oslib-posix.c | 16 ++++++++++------ util/oslib-win32.c | 9 +++++++-- 5 files changed, 24 insertions(+), 8 deletions(-)
block.c +5 -0
@@ -5195,6 +5195,11 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size)return qemu_memalign (bdrv_opt_mem_align (bs), size); } +void *qemu_try_blockalign (BlockDriverState *bs,size_t size) +{ +return qemu_try_memalign (bdrv_opt_mem_align (bs), size); +} +/* * Check if all memory in this vector is sector aligned. */
include/block/block.h +1 -0
@@ -433,6 +433,7 @@ void bdrv_img_create(const char *filename, const char *fmt,size_t bdrv_opt_mem_align (BlockDriverState *bs);void bdrv_set_guest_block_size (BlockDriverState *bs,int align);void *qemu_blockalign (BlockDriverState *bs,size_t size); +void *qemu_try_blockalign (BlockDriverState *bs,size_t size);bool bdrv_qiov_is_aligned (BlockDriverState *bs,QEMUIOVector *qiov);struct HBitmapIter ;
include/qemu/osdep.h +1 -0
@@ -95,6 +95,7 @@ typedef signed int int_fast16_t;#define qemu_printf printfint qemu_daemon (int nochdir,int noclose); +void *qemu_try_memalign (size_t alignment,size_t size);void *qemu_memalign (size_t alignment,size_t size);void *qemu_anon_ram_alloc (size_t size);void qemu_vfree (void *ptr);
util/oslib-posix.c +10 -6
@@ -90,7 +90,7 @@ void *qemu_oom_check(void *ptr)return ptr; } -void *qemu_memalign (size_t alignment,size_t size) +void *qemu_try_memalign (size_t alignment,size_t size) {void *ptr;
@@ -102,19 +102,23 @@ void *qemu_memalign(size_t alignment, size_t size)int ret; ret =posix_memalign (&ptr, alignment, size);if (ret !=0 ) { -fprintf (stderr,"Failed to allocate %zu B: %s\n" , - size,strerror (ret)); -abort (); + errno = ret; + ptr =NULL ; }#elif defined(CONFIG_BSD ) - ptr =qemu_oom_check (valloc (size)); + ptr =valloc (size);#else - ptr =qemu_oom_check (memalign (alignment, size)); + ptr =memalign (alignment, size);#endif trace_qemu_memalign (alignment, size, ptr);return ptr; } +void *qemu_memalign (size_t alignment,size_t size) +{ +return qemu_oom_check (qemu_try_memalign (alignment, size)); +} +/* alloc shared memory pages */ void *qemu_anon_ram_alloc (size_t size) {
util/oslib-win32.c +7 -2
@@ -46,18 +46,23 @@ void *qemu_oom_check(void *ptr)return ptr; } -void *qemu_memalign (size_t alignment,size_t size) +void *qemu_try_memalign (size_t alignment,size_t size) {void *ptr;if (!size) {abort (); } - ptr =qemu_oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE )); + ptr =VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE );trace_qemu_memalign (alignment, size, ptr);return ptr; } +void *qemu_memalign (size_t alignment,size_t size) +{ +return qemu_oom_check (qemu_try_memalign (alignment, size)); +} +void *qemu_anon_ram_alloc (size_t size) {void *ptr;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 01/20] block: Introduce qemu_try_blockalign()
Quoted 10 lines · Kevin Wolf
This function returns NULL instead of aborting when an allocation fails.
Signed-off-by Kevin Wolf <[email protected]>--- block.c | 5 +++++ include/block/block.h | 1 + include/qemu/osdep.h | 1 + util/oslib-posix.c | 16 ++++++++++------ util/oslib-win32.c | 9 +++++++-- 5 files changed, 24 insertions(+), 8 deletions(-)
PATCH 2/20 block: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 bounce buffer allocations in block.c. While at it, convert bdrv_commit() from plain g_malloc() to qemu_try_blockalign().
--- block.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
block.c +22 -6
@@ -2270,7 +2270,10 @@ int bdrv_commit(BlockDriverState *bs) } total_sectors = length >>BDRV_SECTOR_BITS ; - buf =g_malloc (COMMIT_BUF_SECTORS *BDRV_SECTOR_SIZE ); + +/* qemu_try_blockalign() for bs will choose an alignment that works for +* bs->backing_hd as well, so no need to compare the alignment manually. */ + buf =qemu_try_blockalign (bs,COMMIT_BUF_SECTORS *BDRV_SECTOR_SIZE );for (sector =0 ; sector < total_sectors; sector += n) { ret =bdrv_is_allocated (bs, sector,COMMIT_BUF_SECTORS , &n);
@@ -2969,7 +2972,12 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, cluster_sector_num, cluster_nb_sectors); iov.iov_len = cluster_nb_sectors *BDRV_SECTOR_SIZE ; - iov.iov_base = bounce_buffer =qemu_blockalign (bs, iov.iov_len); + iov.iov_base = bounce_buffer =qemu_try_blockalign (bs, iov.iov_len); +if (bounce_buffer ==NULL ) { + ret = -ENOMEM ; + goto err; + } +qemu_iovec_init_external (&bounce_qiov, &iov,1 ); ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
@@ -3241,7 +3249,11 @@ static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,/* Fall back to bounce buffer if write zeroes is unsupported */ iov.iov_len = num *BDRV_SECTOR_SIZE ;if (iov.iov_base ==NULL ) { - iov.iov_base =qemu_blockalign (bs, num *BDRV_SECTOR_SIZE ); + iov.iov_base =qemu_try_blockalign (bs, num *BDRV_SECTOR_SIZE ); +if (iov.iov_base ==NULL ) { + ret = -ENOMEM ; + goto fail; + }memset (iov.iov_base,0 , num *BDRV_SECTOR_SIZE ); }qemu_iovec_init_external (&qiov, &iov,1 );
@@ -3261,6 +3273,7 @@ static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, nb_sectors -= num; } +fail:qemu_vfree (iov.iov_base);return ret; }
@@ -4569,8 +4582,9 @@ static void bdrv_aio_bh_cb(void *opaque) {BlockDriverAIOCBSync *acb = opaque; -if (!acb->is_write) +if (!acb->is_write && acb->ret >=0 ) {qemu_iovec_from_buf (acb->qiov,0 , acb->bounce, acb->qiov->size); + }qemu_vfree (acb->bounce); acb->common.cb (acb->common.opaque, acb->ret);qemu_bh_delete (acb->bh);
@@ -4592,10 +4606,12 @@ static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, acb =qemu_aio_get (&bdrv_em_aiocb_info, bs, cb, opaque); acb->is_write = is_write; acb->qiov = qiov; - acb->bounce =qemu_blockalign (bs, qiov->size); + acb->bounce =qemu_try_blockalign (bs, qiov->size); acb->bh =qemu_bh_new (bdrv_aio_bh_cb, acb); -if (is_write) { +if (acb->bounce ==NULL ) { + acb->ret = -ENOMEM ; + }else if (is_write) {qemu_iovec_to_buf (acb->qiov,0 , acb->bounce, qiov->size); acb->ret = bs->drv->bdrv_write (bs, sector_num, acb->bounce, nb_sectors); }else {
Signature · 1 line
1.8.3.1
block.c int bdrv_commit(BlockDriverState *bs) · line 2276
Quoted 9 lines · Kevin Wolf
block.c +4
-1@@ -2270,7 +2270,10 @@ int bdrv_commit(BlockDriverState *bs) } total_sectors = length >>BDRV_SECTOR_BITS ; - buf =g_malloc (COMMIT_BUF_SECTORS *BDRV_SECTOR_SIZE ); + +/* qemu_try_blockalign() for bs will choose an alignment that works for +* bs->backing_hd as well, so no need to compare the alignment manually. */ + buf =qemu_try_blockalign (bs,COMMIT_BUF_SECTORS *BDRV_SECTOR_SIZE );
Missing ENOMEM error handling?
PATCH 3/20 bochs: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 bochs block driver.
--- block/bochs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
block/bochs.c +5 -1
@@ -131,7 +131,11 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,return -EFBIG ; } - s->catalog_bitmap =g_malloc (s->catalog_size *4 ); + s->catalog_bitmap =g_try_malloc (s->catalog_size *4 ); +if (s->catalog_size && s->catalog_bitmap ==NULL ) { +error_setg (errp,"Could not allocate memory for catalog" ); +return -ENOMEM ; + } ret =bdrv_pread (bs->file,le32_to_cpu (bochs.header), s->catalog_bitmap, s->catalog_size *4 );
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 03/20] bochs: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 bochs block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/bochs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 4/20 cloop: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 cloop block driver.
--- block/cloop.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
block/cloop.c +20 -3
@@ -116,7 +116,12 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags, "try increasing block size");return -EINVAL ; } - s->offsets =g_malloc (offsets_size); + + s->offsets =g_try_malloc (offsets_size); +if (s->offsets ==NULL ) { +error_setg (errp,"Could not allocate offsets table" ); +return -ENOMEM ; + } ret =bdrv_pread (bs->file,128 +4 +4 , s->offsets, offsets_size);if (ret <0 ) {
@@ -158,8 +163,20 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags, }/* initialize zlib engine */ - s->compressed_block =g_malloc (max_compressed_block_size +1 ); - s->uncompressed_block =g_malloc (s->block_size); + s->compressed_block =g_try_malloc (max_compressed_block_size +1 ); +if (s->compressed_block ==NULL ) { +error_setg (errp,"Could not allocate compressed_block" ); + ret = -ENOMEM ; + goto fail; + } + + s->uncompressed_block =g_try_malloc (s->block_size); +if (s->uncompressed_block ==NULL ) { +error_setg (errp,"Could not allocate uncompressed_block" ); + ret = -ENOMEM ; + goto fail; + } +if (inflateInit (&s->zstream) !=Z_OK ) { ret = -EINVAL ; goto fail;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 04/20] cloop: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 cloop block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/cloop.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
PATCH 5/20 curl: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 curl block driver.
--- block/curl.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
block/curl.c +7 -1
@@ -604,7 +604,13 @@ static void curl_readv_bh_cb(void *p) state->buf_start = start; state->buf_len = acb->end + s->readahead_size; end =MIN (start + state->buf_len, s->len) -1 ; - state->orig_buf =g_malloc (state->buf_len); + state->orig_buf =g_try_malloc (state->buf_len); +if (state->buf_len && state->orig_buf ==NULL ) { +curl_clean_state (state); + acb->common.cb (acb->common.opaque, -ENOMEM ); +qemu_aio_release (acb); +return ; + } state->acb[0 ] = acb;snprintf (state->range,127 ,"%zd-%zd" , start, end);
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 05/20] curl: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 curl block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/curl.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
PATCH 6/20 dmg: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 dmg block driver.
--- block/dmg.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
block/dmg.c +9 -2
@@ -284,8 +284,15 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags, }/* initialize zlib engine */ - s->compressed_chunk =g_malloc (max_compressed_size +1 ); - s->uncompressed_chunk =g_malloc (512 * max_sectors_per_chunk); + s->compressed_chunk =qemu_try_blockalign (bs->file, + max_compressed_size +1 ); + s->uncompressed_chunk =qemu_try_blockalign (bs->file, +512 * max_sectors_per_chunk); +if (s->compressed_chunk ==NULL || s->uncompressed_chunk ==NULL ) { + ret = -ENOMEM ; + goto fail; + } +if (inflateInit (&s->zstream) !=Z_OK ) { ret = -EINVAL ; goto fail;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 06/20] dmg: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 dmg block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/dmg.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
PATCH 7/20 iscsi: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 iscsi block driver.
--- block/iscsi.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
block/iscsi.c +13 -3
@@ -296,7 +296,10 @@ static int coroutine_fn iscsi_co_writev(BlockDriverState *bs, data = iov->iov[0 ].iov_base; }else {size_t size =MIN (nb_sectors *BDRV_SECTOR_SIZE , iov->size); - buf =g_malloc (size); + buf =g_try_malloc (size); +if (buf ==NULL ) { +return -ENOMEM ; + }qemu_iovec_to_buf (iov,0 , buf, size); data = buf; }
@@ -550,7 +553,11 @@ static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,#else struct iovec *iov = (struct iovec *)acb->ioh->dxferp; - acb->buf =g_malloc (acb->ioh->dxfer_len); + acb->buf =g_try_malloc (acb->ioh->dxfer_len); +if (acb->buf ==NULL ) { +qemu_aio_release (acb); +return NULL ; + } data.data = acb->buf; data.size =iov_to_buf (iov, acb->ioh->iovec_count,0 , acb->buf, acb->ioh->dxfer_len);
@@ -823,7 +830,10 @@ coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, nb_blocks =sector_qemu2lun (nb_sectors, iscsilun);if (iscsilun->zeroblock ==NULL ) { - iscsilun->zeroblock =g_malloc0 (iscsilun->block_size); + iscsilun->zeroblock =g_try_malloc0 (iscsilun->block_size); +if (iscsilun->zeroblock ==NULL ) { +return -ENOMEM ; + } }iscsi_co_init_iscsitask (iscsilun, &iTask);
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 07/20] iscsi: Handle failure for potentially large allocations
Il 21/05/2014 18:28, Kevin Wolf ha scritto:
Quoted 43 lines
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 iscsi block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/iscsi.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)block/iscsi.c +9
-2@@ -296,7 +296,10 @@ static int coroutine_fn iscsi_co_writev(BlockDriverState *bs, data = iov->iov[0 ].iov_base; }else {size_t size =MIN (nb_sectors *BDRV_SECTOR_SIZE , iov->size); - buf =g_malloc (size); + buf =g_try_malloc (size); +if (buf ==NULL ) { +return -ENOMEM ; + }qemu_iovec_to_buf (iov,0 , buf, size); data = buf; }@@ -550,7 +553,11 @@ static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,#else struct iovec *iov = (struct iovec *)acb->ioh->dxferp; - acb->buf =g_malloc (acb->ioh->dxfer_len); + acb->buf =g_try_malloc (acb->ioh->dxfer_len); +if (acb->buf ==NULL ) { +qemu_aio_release (acb); +return NULL ; + } data.data = acb->buf; data.size =iov_to_buf (iov, acb->ioh->iovec_count,0 , acb->buf, acb->ioh->dxfer_len);@@ -823,7 +830,10 @@ coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, nb_blocks =sector_qemu2lun (nb_sectors, iscsilun);
block/iscsi.c coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num, · line 839
Quoted 10 lines
block/iscsi.c +4
-1if (iscsilun->zeroblock ==NULL ) { - iscsilun->zeroblock =g_malloc0 (iscsilun->block_size); + iscsilun->zeroblock =g_try_malloc0 (iscsilun->block_size); +if (iscsilun->zeroblock ==NULL ) { +return -ENOMEM ; + } }iscsi_co_init_iscsitask (iscsilun, &iTask);
Re: [Qemu-devel] [PATCH 07/20] iscsi: Handle failure for potentially large allocations
In reply to Kevin Wolfblock/iscsi.c static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, · line 560
Quoted 10 lines · Kevin Wolf
block/iscsi.c +5
-1@@ -550,7 +553,11 @@ static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,#else struct iovec *iov = (struct iovec *)acb->ioh->dxferp; - acb->buf =g_malloc (acb->ioh->dxfer_len); + acb->buf =g_try_malloc (acb->ioh->dxfer_len); +if (acb->buf ==NULL ) { +qemu_aio_release (acb); +return NULL ; + }
Leaks acb->task which happens to be malloc(3) allocated :(.
PATCH 8/20 nfs: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 nfs block driver.
--- block/nfs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
block/nfs.c +5 -1
@@ -165,7 +165,11 @@ static int coroutine_fn nfs_co_writev(BlockDriverState *bs,nfs_co_init_task (client, &task); - buf =g_malloc (nb_sectors *BDRV_SECTOR_SIZE ); + buf =g_try_malloc (nb_sectors *BDRV_SECTOR_SIZE ); +if (buf ==NULL ) { +return -ENOMEM ; + } +qemu_iovec_to_buf (iov,0 , buf, nb_sectors *BDRV_SECTOR_SIZE ); if (nfs_pwrite_async(client->context, client->fh,
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 08/20] nfs: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 nfs block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/nfs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Re: [Qemu-devel] [PATCH 08/20] nfs: Handle failure for potentially large allocations
In reply to Kevin WolfFor this case and for the iscsi case, isn't it likely that once this happens the guest is pretty much doomed since I/O to the disk will no longer work ?
Should we also change the block layer so that IF *_readv/_writev fails with -ENOMEM
then it should try again but break the request up into a chain of smaller chunks ?
Quoted 32 lines · Kevin Wolf
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 nfs block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/nfs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)block/nfs.c +5
-2@@ -165,7 +165,11 @@ static int coroutine_fn nfs_co_writev(BlockDriverState *bs,nfs_co_init_task (client, &task); - buf =g_malloc (nb_sectors *BDRV_SECTOR_SIZE ); + buf =g_try_malloc (nb_sectors *BDRV_SECTOR_SIZE ); +if (buf ==NULL ) { +return -ENOMEM ; + } +qemu_iovec_to_buf (iov,0 , buf, nb_sectors *BDRV_SECTOR_SIZE ); if (nfs_pwrite_async(client->context, client->fh, --1.8.3.1
PATCH 9/20 parallels: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 parallels block driver.
--- block/parallels.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
block/parallels.c +5 -1
@@ -105,7 +105,11 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags, ret = -EFBIG ; goto fail; } - s->catalog_bitmap =g_malloc (s->catalog_size *4 ); + s->catalog_bitmap =g_try_malloc (s->catalog_size *4 ); +if (s->catalog_bitmap ==NULL ) { + ret = -ENOMEM ; + goto fail; + } ret =bdrv_pread (bs->file,64 , s->catalog_bitmap, s->catalog_size *4 );if (ret <0 ) {
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 09/20] parallels: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 parallels block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/parallels.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 10/20 qcow1: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 qcow1 block driver.
--- block/qcow.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
block/qcow.c +24 -5
@@ -182,7 +182,12 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags, } s->l1_table_offset = header.l1_table_offset; - s->l1_table =g_malloc (s->l1_size *sizeof (uint64_t )); + s->l1_table =g_try_malloc (s->l1_size *sizeof (uint64_t )); +if (s->l1_table ==NULL ) { +error_setg (errp,"Could not allocate memory for L1 table" ); + ret = -ENOMEM ; + goto fail; + } ret =bdrv_pread (bs->file, s->l1_table_offset, s->l1_table, s->l1_size *sizeof (uint64_t ));
@@ -193,8 +198,16 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,for (i =0 ;i < s->l1_size; i++) {be64_to_cpus (&s->l1_table[i]); } -/* alloc L2 cache */ - s->l2_cache =g_malloc (s->l2_size *L2_CACHE_SIZE *sizeof (uint64_t )); + +/* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */ + s->l2_cache = +qemu_try_blockalign (bs->file, + s->l2_size *L2_CACHE_SIZE *sizeof (uint64_t )); +if (s->l2_cache ==NULL ) { +error_setg (errp,"Could not allocate L2 table cache" ); + ret = -ENOMEM ; + goto fail; + } s->cluster_cache =g_malloc (s->cluster_size); s->cluster_data =g_malloc (s->cluster_size); s->cluster_cache_offset =-1 ;
@@ -517,7 +530,10 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,void *orig_buf;if (qiov->niov >1 ) { - buf = orig_buf =qemu_blockalign (bs, qiov->size); + buf = orig_buf =qemu_try_blockalign (bs, qiov->size); +if (buf ==NULL ) { +return -ENOMEM ; + } }else { orig_buf =NULL ; buf = (uint8_t *)qiov->iov->iov_base;
@@ -619,7 +635,10 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num, s->cluster_cache_offset =-1 ;/* disable compressed cache */ if (qiov->niov >1 ) { - buf = orig_buf =qemu_blockalign (bs, qiov->size); + buf = orig_buf =qemu_try_blockalign (bs, qiov->size); +if (buf ==NULL ) { +return -ENOMEM ; + }qemu_iovec_to_buf (qiov,0 , buf, qiov->size); }else { orig_buf =NULL ;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 10/20] qcow1: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 qcow1 block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/qcow.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
PATCH 11/20 qcow2: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 qcow2 block driver.
--- block/qcow2-cache.c | 12 +++++++++++- block/qcow2-cluster.c | 28 +++++++++++++++++++++++----- block/qcow2-refcount.c | 46 ++++++++++++++++++++++++++++++++++++---------- block/qcow2-snapshot.c | 18 +++++++++++++++--- block/qcow2.c | 37 +++++++++++++++++++++++++++++++------ 5 files changed, 116 insertions(+), 25 deletions(-)
block/qcow2-cache.c +11 -1
@@ -53,10 +53,20 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) c->entries =g_malloc0 (sizeof (*c->entries) * num_tables);for (i =0 ; i < c->size; i++) { - c->entries[i].table =qemu_blockalign (bs, s->cluster_size); + c->entries[i].table =qemu_try_blockalign (bs, s->cluster_size); +if (c->entries[i].table ==NULL ) { + goto fail; + } }return c; + +fail: +for (i =0 ; i < c->size; i++) { +g_free (c->entries[i].table); + } +g_free (c); +return NULL ; }int qcow2_cache_destroy (BlockDriverState * bs,Qcow2Cache *c)
block/qcow2-cluster.c +23 -5
@@ -72,7 +72,11 @@ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,#endif new_l1_size2 =sizeof (uint64_t ) * new_l1_size; - new_l1_table =g_malloc0 (align_offset (new_l1_size2,512 )); + new_l1_table =g_try_malloc0 (align_offset (new_l1_size2,512 )); +if (new_l1_table ==NULL ) { +return -ENOMEM ; + } +memcpy (new_l1_table, s->l1_table, s->l1_size *sizeof (uint64_t ));/* write new table (align to cluster) */
@@ -372,7 +376,10 @@ static int coroutine_fn copy_sectors(BlockDriverState *bs, } iov.iov_len = n *BDRV_SECTOR_SIZE ; - iov.iov_base =qemu_blockalign (bs, iov.iov_len); + iov.iov_base =qemu_try_blockalign (bs, iov.iov_len); +if (iov.iov_base ==NULL ) { +return -ENOMEM ; + }qemu_iovec_init_external (&qiov, &iov,1 );
@@ -702,7 +709,11 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)trace_qcow2_cluster_link_l2 (qemu_coroutine_self (), m->nb_clusters);assert (m->nb_clusters >0 ); - old_cluster =g_malloc (m->nb_clusters *sizeof (uint64_t )); + old_cluster =g_try_malloc (m->nb_clusters *sizeof (uint64_t )); +if (old_cluster ==NULL ) { + ret = -ENOMEM ; + goto err; + }/* copy content of unmodified sectors */ ret =perform_cow (bs, m, &m->cow_start);
@@ -1562,7 +1573,10 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,if (!is_active_l1) {/* inactive L2 tables require a buffer to be stored in when loading * them from disk */ - l2_table =qemu_blockalign (bs, s->cluster_size); + l2_table =qemu_try_blockalign (bs, s->cluster_size); +if (l2_table ==NULL ) { +return -ENOMEM ; + } }for (i =0 ; i < l1_size; i++) {
@@ -1740,7 +1754,11 @@ int qcow2_expand_zero_clusters(BlockDriverState *bs) nb_clusters =size_to_clusters (s, bs->file->total_sectors *BDRV_SECTOR_SIZE ); - expanded_clusters =g_malloc0 ((nb_clusters +7 ) /8 ); + expanded_clusters =g_try_malloc0 ((nb_clusters +7 ) /8 ); +if (expanded_clusters ==NULL ) { + ret = -ENOMEM ; + goto fail; + } ret =expand_zero_clusters_in_l1 (bs, s->l1_table, s->l1_size, &expanded_clusters, &nb_clusters);
block/qcow2-refcount.c +36 -10
97 lines not on this page · raw message
block/qcow2-snapshot.c +15 -3
@@ -381,7 +381,12 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) sn->l1_table_offset = l1_table_offset; sn->l1_size = s->l1_size; - l1_table =g_malloc (s->l1_size *sizeof (uint64_t )); + l1_table =g_try_malloc (s->l1_size *sizeof (uint64_t )); +if (s->l1_size && l1_table ==NULL ) { + ret = -ENOMEM ; + goto fail; + } +for (i =0 ; i < s->l1_size; i++) { l1_table[i] =cpu_to_be64 (s->l1_table[i]); }
@@ -499,7 +504,11 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)* Decrease the refcount referenced by the old one only when the L1 * table is overwritten. */ - sn_l1_table =g_malloc0 (cur_l1_bytes); + sn_l1_table =g_try_malloc0 (cur_l1_bytes); +if (cur_l1_bytes && sn_l1_table ==NULL ) { + ret = -ENOMEM ; + goto fail; + } ret =bdrv_pread (bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes);if (ret <0 ) {
@@ -698,7 +707,10 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs,return -EFBIG ; } new_l1_bytes = sn->l1_size *sizeof (uint64_t ); - new_l1_table =g_malloc0 (align_offset (new_l1_bytes,512 )); + new_l1_table =g_try_malloc0 (align_offset (new_l1_bytes,512 )); +if (new_l1_bytes && new_l1_table ==NULL ) { +return -ENOMEM ; + } ret =bdrv_pread (bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes);if (ret <0 ) {
block/qcow2.c +31 -6
@@ -676,8 +676,13 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,if (s->l1_size >0 ) { - s->l1_table =g_malloc0 ( + s->l1_table =qemu_try_blockalign (bs->file,align_offset (s->l1_size *sizeof (uint64_t ),512 )); +if (s->l1_size && s->l1_table ==NULL ) { +error_setg (errp,"Could not allocate L1 table" ); + ret = -ENOMEM ; + goto fail; + } ret =bdrv_pread (bs->file, s->l1_table_offset, s->l1_table, s->l1_size *sizeof (uint64_t ));if (ret <0 ) {
@@ -692,11 +697,22 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,/* alloc L2 table/refcount block cache */ s->l2_table_cache =qcow2_cache_create (bs,L2_CACHE_SIZE ); s->refcount_block_cache =qcow2_cache_create (bs,REFCOUNT_CACHE_SIZE ); +if (s->l2_table_cache ==NULL || s->refcount_block_cache ==NULL ) { +error_setg (errp,"Could not allocate metadata caches" ); + ret = -ENOMEM ; + goto fail; + } s->cluster_cache =g_malloc (s->cluster_size);/* one more sector for decompressed data alignment */ - s->cluster_data =qemu_blockalign (bs,QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size - +512 ); + s->cluster_data =qemu_try_blockalign (bs,QCOW_MAX_CRYPT_CLUSTERS + * s->cluster_size +512 ); +if (s->cluster_data ==NULL ) { +error_setg (errp,"Could not allocate temporary cluster buffer" ); + ret = -ENOMEM ; + goto fail; + } + s->cluster_cache_offset =-1 ; s->flags = flags;
@@ -1063,7 +1079,12 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,*/ if (!cluster_data) { cluster_data = -qemu_blockalign (bs,QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); +qemu_try_blockalign (bs,QCOW_MAX_CRYPT_CLUSTERS + * s->cluster_size); +if (cluster_data ==NULL ) { + ret = -ENOMEM ; + goto fail; + } } assert(cur_nr_sectors <=
@@ -1163,8 +1184,12 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,if (s->crypt_method) {if (!cluster_data) { - cluster_data =qemu_blockalign (bs,QCOW_MAX_CRYPT_CLUSTERS * - s->cluster_size); + cluster_data =qemu_try_blockalign (bs,QCOW_MAX_CRYPT_CLUSTERS + * s->cluster_size); +if (cluster_data ==NULL ) { + ret = -ENOMEM ; + goto fail; + } } assert(hd_qiov.size <=
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 11/20] qcow2: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
block/qcow2-cache.c +1
-1@@ -53,10 +53,20 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) c->entries =g_malloc0 (sizeof (*c->entries) * num_tables);for (i =0 ; i < c->size; i++) { - c->entries[i].table =qemu_blockalign (bs, s->cluster_size); + c->entries[i].table =qemu_try_blockalign (bs, s->cluster_size);
block/qcow2-cache.c Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables) · line 66
Quoted 10 lines
block/qcow2-cache.c +7
-0+if (c->entries[i].table ==NULL ) { + goto fail; + } }return c; + +fail: +for (i =0 ; i < c->size; i++) { +g_free (c->entries[i].table);
qemu_blockalign() must be paired with qemu_vfree().
Come to think of it, in the patches where you converted malloc to blockalign, please check that the buffer is vfreed.
block/qcow2.c static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, · line 679
Quoted 10 lines
block/qcow2.c +1
-1@@ -676,8 +676,13 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,if (s->l1_size >0 ) { - s->l1_table = g_malloc0( + s->l1_table = qemu_try_blockalign(bs->file,
Is blockalign used consistently for s->l1_table? Or places in this patch have l1_table = g_try_malloc0(). We need to be careful because of g_free() vs qemu_vfree().
PATCH 12/20 qed: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 qed block driver.
--- block/qed-check.c | 7 +++++-- block/qed.c | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-)
block/qed-check.c +5 -2
@@ -227,8 +227,11 @@ int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix) };int ret; - check.used_clusters =g_malloc0 (((check.nclusters +31 ) /32 ) * -sizeof (check.used_clusters[0 ])); + check.used_clusters =g_try_malloc0 (((check.nclusters +31 ) /32 ) * +sizeof (check.used_clusters[0 ])); +if (check.nclusters && check.used_clusters ==NULL ) { +return -ENOMEM ; + } check.result->bfi.total_clusters = (s->header.image_size + s->header.cluster_size - 1) /
block/qed.c +5 -1
@@ -1208,7 +1208,11 @@ static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)struct iovec *iov = acb->qiov->iov;if (!iov->iov_base) { - iov->iov_base =qemu_blockalign (acb->common.bs, iov->iov_len); + iov->iov_base =qemu_try_blockalign (acb->common.bs, iov->iov_len); +if (iov->iov_base ==NULL ) { +qed_aio_complete (acb, -ENOMEM ); +return ; + }memset (iov->iov_base,0 , iov->iov_len); } }
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 12/20] qed: Handle failure for potentially large allocations
Quoted 11 lines · Kevin Wolf
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 qed block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/qed-check.c | 7 +++++-- block/qed.c | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-)
PATCH 13/20 raw-posix: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 raw-posix block driver.
--- block/raw-posix.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
block/raw-posix.c +5 -1
@@ -753,7 +753,11 @@ static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)* Ok, we have to do it the hard way, copy all segments into * a single aligned buffer. */ - buf =qemu_blockalign (aiocb->bs, aiocb->aio_nbytes); + buf =qemu_try_blockalign (aiocb->bs, aiocb->aio_nbytes); +if (buf ==NULL ) { +return -ENOMEM ; + } +if (aiocb->aio_type &QEMU_AIO_WRITE ) {char *p = buf;int i;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 13/20] raw-posix: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 raw-posix block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/raw-posix.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 14/20 raw-win32: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 raw-win32 block driver.
--- block/win32-aio.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
block/win32-aio.c +5 -1
@@ -138,7 +138,10 @@ BlockDriverAIOCB *win32_aio_submit(BlockDriverState *bs, waiocb->is_read = (type ==QEMU_AIO_READ );if (qiov->niov >1 ) { - waiocb->buf =qemu_blockalign (bs, qiov->size); + waiocb->buf =qemu_try_blockalign (bs, qiov->size); +if (waiocb->buf ==NULL ) { + goto out; + }if (type &QEMU_AIO_WRITE ) {iov_to_buf (qiov->iov, qiov->niov,0 , waiocb->buf, qiov->size); }
@@ -167,6 +170,7 @@ BlockDriverAIOCB *win32_aio_submit(BlockDriverState *bs, out_dec_count: aio->count--; +out:qemu_aio_release (waiocb);return NULL ; }
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 14/20] raw-win32: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 raw-win32 block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/win32-aio.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 15/20 rbd: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 rbd block driver.
--- block/rbd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
block/rbd.c +4 -1
@@ -630,7 +630,10 @@ static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs,if (cmd ==RBD_AIO_DISCARD || cmd ==RBD_AIO_FLUSH ) { acb->bounce =NULL ; }else { - acb->bounce =qemu_blockalign (bs, qiov->size); + acb->bounce =qemu_try_blockalign (bs, qiov->size); +if (acb->bounce ==NULL ) { + goto failed; + } } acb->ret =0 ; acb->error =0 ;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 15/20] rbd: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 rbd block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/rbd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
PATCH 16/20 vdi: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 vdi block driver.
--- block/vdi.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
block/vdi.c +17 -3
@@ -293,7 +293,12 @@ static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,return -ENOTSUP ; } - bmap =g_malloc (s->header.blocks_in_image *sizeof (uint32_t )); + bmap =g_try_malloc (s->header.blocks_in_image *sizeof (uint32_t )); +if (s->header.blocks_in_image && bmap ==NULL ) { + res->check_errors++; +return -ENOMEM ; + } +memset (bmap,0xff , s->header.blocks_in_image *sizeof (uint32_t ));/* Check block map and value of blocks_allocated. */
@@ -472,7 +477,12 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags, bmap_size = header.blocks_in_image *sizeof (uint32_t ); bmap_size = (bmap_size +SECTOR_SIZE -1 ) /SECTOR_SIZE ; - s->bmap =g_malloc (bmap_size *SECTOR_SIZE ); + s->bmap =qemu_try_blockalign (bs->file, bmap_size *SECTOR_SIZE ); +if (bmap_size && s->bmap ==NULL ) { + ret = -ENOMEM ; + goto fail; + } + ret =bdrv_read (bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size);if (ret <0 ) { goto fail_free_bmap;
@@ -760,7 +770,11 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options, }if (bmap_size >0 ) { -uint32_t *bmap =g_malloc0 (bmap_size); +uint32_t *bmap =g_try_malloc0 (bmap_size); +if (bmap ==NULL ) { + result = -ENOMEM ; + goto close_and_exit; + }for (i =0 ; i < blocks; i++) {if (image_type ==VDI_TYPE_STATIC ) { bmap[i] = i;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 16/20] vdi: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 vdi block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/vdi.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
PATCH 17/20 vhdx: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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.
--- block/vhdx-log.c | 6 +++++- block/vhdx.c | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)
block/vhdx-log.c +5 -1
@@ -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);
block/vhdx.c +10 -2
@@ -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;
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 17/20] vhdx: Handle failure for potentially large allocations
Quoted 11 lines · Kevin Wolf
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 <[email protected]>--- block/vhdx-log.c | 6 +++++- block/vhdx.c | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-)
PATCH 18/20 vmdk: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 vmdk block driver.
--- block/vmdk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
block/vmdk.c +10 -2
@@ -448,7 +448,11 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,/* read the L1 table */ l1_size = extent->l1_size *sizeof (uint32_t ); - extent->l1_table =g_malloc (l1_size); + extent->l1_table =g_try_malloc (l1_size); +if (l1_size && extent->l1_table ==NULL ) { +return -ENOMEM ; + } + ret = bdrv_pread(extent->file, extent->l1_table_offset, extent->l1_table,
@@ -464,7 +468,11 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent, }if (extent->l1_backup_table_offset) { - extent->l1_backup_table =g_malloc (l1_size); + extent->l1_backup_table =g_try_malloc (l1_size); +if (l1_size && extent->l1_backup_table ==NULL ) { + ret = -ENOMEM ; + goto fail_l1; + } ret = bdrv_pread(extent->file, extent->l1_backup_table_offset, extent->l1_backup_table,
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 18/20] vmdk: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 vmdk block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/vmdk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
PATCH 19/20 vpc: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 vpc block driver.
--- block/vpc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
block/vpc.c +5 -1
@@ -269,7 +269,11 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } - s->pagetable =qemu_blockalign (bs, s->max_table_entries *4 ); + s->pagetable =qemu_try_blockalign (bs, s->max_table_entries *4 ); +if (s->pagetable ==NULL ) { + ret = -ENOMEM ; + goto fail; + } s->bat_offset =be64_to_cpu (dyndisk_header->table_offset);
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 19/20] vpc: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 vpc block driver.
Signed-off-by Kevin Wolf <[email protected]>--- block/vpc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
PATCH 20/20 mirror: Handle failure for potentially large allocations
In reply to Kevin WolfSome 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 mirror block job.
--- block/mirror.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
block/mirror.c +6 -1
@@ -350,7 +350,12 @@ static void coroutine_fn mirror_run(void *opaque) } end = s->common.len >>BDRV_SECTOR_BITS ; - s->buf =qemu_blockalign (bs, s->buf_size); + s->buf =qemu_try_blockalign (bs, s->buf_size); +if (s->buf ==NULL ) { + ret = -ENOMEM ; + goto immediate_exit; + } + sectors_per_chunk = s->granularity >>BDRV_SECTOR_BITS ;mirror_free_init (s);
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 20/20] mirror: Handle failure for potentially large allocations
Quoted 10 lines · Kevin Wolf
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 mirror block job.
Signed-off-by Kevin Wolf <[email protected]>--- block/mirror.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)