From patchwork Fri Jun 5 17:34:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 187558 Delivered-To: patch@linaro.org Received: by 2002:a92:cf06:0:0:0:0:0 with SMTP id c6csp2585044ilo; Fri, 5 Jun 2020 10:38:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg3tjsJXaHBK/Vlv5viwg/RPtAZjMOzaavLz3sRtyThxQ9+UzaoCcBYgcXcVLctG/tPfRv X-Received: by 2002:a25:4f44:: with SMTP id d65mr19292944ybb.149.1591378696035; Fri, 05 Jun 2020 10:38:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591378696; cv=none; d=google.com; s=arc-20160816; b=wjN3GzY7EMLJZVSUpbngb7KrA2scKyuFo+hqZjOW+YXR5vmbkkR/5FQIzrq/ve+6QA iGsnvL1ZLvRFhdQ8wih4DC49mN8sZfMtWpTQWdIgCa7bnoutTef5sj1Ojw/gBLtS1ke1 5QpfVlWogaxHOR2ymLUelgbCzStIBkI06MGSobSjS8oOZYjLKxmrcmPMjSWXcGzGCTs9 IaVt4riG9jy81aM20gzZrmBJ0vT/xmB1a+eXiWJhLxwS6i2etIqcS7NexeBs0gBjoc+p pdsDBDe1SFKPtCFbFgsDHQBOlE4cohWz9QRALw8wHeiBlhf1fDMEtloVmkx3C+3PCeMO FWeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=QonKzFLvxHetjYHv0eko5X68Tto6P2i1o+bAUOOMVq8=; b=nXx+ExjLOlB25BTScfPHk81rfPrmg6E2ZZal0RcXCydZu47rIqoznAnlA8we37wLk8 O+I6RDFmLewDACfhAp2CM6sBApWw2YjprXRnRJR9+7S/VeGGMMVCeK7N12qRqcSn1Pb6 L0fRZT9d3n/Ln3dgo15OSxWsZqJVtsCjzskMVYpLzFRVDF3IVGQMuBWGMDlJv+rYlM+N a9y2RZEsYq+aAdpXLANRj8osn2BT0nZrY8VGfkWUOCvK/pwvXDQDKFNv0Xp7S0WLmd/R O8gDVSYf47Uy4DLAAv8mlaavN8X/8GftbNrIl7GZk4Xgf86I2NffHCySR/YyGsl165Cg k1bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="IqfJ0B/6"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id n13si7015526ybp.102.2020.06.05.10.38.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Jun 2020 10:38:16 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="IqfJ0B/6"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:54636 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jhGIJ-0003Og-Gy for patch@linaro.org; Fri, 05 Jun 2020 13:38:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48138) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jhGHh-0003Jb-S4 for qemu-devel@nongnu.org; Fri, 05 Jun 2020 13:37:37 -0400 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]:34899) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jhGHg-0003q0-Qn for qemu-devel@nongnu.org; Fri, 05 Jun 2020 13:37:37 -0400 Received: by mail-pf1-x443.google.com with SMTP id h185so5290034pfg.2 for ; Fri, 05 Jun 2020 10:37:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QonKzFLvxHetjYHv0eko5X68Tto6P2i1o+bAUOOMVq8=; b=IqfJ0B/6o/HB9WrnAdz0DzKWhSIhvU3L3BtJurcDXQWusz3uv1YGZvnKy6Rs3X+nWg 5XXLGHGguFT97AAbAjokOTflLlO8dBghVl/0KYD2FAsii+0MG5mJmbWUU1iqkkANWH6K b5Huid8G04XSqnLaybnwT7+/cqUnhzOJste3Ud3rYr+k1h0sL0mxN2juY8D7p9zzlniG /ebMO5NcoumH2H5cwQwHGKreFd4l9wrzaW8mEz1qQLUeZ9kgBm8dDZTV+jpJnfAxhfDw PIQEJ+8ILMnHve/9wjDAkQP8dkg2LGOmxki/mXRV3+eo6+tO1NUrXUWVu8oqOuGLanjL 4M+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QonKzFLvxHetjYHv0eko5X68Tto6P2i1o+bAUOOMVq8=; b=JAoG+8fpzJrNde8xQJNhgsrGHr9zSgIenUUp43UDT2aHr8sKmqBLKTpQ8kpxPihDXo gWI/1iMTUXM9Va6lzkK2ahDTuNzHF2M+00E/M7Ju0xjfPQ5U1ErwMioBzA1hEOTMri/T 8wDxlFqtcKlkpKwB3l+/KiNynlFyCtEhwfXIsQwlb+UZZiZ7WEpl67hFI2enUOa7Po10 z3mjIUgrqgXBtZOOaD8bj3TVZhjxv6AVDN0I/gzjFaw09o3qE2Xd6/AEyCa13Kt/or65 FYZa5ubSM5QDAIHkw8kQrzsxhRLAmNDFZJTDHOJlRQmga4spjU3NQsrkSHugM6NoMpCX E+0g== X-Gm-Message-State: AOAM533mlCb0T8+m1XBlvyA8zAeQR7PcLC5vE5Cnc6OFLLGFFGnxTOYH nrN1//d522eqvSD8jgVQZuypMXrPPF3J+Q== X-Received: by 2002:aa7:868b:: with SMTP id d11mr10304598pfo.52.1591378655149; Fri, 05 Jun 2020 10:37:35 -0700 (PDT) Received: from Rfoley-MA01.hsd1.ma.comcast.net (c-73-47-162-176.hsd1.ma.comcast.net. [73.47.162.176]) by smtp.gmail.com with ESMTPSA id a19sm188307pfd.165.2020.06.05.10.37.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jun 2020 10:37:34 -0700 (PDT) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v2 02/13] cpu: convert queued work to a QSIMPLEQ Date: Fri, 5 Jun 2020 13:34:11 -0400 Message-Id: <20200605173422.1490-3-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200605173422.1490-1-robert.foley@linaro.org> References: <20200605173422.1490-1-robert.foley@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::443; envelope-from=robert.foley@linaro.org; helo=mail-pf1-x443.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: robert.foley@linaro.org, Eduardo Habkost , cota@braap.org, Paolo Bonzini , peter.puhov@linaro.org, alex.bennee@linaro.org, Richard Henderson Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: "Emilio G. Cota" We convert queued work to a QSIMPLEQ, 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 Reviewed-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Emilio G. Cota Signed-off-by: Robert Foley --- cpus-common.c | 25 ++++++++----------------- cpus.c | 14 ++++++++++++-- hw/core/cpu.c | 1 + include/hw/core/cpu.h | 6 +++--- 4 files changed, 24 insertions(+), 22 deletions(-) -- 2.17.1 diff --git a/cpus-common.c b/cpus-common.c index 70a9d12981..8f5512b3d7 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -97,7 +97,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; @@ -106,13 +106,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); @@ -306,17 +300,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 5670c96bcf..af44027549 100644 --- a/cpus.c +++ b/cpus.c @@ -97,9 +97,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)) { @@ -1498,7 +1508,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/hw/core/cpu.c b/hw/core/cpu.c index 5284d384fb..77703d62b7 100644 --- a/hw/core/cpu.c +++ b/hw/core/cpu.c @@ -368,6 +368,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); diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 07f7698155..d78ff1d165 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -331,8 +331,8 @@ struct qemu_work_item; * @opaque: User data. * @mem_io_pc: Host Program Counter 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). @@ -376,7 +376,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;