qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Fam Zheng <famz@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	pbonzini@redhat.com, qemu-devel@nongnu.org,
	qemu-block@nongnu.org, qemu-stable@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v6 2/3] block: Fix NULL deference for unaligned write if qiov is NULL
Date: Tue, 12 May 2015 13:18:38 +0100	[thread overview]
Message-ID: <20150512121838.GC11497@stefanha-thinkpad.redhat.com> (raw)
In-Reply-To: <1431410972-13087-3-git-send-email-famz@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 3617 bytes --]

On Tue, May 12, 2015 at 02:09:31PM +0800, Fam Zheng wrote:
> +static int coroutine_fn bdrv_co_do_zero_pwritev(BlockDriverState *bs,
> +                                                int64_t offset,
> +                                                unsigned int bytes,
> +                                                BdrvRequestFlags flags)
> +{
> +    BdrvTrackedRequest req;
> +    uint8_t *buf = NULL;
> +    QEMUIOVector local_qiov;
> +    struct iovec iov;
> +    uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
> +    unsigned int head_padding_bytes, tail_padding_bytes;
> +    int ret;
> +
> +    head_padding_bytes = offset & (align - 1);
> +    tail_padding_bytes = align - ((offset + bytes) & (align - 1));
> +    tracked_request_begin(&req, bs, offset, bytes, true);
> +
> +    mark_request_serialising(&req, align);

This is only necessary if (head_padding_bytes || tail_padding_bytes).
Serialized requests are more expensive so let's only do it when
necessary.

> +    wait_serialising_requests(&req);
> +
> +    assert(flags & BDRV_REQ_ZERO_WRITE);
> +    if (head_padding_bytes || tail_padding_bytes) {
> +        buf = qemu_blockalign(bs, align);
> +        iov = (struct iovec) {
> +            .iov_base   = buf,
> +            .iov_len    = align,
> +        };
> +        qemu_iovec_init_external(&local_qiov, &iov, 1);
> +    }
> +    if (head_padding_bytes) {
> +        uint64_t zero_bytes = MIN(bytes, align - head_padding_bytes);
> +
> +        /* RMW the unaligned part before head. */
> +        BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_HEAD);
> +        ret = bdrv_aligned_preadv(bs, &req, offset & ~(align - 1), align,
> +                                  align, &local_qiov, 0);
> +        if (ret < 0) {
> +            goto fail;
> +        }
> +        BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD);
> +
> +        memset(buf + head_padding_bytes, 0, zero_bytes);
> +        ret = bdrv_aligned_pwritev(bs, &req, offset & ~(align - 1), align,
> +                                   &local_qiov,
> +                                   flags & ~BDRV_REQ_ZERO_WRITE);
> +        if (ret < 0) {
> +            goto fail;
> +        }
> +        offset += zero_bytes;
> +        bytes -= zero_bytes;
> +    }
> +
> +    assert((offset & (align - 1)) == 0);
> +    if (bytes >= align) {
> +        /* Write the aligned part in the middle. */
> +        uint64_t aligned_bytes = bytes & ~(align - 1);
> +        ret = bdrv_aligned_pwritev(bs, &req, offset, aligned_bytes,
> +                                   NULL, flags);
> +        if (ret < 0) {
> +            goto fail;
> +        }
> +        bytes -= aligned_bytes;
> +        offset += aligned_bytes;
> +    }
> +
> +    assert((offset & (align - 1)) == 0);
> +    if (bytes) {
> +        assert(align == tail_padding_bytes + bytes);
> +        /* RMW the unaligned part after tail. */
> +        BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_TAIL);
> +        ret = bdrv_aligned_preadv(bs, &req, offset, align,
> +                                  align, &local_qiov, 0);
> +        if (ret < 0) {
> +            goto fail;
> +        }
> +        BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL);
> +
> +        memset(buf, 0, bytes);
> +        printf("tail part %ld %d\n", offset, bytes);

Please drop the debug printf.

> +        ret = bdrv_aligned_pwritev(bs, &req, offset, align,
> +                                   &local_qiov, flags & ~BDRV_REQ_ZERO_WRITE);
> +    }
> +fail:
> +    tracked_request_end(&req);
> +    if (buf) {
> +        qemu_vfree(buf);
> +    }

if (buf) is unnecessary.  qemu_vfree(NULL) is a nop.

[-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --]

  parent reply	other threads:[~2015-05-12 12:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-12  6:09 [Qemu-devel] [PATCH v6 0/3] block: Fix unaligned bdrv_aio_write_zeroes Fam Zheng
2015-05-12  6:09 ` [Qemu-devel] [PATCH v6 1/3] Revert "block: Fix unaligned zero write" Fam Zheng
2015-05-12 11:52   ` Kevin Wolf
2015-05-12  6:09 ` [Qemu-devel] [PATCH v6 2/3] block: Fix NULL deference for unaligned write if qiov is NULL Fam Zheng
2015-05-12 11:52   ` Kevin Wolf
2015-05-13  5:03     ` Fam Zheng
2015-05-12 12:18   ` Stefan Hajnoczi [this message]
2015-05-13  5:03     ` Fam Zheng
2015-05-12  6:09 ` [Qemu-devel] [PATCH v6 3/3] qemu-iotests: Test unaligned sub-block zero write Fam Zheng
2015-05-12 11:54   ` Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150512121838.GC11497@stefanha-thinkpad.redhat.com \
    --to=stefanha@redhat.com \
    --cc=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).