From nobody Fri May 3 20:41:12 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 1487360524514978.4235768375071; Fri, 17 Feb 2017 11:42:04 -0800 (PST) Received: from localhost ([::1]:55463 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceoPr-0001IB-BY for importer@patchew.org; Fri, 17 Feb 2017 14:42:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceoOM-0000R3-RR for qemu-devel@nongnu.org; Fri, 17 Feb 2017 14:40:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ceoOK-0002uu-Nj for qemu-devel@nongnu.org; Fri, 17 Feb 2017 14:40:30 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:33617) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ceoOK-0002tu-Gc; Fri, 17 Feb 2017 14:40:28 -0500 Received: by mail-wm0-x241.google.com with SMTP id v77so4231576wmv.0; Fri, 17 Feb 2017 11:40:28 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id s17sm14165343wrc.6.2017.02.17.11.40.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2017 11:40:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=Ckmbl/oBQXrUIQ++ZbgaOtN1/AviP+mluRUIyyeB560=; b=YGyH5l3EysjUl8DMlIqYkYAkf0cTw3dFhyjMJCv2s19GS/kEnDn0fkc8WWLfawsGL/ A4JsAjYFXz14EOcGX7b/yjZfOLWr++NcL91c7JL8MVO7/C0RTmO+ypZu03E8obYifsOF PDCd2wAySoVW5jgHwZ/jTkVkp0Ij8aqA+sMPZWMQLnznw1Z0eQbt+Tdp74E6zWwcIAIG VdA4I/KVK+QKyaIO3NVDFXiyj014DWcCvr7AC+hA0VHRZh1gdMN0ZCx7MvNjlN7JTZ/5 ILJWv9MEteY83htglIeev3K7nihYqiG3xXxqEVhYOl8VW1Yxb5N4Nm8+gMAIqP/GQedn j9aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=Ckmbl/oBQXrUIQ++ZbgaOtN1/AviP+mluRUIyyeB560=; b=slG74fd/5RyzCUgfut/jsun0qVclZSvUoBT5pZC1oc1MMuWDDVV5TBBiZoXe7v42q6 uxHmsdZEFe67SV7A0Z4XfNhnKKi6BmLenhiA1hx1VI/7shItZ+fkUOvH6sW3IOHH7LJ9 lRECys6i3CgD+Isy8PQn5AG6gkTt5ss5Q0IXzAEAt2BqEGryig1D8FSQ+GwODktxFS+X Y6k0L82hFeQzBJxDUE9VRQ3D//XABjFOw0ObEx/7xthNuc/rdI3UXEg8lMwYVRqwqaYf hJrCSOLlTIKTzNvGWMPibhM2CjTmDU8j2pNg2ed2p0iYsF5fmeFNQ3u4S17BU+JSfiNg lL+g== X-Gm-Message-State: AMke39n9AU4r1o+vwZbIqGtIBgCvyx5WoSq5rQKCsT57xU0DoTrHXRdbYh3aPfk/GyBc4g== X-Received: by 10.28.93.138 with SMTP id r132mr5475719wmb.17.1487360427404; Fri, 17 Feb 2017 11:40:27 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 17 Feb 2017 20:40:26 +0100 Message-Id: <20170217194028.8398-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170217194028.8398-1-pbonzini@redhat.com> References: <20170217194028.8398-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH 1/3] curl: do not use aio_context_acquire/release 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-block@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" Now that all bottom halves and callbacks take care of taking the AioContext lock, we can migrate some users away from it and to a specific QemuMutex or CoMutex. Protect BDRVCURLState access with a QemuMutex. Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- block/curl.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/block/curl.c b/block/curl.c index 2939cc7..e83dcd8 100644 --- a/block/curl.c +++ b/block/curl.c @@ -135,6 +135,7 @@ typedef struct BDRVCURLState { char *cookie; bool accept_range; AioContext *aio_context; + QemuMutex mutex; char *username; char *password; char *proxyusername; @@ -333,6 +334,7 @@ static int curl_find_buf(BDRVCURLState *s, size_t start= , size_t len, return FIND_RET_NONE; } =20 +/* Called with s->mutex held. */ static void curl_multi_check_completion(BDRVCURLState *s) { int msgs_in_queue; @@ -374,7 +376,9 @@ static void curl_multi_check_completion(BDRVCURLState *= s) continue; } =20 + qemu_mutex_unlock(&s->mutex); acb->common.cb(acb->common.opaque, -EPROTO); + qemu_mutex_lock(&s->mutex); qemu_aio_unref(acb); state->acb[i] =3D NULL; } @@ -386,6 +390,7 @@ static void curl_multi_check_completion(BDRVCURLState *= s) } } =20 +/* Called with s->mutex held. */ static void curl_multi_do_locked(CURLState *s) { CURLSocket *socket, *next_socket; @@ -409,19 +414,19 @@ static void curl_multi_do(void *arg) { CURLState *s =3D (CURLState *)arg; =20 - aio_context_acquire(s->s->aio_context); + qemu_mutex_lock(&s->s->mutex); curl_multi_do_locked(s); - aio_context_release(s->s->aio_context); + qemu_mutex_unlock(&s->s->mutex); } =20 static void curl_multi_read(void *arg) { CURLState *s =3D (CURLState *)arg; =20 - aio_context_acquire(s->s->aio_context); + qemu_mutex_lock(&s->s->mutex); curl_multi_do_locked(s); curl_multi_check_completion(s->s); - aio_context_release(s->s->aio_context); + qemu_mutex_unlock(&s->s->mutex); } =20 static void curl_multi_timeout_do(void *arg) @@ -434,11 +439,11 @@ static void curl_multi_timeout_do(void *arg) return; } =20 - aio_context_acquire(s->aio_context); + qemu_mutex_lock(&s->mutex); curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); =20 curl_multi_check_completion(s); - aio_context_release(s->aio_context); + qemu_mutex_unlock(&s->mutex); #else abort(); #endif @@ -771,6 +776,7 @@ static int curl_open(BlockDriverState *bs, QDict *optio= ns, int flags, 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); @@ -801,12 +807,11 @@ static void curl_readv_bh_cb(void *p) CURLAIOCB *acb =3D p; BlockDriverState *bs =3D acb->common.bs; BDRVCURLState *s =3D bs->opaque; - AioContext *ctx =3D bdrv_get_aio_context(bs); =20 size_t start =3D acb->sector_num * BDRV_SECTOR_SIZE; size_t end; =20 - aio_context_acquire(ctx); + qemu_mutex_lock(&s->mutex); =20 // In case we have the requested data already (e.g. read-ahead), // we can just call the callback and be done. @@ -854,7 +859,7 @@ static void curl_readv_bh_cb(void *p) curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); =20 out: - aio_context_release(ctx); + qemu_mutex_unlock(&s->mutex); if (ret !=3D -EINPROGRESS) { acb->common.cb(acb->common.opaque, ret); qemu_aio_unref(acb); @@ -883,6 +888,7 @@ static void curl_close(BlockDriverState *bs) =20 DPRINTF("CURL: Close\n"); curl_detach_aio_context(bs); + qemu_mutex_destroy(&s->mutex); =20 g_free(s->cookie); g_free(s->url); --=20 2.9.3 From nobody Fri May 3 20:41:12 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 1487360733676741.0936818810377; Fri, 17 Feb 2017 11:45:33 -0800 (PST) Received: from localhost ([::1]:55478 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceoTD-0004ee-Gb for importer@patchew.org; Fri, 17 Feb 2017 14:45:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40319) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceoON-0000R5-IR for qemu-devel@nongnu.org; Fri, 17 Feb 2017 14:40:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ceoOM-0002vU-2S for qemu-devel@nongnu.org; Fri, 17 Feb 2017 14:40:31 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:35052) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ceoOL-0002v7-Pt; Fri, 17 Feb 2017 14:40:30 -0500 Received: by mail-wm0-x242.google.com with SMTP id u63so4215867wmu.2; Fri, 17 Feb 2017 11:40:29 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id s17sm14165343wrc.6.2017.02.17.11.40.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2017 11:40:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=an1FaeUm8tRCZT+Yo2wEjf+04wWr2eiV9ym1mADGHfk=; b=Z6Wr1ScpMODxDInf8h76gTh34XeMWiMG79y+pCAey6raMN0YtqlrDXAppXUT6Aw65K ouwhSgQpuUfp0tC3tlQzuM6UP1NEzvK6h6Dbf6J30HKRPorJEYvn3LiIMTbphaLGuqLU +pqNPG2xCp/NRb8HeMmmZy7YJpe2Zbz2jjFrVXpkhzp6/iIeRiKXXKLyOyCRkmFSzrVV x3L5aNIegRFyGbxZrxQGzY2NPOraYPndmwFpZ3QJMXeCS4Tgibaslx+4HdE8BuSIPmN2 RkFHqhT6rHLdiq0JWMiQUqLNdSS6d8FknHH7jHONQeYq2MXCEV5a2N4fKrI3Ma7KIFWt xWNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=an1FaeUm8tRCZT+Yo2wEjf+04wWr2eiV9ym1mADGHfk=; b=TRo7gKEmwJJbqeF/GWZ0AXrVsfVnKQNsp+njo2pln3KSdzFgkB2LGhd6RV/HNzVT9b sa6m/q97qlkvcpPS3+lp4q0JYOq7XU6CAQr3o8JQs9dqnYIx7agfYSs5w8EjUXNPjsOo ETH5CO1xynmlFwADUqeR680Efpj0HZ6s0/6KXmkXWCIeqWBuLhonmk/YQQ4FSkzecuT1 HZtWvRD6I7VNJR/obWQYoJzFnjfxNc8CQk6D5Sz09VYy3c2HI8NEbiGdbqMrWsy0sM7U EcJN41MRaWJBxuC29cPHocKZzxkLk5QAQtvA6L9CmN3j4UYcAASRLIgxW/tgIrX0NxVe 0jaA== X-Gm-Message-State: AMke39n0Zshyc2M0sWOY/RO0LPnOXPZaTLTdjDquwP+8r3mpOz3zsAR3zYN3ba1qChFBow== X-Received: by 10.28.15.2 with SMTP id 2mr5698878wmp.66.1487360428467; Fri, 17 Feb 2017 11:40:28 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 17 Feb 2017 20:40:27 +0100 Message-Id: <20170217194028.8398-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170217194028.8398-1-pbonzini@redhat.com> References: <20170217194028.8398-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PATCH 2/3] nfs: do not use aio_context_acquire/release 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-block@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" Now that all bottom halves and callbacks take care of taking the AioContext lock, we can migrate some users away from it and to a specific QemuMutex or CoMutex. Protect libnfs calls with a QemuMutex. Callbacks are invoked using bottom halves, so we don't even have to drop it around callback invocations. Signed-off-by: Paolo Bonzini --- block/nfs.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/block/nfs.c b/block/nfs.c index 08b43dd..4eddcee 100644 --- a/block/nfs.c +++ b/block/nfs.c @@ -54,6 +54,7 @@ typedef struct NFSClient { int events; bool has_zero_init; AioContext *aio_context; + QemuMutex mutex; blkcnt_t st_blocks; bool cache_used; NFSServer *server; @@ -191,6 +192,7 @@ static void nfs_parse_filename(const char *filename, QD= ict *options, static void nfs_process_read(void *arg); static void nfs_process_write(void *arg); =20 +/* Called with QemuMutex held. */ static void nfs_set_events(NFSClient *client) { int ev =3D nfs_which_events(client->context); @@ -209,20 +211,20 @@ static void nfs_process_read(void *arg) { NFSClient *client =3D arg; =20 - aio_context_acquire(client->aio_context); + qemu_mutex_lock(&client->mutex); nfs_service(client->context, POLLIN); nfs_set_events(client); - aio_context_release(client->aio_context); + qemu_mutex_unlock(&client->mutex); } =20 static void nfs_process_write(void *arg) { NFSClient *client =3D arg; =20 - aio_context_acquire(client->aio_context); + qemu_mutex_lock(&client->mutex); nfs_service(client->context, POLLOUT); nfs_set_events(client); - aio_context_release(client->aio_context); + qemu_mutex_unlock(&client->mutex); } =20 static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) @@ -242,6 +244,7 @@ static void nfs_co_generic_bh_cb(void *opaque) aio_co_wake(task->co); } =20 +/* Called (via nfs_service) with QemuMutex held. */ static void nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, void *private_data) @@ -273,6 +276,7 @@ static int coroutine_fn nfs_co_readv(BlockDriverState *= bs, nfs_co_init_task(bs, &task); task.iov =3D iov; =20 + qemu_mutex_lock(&client->mutex); if (nfs_pread_async(client->context, client->fh, sector_num * BDRV_SECTOR_SIZE, nb_sectors * BDRV_SECTOR_SIZE, @@ -281,6 +285,7 @@ static int coroutine_fn nfs_co_readv(BlockDriverState *= bs, } =20 nfs_set_events(client); + qemu_mutex_unlock(&client->mutex); while (!task.complete) { qemu_coroutine_yield(); } @@ -314,6 +319,7 @@ static int coroutine_fn nfs_co_writev(BlockDriverState = *bs, =20 qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE); =20 + qemu_mutex_lock(&client->mutex); if (nfs_pwrite_async(client->context, client->fh, sector_num * BDRV_SECTOR_SIZE, nb_sectors * BDRV_SECTOR_SIZE, @@ -323,6 +329,7 @@ static int coroutine_fn nfs_co_writev(BlockDriverState = *bs, } =20 nfs_set_events(client); + qemu_mutex_unlock(&client->mutex); while (!task.complete) { qemu_coroutine_yield(); } @@ -343,12 +350,14 @@ static int coroutine_fn nfs_co_flush(BlockDriverState= *bs) =20 nfs_co_init_task(bs, &task); =20 + qemu_mutex_lock(&client->mutex); if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb, &task) !=3D 0) { return -ENOMEM; } =20 nfs_set_events(client); + qemu_mutex_unlock(&client->mutex); while (!task.complete) { qemu_coroutine_yield(); } @@ -434,6 +443,7 @@ static void nfs_file_close(BlockDriverState *bs) { NFSClient *client =3D bs->opaque; nfs_client_close(client); + qemu_mutex_destroy(&client->mutex); } =20 static NFSServer *nfs_config(QDict *options, Error **errp) @@ -641,6 +651,7 @@ static int nfs_file_open(BlockDriverState *bs, QDict *o= ptions, int flags, if (ret < 0) { return ret; } + qemu_mutex_init(&client->mutex); bs->total_sectors =3D ret; ret =3D 0; return ret; @@ -696,6 +707,7 @@ static int nfs_has_zero_init(BlockDriverState *bs) return client->has_zero_init; } =20 +/* Called (via nfs_service) with QemuMutex held. */ static void nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *dat= a, void *private_data) --=20 2.9.3 From nobody Fri May 3 20:41:12 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 148736053986757.66149416279302; Fri, 17 Feb 2017 11:42:19 -0800 (PST) Received: from localhost ([::1]:55464 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceoQ6-0001Xb-JH for importer@patchew.org; Fri, 17 Feb 2017 14:42:18 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40351) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ceoOP-0000Rs-A8 for qemu-devel@nongnu.org; Fri, 17 Feb 2017 14:40:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ceoON-0002vs-6S for qemu-devel@nongnu.org; Fri, 17 Feb 2017 14:40:33 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:34573) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ceoOM-0002vc-Sy; Fri, 17 Feb 2017 14:40:31 -0500 Received: by mail-wm0-x241.google.com with SMTP id c85so4235453wmi.1; Fri, 17 Feb 2017 11:40:30 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id s17sm14165343wrc.6.2017.02.17.11.40.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2017 11:40:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=Kq0I8NEqVliXNqcrohxgVgFT0Nj/o9iWYBBcVRuEP44=; b=AqeXCUCVEaememlMUVcfO9Hy165W8uRUgCcP89Ckj4KQ2arw6dfFIVSxTmBMYFtgsu uNE2XcixnVQ9HciB5llFPUA5jseyQdp7j/u2w8LVYNlkBMfSB5CZEgen3CP8zQjvnzJ7 RMpV+oy2uNhZzhg9hULinJqahc5cc/OaKT0OWnYyJGAmQZMZViz6u6tP8BjybWWOFl+o LMLztfSSKpa9YaiTQyrz82QA7ez82iboDm8MZmljJYgZtOAv7N/mJMwHfKrAggX/TjIZ 5tB7r2++dROVF7m1qXhaFdYV42QnCdTThIGEPlHHO6hxK4rL4U19pM0pAsbyWckkbUAA U1Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=Kq0I8NEqVliXNqcrohxgVgFT0Nj/o9iWYBBcVRuEP44=; b=QaNMRQ7P+VuNi+o8yOwsUyECaXhKCfOAukKPxoLDpKzEqS/55FpIs9e1XdSkPDBln1 JS+9DyPx/i11hplCa7HCYneizloG5LMR7XQQpueMwNkVJKZRSOWQ3xgIY4l44PuHM7EM aXvQdCckJA0qkMMVElCfOEnI96kPEPyBb98h5ZGz44KyY5HYAc+iNJJca28zvieADG0P Fam/6mOTitSdj2ZvmZ7lOkxqvh++mo40bub1PQP/Jy7A1CyRJyTs3zQn9c3IrmuYNKiJ BV6+3024gZKdyDWxJxPVUUZjPyHUo5NyfzNyaLZq3Ywk0xqW3g/IqKmPo7pc+ZhZl01Y nOAw== X-Gm-Message-State: AMke39madHDJNcjvVz+llzsjca2MmsN0KYAur3j3fmh/pCTANGw9Ni8jQ7bNYoxbaNxU+w== X-Received: by 10.28.69.28 with SMTP id s28mr5171485wma.40.1487360429754; Fri, 17 Feb 2017 11:40:29 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 17 Feb 2017 20:40:28 +0100 Message-Id: <20170217194028.8398-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170217194028.8398-1-pbonzini@redhat.com> References: <20170217194028.8398-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH 3/3] iscsi: do not use aio_context_acquire/release 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-block@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" Now that all bottom halves and callbacks take care of taking the AioContext lock, we can migrate some users away from it and to a specific QemuMutex or CoMutex. Protect libiscsi calls with a QemuMutex. Callbacks are invoked using bottom halves, so we don't even have to drop it around callback invocations. Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- block/iscsi.c | 83 +++++++++++++++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 64 insertions(+), 19 deletions(-) diff --git a/block/iscsi.c b/block/iscsi.c index 2561be9..e483f6d 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -58,6 +58,7 @@ typedef struct IscsiLun { int events; QEMUTimer *nop_timer; QEMUTimer *event_timer; + QemuMutex mutex; struct scsi_inquiry_logical_block_provisioning lbp; struct scsi_inquiry_block_limits bl; unsigned char *zeroblock; @@ -252,6 +253,7 @@ static int iscsi_translate_sense(struct scsi_sense *sen= se) return ret; } =20 +/* Called (via iscsi_service) with QemuMutex held. */ static void iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, void *command_data, void *opaque) @@ -352,6 +354,7 @@ static const AIOCBInfo iscsi_aiocb_info =3D { static void iscsi_process_read(void *arg); static void iscsi_process_write(void *arg); =20 +/* Called with QemuMutex held. */ static void iscsi_set_events(IscsiLun *iscsilun) { @@ -395,10 +398,10 @@ iscsi_process_read(void *arg) IscsiLun *iscsilun =3D arg; struct iscsi_context *iscsi =3D iscsilun->iscsi; =20 - aio_context_acquire(iscsilun->aio_context); + qemu_mutex_lock(&iscsilun->mutex); iscsi_service(iscsi, POLLIN); iscsi_set_events(iscsilun); - aio_context_release(iscsilun->aio_context); + qemu_mutex_unlock(&iscsilun->mutex); } =20 static void @@ -407,10 +410,10 @@ iscsi_process_write(void *arg) IscsiLun *iscsilun =3D arg; struct iscsi_context *iscsi =3D iscsilun->iscsi; =20 - aio_context_acquire(iscsilun->aio_context); + qemu_mutex_lock(&iscsilun->mutex); iscsi_service(iscsi, POLLOUT); iscsi_set_events(iscsilun); - aio_context_release(iscsilun->aio_context); + qemu_mutex_unlock(&iscsilun->mutex); } =20 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) @@ -589,6 +592,7 @@ iscsi_co_writev_flags(BlockDriverState *bs, int64_t sec= tor_num, int nb_sectors, uint64_t lba; uint32_t num_sectors; bool fua =3D flags & BDRV_REQ_FUA; + int r =3D 0; =20 if (fua) { assert(iscsilun->dpofua); @@ -604,6 +608,7 @@ iscsi_co_writev_flags(BlockDriverState *bs, int64_t sec= tor_num, int nb_sectors, lba =3D sector_qemu2lun(sector_num, iscsilun); num_sectors =3D sector_qemu2lun(nb_sectors, iscsilun); iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); retry: if (iscsilun->use_16_for_rw) { #if LIBISCSI_API_VERSION >=3D (20160603) @@ -640,7 +645,9 @@ retry: #endif while (!iTask.complete) { iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); qemu_coroutine_yield(); + qemu_mutex_lock(&iscsilun->mutex); } =20 if (iTask.task !=3D NULL) { @@ -655,12 +662,15 @@ retry: =20 if (iTask.status !=3D SCSI_STATUS_GOOD) { iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors); - return iTask.err_code; + r =3D iTask.err_code; + goto out_unlock; } =20 iscsi_allocmap_set_allocated(iscsilun, sector_num, nb_sectors); =20 - return 0; +out_unlock: + qemu_mutex_unlock(&iscsilun->mutex); + return r; } =20 =20 @@ -693,18 +703,21 @@ static int64_t coroutine_fn iscsi_co_get_block_status= (BlockDriverState *bs, goto out; } =20 + qemu_mutex_lock(&iscsilun->mutex); retry: if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, sector_qemu2lun(sector_num, iscsilun), 8 + 16, iscsi_co_generic_cb, &iTask) =3D=3D NULL) { ret =3D -ENOMEM; - goto out; + goto out_unlock; } =20 while (!iTask.complete) { iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); qemu_coroutine_yield(); + qemu_mutex_lock(&iscsilun->mutex); } =20 if (iTask.do_retry) { @@ -721,20 +734,20 @@ retry: * because the device is busy or the cmd is not * supported) we pretend all blocks are allocated * for backwards compatibility */ - goto out; + goto out_unlock; } =20 lbas =3D scsi_datain_unmarshall(iTask.task); if (lbas =3D=3D NULL) { ret =3D -EIO; - goto out; + goto out_unlock; } =20 lbasd =3D &lbas->descriptors[0]; =20 if (sector_qemu2lun(sector_num, iscsilun) !=3D lbasd->lba) { ret =3D -EIO; - goto out; + goto out_unlock; } =20 *pnum =3D sector_lun2qemu(lbasd->num_blocks, iscsilun); @@ -756,6 +769,8 @@ retry: if (*pnum > nb_sectors) { *pnum =3D nb_sectors; } +out_unlock: + qemu_mutex_unlock(&iscsilun->mutex); out: if (iTask.task !=3D NULL) { scsi_free_scsi_task(iTask.task); @@ -818,6 +833,7 @@ static int coroutine_fn iscsi_co_readv(BlockDriverState= *bs, num_sectors =3D sector_qemu2lun(nb_sectors, iscsilun); =20 iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); retry: if (iscsilun->use_16_for_rw) { #if LIBISCSI_API_VERSION >=3D (20160603) @@ -855,7 +871,9 @@ retry: #endif while (!iTask.complete) { iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); qemu_coroutine_yield(); + qemu_mutex_lock(&iscsilun->mutex); } =20 if (iTask.task !=3D NULL) { @@ -867,6 +885,7 @@ retry: iTask.complete =3D 0; goto retry; } + qemu_mutex_unlock(&iscsilun->mutex); =20 if (iTask.status !=3D SCSI_STATUS_GOOD) { return iTask.err_code; @@ -881,6 +900,7 @@ static int coroutine_fn iscsi_co_flush(BlockDriverState= *bs) struct IscsiTask iTask; =20 iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); retry: if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0= , 0, 0, iscsi_co_generic_cb, &iTask) =3D= =3D NULL) { @@ -889,7 +909,9 @@ retry: =20 while (!iTask.complete) { iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); qemu_coroutine_yield(); + qemu_mutex_lock(&iscsilun->mutex); } =20 if (iTask.task !=3D NULL) { @@ -901,6 +923,7 @@ retry: iTask.complete =3D 0; goto retry; } + qemu_mutex_unlock(&iscsilun->mutex); =20 if (iTask.status !=3D SCSI_STATUS_GOOD) { return iTask.err_code; @@ -910,6 +933,7 @@ retry: } =20 #ifdef __linux__ +/* Called (via iscsi_service) with QemuMutex held. */ static void iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, void *command_data, void *opaque) @@ -1034,6 +1058,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *= bs, acb->task->expxferlen =3D acb->ioh->dxfer_len; =20 data.size =3D 0; + qemu_mutex_lock(&iscsilun->mutex); if (acb->task->xfer_dir =3D=3D SCSI_XFER_WRITE) { if (acb->ioh->iovec_count =3D=3D 0) { data.data =3D acb->ioh->dxferp; @@ -1049,6 +1074,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *= bs, iscsi_aio_ioctl_cb, (data.size > 0) ? &data : NULL, acb) !=3D 0) { + qemu_mutex_unlock(&iscsilun->mutex); scsi_free_scsi_task(acb->task); qemu_aio_unref(acb); return NULL; @@ -1068,6 +1094,7 @@ static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *= bs, } =20 iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); =20 return &acb->common; } @@ -1092,6 +1119,7 @@ coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, = int64_t offset, int count) IscsiLun *iscsilun =3D bs->opaque; struct IscsiTask iTask; struct unmap_list list; + int r =3D 0; =20 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) { return -ENOTSUP; @@ -1106,15 +1134,19 @@ coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs= , int64_t offset, int count) list.num =3D count / iscsilun->block_size; =20 iscsi_co_init_iscsitask(iscsilun, &iTask); + qemu_mutex_lock(&iscsilun->mutex); retry: if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, iscsi_co_generic_cb, &iTask) =3D=3D NULL) { - return -ENOMEM; + r =3D -ENOMEM; + goto out_unlock; } =20 while (!iTask.complete) { iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); qemu_coroutine_yield(); + qemu_mutex_lock(&iscsilun->mutex); } =20 if (iTask.task !=3D NULL) { @@ -1131,17 +1163,20 @@ retry: /* the target might fail with a check condition if it is not happy with the alignment of the UNMAP request we silently fail in this case */ - return 0; + goto out_unlock; } =20 if (iTask.status !=3D SCSI_STATUS_GOOD) { - return iTask.err_code; + r =3D iTask.err_code; + goto out_unlock; } =20 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS, count >> BDRV_SECTOR_BITS); =20 - return 0; +out_unlock: + qemu_mutex_unlock(&iscsilun->mutex); + return r; } =20 static int @@ -1153,6 +1188,7 @@ coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState = *bs, int64_t offset, uint64_t lba; uint32_t nb_blocks; bool use_16_for_ws =3D iscsilun->use_16_for_rw; + int r =3D 0; =20 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) { return -ENOTSUP; @@ -1186,6 +1222,7 @@ coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState = *bs, int64_t offset, } } =20 + qemu_mutex_lock(&iscsilun->mutex); iscsi_co_init_iscsitask(iscsilun, &iTask); retry: if (use_16_for_ws) { @@ -1205,7 +1242,9 @@ retry: =20 while (!iTask.complete) { iscsi_set_events(iscsilun); + qemu_mutex_unlock(&iscsilun->mutex); qemu_coroutine_yield(); + qemu_mutex_lock(&iscsilun->mutex); } =20 if (iTask.status =3D=3D SCSI_STATUS_CHECK_CONDITION && @@ -1215,7 +1254,8 @@ retry: /* WRITE SAME is not supported by the target */ iscsilun->has_write_same =3D false; scsi_free_scsi_task(iTask.task); - return -ENOTSUP; + r =3D -ENOTSUP; + goto out_unlock; } =20 if (iTask.task !=3D NULL) { @@ -1231,7 +1271,8 @@ retry: if (iTask.status !=3D SCSI_STATUS_GOOD) { iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS, count >> BDRV_SECTOR_BITS); - return iTask.err_code; + r =3D iTask.err_code; + goto out_unlock; } =20 if (flags & BDRV_REQ_MAY_UNMAP) { @@ -1242,7 +1283,9 @@ retry: count >> BDRV_SECTOR_BITS); } =20 - return 0; +out_unlock: + qemu_mutex_unlock(&iscsilun->mutex); + return r; } =20 static void parse_chap(struct iscsi_context *iscsi, const char *target, @@ -1397,7 +1440,7 @@ static void iscsi_nop_timed_event(void *opaque) { IscsiLun *iscsilun =3D opaque; =20 - aio_context_acquire(iscsilun->aio_context); + qemu_mutex_lock(&iscsilun->mutex); if (iscsi_get_nops_in_flight(iscsilun->iscsi) >=3D MAX_NOP_FAILURES) { error_report("iSCSI: NOP timeout. Reconnecting..."); iscsilun->request_timed_out =3D true; @@ -1410,7 +1453,7 @@ static void iscsi_nop_timed_event(void *opaque) iscsi_set_events(iscsilun); =20 out: - aio_context_release(iscsilun->aio_context); + qemu_mutex_unlock(&iscsilun->mutex); } =20 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) @@ -1812,6 +1855,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *op= tions, int flags, scsi_free_scsi_task(task); task =3D NULL; =20 + qemu_mutex_init(&iscsilun->mutex); iscsi_attach_aio_context(bs, iscsilun->aio_context); =20 /* Guess the internal cluster (page) size of the iscsi target by the m= eans @@ -1860,6 +1904,7 @@ static void iscsi_close(BlockDriverState *bs) iscsi_destroy_context(iscsi); g_free(iscsilun->zeroblock); iscsi_allocmap_free(iscsilun); + qemu_mutex_destroy(&iscsilun->mutex); memset(iscsilun, 0, sizeof(IscsiLun)); } =20 --=20 2.9.3