From patchwork Thu Oct 25 14:45:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 10656121 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6A48314BB for ; Thu, 25 Oct 2018 15:52:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 586AE2BE88 for ; Thu, 25 Oct 2018 15:52:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48DC82BE8D; Thu, 25 Oct 2018 15:52:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B6EA92BE88 for ; Thu, 25 Oct 2018 15:52:38 +0000 (UTC) Received: from localhost ([::1]:55304 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFhw5-0007ja-RS for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Oct 2018 11:52:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60142) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFh5V-0003ub-Vw for qemu-devel@nongnu.org; Thu, 25 Oct 2018 10:58:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFguW-0007gA-Gj for qemu-devel@nongnu.org; Thu, 25 Oct 2018 10:46:58 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:56941) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFguV-0007TS-EV for qemu-devel@nongnu.org; Thu, 25 Oct 2018 10:46:55 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 0E6A421C5C; Thu, 25 Oct 2018 10:46:46 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 25 Oct 2018 10:46:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h= from:to:cc:subject:date:message-id; s=mesmtp; bh=Kl9/e94b4iz8+la PSJpRLm9JH295gK36UvzagV32Dew=; b=KPNe+utVmnR6O4dn3jc+xw4Os1d18yw Ai49ceFjog2CypPaxYUV5W7k4m4kwoh3LDKZksJltmN1V1TYY93mg0JQRye//XdE 1CGnPAbMl3wk0ElVcSCSN+aUA+ndvyGAPA86VbwwtFWCMtRMDtOymwLI4KdZaYDw PYNiW3HJiFyg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:message-id:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=Kl9/e94b4iz8+laPSJpRLm9JH295gK36UvzagV32Dew=; b=EfPlgU8P CdBJv6gMAHz5zdd89UcfhAb/If7hl4o0K8056FxQ2q7FVCMy0i1IUH5EPJRTHTZX QOOhykOmIGaJ7SmXNlfJk3viJ7oTyUW1H//3vz0v5pXbkneJCnC5HQMmApL7u10I aK2sjgBvh3gYrIf9NzXBQGTDrBFXwtNfbX8fqcS9GWXimYSo4EelpHd82VNNSLYz Q/fosEvWYROr05MYaopMlkbZJddZg7eIJQVKlE6Ea+ivI/Ijo0hruxQLfPHNQ/30 jemYakrR5lltrS8SDkUJhKMpacOZwzxySTfE00BUbc8oqj4VgQcmMts7U8XxKqZy Igldg8+CDh0gHg== X-ME-Sender: X-ME-Proxy: Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 4F17BE421C; Thu, 25 Oct 2018 10:46:45 -0400 (EDT) From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Thu, 25 Oct 2018 10:45:34 -0400 Message-Id: <20181025144644.15464-1-cota@braap.org> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.27 Subject: [Qemu-devel] [RFC v4 01/71] cpu: convert queued work to a QSIMPLEQ X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Instead of open-coding it. While at it, make sure that all accesses to the list are performed while holding the list's lock. Reviewed-by: Richard Henderson Signed-off-by: Emilio G. Cota Reviewed-by: Alex Bennée --- include/qom/cpu.h | 6 +++--- cpus-common.c | 25 ++++++++----------------- cpus.c | 14 ++++++++++++-- qom/cpu.c | 1 + 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/include/qom/cpu.h b/include/qom/cpu.h index dc130cd307..53488b202f 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -315,8 +315,8 @@ struct qemu_work_item; * @mem_io_pc: Host Program Counter at which the memory was accessed. * @mem_io_vaddr: Target virtual address at which the memory was accessed. * @kvm_fd: vCPU file descriptor for KVM. - * @work_mutex: Lock to prevent multiple access to queued_work_*. - * @queued_work_first: First asynchronous work pending. + * @work_mutex: Lock to prevent multiple access to @work_list. + * @work_list: List of pending asynchronous work. * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes * to @trace_dstate). * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask). @@ -357,7 +357,7 @@ struct CPUState { sigjmp_buf jmp_env; QemuMutex work_mutex; - struct qemu_work_item *queued_work_first, *queued_work_last; + QSIMPLEQ_HEAD(, qemu_work_item) work_list; CPUAddressSpace *cpu_ases; int num_ases; diff --git a/cpus-common.c b/cpus-common.c index 98dd8c6ff1..a2a6cd93a1 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -107,7 +107,7 @@ void cpu_list_remove(CPUState *cpu) } struct qemu_work_item { - struct qemu_work_item *next; + QSIMPLEQ_ENTRY(qemu_work_item) node; run_on_cpu_func func; run_on_cpu_data data; bool free, exclusive, done; @@ -116,13 +116,7 @@ struct qemu_work_item { static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) { qemu_mutex_lock(&cpu->work_mutex); - if (cpu->queued_work_first == NULL) { - cpu->queued_work_first = wi; - } else { - cpu->queued_work_last->next = wi; - } - cpu->queued_work_last = wi; - wi->next = NULL; + QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node); wi->done = false; qemu_mutex_unlock(&cpu->work_mutex); @@ -314,17 +308,14 @@ void process_queued_cpu_work(CPUState *cpu) { struct qemu_work_item *wi; - if (cpu->queued_work_first == NULL) { + qemu_mutex_lock(&cpu->work_mutex); + if (QSIMPLEQ_EMPTY(&cpu->work_list)) { + qemu_mutex_unlock(&cpu->work_mutex); return; } - - qemu_mutex_lock(&cpu->work_mutex); - while (cpu->queued_work_first != NULL) { - wi = cpu->queued_work_first; - cpu->queued_work_first = wi->next; - if (!cpu->queued_work_first) { - cpu->queued_work_last = NULL; - } + while (!QSIMPLEQ_EMPTY(&cpu->work_list)) { + wi = QSIMPLEQ_FIRST(&cpu->work_list); + QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node); qemu_mutex_unlock(&cpu->work_mutex); if (wi->exclusive) { /* Running work items outside the BQL avoids the following deadlock: diff --git a/cpus.c b/cpus.c index cce64874e6..6d86522031 100644 --- a/cpus.c +++ b/cpus.c @@ -88,9 +88,19 @@ bool cpu_is_stopped(CPUState *cpu) return cpu->stopped || !runstate_is_running(); } +static inline bool cpu_work_list_empty(CPUState *cpu) +{ + bool ret; + + qemu_mutex_lock(&cpu->work_mutex); + ret = QSIMPLEQ_EMPTY(&cpu->work_list); + qemu_mutex_unlock(&cpu->work_mutex); + return ret; +} + static bool cpu_thread_is_idle(CPUState *cpu) { - if (cpu->stop || cpu->queued_work_first) { + if (cpu->stop || !cpu_work_list_empty(cpu)) { return false; } if (cpu_is_stopped(cpu)) { @@ -1509,7 +1519,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) cpu = first_cpu; } - while (cpu && !cpu->queued_work_first && !cpu->exit_request) { + while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) { atomic_mb_set(&tcg_current_rr_cpu, cpu); current_cpu = cpu; diff --git a/qom/cpu.c b/qom/cpu.c index 20ad54d43f..c47169896e 100644 --- a/qom/cpu.c +++ b/qom/cpu.c @@ -373,6 +373,7 @@ static void cpu_common_initfn(Object *obj) cpu->nr_threads = 1; qemu_mutex_init(&cpu->work_mutex); + QSIMPLEQ_INIT(&cpu->work_list); QTAILQ_INIT(&cpu->breakpoints); QTAILQ_INIT(&cpu->watchpoints);