All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Max Reitz <mreitz@redhat.com>, qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2] qcow2: Rewrite qcow2_alloc_bytes()
Date: Thu, 05 Feb 2015 10:53:22 -0700	[thread overview]
Message-ID: <54D3AE12.4070306@redhat.com> (raw)
In-Reply-To: <1423151912-24863-1-git-send-email-mreitz@redhat.com>

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

On 02/05/2015 08:58 AM, Max Reitz wrote:
> qcow2_alloc_bytes() is a function with insufficient error handling and
> an unnecessary goto. This patch rewrites it.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
> v2:
> - s/free_cluster_index/free_byte_index/ [Eric]
> - added an assertion at the start of the function that
>   s->free_byte_offset is either 0 or points to the tail of a cluster
>   (but never to the start)
> - use ROUND_UP() instead of start_of_cluster() + cluster_size [Eric]
> - added an assertion that s->free_byte_offset is set before using it
>   [Eric]

Looks nicer.

> +        cluster_end = ROUND_UP(s->free_byte_offset, s->cluster_size);
> +        if (!s->free_byte_offset || cluster_end != new_cluster) {

Here, I can prove that the left side of the || makes no difference to
the outcome (using just the right half is sufficient, since
!s->free_byte_offset implies cluster_end == 0, but new_cluster will be
non-zero because we never allocate the header cluster). I'd be fine if
you wanted to respin that and add the comment for the micro-optimized
shorter code; but for maintenance purposes, it's easier to rely on the
explicit condition (even if redundant) than to think about whether a
proof listed in a comment is correct, so I'm also fine leaving things as is.

> +            s->free_byte_offset = new_cluster;

At this point, s->free_byte_offset violates the precondition if we just
allocated a cluster, so we have to make sure that we restore the
precondition before exiting...

> +    if (offset_into_cluster(s, s->free_byte_offset)) {
> +        int ret = qcow2_update_cluster_refcount(bs,
> +                s->free_byte_offset >> s->cluster_bits, 1,
> +                QCOW2_DISCARD_NEVER);
> +        if (ret < 0) {
> +            if (new_cluster > 0) {
> +                qcow2_free_clusters(bs, new_cluster, s->cluster_size,
> +                                    QCOW2_DISCARD_OTHER);
> +            }
> +            return ret;

...this early exit only happens if s->free_byte_offset was pointing to a
tail, and there are no other early exits, with the final exit properly
restoring the precondition.

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

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2015-02-05 17:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-05 15:58 [Qemu-devel] [PATCH v2] qcow2: Rewrite qcow2_alloc_bytes() Max Reitz
2015-02-05 17:53 ` Eric Blake [this message]
2015-02-06 14:08 ` Kevin Wolf
2015-02-06 14:15   ` Max Reitz

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=54D3AE12.4070306@redhat.com \
    --to=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --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.