From nobody Wed Nov 5 10:12:08 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534670178507970.9020430188041; Sun, 19 Aug 2018 02:16:18 -0700 (PDT) Received: from localhost ([::1]:42136 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frJoe-0003yC-Hp for importer@patchew.org; Sun, 19 Aug 2018 05:16:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57170) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frJmS-0002N7-8E for qemu-devel@nongnu.org; Sun, 19 Aug 2018 05:13:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frJmP-0000dL-Gq for qemu-devel@nongnu.org; Sun, 19 Aug 2018 05:13:52 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:37507) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1frJmP-0000ci-6V; Sun, 19 Aug 2018 05:13:49 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 9835821B87; Sun, 19 Aug 2018 05:13:48 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sun, 19 Aug 2018 05:13:48 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 2474910292; Sun, 19 Aug 2018 05:13:48 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=mesmtp; bh=JnUxYNRW3fwY2U mblAgqdR4V4w8xakOQDyPvvvXQEZE=; b=bTJreHAQS0fZYPUm0yaKPo2MAKnIyx n+o9XFOKyLczWmqobOY/SW+pWZHmvIB3hOKHvCJF69/ODHK5Ww6XN71fhbQA8spY 2zVqS+3gbQMuH1ZvWnUEum+ZCSkkLNLcvSiq1xWw7YcGLKVcA/7BCf4tXR73sUh6 oqwxNvbbWDgI0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=JnUxYNRW3fwY2UmblAgqdR4V4w8xakOQDyPvvvXQEZE=; b=iRBPveLE qHerKDCvkUJNdkM49Ge7PhGqmGqjIdYqjKwCBhgBTjVq7VAcDkGSLKKnvVTU8gzd tbDb2W6t5wzS21jbECjwmqNXQt3cW+PWO4EIMGiDuRMNBnxUBnY4htUIChNbOCyq UCND/Tr/TyT3aNXtnJ/Qu9hPdjxdz4MR3DeNZ5V+OFJVoyneiS/AuS8aVxwxSJJO RodbCbKOgURSmovXRKFYC+leuSeJUbNiyiP6Y7m2R7QhpW41wqPv0tbrQTWeR5bH bISmF9FOGjvMa/LC5GqG+TzEWE7PLpUjbiy2SyfMr18VT38bHRk/Yrv4zYysopWH cwSqo5oR9r4g2Q== X-ME-Proxy: X-ME-Sender: From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Sun, 19 Aug 2018 05:13:35 -0400 Message-Id: <20180819091335.22863-12-cota@braap.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180819091335.22863-1-cota@braap.org> References: <20180819091335.22863-1-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.25 Subject: [Qemu-devel] [PATCH v2 11/11] qom: convert the CPU list to RCU 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: Peter Crosthwaite , Riku Voipio , Alexander Graf , Laurent Vivier , qemu-ppc@nongnu.org, Paolo Bonzini , David Gibson , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Iterating over the list without using atomics is undefined behaviour, since the list can be modified concurrently by other threads (e.g. every time a new thread is created in user-mode). Fix it by implementing the CPU list as an RCU QTAILQ. This requires a little bit of extra work to traverse list in reverse order (see previous patch), but other than that the conversion is trivial. Signed-off-by: Emilio G. Cota --- include/qom/cpu.h | 11 +++++------ cpus-common.c | 4 ++-- cpus.c | 2 +- linux-user/main.c | 2 +- linux-user/syscall.c | 2 +- target/s390x/cpu_models.c | 2 +- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/qom/cpu.h b/include/qom/cpu.h index ecf6ed556a..dc130cd307 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -26,6 +26,7 @@ #include "exec/memattrs.h" #include "qapi/qapi-types-run-state.h" #include "qemu/bitmap.h" +#include "qemu/rcu_queue.h" #include "qemu/queue.h" #include "qemu/thread.h" =20 @@ -442,13 +443,11 @@ struct CPUState { =20 QTAILQ_HEAD(CPUTailQ, CPUState); extern struct CPUTailQ cpus; -#define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node) -#define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node) +#define first_cpu QTAILQ_FIRST_RCU(&cpus) +#define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node) +#define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus, node) #define CPU_FOREACH_SAFE(cpu, next_cpu) \ - QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu) -#define CPU_FOREACH_REVERSE(cpu) \ - QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node) -#define first_cpu QTAILQ_FIRST(&cpus) + QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus, node, next_cpu) =20 extern __thread CPUState *current_cpu; =20 diff --git a/cpus-common.c b/cpus-common.c index 59f751ecf9..98dd8c6ff1 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -84,7 +84,7 @@ void cpu_list_add(CPUState *cpu) } else { assert(!cpu_index_auto_assigned); } - QTAILQ_INSERT_TAIL(&cpus, cpu, node); + QTAILQ_INSERT_TAIL_RCU(&cpus, cpu, node); qemu_mutex_unlock(&qemu_cpu_list_lock); =20 finish_safe_work(cpu); @@ -101,7 +101,7 @@ void cpu_list_remove(CPUState *cpu) =20 assert(!(cpu_index_auto_assigned && cpu !=3D QTAILQ_LAST(&cpus, CPUTai= lQ))); =20 - QTAILQ_REMOVE(&cpus, cpu, node); + QTAILQ_REMOVE_RCU(&cpus, cpu, node); cpu->cpu_index =3D UNASSIGNED_CPU_INDEX; qemu_mutex_unlock(&qemu_cpu_list_lock); } diff --git a/cpus.c b/cpus.c index b5844b7103..d60f8603fd 100644 --- a/cpus.c +++ b/cpus.c @@ -1491,7 +1491,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) atomic_mb_set(&cpu->exit_request, 0); } =20 - qemu_tcg_rr_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus)); + qemu_tcg_rr_wait_io_event(cpu ? cpu : first_cpu); deal_with_unplugged_cpus(); } =20 diff --git a/linux-user/main.c b/linux-user/main.c index ea00dd9057..923cbb753a 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -126,7 +126,7 @@ void fork_end(int child) Discard information about the parent threads. */ CPU_FOREACH_SAFE(cpu, next_cpu) { if (cpu !=3D thread_cpu) { - QTAILQ_REMOVE(&cpus, cpu, node); + QTAILQ_REMOVE_RCU(&cpus, cpu, node); } } qemu_init_cpu_list(); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index bb42a225eb..95ac0102bf 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8051,7 +8051,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long = arg1, TaskState *ts; =20 /* Remove the CPU from the list. */ - QTAILQ_REMOVE(&cpus, cpu, node); + QTAILQ_REMOVE_RCU(&cpus, cpu, node); =20 cpu_list_unlock(); =20 diff --git a/target/s390x/cpu_models.c b/target/s390x/cpu_models.c index 604898a882..d1a45bd8c0 100644 --- a/target/s390x/cpu_models.c +++ b/target/s390x/cpu_models.c @@ -1110,7 +1110,7 @@ void s390_set_qemu_cpu_model(uint16_t type, uint8_t g= en, uint8_t ec_ga, const S390CPUDef *def =3D s390_find_cpu_def(type, gen, ec_ga, NULL); =20 g_assert(def); - g_assert(QTAILQ_EMPTY(&cpus)); + g_assert(QTAILQ_EMPTY_RCU(&cpus)); =20 /* TCG emulates some features that can usually not be enabled with * the emulated machine generation. Make sure they can be enabled --=20 2.17.1