qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Cody <jcody@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-stable@nongnu.org,
	qemu-block@nongnu.org, rjones@redhat.com
Subject: Re: [Qemu-devel] [PATCH 6/7] curl: convert readv to coroutines
Date: Fri, 12 May 2017 17:40:39 -0400	[thread overview]
Message-ID: <20170512214039.GF19824@localhost.localdomain> (raw)
In-Reply-To: <20170510143205.32013-7-pbonzini@redhat.com>

On Wed, May 10, 2017 at 04:32:04PM +0200, Paolo Bonzini wrote:
> This is pretty simple.  The bottom half goes away because, unlike
> bdrv_aio_readv, coroutine-based read can return immediately without
> yielding.  However, for simplicity I kept the former bottom half
> handler in a separate function.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  block/curl.c | 94 ++++++++++++++++++++++++------------------------------------
>  1 file changed, 38 insertions(+), 56 deletions(-)
> 
> diff --git a/block/curl.c b/block/curl.c
> index 3e288f2bc7..80870bd60c 100644
> --- a/block/curl.c
> +++ b/block/curl.c
> @@ -76,10 +76,6 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
>  #define CURL_TIMEOUT_DEFAULT 5
>  #define CURL_TIMEOUT_MAX 10000
>  
> -#define FIND_RET_NONE   0
> -#define FIND_RET_OK     1
> -#define FIND_RET_WAIT   2
> -
>  #define CURL_BLOCK_OPT_URL       "url"
>  #define CURL_BLOCK_OPT_READAHEAD "readahead"
>  #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
> @@ -93,11 +89,12 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
>  struct BDRVCURLState;
>  
>  typedef struct CURLAIOCB {
> -    BlockAIOCB common;
> +    Coroutine *co;
>      QEMUIOVector *qiov;
>  
>      uint64_t offset;
>      uint64_t bytes;
> +    int ret;
>  
>      size_t start;
>      size_t end;
> @@ -268,11 +265,11 @@ static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
>                                    request_length - offset);
>              }
>  
> +            acb->ret = 0;
> +            s->acb[i] = NULL;
>              qemu_mutex_unlock(&s->s->mutex);
> -            acb->common.cb(acb->common.opaque, 0);
> +            aio_co_wake(acb->co);
>              qemu_mutex_lock(&s->s->mutex);
> -            qemu_aio_unref(acb);
> -            s->acb[i] = NULL;
>          }
>      }
>  
> @@ -282,8 +279,8 @@ read_end:
>  }
>  
>  /* Called with s->mutex held.  */
> -static int curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
> -                         CURLAIOCB *acb)
> +static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
> +                          CURLAIOCB *acb)
>  {
>      int i;
>      uint64_t end = start + len;
> @@ -312,7 +309,8 @@ static int curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
>              if (clamped_len < len) {
>                  qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
>              }
> -            return FIND_RET_OK;
> +            acb->ret = 0;
> +            return true;
>          }
>  
>          // Wait for unfinished chunks
> @@ -330,13 +328,13 @@ static int curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
>              for (j=0; j<CURL_NUM_ACB; j++) {
>                  if (!state->acb[j]) {
>                      state->acb[j] = acb;
> -                    return FIND_RET_WAIT;
> +                    return true;
>                  }
>              }
>          }
>      }
>  
> -    return FIND_RET_NONE;
> +    return false;
>  }
>  
>  /* Called with s->mutex held.  */
> @@ -381,11 +379,11 @@ static void curl_multi_check_completion(BDRVCURLState *s)
>                          continue;
>                      }
>  
> +                    acb->ret = -EIO;
> +                    state->acb[i] = NULL;
>                      qemu_mutex_unlock(&s->mutex);
> -                    acb->common.cb(acb->common.opaque, -EIO);
> +                    aio_co_wake(acb->co);
>                      qemu_mutex_lock(&s->mutex);
> -                    qemu_aio_unref(acb);
> -                    state->acb[i] = NULL;
>                  }
>              }
>  
> @@ -821,19 +819,11 @@ out_noclean:
>      return -EINVAL;
>  }
>  
> -static const AIOCBInfo curl_aiocb_info = {
> -    .aiocb_size         = sizeof(CURLAIOCB),
> -};
> -
> -
> -static void curl_readv_bh_cb(void *p)
> +static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
>  {
>      CURLState *state;
>      int running;
> -    int ret = -EINPROGRESS;
>  
> -    CURLAIOCB *acb = p;
> -    BlockDriverState *bs = acb->common.bs;
>      BDRVCURLState *s = bs->opaque;
>  
>      uint64_t start = acb->offset;
> @@ -843,14 +833,8 @@ static void curl_readv_bh_cb(void *p)
>  
>      // In case we have the requested data already (e.g. read-ahead),
>      // we can just call the callback and be done.
> -    switch (curl_find_buf(s, start, acb->bytes, acb)) {
> -        case FIND_RET_OK:
> -            ret = 0;
> -            goto out;
> -        case FIND_RET_WAIT:
> -            goto out;
> -        default:
> -            break;
> +    if (curl_find_buf(s, start, acb->bytes, acb)) {
> +        goto out;
>      }
>  
>      // No cache found, so let's start a new request
> @@ -866,7 +850,7 @@ static void curl_readv_bh_cb(void *p)
>  
>      if (curl_init_state(s, state) < 0) {
>          curl_clean_state(state);
> -        ret = -EIO;
> +        acb->ret = -EIO;
>          goto out;
>      }
>  
> @@ -881,7 +865,7 @@ static void curl_readv_bh_cb(void *p)
>      state->orig_buf = g_try_malloc(state->buf_len);
>      if (state->buf_len && state->orig_buf == NULL) {
>          curl_clean_state(state);
> -        ret = -ENOMEM;
> +        acb->ret = -ENOMEM;
>          goto out;
>      }
>      state->acb[0] = acb;
> @@ -898,26 +882,24 @@ static void curl_readv_bh_cb(void *p)
>  
>  out:
>      qemu_mutex_unlock(&s->mutex);
> -    if (ret != -EINPROGRESS) {
> -        acb->common.cb(acb->common.opaque, ret);
> -        qemu_aio_unref(acb);
> -    }
>  }
>  
> -static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
> -        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
> -        BlockCompletionFunc *cb, void *opaque)
> +static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
> +        uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
>  {
> -    CURLAIOCB *acb;
> -
> -    acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
> -
> -    acb->qiov = qiov;
> -    acb->offset = sector_num * BDRV_SECTOR_SIZE;
> -    acb->bytes = nb_sectors * BDRV_SECTOR_SIZE;
> -
> -    aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
> -    return &acb->common;
> +    CURLAIOCB acb = {
> +        .co = qemu_coroutine_self(),
> +        .ret = -EINPROGRESS,
> +        .qiov = qiov,
> +        .offset = offset,
> +        .bytes = bytes
> +    };
> +
> +    curl_setup_preadv(bs, &acb);
> +    while (acb.ret == -EINPROGRESS) {
> +        qemu_coroutine_yield();
> +    }
> +    return acb.ret;
>  }
>  
>  static void curl_close(BlockDriverState *bs)
> @@ -948,7 +930,7 @@ static BlockDriver bdrv_http = {
>      .bdrv_close                 = curl_close,
>      .bdrv_getlength             = curl_getlength,
>  
> -    .bdrv_aio_readv             = curl_aio_readv,
> +    .bdrv_co_preadv             = curl_co_preadv,
>  
>      .bdrv_detach_aio_context    = curl_detach_aio_context,
>      .bdrv_attach_aio_context    = curl_attach_aio_context,
> @@ -964,7 +946,7 @@ static BlockDriver bdrv_https = {
>      .bdrv_close                 = curl_close,
>      .bdrv_getlength             = curl_getlength,
>  
> -    .bdrv_aio_readv             = curl_aio_readv,
> +    .bdrv_co_preadv             = curl_co_preadv,
>  
>      .bdrv_detach_aio_context    = curl_detach_aio_context,
>      .bdrv_attach_aio_context    = curl_attach_aio_context,
> @@ -980,7 +962,7 @@ static BlockDriver bdrv_ftp = {
>      .bdrv_close                 = curl_close,
>      .bdrv_getlength             = curl_getlength,
>  
> -    .bdrv_aio_readv             = curl_aio_readv,
> +    .bdrv_co_preadv             = curl_co_preadv,
>  
>      .bdrv_detach_aio_context    = curl_detach_aio_context,
>      .bdrv_attach_aio_context    = curl_attach_aio_context,
> @@ -996,7 +978,7 @@ static BlockDriver bdrv_ftps = {
>      .bdrv_close                 = curl_close,
>      .bdrv_getlength             = curl_getlength,
>  
> -    .bdrv_aio_readv             = curl_aio_readv,
> +    .bdrv_co_preadv             = curl_co_preadv,
>  
>      .bdrv_detach_aio_context    = curl_detach_aio_context,
>      .bdrv_attach_aio_context    = curl_attach_aio_context,
> -- 
> 2.12.2
> 
>

Reviewed-by: Jeff Cody <jcody@redhat.com>

  reply	other threads:[~2017-05-12 21:40 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-10 14:31 [Qemu-devel] [PATCH v2 0/7] curl: locking cleanups/fixes, coroutine conversion, remove aio_poll Paolo Bonzini
2017-05-10 14:31 ` [Qemu-devel] [PATCH 1/7] curl: strengthen assertion in curl_clean_state Paolo Bonzini
2017-05-10 16:33   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-11 20:35   ` [Qemu-devel] " Jeff Cody
2017-05-10 14:32 ` [Qemu-devel] [PATCH 2/7] curl: never invoke callbacks with s->mutex held Paolo Bonzini
2017-05-10 16:33   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-11 20:40   ` [Qemu-devel] " Jeff Cody
2017-05-10 14:32 ` [Qemu-devel] [PATCH 3/7] curl: avoid recursive locking of BDRVCURLState mutex Paolo Bonzini
2017-05-10 16:38   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-11 20:56   ` [Qemu-devel] " Jeff Cody
2017-05-12 14:48     ` Paolo Bonzini
2017-05-10 14:32 ` [Qemu-devel] [PATCH 4/7] curl: split curl_find_state/curl_init_state Paolo Bonzini
2017-05-10 17:26   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-11 13:49     ` [Qemu-devel] " Paolo Bonzini
2017-05-12 21:38   ` Jeff Cody
2017-05-10 14:32 ` [Qemu-devel] [PATCH 5/7] curl: convert CURLAIOCB to byte values Paolo Bonzini
2017-05-10 17:36   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-10 18:37     ` Eric Blake
2017-05-12 21:38   ` [Qemu-devel] " Jeff Cody
2017-05-10 14:32 ` [Qemu-devel] [PATCH 6/7] curl: convert readv to coroutines Paolo Bonzini
2017-05-12 21:40   ` Jeff Cody [this message]
2017-05-10 14:32 ` [Qemu-devel] [PATCH 7/7] curl: do not do aio_poll when waiting for a free CURLState Paolo Bonzini
2017-05-10 17:54   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-12 21:41   ` [Qemu-devel] " Jeff Cody
2017-05-10 15:11 ` [Qemu-devel] [PATCH v2 0/7] curl: locking cleanups/fixes, coroutine conversion, remove aio_poll no-reply
2017-05-10 15:57 ` Richard W.M. Jones
2017-05-15 19:12   ` [Qemu-devel] [Qemu-block] " Max Reitz
2017-05-15 20:30     ` Richard W.M. Jones

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=20170512214039.GF19824@localhost.localdomain \
    --to=jcody@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=rjones@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 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).