All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Butsykin <pbutsykin@virtuozzo.com>
To: Stefan Hajnoczi <stefanha@gmail.com>, "Denis V. Lunev" <den@openvz.org>
Cc: Kevin Wolf <kwolf@redhat.com>, Jeff Cody <jcody@redhat.com>,
	qemu-devel@nongnu.org, Markus Armbruster <armbru@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	John Snow <jsnow@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 02/10] qcow2: add qcow2_co_write_compressed
Date: Mon, 30 May 2016 15:58:01 +0300	[thread overview]
Message-ID: <574C38D9.8020709@virtuozzo.com> (raw)
In-Reply-To: <574C0404.9040507@virtuozzo.com>

On 30.05.2016 12:12, Pavel Butsykin wrote:
> On 27.05.2016 20:33, Stefan Hajnoczi wrote:
>> On Sat, May 14, 2016 at 03:45:50PM +0300, Denis V. Lunev wrote:
>>> +    qemu_co_mutex_lock(&s->lock);
>>> +    cluster_offset = \
>>> +        qcow2_alloc_compressed_cluster_offset(bs, sector_num << 9,
>>> out_len);
>>
>> The backslash isn't necessary for wrapping lines in C.  This kind of
>> thing is only necessary in languages like Python where the grammar is
>> whitespace sensistive.
>>
>> The C compiler is happy with an arbitrary amount of whitespace
>> (newlines) in the middle of a statement.  The backslash in C is handled
>> by the preprocessor: it joins the line.  That's useful for macro
>> definitions where you need to tell the preprocessor that several lines
>> belong to one macro definition.  But it's not needed for normal C code.
>>
> Thanks for the explanation, but the backslash is used more for the
> person as a marker a line break. The current coding style misses this
> point, but I can remove the backslash, because I don't think it's
> something important :)
>
>>> +    if (!cluster_offset) {
>>> +        qemu_co_mutex_unlock(&s->lock);
>>> +        ret = -EIO;
>>> +        goto fail;
>>> +    }
>>> +    cluster_offset &= s->cluster_offset_mask;
>>>
>>> -        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
>>> -        ret = bdrv_pwrite(bs->file->bs, cluster_offset, out_buf,
>>> out_len);
>>> -        if (ret < 0) {
>>> -            goto fail;
>>> -        }
>>> +    ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset,
>>> out_len);
>>> +    qemu_co_mutex_unlock(&s->lock);
>>> +    if (ret < 0) {
>>> +        goto fail;
>>>       }
>>>
>>> +    iov = (struct iovec) {
>>> +        .iov_base   = out_buf,
>>> +        .iov_len    = out_len,
>>> +    };
>>> +    qemu_iovec_init_external(&hd_qiov, &iov, 1);
>>> +
>>> +    BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
>>> +    ret = bdrv_co_pwritev(bs->file->bs, cluster_offset, out_len,
>>> &hd_qiov, 0);
>>
>> There is a race condition here:
>>
>> If the newly allocated cluster is only partially filled by compressed
>> data then qcow2_alloc_compressed_cluster_offset() remembers that more
>> bytes are still available in the cluster.  The
>> qcow2_alloc_compressed_cluster_offset() caller will continue filling the
>> same cluster.
>>
>> Imagine two compressed writes running at the same time.  Write A
>> allocates just a few bytes so write B shares a sector with the first
>> write:

Sorry, but it seems this will never happen, because the second write
will not pass this check:

uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
                                                uint64_t offset,
                                                int compressed_size)
{
     ...
     /* Compression can't overwrite anything. Fail if the cluster was 
already
      * allocated. */
     cluster_offset = be64_to_cpu(l2_table[l2_index]);
     if (cluster_offset & L2E_OFFSET_MASK) {
         qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
         return 0;
     }
    ...

As you can see we can't do the compressed write in the already allocated
cluster.

>>
>>       Sector 1
>>    |AAABBBBBBBBB|
>>
>> The race condition is that bdrv_co_pwritev() uses read-modify-write (a
>> bounce buffer).  If both requests call bdrv_co_pwritev() around the same
>> time then the following could happen:
>>
>>       Sector 1
>>    |000BBBBBBBBB|
>>
>> or:
>>
>>       Sector 1
>>    |AAA000000000|
>>
>> It's necessary to hold s->lock around the compressed data write to avoid
>> this race condition.
>>
> I agree, there is really a race.. Thank you, this is a very good point!
>
>

  reply	other threads:[~2016-05-30 13:12 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-14 12:45 [Qemu-devel] [PATCH v3 00/10] backup compression Denis V. Lunev
2016-05-14 12:45 ` [Qemu-devel] [PATCH 01/10] block/io: add bdrv_co_write_compressed Denis V. Lunev
2016-05-16 16:52   ` Eric Blake
2016-05-17 15:01     ` Pavel Butsykin
2016-05-19 21:25   ` Stefan Hajnoczi
2016-05-19 21:39     ` Denis V. Lunev
2016-05-14 12:45 ` [Qemu-devel] [PATCH 02/10] qcow2: add qcow2_co_write_compressed Denis V. Lunev
2016-05-27 17:33   ` Stefan Hajnoczi
2016-05-30  9:12     ` Pavel Butsykin
2016-05-30 12:58       ` Pavel Butsykin [this message]
2016-05-31 18:42         ` Eric Blake
2016-05-31 21:00           ` Denis V. Lunev
2016-05-31 21:13             ` Eric Blake
2016-06-01  9:53               ` Pavel Butsykin
2016-06-01  9:31           ` Kevin Wolf
2016-06-01  9:25     ` Kevin Wolf
2016-06-01 20:06       ` Stefan Hajnoczi
2016-05-14 12:45 ` [Qemu-devel] [PATCH 03/10] vmdk: add vmdk_co_write_compressed Denis V. Lunev
2016-05-27 17:38   ` Stefan Hajnoczi
2016-05-14 12:45 ` [Qemu-devel] [PATCH 04/10] qcow: add qcow_co_write_compressed Denis V. Lunev
2016-05-27 17:45   ` Stefan Hajnoczi
2016-05-30 14:27     ` Pavel Butsykin
2016-05-14 12:45 ` [Qemu-devel] [PATCH 05/10] block: remove BlockDriver.bdrv_write_compressed Denis V. Lunev
2016-05-16 16:57   ` Eric Blake
2016-05-17 12:22     ` Pavel Butsykin
2016-05-14 12:45 ` [Qemu-devel] [PATCH 06/10] drive-backup: added support for data compression Denis V. Lunev
2016-05-16 16:59   ` Eric Blake
2016-05-27 17:56   ` Stefan Hajnoczi
2016-05-14 12:45 ` [Qemu-devel] [PATCH 07/10] blockdev-backup: " Denis V. Lunev
2016-05-16 17:00   ` Eric Blake
2016-05-27 17:57   ` Stefan Hajnoczi
2016-05-14 12:45 ` [Qemu-devel] [PATCH 08/10] qemu-iotests: test backup compression in 055 Denis V. Lunev
2016-05-14 12:45 ` [Qemu-devel] [PATCH 09/10] block: fix backup in vmdk format image Denis V. Lunev
2016-05-27 18:01   ` Stefan Hajnoczi

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=574C38D9.8020709@virtuozzo.com \
    --to=pbutsykin@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=den@openvz.org \
    --cc=jcody@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --cc=stefanha@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.