|
|
9ae3a8 |
From 22db646a6d358e08c4c11f12e3dcf96f25525bf8 Mon Sep 17 00:00:00 2001
|
|
|
9ae3a8 |
From: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
Date: Mon, 25 May 2015 04:45:56 +0200
|
|
|
9ae3a8 |
Subject: [PATCH 4/6] block: Fix NULL deference for unaligned write if qiov is
|
|
|
9ae3a8 |
NULL
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Message-id: <1432529157-20381-3-git-send-email-famz@redhat.com>
|
|
|
9ae3a8 |
Patchwork-id: 65120
|
|
|
9ae3a8 |
O-Subject: [RHEL-7.2 qemu-kvm PATCH v2 2/3] block: Fix NULL deference for unaligned write if qiov is NULL
|
|
|
9ae3a8 |
Bugzilla: 1200295
|
|
|
9ae3a8 |
RH-Acked-by: Max Reitz <mreitz@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
For zero write, callers pass in NULL qiov (qemu-io "write -z" or
|
|
|
9ae3a8 |
scsi-disk "write same").
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Commit fc3959e466 fixed bdrv_co_write_zeroes which is the common case
|
|
|
9ae3a8 |
for this bug, but it still exists in bdrv_aio_write_zeroes. A simpler
|
|
|
9ae3a8 |
fix would be in bdrv_co_do_pwritev which is the NULL dereference point
|
|
|
9ae3a8 |
and covers both cases.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
So don't access it in bdrv_co_do_pwritev in this case, use three aligned
|
|
|
9ae3a8 |
writes.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
[Initialize ret to 0 in bdrv_co_do_zero_pwritev() to avoid uninitialized
|
|
|
9ae3a8 |
variable warning with gcc 4.9.2.
|
|
|
9ae3a8 |
--Stefan]
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
Message-id: 1431522721-3266-3-git-send-email-famz@redhat.com
|
|
|
9ae3a8 |
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
(cherry picked from commit 9eeb6dd1b27bd57eb4e3869290e87feac8e8b226)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
We don't have block/io.c in downstream, applied the change to
|
|
|
9ae3a8 |
block.c
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Fam Zheng <famz@redhat.com>
|
|
|
9ae3a8 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
block.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
|
|
|
9ae3a8 |
1 file changed, 95 insertions(+), 2 deletions(-)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
diff --git a/block.c b/block.c
|
|
|
9ae3a8 |
index 89ab829..45543d5 100644
|
|
|
9ae3a8 |
--- a/block.c
|
|
|
9ae3a8 |
+++ b/block.c
|
|
|
9ae3a8 |
@@ -3069,6 +3069,94 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,
|
|
|
9ae3a8 |
return ret;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+static int coroutine_fn bdrv_co_do_zero_pwritev(BlockDriverState *bs,
|
|
|
9ae3a8 |
+ int64_t offset,
|
|
|
9ae3a8 |
+ unsigned int bytes,
|
|
|
9ae3a8 |
+ BdrvRequestFlags flags,
|
|
|
9ae3a8 |
+ BdrvTrackedRequest *req)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ uint8_t *buf = NULL;
|
|
|
9ae3a8 |
+ QEMUIOVector local_qiov;
|
|
|
9ae3a8 |
+ struct iovec iov;
|
|
|
9ae3a8 |
+ uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
|
|
|
9ae3a8 |
+ unsigned int head_padding_bytes, tail_padding_bytes;
|
|
|
9ae3a8 |
+ int ret = 0;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ head_padding_bytes = offset & (align - 1);
|
|
|
9ae3a8 |
+ tail_padding_bytes = align - ((offset + bytes) & (align - 1));
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ assert(flags & BDRV_REQ_ZERO_WRITE);
|
|
|
9ae3a8 |
+ if (head_padding_bytes || tail_padding_bytes) {
|
|
|
9ae3a8 |
+ buf = qemu_blockalign(bs, align);
|
|
|
9ae3a8 |
+ iov = (struct iovec) {
|
|
|
9ae3a8 |
+ .iov_base = buf,
|
|
|
9ae3a8 |
+ .iov_len = align,
|
|
|
9ae3a8 |
+ };
|
|
|
9ae3a8 |
+ qemu_iovec_init_external(&local_qiov, &iov, 1);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ if (head_padding_bytes) {
|
|
|
9ae3a8 |
+ uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* RMW the unaligned part before head. */
|
|
|
9ae3a8 |
+ mark_request_serialising(req, align);
|
|
|
9ae3a8 |
+ wait_serialising_requests(req);
|
|
|
9ae3a8 |
+ BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_HEAD);
|
|
|
9ae3a8 |
+ ret = bdrv_aligned_preadv(bs, req, offset & ~(align - 1), align,
|
|
|
9ae3a8 |
+ align, &local_qiov, 0);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ goto fail;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ memset(buf + head_padding_bytes, 0, zero_bytes);
|
|
|
9ae3a8 |
+ ret = bdrv_aligned_pwritev(bs, req, offset & ~(align - 1), align,
|
|
|
9ae3a8 |
+ &local_qiov,
|
|
|
9ae3a8 |
+ flags & ~BDRV_REQ_ZERO_WRITE);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ goto fail;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ offset += zero_bytes;
|
|
|
9ae3a8 |
+ bytes -= zero_bytes;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ assert(!bytes || (offset & (align - 1)) == 0);
|
|
|
9ae3a8 |
+ if (bytes >= align) {
|
|
|
9ae3a8 |
+ /* Write the aligned part in the middle. */
|
|
|
9ae3a8 |
+ uint64_t aligned_bytes = bytes & ~(align - 1);
|
|
|
9ae3a8 |
+ ret = bdrv_aligned_pwritev(bs, req, offset, aligned_bytes,
|
|
|
9ae3a8 |
+ NULL, flags);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ goto fail;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ bytes -= aligned_bytes;
|
|
|
9ae3a8 |
+ offset += aligned_bytes;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ assert(!bytes || (offset & (align - 1)) == 0);
|
|
|
9ae3a8 |
+ if (bytes) {
|
|
|
9ae3a8 |
+ assert(align == tail_padding_bytes + bytes);
|
|
|
9ae3a8 |
+ /* RMW the unaligned part after tail. */
|
|
|
9ae3a8 |
+ mark_request_serialising(req, align);
|
|
|
9ae3a8 |
+ wait_serialising_requests(req);
|
|
|
9ae3a8 |
+ BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_TAIL);
|
|
|
9ae3a8 |
+ ret = bdrv_aligned_preadv(bs, req, offset, align,
|
|
|
9ae3a8 |
+ align, &local_qiov, 0);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ goto fail;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ memset(buf, 0, bytes);
|
|
|
9ae3a8 |
+ ret = bdrv_aligned_pwritev(bs, req, offset, align,
|
|
|
9ae3a8 |
+ &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+fail:
|
|
|
9ae3a8 |
+ qemu_vfree(buf);
|
|
|
9ae3a8 |
+ return ret;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
/*
|
|
|
9ae3a8 |
* Handle a write request in coroutine context
|
|
|
9ae3a8 |
*/
|
|
|
9ae3a8 |
@@ -3108,6 +3196,11 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
|
|
|
9ae3a8 |
*/
|
|
|
9ae3a8 |
tracked_request_begin(&req, bs, offset, bytes, true);
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+ if (!qiov) {
|
|
|
9ae3a8 |
+ ret = bdrv_co_do_zero_pwritev(bs, offset, bytes, flags, &req;;
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
if (offset & (align - 1)) {
|
|
|
9ae3a8 |
QEMUIOVector head_qiov;
|
|
|
9ae3a8 |
struct iovec head_iov;
|
|
|
9ae3a8 |
@@ -3181,14 +3274,14 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
|
|
|
9ae3a8 |
flags);
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
fail:
|
|
|
9ae3a8 |
- tracked_request_end(&req;;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
if (use_local_qiov) {
|
|
|
9ae3a8 |
qemu_iovec_destroy(&local_qiov);
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
qemu_vfree(head_buf);
|
|
|
9ae3a8 |
qemu_vfree(tail_buf);
|
|
|
9ae3a8 |
-
|
|
|
9ae3a8 |
+out:
|
|
|
9ae3a8 |
+ tracked_request_end(&req;;
|
|
|
9ae3a8 |
return ret;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
--
|
|
|
9ae3a8 |
1.8.3.1
|
|
|
9ae3a8 |
|