qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: qemu-block@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>, "Fam Zheng" <fam@euphon.net>,
	"Vladimir Sementsov-Ogievskiy" <vsementsov@virtuozzo.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Juan Quintela" <quintela@redhat.com>,
	qemu-devel@nongnu.org, "John Snow" <jsnow@redhat.com>,
	"Emanuele Giuseppe Esposito" <eesposit@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Hanna Reitz" <hreitz@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Eric Blake" <eblake@redhat.com>
Subject: [PATCH v7 01/31] main-loop.h: introduce qemu_in_main_thread()
Date: Fri, 11 Feb 2022 09:51:23 -0500	[thread overview]
Message-ID: <20220211145153.2861415-2-eesposit@redhat.com> (raw)
In-Reply-To: <20220211145153.2861415-1-eesposit@redhat.com>

When invoked from the main loop, this function is the same
as qemu_mutex_iothread_locked, and returns true if the BQL is held.
When invoked from iothreads or tests, it returns true only
if the current AioContext is the Main Loop.

This essentially just extends qemu_mutex_iothread_locked to work
also in unit tests or other users like storage-daemon, that run
in the Main Loop but end up using the implementation in
stubs/iothread-lock.c.

Using qemu_mutex_iothread_locked in unit tests defaults to false
because they use the implementation in stubs/iothread-lock,
making all assertions added in next patches fail despite the
AioContext is still the main loop.

See the comment in the function header for more information.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 include/qemu/main-loop.h | 24 ++++++++++++++++++++++++
 softmmu/cpus.c           |  5 +++++
 stubs/iothread-lock.c    |  5 +++++
 3 files changed, 34 insertions(+)

diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
index 8dbc6fcb89..bc42b5939d 100644
--- a/include/qemu/main-loop.h
+++ b/include/qemu/main-loop.h
@@ -242,9 +242,33 @@ AioContext *iohandler_get_aio_context(void);
  * must always be taken outside other locks.  This function helps
  * functions take different paths depending on whether the current
  * thread is running within the main loop mutex.
+ *
+ * This function should never be used in the block layer, because
+ * unit tests, block layer tools and qemu-storage-daemon do not
+ * have a BQL.
+ * Please instead refer to qemu_in_main_thread().
  */
 bool qemu_mutex_iothread_locked(void);
 
+/**
+ * qemu_in_main_thread: return whether it's possible to safely access
+ * the global state of the block layer.
+ *
+ * Global state of the block layer is not accessible from I/O threads
+ * or worker threads; only from threads that "own" the default
+ * AioContext that qemu_get_aio_context() returns.  For tests, block
+ * layer tools and qemu-storage-daemon there is a designated thread that
+ * runs the event loop for qemu_get_aio_context(), and that is the
+ * main thread.
+ *
+ * For emulators, however, any thread that holds the BQL can act
+ * as the block layer main thread; this will be any of the actual
+ * main thread, the vCPU threads or the RCU thread.
+ *
+ * For clarity, do not use this function outside the block layer.
+ */
+bool qemu_in_main_thread(void);
+
 /**
  * qemu_mutex_lock_iothread: Lock the main loop mutex.
  *
diff --git a/softmmu/cpus.c b/softmmu/cpus.c
index 23bca46b07..fd4e139289 100644
--- a/softmmu/cpus.c
+++ b/softmmu/cpus.c
@@ -485,6 +485,11 @@ bool qemu_mutex_iothread_locked(void)
     return iothread_locked;
 }
 
+bool qemu_in_main_thread(void)
+{
+    return qemu_mutex_iothread_locked();
+}
+
 /*
  * The BQL is taken from so many places that it is worth profiling the
  * callers directly, instead of funneling them all through a single function.
diff --git a/stubs/iothread-lock.c b/stubs/iothread-lock.c
index 5b45b7fc8b..ff7386e42c 100644
--- a/stubs/iothread-lock.c
+++ b/stubs/iothread-lock.c
@@ -6,6 +6,11 @@ bool qemu_mutex_iothread_locked(void)
     return false;
 }
 
+bool qemu_in_main_thread(void)
+{
+    return qemu_get_current_aio_context() == qemu_get_aio_context();
+}
+
 void qemu_mutex_lock_iothread_impl(const char *file, int line)
 {
 }
-- 
2.31.1



  reply	other threads:[~2022-02-11 14:58 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-11 14:51 [PATCH v7 00/31] block layer: split block APIs in global state and I/O Emanuele Giuseppe Esposito
2022-02-11 14:51 ` Emanuele Giuseppe Esposito [this message]
2022-02-11 14:51 ` [PATCH v7 02/31] main loop: macros to mark GS and I/O functions Emanuele Giuseppe Esposito
2022-03-01 11:29   ` Kevin Wolf
2022-02-11 14:51 ` [PATCH v7 03/31] include/block/block: split header into I/O and global state API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 04/31] assertions for block " Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 05/31] IO_CODE and IO_OR_GS_CODE for block I/O API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 06/31] block/export/fuse.c: allow writable exports to take RESIZE permission Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 07/31] include/sysemu/block-backend: split header into I/O and global state (GS) API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 08/31] block/block-backend.c: assertions for block-backend Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 09/31] IO_CODE and IO_OR_GS_CODE for block-backend I/O API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 10/31] block.c: assertions to the block layer permissions API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 11/31] include/block/block_int: split header into I/O and global state API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 12/31] assertions for block_int " Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 13/31] IO_CODE and IO_OR_GS_CODE for block_int I/O API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 14/31] block: introduce assert_bdrv_graph_writable Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 15/31] include/block/blockjob_int.h: split header into I/O and GS API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 16/31] GS and IO CODE macros for blockjob_int.h Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 17/31] block.c: add assertions to static functions Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 18/31] include/block/blockjob.h: global state API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 19/31] assertions for blockjob.h " Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 20/31] include/sysemu/blockdev.h: " Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 21/31] assertions for blockdev.h " Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 22/31] include/block/snapshot: global state API + assertions Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 23/31] block/copy-before-write.h: " Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 24/31] block/coroutines: I/O and "I/O or GS" API Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 25/31] block_int-common.h: split function pointers in BlockDriver Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 26/31] block_int-common.h: assertions in the callers of BlockDriver function pointers Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 27/31] block_int-common.h: split function pointers in BdrvChildClass Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 28/31] block_int-common.h: assertions in the callers of BdrvChildClass function pointers Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 29/31] block-backend-common.h: split function pointers in BlockDevOps Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 30/31] job.h: split function pointers in JobDriver Emanuele Giuseppe Esposito
2022-02-11 14:51 ` [PATCH v7 31/31] job.h: assertions in the callers of JobDriver function pointers Emanuele Giuseppe Esposito
2022-03-01 16:08 ` [PATCH v7 00/31] block layer: split block APIs in global state and I/O Kevin Wolf
2022-03-03 12:06   ` Emanuele Giuseppe Esposito

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220211145153.2861415-2-eesposit@redhat.com \
    --to=eesposit@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=fam@euphon.net \
    --cc=hreitz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=richard.henderson@linaro.org \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).