From nobody Mon Apr 29 16:15:48 2024 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 1494322648514552.9912131520153; Tue, 9 May 2017 02:37:28 -0700 (PDT) Received: from localhost ([::1]:36091 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81aA-0005tc-Kq for importer@patchew.org; Tue, 09 May 2017 05:37:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34883) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81Yl-0004wJ-Nu for qemu-devel@nongnu.org; Tue, 09 May 2017 05:36:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d81Yh-0004p0-DR for qemu-devel@nongnu.org; Tue, 09 May 2017 05:35:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53066) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d81Yh-0004nt-7U; Tue, 09 May 2017 05:35:55 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 269938124F; Tue, 9 May 2017 09:35:54 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-23.ams2.redhat.com [10.36.117.23]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v499Zo7J015222; Tue, 9 May 2017 05:35:52 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 269938124F Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 269938124F From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 9 May 2017 11:35:46 +0200 Message-Id: <20170509093549.25157-2-pbonzini@redhat.com> In-Reply-To: <20170509093549.25157-1-pbonzini@redhat.com> References: <20170509093549.25157-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 09 May 2017 09:35:54 +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] [PATCH 1/4] curl: strengthen assertion in curl_clean_state 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: jcody@redhat.com, qemu-stable@nongnu.org, rjones@redhat.com 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" curl_clean_state should only be called after all AIOCBs have been completed. This is not so obvious for the call from curl_detach_aio_contex= t, so assert that. Cc: qemu-stable@nongnu.org Cc: jcody@redhat.com Signed-off-by: Paolo Bonzini --- block/curl.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/block/curl.c b/block/curl.c index 2708d57c2f..25a301e7b4 100644 --- a/block/curl.c +++ b/block/curl.c @@ -532,6 +532,11 @@ static CURLState *curl_init_state(BlockDriverState *bs= , BDRVCURLState *s) =20 static void curl_clean_state(CURLState *s) { + int j; + for (j =3D 0; j < CURL_NUM_ACB; j++) { + assert(!s->acb[j]); + } + if (s->s->multi) curl_multi_remove_handle(s->s->multi, s->curl); =20 --=20 2.12.2 From nobody Mon Apr 29 16:15:48 2024 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 1494322748260698.568220532311; Tue, 9 May 2017 02:39:08 -0700 (PDT) Received: from localhost ([::1]:36101 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81bm-0007hm-NB for importer@patchew.org; Tue, 09 May 2017 05:39:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81Yl-0004wI-Ns for qemu-devel@nongnu.org; Tue, 09 May 2017 05:36:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d81Yi-0004qs-UU for qemu-devel@nongnu.org; Tue, 09 May 2017 05:35:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53082) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d81Yi-0004pl-Lk; Tue, 09 May 2017 05:35:56 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9C3AB81240; Tue, 9 May 2017 09:35:55 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-23.ams2.redhat.com [10.36.117.23]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v499Zo7K015222; Tue, 9 May 2017 05:35:54 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9C3AB81240 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 9C3AB81240 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 9 May 2017 11:35:47 +0200 Message-Id: <20170509093549.25157-3-pbonzini@redhat.com> In-Reply-To: <20170509093549.25157-1-pbonzini@redhat.com> References: <20170509093549.25157-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 09 May 2017 09:35:55 +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] [PATCH 2/4] curl: never invoke callbacks with s->mutex held 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: jcody@redhat.com, qemu-stable@nongnu.org, rjones@redhat.com 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" All curl callbacks go through curl_multi_do, and hence are called with s->mutex held. Note that with comments, and make curl_read_cb drop the lock before invoking the callback. Likewise for curl_find_buf, where the callback can be invoked by the caller. Cc: qemu-stable@nongnu.org Cc: jcody@redhat.com Signed-off-by: Paolo Bonzini --- block/curl.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/block/curl.c b/block/curl.c index 25a301e7b4..a362c1b2a6 100644 --- a/block/curl.c +++ b/block/curl.c @@ -147,6 +147,7 @@ static void curl_multi_do(void *arg); static void curl_multi_read(void *arg); =20 #ifdef NEED_CURL_TIMER_CALLBACK +/* Called from curl_multi_do_locked, with s->mutex held. */ static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) { BDRVCURLState *s =3D opaque; @@ -163,6 +164,7 @@ static int curl_timer_cb(CURLM *multi, long timeout_ms,= void *opaque) } #endif =20 +/* Called from curl_multi_do_locked, with s->mutex held. */ static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, void *userp, void *sp) { @@ -212,6 +214,7 @@ static int curl_sock_cb(CURL *curl, curl_socket_t fd, i= nt action, return 0; } =20 +/* Called from curl_multi_do_locked, with s->mutex held. */ static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *o= paque) { BDRVCURLState *s =3D opaque; @@ -226,6 +229,7 @@ static size_t curl_header_cb(void *ptr, size_t size, si= ze_t nmemb, void *opaque) return realsize; } =20 +/* Called from curl_multi_do_locked, with s->mutex held. */ static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opa= que) { CURLState *s =3D ((CURLState*)opaque); @@ -264,7 +268,9 @@ static size_t curl_read_cb(void *ptr, size_t size, size= _t nmemb, void *opaque) request_length - offset); } =20 + qemu_mutex_unlock(&s->s->mutex); acb->common.cb(acb->common.opaque, 0); + qemu_mutex_lock(&s->s->mutex); qemu_aio_unref(acb); s->acb[i] =3D NULL; } @@ -305,8 +311,6 @@ static int curl_find_buf(BDRVCURLState *s, size_t start= , size_t len, if (clamped_len < len) { qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped= _len); } - acb->common.cb(acb->common.opaque, 0); - return FIND_RET_OK; } =20 @@ -832,8 +836,8 @@ static void curl_readv_bh_cb(void *p) // we can just call the callback and be done. switch (curl_find_buf(s, start, acb->nb_sectors * BDRV_SECTOR_SIZE, ac= b)) { case FIND_RET_OK: - qemu_aio_unref(acb); - // fall through + ret =3D 0; + goto out; case FIND_RET_WAIT: goto out; default: --=20 2.12.2 From nobody Mon Apr 29 16:15:48 2024 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 1494322839870217.40576074901276; Tue, 9 May 2017 02:40:39 -0700 (PDT) Received: from localhost ([::1]:36117 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81dG-0000RM-84 for importer@patchew.org; Tue, 09 May 2017 05:40:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81Yl-0004wL-O0 for qemu-devel@nongnu.org; Tue, 09 May 2017 05:36:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d81Yk-0004sV-Cv for qemu-devel@nongnu.org; Tue, 09 May 2017 05:35:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54210) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d81Yk-0004rS-3q; Tue, 09 May 2017 05:35:58 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 123BE4E334; Tue, 9 May 2017 09:35:57 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-23.ams2.redhat.com [10.36.117.23]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v499Zo7L015222; Tue, 9 May 2017 05:35:55 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 123BE4E334 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 123BE4E334 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 9 May 2017 11:35:48 +0200 Message-Id: <20170509093549.25157-4-pbonzini@redhat.com> In-Reply-To: <20170509093549.25157-1-pbonzini@redhat.com> References: <20170509093549.25157-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 09 May 2017 09:35:57 +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] [PATCH 3/4] curl: avoid recursive locking of BDRVCURLState mutex 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: jcody@redhat.com, qemu-stable@nongnu.org, rjones@redhat.com 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" The curl driver has a ugly hack where, if it cannot find an empty CURLState, it just uses aio_poll to wait for one to be empty. This is probably buggy when used together with dataplane, and a fix is in the next patch. A more immediate effect of the bug however is that it can cause a recursive call to curl_readv_bh_cb and recursively taking the BDRVCURLState mutex. This causes a deadlock. The fix is to unlock the mutex around aio_poll, but for cleanliness we should also take the mutex around all calls to curl_init_state, even if reaching the unlock/lock pair is impossible. The same is true for curl_clean_state. Reported-by: Richard W.M. Jones Cc: jcody@redhat.com Cc: qemu-stable@nongnu.org Signed-off-by: Paolo Bonzini --- block/curl.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/block/curl.c b/block/curl.c index a362c1b2a6..e8fcc5ca34 100644 --- a/block/curl.c +++ b/block/curl.c @@ -281,6 +281,7 @@ read_end: return size * nmemb; } =20 +/* Called with s->mutex held. */ static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, CURLAIOCB *acb) { @@ -453,6 +454,7 @@ static void curl_multi_timeout_do(void *arg) #endif } =20 +/* Called with s->mutex held. */ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s) { CURLState *state =3D NULL; @@ -471,7 +473,9 @@ static CURLState *curl_init_state(BlockDriverState *bs,= BDRVCURLState *s) break; } if (!state) { + qemu_mutex_unlock(&s->mutex); aio_poll(bdrv_get_aio_context(bs), true); + qemu_mutex_lock(&s->mutex); } } while(!state); =20 @@ -534,6 +538,7 @@ static CURLState *curl_init_state(BlockDriverState *bs,= BDRVCURLState *s) return state; } =20 +/* Called with s->mutex held. */ static void curl_clean_state(CURLState *s) { int j; @@ -565,6 +570,7 @@ static void curl_detach_aio_context(BlockDriverState *b= s) BDRVCURLState *s =3D bs->opaque; int i; =20 + qemu_mutex_lock(&s->mutex); for (i =3D 0; i < CURL_NUM_STATES; i++) { if (s->states[i].in_use) { curl_clean_state(&s->states[i]); @@ -580,6 +586,7 @@ static void curl_detach_aio_context(BlockDriverState *b= s) curl_multi_cleanup(s->multi); s->multi =3D NULL; } + qemu_mutex_unlock(&s->mutex); =20 timer_del(&s->timer); } @@ -745,9 +752,12 @@ static int curl_open(BlockDriverState *bs, QDict *opti= ons, int flags, } =20 DPRINTF("CURL: Opening %s\n", file); + qemu_mutex_init(&s->mutex); s->aio_context =3D bdrv_get_aio_context(bs); s->url =3D g_strdup(file); + qemu_mutex_lock(&s->mutex); state =3D curl_init_state(bs, s); + qemu_mutex_unlock(&s->mutex); if (!state) goto out_noclean; =20 @@ -791,11 +801,12 @@ static int curl_open(BlockDriverState *bs, QDict *opt= ions, int flags, } DPRINTF("CURL: Size =3D %zd\n", s->len); =20 + qemu_mutex_lock(&s->mutex); curl_clean_state(state); + qemu_mutex_unlock(&s->mutex); curl_easy_cleanup(state->curl); state->curl =3D NULL; =20 - qemu_mutex_init(&s->mutex); curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); =20 qemu_opts_del(opts); --=20 2.12.2 From nobody Mon Apr 29 16:15:48 2024 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 1494322650625818.8030495724947; Tue, 9 May 2017 02:37:30 -0700 (PDT) Received: from localhost ([::1]:36093 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81aC-0005v2-8W for importer@patchew.org; Tue, 09 May 2017 05:37:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34917) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d81Yn-0004wh-26 for qemu-devel@nongnu.org; Tue, 09 May 2017 05:36:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d81Yl-0004u9-Qv for qemu-devel@nongnu.org; Tue, 09 May 2017 05:36:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53102) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d81Yl-0004t8-Ju; Tue, 09 May 2017 05:35:59 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 89DD68124D; Tue, 9 May 2017 09:35:58 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-23.ams2.redhat.com [10.36.117.23]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v499Zo7M015222; Tue, 9 May 2017 05:35:57 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 89DD68124D Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 89DD68124D From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 9 May 2017 11:35:49 +0200 Message-Id: <20170509093549.25157-5-pbonzini@redhat.com> In-Reply-To: <20170509093549.25157-1-pbonzini@redhat.com> References: <20170509093549.25157-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 09 May 2017 09:35:58 +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] [PATCH 4/4] curl: improve search for unused CURLState 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: jcody@redhat.com, qemu-stable@nongnu.org, rjones@redhat.com 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" The main change here is to pull the search loop to a new function, making it easier to switch from aio_poll to BDRV_POLL_WHILE. However, the "for (j...)" loop is also dead (there is no other read of "j" outside the loop), so remove it. Cc: jcody@redhat.com Cc: qemu-stable@nongnu.org Signed-off-by: Paolo Bonzini --- block/curl.c | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/block/curl.c b/block/curl.c index e8fcc5ca34..79e504a2cc 100644 --- a/block/curl.c +++ b/block/curl.c @@ -455,29 +455,41 @@ static void curl_multi_timeout_do(void *arg) } =20 /* Called with s->mutex held. */ +static bool curl_find_unused_state_locked(BDRVCURLState *s, CURLState **st= ate) +{ + int i; + + for (i =3D 0; i < CURL_NUM_STATES; i++) { + if (!s->states[i].in_use) { + s->states[i].in_use =3D 1; + *state =3D &s->states[i]; + return true; + } + } + + return false; +} + +static bool curl_find_unused_state(BDRVCURLState *s, CURLState **state) +{ + bool ret; + + qemu_mutex_lock(&s->mutex); + ret =3D curl_find_unused_state_locked(s, state); + qemu_mutex_unlock(&s->mutex); + return ret; +} + +/* Called with s->mutex held. */ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s) { CURLState *state =3D NULL; - int i, j; - - do { - for (i=3D0; istates[i].acb[j]) - continue; - if (s->states[i].in_use) - continue; - - state =3D &s->states[i]; - state->in_use =3D 1; - break; - } - if (!state) { - qemu_mutex_unlock(&s->mutex); - aio_poll(bdrv_get_aio_context(bs), true); - qemu_mutex_lock(&s->mutex); - } - } while(!state); + + if (!curl_find_unused_state_locked(s, &state)) { + qemu_mutex_unlock(&s->mutex); + BDRV_POLL_WHILE(bs, !curl_find_unused_state(s, &state)); + qemu_mutex_lock(&s->mutex); + } =20 if (!state->curl) { state->curl =3D curl_easy_init(); --=20 2.12.2