[GEDI] [PATCH v3 07/17] block/io: support int64_t bytes in bdrv_co_do_copy_on_readv()

Eric Blake eblake at redhat.com
Thu May 21 22:29:53 UTC 2020


On 4/30/20 6:10 AM, Vladimir Sementsov-Ogievskiy wrote:
> We are generally moving to int64_t for both offset and bytes parameters
> on all io paths.
> 
> Main motivation is realization of 64-bit write_zeroes operation for
> fast zeroing large disk chunks, up to the whole disk.
> 
> We chose signed type, to be consistent with off_t (which is signed) and
> with possibility for signed return type (where negative value means
> error).
> 
> So, prepare bdrv_co_do_copy_on_readv() now.
> 
> Series: 64bit-block-status
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov at virtuozzo.com>
> ---
>   block/io.c         | 6 +++---
>   block/trace-events | 2 +-
>   2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/block/io.c b/block/io.c
> index 8bb4ea6285..6990d8cabe 100644
> --- a/block/io.c
> +++ b/block/io.c
> @@ -1088,7 +1088,7 @@ bdrv_driver_pwritev_compressed(BlockDriverState *bs, int64_t offset,
>   }
>   
>   static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
> -        int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
> +        int64_t offset, int64_t bytes, QEMUIOVector *qiov,
>           size_t qiov_offset, int flags)

Widens from 32-bit to 63-bit.  One caller:

bdrv_aligned_preadv() passes unsigned int (for now) - safe

>   {
>       BlockDriverState *bs = child->bs;
> @@ -1103,11 +1103,11 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
>       BlockDriver *drv = bs->drv;
>       int64_t cluster_offset;
>       int64_t cluster_bytes;
> -    size_t skip_bytes;
> +    int64_t skip_bytes;
>       int ret;
>       int max_transfer = MIN_NON_ZERO(bs->bl.max_transfer,
>                                       BDRV_REQUEST_MAX_BYTES);
> -    unsigned int progress = 0;
> +    int64_t progress = 0;
>       bool skip_write;

Use of 'bytes', 'sskip_bytes', and 'progress' within the function:
     bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, 
&cluster_bytes);
  - safe, takes int64_t. Pre-patch, cluster_bytes could be 33 bits (a 
misaligned request just under UINT_MAX can expand to > UINT_MAX when 
aligned to clusters), but only if bytes could be larger than our <2G cap 
that we use elsewhere.  But even if we relax that 2G cap in later 
patches, we should be okay even if 'bytes' starts at larger than 32 
bits, because we don't allow images that would overflow INT64_MAX when 
rounded up to cluster boundaries

     skip_bytes = offset - cluster_offset;
  - actually oversized - the difference is never going to be larger than 
a cluster (which is capped at 2M for qcow2, for example), but doesn't 
hurt that it is now a 64-bit value

     trace_bdrv_co_do_copy_on_readv(bs, offset, bytes,
  - safe, tweaked in this patch

                 assert(progress >= bytes);
  - safe: progress never exceeds pnum, and both variables are same type 
pre- and post-patch

             assert(skip_bytes < pnum);
  - safe

                 qemu_iovec_from_buf(qiov, qiov_offset + progress,
                                     bounce_buffer + skip_bytes,
                                     MIN(pnum - skip_bytes, bytes - 
progress));
  - tricky - pre-patch, pnum was int64_t, post-patch, we have three more 
int64_t entities.  Either way, we're passing int64_t to a size_t 
parameter, which narrows on 64-bit.  However, we're safe: this call is 
in a loop where pnum is clamped at MAX_BOUNCE_BUFFER which is less than 
32 bits, and the MIN() here means we never overflow

             ret = bdrv_driver_preadv(bs, offset + progress,
                                      MIN(pnum - skip_bytes, bytes - 
progress),
                                      qiov, qiov_offset + progress, 0);
  - safe - takes int64_t (earlier in this series), and same analysis 
about the MIN() picking something clamped at MAX_BOUNCE_BUFFER

         progress += pnum - skip_bytes;
         skip_bytes = 0;
  - safe

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

>   
>       if (!drv) {
> diff --git a/block/trace-events b/block/trace-events
> index 29dff8881c..179b47bf63 100644
> --- a/block/trace-events
> +++ b/block/trace-events
> @@ -14,7 +14,7 @@ blk_root_detach(void *child, void *blk, void *bs) "child %p blk %p bs %p"
>   bdrv_co_preadv(void *bs, int64_t offset, int64_t nbytes, unsigned int flags) "bs %p offset %"PRId64" nbytes %"PRId64" flags 0x%x"
>   bdrv_co_pwritev(void *bs, int64_t offset, int64_t nbytes, unsigned int flags) "bs %p offset %"PRId64" nbytes %"PRId64" flags 0x%x"
>   bdrv_co_pwrite_zeroes(void *bs, int64_t offset, int count, int flags) "bs %p offset %"PRId64" count %d flags 0x%x"
> -bdrv_co_do_copy_on_readv(void *bs, int64_t offset, unsigned int bytes, int64_t cluster_offset, int64_t cluster_bytes) "bs %p offset %"PRId64" bytes %u cluster_offset %"PRId64" cluster_bytes %"PRId64
> +bdrv_co_do_copy_on_readv(void *bs, int64_t offset, int64_t bytes, int64_t cluster_offset, int64_t cluster_bytes) "bs %p offset %" PRId64 " bytes %" PRId64 " cluster_offset %" PRId64 " cluster_bytes %" PRId64
>   bdrv_co_copy_range_from(void *src, uint64_t src_offset, void *dst, uint64_t dst_offset, uint64_t bytes, int read_flags, int write_flags) "src %p offset %"PRIu64" dst %p offset %"PRIu64" bytes %"PRIu64" rw flags 0x%x 0x%x"
>   bdrv_co_copy_range_to(void *src, uint64_t src_offset, void *dst, uint64_t dst_offset, uint64_t bytes, int read_flags, int write_flags) "src %p offset %"PRIu64" dst %p offset %"PRIu64" bytes %"PRIu64" rw flags 0x%x 0x%x"
>   
> 

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



More information about the integration mailing list