From nobody Mon Feb 9 23:39:11 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1494950318474687.7478335763312; Tue, 16 May 2017 08:58:38 -0700 (PDT) Received: from localhost ([::1]:42905 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAers-0006gH-2z for importer@patchew.org; Tue, 16 May 2017 11:58:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46801) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dAeoD-0003Us-0c for qemu-devel@nongnu.org; Tue, 16 May 2017 11:54:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dAeoB-0001df-L6 for qemu-devel@nongnu.org; Tue, 16 May 2017 11:54:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54454) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dAeo6-0001ZA-Q5; Tue, 16 May 2017 11:54:42 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D5703C04B927; Tue, 16 May 2017 15:54:35 +0000 (UTC) Received: from localhost (ovpn-116-10.phx2.redhat.com [10.3.116.10]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2A60AC09C0; Tue, 16 May 2017 15:54:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D5703C04B927 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jcody@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D5703C04B927 From: Jeff Cody To: qemu-block@nongnu.org Date: Tue, 16 May 2017 11:54:19 -0400 Message-Id: <20170516155420.10106-8-jcody@redhat.com> In-Reply-To: <20170516155420.10106-1-jcody@redhat.com> References: <20170516155420.10106-1-jcody@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 16 May 2017 15:54:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 7/8] curl: convert readv to coroutines X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, jcody@redhat.com, qemu-devel@nongnu.org, Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini 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. Reviewed-by: Jeff Cody Signed-off-by: Paolo Bonzini Reviewed-by: Max Reitz Message-id: 20170515100059.15795-7-pbonzini@redhat.com Signed-off-by: Jeff Cody --- block/curl.c | 92 ++++++++++++++++++++++++--------------------------------= ---- 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/block/curl.c b/block/curl.c index a71a428..1c04903 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 =20 -#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" @@ -94,11 +90,12 @@ static CURLMcode __curl_multi_socket_action(CURLM *mult= i_handle, struct BDRVCURLState; =20 typedef struct CURLAIOCB { - BlockAIOCB common; + Coroutine *co; QEMUIOVector *qiov; =20 uint64_t offset; uint64_t bytes; + int ret; =20 size_t start; size_t end; @@ -269,11 +266,11 @@ static size_t curl_read_cb(void *ptr, size_t size, si= ze_t nmemb, void *opaque) request_length - offset); } =20 + acb->ret =3D 0; + s->acb[i] =3D 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] =3D NULL; } } =20 @@ -283,8 +280,8 @@ read_end: } =20 /* 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 =3D start + len; @@ -313,7 +310,8 @@ static int curl_find_buf(BDRVCURLState *s, uint64_t sta= rt, uint64_t len, if (clamped_len < len) { qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped= _len); } - return FIND_RET_OK; + acb->ret =3D 0; + return true; } =20 // Wait for unfinished chunks @@ -331,13 +329,13 @@ static int curl_find_buf(BDRVCURLState *s, uint64_t s= tart, uint64_t len, for (j=3D0; jacb[j]) { state->acb[j] =3D acb; - return FIND_RET_WAIT; + return true; } } } } =20 - return FIND_RET_NONE; + return false; } =20 /* Called with s->mutex held. */ @@ -382,11 +380,11 @@ static void curl_multi_check_completion(BDRVCURLState= *s) continue; } =20 + acb->ret =3D -EIO; + state->acb[i] =3D 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] =3D NULL; } } =20 @@ -844,19 +842,11 @@ out_noclean: return -EINVAL; } =20 -static const AIOCBInfo curl_aiocb_info =3D { - .aiocb_size =3D 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 =3D -EINPROGRESS; =20 - CURLAIOCB *acb =3D p; - BlockDriverState *bs =3D acb->common.bs; BDRVCURLState *s =3D bs->opaque; =20 uint64_t start =3D acb->offset; @@ -866,14 +856,8 @@ static void curl_readv_bh_cb(void *p) =20 // 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 =3D 0; - goto out; - case FIND_RET_WAIT: - goto out; - default: - break; + if (curl_find_buf(s, start, acb->bytes, acb)) { + goto out; } =20 // No cache found, so let's start a new request @@ -889,7 +873,7 @@ static void curl_readv_bh_cb(void *p) =20 if (curl_init_state(s, state) < 0) { curl_clean_state(state); - ret =3D -EIO; + acb->ret =3D -EIO; goto out; } =20 @@ -904,7 +888,7 @@ static void curl_readv_bh_cb(void *p) state->orig_buf =3D g_try_malloc(state->buf_len); if (state->buf_len && state->orig_buf =3D=3D NULL) { curl_clean_state(state); - ret =3D -ENOMEM; + acb->ret =3D -ENOMEM; goto out; } state->acb[0] =3D acb; @@ -921,26 +905,24 @@ static void curl_readv_bh_cb(void *p) =20 out: qemu_mutex_unlock(&s->mutex); - if (ret !=3D -EINPROGRESS) { - acb->common.cb(acb->common.opaque, ret); - qemu_aio_unref(acb); - } } =20 -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; + CURLAIOCB acb =3D { + .co =3D qemu_coroutine_self(), + .ret =3D -EINPROGRESS, + .qiov =3D qiov, + .offset =3D offset, + .bytes =3D bytes + }; =20 - acb =3D qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); - - acb->qiov =3D qiov; - acb->offset =3D sector_num * BDRV_SECTOR_SIZE; - acb->bytes =3D nb_sectors * BDRV_SECTOR_SIZE; - - aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), curl_readv_bh_cb, ac= b); - return &acb->common; + curl_setup_preadv(bs, &acb); + while (acb.ret =3D=3D -EINPROGRESS) { + qemu_coroutine_yield(); + } + return acb.ret; } =20 static void curl_close(BlockDriverState *bs) @@ -971,7 +953,7 @@ static BlockDriver bdrv_http =3D { .bdrv_close =3D curl_close, .bdrv_getlength =3D curl_getlength, =20 - .bdrv_aio_readv =3D curl_aio_readv, + .bdrv_co_preadv =3D curl_co_preadv, =20 .bdrv_detach_aio_context =3D curl_detach_aio_context, .bdrv_attach_aio_context =3D curl_attach_aio_context, @@ -987,7 +969,7 @@ static BlockDriver bdrv_https =3D { .bdrv_close =3D curl_close, .bdrv_getlength =3D curl_getlength, =20 - .bdrv_aio_readv =3D curl_aio_readv, + .bdrv_co_preadv =3D curl_co_preadv, =20 .bdrv_detach_aio_context =3D curl_detach_aio_context, .bdrv_attach_aio_context =3D curl_attach_aio_context, @@ -1003,7 +985,7 @@ static BlockDriver bdrv_ftp =3D { .bdrv_close =3D curl_close, .bdrv_getlength =3D curl_getlength, =20 - .bdrv_aio_readv =3D curl_aio_readv, + .bdrv_co_preadv =3D curl_co_preadv, =20 .bdrv_detach_aio_context =3D curl_detach_aio_context, .bdrv_attach_aio_context =3D curl_attach_aio_context, @@ -1019,7 +1001,7 @@ static BlockDriver bdrv_ftps =3D { .bdrv_close =3D curl_close, .bdrv_getlength =3D curl_getlength, =20 - .bdrv_aio_readv =3D curl_aio_readv, + .bdrv_co_preadv =3D curl_co_preadv, =20 .bdrv_detach_aio_context =3D curl_detach_aio_context, .bdrv_attach_aio_context =3D curl_attach_aio_context, --=20 2.9.3