Skip to content

Commit

Permalink
qed: Handle failure for potentially large allocations
Browse files Browse the repository at this point in the history
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]>
Reviewed-by: Stefan Hajnoczi <[email protected]>
Reviewed-by: Benoit Canet <[email protected]>
  • Loading branch information
kevmw committed Aug 15, 2014
1 parent de82815 commit 4f4896d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions block/qed-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) /
Expand Down
6 changes: 5 additions & 1 deletion block/qed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,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);
}
}
Expand Down

0 comments on commit 4f4896d

Please sign in to comment.