From nobody Tue Nov 4 15:43:37 2025 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.zohomail.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 1503967325661911.3998759318373; Mon, 28 Aug 2017 17:42:05 -0700 (PDT) Received: from localhost ([::1]:42023 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmUbT-0007e9-KY for importer@patchew.org; Mon, 28 Aug 2017 20:42:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47834) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmUCM-00018X-RN for qemu-devel@nongnu.org; Mon, 28 Aug 2017 20:16:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dmUCJ-0005M3-MV for qemu-devel@nongnu.org; Mon, 28 Aug 2017 20:16:06 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:57325) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dmUCJ-0005Ku-Er for qemu-devel@nongnu.org; Mon, 28 Aug 2017 20:16:03 -0400 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v7T0E8sW041493 for ; Mon, 28 Aug 2017 20:16:02 -0400 Received: from e14.ny.us.ibm.com (e14.ny.us.ibm.com [129.33.205.204]) by mx0a-001b2d01.pphosted.com with ESMTP id 2cmsrsj1fp-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 28 Aug 2017 20:16:02 -0400 Received: from localhost by e14.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 28 Aug 2017 20:16:01 -0400 Received: from b01cxnp23034.gho.pok.ibm.com (9.57.198.29) by e14.ny.us.ibm.com (146.89.104.201) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 28 Aug 2017 20:15:58 -0400 Received: from b01ledav001.gho.pok.ibm.com (b01ledav001.gho.pok.ibm.com [9.57.199.106]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v7T0Fw3w25952488; Tue, 29 Aug 2017 00:15:58 GMT Received: from b01ledav001.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6216B2804A; Mon, 28 Aug 2017 20:15:51 -0400 (EDT) Received: from localhost (unknown [9.80.85.217]) by b01ledav001.gho.pok.ibm.com (Postfix) with ESMTP id 298F928046; Mon, 28 Aug 2017 20:15:51 -0400 (EDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 28 Aug 2017 19:14:05 -0500 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1503965694-10794-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1503965694-10794-1-git-send-email-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17082900-0052-0000-0000-000002573341 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007630; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000226; SDB=6.00909028; UDB=6.00455849; IPR=6.00689279; BA=6.00005557; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016909; XFM=3.00000015; UTC=2017-08-29 00:15:59 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17082900-0053-0000-0000-000051D1D941 Message-Id: <1503965694-10794-31-git-send-email-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-08-28_13:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1708290001 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH 30/79] 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: Paolo Bonzini , Jeff Cody , qemu-stable@nongnu.org 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 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 the simplest way to fix it is to use coroutines instead. 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: Kun Wei Tested-by: Richard W.M. Jones Reviewed-by: Max Reitz Reviewed-by: Jeff Cody Signed-off-by: Paolo Bonzini Message-id: 20170515100059.15795-4-pbonzini@redhat.com Cc: qemu-stable@nongnu.org Cc: Jeff Cody Signed-off-by: Paolo Bonzini Signed-off-by: Jeff Cody (cherry picked from commit 456af346297ebef86aa097b3609534d34f3d2f75) Signed-off-by: Michael Roth --- block/curl.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/block/curl.c b/block/curl.c index 59e69c6..dddf5ef 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); } @@ -677,6 +684,7 @@ static int curl_open(BlockDriverState *bs, QDict *optio= ns, int flags, return -EROFS; } =20 + qemu_mutex_init(&s->mutex); opts =3D qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); qemu_opts_absorb_qdict(opts, options, &local_err); if (local_err) { @@ -747,7 +755,9 @@ static int curl_open(BlockDriverState *bs, QDict *optio= ns, int flags, DPRINTF("CURL: Opening %s\n", file); 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); @@ -806,6 +817,7 @@ out: curl_easy_cleanup(state->curl); state->curl =3D NULL; out_noclean: + qemu_mutex_destroy(&s->mutex); g_free(s->cookie); g_free(s->url); qemu_opts_del(opts); --=20 2.7.4