From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790233294812.4888786228838; Tue, 15 Jun 2021 13:50:33 -0700 (PDT) Received: from localhost ([::1]:43212 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG12-00058a-4C for importer2@patchew.org; Tue, 15 Jun 2021 16:50:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57830) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFym-0001dR-2z for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:42667) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyh-0000vW-3R for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:11 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-255-g1njSEudP6GnHSuXNz6a7g-1; Tue, 15 Jun 2021 16:48:04 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0AE47100C622; Tue, 15 Jun 2021 20:48:03 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B5CF5D9E2; Tue, 15 Jun 2021 20:47:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790085; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vvesYzTVnWk+zsGXmt91yWNRvOaxcLtm5Y9F9yNCWSw=; b=CaYjsQwa6n8Pu8KU9L4x0T+ODooVt6Z4KbR+8FRN6SFcBKfT4TS+PT21b33WWmO9Fo7elY WgskwZST+ZQvAwcs852DIYCvIR4du5JIIk5cGDAtGhv4DZ9JY/YDtEflSzhxHBRnsx153y 9jAypdIgeZIR8AwusruSc5PFpNERtN4= X-MC-Unique: g1njSEudP6GnHSuXNz6a7g-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 01/34] async: the main AioContext is only "current" if under the BQL Date: Tue, 15 Jun 2021 15:47:23 -0500 Message-Id: <20210615204756.281505-2-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Block I/O path" , Max Reitz , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini If we want to wake up a coroutine from a worker thread, aio_co_wake() currently does not work. In that scenario, aio_co_wake() calls aio_co_enter(), but there is no current AioContext and therefore qemu_get_current_aio_context() returns the main thread. aio_co_wake() then attempts to call aio_context_acquire() instead of going through aio_co_schedule(). The default case of qemu_get_current_aio_context() was added to cover synchronous I/O started from the vCPU thread, but the main and vCPU threads are quite different. The main thread is an I/O thread itself, only running a more complicated event loop; the vCPU thread instead is essentially a worker thread that occasionally calls qemu_mutex_lock_iothread(). It is only in those critical sections that it acts as if it were the home thread of the main AioContext. Therefore, this patch detaches qemu_get_current_aio_context() from iothreads, which is a useless complication. The AioContext pointer is stored directly in the thread-local variable, including for the main loop. Worker threads (including vCPU threads) optionally behave as temporary home threads if they have taken the big QEMU lock, but if that is not the case they will always schedule coroutines on remote threads via aio_co_schedule(). With this change, the stub qemu_mutex_iothread_locked() must be changed from true to false. The previous value of true was needed because the main thread did not have an AioContext in the thread-local variable, but now it does have one. Reported-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Message-Id: <20210609122234.544153-1-pbonzini@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy Tested-by: Vladimir Sementsov-Ogievskiy [eblake: tweak commit message per Vladimir's review] Signed-off-by: Eric Blake --- include/block/aio.h | 5 ++++- iothread.c | 9 +-------- stubs/iothread-lock.c | 2 +- stubs/iothread.c | 8 -------- tests/unit/iothread.c | 9 +-------- util/async.c | 20 ++++++++++++++++++++ util/main-loop.c | 1 + stubs/meson.build | 1 - 8 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 stubs/iothread.c diff --git a/include/block/aio.h b/include/block/aio.h index 5f342267d5ce..10fcae151540 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -691,10 +691,13 @@ void aio_co_enter(AioContext *ctx, struct Coroutine *= co); * Return the AioContext whose event loop runs in the current thread. * * If called from an IOThread this will be the IOThread's AioContext. If - * called from another thread it will be the main loop AioContext. + * called from the main thread or with the "big QEMU lock" taken it + * will be the main loop AioContext. */ AioContext *qemu_get_current_aio_context(void); +void qemu_set_current_aio_context(AioContext *ctx); + /** * aio_context_setup: * @ctx: the aio context diff --git a/iothread.c b/iothread.c index 7f086387be9a..2c5ccd736733 100644 --- a/iothread.c +++ b/iothread.c @@ -39,13 +39,6 @@ DECLARE_CLASS_CHECKERS(IOThreadClass, IOTHREAD, #define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL #endif -static __thread IOThread *my_iothread; - -AioContext *qemu_get_current_aio_context(void) -{ - return my_iothread ? my_iothread->ctx : qemu_get_aio_context(); -} - static void *iothread_run(void *opaque) { IOThread *iothread =3D opaque; @@ -56,7 +49,7 @@ static void *iothread_run(void *opaque) * in this new thread uses glib. */ g_main_context_push_thread_default(iothread->worker_context); - my_iothread =3D iothread; + qemu_set_current_aio_context(iothread->ctx); iothread->thread_id =3D qemu_get_thread_id(); qemu_sem_post(&iothread->init_done_sem); diff --git a/stubs/iothread-lock.c b/stubs/iothread-lock.c index 2a6efad64a16..5b45b7fc8b90 100644 --- a/stubs/iothread-lock.c +++ b/stubs/iothread-lock.c @@ -3,7 +3,7 @@ bool qemu_mutex_iothread_locked(void) { - return true; + return false; } void qemu_mutex_lock_iothread_impl(const char *file, int line) diff --git a/stubs/iothread.c b/stubs/iothread.c deleted file mode 100644 index 8cc9e28c5555..000000000000 --- a/stubs/iothread.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "qemu/osdep.h" -#include "block/aio.h" -#include "qemu/main-loop.h" - -AioContext *qemu_get_current_aio_context(void) -{ - return qemu_get_aio_context(); -} diff --git a/tests/unit/iothread.c b/tests/unit/iothread.c index afde12b4efb5..f9b0791084e7 100644 --- a/tests/unit/iothread.c +++ b/tests/unit/iothread.c @@ -30,13 +30,6 @@ struct IOThread { bool stopping; }; -static __thread IOThread *my_iothread; - -AioContext *qemu_get_current_aio_context(void) -{ - return my_iothread ? my_iothread->ctx : qemu_get_aio_context(); -} - static void iothread_init_gcontext(IOThread *iothread) { GSource *source; @@ -54,9 +47,9 @@ static void *iothread_run(void *opaque) rcu_register_thread(); - my_iothread =3D iothread; qemu_mutex_lock(&iothread->init_done_lock); iothread->ctx =3D aio_context_new(&error_abort); + qemu_set_current_aio_context(iothread->ctx); /* * We must connect the ctx to a GMainContext, because in older versions diff --git a/util/async.c b/util/async.c index 674dbefb7c24..5d9b7cc1eba2 100644 --- a/util/async.c +++ b/util/async.c @@ -649,3 +649,23 @@ void aio_context_release(AioContext *ctx) { qemu_rec_mutex_unlock(&ctx->lock); } + +static __thread AioContext *my_aiocontext; + +AioContext *qemu_get_current_aio_context(void) +{ + if (my_aiocontext) { + return my_aiocontext; + } + if (qemu_mutex_iothread_locked()) { + /* Possibly in a vCPU thread. */ + return qemu_get_aio_context(); + } + return NULL; +} + +void qemu_set_current_aio_context(AioContext *ctx) +{ + assert(!my_aiocontext); + my_aiocontext =3D ctx; +} diff --git a/util/main-loop.c b/util/main-loop.c index d9c55df6f5e7..4ae5b23e991e 100644 --- a/util/main-loop.c +++ b/util/main-loop.c @@ -170,6 +170,7 @@ int qemu_init_main_loop(Error **errp) if (!qemu_aio_context) { return -EMFILE; } + qemu_set_current_aio_context(qemu_aio_context); qemu_notify_bh =3D qemu_bh_new(notify_event_cb, NULL); gpollfds =3D g_array_new(FALSE, FALSE, sizeof(GPollFD)); src =3D aio_get_g_source(qemu_aio_context); diff --git a/stubs/meson.build b/stubs/meson.build index 65c22c0568ce..4993797f0550 100644 --- a/stubs/meson.build +++ b/stubs/meson.build @@ -16,7 +16,6 @@ stub_ss.add(files('fw_cfg.c')) stub_ss.add(files('gdbstub.c')) stub_ss.add(files('get-vm-name.c')) stub_ss.add(when: 'CONFIG_LINUX_IO_URING', if_true: files('io_uring.c')) -stub_ss.add(files('iothread.c')) stub_ss.add(files('iothread-lock.c')) stub_ss.add(files('isa-bus.c')) stub_ss.add(files('is-daemonized.c')) --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1623790607; cv=none; d=zohomail.com; s=zohoarc; b=KfrDRdswUApO/QrTorx+8CmwcqnOYqTnThpIUniDkxP4OCKLSVZx7d9MKblHvoE25wffBx0QOlv3RNJ783gM49iuZq3Cqf/Y5HuPaF3q5/C1+fOv4pl2iX9yn8+2AmfzpDdeIJ33k160Qw4dO2S9VIiMPUiGoWPI/R+8MDNjTLM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623790607; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7sss+8frESIKewJODcAPUvGBz8v06flQRyCv97eMUxQ=; b=T1NN+mR/zE44YAowv4uDXcsJNmC6xZcER9wySp0cCwMTqzVJmY9eYn/ufmJkoE3zqg7tifkhs/YeC2mx5kxQkqTQTGi4ZvJGB4L/mWZmqmtc2xTl0flcONA3Hq6P6LgogrqoZw9iwwlqLejpdvdHX142xOilwY2JU11+xQHOIfY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790607148439.6877094454129; Tue, 15 Jun 2021 13:56:47 -0700 (PDT) Received: from localhost ([::1]:59298 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG74-0007wV-1h for importer2@patchew.org; Tue, 15 Jun 2021 16:56:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57776) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyj-0001Zv-F9 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:54846) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyh-0000vc-5x for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:09 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-94-WmvvPunhMDSM90SJ6NEguw-1; Tue, 15 Jun 2021 16:48:04 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 843B1800D55; Tue, 15 Jun 2021 20:48:03 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35FAC5D9CA; Tue, 15 Jun 2021 20:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7sss+8frESIKewJODcAPUvGBz8v06flQRyCv97eMUxQ=; b=iKBQl4wlAoC+mZCIJiNFMdG/FRBT4pqfFj5WqHuAYq+EHVggpD89+JHgKBLsK8OsJZu2+E x6mP+ipy+3LmOCw61XPKZqFCWLKDhYUZCmNH4yS+bDGAkdh494LgZpPIObTB6Yc3Eo6Lh/ 2RuvYfiUuDJ5Gy/ADYBvzwaXEAFJ1uU= X-MC-Unique: WmvvPunhMDSM90SJ6NEguw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 02/34] tests: cover aio_co_enter from a worker thread without BQL taken Date: Tue, 15 Jun 2021 15:47:24 -0500 Message-Id: <20210615204756.281505-3-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Vladimir Sementsov-Ogievskiy Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Add a testcase for the test fixed by commit 'async: the main AioContext is only "current" if under the BQL. Signed-off-by: Paolo Bonzini Message-Id: <20210614110214.726722-1-pbonzini@redhat.com> Reviewed-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Eric Blake --- tests/unit/test-aio.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/unit/test-aio.c b/tests/unit/test-aio.c index 8a4607846340..6feeb9a4a9fd 100644 --- a/tests/unit/test-aio.c +++ b/tests/unit/test-aio.c @@ -877,6 +877,42 @@ static void test_queue_chaining(void) g_assert_cmpint(data_b.i, =3D=3D, data_b.max); } +static void co_check_current_thread(void *opaque) +{ + QemuThread *main_thread =3D opaque; + assert(qemu_thread_is_self(main_thread)); +} + +static void *test_aio_co_enter(void *co) +{ + /* + * qemu_get_current_aio_context() should not to be the main thread + * AioContext, because this is a worker thread that has not taken + * the BQL. So aio_co_enter will schedule the coroutine in the + * main thread AioContext. + */ + aio_co_enter(qemu_get_aio_context(), co); + return NULL; +} + +static void test_worker_thread_co_enter(void) +{ + QemuThread this_thread, worker_thread; + Coroutine *co; + + qemu_thread_get_self(&this_thread); + co =3D qemu_coroutine_create(co_check_current_thread, &this_thread); + + qemu_thread_create(&worker_thread, "test_acquire_thread", + test_aio_co_enter, + co, QEMU_THREAD_JOINABLE); + + /* Test aio_co_enter from a worker thread. */ + qemu_thread_join(&worker_thread); + g_assert(aio_poll(ctx, true)); + g_assert(!aio_poll(ctx, false)); +} + /* End of tests. */ int main(int argc, char **argv) @@ -903,6 +939,7 @@ int main(int argc, char **argv) g_test_add_func("/aio/timer/schedule", test_timer_schedule); g_test_add_func("/aio/coroutine/queue-chaining", test_queue_chaining); + g_test_add_func("/aio/coroutine/worker-thread-co-enter", test_worker_t= hread_co_enter); g_test_add_func("/aio-gsource/flush", test_source_fl= ush); g_test_add_func("/aio-gsource/bh/schedule", test_source_bh= _schedule); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790377154859.4128718254858; Tue, 15 Jun 2021 13:52:57 -0700 (PDT) Received: from localhost ([::1]:50814 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG3L-0002Da-5c for importer2@patchew.org; Tue, 15 Jun 2021 16:52:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57768) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyj-0001Zs-AC for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:59705) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyh-0000vk-3b for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:08 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-160--y7cCFxMN3iRa4zO_ccTJw-1; Tue, 15 Jun 2021 16:48:04 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1AAA7801106; Tue, 15 Jun 2021 20:48:04 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id B145F5D9CA; Tue, 15 Jun 2021 20:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UtJew8wW0RSFLaTtJ46vtbsdZhNnPEfaTbUSdYtdQ0w=; b=BzryTI0GMhaF1d8VtZDiS7EEwour96IlA5G4/sVeQ77XcWWBz6YGp643obW4APlbnbHR0R s4e39y+acBa2v28ot7oT1LKPiY8M2tpUIjZTg8MVNsGxji9lKMvm45is19JaNg2Q8MnRtq qjmAyX6UCVRH5610fyXsT+YXaI3IQlg= X-MC-Unique: -y7cCFxMN3iRa4zO_ccTJw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 03/34] co-queue: drop extra coroutine_fn marks Date: Tue, 15 Jun 2021 15:47:25 -0500 Message-Id: <20210615204756.281505-4-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy qemu_co_queue_next() and qemu_co_queue_restart_all() just call aio_co_wake() which works well in non-coroutine context. So these functions can be called from non-coroutine context as well. And actually qemu_co_queue_restart_all() is called from nbd_cancel_in_flight(), which is called from non-coroutine context. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-2-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- include/qemu/coroutine.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 292e61aef0c2..4829ff373d3c 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -210,13 +210,15 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *qu= eue, QemuLockable *lock); /** * Removes the next coroutine from the CoQueue, and wake it up. * Returns true if a coroutine was removed, false if the queue is empty. + * OK to run from coroutine and non-coroutine context. */ -bool coroutine_fn qemu_co_queue_next(CoQueue *queue); +bool qemu_co_queue_next(CoQueue *queue); /** * Empties the CoQueue; all coroutines are woken up. + * OK to run from coroutine and non-coroutine context. */ -void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue); +void qemu_co_queue_restart_all(CoQueue *queue); /** * Removes the next coroutine from the CoQueue, and wake it up. Unlike --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790224338760.917276275337; Tue, 15 Jun 2021 13:50:24 -0700 (PDT) Received: from localhost ([::1]:42582 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG0t-0004j7-BK for importer2@patchew.org; Tue, 15 Jun 2021 16:50:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57810) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyk-0001aJ-Tv for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27544) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyi-0000wo-Ls for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:10 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-495-j4J_jKzEPxCCMsopHklKmA-1; Tue, 15 Jun 2021 16:48:06 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 308F380ED8B; Tue, 15 Jun 2021 20:48:05 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 484A95D9CA; Tue, 15 Jun 2021 20:48:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bHN0gX4+g7tvxbjtZFv17OdP3e6NFvcgyr0j+JE2dDc=; b=VZmVcRYOi8oH/QDORj4WRr01GltISSdYvilTqiECArcrulpclpXliz1MmVA/Xo8NZZECY1 7z6KJQ6KViBu83ZhZ4mBdS420a1UGt7VHHbG/6gVCgCu6vxyMzlV0+TsaW3JZ7yQvLIL8K nHQhNMZi4OscXD0Ud5AWWTvWq56U5II= X-MC-Unique: j4J_jKzEPxCCMsopHklKmA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 04/34] block/nbd: fix channel object leak Date: Tue, 15 Jun 2021 15:47:26 -0500 Message-Id: <20210615204756.281505-5-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Roman Kagan nbd_free_connect_thread leaks the channel object if it hasn't been stolen. Unref it and fix the leak. Signed-off-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-3-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 1 + 1 file changed, 1 insertion(+) diff --git a/block/nbd.c b/block/nbd.c index 616f9ae6c4da..f4b3407587df 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -381,6 +381,7 @@ static void nbd_free_connect_thread(NBDConnectThread *t= hr) { if (thr->sioc) { qio_channel_close(QIO_CHANNEL(thr->sioc), NULL); + object_unref(OBJECT(thr->sioc)); } error_free(thr->err); qapi_free_SocketAddress(thr->saddr); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790816293489.6234059786291; Tue, 15 Jun 2021 14:00:16 -0700 (PDT) Received: from localhost ([::1]:39432 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGAR-0005TD-80 for importer2@patchew.org; Tue, 15 Jun 2021 17:00:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57834) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFym-0001e6-8i for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:23222) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyj-0000xU-7j for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:12 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-97-2oUjowCqPg-W-I6fSOeM0g-1; Tue, 15 Jun 2021 16:48:07 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0623E80EDAA; Tue, 15 Jun 2021 20:48:06 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57CAD5D9CA; Tue, 15 Jun 2021 20:48:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mlTl4h8WRBzjszeGX5X7kM5uxNydnj4ozPH/5NYr4ZA=; b=HaCFJ+0/PhxwE6g0vs/9pTujEIoSULVH2cKMs5WgkZv3vgtIg8Vz43sn+sS3qpjVbPS2Ft +OT3X1rZoS+HeKQA6R8fwc4uuGiJkgQcH2nIRD/SzKTj+szGN/y+Mqm0tVLObMgLQ1DMSf /yKj6Vkfdsj2I8sNTYNQnfucqvzSPEE= X-MC-Unique: 2oUjowCqPg-W-I6fSOeM0g-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 05/34] block/nbd: fix how state is cleared on nbd_open() failure paths Date: Tue, 15 Jun 2021 15:47:27 -0500 Message-Id: <20210615204756.281505-6-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We have two "return error" paths in nbd_open() after nbd_process_options(). Actually we should call nbd_clear_bdrvstate() on these paths. Interesting that nbd_process_options() calls nbd_clear_bdrvstate() by itself. Let's fix leaks and refactor things to be more obvious: - intialize yank at top of nbd_open() - move yank cleanup to nbd_clear_bdrvstate() - refactor nbd_open() so that all failure paths except for yank-register goes through nbd_clear_bdrvstate() Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-4-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index f4b3407587df..01d2c2efad63 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -152,8 +152,12 @@ static void nbd_co_establish_connection_cancel(BlockDr= iverState *bs, static int nbd_client_handshake(BlockDriverState *bs, Error **errp); static void nbd_yank(void *opaque); -static void nbd_clear_bdrvstate(BDRVNBDState *s) +static void nbd_clear_bdrvstate(BlockDriverState *bs) { + BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + + yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); + object_unref(OBJECT(s->tlscreds)); qapi_free_SocketAddress(s->saddr); s->saddr =3D NULL; @@ -2275,9 +2279,6 @@ static int nbd_process_options(BlockDriverState *bs, = QDict *options, ret =3D 0; error: - if (ret < 0) { - nbd_clear_bdrvstate(s); - } qemu_opts_del(opts); return ret; } @@ -2288,11 +2289,6 @@ static int nbd_open(BlockDriverState *bs, QDict *opt= ions, int flags, int ret; BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; - ret =3D nbd_process_options(bs, options, errp); - if (ret < 0) { - return ret; - } - s->bs =3D bs; qemu_co_mutex_init(&s->send_mutex); qemu_co_queue_init(&s->free_sema); @@ -2301,20 +2297,23 @@ static int nbd_open(BlockDriverState *bs, QDict *op= tions, int flags, return -EEXIST; } + ret =3D nbd_process_options(bs, options, errp); + if (ret < 0) { + goto fail; + } + /* * establish TCP connection, return error if it fails * TODO: Configurable retry-until-timeout behaviour. */ if (nbd_establish_connection(bs, s->saddr, errp) < 0) { - yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); - return -ECONNREFUSED; + ret =3D -ECONNREFUSED; + goto fail; } ret =3D nbd_client_handshake(bs, errp); if (ret < 0) { - yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); - nbd_clear_bdrvstate(s); - return ret; + goto fail; } /* successfully connected */ s->state =3D NBD_CLIENT_CONNECTED; @@ -2326,6 +2325,10 @@ static int nbd_open(BlockDriverState *bs, QDict *opt= ions, int flags, aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co); return 0; + +fail: + nbd_clear_bdrvstate(bs); + return ret; } static int nbd_co_flush(BlockDriverState *bs) @@ -2369,11 +2372,8 @@ static void nbd_refresh_limits(BlockDriverState *bs,= Error **errp) static void nbd_close(BlockDriverState *bs) { - BDRVNBDState *s =3D bs->opaque; - nbd_client_close(bs); - yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); - nbd_clear_bdrvstate(s); + nbd_clear_bdrvstate(bs); } /* --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790992707521.499504444321; Tue, 15 Jun 2021 14:03:12 -0700 (PDT) Received: from localhost ([::1]:47786 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGDG-0002xC-1f for importer2@patchew.org; Tue, 15 Jun 2021 17:03:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57872) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyn-0001im-Pb for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:48258) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyj-0000y5-Ro for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:13 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-469-6zA-JQKPOTitt3Vue_zoTA-1; Tue, 15 Jun 2021 16:48:07 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9EA40195D577; Tue, 15 Jun 2021 20:48:06 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 342BE5D9E2; Tue, 15 Jun 2021 20:48:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fPL7tIn4VeWEaXBmh8QQ5kKPViX3Mn24TzXBre3WcZ4=; b=f8ZnkaBATYX1sWPvAlrrOpnZ5FzV3Q/tc/ZWkmDrjBJ0Wh69VQgynj3gLL6TFUy5iQPa/O Et7GGiKnJPl9qVj1run6luV7foNIb2cvNG6senF6tSgrkBjx99VyzVzUXEIwXLqLIXrGny /spMBnI0bf4VVdzYJeiMjArpxL9QxBI= X-MC-Unique: 6zA-JQKPOTitt3Vue_zoTA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 06/34] block/nbd: connect_thread_func(): do qio_channel_set_delay(false) Date: Tue, 15 Jun 2021 15:47:28 -0500 Message-Id: <20210615204756.281505-7-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy nbd_open() does it (through nbd_establish_connection()). Actually we lost that call on reconnect path in 1dc4718d849e1a1fe "block/nbd: use non-blocking connect: fix vm hang on connect()" when we have introduced reconnect thread. Fixes: 1dc4718d849e1a1fe665ce5241ed79048cfa2cfc Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-5-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- block/nbd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block/nbd.c b/block/nbd.c index 01d2c2efad63..f3a036354dc7 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -408,6 +408,8 @@ static void *connect_thread_func(void *opaque) thr->sioc =3D NULL; } + qio_channel_set_delay(QIO_CHANNEL(thr->sioc), false); + qemu_mutex_lock(&thr->mutex); switch (thr->state) { --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1623790418; cv=none; d=zohomail.com; s=zohoarc; b=b3UHTumTeGj8w4kgdp4Gcm966ziL66qWFb+1dZNXK2XOiWf7xlZe6cejW4wTShYqm7nr83b24nXM5ky7/ZclKGKVdypb5PNYvy17/kbOt9eCFcRNTG7bsBZp51HMAu0KSBl2QJiZ+mphGL/WlGHtCJAP7j6FK9pDAE2hwleo9Ug= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623790418; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gsu8CgwCiI97eFuf0FJbJi4zkbnxu3TQaKxCTZSDDmM=; b=CC1g8+TAaeC9kG9eMXdvcB3STXVj9VDshwtWpZaihOZ987S9cH4+4S2iMPHqqBaEHoh978zNs4k/k3np+sTFDD0u7M4fPGkT0FrFBjfgJY5joOkOxBiOL54MqOTozIhHOWPFoDHskzys9dENUyJkytLn9W0E2qwF52ko9nHhvao= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162379041843195.9641197695679; Tue, 15 Jun 2021 13:53:38 -0700 (PDT) Received: from localhost ([::1]:52614 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG41-0003Qp-DB for importer2@patchew.org; Tue, 15 Jun 2021 16:53:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57916) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyp-0001mu-Bq for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:44972) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyn-00010j-MV for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:15 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-122-eAiwDzOjN0u2kytiUwkJFQ-1; Tue, 15 Jun 2021 16:48:11 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8F9CC100C611; Tue, 15 Jun 2021 20:48:10 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF1175D9CA; Tue, 15 Jun 2021 20:48:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gsu8CgwCiI97eFuf0FJbJi4zkbnxu3TQaKxCTZSDDmM=; b=i3GyDeZQaNWT3jtVHly45Z1cur6Bm0YQiGG54kP3qZFl5KGmaAfQpY95vM4G50zZWHRilO macHlaVrtzten4J81DZkPovZWuxsAgZKp6Vdff1EYE0OcYTllmZe+Hdzi7h8DIftP3NAq3 mPYvlDT2eJpHMhp+kSgA+Pdc09YDvqw= X-MC-Unique: eAiwDzOjN0u2kytiUwkJFQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 07/34] qemu-sockets: introduce socket_address_parse_named_fd() Date: Tue, 15 Jun 2021 15:47:29 -0500 Message-Id: <20210615204756.281505-8-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Gerd Hoffmann Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Add function that transforms named fd inside SocketAddress structure into number representation. This way it may be then used in a context where current monitor is not available. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-6-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake [eblake: comment tweak] Signed-off-by: Eric Blake --- include/qemu/sockets.h | 11 +++++++++++ util/qemu-sockets.c | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 7d1f8135767d..0c34bf23987e 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -111,4 +111,15 @@ SocketAddress *socket_remote_address(int fd, Error **e= rrp); */ SocketAddress *socket_address_flatten(SocketAddressLegacy *addr); +/** + * socket_address_parse_named_fd: + * + * Modify @addr, replacing a named fd by its corresponding number. + * Needed for callers that plan to pass @addr to a context where the + * current monitor is not available. + * + * Return 0 on success. + */ +int socket_address_parse_named_fd(SocketAddress *addr, Error **errp); + #endif /* QEMU_SOCKETS_H */ diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index c415c342c12b..080a240b74ee 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1164,6 +1164,25 @@ static int socket_get_fd(const char *fdstr, Error **= errp) return fd; } +int socket_address_parse_named_fd(SocketAddress *addr, Error **errp) +{ + int fd; + + if (addr->type !=3D SOCKET_ADDRESS_TYPE_FD) { + return 0; + } + + fd =3D socket_get_fd(addr->u.fd.str, errp); + if (fd < 0) { + return fd; + } + + g_free(addr->u.fd.str); + addr->u.fd.str =3D g_strdup_printf("%d", fd); + + return 0; +} + int socket_connect(SocketAddress *addr, Error **errp) { int fd; --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790648930953.3048957727349; Tue, 15 Jun 2021 13:57:28 -0700 (PDT) Received: from localhost ([::1]:32826 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG7j-0000pp-Qp for importer2@patchew.org; Tue, 15 Jun 2021 16:57:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58010) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFys-0001zT-PL for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:39697) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyq-000131-Jl for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:18 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-166-gN8luQ-hOAqehK4FaSaNYA-1; Tue, 15 Jun 2021 16:48:12 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 388238015DB; Tue, 15 Jun 2021 20:48:11 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id BE7795D9E2; Tue, 15 Jun 2021 20:48:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790096; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WxaToqXqET+32RXmcOxyTCwSQw6bTuWZJnfHX32Y7h8=; b=YLiUdIZQQTJ99X/wFyaqZNa8EZuU1EDCOb/ewuxrdqdfy+1oB7r0ZEA9Qug/ahsVq94RDl QqEEm0rBZarvTVtfG2/IKdz2Upj7UvTk79otaCz9ecA57T7Cf/QWGE4M+8hPVav5VRrVzd ALHiBSwBrqr4pHYZWKkHiJeSUKCb/34= X-MC-Unique: gN8luQ-hOAqehK4FaSaNYA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 08/34] block/nbd: call socket_address_parse_named_fd() in advance Date: Tue, 15 Jun 2021 15:47:30 -0500 Message-Id: <20210615204756.281505-9-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Detecting monitor by current coroutine works bad when we are not in coroutine context. And that's exactly so in nbd reconnect code, where qio_channel_socket_connect_sync() is called from thread. Monitor is needed only to parse named file descriptor. So, let's just parse it during nbd_open(), so that all further users of s->saddr don't need to access monitor. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-7-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- block/nbd.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/block/nbd.c b/block/nbd.c index f3a036354dc7..1c99654ef7e5 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -2140,6 +2140,12 @@ static SocketAddress *nbd_config(BDRVNBDState *s, QD= ict *options, goto done; } + if (socket_address_parse_named_fd(saddr, errp) < 0) { + qapi_free_SocketAddress(saddr); + saddr =3D NULL; + goto done; + } + done: qobject_unref(addr); visit_free(iv); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162379066258064.69053840987226; Tue, 15 Jun 2021 13:57:42 -0700 (PDT) Received: from localhost ([::1]:33672 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG7x-0001Q4-HK for importer2@patchew.org; Tue, 15 Jun 2021 16:57:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58000) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFys-0001xJ-9Y for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:32942) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyp-00012F-9i for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:17 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-14-IcB2MjQiNAWLSSG0IUP6WA-1; Tue, 15 Jun 2021 16:48:12 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E53925211; Tue, 15 Jun 2021 20:48:11 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 684EA5D9CA; Tue, 15 Jun 2021 20:48:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FLxngiSXkoMyw8lK/cB6aeauM/90I+KZKJ2nfxpLQSk=; b=UNzy6Fbd2QA1oKNIEmIIydbOsorCHDyMwZgJEKubwQQhtZMrDhHgU20cxpfrFVBpLoLLAQ qMntlIlVnzosNuINeKbFHr8E7vTts0ucRJrikm1oz8NtZ8/4rhn/mGhdwv86t3A5XZEO38 YGqQzTHfPros+WUyhLicgn03ugqgLEc= X-MC-Unique: IcB2MjQiNAWLSSG0IUP6WA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 09/34] block/nbd: ensure ->connection_thread is always valid Date: Tue, 15 Jun 2021 15:47:31 -0500 Message-Id: <20210615204756.281505-10-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Roman Kagan Simplify lifetime management of BDRVNBDState->connect_thread by delaying the possible cleanup of it until the BDRVNBDState itself goes away. This also reverts 0267101af6 "block/nbd: fix possible use after free of s->connect_thread" as now s->connect_thread can't be cleared until the very end. Signed-off-by: Roman Kagan [vsementsov: rebase, revert 0267101af6 changes] Signed-off-by: Vladimir Sementsov-Ogievskiy [eblake: tweak comment] Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-8-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 56 ++++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 1c99654ef7e5..08ae47d83c07 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -144,17 +144,31 @@ typedef struct BDRVNBDState { NBDConnectThread *connect_thread; } BDRVNBDState; +static void nbd_free_connect_thread(NBDConnectThread *thr); static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *s= addr, Error **errp); static int nbd_co_establish_connection(BlockDriverState *bs, Error **errp); -static void nbd_co_establish_connection_cancel(BlockDriverState *bs, - bool detach); +static void nbd_co_establish_connection_cancel(BlockDriverState *bs); static int nbd_client_handshake(BlockDriverState *bs, Error **errp); static void nbd_yank(void *opaque); static void nbd_clear_bdrvstate(BlockDriverState *bs) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + NBDConnectThread *thr =3D s->connect_thread; + bool thr_running; + + qemu_mutex_lock(&thr->mutex); + thr_running =3D thr->state =3D=3D CONNECT_THREAD_RUNNING; + if (thr_running) { + thr->state =3D CONNECT_THREAD_RUNNING_DETACHED; + } + qemu_mutex_unlock(&thr->mutex); + + /* the runaway thread will clean up itself */ + if (!thr_running) { + nbd_free_connect_thread(thr); + } yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); @@ -295,7 +309,7 @@ static void coroutine_fn nbd_client_co_drain_begin(Bloc= kDriverState *bs) s->drained =3D true; qemu_co_sleep_wake(&s->reconnect_sleep); - nbd_co_establish_connection_cancel(bs, false); + nbd_co_establish_connection_cancel(bs); reconnect_delay_timer_del(s); @@ -333,7 +347,7 @@ static void nbd_teardown_connection(BlockDriverState *b= s) s->state =3D NBD_CLIENT_QUIT; if (s->connection_co) { qemu_co_sleep_wake(&s->reconnect_sleep); - nbd_co_establish_connection_cancel(bs, true); + nbd_co_establish_connection_cancel(bs); } if (qemu_in_coroutine()) { s->teardown_co =3D qemu_coroutine_self(); @@ -446,11 +460,6 @@ nbd_co_establish_connection(BlockDriverState *bs, Erro= r **errp) BDRVNBDState *s =3D bs->opaque; NBDConnectThread *thr =3D s->connect_thread; - if (!thr) { - /* detached */ - return -1; - } - qemu_mutex_lock(&thr->mutex); switch (thr->state) { @@ -494,12 +503,6 @@ nbd_co_establish_connection(BlockDriverState *bs, Erro= r **errp) s->wait_connect =3D true; qemu_coroutine_yield(); - if (!s->connect_thread) { - /* detached */ - return -1; - } - assert(thr =3D=3D s->connect_thread); - qemu_mutex_lock(&thr->mutex); switch (thr->state) { @@ -547,18 +550,12 @@ nbd_co_establish_connection(BlockDriverState *bs, Err= or **errp) * nbd_co_establish_connection_cancel * Cancel nbd_co_establish_connection asynchronously: it will finish soon,= to * allow drained section to begin. - * - * If detach is true, also cleanup the state (or if thread is running, mov= e it - * to CONNECT_THREAD_RUNNING_DETACHED state). s->connect_thread becomes NU= LL if - * detach is true. */ -static void nbd_co_establish_connection_cancel(BlockDriverState *bs, - bool detach) +static void nbd_co_establish_connection_cancel(BlockDriverState *bs) { BDRVNBDState *s =3D bs->opaque; NBDConnectThread *thr =3D s->connect_thread; bool wake =3D false; - bool do_free =3D false; qemu_mutex_lock(&thr->mutex); @@ -569,21 +566,10 @@ static void nbd_co_establish_connection_cancel(BlockD= riverState *bs, s->wait_connect =3D false; wake =3D true; } - if (detach) { - thr->state =3D CONNECT_THREAD_RUNNING_DETACHED; - s->connect_thread =3D NULL; - } - } else if (detach) { - do_free =3D true; } qemu_mutex_unlock(&thr->mutex); - if (do_free) { - nbd_free_connect_thread(thr); - s->connect_thread =3D NULL; - } - if (wake) { aio_co_wake(s->connection_co); } @@ -2310,6 +2296,8 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, goto fail; } + nbd_init_connect_thread(s); + /* * establish TCP connection, return error if it fails * TODO: Configurable retry-until-timeout behaviour. @@ -2326,8 +2314,6 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, /* successfully connected */ s->state =3D NBD_CLIENT_CONNECTED; - nbd_init_connect_thread(s); - s->connection_co =3D qemu_coroutine_create(nbd_connection_entry, s); bdrv_inc_in_flight(bs); aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790418364953.6612203699289; Tue, 15 Jun 2021 13:53:38 -0700 (PDT) Received: from localhost ([::1]:52564 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG41-0003P9-C3 for importer2@patchew.org; Tue, 15 Jun 2021 16:53:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57974) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyr-0001uP-Jp for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:28956) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyp-00012n-R0 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:17 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-555-GL52oNkVMCKR232P3sFdUQ-1; Tue, 15 Jun 2021 16:48:13 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9963C80ED8E; Tue, 15 Jun 2021 20:48:12 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1DF9E5D9CA; Tue, 15 Jun 2021 20:48:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790095; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0GSrRGYbIMd1843jZBfSUz5Adw+Pbc8WooYhh2TkHeQ=; b=FgyRoTTfjJlCvZgyIw3uoTGTF6Dddp5XHx6rigPyzIXYitCGodVq+Ra6pXT1YmzfSNmGjp Z1r6APiVQUJKacLwpjrRICtXgnOCGANByKHKTrgDZoX9Inib4GAeiZuXqoaBqCGZ5V1UtA Wa8KxSKj5nMQB8Fpn9GYcD2hNIMig74= X-MC-Unique: GL52oNkVMCKR232P3sFdUQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 10/34] block/nbd: nbd_client_handshake(): fix leak of s->ioc Date: Tue, 15 Jun 2021 15:47:32 -0500 Message-Id: <20210615204756.281505-11-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Message-Id: <20210610100802.5888-9-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block/nbd.c b/block/nbd.c index 08ae47d83c07..77b85ca47189 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -1889,6 +1889,8 @@ static int nbd_client_handshake(BlockDriverState *bs,= Error **errp) nbd_yank, bs); object_unref(OBJECT(s->sioc)); s->sioc =3D NULL; + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; return ret; } --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790901045497.6020481644283; Tue, 15 Jun 2021 14:01:41 -0700 (PDT) Received: from localhost ([::1]:42006 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGBo-0007E6-2e for importer2@patchew.org; Tue, 15 Jun 2021 17:01:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyt-000211-4B for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29300) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyq-000134-NL for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:18 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-420-ElTSs5LiN7auVzl56l8nhw-1; Tue, 15 Jun 2021 16:48:14 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 51A02195D560; Tue, 15 Jun 2021 20:48:13 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id C8F585D9CA; Tue, 15 Jun 2021 20:48:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790096; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eXN+I56T0G8Jc3hHgU/ilIz1SEkkY/e6sb5QI1haj94=; b=PHAKoldZroYUEEJlDgMtgo0Yye51dn1QPa76YzCOCdXbAdCJO7iDcYymLBROYovDOic6rO P+Sq7g4W8Vky9ScDal8f3qCtugM2JF19r2nGbDAzdSSL08oLeAou78HTcouaux2H6+7y8u 8o3lTwI46v+FjJShAnBlyrJt1S72WtE= X-MC-Unique: ElTSs5LiN7auVzl56l8nhw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 11/34] block/nbd: BDRVNBDState: drop unused connect_err and connect_status Date: Tue, 15 Jun 2021 15:47:33 -0500 Message-Id: <20210615204756.281505-12-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy These fields are write-only. Drop them. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-10-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 77b85ca47189..fdfb1ff7a158 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -121,8 +121,6 @@ typedef struct BDRVNBDState { bool wait_drained_end; int in_flight; NBDClientState state; - int connect_status; - Error *connect_err; bool wait_in_flight; QEMUTimer *reconnect_delay_timer; @@ -578,7 +576,6 @@ static void nbd_co_establish_connection_cancel(BlockDri= verState *bs) static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { int ret; - Error *local_err =3D NULL; if (!nbd_client_connecting(s)) { return; @@ -619,14 +616,14 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNB= DState *s) s->ioc =3D NULL; } - if (nbd_co_establish_connection(s->bs, &local_err) < 0) { + if (nbd_co_establish_connection(s->bs, NULL) < 0) { ret =3D -ECONNREFUSED; goto out; } bdrv_dec_in_flight(s->bs); - ret =3D nbd_client_handshake(s->bs, &local_err); + ret =3D nbd_client_handshake(s->bs, NULL); if (s->drained) { s->wait_drained_end =3D true; @@ -641,11 +638,6 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBD= State *s) bdrv_inc_in_flight(s->bs); out: - s->connect_status =3D ret; - error_free(s->connect_err); - s->connect_err =3D NULL; - error_propagate(&s->connect_err, local_err); - if (ret >=3D 0) { /* successfully connected */ s->state =3D NBD_CLIENT_CONNECTED; --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790270333768.8894224440467; Tue, 15 Jun 2021 13:51:10 -0700 (PDT) Received: from localhost ([::1]:44956 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG1d-0006Hx-AG for importer2@patchew.org; Tue, 15 Jun 2021 16:51:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58048) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyu-00026n-DE for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:20 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26455) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyr-00013K-C3 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:20 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-517-cBJXQwFONKKXbCXgIvY21Q-1; Tue, 15 Jun 2021 16:48:15 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EA9FB100C618; Tue, 15 Jun 2021 20:48:13 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8051E5D9CA; Tue, 15 Jun 2021 20:48:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790096; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9qw15AXj1YNaf+iLy2ksTn4Zvh/VDMB0WF+Nxw4PTbk=; b=fDb5OtPMn2dtXVU0Ru1vmDxFUP+qplzBUzYGkb75Py1UH87bjlv0zq/qEDtMlGsRCrVxsO KfaCGvfr5+E/2Y5M5LXoil1OFBQB5rqZFIWTV2mH3CjTYx/ituOut3PoOufu5+YNfTPUIF 1eVrE/iSLftciwkKI7NpdXePFGwZFfE= X-MC-Unique: cBJXQwFONKKXbCXgIvY21Q-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 12/34] block/nbd: simplify waking of nbd_co_establish_connection() Date: Tue, 15 Jun 2021 15:47:34 -0500 Message-Id: <20210615204756.281505-13-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Instead of managing connect_bh, bh_ctx, and wait_connect fields, we can use a single link to the waiting coroutine with proper mutex protection. So new logic is: nbd_co_establish_connection() sets wait_co under the mutex, releases the mutex, then yield()s. Note that wait_co may be scheduled by the thread immediately after unlocking the mutex. Still, the main thread (or iothread) will not reach the code for entering the coroutine until the yield(), so we are safe. connect_thread_func() and nbd_co_establish_connection_cancel() do the following to handle wait_co: Under the mutex, if thr->wait_co is not NULL, make it NULL and schedule it. This way, we avoid scheduling the coroutine twice. Still scheduling is a bit different: In connect_thread_func() we can just call aio_co_wake under mutex, after commit [async: the main AioContext is only "current" if under the BQL] we are sure that aio_co_wake() will not try to acquire the aio context and do qemu_aio_coroutine_enter() but simply schedule the coroutine by aio_co_schedule(). nbd_co_establish_connection_cancel() will be called from non-coroutine context in further patch and will be able to go through qemu_aio_coroutine_enter() path of aio_co_wake(). So keep current behavior of waking the coroutine after the critical section. Also, this commit reduces the dependence of nbd_co_establish_connection() on the internals of bs (we now use a generic pointer to the coroutine, instead of direct use of s->connection_co). This is a step towards splitting the connection API out of nbd.c. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-11-vsementsov@virtuozzo.com> Reviewied-by: Eric Blake Signed-off-by: Eric Blake --- block/nbd.c | 55 +++++++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index fdfb1ff7a158..653af629408d 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -87,12 +87,6 @@ typedef enum NBDConnectThreadState { typedef struct NBDConnectThread { /* Initialization constants */ SocketAddress *saddr; /* address to connect to */ - /* - * Bottom half to schedule on completion. Scheduled only if bh_ctx is = not - * NULL - */ - QEMUBHFunc *bh_func; - void *bh_opaque; /* * Result of last attempt. Valid in FAIL and SUCCESS states. @@ -101,10 +95,15 @@ typedef struct NBDConnectThread { QIOChannelSocket *sioc; Error *err; - /* state and bh_ctx are protected by mutex */ QemuMutex mutex; + /* All further fields are protected by mutex */ NBDConnectThreadState state; /* current state of the thread */ - AioContext *bh_ctx; /* where to schedule bh (NULL means don't schedule= ) */ + + /* + * wait_co: if non-NULL, which coroutine to wake in + * nbd_co_establish_connection() after yield() + */ + Coroutine *wait_co; } NBDConnectThread; typedef struct BDRVNBDState { @@ -138,7 +137,6 @@ typedef struct BDRVNBDState { char *x_dirty_bitmap; bool alloc_depth; - bool wait_connect; NBDConnectThread *connect_thread; } BDRVNBDState; @@ -370,15 +368,6 @@ static bool nbd_client_connecting_wait(BDRVNBDState *s) return qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTING_WA= IT; } -static void connect_bh(void *opaque) -{ - BDRVNBDState *state =3D opaque; - - assert(state->wait_connect); - state->wait_connect =3D false; - aio_co_wake(state->connection_co); -} - static void nbd_init_connect_thread(BDRVNBDState *s) { s->connect_thread =3D g_new(NBDConnectThread, 1); @@ -386,8 +375,6 @@ static void nbd_init_connect_thread(BDRVNBDState *s) *s->connect_thread =3D (NBDConnectThread) { .saddr =3D QAPI_CLONE(SocketAddress, s->saddr), .state =3D CONNECT_THREAD_NONE, - .bh_func =3D connect_bh, - .bh_opaque =3D s, }; qemu_mutex_init(&s->connect_thread->mutex); @@ -427,11 +414,9 @@ static void *connect_thread_func(void *opaque) switch (thr->state) { case CONNECT_THREAD_RUNNING: thr->state =3D ret < 0 ? CONNECT_THREAD_FAIL : CONNECT_THREAD_SUCC= ESS; - if (thr->bh_ctx) { - aio_bh_schedule_oneshot(thr->bh_ctx, thr->bh_func, thr->bh_opa= que); - - /* play safe, don't reuse bh_ctx on further connection attempt= s */ - thr->bh_ctx =3D NULL; + if (thr->wait_co) { + aio_co_wake(thr->wait_co); + thr->wait_co =3D NULL; } break; case CONNECT_THREAD_RUNNING_DETACHED: @@ -485,20 +470,14 @@ nbd_co_establish_connection(BlockDriverState *bs, Err= or **errp) abort(); } - thr->bh_ctx =3D qemu_get_current_aio_context(); + thr->wait_co =3D qemu_coroutine_self(); qemu_mutex_unlock(&thr->mutex); - /* * We are going to wait for connect-thread finish, but * nbd_client_co_drain_begin() can interrupt. - * - * Note that wait_connect variable is not visible for connect-thread. = It - * doesn't need mutex protection, it used only inside home aio context= of - * bs. */ - s->wait_connect =3D true; qemu_coroutine_yield(); qemu_mutex_lock(&thr->mutex); @@ -553,23 +532,19 @@ static void nbd_co_establish_connection_cancel(BlockD= riverState *bs) { BDRVNBDState *s =3D bs->opaque; NBDConnectThread *thr =3D s->connect_thread; - bool wake =3D false; + Coroutine *wait_co =3D NULL; qemu_mutex_lock(&thr->mutex); if (thr->state =3D=3D CONNECT_THREAD_RUNNING) { /* We can cancel only in running state, when bh is not yet schedul= ed */ - thr->bh_ctx =3D NULL; - if (s->wait_connect) { - s->wait_connect =3D false; - wake =3D true; - } + wait_co =3D g_steal_pointer(&thr->wait_co); } qemu_mutex_unlock(&thr->mutex); - if (wake) { - aio_co_wake(s->connection_co); + if (wait_co) { + aio_co_wake(wait_co); } } --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791049429220.85454343450317; Tue, 15 Jun 2021 14:04:09 -0700 (PDT) Received: from localhost ([::1]:50508 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGEC-0004lq-9y for importer2@patchew.org; Tue, 15 Jun 2021 17:04:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58104) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyx-0002Hu-6R for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:42614) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyu-00015Z-Pl for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-499-JeO1yeX4M-etTCMyCbJq4Q-1; Tue, 15 Jun 2021 16:48:18 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 847F280ED91; Tue, 15 Jun 2021 20:48:17 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 21CFE5D9CA; Tue, 15 Jun 2021 20:48:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790100; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OnoAj/nEtKG/XNToof+6cgt1XTN3w6Q5uG8xvQhE1oc=; b=Q90VOlZazypSumAdmT4B9oNH/WmD/XUvaW+UhpTm+IGcj+t5317tiRG5q/LQVOL0hGFz7A TWMCuIOGL0xh6AHTPHOeAZWAVzy6vKc6VZOJwBmePt6hFoaEA563uVcj//22Vpj9cxoO8g sE6kkEntAvE0hXELCHVVxiTzwiwOv0I= X-MC-Unique: JeO1yeX4M-etTCMyCbJq4Q-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 13/34] block/nbd: drop thr->state Date: Tue, 15 Jun 2021 15:47:35 -0500 Message-Id: <20210615204756.281505-14-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We don't need all these states. The code refactored to use two boolean variables looks simpler. While moving the comment in nbd_co_establish_connection() rework it to give better information. Also, we are going to move the connection code to separate file and mentioning drained section would be confusing. Improve also the comment in NBDConnectThread, while dropping removed state names from it. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-12-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake [eblake: comment tweak] Signed-off-by: Eric Blake --- block/nbd.c | 148 +++++++++++++++++----------------------------------- 1 file changed, 49 insertions(+), 99 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 653af629408d..f2d5b47026a3 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -66,38 +66,25 @@ typedef enum NBDClientState { NBD_CLIENT_QUIT } NBDClientState; -typedef enum NBDConnectThreadState { - /* No thread, no pending results */ - CONNECT_THREAD_NONE, - - /* Thread is running, no results for now */ - CONNECT_THREAD_RUNNING, - - /* - * Thread is running, but requestor exited. Thread should close - * the new socket and free the connect state on exit. - */ - CONNECT_THREAD_RUNNING_DETACHED, - - /* Thread finished, results are stored in a state */ - CONNECT_THREAD_FAIL, - CONNECT_THREAD_SUCCESS -} NBDConnectThreadState; - typedef struct NBDConnectThread { /* Initialization constants */ SocketAddress *saddr; /* address to connect to */ - /* - * Result of last attempt. Valid in FAIL and SUCCESS states. - * If you want to steal error, don't forget to set pointer to NULL. - */ - QIOChannelSocket *sioc; - Error *err; - QemuMutex mutex; - /* All further fields are protected by mutex */ - NBDConnectThreadState state; /* current state of the thread */ + + /* + * @sioc and @err represent a connection attempt. While running + * is true, they are only used by the connection thread, and mutex + * locking is not needed. Once the thread finishes, + * nbd_co_establish_connection then steals these pointers while + * under the mutex. + */ + QIOChannelSocket *sioc; + Error *err; + + /* All further fields are accessed only under mutex */ + bool running; /* thread is running now */ + bool detached; /* thread is detached and should cleanup the state */ /* * wait_co: if non-NULL, which coroutine to wake in @@ -152,17 +139,19 @@ static void nbd_clear_bdrvstate(BlockDriverState *bs) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; NBDConnectThread *thr =3D s->connect_thread; - bool thr_running; + bool do_free =3D false; qemu_mutex_lock(&thr->mutex); - thr_running =3D thr->state =3D=3D CONNECT_THREAD_RUNNING; - if (thr_running) { - thr->state =3D CONNECT_THREAD_RUNNING_DETACHED; + assert(!thr->detached); + if (thr->running) { + thr->detached =3D true; + } else { + do_free =3D true; } qemu_mutex_unlock(&thr->mutex); /* the runaway thread will clean up itself */ - if (!thr_running) { + if (do_free) { nbd_free_connect_thread(thr); } @@ -374,7 +363,6 @@ static void nbd_init_connect_thread(BDRVNBDState *s) *s->connect_thread =3D (NBDConnectThread) { .saddr =3D QAPI_CLONE(SocketAddress, s->saddr), - .state =3D CONNECT_THREAD_NONE, }; qemu_mutex_init(&s->connect_thread->mutex); @@ -395,7 +383,7 @@ static void *connect_thread_func(void *opaque) { NBDConnectThread *thr =3D opaque; int ret; - bool do_free =3D false; + bool do_free; thr->sioc =3D qio_channel_socket_new(); @@ -411,20 +399,13 @@ static void *connect_thread_func(void *opaque) qemu_mutex_lock(&thr->mutex); - switch (thr->state) { - case CONNECT_THREAD_RUNNING: - thr->state =3D ret < 0 ? CONNECT_THREAD_FAIL : CONNECT_THREAD_SUCC= ESS; - if (thr->wait_co) { - aio_co_wake(thr->wait_co); - thr->wait_co =3D NULL; - } - break; - case CONNECT_THREAD_RUNNING_DETACHED: - do_free =3D true; - break; - default: - abort(); + assert(thr->running); + thr->running =3D false; + if (thr->wait_co) { + aio_co_wake(thr->wait_co); + thr->wait_co =3D NULL; } + do_free =3D thr->detached; qemu_mutex_unlock(&thr->mutex); @@ -438,36 +419,24 @@ static void *connect_thread_func(void *opaque) static int coroutine_fn nbd_co_establish_connection(BlockDriverState *bs, Error **errp) { - int ret; QemuThread thread; BDRVNBDState *s =3D bs->opaque; NBDConnectThread *thr =3D s->connect_thread; + assert(!s->sioc); + qemu_mutex_lock(&thr->mutex); - switch (thr->state) { - case CONNECT_THREAD_FAIL: - case CONNECT_THREAD_NONE: + if (!thr->running) { + if (thr->sioc) { + /* Previous attempt finally succeeded in background */ + goto out; + } + thr->running =3D true; error_free(thr->err); thr->err =3D NULL; - thr->state =3D CONNECT_THREAD_RUNNING; qemu_thread_create(&thread, "nbd-connect", connect_thread_func, thr, QEMU_THREAD_DETACHED); - break; - case CONNECT_THREAD_SUCCESS: - /* Previous attempt finally succeeded in background */ - thr->state =3D CONNECT_THREAD_NONE; - s->sioc =3D thr->sioc; - thr->sioc =3D NULL; - yank_register_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), - nbd_yank, bs); - qemu_mutex_unlock(&thr->mutex); - return 0; - case CONNECT_THREAD_RUNNING: - /* Already running, will wait */ - break; - default: - abort(); } thr->wait_co =3D qemu_coroutine_self(); @@ -482,10 +451,16 @@ nbd_co_establish_connection(BlockDriverState *bs, Err= or **errp) qemu_mutex_lock(&thr->mutex); - switch (thr->state) { - case CONNECT_THREAD_SUCCESS: - case CONNECT_THREAD_FAIL: - thr->state =3D CONNECT_THREAD_NONE; +out: + if (thr->running) { + /* + * The connection attempt was canceled and the coroutine resumed + * before the connection thread finished its job. Report the + * attempt as failed, but leave the connection thread running, + * to reuse it for the next connection attempt. + */ + error_setg(errp, "Connection attempt cancelled by other operation"= ); + } else { error_propagate(errp, thr->err); thr->err =3D NULL; s->sioc =3D thr->sioc; @@ -494,33 +469,11 @@ nbd_co_establish_connection(BlockDriverState *bs, Err= or **errp) yank_register_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), nbd_yank, bs); } - ret =3D (s->sioc ? 0 : -1); - break; - case CONNECT_THREAD_RUNNING: - case CONNECT_THREAD_RUNNING_DETACHED: - /* - * Obviously, drained section wants to start. Report the attempt as - * failed. Still connect thread is executing in background, and its - * result may be used for next connection attempt. - */ - ret =3D -1; - error_setg(errp, "Connection attempt cancelled by other operation"= ); - break; - - case CONNECT_THREAD_NONE: - /* - * Impossible. We've seen this thread running. So it should be - * running or at least give some results. - */ - abort(); - - default: - abort(); } qemu_mutex_unlock(&thr->mutex); - return ret; + return s->sioc ? 0 : -1; } /* @@ -532,14 +485,11 @@ static void nbd_co_establish_connection_cancel(BlockD= riverState *bs) { BDRVNBDState *s =3D bs->opaque; NBDConnectThread *thr =3D s->connect_thread; - Coroutine *wait_co =3D NULL; + Coroutine *wait_co; qemu_mutex_lock(&thr->mutex); - if (thr->state =3D=3D CONNECT_THREAD_RUNNING) { - /* We can cancel only in running state, when bh is not yet schedul= ed */ - wait_co =3D g_steal_pointer(&thr->wait_co); - } + wait_co =3D g_steal_pointer(&thr->wait_co); qemu_mutex_unlock(&thr->mutex); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790469798671.0390545740255; Tue, 15 Jun 2021 13:54:29 -0700 (PDT) Received: from localhost ([::1]:54362 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG4o-0004ak-BY for importer2@patchew.org; Tue, 15 Jun 2021 16:54:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58148) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyy-0002Mb-JB for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50809) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyv-00015x-F9 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:24 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-393-z01qVJQlNyeDbGF0UiNsYg-1; Tue, 15 Jun 2021 16:48:19 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3BB58100C620; Tue, 15 Jun 2021 20:48:18 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id B36245D9CA; Tue, 15 Jun 2021 20:48:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790100; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Zhs7WliC/s9wT00d6baGraMoKIff72akb6ljKk4x0Ig=; b=aYg4u4yHFMrdPlRBQlwuJJYSmwhAu0Lltgw5xAkVXUfGTpPOtJuvnNXR1321iGEmFjpRlK x1+610sgeQ7qr6M1acKQoHW4QJdICBd/mYEeqtjTxS6HlyBfM+htR0iEgUJuxfPylx68Eu VclOzeaB7nkNn5oeb2T/CAS1Y1izu3A= X-MC-Unique: z01qVJQlNyeDbGF0UiNsYg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 14/34] block/nbd: bs-independent interface for nbd_co_establish_connection() Date: Tue, 15 Jun 2021 15:47:36 -0500 Message-Id: <20210615204756.281505-15-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We are going to split connection code to a separate file. Now we are ready to give nbd_co_establish_connection() clean and bs-independent interface. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-13-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index f2d5b47026a3..15b569a899fd 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -130,7 +130,8 @@ typedef struct BDRVNBDState { static void nbd_free_connect_thread(NBDConnectThread *thr); static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *s= addr, Error **errp); -static int nbd_co_establish_connection(BlockDriverState *bs, Error **errp); +static coroutine_fn QIOChannelSocket * +nbd_co_establish_connection(NBDConnectThread *thr, Error **errp); static void nbd_co_establish_connection_cancel(BlockDriverState *bs); static int nbd_client_handshake(BlockDriverState *bs, Error **errp); static void nbd_yank(void *opaque); @@ -416,22 +417,37 @@ static void *connect_thread_func(void *opaque) return NULL; } -static int coroutine_fn -nbd_co_establish_connection(BlockDriverState *bs, Error **errp) +/* + * Get a new connection in context of @thr: + * if the thread is running, wait for completion + * if the thread already succeeded in the background, and user didn't ge= t the + * result, just return it now + * otherwise the thread is not running, so start a thread and wait for + * completion + */ +static coroutine_fn QIOChannelSocket * +nbd_co_establish_connection(NBDConnectThread *thr, Error **errp) { + QIOChannelSocket *sioc =3D NULL; QemuThread thread; - BDRVNBDState *s =3D bs->opaque; - NBDConnectThread *thr =3D s->connect_thread; - - assert(!s->sioc); qemu_mutex_lock(&thr->mutex); + /* + * Don't call nbd_co_establish_connection() in several coroutines in + * parallel. Only one call at once is supported. + */ + assert(!thr->wait_co); + if (!thr->running) { if (thr->sioc) { /* Previous attempt finally succeeded in background */ - goto out; + sioc =3D g_steal_pointer(&thr->sioc); + qemu_mutex_unlock(&thr->mutex); + + return sioc; } + thr->running =3D true; error_free(thr->err); thr->err =3D NULL; @@ -445,13 +461,12 @@ nbd_co_establish_connection(BlockDriverState *bs, Err= or **errp) /* * We are going to wait for connect-thread finish, but - * nbd_client_co_drain_begin() can interrupt. + * nbd_co_establish_connection_cancel() can interrupt. */ qemu_coroutine_yield(); qemu_mutex_lock(&thr->mutex); -out: if (thr->running) { /* * The connection attempt was canceled and the coroutine resumed @@ -463,17 +478,12 @@ out: } else { error_propagate(errp, thr->err); thr->err =3D NULL; - s->sioc =3D thr->sioc; - thr->sioc =3D NULL; - if (s->sioc) { - yank_register_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), - nbd_yank, bs); - } + sioc =3D g_steal_pointer(&thr->sioc); } qemu_mutex_unlock(&thr->mutex); - return s->sioc ? 0 : -1; + return sioc; } /* @@ -541,11 +551,15 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNB= DState *s) s->ioc =3D NULL; } - if (nbd_co_establish_connection(s->bs, NULL) < 0) { + s->sioc =3D nbd_co_establish_connection(s->connect_thread, NULL); + if (!s->sioc) { ret =3D -ECONNREFUSED; goto out; } + yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_y= ank, + s->bs); + bdrv_dec_in_flight(s->bs); ret =3D nbd_client_handshake(s->bs, NULL); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791222637842.8474884998648; Tue, 15 Jun 2021 14:07:02 -0700 (PDT) Received: from localhost ([::1]:58910 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGGy-0002Dj-Eh for importer2@patchew.org; Tue, 15 Jun 2021 17:07:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58158) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyz-0002Pf-4t for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53899) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyw-000186-RR for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:24 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-357-rpEqD-pBMqaZDlrPT5m7Ig-1; Tue, 15 Jun 2021 16:48:20 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E21F28030CF; Tue, 15 Jun 2021 20:48:18 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 697D55D9CA; Tue, 15 Jun 2021 20:48:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790102; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6uh9cBHhUyG1PktDZmCD1K73GPQPiY9MUtvcCA3NkEk=; b=hvIZcxFmtWyEEpGIaW3naacI1UZVP4+IjNY7eq3ZiIRVna4VZ2TarbEMnv6oa6HB0PeadF xS2qi++1wxH5W/o7B0BSvbBGjQBAzsnvNebjKjJiHru2nl8K2xwbumkoDrH7W0hmgb6K0O 7SGuUWu1CZ4unLdrEg5y5pXPp5iJL3o= X-MC-Unique: rpEqD-pBMqaZDlrPT5m7Ig-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 15/34] block/nbd: make nbd_co_establish_connection_cancel() bs-independent Date: Tue, 15 Jun 2021 15:47:37 -0500 Message-Id: <20210615204756.281505-16-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy nbd_co_establish_connection_cancel() actually needs only pointer to NBDConnectThread. So, make it clean. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-14-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 15b569a899fd..bee615e5c4c1 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -132,7 +132,7 @@ static int nbd_establish_connection(BlockDriverState *b= s, SocketAddress *saddr, Error **errp); static coroutine_fn QIOChannelSocket * nbd_co_establish_connection(NBDConnectThread *thr, Error **errp); -static void nbd_co_establish_connection_cancel(BlockDriverState *bs); +static void nbd_co_establish_connection_cancel(NBDConnectThread *thr); static int nbd_client_handshake(BlockDriverState *bs, Error **errp); static void nbd_yank(void *opaque); @@ -295,7 +295,7 @@ static void coroutine_fn nbd_client_co_drain_begin(Bloc= kDriverState *bs) s->drained =3D true; qemu_co_sleep_wake(&s->reconnect_sleep); - nbd_co_establish_connection_cancel(bs); + nbd_co_establish_connection_cancel(s->connect_thread); reconnect_delay_timer_del(s); @@ -333,7 +333,7 @@ static void nbd_teardown_connection(BlockDriverState *b= s) s->state =3D NBD_CLIENT_QUIT; if (s->connection_co) { qemu_co_sleep_wake(&s->reconnect_sleep); - nbd_co_establish_connection_cancel(bs); + nbd_co_establish_connection_cancel(s->connect_thread); } if (qemu_in_coroutine()) { s->teardown_co =3D qemu_coroutine_self(); @@ -488,13 +488,14 @@ nbd_co_establish_connection(NBDConnectThread *thr, Er= ror **errp) /* * nbd_co_establish_connection_cancel - * Cancel nbd_co_establish_connection asynchronously: it will finish soon,= to - * allow drained section to begin. + * Cancel nbd_co_establish_connection() asynchronously. + * + * Note that this function neither directly stops the thread nor closes the + * socket, but rather safely wakes nbd_co_establish_connection() which is + * sleeping in yield() */ -static void nbd_co_establish_connection_cancel(BlockDriverState *bs) +static void nbd_co_establish_connection_cancel(NBDConnectThread *thr) { - BDRVNBDState *s =3D bs->opaque; - NBDConnectThread *thr =3D s->connect_thread; Coroutine *wait_co; qemu_mutex_lock(&thr->mutex); --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16237913688581006.3433563632062; Tue, 15 Jun 2021 14:09:28 -0700 (PDT) Received: from localhost ([::1]:38968 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGJL-0007w1-Nb for importer2@patchew.org; Tue, 15 Jun 2021 17:09:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz1-0002Zf-NU for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:27221) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyw-000188-VT for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:27 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-460-RXDZKMLwPPqheW22e1o7BQ-1; Tue, 15 Jun 2021 16:48:20 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A3F78195D563; Tue, 15 Jun 2021 20:48:19 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1FD665D9CA; Tue, 15 Jun 2021 20:48:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790102; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7pv8yi6pgb0b49kmgyArOHXB7KPQRK0jmmDA7LKPFvg=; b=JW1/1LtfQCoOR5+6YRs0t6QQ2zTnSZEzUNJxDIqn+mQko6ma4fPP1St6uRHRUZejgVqAVa 6sNoF7OCwdOUFhIHKMJAGLQBj+wZxEkjJcPDsKry2k5qvwX77X6WFnAOHVYpBu+Gq3qyoY 8ZVZjEJj5/e6YkfpdPscjA4w6R3FCRw= X-MC-Unique: RXDZKMLwPPqheW22e1o7BQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 16/34] block/nbd: rename NBDConnectThread to NBDClientConnection Date: Tue, 15 Jun 2021 15:47:38 -0500 Message-Id: <20210615204756.281505-17-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We are going to move the connection code to its own file, and want clear names and APIs first. The structure is shared between user and (possibly) several runs of connect-thread. So it's wrong to call it "thread". Let's rename to something more generic. Appropriately rename connect_thread and thr variables to conn. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-15-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 134 ++++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index bee615e5c4c1..ce8d38d17abf 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -66,7 +66,7 @@ typedef enum NBDClientState { NBD_CLIENT_QUIT } NBDClientState; -typedef struct NBDConnectThread { +typedef struct NBDClientConnection { /* Initialization constants */ SocketAddress *saddr; /* address to connect to */ @@ -91,7 +91,7 @@ typedef struct NBDConnectThread { * nbd_co_establish_connection() after yield() */ Coroutine *wait_co; -} NBDConnectThread; +} NBDClientConnection; typedef struct BDRVNBDState { QIOChannelSocket *sioc; /* The master data channel */ @@ -124,36 +124,36 @@ typedef struct BDRVNBDState { char *x_dirty_bitmap; bool alloc_depth; - NBDConnectThread *connect_thread; + NBDClientConnection *conn; } BDRVNBDState; -static void nbd_free_connect_thread(NBDConnectThread *thr); +static void nbd_free_connect_thread(NBDClientConnection *conn); static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *s= addr, Error **errp); static coroutine_fn QIOChannelSocket * -nbd_co_establish_connection(NBDConnectThread *thr, Error **errp); -static void nbd_co_establish_connection_cancel(NBDConnectThread *thr); +nbd_co_establish_connection(NBDClientConnection *conn, Error **errp); +static void nbd_co_establish_connection_cancel(NBDClientConnection *conn); static int nbd_client_handshake(BlockDriverState *bs, Error **errp); static void nbd_yank(void *opaque); static void nbd_clear_bdrvstate(BlockDriverState *bs) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; - NBDConnectThread *thr =3D s->connect_thread; + NBDClientConnection *conn =3D s->conn; bool do_free =3D false; - qemu_mutex_lock(&thr->mutex); - assert(!thr->detached); - if (thr->running) { - thr->detached =3D true; + qemu_mutex_lock(&conn->mutex); + assert(!conn->detached); + if (conn->running) { + conn->detached =3D true; } else { do_free =3D true; } - qemu_mutex_unlock(&thr->mutex); + qemu_mutex_unlock(&conn->mutex); /* the runaway thread will clean up itself */ if (do_free) { - nbd_free_connect_thread(thr); + nbd_free_connect_thread(conn); } yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); @@ -295,7 +295,7 @@ static void coroutine_fn nbd_client_co_drain_begin(Bloc= kDriverState *bs) s->drained =3D true; qemu_co_sleep_wake(&s->reconnect_sleep); - nbd_co_establish_connection_cancel(s->connect_thread); + nbd_co_establish_connection_cancel(s->conn); reconnect_delay_timer_del(s); @@ -333,7 +333,7 @@ static void nbd_teardown_connection(BlockDriverState *b= s) s->state =3D NBD_CLIENT_QUIT; if (s->connection_co) { qemu_co_sleep_wake(&s->reconnect_sleep); - nbd_co_establish_connection_cancel(s->connect_thread); + nbd_co_establish_connection_cancel(s->conn); } if (qemu_in_coroutine()) { s->teardown_co =3D qemu_coroutine_self(); @@ -360,65 +360,65 @@ static bool nbd_client_connecting_wait(BDRVNBDState *= s) static void nbd_init_connect_thread(BDRVNBDState *s) { - s->connect_thread =3D g_new(NBDConnectThread, 1); + s->conn =3D g_new(NBDClientConnection, 1); - *s->connect_thread =3D (NBDConnectThread) { + *s->conn =3D (NBDClientConnection) { .saddr =3D QAPI_CLONE(SocketAddress, s->saddr), }; - qemu_mutex_init(&s->connect_thread->mutex); + qemu_mutex_init(&s->conn->mutex); } -static void nbd_free_connect_thread(NBDConnectThread *thr) +static void nbd_free_connect_thread(NBDClientConnection *conn) { - if (thr->sioc) { - qio_channel_close(QIO_CHANNEL(thr->sioc), NULL); - object_unref(OBJECT(thr->sioc)); + if (conn->sioc) { + qio_channel_close(QIO_CHANNEL(conn->sioc), NULL); + object_unref(OBJECT(conn->sioc)); } - error_free(thr->err); - qapi_free_SocketAddress(thr->saddr); - g_free(thr); + error_free(conn->err); + qapi_free_SocketAddress(conn->saddr); + g_free(conn); } static void *connect_thread_func(void *opaque) { - NBDConnectThread *thr =3D opaque; + NBDClientConnection *conn =3D opaque; int ret; bool do_free; - thr->sioc =3D qio_channel_socket_new(); + conn->sioc =3D qio_channel_socket_new(); - error_free(thr->err); - thr->err =3D NULL; - ret =3D qio_channel_socket_connect_sync(thr->sioc, thr->saddr, &thr->e= rr); + error_free(conn->err); + conn->err =3D NULL; + ret =3D qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn= ->err); if (ret < 0) { - object_unref(OBJECT(thr->sioc)); - thr->sioc =3D NULL; + object_unref(OBJECT(conn->sioc)); + conn->sioc =3D NULL; } - qio_channel_set_delay(QIO_CHANNEL(thr->sioc), false); + qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false); - qemu_mutex_lock(&thr->mutex); + qemu_mutex_lock(&conn->mutex); - assert(thr->running); - thr->running =3D false; - if (thr->wait_co) { - aio_co_wake(thr->wait_co); - thr->wait_co =3D NULL; + assert(conn->running); + conn->running =3D false; + if (conn->wait_co) { + aio_co_wake(conn->wait_co); + conn->wait_co =3D NULL; } - do_free =3D thr->detached; + do_free =3D conn->detached; - qemu_mutex_unlock(&thr->mutex); + qemu_mutex_unlock(&conn->mutex); if (do_free) { - nbd_free_connect_thread(thr); + nbd_free_connect_thread(conn); } return NULL; } /* - * Get a new connection in context of @thr: + * Get a new connection in context of @conn: * if the thread is running, wait for completion * if the thread already succeeded in the background, and user didn't ge= t the * result, just return it now @@ -426,38 +426,38 @@ static void *connect_thread_func(void *opaque) * completion */ static coroutine_fn QIOChannelSocket * -nbd_co_establish_connection(NBDConnectThread *thr, Error **errp) +nbd_co_establish_connection(NBDClientConnection *conn, Error **errp) { QIOChannelSocket *sioc =3D NULL; QemuThread thread; - qemu_mutex_lock(&thr->mutex); + qemu_mutex_lock(&conn->mutex); /* * Don't call nbd_co_establish_connection() in several coroutines in * parallel. Only one call at once is supported. */ - assert(!thr->wait_co); + assert(!conn->wait_co); - if (!thr->running) { - if (thr->sioc) { + if (!conn->running) { + if (conn->sioc) { /* Previous attempt finally succeeded in background */ - sioc =3D g_steal_pointer(&thr->sioc); - qemu_mutex_unlock(&thr->mutex); + sioc =3D g_steal_pointer(&conn->sioc); + qemu_mutex_unlock(&conn->mutex); return sioc; } - thr->running =3D true; - error_free(thr->err); - thr->err =3D NULL; + conn->running =3D true; + error_free(conn->err); + conn->err =3D NULL; qemu_thread_create(&thread, "nbd-connect", - connect_thread_func, thr, QEMU_THREAD_DETACHED); + connect_thread_func, conn, QEMU_THREAD_DETACHED= ); } - thr->wait_co =3D qemu_coroutine_self(); + conn->wait_co =3D qemu_coroutine_self(); - qemu_mutex_unlock(&thr->mutex); + qemu_mutex_unlock(&conn->mutex); /* * We are going to wait for connect-thread finish, but @@ -465,9 +465,9 @@ nbd_co_establish_connection(NBDConnectThread *thr, Erro= r **errp) */ qemu_coroutine_yield(); - qemu_mutex_lock(&thr->mutex); + qemu_mutex_lock(&conn->mutex); - if (thr->running) { + if (conn->running) { /* * The connection attempt was canceled and the coroutine resumed * before the connection thread finished its job. Report the @@ -476,12 +476,12 @@ nbd_co_establish_connection(NBDConnectThread *thr, Er= ror **errp) */ error_setg(errp, "Connection attempt cancelled by other operation"= ); } else { - error_propagate(errp, thr->err); - thr->err =3D NULL; - sioc =3D g_steal_pointer(&thr->sioc); + error_propagate(errp, conn->err); + conn->err =3D NULL; + sioc =3D g_steal_pointer(&conn->sioc); } - qemu_mutex_unlock(&thr->mutex); + qemu_mutex_unlock(&conn->mutex); return sioc; } @@ -494,15 +494,15 @@ nbd_co_establish_connection(NBDConnectThread *thr, Er= ror **errp) * socket, but rather safely wakes nbd_co_establish_connection() which is * sleeping in yield() */ -static void nbd_co_establish_connection_cancel(NBDConnectThread *thr) +static void nbd_co_establish_connection_cancel(NBDClientConnection *conn) { Coroutine *wait_co; - qemu_mutex_lock(&thr->mutex); + qemu_mutex_lock(&conn->mutex); - wait_co =3D g_steal_pointer(&thr->wait_co); + wait_co =3D g_steal_pointer(&conn->wait_co); - qemu_mutex_unlock(&thr->mutex); + qemu_mutex_unlock(&conn->mutex); if (wait_co) { aio_co_wake(wait_co); @@ -552,7 +552,7 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDS= tate *s) s->ioc =3D NULL; } - s->sioc =3D nbd_co_establish_connection(s->connect_thread, NULL); + s->sioc =3D nbd_co_establish_connection(s->conn, NULL); if (!s->sioc) { ret =3D -ECONNREFUSED; goto out; --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790711153416.0959528824933; Tue, 15 Jun 2021 13:58:31 -0700 (PDT) Received: from localhost ([::1]:34620 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltG8b-00023D-RQ for importer2@patchew.org; Tue, 15 Jun 2021 16:58:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58198) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz0-0002UQ-J8 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:31254) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyx-00018Q-Nw for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:26 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-230-L8nTccoaOLeoNTpCN0mXRg-1; Tue, 15 Jun 2021 16:48:21 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 59CBD80ED8E; Tue, 15 Jun 2021 20:48:20 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id D211F5D9CA; Tue, 15 Jun 2021 20:48:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jzmWJ7+j2yt22NgLLb4CGPE97wxAljQ20otK6bDubQc=; b=gDpE0iYu+aOzGfmGYZOKxljsLNmwU0s++GpeYRUc56HLkHfMRUHRN1PA0VhqbK4mPBL9JE oxsjFbZZKOXetiolug3FcZtAfNwW1rzuj2U3RFAbE7kJTDrNErrmkc5NlHMe4C7wu38wnt IKaZnjynvdoWsQi41yxToEp7mfBithc= X-MC-Unique: L8nTccoaOLeoNTpCN0mXRg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 17/34] block/nbd: introduce nbd_client_connection_new() Date: Tue, 15 Jun 2021 15:47:39 -0500 Message-Id: <20210615204756.281505-18-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy This is a step of creating bs-independent nbd connection interface. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-16-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index ce8d38d17abf..e7261aeaef7b 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -358,15 +358,18 @@ static bool nbd_client_connecting_wait(BDRVNBDState *= s) return qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTING_WA= IT; } -static void nbd_init_connect_thread(BDRVNBDState *s) +static NBDClientConnection * +nbd_client_connection_new(const SocketAddress *saddr) { - s->conn =3D g_new(NBDClientConnection, 1); + NBDClientConnection *conn =3D g_new(NBDClientConnection, 1); - *s->conn =3D (NBDClientConnection) { - .saddr =3D QAPI_CLONE(SocketAddress, s->saddr), + *conn =3D (NBDClientConnection) { + .saddr =3D QAPI_CLONE(SocketAddress, saddr), }; - qemu_mutex_init(&s->conn->mutex); + qemu_mutex_init(&conn->mutex); + + return conn; } static void nbd_free_connect_thread(NBDClientConnection *conn) @@ -2230,7 +2233,7 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, goto fail; } - nbd_init_connect_thread(s); + s->conn =3D nbd_client_connection_new(s->saddr); /* * establish TCP connection, return error if it fails --=20 2.31.1 From nobody Sun May 5 03:45:44 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790913850345.66548534199046; Tue, 15 Jun 2021 14:01:53 -0700 (PDT) Received: from localhost ([::1]:42636 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGC0-0007f3-Nn for importer2@patchew.org; Tue, 15 Jun 2021 17:01:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58204) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz1-0002XR-8h for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:58958) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyy-00018o-NV for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:26 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-258-C5V3G3VEMEe_rzem72wj6Q-1; Tue, 15 Jun 2021 16:48:21 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E9D2010064B0; Tue, 15 Jun 2021 20:48:20 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FA025D9CA; Tue, 15 Jun 2021 20:48:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790104; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yrpC/1JwhBQjWgXCdr94wN9YVSaSpsW0HlNiqF3x0R4=; b=FZUFPSG/NqqC4KrwqN9XhMFRjQzNXZvkf2XA//skXmJT6j2yQNpxA5qvCUvqKCawczfUmL LG9TnKO5r0RZOeUrJDYN+rigwIvzoQr1xWPYlPElBQs2y6CwIzjLq1FkYyY+Mhmssv4xsk 6F+ldecmdon0YvK4uZY4QHHBPqv72Tk= X-MC-Unique: C5V3G3VEMEe_rzem72wj6Q-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 18/34] block/nbd: introduce nbd_client_connection_release() Date: Tue, 15 Jun 2021 15:47:40 -0500 Message-Id: <20210615204756.281505-19-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy This is a last step of creating bs-independent nbd connection interface. With next commit we can finally move it to separate file. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-17-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- block/nbd.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index e7261aeaef7b..fa6e5e85bd6f 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -127,7 +127,7 @@ typedef struct BDRVNBDState { NBDClientConnection *conn; } BDRVNBDState; -static void nbd_free_connect_thread(NBDClientConnection *conn); +static void nbd_client_connection_release(NBDClientConnection *conn); static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *s= addr, Error **errp); static coroutine_fn QIOChannelSocket * @@ -139,22 +139,9 @@ static void nbd_yank(void *opaque); static void nbd_clear_bdrvstate(BlockDriverState *bs) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; - NBDClientConnection *conn =3D s->conn; - bool do_free =3D false; - qemu_mutex_lock(&conn->mutex); - assert(!conn->detached); - if (conn->running) { - conn->detached =3D true; - } else { - do_free =3D true; - } - qemu_mutex_unlock(&conn->mutex); - - /* the runaway thread will clean up itself */ - if (do_free) { - nbd_free_connect_thread(conn); - } + nbd_client_connection_release(s->conn); + s->conn =3D NULL; yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); @@ -372,7 +359,7 @@ nbd_client_connection_new(const SocketAddress *saddr) return conn; } -static void nbd_free_connect_thread(NBDClientConnection *conn) +static void nbd_client_connection_do_free(NBDClientConnection *conn) { if (conn->sioc) { qio_channel_close(QIO_CHANNEL(conn->sioc), NULL); @@ -414,12 +401,34 @@ static void *connect_thread_func(void *opaque) qemu_mutex_unlock(&conn->mutex); if (do_free) { - nbd_free_connect_thread(conn); + nbd_client_connection_do_free(conn); } return NULL; } +static void nbd_client_connection_release(NBDClientConnection *conn) +{ + bool do_free =3D false; + + if (!conn) { + return; + } + + qemu_mutex_lock(&conn->mutex); + assert(!conn->detached); + if (conn->running) { + conn->detached =3D true; + } else { + do_free =3D true; + } + qemu_mutex_unlock(&conn->mutex); + + if (do_free) { + nbd_client_connection_do_free(conn); + } +} + /* * Get a new connection in context of @conn: * if the thread is running, wait for completion --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791585215136.61503707922986; Tue, 15 Jun 2021 14:13:05 -0700 (PDT) Received: from localhost ([::1]:48400 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGMp-0005wI-VF for importer2@patchew.org; Tue, 15 Jun 2021 17:13:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzA-0002qH-Fl for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:36 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:58622) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyy-00018q-TU for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:35 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-140--vxgKsD2MYSM0eYGu3MUqA-1; Tue, 15 Jun 2021 16:48:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A8935100C61A; Tue, 15 Jun 2021 20:48:21 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 301FC5D9CA; Tue, 15 Jun 2021 20:48:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790104; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QdrE4d7vMv4zt88o+nX63h1bWvkDQO/Bc3c70YHnaXY=; b=irb8Mj4SuRbqUf8bAoBoNBRoctds5mhA7djksUsSmhQg3CZiD8qXL4WjRGmGqlaVIZu6th efL20Zd+6MKWGqWQ0u/IlBctDVDwCIMrk30P4cFnY99YG4RpWDtJDHnbwnNjgpjOIAvK2z ZquZvAOxnFEobzRao2C6KTsnfwWUX7s= X-MC-Unique: -vxgKsD2MYSM0eYGu3MUqA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 19/34] nbd: move connection code from block/nbd to nbd/client-connection Date: Tue, 15 Jun 2021 15:47:41 -0500 Message-Id: <20210615204756.281505-20-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We now have bs-independent connection API, which consists of four functions: nbd_client_connection_new() nbd_client_connection_release() nbd_co_establish_connection() nbd_co_establish_connection_cancel() Move them to a separate file together with NBDClientConnection structure which becomes private to the new API. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-18-vsementsov@virtuozzo.com> [eblake: comment tweaks] Signed-off-by: Eric Blake --- include/block/nbd.h | 11 ++ block/nbd.c | 207 ----------------------------------- nbd/client-connection.c | 232 ++++++++++++++++++++++++++++++++++++++++ nbd/meson.build | 1 + 4 files changed, 244 insertions(+), 207 deletions(-) create mode 100644 nbd/client-connection.c diff --git a/include/block/nbd.h b/include/block/nbd.h index 5f34d23bb037..57381be76fc5 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -406,4 +406,15 @@ const char *nbd_info_lookup(uint16_t info); const char *nbd_cmd_lookup(uint16_t info); const char *nbd_err_lookup(int err); +/* nbd/client-connection.c */ +typedef struct NBDClientConnection NBDClientConnection; + +NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr); +void nbd_client_connection_release(NBDClientConnection *conn); + +QIOChannelSocket *coroutine_fn +nbd_co_establish_connection(NBDClientConnection *conn, Error **errp); + +void coroutine_fn nbd_co_establish_connection_cancel(NBDClientConnection *= conn); + #endif diff --git a/block/nbd.c b/block/nbd.c index fa6e5e85bd6f..26914509f153 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -66,33 +66,6 @@ typedef enum NBDClientState { NBD_CLIENT_QUIT } NBDClientState; -typedef struct NBDClientConnection { - /* Initialization constants */ - SocketAddress *saddr; /* address to connect to */ - - QemuMutex mutex; - - /* - * @sioc and @err represent a connection attempt. While running - * is true, they are only used by the connection thread, and mutex - * locking is not needed. Once the thread finishes, - * nbd_co_establish_connection then steals these pointers while - * under the mutex. - */ - QIOChannelSocket *sioc; - Error *err; - - /* All further fields are accessed only under mutex */ - bool running; /* thread is running now */ - bool detached; /* thread is detached and should cleanup the state */ - - /* - * wait_co: if non-NULL, which coroutine to wake in - * nbd_co_establish_connection() after yield() - */ - Coroutine *wait_co; -} NBDClientConnection; - typedef struct BDRVNBDState { QIOChannelSocket *sioc; /* The master data channel */ QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) = */ @@ -127,12 +100,8 @@ typedef struct BDRVNBDState { NBDClientConnection *conn; } BDRVNBDState; -static void nbd_client_connection_release(NBDClientConnection *conn); static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *s= addr, Error **errp); -static coroutine_fn QIOChannelSocket * -nbd_co_establish_connection(NBDClientConnection *conn, Error **errp); -static void nbd_co_establish_connection_cancel(NBDClientConnection *conn); static int nbd_client_handshake(BlockDriverState *bs, Error **errp); static void nbd_yank(void *opaque); @@ -345,182 +314,6 @@ static bool nbd_client_connecting_wait(BDRVNBDState *= s) return qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTING_WA= IT; } -static NBDClientConnection * -nbd_client_connection_new(const SocketAddress *saddr) -{ - NBDClientConnection *conn =3D g_new(NBDClientConnection, 1); - - *conn =3D (NBDClientConnection) { - .saddr =3D QAPI_CLONE(SocketAddress, saddr), - }; - - qemu_mutex_init(&conn->mutex); - - return conn; -} - -static void nbd_client_connection_do_free(NBDClientConnection *conn) -{ - if (conn->sioc) { - qio_channel_close(QIO_CHANNEL(conn->sioc), NULL); - object_unref(OBJECT(conn->sioc)); - } - error_free(conn->err); - qapi_free_SocketAddress(conn->saddr); - g_free(conn); -} - -static void *connect_thread_func(void *opaque) -{ - NBDClientConnection *conn =3D opaque; - int ret; - bool do_free; - - conn->sioc =3D qio_channel_socket_new(); - - error_free(conn->err); - conn->err =3D NULL; - ret =3D qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn= ->err); - if (ret < 0) { - object_unref(OBJECT(conn->sioc)); - conn->sioc =3D NULL; - } - - qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false); - - qemu_mutex_lock(&conn->mutex); - - assert(conn->running); - conn->running =3D false; - if (conn->wait_co) { - aio_co_wake(conn->wait_co); - conn->wait_co =3D NULL; - } - do_free =3D conn->detached; - - qemu_mutex_unlock(&conn->mutex); - - if (do_free) { - nbd_client_connection_do_free(conn); - } - - return NULL; -} - -static void nbd_client_connection_release(NBDClientConnection *conn) -{ - bool do_free =3D false; - - if (!conn) { - return; - } - - qemu_mutex_lock(&conn->mutex); - assert(!conn->detached); - if (conn->running) { - conn->detached =3D true; - } else { - do_free =3D true; - } - qemu_mutex_unlock(&conn->mutex); - - if (do_free) { - nbd_client_connection_do_free(conn); - } -} - -/* - * Get a new connection in context of @conn: - * if the thread is running, wait for completion - * if the thread already succeeded in the background, and user didn't ge= t the - * result, just return it now - * otherwise the thread is not running, so start a thread and wait for - * completion - */ -static coroutine_fn QIOChannelSocket * -nbd_co_establish_connection(NBDClientConnection *conn, Error **errp) -{ - QIOChannelSocket *sioc =3D NULL; - QemuThread thread; - - qemu_mutex_lock(&conn->mutex); - - /* - * Don't call nbd_co_establish_connection() in several coroutines in - * parallel. Only one call at once is supported. - */ - assert(!conn->wait_co); - - if (!conn->running) { - if (conn->sioc) { - /* Previous attempt finally succeeded in background */ - sioc =3D g_steal_pointer(&conn->sioc); - qemu_mutex_unlock(&conn->mutex); - - return sioc; - } - - conn->running =3D true; - error_free(conn->err); - conn->err =3D NULL; - qemu_thread_create(&thread, "nbd-connect", - connect_thread_func, conn, QEMU_THREAD_DETACHED= ); - } - - conn->wait_co =3D qemu_coroutine_self(); - - qemu_mutex_unlock(&conn->mutex); - - /* - * We are going to wait for connect-thread finish, but - * nbd_co_establish_connection_cancel() can interrupt. - */ - qemu_coroutine_yield(); - - qemu_mutex_lock(&conn->mutex); - - if (conn->running) { - /* - * The connection attempt was canceled and the coroutine resumed - * before the connection thread finished its job. Report the - * attempt as failed, but leave the connection thread running, - * to reuse it for the next connection attempt. - */ - error_setg(errp, "Connection attempt cancelled by other operation"= ); - } else { - error_propagate(errp, conn->err); - conn->err =3D NULL; - sioc =3D g_steal_pointer(&conn->sioc); - } - - qemu_mutex_unlock(&conn->mutex); - - return sioc; -} - -/* - * nbd_co_establish_connection_cancel - * Cancel nbd_co_establish_connection() asynchronously. - * - * Note that this function neither directly stops the thread nor closes the - * socket, but rather safely wakes nbd_co_establish_connection() which is - * sleeping in yield() - */ -static void nbd_co_establish_connection_cancel(NBDClientConnection *conn) -{ - Coroutine *wait_co; - - qemu_mutex_lock(&conn->mutex); - - wait_co =3D g_steal_pointer(&conn->wait_co); - - qemu_mutex_unlock(&conn->mutex); - - if (wait_co) { - aio_co_wake(wait_co); - } -} - static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { int ret; diff --git a/nbd/client-connection.c b/nbd/client-connection.c new file mode 100644 index 000000000000..f3b270d38cc8 --- /dev/null +++ b/nbd/client-connection.c @@ -0,0 +1,232 @@ +/* + * QEMU Block driver for NBD + * + * Copyright (c) 2021 Virtuozzo International GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" + +#include "block/nbd.h" + +#include "qapi/qapi-visit-sockets.h" +#include "qapi/clone-visitor.h" + +struct NBDClientConnection { + /* Initialization constants */ + SocketAddress *saddr; /* address to connect to */ + + QemuMutex mutex; + + /* + * @sioc and @err represent a connection attempt. While running + * is true, they are only used by the connection thread, and mutex + * locking is not needed. Once the thread finishes, + * nbd_co_establish_connection then steals these pointers while + * under the mutex. + */ + QIOChannelSocket *sioc; + Error *err; + + /* All further fields are accessed only under mutex */ + bool running; /* thread is running now */ + bool detached; /* thread is detached and should cleanup the state */ + + /* + * wait_co: if non-NULL, which coroutine to wake in + * nbd_co_establish_connection() after yield() + */ + Coroutine *wait_co; +}; + +NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr) +{ + NBDClientConnection *conn =3D g_new(NBDClientConnection, 1); + + *conn =3D (NBDClientConnection) { + .saddr =3D QAPI_CLONE(SocketAddress, saddr), + }; + + qemu_mutex_init(&conn->mutex); + + return conn; +} + +static void nbd_client_connection_do_free(NBDClientConnection *conn) +{ + if (conn->sioc) { + qio_channel_close(QIO_CHANNEL(conn->sioc), NULL); + object_unref(OBJECT(conn->sioc)); + } + error_free(conn->err); + qapi_free_SocketAddress(conn->saddr); + g_free(conn); +} + +static void *connect_thread_func(void *opaque) +{ + NBDClientConnection *conn =3D opaque; + int ret; + bool do_free; + + conn->sioc =3D qio_channel_socket_new(); + + error_free(conn->err); + conn->err =3D NULL; + ret =3D qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn= ->err); + if (ret < 0) { + object_unref(OBJECT(conn->sioc)); + conn->sioc =3D NULL; + } + + qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false); + + qemu_mutex_lock(&conn->mutex); + + assert(conn->running); + conn->running =3D false; + if (conn->wait_co) { + aio_co_wake(conn->wait_co); + conn->wait_co =3D NULL; + } + do_free =3D conn->detached; + + qemu_mutex_unlock(&conn->mutex); + + if (do_free) { + nbd_client_connection_do_free(conn); + } + + return NULL; +} + +void nbd_client_connection_release(NBDClientConnection *conn) +{ + bool do_free =3D false; + + if (!conn) { + return; + } + + qemu_mutex_lock(&conn->mutex); + assert(!conn->detached); + if (conn->running) { + conn->detached =3D true; + } else { + do_free =3D true; + } + qemu_mutex_unlock(&conn->mutex); + + if (do_free) { + nbd_client_connection_do_free(conn); + } +} + +/* + * Get a new connection in context of @conn: + * if the thread is running, wait for completion + * if the thread already succeeded in the background, and user didn't ge= t the + * result, just return it now + * otherwise the thread is not running, so start a thread and wait for + * completion + */ +QIOChannelSocket *coroutine_fn +nbd_co_establish_connection(NBDClientConnection *conn, Error **errp) +{ + QIOChannelSocket *sioc =3D NULL; + QemuThread thread; + + qemu_mutex_lock(&conn->mutex); + + /* + * Don't call nbd_co_establish_connection() in several coroutines in + * parallel. Only one call at once is supported. + */ + assert(!conn->wait_co); + + if (!conn->running) { + if (conn->sioc) { + /* Previous attempt finally succeeded in background */ + sioc =3D g_steal_pointer(&conn->sioc); + qemu_mutex_unlock(&conn->mutex); + + return sioc; + } + + conn->running =3D true; + error_free(conn->err); + conn->err =3D NULL; + qemu_thread_create(&thread, "nbd-connect", + connect_thread_func, conn, QEMU_THREAD_DETACHED= ); + } + + conn->wait_co =3D qemu_coroutine_self(); + + qemu_mutex_unlock(&conn->mutex); + + /* + * We are going to wait for connect-thread finish, but + * nbd_co_establish_connection_cancel() can interrupt. + */ + qemu_coroutine_yield(); + + qemu_mutex_lock(&conn->mutex); + + if (conn->running) { + /* + * The connection attempt was canceled and the coroutine resumed + * before the connection thread finished its job. Report the + * attempt as failed, but leave the connection thread running, + * to reuse it for the next connection attempt. + */ + error_setg(errp, "Connection attempt cancelled by other operation"= ); + } else { + error_propagate(errp, conn->err); + conn->err =3D NULL; + sioc =3D g_steal_pointer(&conn->sioc); + } + + qemu_mutex_unlock(&conn->mutex); + + return sioc; +} + +/* + * nbd_co_establish_connection_cancel + * Cancel nbd_co_establish_connection() asynchronously. + * + * Note that this function neither directly stops the thread nor closes the + * socket, but rather safely wakes nbd_co_establish_connection() which is + * sleeping in yield() + */ +void nbd_co_establish_connection_cancel(NBDClientConnection *conn) +{ + Coroutine *wait_co; + + qemu_mutex_lock(&conn->mutex); + + wait_co =3D g_steal_pointer(&conn->wait_co); + + qemu_mutex_unlock(&conn->mutex); + + if (wait_co) { + aio_co_wake(wait_co); + } +} diff --git a/nbd/meson.build b/nbd/meson.build index 2baaa3694801..b26d70565ebb 100644 --- a/nbd/meson.build +++ b/nbd/meson.build @@ -1,5 +1,6 @@ block_ss.add(files( 'client.c', + 'client-connection.c', 'common.c', )) blockdev_ss.add(files( --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623790919522427.58546240746637; Tue, 15 Jun 2021 14:01:59 -0700 (PDT) Received: from localhost ([::1]:42922 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGC6-0007qu-0Q for importer2@patchew.org; Tue, 15 Jun 2021 17:01:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58288) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz4-0002iW-HV for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:55744) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFyz-0001A6-K0 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:30 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-497-JSV2XO9WPCqYnsJM_g9xCg-1; Tue, 15 Jun 2021 16:48:23 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 30C07FC9E; Tue, 15 Jun 2021 20:48:22 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id D63F75D9CA; Tue, 15 Jun 2021 20:48:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790105; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s2+zlhDaNJzWudZvz0cVxV+WVRcQjyu9L8bzdOoRfmk=; b=JsLbPZV4qF03M2HhYuTWMRigUyqdxqTfVnFabRkPZGNO596SyZYLVzv1NKga9ejyM9qF0q RwsvIPHD41RUhKCiinW3EwUWTyjusxtdjq+m2b4mKkYr/107Vb985wt24/i6bc38rlZ5+V BJEUsc6x2Xnl8tyaIBXvqq7F7gMGl9g= X-MC-Unique: JSV2XO9WPCqYnsJM_g9xCg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 20/34] nbd/client-connection: use QEMU_LOCK_GUARD Date: Tue, 15 Jun 2021 15:47:42 -0500 Message-Id: <20210615204756.281505-21-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We don't update connect_thread_func() to use QEMU_LOCK_GUARD, as it will get more complex critical sections logic in further commit, where QEMU_LOCK_GUARD doesn't help. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-19-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- nbd/client-connection.c | 95 +++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/nbd/client-connection.c b/nbd/client-connection.c index f3b270d38cc8..eb5cae2eaea4 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -125,14 +125,14 @@ void nbd_client_connection_release(NBDClientConnectio= n *conn) return; } - qemu_mutex_lock(&conn->mutex); - assert(!conn->detached); - if (conn->running) { - conn->detached =3D true; - } else { - do_free =3D true; + WITH_QEMU_LOCK_GUARD(&conn->mutex) { + assert(!conn->detached); + if (conn->running) { + conn->detached =3D true; + } else { + do_free =3D true; + } } - qemu_mutex_unlock(&conn->mutex); if (do_free) { nbd_client_connection_do_free(conn); @@ -150,62 +150,55 @@ void nbd_client_connection_release(NBDClientConnectio= n *conn) QIOChannelSocket *coroutine_fn nbd_co_establish_connection(NBDClientConnection *conn, Error **errp) { - QIOChannelSocket *sioc =3D NULL; QemuThread thread; - qemu_mutex_lock(&conn->mutex); + WITH_QEMU_LOCK_GUARD(&conn->mutex) { + /* + * Don't call nbd_co_establish_connection() in several coroutines = in + * parallel. Only one call at once is supported. + */ + assert(!conn->wait_co); - /* - * Don't call nbd_co_establish_connection() in several coroutines in - * parallel. Only one call at once is supported. - */ - assert(!conn->wait_co); + if (!conn->running) { + if (conn->sioc) { + /* Previous attempt finally succeeded in background */ + return g_steal_pointer(&conn->sioc); + } - if (!conn->running) { - if (conn->sioc) { - /* Previous attempt finally succeeded in background */ - sioc =3D g_steal_pointer(&conn->sioc); - qemu_mutex_unlock(&conn->mutex); - - return sioc; + conn->running =3D true; + error_free(conn->err); + conn->err =3D NULL; + qemu_thread_create(&thread, "nbd-connect", + connect_thread_func, conn, QEMU_THREAD_DETA= CHED); } - conn->running =3D true; - error_free(conn->err); - conn->err =3D NULL; - qemu_thread_create(&thread, "nbd-connect", - connect_thread_func, conn, QEMU_THREAD_DETACHED= ); + conn->wait_co =3D qemu_coroutine_self(); } - conn->wait_co =3D qemu_coroutine_self(); - - qemu_mutex_unlock(&conn->mutex); - /* * We are going to wait for connect-thread finish, but * nbd_co_establish_connection_cancel() can interrupt. */ qemu_coroutine_yield(); - qemu_mutex_lock(&conn->mutex); - - if (conn->running) { - /* - * The connection attempt was canceled and the coroutine resumed - * before the connection thread finished its job. Report the - * attempt as failed, but leave the connection thread running, - * to reuse it for the next connection attempt. - */ - error_setg(errp, "Connection attempt cancelled by other operation"= ); - } else { - error_propagate(errp, conn->err); - conn->err =3D NULL; - sioc =3D g_steal_pointer(&conn->sioc); + WITH_QEMU_LOCK_GUARD(&conn->mutex) { + if (conn->running) { + /* + * The connection attempt was canceled and the coroutine resum= ed + * before the connection thread finished its job. Report the + * attempt as failed, but leave the connection thread running, + * to reuse it for the next connection attempt. + */ + error_setg(errp, "Connection attempt cancelled by other operat= ion"); + return NULL; + } else { + error_propagate(errp, conn->err); + conn->err =3D NULL; + return g_steal_pointer(&conn->sioc); + } } - qemu_mutex_unlock(&conn->mutex); - - return sioc; + abort(); /* unreachable */ } /* @@ -220,11 +213,9 @@ void nbd_co_establish_connection_cancel(NBDClientConne= ction *conn) { Coroutine *wait_co; - qemu_mutex_lock(&conn->mutex); - - wait_co =3D g_steal_pointer(&conn->wait_co); - - qemu_mutex_unlock(&conn->mutex); + WITH_QEMU_LOCK_GUARD(&conn->mutex) { + wait_co =3D g_steal_pointer(&conn->wait_co); + } if (wait_co) { aio_co_wake(wait_co); --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162379128625187.00797583028316; Tue, 15 Jun 2021 14:08:06 -0700 (PDT) Received: from localhost ([::1]:33766 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGI1-0004KY-6t for importer2@patchew.org; Tue, 15 Jun 2021 17:08:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58330) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0002l6-PU for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:38284) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz0-0001B3-Ri for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:34 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-276-ek91K7jPOKiUYY4LHwS39Q-1; Tue, 15 Jun 2021 16:48:23 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D0D0B100C621; Tue, 15 Jun 2021 20:48:22 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64B2E5D9E2; Tue, 15 Jun 2021 20:48:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dFMVKfQtMMpQYNRwQPBWHumqxuiHO+WDs9tT8kpnAiw=; b=KiQdN9TgyIXLim7N4ufwtavsD/EnhHjQR76+tUM8y26FaJdPg+bRUYkNFKq7kl4Cd+JOcM 9T9Tjzy316vud6/xruZuoX7jlvjrf4omX/EnQbsJBBtLVMPEZQ2TMWjsKCdhxoLpkjdupe YNCn43u9gBl0OgQO4J3gAajFOkdul2U= X-MC-Unique: ek91K7jPOKiUYY4LHwS39Q-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 21/34] nbd/client-connection: add possibility of negotiation Date: Tue, 15 Jun 2021 15:47:43 -0500 Message-Id: <20210615204756.281505-22-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Add arguments and logic to support nbd negotiation in the same thread after successful connection. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-20-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- include/block/nbd.h | 9 +++- block/nbd.c | 4 +- nbd/client-connection.c | 105 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 109 insertions(+), 9 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 57381be76fc5..5d86e6a393ff 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -409,11 +409,16 @@ const char *nbd_err_lookup(int err); /* nbd/client-connection.c */ typedef struct NBDClientConnection NBDClientConnection; -NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr); +NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, + bool do_negotiation, + const char *export_name, + const char *x_dirty_bitmap, + QCryptoTLSCreds *tlscreds); void nbd_client_connection_release(NBDClientConnection *conn); QIOChannelSocket *coroutine_fn -nbd_co_establish_connection(NBDClientConnection *conn, Error **errp); +nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, + QIOChannel **ioc, Error **errp); void coroutine_fn nbd_co_establish_connection_cancel(NBDClientConnection *= conn); diff --git a/block/nbd.c b/block/nbd.c index 26914509f153..df9d241313f4 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -357,7 +357,7 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDS= tate *s) s->ioc =3D NULL; } - s->sioc =3D nbd_co_establish_connection(s->conn, NULL); + s->sioc =3D nbd_co_establish_connection(s->conn, NULL, NULL, NULL); if (!s->sioc) { ret =3D -ECONNREFUSED; goto out; @@ -2035,7 +2035,7 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, goto fail; } - s->conn =3D nbd_client_connection_new(s->saddr); + s->conn =3D nbd_client_connection_new(s->saddr, false, NULL, NULL, NUL= L); /* * establish TCP connection, return error if it fails diff --git a/nbd/client-connection.c b/nbd/client-connection.c index eb5cae2eaea4..4ed37cd73fa1 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -30,8 +30,11 @@ #include "qapi/clone-visitor.h" struct NBDClientConnection { - /* Initialization constants */ + /* Initialization constants, never change */ SocketAddress *saddr; /* address to connect to */ + QCryptoTLSCreds *tlscreds; + NBDExportInfo initial_info; + bool do_negotiation; QemuMutex mutex; @@ -42,7 +45,9 @@ struct NBDClientConnection { * nbd_co_establish_connection then steals these pointers while * under the mutex. */ + NBDExportInfo updated_info; QIOChannelSocket *sioc; + QIOChannel *ioc; Error *err; /* All further fields are accessed only under mutex */ @@ -56,12 +61,25 @@ struct NBDClientConnection { Coroutine *wait_co; }; -NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr) +NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, + bool do_negotiation, + const char *export_name, + const char *x_dirty_bitmap, + QCryptoTLSCreds *tlscreds) { NBDClientConnection *conn =3D g_new(NBDClientConnection, 1); + object_ref(OBJECT(tlscreds)); *conn =3D (NBDClientConnection) { .saddr =3D QAPI_CLONE(SocketAddress, saddr), + .tlscreds =3D tlscreds, + .do_negotiation =3D do_negotiation, + + .initial_info.request_sizes =3D true, + .initial_info.structured_reply =3D true, + .initial_info.base_allocation =3D true, + .initial_info.x_dirty_bitmap =3D g_strdup(x_dirty_bitmap), + .initial_info.name =3D g_strdup(export_name ?: "") }; qemu_mutex_init(&conn->mutex); @@ -77,9 +95,61 @@ static void nbd_client_connection_do_free(NBDClientConne= ction *conn) } error_free(conn->err); qapi_free_SocketAddress(conn->saddr); + object_unref(OBJECT(conn->tlscreds)); + g_free(conn->initial_info.x_dirty_bitmap); + g_free(conn->initial_info.name); g_free(conn); } +/* + * Connect to @addr and do NBD negotiation if @info is not null. If @tlscr= eds + * are given @outioc is returned. @outioc is provided only on success. Th= e call + * may be cancelled from other thread by simply qio_channel_shutdown(sioc). + */ +static int nbd_connect(QIOChannelSocket *sioc, SocketAddress *addr, + NBDExportInfo *info, QCryptoTLSCreds *tlscreds, + QIOChannel **outioc, Error **errp) +{ + int ret; + + if (outioc) { + *outioc =3D NULL; + } + + ret =3D qio_channel_socket_connect_sync(sioc, addr, errp); + if (ret < 0) { + return ret; + } + + qio_channel_set_delay(QIO_CHANNEL(sioc), false); + + if (!info) { + return 0; + } + + ret =3D nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), tlscreds, + tlscreds ? addr->u.inet.host : NULL, + outioc, info, errp); + if (ret < 0) { + /* + * nbd_receive_negotiate() may setup tls ioc and return it even on + * failure path. In this case we should use it instead of original + * channel. + */ + if (outioc && *outioc) { + qio_channel_close(QIO_CHANNEL(*outioc), NULL); + object_unref(OBJECT(*outioc)); + *outioc =3D NULL; + } else { + qio_channel_close(QIO_CHANNEL(sioc), NULL); + } + + return ret; + } + + return 0; +} + static void *connect_thread_func(void *opaque) { NBDClientConnection *conn =3D opaque; @@ -90,13 +160,18 @@ static void *connect_thread_func(void *opaque) error_free(conn->err); conn->err =3D NULL; - ret =3D qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn= ->err); + conn->updated_info =3D conn->initial_info; + + ret =3D nbd_connect(conn->sioc, conn->saddr, + conn->do_negotiation ? &conn->updated_info : NULL, + conn->tlscreds, &conn->ioc, &conn->err); if (ret < 0) { object_unref(OBJECT(conn->sioc)); conn->sioc =3D NULL; } - qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false); + conn->updated_info.x_dirty_bitmap =3D NULL; + conn->updated_info.name =3D NULL; qemu_mutex_lock(&conn->mutex); @@ -146,12 +221,24 @@ void nbd_client_connection_release(NBDClientConnectio= n *conn) * result, just return it now * otherwise the thread is not running, so start a thread and wait for * completion + * + * If @info is not NULL, also do nbd-negotiation after successful connecti= on. + * In this case info is used only as out parameter, and is fully initializ= ed by + * nbd_co_establish_connection(). "IN" fields of info as well as related o= nly to + * nbd_receive_export_list() would be zero (see description of NBDExportIn= fo in + * include/block/nbd.h). */ QIOChannelSocket *coroutine_fn -nbd_co_establish_connection(NBDClientConnection *conn, Error **errp) +nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, + QIOChannel **ioc, Error **errp) { QemuThread thread; + if (conn->do_negotiation) { + assert(info); + assert(ioc); + } + WITH_QEMU_LOCK_GUARD(&conn->mutex) { /* * Don't call nbd_co_establish_connection() in several coroutines = in @@ -162,6 +249,10 @@ nbd_co_establish_connection(NBDClientConnection *conn,= Error **errp) if (!conn->running) { if (conn->sioc) { /* Previous attempt finally succeeded in background */ + if (conn->do_negotiation) { + *ioc =3D g_steal_pointer(&conn->ioc); + memcpy(info, &conn->updated_info, sizeof(*info)); + } return g_steal_pointer(&conn->sioc); } @@ -194,6 +285,10 @@ nbd_co_establish_connection(NBDClientConnection *conn,= Error **errp) } else { error_propagate(errp, conn->err); conn->err =3D NULL; + if (conn->sioc && conn->do_negotiation) { + *ioc =3D g_steal_pointer(&conn->ioc); + memcpy(info, &conn->updated_info, sizeof(*info)); + } return g_steal_pointer(&conn->sioc); } } --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791061875401.1856161397535; Tue, 15 Jun 2021 14:04:21 -0700 (PDT) Received: from localhost ([::1]:51678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGEO-0005b7-SE for importer2@patchew.org; Tue, 15 Jun 2021 17:04:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58328) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0002kh-LP for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50035) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz1-0001BJ-1q for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:34 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-479-t56aIzLYNfWOJqgt28RR2w-1; Tue, 15 Jun 2021 16:48:24 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6F5DD100C609; Tue, 15 Jun 2021 20:48:23 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 052925D9CA; Tue, 15 Jun 2021 20:48:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8iBpRtC0NrALa44ob+kWRWI5IIL6PR0RL+YVsIDMG98=; b=G+ycXXuQHv8D7/mqsdihBxZXzdCP8FABsypKDBxLXdOjciwh9e90nMR0jwbopCy6lAlUzQ 1P5awIkGCWFu/mlX2zCXIVZ/c54cMJb3M6Bnxuw6Hf4HCg33OhpdgkNu2R/wuK/svu2pjV pxqY+6r3NlyA1ddcvKXdtiutFSdEZw8= X-MC-Unique: t56aIzLYNfWOJqgt28RR2w-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 22/34] nbd/client-connection: implement connection retry Date: Tue, 15 Jun 2021 15:47:44 -0500 Message-Id: <20210615204756.281505-23-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Add an option for a thread to retry connecting until it succeeds. We'll use nbd/client-connection both for reconnect and for initial connection in nbd_open(), so we need a possibility to use same NBDClientConnection instance to connect once in nbd_open() and then use retry semantics for reconnect. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-21-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake [eblake: grammar tweak] Signed-off-by: Eric Blake --- include/block/nbd.h | 2 ++ nbd/client-connection.c | 56 +++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 5d86e6a393ff..5bb54d831c8a 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -409,6 +409,8 @@ const char *nbd_err_lookup(int err); /* nbd/client-connection.c */ typedef struct NBDClientConnection NBDClientConnection; +void nbd_client_connection_enable_retry(NBDClientConnection *conn); + NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, bool do_negotiation, const char *export_name, diff --git a/nbd/client-connection.c b/nbd/client-connection.c index 4ed37cd73fa1..032b38ed3e9b 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -35,6 +35,7 @@ struct NBDClientConnection { QCryptoTLSCreds *tlscreds; NBDExportInfo initial_info; bool do_negotiation; + bool do_retry; QemuMutex mutex; @@ -61,6 +62,15 @@ struct NBDClientConnection { Coroutine *wait_co; }; +/* + * The function isn't protected by any mutex, only call it when the client + * connection attempt has not yet started. + */ +void nbd_client_connection_enable_retry(NBDClientConnection *conn) +{ + conn->do_retry =3D true; +} + NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, bool do_negotiation, const char *export_name, @@ -155,24 +165,44 @@ static void *connect_thread_func(void *opaque) NBDClientConnection *conn =3D opaque; int ret; bool do_free; + uint64_t timeout =3D 1; + uint64_t max_timeout =3D 16; - conn->sioc =3D qio_channel_socket_new(); + while (true) { + conn->sioc =3D qio_channel_socket_new(); - error_free(conn->err); - conn->err =3D NULL; - conn->updated_info =3D conn->initial_info; + error_free(conn->err); + conn->err =3D NULL; + conn->updated_info =3D conn->initial_info; - ret =3D nbd_connect(conn->sioc, conn->saddr, - conn->do_negotiation ? &conn->updated_info : NULL, - conn->tlscreds, &conn->ioc, &conn->err); - if (ret < 0) { - object_unref(OBJECT(conn->sioc)); - conn->sioc =3D NULL; + ret =3D nbd_connect(conn->sioc, conn->saddr, + conn->do_negotiation ? &conn->updated_info : NUL= L, + conn->tlscreds, &conn->ioc, &conn->err); + + /* + * conn->updated_info will finally be returned to the user. Clear = the + * pointers to our internally allocated strings, which are IN para= meters + * of nbd_receive_negotiate() and therefore nbd_connect(). Caller + * shoudn't be interested in these fields. + */ + conn->updated_info.x_dirty_bitmap =3D NULL; + conn->updated_info.name =3D NULL; + + if (ret < 0) { + object_unref(OBJECT(conn->sioc)); + conn->sioc =3D NULL; + if (conn->do_retry) { + sleep(timeout); + if (timeout < max_timeout) { + timeout *=3D 2; + } + continue; + } + } + + break; } - conn->updated_info.x_dirty_bitmap =3D NULL; - conn->updated_info.name =3D NULL; - qemu_mutex_lock(&conn->mutex); assert(conn->running); --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791415735681.6151833312136; Tue, 15 Jun 2021 14:10:15 -0700 (PDT) Received: from localhost ([::1]:42160 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGK6-0001ft-Kv for importer2@patchew.org; Tue, 15 Jun 2021 17:10:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58380) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzA-0002qn-Mn for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:36 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33288) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz1-0001Bq-LF for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:36 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-266-HnsWyvyZP0mAiOl73MQmZQ-1; Tue, 15 Jun 2021 16:48:24 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F073E107ACF6; Tue, 15 Jun 2021 20:48:23 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id A04BE5D9CA; Tue, 15 Jun 2021 20:48:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vzFaETPbCpP681WQuP7XvvfW/B9P/wTmbMtNfUCE9lE=; b=b5L8pEWV78dHGvMiGzi36uDqFOfhGE0b6+z89tfikZ/uCLoP6byfdkocg8wOBCsPcuh8eV 5XMCQcL3nIJWc4IB8dXcD7AiQXItjZbiLL972G6WM8071UutkJ5VANuhXnWDxHtG6GBXy2 o1kNduspfKUDNxt4Ls4DWSc+Z0wjcz0= X-MC-Unique: HnsWyvyZP0mAiOl73MQmZQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 23/34] nbd/client-connection: shutdown connection on release Date: Tue, 15 Jun 2021 15:47:45 -0500 Message-Id: <20210615204756.281505-24-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Now, when a thread can do negotiation and retry, it may run relatively long. We need a mechanism to stop it, when the user is not interested in a result any more. So, on nbd_client_connection_release() let's shutdown the socket, and do not retry connection if thread is detached. Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-22-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- nbd/client-connection.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/nbd/client-connection.c b/nbd/client-connection.c index 032b38ed3e9b..883f9cf158cb 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -168,9 +168,13 @@ static void *connect_thread_func(void *opaque) uint64_t timeout =3D 1; uint64_t max_timeout =3D 16; - while (true) { + qemu_mutex_lock(&conn->mutex); + while (!conn->detached) { + assert(!conn->sioc); conn->sioc =3D qio_channel_socket_new(); + qemu_mutex_unlock(&conn->mutex); + error_free(conn->err); conn->err =3D NULL; conn->updated_info =3D conn->initial_info; @@ -188,14 +192,20 @@ static void *connect_thread_func(void *opaque) conn->updated_info.x_dirty_bitmap =3D NULL; conn->updated_info.name =3D NULL; + qemu_mutex_lock(&conn->mutex); + if (ret < 0) { object_unref(OBJECT(conn->sioc)); conn->sioc =3D NULL; - if (conn->do_retry) { + if (conn->do_retry && !conn->detached) { + qemu_mutex_unlock(&conn->mutex); + sleep(timeout); if (timeout < max_timeout) { timeout *=3D 2; } + + qemu_mutex_lock(&conn->mutex); continue; } } @@ -203,7 +213,7 @@ static void *connect_thread_func(void *opaque) break; } - qemu_mutex_lock(&conn->mutex); + /* mutex is locked */ assert(conn->running); conn->running =3D false; @@ -237,6 +247,10 @@ void nbd_client_connection_release(NBDClientConnection= *conn) } else { do_free =3D true; } + if (conn->sioc) { + qio_channel_shutdown(QIO_CHANNEL(conn->sioc), + QIO_CHANNEL_SHUTDOWN_BOTH, NULL); + } } if (do_free) { --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791645698665.3061353408923; Tue, 15 Jun 2021 14:14:05 -0700 (PDT) Received: from localhost ([::1]:51400 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGNo-0007yP-MN for importer2@patchew.org; Tue, 15 Jun 2021 17:14:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58478) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzE-00034X-58 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:23119) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001Ep-D2 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:39 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-135-eeLYOryCNAadQpquEP8r-w-1; Tue, 15 Jun 2021 16:48:28 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 959EE80EDA7; Tue, 15 Jun 2021 20:48:27 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 256FD5D9CA; Tue, 15 Jun 2021 20:48:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790112; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BnaoTipBCU3dHjYzpfRJeeaDu/fHIsONolRvLPHuKiA=; b=PyHK3XK1gEYH7g/HvN7TS+3J/ju4lGuc3t5Qf2bDRAlEB7zqzmf8Akjkmiht7MEu8GVNrN I3QcFVmWfnUAfgRmO2KkQCITwOIMczhOoGcLZGChFrwzDiegyu7DmSWOSx28N49pch2foe fkEdrl4DyQHkRJF4kmWa3LoOxqu3iZM= X-MC-Unique: eeLYOryCNAadQpquEP8r-w-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 24/34] block/nbd: split nbd_handle_updated_info out of nbd_client_handshake() Date: Tue, 15 Jun 2021 15:47:46 -0500 Message-Id: <20210615204756.281505-25-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Roman Kagan , Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy To be reused in the following patch. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Roman Kagan Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-23-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 112 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 48 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index df9d241313f4..240c6e1b3d72 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -314,6 +314,51 @@ static bool nbd_client_connecting_wait(BDRVNBDState *s) return qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTING_WA= IT; } +/* + * Update @bs with information learned during a completed negotiation proc= ess. + * Return failure if the server's advertised options are incompatible with= the + * client's needs. + */ +static int nbd_handle_updated_info(BlockDriverState *bs, Error **errp) +{ + BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + int ret; + + if (s->x_dirty_bitmap) { + if (!s->info.base_allocation) { + error_setg(errp, "requested x-dirty-bitmap %s not found", + s->x_dirty_bitmap); + return -EINVAL; + } + if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") =3D=3D 0) { + s->alloc_depth =3D true; + } + } + + if (s->info.flags & NBD_FLAG_READ_ONLY) { + ret =3D bdrv_apply_auto_read_only(bs, "NBD export is read-only", e= rrp); + if (ret < 0) { + return ret; + } + } + + if (s->info.flags & NBD_FLAG_SEND_FUA) { + bs->supported_write_flags =3D BDRV_REQ_FUA; + bs->supported_zero_flags |=3D BDRV_REQ_FUA; + } + + if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { + bs->supported_zero_flags |=3D BDRV_REQ_MAY_UNMAP; + if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) { + bs->supported_zero_flags |=3D BDRV_REQ_NO_FALLBACK; + } + } + + trace_nbd_client_handshake_success(s->export); + + return 0; +} + static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { int ret; @@ -1575,32 +1620,25 @@ static int nbd_client_handshake(BlockDriverState *b= s, Error **errp) s->sioc =3D NULL; return ret; } - if (s->x_dirty_bitmap) { - if (!s->info.base_allocation) { - error_setg(errp, "requested x-dirty-bitmap %s not found", - s->x_dirty_bitmap); - ret =3D -EINVAL; - goto fail; - } - if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") =3D=3D 0) { - s->alloc_depth =3D true; - } - } - if (s->info.flags & NBD_FLAG_READ_ONLY) { - ret =3D bdrv_apply_auto_read_only(bs, "NBD export is read-only", e= rrp); - if (ret < 0) { - goto fail; - } - } - if (s->info.flags & NBD_FLAG_SEND_FUA) { - bs->supported_write_flags =3D BDRV_REQ_FUA; - bs->supported_zero_flags |=3D BDRV_REQ_FUA; - } - if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { - bs->supported_zero_flags |=3D BDRV_REQ_MAY_UNMAP; - if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) { - bs->supported_zero_flags |=3D BDRV_REQ_NO_FALLBACK; - } + + ret =3D nbd_handle_updated_info(bs, errp); + if (ret < 0) { + /* + * We have connected, but must fail for other reasons. + * Send NBD_CMD_DISC as a courtesy to the server. + */ + NBDRequest request =3D { .type =3D NBD_CMD_DISC }; + + nbd_send_request(s->ioc ?: QIO_CHANNEL(s->sioc), &request); + + yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), + nbd_yank, bs); + object_unref(OBJECT(s->sioc)); + s->sioc =3D NULL; + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; + + return ret; } if (!s->ioc) { @@ -1608,29 +1646,7 @@ static int nbd_client_handshake(BlockDriverState *bs= , Error **errp) object_ref(OBJECT(s->ioc)); } - trace_nbd_client_handshake_success(s->export); - return 0; - - fail: - /* - * We have connected, but must fail for other reasons. - * Send NBD_CMD_DISC as a courtesy to the server. - */ - { - NBDRequest request =3D { .type =3D NBD_CMD_DISC }; - - nbd_send_request(s->ioc ?: QIO_CHANNEL(s->sioc), &request); - - yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), - nbd_yank, bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; - object_unref(OBJECT(s->ioc)); - s->ioc =3D NULL; - - return ret; - } } /* --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791272883273.246567022029; Tue, 15 Jun 2021 14:07:52 -0700 (PDT) Received: from localhost ([::1]:33270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGHn-00040K-QP for importer2@patchew.org; Tue, 15 Jun 2021 17:07:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58436) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzC-0002yE-Hl for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40556) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001Ec-BO for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:38 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-100-dYlH8bbXNrq_9BZGK-b6gQ-1; Tue, 15 Jun 2021 16:48:29 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3A0C7107ACF6; Tue, 15 Jun 2021 20:48:28 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD9B45D9CA; Tue, 15 Jun 2021 20:48:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790110; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=otXHFzyadtHeroBDsWABx3X+6dlJWcr4p+G+V5rD7sk=; b=b0arqWEKR6959n7AG28W0SFPsAeQZ9KH2Fdf8uiJ2sP1vo/7cIUG31gevP2G3HqQWZULPY agJGSdgKcobr3pMVgyEum5XtYFWqa5r8yLpkg+3H3o+xiEhTOA2KlRGQbbVZIjjpTbOfhm Ef+XDKD6eabFYDZGalGUkLG8PMXheKc= X-MC-Unique: dYlH8bbXNrq_9BZGK-b6gQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 25/34] block/nbd: use negotiation of NBDClientConnection Date: Tue, 15 Jun 2021 15:47:47 -0500 Message-Id: <20210615204756.281505-26-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Now that we can opt in to negotiation as part of the client connection thread, use that to simplify connection_co. This is another step on the way to moving all reconnect code into NBDClientConnection. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-24-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 240c6e1b3d72..311471644487 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -362,6 +362,7 @@ static int nbd_handle_updated_info(BlockDriverState *bs= , Error **errp) static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { int ret; + AioContext *aio_context =3D bdrv_get_aio_context(s->bs); if (!nbd_client_connecting(s)) { return; @@ -402,30 +403,44 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNB= DState *s) s->ioc =3D NULL; } - s->sioc =3D nbd_co_establish_connection(s->conn, NULL, NULL, NULL); + s->sioc =3D nbd_co_establish_connection(s->conn, &s->info, &s->ioc, NU= LL); if (!s->sioc) { ret =3D -ECONNREFUSED; goto out; } + qio_channel_set_blocking(QIO_CHANNEL(s->sioc), false, NULL); + qio_channel_attach_aio_context(QIO_CHANNEL(s->sioc), aio_context); + if (s->ioc) { + qio_channel_set_blocking(QIO_CHANNEL(s->ioc), false, NULL); + qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), aio_context); + } else { + s->ioc =3D QIO_CHANNEL(s->sioc); + object_ref(OBJECT(s->ioc)); + } + yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_y= ank, s->bs); - bdrv_dec_in_flight(s->bs); + ret =3D nbd_handle_updated_info(s->bs, NULL); + if (ret < 0) { + /* + * We have connected, but must fail for other reasons. + * Send NBD_CMD_DISC as a courtesy to the server. + */ + NBDRequest request =3D { .type =3D NBD_CMD_DISC }; - ret =3D nbd_client_handshake(s->bs, NULL); + nbd_send_request(s->ioc, &request); - if (s->drained) { - s->wait_drained_end =3D true; - while (s->drained) { - /* - * We may be entered once from nbd_client_attach_aio_context_bh - * and then from nbd_client_co_drain_end. So here is a loop. - */ - qemu_coroutine_yield(); - } + yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), + nbd_yank, s->bs); + object_unref(OBJECT(s->sioc)); + s->sioc =3D NULL; + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; + + return; } - bdrv_inc_in_flight(s->bs); out: if (ret >=3D 0) { @@ -2051,7 +2066,8 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, goto fail; } - s->conn =3D nbd_client_connection_new(s->saddr, false, NULL, NULL, NUL= L); + s->conn =3D nbd_client_connection_new(s->saddr, true, s->export, + s->x_dirty_bitmap, s->tlscreds); /* * establish TCP connection, return error if it fails --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791716349578.406233376893; Tue, 15 Jun 2021 14:15:16 -0700 (PDT) Received: from localhost ([::1]:56918 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGOx-0003Gi-C5 for importer2@patchew.org; Tue, 15 Jun 2021 17:15:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58432) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzC-0002wj-60 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29282) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001Eh-1u for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:37 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-132-QKFRxjSXNR6lUWZJCbZYoQ-1; Tue, 15 Jun 2021 16:48:29 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CBC0A8015D0; Tue, 15 Jun 2021 20:48:28 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 616AA5D9CA; Tue, 15 Jun 2021 20:48:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790111; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=At05gGlpxt0XLLHVPGyJEHnozEMwh9TIABnm3a/hkVs=; b=Y45bHnfbbrlgTHZkWw4L0qimq/kInqXJwT6VD/xA1NOThvck4WK7soCuDfCRo/7eapoFRe LVIcuO0+cipCWbUv+nosC8R6HffP0N49AEQL3sr1i5pGXjU+T3uC2tBXzWfTK+tuMXmMwb 51hCxsmH9RPRUS3ocL6W/+y1Pt/gTTc= X-MC-Unique: QKFRxjSXNR6lUWZJCbZYoQ-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 26/34] block/nbd: don't touch s->sioc in nbd_teardown_connection() Date: Tue, 15 Jun 2021 15:47:48 -0500 Message-Id: <20210615204756.281505-27-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Negotiation during reconnect is now done in a thread, and s->sioc is not available during negotiation. Negotiation in thread will be cancelled by nbd_client_connection_release() called from nbd_clear_bdrvstate(). So, we don't need this code chunk anymore. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-25-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 311471644487..2abcedd464c7 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -280,10 +280,6 @@ static void nbd_teardown_connection(BlockDriverState *= bs) if (s->ioc) { /* finish any pending coroutines */ qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); - } else if (s->sioc) { - /* abort negotiation */ - qio_channel_shutdown(QIO_CHANNEL(s->sioc), QIO_CHANNEL_SHUTDOWN_BO= TH, - NULL); } s->state =3D NBD_CLIENT_QUIT; --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791089186668.6532767696251; Tue, 15 Jun 2021 14:04:49 -0700 (PDT) Received: from localhost ([::1]:52782 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGEq-0006Ka-4R for importer2@patchew.org; Tue, 15 Jun 2021 17:04:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58472) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzD-00033E-OV for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50927) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001Eu-Ca for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:39 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-60-Hr75lVBoMWaE7G2p0l17CA-1; Tue, 15 Jun 2021 16:48:30 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 74D57FCA6; Tue, 15 Jun 2021 20:48:29 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 030C95D9CA; Tue, 15 Jun 2021 20:48:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790112; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2+BLBZYyc/XTK+SJC9L390FFiqjPgeOkKjk8dlP/7Rs=; b=ZqH18TT1I+GgLVBe/sklS0f71gPUChnkeEdXVodtknlt9wnxD3h1qAnuHBXWHL/XWBDsd6 Y2ftIgITCdm3/FPoSwja13WsoMNO7dCxHLXvayiF0Db/k6OU422B6DJlEVqO6S9BPKeIew ZIBIOwep2N4i0zN6XmqJgqtNoQUEY60= X-MC-Unique: Hr75lVBoMWaE7G2p0l17CA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 27/34] block/nbd: drop BDRVNBDState::sioc Date: Tue, 15 Jun 2021 15:47:49 -0500 Message-Id: <20210615204756.281505-28-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Currently sioc pointer is used just to pass from socket-connection to nbd negotiation. Drop the field, and use local variables instead. With next commit we'll update nbd/client-connection.c to behave appropriately (return only top-most ioc, not two channels). Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-26-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 98 ++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 2abcedd464c7..9f193d130bcd 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -67,8 +67,7 @@ typedef enum NBDClientState { } NBDClientState; typedef struct BDRVNBDState { - QIOChannelSocket *sioc; /* The master data channel */ - QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) = */ + QIOChannel *ioc; /* The current I/O channel */ NBDExportInfo info; CoMutex send_mutex; @@ -100,9 +99,11 @@ typedef struct BDRVNBDState { NBDClientConnection *conn; } BDRVNBDState; -static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *s= addr, - Error **errp); -static int nbd_client_handshake(BlockDriverState *bs, Error **errp); +static QIOChannelSocket *nbd_establish_connection(BlockDriverState *bs, + SocketAddress *saddr, + Error **errp); +static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *si= oc, + Error **errp); static void nbd_yank(void *opaque); static void nbd_clear_bdrvstate(BlockDriverState *bs) @@ -359,6 +360,7 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDS= tate *s) { int ret; AioContext *aio_context =3D bdrv_get_aio_context(s->bs); + QIOChannelSocket *sioc; if (!nbd_client_connecting(s)) { return; @@ -393,27 +395,26 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNB= DState *s) qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank, s->bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; object_unref(OBJECT(s->ioc)); s->ioc =3D NULL; } - s->sioc =3D nbd_co_establish_connection(s->conn, &s->info, &s->ioc, NU= LL); - if (!s->sioc) { + sioc =3D nbd_co_establish_connection(s->conn, &s->info, &s->ioc, NULL); + if (!sioc) { ret =3D -ECONNREFUSED; goto out; } - qio_channel_set_blocking(QIO_CHANNEL(s->sioc), false, NULL); - qio_channel_attach_aio_context(QIO_CHANNEL(s->sioc), aio_context); if (s->ioc) { - qio_channel_set_blocking(QIO_CHANNEL(s->ioc), false, NULL); - qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), aio_context); + /* sioc is referenced by s->ioc */ + object_unref(OBJECT(sioc)); } else { - s->ioc =3D QIO_CHANNEL(s->sioc); - object_ref(OBJECT(s->ioc)); + s->ioc =3D QIO_CHANNEL(sioc); } + sioc =3D NULL; + + qio_channel_set_blocking(QIO_CHANNEL(s->ioc), false, NULL); + qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), aio_context); yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_y= ank, s->bs); @@ -430,8 +431,6 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDS= tate *s) yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank, s->bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; object_unref(OBJECT(s->ioc)); s->ioc =3D NULL; @@ -566,8 +565,6 @@ static coroutine_fn void nbd_connection_entry(void *opa= que) qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank, s->bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; object_unref(OBJECT(s->ioc)); s->ioc =3D NULL; } @@ -1566,7 +1563,7 @@ static void nbd_yank(void *opaque) BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; qatomic_store_release(&s->state, NBD_CLIENT_QUIT); - qio_channel_shutdown(QIO_CHANNEL(s->sioc), QIO_CHANNEL_SHUTDOWN_BOTH, = NULL); + qio_channel_shutdown(QIO_CHANNEL(s->ioc), QIO_CHANNEL_SHUTDOWN_BOTH, N= ULL); } static void nbd_client_close(BlockDriverState *bs) @@ -1581,57 +1578,64 @@ static void nbd_client_close(BlockDriverState *bs) nbd_teardown_connection(bs); } -static int nbd_establish_connection(BlockDriverState *bs, - SocketAddress *saddr, - Error **errp) +static QIOChannelSocket *nbd_establish_connection(BlockDriverState *bs, + SocketAddress *saddr, + Error **errp) { ERRP_GUARD(); - BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + QIOChannelSocket *sioc; - s->sioc =3D qio_channel_socket_new(); - qio_channel_set_name(QIO_CHANNEL(s->sioc), "nbd-client"); + sioc =3D qio_channel_socket_new(); + qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); - qio_channel_socket_connect_sync(s->sioc, saddr, errp); + qio_channel_socket_connect_sync(sioc, saddr, errp); if (*errp) { - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; - return -1; + object_unref(OBJECT(sioc)); + return NULL; } yank_register_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), nbd_yank= , bs); - qio_channel_set_delay(QIO_CHANNEL(s->sioc), false); + qio_channel_set_delay(QIO_CHANNEL(sioc), false); - return 0; + return sioc; } -/* nbd_client_handshake takes ownership on s->sioc. On failure it's unref'= ed. */ -static int nbd_client_handshake(BlockDriverState *bs, Error **errp) +/* nbd_client_handshake takes ownership on sioc. */ +static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *si= oc, + Error **errp) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; AioContext *aio_context =3D bdrv_get_aio_context(bs); int ret; trace_nbd_client_handshake(s->export); - qio_channel_set_blocking(QIO_CHANNEL(s->sioc), false, NULL); - qio_channel_attach_aio_context(QIO_CHANNEL(s->sioc), aio_context); + qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL); + qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context); s->info.request_sizes =3D true; s->info.structured_reply =3D true; s->info.base_allocation =3D true; s->info.x_dirty_bitmap =3D g_strdup(s->x_dirty_bitmap); s->info.name =3D g_strdup(s->export ?: ""); - ret =3D nbd_receive_negotiate(aio_context, QIO_CHANNEL(s->sioc), s->tl= screds, + ret =3D nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscr= eds, s->hostname, &s->ioc, &s->info, errp); g_free(s->info.x_dirty_bitmap); g_free(s->info.name); if (ret < 0) { yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), nbd_yank, bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; + object_unref(OBJECT(sioc)); return ret; } + if (s->ioc) { + /* sioc is referenced by s->ioc */ + object_unref(OBJECT(sioc)); + } else { + s->ioc =3D QIO_CHANNEL(sioc); + } + sioc =3D NULL; + ret =3D nbd_handle_updated_info(bs, errp); if (ret < 0) { /* @@ -1640,23 +1644,15 @@ static int nbd_client_handshake(BlockDriverState *b= s, Error **errp) */ NBDRequest request =3D { .type =3D NBD_CMD_DISC }; - nbd_send_request(s->ioc ?: QIO_CHANNEL(s->sioc), &request); + nbd_send_request(s->ioc, &request); yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), nbd_yank, bs); - object_unref(OBJECT(s->sioc)); - s->sioc =3D NULL; object_unref(OBJECT(s->ioc)); s->ioc =3D NULL; - return ret; } - if (!s->ioc) { - s->ioc =3D QIO_CHANNEL(s->sioc); - object_ref(OBJECT(s->ioc)); - } - return 0; } @@ -2048,6 +2044,7 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, { int ret; BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + QIOChannelSocket *sioc; s->bs =3D bs; qemu_co_mutex_init(&s->send_mutex); @@ -2069,12 +2066,13 @@ static int nbd_open(BlockDriverState *bs, QDict *op= tions, int flags, * establish TCP connection, return error if it fails * TODO: Configurable retry-until-timeout behaviour. */ - if (nbd_establish_connection(bs, s->saddr, errp) < 0) { + sioc =3D nbd_establish_connection(bs, s->saddr, errp); + if (!sioc) { ret =3D -ECONNREFUSED; goto fail; } - ret =3D nbd_client_handshake(bs, errp); + ret =3D nbd_client_handshake(bs, sioc, errp); if (ret < 0) { goto fail; } --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16237912699361018.9555858313488; Tue, 15 Jun 2021 14:07:49 -0700 (PDT) Received: from localhost ([::1]:32968 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGHk-0003ny-U5 for importer2@patchew.org; Tue, 15 Jun 2021 17:07:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58516) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzF-000398-FF for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:22168) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001F1-Cm for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:41 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-156-wsJghhfuPCmxHy1ZkUYKkw-1; Tue, 15 Jun 2021 16:48:31 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1370C80ED8B; Tue, 15 Jun 2021 20:48:30 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9C4685D9CA; Tue, 15 Jun 2021 20:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790112; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l3EM11ha9LMzyhSqakhs5aR6dObYOuLyVzRNH2DR1oE=; b=TgrGOmHLgB04TUZGcFlnth78ZLzp/uFfhEpFPA9O3cJdiI1Sc+9Yt1psWUhkVSH8RJAelo eiEsa2sPCmjVY0XaaIUWdqlwhvwEet2tx++Oef+9aMCVMCpFxJGJKK0bCzLIVkgqxctEer bjEPCxKb+Zr9r+ZaxSYSayRmyM2MoR4= X-MC-Unique: wsJghhfuPCmxHy1ZkUYKkw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 28/34] nbd/client-connection: return only one io channel Date: Tue, 15 Jun 2021 15:47:50 -0500 Message-Id: <20210615204756.281505-29-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy block/nbd doesn't need underlying sioc channel anymore. So, we can update nbd/client-connection interface to return only one top-most io channel, which is more straight forward. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-27-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- include/block/nbd.h | 4 ++-- block/nbd.c | 13 ++----------- nbd/client-connection.c | 33 +++++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 5bb54d831c8a..10c8a0bcca80 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -418,9 +418,9 @@ NBDClientConnection *nbd_client_connection_new(const So= cketAddress *saddr, QCryptoTLSCreds *tlscreds); void nbd_client_connection_release(NBDClientConnection *conn); -QIOChannelSocket *coroutine_fn +QIOChannel *coroutine_fn nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, - QIOChannel **ioc, Error **errp); + Error **errp); void coroutine_fn nbd_co_establish_connection_cancel(NBDClientConnection *= conn); diff --git a/block/nbd.c b/block/nbd.c index 9f193d130bcd..411435c1559e 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -360,7 +360,6 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBDS= tate *s) { int ret; AioContext *aio_context =3D bdrv_get_aio_context(s->bs); - QIOChannelSocket *sioc; if (!nbd_client_connecting(s)) { return; @@ -399,20 +398,12 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNB= DState *s) s->ioc =3D NULL; } - sioc =3D nbd_co_establish_connection(s->conn, &s->info, &s->ioc, NULL); - if (!sioc) { + s->ioc =3D nbd_co_establish_connection(s->conn, &s->info, NULL); + if (!s->ioc) { ret =3D -ECONNREFUSED; goto out; } - if (s->ioc) { - /* sioc is referenced by s->ioc */ - object_unref(OBJECT(sioc)); - } else { - s->ioc =3D QIO_CHANNEL(sioc); - } - sioc =3D NULL; - qio_channel_set_blocking(QIO_CHANNEL(s->ioc), false, NULL); qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), aio_context); diff --git a/nbd/client-connection.c b/nbd/client-connection.c index 883f9cf158cb..72138a5ff74a 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -272,15 +272,15 @@ void nbd_client_connection_release(NBDClientConnectio= n *conn) * nbd_receive_export_list() would be zero (see description of NBDExportIn= fo in * include/block/nbd.h). */ -QIOChannelSocket *coroutine_fn +QIOChannel *coroutine_fn nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, - QIOChannel **ioc, Error **errp) + Error **errp) { + QIOChannel *ioc; QemuThread thread; if (conn->do_negotiation) { assert(info); - assert(ioc); } WITH_QEMU_LOCK_GUARD(&conn->mutex) { @@ -294,10 +294,17 @@ nbd_co_establish_connection(NBDClientConnection *conn= , NBDExportInfo *info, if (conn->sioc) { /* Previous attempt finally succeeded in background */ if (conn->do_negotiation) { - *ioc =3D g_steal_pointer(&conn->ioc); + ioc =3D g_steal_pointer(&conn->ioc); memcpy(info, &conn->updated_info, sizeof(*info)); } - return g_steal_pointer(&conn->sioc); + if (ioc) { + /* TLS channel now has own reference to parent */ + object_unref(OBJECT(conn->sioc)); + } else { + ioc =3D QIO_CHANNEL(conn->sioc); + } + conn->sioc =3D NULL; + return ioc; } conn->running =3D true; @@ -329,11 +336,21 @@ nbd_co_establish_connection(NBDClientConnection *conn= , NBDExportInfo *info, } else { error_propagate(errp, conn->err); conn->err =3D NULL; - if (conn->sioc && conn->do_negotiation) { - *ioc =3D g_steal_pointer(&conn->ioc); + if (!conn->sioc) { + return NULL; + } + if (conn->do_negotiation) { + ioc =3D g_steal_pointer(&conn->ioc); memcpy(info, &conn->updated_info, sizeof(*info)); } - return g_steal_pointer(&conn->sioc); + if (ioc) { + /* TLS channel now has own reference to parent */ + object_unref(OBJECT(conn->sioc)); + } else { + ioc =3D QIO_CHANNEL(conn->sioc); + } + conn->sioc =3D NULL; + return ioc; } } --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1623791944; cv=none; d=zohomail.com; s=zohoarc; b=BzkFjjNPpDir9jYzNUys/eYiDQoddR1JogwmOzlQy6fKtJjQVvVmZS0UZLtZSi5jsTn7P4/HfP0s06DZYA8fD2JP/LCh1VxxdmDoXo8wZ6NFzZcNPB0jTAkM3ItaizPgmWPQ5aTHfnfcYP27Vx8PwhiA3GBKUFDGop3IVUIni4A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623791944; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fb8CRE5Sp++9H3ScBiFAJgZbGVVw56PvVfWYcQsn8DM=; b=QsRawhPsOeYimBX7W0iv58yXqlNb4wWuIhFtaf5zQbA2ps0XyRGJOSYEW8R2G1E3PbVphPDNz+QL1mLexT2/GRz8i8iAvyqLK7p1pmT6s0L+HanHJUspupTOFuoLJNNmF42DtyCDrmA6C3aL2deLJXVHbvQ0H4E7YDF22b8xKOI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791944017388.6666965170904; Tue, 15 Jun 2021 14:19:04 -0700 (PDT) Received: from localhost ([::1]:37052 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGSd-0000cx-0x for importer2@patchew.org; Tue, 15 Jun 2021 17:19:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58578) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzH-0003Gb-Re for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26167) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001FF-MI for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:43 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-278-JmOwEe74Nj-z4kYdi9hSOA-1; Tue, 15 Jun 2021 16:48:31 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ACAF5107ACF6; Tue, 15 Jun 2021 20:48:30 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 41D8A5D9CA; Tue, 15 Jun 2021 20:48:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fb8CRE5Sp++9H3ScBiFAJgZbGVVw56PvVfWYcQsn8DM=; b=cImgh4zFdpXDXfH6tPCf0RKwgtGb8JTEKBbKhiyQ0KgxSQW1Cv9fMK1vrfpqqwOHlHqc/U 5oi5peeI4aPSSJbNIabqkyGwkt4Kt+iF+1WbWsSHxUIjujJ02hcIaxBw/bmg89tvTNWTd8 +SO5JuJUS/LpXR9B06vWAaurCV+qUqI= X-MC-Unique: JmOwEe74Nj-z4kYdi9hSOA-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 29/34] block-coroutine-wrapper: allow non bdrv_ prefix Date: Tue, 15 Jun 2021 15:47:51 -0500 Message-Id: <20210615204756.281505-30-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vladimir Sementsov-Ogievskiy , Eduardo Habkost , Cleber Rosa Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We are going to reuse the script to generate a nbd_ function in further commit. Prepare the script now. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-28-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- scripts/block-coroutine-wrapper.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/block-coroutine-wrapper.py b/scripts/block-coroutine-w= rapper.py index 0461fd1c459c..85dbeb9ecf9c 100644 --- a/scripts/block-coroutine-wrapper.py +++ b/scripts/block-coroutine-wrapper.py @@ -98,12 +98,13 @@ def snake_to_camel(func_name: str) -> str: def gen_wrapper(func: FuncDecl) -> str: - assert func.name.startswith('bdrv_') - assert not func.name.startswith('bdrv_co_') + assert not '_co_' in func.name assert func.return_type =3D=3D 'int' assert func.args[0].type in ['BlockDriverState *', 'BdrvChild *'] - name =3D 'bdrv_co_' + func.name[5:] + subsystem, subname =3D func.name.split('_', 1) + + name =3D f'{subsystem}_co_{subname}' bs =3D 'bs' if func.args[0].type =3D=3D 'BlockDriverState *' else 'chi= ld->bs' struct_name =3D snake_to_camel(name) --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791412904802.1140506120594; Tue, 15 Jun 2021 14:10:12 -0700 (PDT) Received: from localhost ([::1]:42034 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGK3-0001ac-R3 for importer2@patchew.org; Tue, 15 Jun 2021 17:10:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58558) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzH-0003EO-1D for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:43 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:58655) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz8-0001FL-TX for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:42 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-354-oKjZu9JaMdyiluWyjZfeSg-1; Tue, 15 Jun 2021 16:48:32 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5486680ED9D; Tue, 15 Jun 2021 20:48:31 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id D9E275D9CA; Tue, 15 Jun 2021 20:48:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T7l0HnqUP75YyQpyHHpODAXaw6TqXMN5/iCEhnn2PUg=; b=WmfrCBFdd0UXzgZgJXpq5zaB9A49GxF3sGwVJOP4WFx7nphqDsRzyx1TgwEvWehzZ2Iewo u+U8yQzikuRmDJ01yBO9UHNhqcUwFQN8/a5KD5FLYodc9+eUn38u76cRZtLBEv6iaSEPJs gLULnEFchKfoD3qbGOiT5S0Oisqcb6M= X-MC-Unique: oKjZu9JaMdyiluWyjZfeSg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 30/34] block/nbd: split nbd_co_do_establish_connection out of nbd_reconnect_attempt Date: Tue, 15 Jun 2021 15:47:52 -0500 Message-Id: <20210615204756.281505-31-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Split out the part that we want to reuse for nbd_open(). Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20210610100802.5888-29-vsementsov@virtuozzo.com> Reviewed-by: Eric Blake Signed-off-by: Eric Blake --- block/nbd.c | 82 ++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 411435c1559e..8caeafc8d351 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -356,11 +356,50 @@ static int nbd_handle_updated_info(BlockDriverState *= bs, Error **errp) return 0; } +static int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *b= s, + Error **errp) +{ + BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; + int ret; + + assert(!s->ioc); + + s->ioc =3D nbd_co_establish_connection(s->conn, &s->info, errp); + if (!s->ioc) { + return -ECONNREFUSED; + } + + ret =3D nbd_handle_updated_info(s->bs, NULL); + if (ret < 0) { + /* + * We have connected, but must fail for other reasons. + * Send NBD_CMD_DISC as a courtesy to the server. + */ + NBDRequest request =3D { .type =3D NBD_CMD_DISC }; + + nbd_send_request(s->ioc, &request); + + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; + + return ret; + } + + qio_channel_set_blocking(s->ioc, false, NULL); + qio_channel_attach_aio_context(s->ioc, bdrv_get_aio_context(bs)); + + yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_y= ank, + bs); + + /* successfully connected */ + s->state =3D NBD_CLIENT_CONNECTED; + qemu_co_queue_restart_all(&s->free_sema); + + return 0; +} + static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s) { - int ret; - AioContext *aio_context =3D bdrv_get_aio_context(s->bs); - if (!nbd_client_connecting(s)) { return; } @@ -398,42 +437,7 @@ static coroutine_fn void nbd_reconnect_attempt(BDRVNBD= State *s) s->ioc =3D NULL; } - s->ioc =3D nbd_co_establish_connection(s->conn, &s->info, NULL); - if (!s->ioc) { - ret =3D -ECONNREFUSED; - goto out; - } - - qio_channel_set_blocking(QIO_CHANNEL(s->ioc), false, NULL); - qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), aio_context); - - yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_y= ank, - s->bs); - - ret =3D nbd_handle_updated_info(s->bs, NULL); - if (ret < 0) { - /* - * We have connected, but must fail for other reasons. - * Send NBD_CMD_DISC as a courtesy to the server. - */ - NBDRequest request =3D { .type =3D NBD_CMD_DISC }; - - nbd_send_request(s->ioc, &request); - - yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), - nbd_yank, s->bs); - object_unref(OBJECT(s->ioc)); - s->ioc =3D NULL; - - return; - } - -out: - if (ret >=3D 0) { - /* successfully connected */ - s->state =3D NBD_CLIENT_CONNECTED; - qemu_co_queue_restart_all(&s->free_sema); - } + nbd_co_do_establish_connection(s->bs, NULL); } static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s) --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791402198330.9786288082564; Tue, 15 Jun 2021 14:10:02 -0700 (PDT) Received: from localhost ([::1]:41610 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGJt-0001HS-7c for importer2@patchew.org; Tue, 15 Jun 2021 17:10:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58610) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzL-0003I2-1I for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:29522) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFz9-0001Fk-EO for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:46 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-191-EoHOLzx6NmOVpMjqD6PbSg-1; Tue, 15 Jun 2021 16:48:33 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EC4C9195D56E; Tue, 15 Jun 2021 20:48:31 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82B705D9CA; Tue, 15 Jun 2021 20:48:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z48b1Gxeot01okVsYxLXZmQhqFnl1OwCWRH1p2kS53g=; b=U6sb6wNm4wwBB8+m/YlpfvAMpqMn0DoV9i5bX23WIT0i8he5g8yFLeAGJyHZGuImxPXu0F KeSLb+gKVl/o1zzwHcWpB23rdKsfpDayVEvlTsw+yOek8zXQ3Z4m1To89MGFQbKfxl/qCI Ik7yC3HJ4w0GOdHg+bSGkuuJBOtpt8o= X-MC-Unique: EoHOLzx6NmOVpMjqD6PbSg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 31/34] nbd/client-connection: add option for non-blocking connection attempt Date: Tue, 15 Jun 2021 15:47:53 -0500 Message-Id: <20210615204756.281505-32-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We'll need a possibility of non-blocking nbd_co_establish_connection(), so that it returns immediately, and it returns success only if a connections was previously established in background. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-30-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- include/block/nbd.h | 2 +- block/nbd.c | 2 +- nbd/client-connection.c | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/block/nbd.h b/include/block/nbd.h index 10c8a0bcca80..78d101b77488 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -420,7 +420,7 @@ void nbd_client_connection_release(NBDClientConnection = *conn); QIOChannel *coroutine_fn nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, - Error **errp); + bool blocking, Error **errp); void coroutine_fn nbd_co_establish_connection_cancel(NBDClientConnection *= conn); diff --git a/block/nbd.c b/block/nbd.c index 8caeafc8d351..bf2e9393146b 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -364,7 +364,7 @@ static int coroutine_fn nbd_co_do_establish_connection(= BlockDriverState *bs, assert(!s->ioc); - s->ioc =3D nbd_co_establish_connection(s->conn, &s->info, errp); + s->ioc =3D nbd_co_establish_connection(s->conn, &s->info, true, errp); if (!s->ioc) { return -ECONNREFUSED; } diff --git a/nbd/client-connection.c b/nbd/client-connection.c index 72138a5ff74a..aff646028786 100644 --- a/nbd/client-connection.c +++ b/nbd/client-connection.c @@ -266,6 +266,8 @@ void nbd_client_connection_release(NBDClientConnection = *conn) * otherwise the thread is not running, so start a thread and wait for * completion * + * If @blocking is false, don't wait for the thread, return immediately. + * * If @info is not NULL, also do nbd-negotiation after successful connecti= on. * In this case info is used only as out parameter, and is fully initializ= ed by * nbd_co_establish_connection(). "IN" fields of info as well as related o= nly to @@ -274,7 +276,7 @@ void nbd_client_connection_release(NBDClientConnection = *conn) */ QIOChannel *coroutine_fn nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, - Error **errp) + bool blocking, Error **errp) { QIOChannel *ioc; QemuThread thread; @@ -314,6 +316,10 @@ nbd_co_establish_connection(NBDClientConnection *conn,= NBDExportInfo *info, connect_thread_func, conn, QEMU_THREAD_DETA= CHED); } + if (!blocking) { + return NULL; + } + conn->wait_co =3D qemu_coroutine_self(); } --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623791803970299.80114781813916; Tue, 15 Jun 2021 14:16:43 -0700 (PDT) Received: from localhost ([::1]:59096 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGQK-0004np-OW for importer2@patchew.org; Tue, 15 Jun 2021 17:16:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58628) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzL-0003J0-SK for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:36587) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzA-0001GT-2M for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:47 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-559-Oj95WWD-P2m4qn39z0tDLg-1; Tue, 15 Jun 2021 16:48:33 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 91EA180EDB1; Tue, 15 Jun 2021 20:48:32 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 234445D9CA; Tue, 15 Jun 2021 20:48:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790115; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XK+osHD091m4Tdr5I26eCX8OVZ2HajRv/geqf0btSkw=; b=C215zEjZR1uNRd5Lf2bpmu2DOglB87FPlufSQp+Ee2dQCAI1YCETMd+t8pPtVvQ9tkmHw8 PG/tT3ScZkzQB6ygO/6504aoZn6JRJh72K/+AVx39JVRq1xgH6iz2YIV3bC2s50Y71P2+9 lgV0i2ycERqtmyDo1GqH4r2dsuv3g9I= X-MC-Unique: Oj95WWD-P2m4qn39z0tDLg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 32/34] block/nbd: reuse nbd_co_do_establish_connection() in nbd_open() Date: Tue, 15 Jun 2021 15:47:54 -0500 Message-Id: <20210615204756.281505-33-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Block layer core" , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy The only last step we need to reuse the function is coroutine-wrapper. nbd_open() may be called from non-coroutine context. So, generate the wrapper and use it. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-31-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/coroutines.h | 6 +++ block/nbd.c | 103 +++------------------------------------------ 2 files changed, 11 insertions(+), 98 deletions(-) diff --git a/block/coroutines.h b/block/coroutines.h index 4cfb4946e65e..514d169d23d6 100644 --- a/block/coroutines.h +++ b/block/coroutines.h @@ -66,4 +66,10 @@ int coroutine_fn bdrv_co_readv_vmstate(BlockDriverState = *bs, int coroutine_fn bdrv_co_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos); +int generated_co_wrapper +nbd_do_establish_connection(BlockDriverState *bs, Error **errp); +int coroutine_fn +nbd_co_do_establish_connection(BlockDriverState *bs, Error **errp); + + #endif /* BLOCK_COROUTINES_INT_H */ diff --git a/block/nbd.c b/block/nbd.c index bf2e9393146b..5e7e238b4790 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -44,6 +44,7 @@ #include "block/qdict.h" #include "block/nbd.h" #include "block/block_int.h" +#include "block/coroutines.h" #include "qemu/yank.h" @@ -99,11 +100,6 @@ typedef struct BDRVNBDState { NBDClientConnection *conn; } BDRVNBDState; -static QIOChannelSocket *nbd_establish_connection(BlockDriverState *bs, - SocketAddress *saddr, - Error **errp); -static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *si= oc, - Error **errp); static void nbd_yank(void *opaque); static void nbd_clear_bdrvstate(BlockDriverState *bs) @@ -356,8 +352,8 @@ static int nbd_handle_updated_info(BlockDriverState *bs= , Error **errp) return 0; } -static int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *b= s, - Error **errp) +int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs, + Error **errp) { BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; int ret; @@ -1573,83 +1569,6 @@ static void nbd_client_close(BlockDriverState *bs) nbd_teardown_connection(bs); } -static QIOChannelSocket *nbd_establish_connection(BlockDriverState *bs, - SocketAddress *saddr, - Error **errp) -{ - ERRP_GUARD(); - QIOChannelSocket *sioc; - - sioc =3D qio_channel_socket_new(); - qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); - - qio_channel_socket_connect_sync(sioc, saddr, errp); - if (*errp) { - object_unref(OBJECT(sioc)); - return NULL; - } - - yank_register_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), nbd_yank= , bs); - qio_channel_set_delay(QIO_CHANNEL(sioc), false); - - return sioc; -} - -/* nbd_client_handshake takes ownership on sioc. */ -static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *si= oc, - Error **errp) -{ - BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; - AioContext *aio_context =3D bdrv_get_aio_context(bs); - int ret; - - trace_nbd_client_handshake(s->export); - qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL); - qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context); - - s->info.request_sizes =3D true; - s->info.structured_reply =3D true; - s->info.base_allocation =3D true; - s->info.x_dirty_bitmap =3D g_strdup(s->x_dirty_bitmap); - s->info.name =3D g_strdup(s->export ?: ""); - ret =3D nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscr= eds, - s->hostname, &s->ioc, &s->info, errp); - g_free(s->info.x_dirty_bitmap); - g_free(s->info.name); - if (ret < 0) { - yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), - nbd_yank, bs); - object_unref(OBJECT(sioc)); - return ret; - } - - if (s->ioc) { - /* sioc is referenced by s->ioc */ - object_unref(OBJECT(sioc)); - } else { - s->ioc =3D QIO_CHANNEL(sioc); - } - sioc =3D NULL; - - ret =3D nbd_handle_updated_info(bs, errp); - if (ret < 0) { - /* - * We have connected, but must fail for other reasons. - * Send NBD_CMD_DISC as a courtesy to the server. - */ - NBDRequest request =3D { .type =3D NBD_CMD_DISC }; - - nbd_send_request(s->ioc, &request); - - yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs->node_name), - nbd_yank, bs); - object_unref(OBJECT(s->ioc)); - s->ioc =3D NULL; - return ret; - } - - return 0; -} /* * Parse nbd_open options @@ -2039,7 +1958,6 @@ static int nbd_open(BlockDriverState *bs, QDict *opti= ons, int flags, { int ret; BDRVNBDState *s =3D (BDRVNBDState *)bs->opaque; - QIOChannelSocket *sioc; s->bs =3D bs; qemu_co_mutex_init(&s->send_mutex); @@ -2057,22 +1975,11 @@ static int nbd_open(BlockDriverState *bs, QDict *op= tions, int flags, s->conn =3D nbd_client_connection_new(s->saddr, true, s->export, s->x_dirty_bitmap, s->tlscreds); - /* - * establish TCP connection, return error if it fails - * TODO: Configurable retry-until-timeout behaviour. - */ - sioc =3D nbd_establish_connection(bs, s->saddr, errp); - if (!sioc) { - ret =3D -ECONNREFUSED; - goto fail; - } - - ret =3D nbd_client_handshake(bs, sioc, errp); + /* TODO: Configurable retry-until-timeout behaviour. */ + ret =3D nbd_do_establish_connection(bs, errp); if (ret < 0) { goto fail; } - /* successfully connected */ - s->state =3D NBD_CLIENT_CONNECTED; s->connection_co =3D qemu_coroutine_create(nbd_connection_entry, s); bdrv_inc_in_flight(bs); --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623792096727224.1482181754467; Tue, 15 Jun 2021 14:21:36 -0700 (PDT) Received: from localhost ([::1]:42282 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGV5-0004BT-Nm for importer2@patchew.org; Tue, 15 Jun 2021 17:21:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58600) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzK-0003I1-0X for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:46574) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzA-0001HE-I7 for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:45 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-355-HqerLBgKMu-yIf7qKR33qw-1; Tue, 15 Jun 2021 16:48:34 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 37A82FC97; Tue, 15 Jun 2021 20:48:33 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id C05F85D9CA; Tue, 15 Jun 2021 20:48:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790115; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jCYU+dUTh+hdt/nbwoaF3c2Xc8nUzuwyD+IQEyyXs/o=; b=Iob/rCovU/1WC9mSTz334wKz8reZjwbqky77f1vjxJ7lXEmkh6FlJBcT54t3gHvRaT1qWD DtDj2w3Yre9x4wnsA/Xe2fE7N6qubbigz1gLTwJgyRUCIxncpJd3hfRQuBZESvKnwcgmwY eIBr2qxYLwf6pF32FQqMyOuENzDqFPY= X-MC-Unique: HqerLBgKMu-yIf7qKR33qw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 33/34] block/nbd: add nbd_client_connected() helper Date: Tue, 15 Jun 2021 15:47:55 -0500 Message-Id: <20210615204756.281505-34-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy We already have two similar helpers for other state. Let's add another one for convenience. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-32-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 5e7e238b4790..5cfb749e089d 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -122,15 +122,20 @@ static void nbd_clear_bdrvstate(BlockDriverState *bs) s->x_dirty_bitmap =3D NULL; } +static bool nbd_client_connected(BDRVNBDState *s) +{ + return qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTED; +} + static void nbd_channel_error(BDRVNBDState *s, int ret) { if (ret =3D=3D -EIO) { - if (qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTED) { + if (nbd_client_connected(s)) { s->state =3D s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT : NBD_CLIENT_CONNECTING_NOWAIT; } } else { - if (qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTED) { + if (nbd_client_connected(s)) { qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); } s->state =3D NBD_CLIENT_QUIT; @@ -228,7 +233,7 @@ static void nbd_client_attach_aio_context(BlockDriverSt= ate *bs, * s->connection_co is either yielded from nbd_receive_reply or from * nbd_co_reconnect_loop() */ - if (qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTED) { + if (nbd_client_connected(s)) { qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context); } @@ -499,7 +504,7 @@ static coroutine_fn void nbd_connection_entry(void *opa= que) nbd_co_reconnect_loop(s); } - if (qatomic_load_acquire(&s->state) !=3D NBD_CLIENT_CONNECTED) { + if (!nbd_client_connected(s)) { continue; } @@ -578,7 +583,7 @@ static int nbd_co_send_request(BlockDriverState *bs, qemu_co_queue_wait(&s->free_sema, &s->send_mutex); } - if (qatomic_load_acquire(&s->state) !=3D NBD_CLIENT_CONNECTED) { + if (!nbd_client_connected(s)) { rc =3D -EIO; goto err; } @@ -605,8 +610,7 @@ static int nbd_co_send_request(BlockDriverState *bs, if (qiov) { qio_channel_set_cork(s->ioc, true); rc =3D nbd_send_request(s->ioc, request); - if (qatomic_load_acquire(&s->state) =3D=3D NBD_CLIENT_CONNECTED && - rc >=3D 0) { + if (nbd_client_connected(s) && rc >=3D 0) { if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, NULL) < 0) { rc =3D -EIO; @@ -931,7 +935,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( s->requests[i].receiving =3D true; qemu_coroutine_yield(); s->requests[i].receiving =3D false; - if (qatomic_load_acquire(&s->state) !=3D NBD_CLIENT_CONNECTED) { + if (!nbd_client_connected(s)) { error_setg(errp, "Connection closed"); return -EIO; } @@ -1090,7 +1094,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState= *s, NBDReply local_reply; NBDStructuredReplyChunk *chunk; Error *local_err =3D NULL; - if (qatomic_load_acquire(&s->state) !=3D NBD_CLIENT_CONNECTED) { + if (!nbd_client_connected(s)) { error_setg(&local_err, "Connection closed"); nbd_iter_channel_error(iter, -EIO, &local_err); goto break_loop; @@ -1115,8 +1119,7 @@ static bool nbd_reply_chunk_iter_receive(BDRVNBDState= *s, } /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply= . */ - if (nbd_reply_is_simple(reply) || - qatomic_load_acquire(&s->state) !=3D NBD_CLIENT_CONNECTED) { + if (nbd_reply_is_simple(reply) || !nbd_client_connected(s)) { goto break_loop; } --=20 2.31.1 From nobody Sun May 5 03:45:45 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623792243241167.3554216004544; Tue, 15 Jun 2021 14:24:03 -0700 (PDT) Received: from localhost ([::1]:46244 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ltGXS-0006zZ-7q for importer2@patchew.org; Tue, 15 Jun 2021 17:24:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58656) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzN-0003Kc-TG for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:49890) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ltFzB-0001Hd-7d for qemu-devel@nongnu.org; Tue, 15 Jun 2021 16:48:49 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-234-IectG_BtOEK_QtK7Bw8eRg-1; Tue, 15 Jun 2021 16:48:34 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CC8F3FC9E; Tue, 15 Jun 2021 20:48:33 +0000 (UTC) Received: from blue.redhat.com (ovpn-114-191.phx2.redhat.com [10.3.114.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 616805D9CA; Tue, 15 Jun 2021 20:48:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623790116; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=U5lplDQFodWakYe4TIKYQfbPLYdG62Lnumqe7JGcOC0=; b=iKkGsoNcX4nhjld2m8H+SbCSrFA1kQR88UtnX65V86IBrt2ArjthW6DAFvP+eW1ULKULFe jmhZh/+lZnOSy58RjFNZhf3KvseJLaS6VFwTKA6xfmM/vzOHln82dLIJytM/cLwL1v5ahd NQV6ThZQ5KXlDx3tsUjZUc6lOaAZMTk= X-MC-Unique: IectG_BtOEK_QtK7Bw8eRg-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 34/34] block/nbd: safer transition to receiving request Date: Tue, 15 Jun 2021 15:47:56 -0500 Message-Id: <20210615204756.281505-35-eblake@redhat.com> In-Reply-To: <20210615204756.281505-1-eblake@redhat.com> References: <20210615204756.281505-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.197, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Max Reitz Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy req->receiving is a flag of request being in one concrete yield point in nbd_co_do_receive_one_chunk(). Such kind of boolean flag is always better to unset before scheduling the coroutine, to avoid double scheduling. So, let's be more careful. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eric Blake Message-Id: <20210610100802.5888-33-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake --- block/nbd.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block/nbd.c b/block/nbd.c index 5cfb749e089d..3cbee762de8d 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -150,6 +150,7 @@ static void nbd_recv_coroutines_wake_all(BDRVNBDState *= s) NBDClientRequest *req =3D &s->requests[i]; if (req->coroutine && req->receiving) { + req->receiving =3D false; aio_co_wake(req->coroutine); } } @@ -548,6 +549,7 @@ static coroutine_fn void nbd_connection_entry(void *opa= que) * connection_co happens through a bottom half, which can only * run after we yield. */ + s->requests[i].receiving =3D false; aio_co_wake(s->requests[i].coroutine); qemu_coroutine_yield(); } @@ -934,7 +936,7 @@ static coroutine_fn int nbd_co_do_receive_one_chunk( /* Wait until we're woken up by nbd_connection_entry. */ s->requests[i].receiving =3D true; qemu_coroutine_yield(); - s->requests[i].receiving =3D false; + assert(!s->requests[i].receiving); if (!nbd_client_connected(s)) { error_setg(errp, "Connection closed"); return -EIO; --=20 2.31.1