From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [PULL 20/20] monitor: do not use mb_read/mb_set
Date: Thu, 25 May 2023 16:15:32 +0200 [thread overview]
Message-ID: <20230525141532.295817-21-pbonzini@redhat.com> (raw)
In-Reply-To: <20230525141532.295817-1-pbonzini@redhat.com>
Instead of relying on magic memory barriers, document the pattern that
is being used. It is the one based on Dekker's algorithm, and in this
case it is embodied as follows:
enqueue request; sleeping = true;
smp_mb(); smp_mb();
if (sleeping) kick(); if (!have a request) yield();
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
monitor/qmp.c | 27 ++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/monitor/qmp.c b/monitor/qmp.c
index e6b1043c9f7b..c8e0156974d9 100644
--- a/monitor/qmp.c
+++ b/monitor/qmp.c
@@ -39,13 +39,16 @@
* coroutine never gets scheduled a second time when it's already
* scheduled (scheduling the same coroutine twice is forbidden).
*
- * It is true if the coroutine is active and processing requests.
- * Additional requests may then be pushed onto mon->qmp_requests,
- * and @qmp_dispatcher_co_shutdown may be set without further ado.
- * @qmp_dispatcher_co_busy must not be woken up in this case.
+ * It is true if the coroutine will process at least one more request
+ * before going to sleep. Either it has been kicked already, or it
+ * is active and processing requests. Additional requests may therefore
+ * be pushed onto mon->qmp_requests, and @qmp_dispatcher_co_shutdown may
+ * be set without further ado. @qmp_dispatcher_co must not be woken up
+ * in this case.
*
- * If false, you also have to set @qmp_dispatcher_co_busy to true and
- * wake up @qmp_dispatcher_co after pushing the new requests.
+ * If false, you have to wake up @qmp_dispatcher_co after pushing new
+ * requests. You also have to set @qmp_dispatcher_co_busy to true
+ * before waking up the coroutine.
*
* The coroutine will automatically change this variable back to false
* before it yields. Nobody else may set the variable to false.
@@ -230,15 +233,18 @@ static QMPRequest *monitor_qmp_dispatcher_pop_any(void)
{
while (true) {
/*
- * busy must be set to true again by whoever
- * rescheduled us to avoid double scheduling
+ * To avoid double scheduling, busy is true on entry to
+ * monitor_qmp_dispatcher_co(), and must be set again before
+ * aio_co_wake()-ing it.
*/
- assert(qatomic_mb_read(&qmp_dispatcher_co_busy) == true);
+ assert(qatomic_read(&qmp_dispatcher_co_busy) == true);
/*
* Mark the dispatcher as not busy already here so that we
* don't miss any new requests coming in the middle of our
* processing.
+ *
+ * Clear qmp_dispatcher_co_busy before reading request.
*/
qatomic_mb_set(&qmp_dispatcher_co_busy, false);
@@ -364,6 +370,9 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data)
void qmp_dispatcher_co_wake(void)
{
+ /* Write request before reading qmp_dispatcher_co_busy. */
+ smp_mb__before_rmw();
+
if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) {
aio_co_wake(qmp_dispatcher_co);
}
--
2.40.1
next prev parent reply other threads:[~2023-05-25 14:24 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-25 14:15 [PULL 00/20] Misc patches for 2023-05-25 Paolo Bonzini
2023-05-25 14:15 ` [PULL 01/20] target/i386: EPYC-Rome model without XSAVES Paolo Bonzini
2023-05-25 14:15 ` [PULL 02/20] meson.build: Fix glib -Wno-unused-function workaround Paolo Bonzini
2023-05-25 14:15 ` [PULL 03/20] meson: fix rule for qemu-ga installer Paolo Bonzini
2023-05-25 14:15 ` [PULL 04/20] meson: move -no-pie from linker to compiler Paolo Bonzini
2023-05-25 14:15 ` [PULL 05/20] tests/docker: simplify HOST_ARCH definition Paolo Bonzini
2023-05-25 14:15 ` [PULL 06/20] tests/vm: fix and " Paolo Bonzini
2023-05-25 14:15 ` [PULL 07/20] Makefile: remove $(TESTS_PYTHON) Paolo Bonzini
2023-05-25 14:15 ` [PULL 08/20] usb/ohci: Set pad to 0 after frame update Paolo Bonzini
2023-05-25 14:15 ` [PULL 09/20] softmmu/ioport.c: allocate MemoryRegionPortioList ports on the heap Paolo Bonzini
2023-05-25 14:15 ` [PULL 10/20] softmmu/ioport.c: QOMify MemoryRegionPortioList Paolo Bonzini
2023-05-25 14:15 ` [PULL 11/20] softmmu/ioport.c: make MemoryRegionPortioList owner of portio_list MemoryRegions Paolo Bonzini
2023-05-25 14:15 ` [PULL 12/20] monitor: use QEMU_LOCK_GUARD a bit more Paolo Bonzini
2023-05-25 14:15 ` [PULL 13/20] monitor: allow calling monitor_resume under mon_lock Paolo Bonzini
2023-05-25 14:15 ` [PULL 14/20] monitor: add more *_locked() functions Paolo Bonzini
2023-05-25 14:15 ` [PULL 15/20] monitor: do not use mb_read/mb_set for suspend_cnt Paolo Bonzini
2023-05-25 14:15 ` [PULL 16/20] monitor: cleanup detection of qmp_dispatcher_co shutting down Paolo Bonzini
2023-05-25 14:15 ` [PULL 17/20] monitor: cleanup fetching of QMP requests Paolo Bonzini
2023-05-25 14:15 ` [PULL 18/20] monitor: introduce qmp_dispatcher_co_wake Paolo Bonzini
2023-05-25 14:15 ` [PULL 19/20] monitor: extract request dequeuing to a new function Paolo Bonzini
2023-05-25 14:15 ` Paolo Bonzini [this message]
2023-05-25 17:15 ` [PULL 00/20] Misc patches for 2023-05-25 Richard Henderson
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=20230525141532.295817-21-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).