From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44167) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boWAE-0006NM-W4 for qemu-devel@nongnu.org; Mon, 26 Sep 2016 09:41:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1boWAB-0004wk-6V for qemu-devel@nongnu.org; Mon, 26 Sep 2016 09:41:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57720) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boWAA-0004wR-T2 for qemu-devel@nongnu.org; Mon, 26 Sep 2016 09:41:43 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 373809D0F4 for ; Mon, 26 Sep 2016 13:41:42 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-31.ams2.redhat.com [10.36.112.31]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8QDexeD009350 for ; Mon, 26 Sep 2016 09:41:41 -0400 From: Paolo Bonzini Date: Mon, 26 Sep 2016 15:40:53 +0200 Message-Id: <1474897258-1205-24-git-send-email-pbonzini@redhat.com> In-Reply-To: <1474897258-1205-1-git-send-email-pbonzini@redhat.com> References: <1474897258-1205-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 23/28] cpus-common: Introduce async_safe_run_on_cpu() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Reviewed-by: Richard Henderson Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Paolo Bonzini --- 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 @@ */ =20 #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; }; =20 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 fun= c, void *data, wi.data =3D data; wi.done =3D false; wi.free =3D false; + wi.exclusive =3D false; =20 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); } =20 +void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *da= ta) +{ + struct qemu_work_item *wi; + + wi =3D g_malloc0(sizeof(struct qemu_work_item)); + wi->func =3D func; + wi->data =3D data; + wi->free =3D true; + wi->exclusive =3D 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 =3D NULL; } qemu_mutex_unlock(&cpu->work_mutex); - wi->func(cpu, wi->data); + if (wi->exclusive) { + /* Running work items outside the BQL avoids the following d= eadlock: + * 1) start_exclusive() is called with the BQL taken while a= nother + * CPU is running; 2) cpu_exec in the other CPU tries to tak= es the + * BQL, so it goes to sleep; start_exclusive() is sleeping t= oo, 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); =20 /** + * 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 asynchron= ously, + * while all other vCPUs are sleeping. + * + * Unlike run_on_cpu and async_run_on_cpu, the function is run outside t= he + * BQL. + */ +void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *da= ta); + +/** * qemu_get_cpu: * @index: The CPUState@cpu_index value of the CPU to obtain. * --=20 2.7.4