qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Alberto Garcia <berto@igalia.com>, qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	qemu-block@nongnu.org, Max Reitz <mreitz@redhat.com>
Subject: Re: [PATCH v5 05/31] qcow2: Process QCOW2_CLUSTER_ZERO_ALLOC clusters in handle_copied()
Date: Tue, 5 May 2020 14:23:49 -0500	[thread overview]
Message-ID: <3579a7ff-0250-cadf-28cf-c840b1d51f9f@redhat.com> (raw)
In-Reply-To: <84fbd11fbfc4a2ee65a4cdca36976d4b18f10ef6.1588699789.git.berto@igalia.com>

On 5/5/20 12:38 PM, Alberto Garcia wrote:
> When writing to a qcow2 file there are two functions that take a
> virtual offset and return a host offset, possibly allocating new
> clusters if necessary:
> 
>     - handle_copied() looks for normal data clusters that are already
>       allocated and have a reference count of 1. In those clusters we
>       can simply write the data and there is no need to perform any
>       copy-on-write.
> 
>     - handle_alloc() looks for clusters that do need copy-on-write,
>       either because they haven't been allocated yet, because their
>       reference count is != 1 or because they are ZERO_ALLOC clusters.
> 
> The ZERO_ALLOC case is a bit special because those are clusters that
> are already allocated and they could perfectly be dealt with in
> handle_copied() (as long as copy-on-write is performed when required).
> 
> In fact, there is extra code specifically for them in handle_alloc()
> that tries to reuse the existing allocation if possible and frees them
> otherwise.
> 
> This patch changes the handling of ZERO_ALLOC clusters so the
> semantics of these two functions are now like this:
> 
>     - handle_copied() looks for clusters that are already allocated and
>       which we can overwrite (NORMAL and ZERO_ALLOC clusters with a
>       reference count of 1).
> 
>     - handle_alloc() looks for clusters for which we need a new
>       allocation (all other cases).
> 
> One important difference after this change is that clusters found
> in handle_copied() may now require copy-on-write, but this will be
> necessary anyway once we add support for subclusters.
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>   block/qcow2-cluster.c | 256 +++++++++++++++++++++++-------------------
>   1 file changed, 141 insertions(+), 115 deletions(-)

> @@ -1053,15 +1058,53 @@ void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
>   static void calculate_l2_meta(BlockDriverState *bs,
>                                 uint64_t host_cluster_offset,
>                                 uint64_t guest_offset, unsigned bytes,
> -                              QCowL2Meta **m, bool keep_old)
> +                              uint64_t *l2_slice, QCowL2Meta **m, bool keep_old)
>   {

Borderline long line, but it fits ;)

>       BDRVQcow2State *s = bs->opaque;
> -    unsigned cow_start_from = 0;
> +    int l2_index = offset_to_l2_slice_index(s, guest_offset);
> +    uint64_t l2_entry;
> +    unsigned cow_start_from, cow_end_to;
>       unsigned cow_start_to = offset_into_cluster(s, guest_offset);
>       unsigned cow_end_from = cow_start_to + bytes;
> -    unsigned cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);
>       unsigned nb_clusters = size_to_clusters(s, cow_end_from);
>       QCowL2Meta *old_m = *m;
> +    QCow2ClusterType type;
> +
> +    assert(nb_clusters <= s->l2_slice_size - l2_index);
> +
> +    /* Return if there's no COW (all clusters are normal and we keep them) */
> +    if (keep_old) {
> +        int i;
> +        for (i = 0; i < nb_clusters; i++) {
> +            l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
> +            if (qcow2_get_cluster_type(bs, l2_entry) != QCOW2_CLUSTER_NORMAL) {
> +                break;
> +            }
> +        }
> +        if (i == nb_clusters) {
> +            return;
> +        }
> +    }
> +
> +    /* Get the L2 entry of the first cluster */
> +    l2_entry = be64_to_cpu(l2_slice[l2_index]);

This is the second time we're grabbing the first entry in this function. 
But I don't think it's worth trying to micro-optimize.


