From patchwork Tue May 5 02:51:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 467903 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 873C014131A for ; Tue, 5 May 2015 12:53:13 +1000 (AEST) Received: from localhost ([::1]:36744 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpSyt-0003XG-Nz for incoming@patchwork.ozlabs.org; Mon, 04 May 2015 22:53:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59519) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpSxK-0000wq-VU for qemu-devel@nongnu.org; Mon, 04 May 2015 22:51:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YpSxJ-0005HI-SR for qemu-devel@nongnu.org; Mon, 04 May 2015 22:51:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60376) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YpSxJ-0005Gx-M3; Mon, 04 May 2015 22:51:33 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t452pX8c016313 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 4 May 2015 22:51:33 -0400 Received: from ad.nay.redhat.com (dhcp-14-137.nay.redhat.com [10.66.14.137]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t452pJIF032085; Mon, 4 May 2015 22:51:29 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Tue, 5 May 2015 10:51:14 +0800 Message-Id: <1430794275-1316-3-git-send-email-famz@redhat.com> In-Reply-To: <1430794275-1316-1-git-send-email-famz@redhat.com> References: <1430794275-1316-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , pbonzini@redhat.com, qemu-block@nongnu.org, qemu-stable@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v5 2/3] block: Fix NULL deference for unaligned write if qiov is NULL 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 For zero write, qiov passed by callers (qemu-io "write -z" and scsi-disk "write same") is NULL. Commit fc3959e466 fixed bdrv_co_write_zeroes which is the common case for this bug, but it still exists in bdrv_aio_write_zeroes. A simpler fix would be in bdrv_co_do_pwritev which is the NULL dereference point and covers both cases. So don't access it in bdrv_co_do_pwritev in this case, use three aligned writes. Signed-off-by: Fam Zheng --- block/io.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/block/io.c b/block/io.c index 4e5a92e..b4b587d 100644 --- a/block/io.c +++ b/block/io.c @@ -1214,6 +1214,8 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs, */ tracked_request_begin(&req, bs, offset, bytes, true); + assert(qiov || flags & BDRV_REQ_ZERO_WRITE); + if (offset & (align - 1)) { QEMUIOVector head_qiov; struct iovec head_iov; @@ -1236,13 +1238,39 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs, } BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD); - qemu_iovec_init(&local_qiov, qiov->niov + 2); - qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1)); - qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); - use_local_qiov = true; + if (qiov) { + qemu_iovec_init(&local_qiov, qiov ? qiov->niov + 2 : 1); + qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1)); + qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); + use_local_qiov = true; + bytes += offset & (align - 1); + offset = offset & ~(align - 1); + } else { + uint64_t local_off = offset & (align - 1); + uint64_t local_bytes = MIN(bytes, align - local_off); - bytes += offset & (align - 1); - offset = offset & ~(align - 1); + memset(head_buf + local_off, 0, local_bytes); + ret = bdrv_aligned_pwritev(bs, &req, offset & ~(align - 1), align, + &head_qiov, 0); + if (ret < 0) { + goto fail; + } + bytes -= local_bytes; + offset = ROUND_UP(offset, align); + } + } + + if (!qiov && bytes >= align) { + uint64_t aligned_bytes = bytes & ~(align - 1); + + assert((offset & (align - 1)) == 0); + ret = bdrv_aligned_pwritev(bs, &req, offset, aligned_bytes, + NULL, flags); + if (ret < 0) { + goto fail; + } + bytes -= aligned_bytes; + offset += aligned_bytes; } if ((offset + bytes) & (align - 1)) { @@ -1270,21 +1298,39 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs, } BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL); - if (!use_local_qiov) { - qemu_iovec_init(&local_qiov, qiov->niov + 1); - qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); - use_local_qiov = true; + if (qiov) { + if (!use_local_qiov) { + qemu_iovec_init(&local_qiov, qiov->niov + 1); + qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); + use_local_qiov = true; + } + + tail_bytes = (offset + bytes) & (align - 1); + qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, + align - tail_bytes); + + bytes = ROUND_UP(bytes, align); + } else { + assert((offset & (align - 1)) == 0); + assert(bytes < align); + + memset(tail_buf, 0, bytes & (align - 1)); + ret = bdrv_aligned_pwritev(bs, &req, offset, align, + &tail_qiov, 0); + if (ret < 0) { + goto fail; + } + offset += align; + bytes = 0; } - tail_bytes = (offset + bytes) & (align - 1); - qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes); - - bytes = ROUND_UP(bytes, align); } - ret = bdrv_aligned_pwritev(bs, &req, offset, bytes, - use_local_qiov ? &local_qiov : qiov, - flags); + if (bytes) { + ret = bdrv_aligned_pwritev(bs, &req, offset, bytes, + use_local_qiov ? &local_qiov : qiov, + flags); + } fail: tracked_request_end(&req);