From patchwork Tue Jun 16 12:53:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 188023 Delivered-To: patch@linaro.org Received: by 2002:a92:cf06:0:0:0:0:0 with SMTP id c6csp3622905ilo; Tue, 16 Jun 2020 05:55:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz9vZ9kzTJi0GVAE05CXFXWJvGISuiFFm4Igs1hqoy5MNaaiF4Uj1Es1gorlZ2ZaK5xyzRT X-Received: by 2002:a25:da92:: with SMTP id n140mr4000857ybf.313.1592312129248; Tue, 16 Jun 2020 05:55:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592312129; cv=none; d=google.com; s=arc-20160816; b=QYXoHriXkbhTz2Y7KLQWv9B89Zp298Eqa4m8YUx7hsaFt7TjT0dQHYIgoPFIb9NG0q sgAyVw9w+32hfmx3mlSzCWT8qdl+KT12ejeCnhZrXY6y1L15m6pKBWKwiqaODclGUeOf ct4HQvaOpdxmlOvHz2O/LGsrXVilMJm72PhxlyW3YtxweU5ZZDGY8ADuNEwUb4cHkQuS uywEXLSRI0Z//i/cZSY+zOQ3Xkb0brrmkP1d56ujMXELifjXXjcX0acxU6PgWpj6X252 ECFN+KIpI5i8HooJrte6/4kdpTjz8P0TfpQYd3ANARt2t3TlwmD5l/ddR57BnGssMeDd 8I0A== 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=I/tP7FlfNorVb50ogwJDlb1Wfq6uclDPBsfOKYTX7pc=; b=R+iZzQE94tz8ClDPy6opY9vp78mISizAjPqmtIWAHZHY+CRBFr6WrWeZl+SLWSafOw m4ea1jh+UN8PGropn58km/ZyNIlmsUy872maUJ7o50tcyHh18VTqX/PJ/I1r4iNsYqFt KSbqkMaD/7w+IgKF61SUvvyc1KpJ8hKoZ/Xn04LSQvgAMN72pkm0UmOwydtrgPtUQczf iNHvLRfdJ+dnv0lVbtu5k3MfkpUBxkdTMqlpHdAM4ItUUUcjk7HOUBsxNngI7xKc9US2 yX8KJn1IMySaMuEc/xUnStq55DoPCUprZgtXZP+AlD4BYoEZQBp4ReBEJHEMUsMYvbf6 rq3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=QpqUtaAT; 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 o2si6483628ybq.107.2020.06.16.05.55.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Jun 2020 05:55:29 -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=QpqUtaAT; 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]:41552 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlB7g-00074a-Ke for patch@linaro.org; Tue, 16 Jun 2020 08:55:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33770) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlB5s-0003lS-GT for qemu-devel@nongnu.org; Tue, 16 Jun 2020 08:53:36 -0400 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]:46661) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jlB5q-000063-IM for qemu-devel@nongnu.org; Tue, 16 Jun 2020 08:53:36 -0400 Received: by mail-wr1-x430.google.com with SMTP id x6so20613013wrm.13 for ; Tue, 16 Jun 2020 05:53:34 -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=I/tP7FlfNorVb50ogwJDlb1Wfq6uclDPBsfOKYTX7pc=; b=QpqUtaAT5ybNKBjCAcATGQrLWb4FLyyUvmEovuC0BnvgVrDlRzJxsz7+/w+Qvq/6xO 0pJiqthVLFwdXjSMSuGPxzmE4CHaeTChB/OEG9sVez4NpTS4VJVRcIj7yviQ/Zc1okAf eYrY/ZLRC9u+IhcvN7CxffMnNB0FagyDyc4EOHe/kM3QDFiq/TJLgA5JaeBizz1ziGJb JKaCPgvPzY3n3/U64+utEwtGpBThDxd/bByselowCxq+nq7H/Lm/0dGh9V7wCTIxW+Aj IoP2Xr/fmKIRHmwbjv7za54oVTXcyay80TSn8TdEw9hkfiHEFVkXnQJ8w39hSWAF3LDG xpbQ== 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=I/tP7FlfNorVb50ogwJDlb1Wfq6uclDPBsfOKYTX7pc=; b=i8Hd6jgrLp+XWlDmn5rPBsAFX0Hvgr40Xm68ssKFDuDQOuCsRdwK7fiPKy5DYyKtta KIQd2o3u6EawRxEmoZYxLGrlj+S4rxCQPw64Ic3hHJqUkVR7OLvuRXuIKlYyN3IlDb8S yiJQj7tbAqbUmVQvu1CPBGdjborAKuRFbCU0Vr5MdtdNBrsz9BsS4dsE/TWsXOTbMjNH ZaxLeo3lyUdqegJFfFW/FwZnIPH5kxH0wsC++toPNM6Pu+0fFhatLVjHmZv05krBiTUQ qUKaHsOUJWpC8BrwQM+rN75fmkFZmokqriCStTSafByJw8/n4NRTN52ECOV8rUtC5PO1 xS6w== X-Gm-Message-State: AOAM5318cFohbNmqNCPvGV2JioJamc5AeajGlQbEhzgPDjEjtCL+na3C HY2H0p/bNzdivXL41fsckug4UQ== X-Received: by 2002:adf:f207:: with SMTP id p7mr2980297wro.206.1592312013077; Tue, 16 Jun 2020 05:53:33 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id g18sm3944235wme.17.2020.06.16.05.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 05:53:28 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id C0B691FF91; Tue, 16 Jun 2020 13:53:24 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 05/21] cpu: convert queued work to a QSIMPLEQ Date: Tue, 16 Jun 2020 13:53:08 +0100 Message-Id: <20200616125324.19045-6-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200616125324.19045-1-alex.bennee@linaro.org> References: <20200616125324.19045-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::430; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x430.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_HELO_NONE=0.001, SPF_PASS=-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 , =?utf-8?q?Philippe_Mathieu-Dau?= =?utf-8?b?ZMOp?= , Richard Henderson , qemu-devel@nongnu.org, Eduardo Habkost , "Emilio G. Cota" , Paolo Bonzini , =?utf-8?q?Alex_Benn=C3=A9e?= , 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 Signed-off-by: Alex Bennée Message-Id: <20200609200738.445-3-robert.foley@linaro.org> Message-Id: <20200612190237.30436-6-alex.bennee@linaro.org> -- 2.20.1 diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 497600c49ef..b3f4b793182 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; diff --git a/cpus-common.c b/cpus-common.c index 70a9d12981a..8f5512b3d78 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 34fc2038084..7317ae06b9e 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)) { @@ -1518,7 +1528,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 f31ec48ee61..80d51c24dd2 100644 --- a/hw/core/cpu.c +++ b/hw/core/cpu.c @@ -370,6 +370,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);