> +static int count_single_write_clusters(BlockDriverState *bs, int nb_clusters,
> +                                       uint64_t *l2_slice, int l2_index,
> +                                       bool new_alloc)
>   {
> +    BDRVQcow2State *s = bs->opaque;
> +    uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index]);
> +    uint64_t expected_offset = l2_entry & L2E_OFFSET_MASK;
>       int i;
>   
>       for (i = 0; i < nb_clusters; i++) {
> -        uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
> -        if (!cluster_needs_cow(bs, l2_entry)) {
> +        l2_entry = be64_to_cpu(l2_slice[l2_index + i]);

And another place where we compute l2_entry for the first cluster twice, 
and again not worth micro-optimizing.

I didn't find anything that needs a change.

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org



  reply	other threads:[~2020-05-05 19:25 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-05 17:38 [PATCH v5 00/31] Add subcluster allocation to qcow2 Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 01/31] qcow2: Make Qcow2AioTask store the full host offset Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 02/31] qcow2: Convert qcow2_get_cluster_offset() into qcow2_get_host_offset() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 03/31] qcow2: Add calculate_l2_meta() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 04/31] qcow2: Split cluster_needs_cow() out of count_cow_clusters() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 05/31] qcow2: Process QCOW2_CLUSTER_ZERO_ALLOC clusters in handle_copied() Alberto Garcia
2020-05-05 19:23   ` Eric Blake [this message]
2020-05-05 17:38 ` [PATCH v5 06/31] qcow2: Add get_l2_entry() and set_l2_entry() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 07/31] qcow2: Document the Extended L2 Entries feature Alberto Garcia
2020-05-05 19:35   ` Eric Blake
2020-05-06 15:02     ` Alberto Garcia
2020-05-06 15:24       ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 08/31] qcow2: Add dummy has_subclusters() function Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 09/31] qcow2: Add subcluster-related fields to BDRVQcow2State Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 10/31] qcow2: Add offset_to_sc_index() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 11/31] qcow2: Add offset_into_subcluster() and size_to_subclusters() Alberto Garcia
2020-05-05 19:42   ` Eric Blake
2020-05-06 10:18     ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 12/31] qcow2: Add l2_entry_size() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 13/31] qcow2: Update get/set_l2_entry() and add get/set_l2_bitmap() Alberto Garcia
2020-05-05 20:04   ` Eric Blake
2020-05-06 12:06     ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 14/31] qcow2: Add QCow2SubclusterType and qcow2_get_subcluster_type() Alberto Garcia
2020-05-05 21:08   ` Eric Blake
2020-05-05 17:38 ` [PATCH v5 15/31] qcow2: Add qcow2_cluster_is_allocated() Alberto Garcia
2020-05-05 21:10   ` Eric Blake
2020-05-05 17:38 ` [PATCH v5 16/31] qcow2: Add cluster type parameter to qcow2_get_host_offset() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 17/31] qcow2: Replace QCOW2_CLUSTER_* with QCOW2_SUBCLUSTER_* Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 18/31] qcow2: Handle QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 19/31] qcow2: Add subcluster support to calculate_l2_meta() Alberto Garcia
2020-05-05 21:59   ` Eric Blake
2020-05-06 17:14     ` Alberto Garcia
2020-05-06 17:39       ` Eric Blake
2020-05-07 15:34         ` Alberto Garcia
2020-05-07 15:50           ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 20/31] qcow2: Add subcluster support to qcow2_get_host_offset() Alberto Garcia
2020-05-06 17:55   ` Eric Blake
2020-05-08 11:44     ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 21/31] qcow2: Add subcluster support to zero_in_l2_slice() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 22/31] qcow2: Add subcluster support to discard_in_l2_slice() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 23/31] qcow2: Add subcluster support to check_refcounts_l2() Alberto Garcia
2020-05-06 17:58   ` Eric Blake
2020-05-05 17:38 ` [PATCH v5 24/31] qcow2: Update L2 bitmap in qcow2_alloc_cluster_link_l2() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 25/31] qcow2: Clear the L2 bitmap when allocating a compressed cluster Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 26/31] qcow2: Add subcluster support to handle_alloc_space() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 27/31] qcow2: Add subcluster support to qcow2_co_pwrite_zeroes() Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 28/31] qcow2: Add the 'extended_l2' option and the QCOW2_INCOMPAT_EXTL2 bit Alberto Garcia
2020-05-06 18:09   ` Eric Blake
2020-05-07 14:37     ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 29/31] qcow2: Assert that expand_zero_clusters_in_l1() does not support subclusters Alberto Garcia
2020-05-06 18:11   ` Eric Blake
2020-05-05 17:38 ` [PATCH v5 30/31] qcow2: Add subcluster support to qcow2_measure() Alberto Garcia
2020-05-06 18:13   ` Eric Blake
2020-05-07 15:16     ` Alberto Garcia
2020-05-05 17:38 ` [PATCH v5 31/31] iotests: Add tests for qcow2 images with extended L2 entries Alberto Garcia
2020-05-20  9:35 ` [PATCH v5 00/31] Add subcluster allocation to qcow2 Derek Su
2020-05-20  9:50   ` Alberto Garcia

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=3579a7ff-0250-cadf-28cf-c840b1d51f9f@redhat.com \
    --to=eblake@redhat.com \
    --cc=berto@igalia.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.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 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).