qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 23/28] cpus-common: Introduce async_safe_run_on_cpu()
Date: Mon, 26 Sep 2016 15:40:53 +0200	[thread overview]
Message-ID: <1474897258-1205-24-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1474897258-1205-1-git-send-email-pbonzini@redhat.com>

Reviewed-by: Richard Henderson <rth@twiddle.net>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cpus-common.c     | 33 +++++++++++++++++++++++++++++++--
 include/qom/cpu.h | 14 ++++++++++++++
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/cpus-common.c b/cpus-common.c
index 429652c..38b1d55 100644
--- a/cpus-common.c
+++ b/cpus-common.c
@@ -18,6 +18,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "exec/cpu-common.h"
 #include "qom/cpu.h"
 #include "sysemu/cpus.h"
@@ -106,7 +107,7 @@ struct qemu_work_item {
     struct qemu_work_item *next;
     run_on_cpu_func func;
     void *data;
-    bool free, done;
+    bool free, exclusive, done;
 };
 
 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
@@ -139,6 +140,7 @@ void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
     wi.data = data;
     wi.done = false;
     wi.free = false;
+    wi.exclusive = false;
 
     queue_work_on_cpu(cpu, &wi);
     while (!atomic_mb_read(&wi.done)) {
@@ -229,6 +231,19 @@ void cpu_exec_end(CPUState *cpu)
     qemu_mutex_unlock(&qemu_cpu_list_lock);
 }
 
+void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data)
+{
+    struct qemu_work_item *wi;
+
+    wi = g_malloc0(sizeof(struct qemu_work_item));
+    wi->func = func;
+    wi->data = data;
+    wi->free = true;
+    wi->exclusive = true;
+
+    queue_work_on_cpu(cpu, wi);
+}
+
 void process_queued_cpu_work(CPUState *cpu)
 {
     struct qemu_work_item *wi;
@@ -245,7 +260,21 @@ void process_queued_cpu_work(CPUState *cpu)
             cpu->queued_work_last = NULL;
         }
         qemu_mutex_unlock(&cpu->work_mutex);
-        wi->func(cpu, wi->data);
+        if (wi->exclusive) {
+            /* Running work items outside the BQL avoids the following deadlock:
+             * 1) start_exclusive() is called with the BQL taken while another
+             * CPU is running; 2) cpu_exec in the other CPU tries to takes the
+             * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
+             * neither CPU can proceed.
+             */
+            qemu_mutex_unlock_iothread();
+            start_exclusive();
+            wi->func(cpu, wi->data);
+            end_exclusive();
+            qemu_mutex_lock_iothread();
+        } else {
+            wi->func(cpu, wi->data);
+        }
         qemu_mutex_lock(&cpu->work_mutex);
         if (wi->free) {
             g_free(wi);
diff --git a/include/qom/cpu.h b/include/qom/cpu.h
index 934c07a..4092dd9 100644
--- a/include/qom/cpu.h
+++ b/include/qom/cpu.h
@@ -656,6 +656,20 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data);
 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data);
 
 /**
+ * async_safe_run_on_cpu:
+ * @cpu: The vCPU to run on.
+ * @func: The function to be executed.
+ * @data: Data to pass to the function.
+ *
+ * Schedules the function @func for execution on the vCPU @cpu asynchronously,
+ * while all other vCPUs are sleeping.
+ *
+ * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
+ * BQL.
+ */
+void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data);
+
+/**
  * qemu_get_cpu:
  * @index: The CPUState@cpu_index value of the CPU to obtain.
  *
-- 
2.7.4

  parent reply	other threads:[~2016-09-26 13:41 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-26 13:40 [Qemu-devel] [PULL 00/28] Misc patches for 2016-09-26 Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 01/28] memory: introduce IOMMUNotifier and its caps Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 02/28] memory: introduce IOMMUOps.notify_flag_changed Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 03/28] intel_iommu: allow UNMAP notifiers Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 04/28] x86: ioapic: boost default version to 0x20 Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 05/28] checkpatch: downgrade "architecture specific defines should be avoided" Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 06/28] compiler: Swap 'public domain' header for license Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 07/28] migration: sync all address spaces Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 08/28] build-sys: remove unused GLIB_CFLAGS Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 09/28] build-sys: put glib_cflags in QEMU_CFLAGS Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 10/28] cpus: pass CPUState to run_on_cpu helpers Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 11/28] cpus: Move common code out of {async_, }run_on_cpu() Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 12/28] cpus: Rename flush_queued_work() Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 13/28] linux-user: Use QemuMutex and QemuCond Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 14/28] linux-user: Add qemu_cpu_is_self() and qemu_cpu_kick() Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 15/28] cpus-common: move CPU list management to common code Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 16/28] cpus-common: move CPU work item " Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 17/28] cpus-common: fix uninitialized variable use in run_on_cpu Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 18/28] cpus-common: move exclusive work infrastructure from linux-user Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 19/28] docs: include formal model for TCG exclusive sections Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 20/28] cpus-common: always defer async_run_on_cpu work items Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 21/28] cpus-common: remove redundant call to exclusive_idle() Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 22/28] cpus-common: simplify locking for start_exclusive/end_exclusive Paolo Bonzini
2016-09-26 13:40 ` Paolo Bonzini [this message]
2016-09-26 13:40 ` [Qemu-devel] [PULL 24/28] tcg: Make tb_flush() thread safe Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 25/28] cpus-common: lock-free fast path for cpu_exec_start/end Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 26/28] replay: move internal data to the structure Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 27/28] replay: vmstate for replay module Paolo Bonzini
2016-09-26 13:40 ` [Qemu-devel] [PULL 28/28] replay: allow replay stopping and restarting Paolo Bonzini
2016-09-26 14:26 ` [Qemu-devel] [PULL 00/28] Misc patches for 2016-09-26 no-reply
2016-09-26 21:19 ` Peter Maydell
2016-09-27  2:12   ` Peter Xu
2016-09-27  8:53     ` Paolo Bonzini
2016-09-27  9:25       ` Peter Xu
2016-09-27 10:11         ` Paolo Bonzini
2016-09-28  3:12           ` Peter Xu
2016-09-28  9:54             ` Jason Wang

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=1474897258-1205-24-git-send-email-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).