From nobody Wed Nov 5 14:27:33 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; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534873237502568.1296050871705; Tue, 21 Aug 2018 10:40:37 -0700 (PDT) Received: from localhost ([::1]:55130 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsAds-0003ay-7v for importer@patchew.org; Tue, 21 Aug 2018 13:40:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33707) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsA4n-0002IH-Rw for qemu-devel@nongnu.org; Tue, 21 Aug 2018 13:04:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fsA4W-0006p3-M9 for qemu-devel@nongnu.org; Tue, 21 Aug 2018 13:04:11 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]:37178) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fsA4V-0006dA-LA for qemu-devel@nongnu.org; Tue, 21 Aug 2018 13:04:00 -0400 Received: by mail-wr1-x436.google.com with SMTP id u12-v6so16462365wrr.4 for ; Tue, 21 Aug 2018 10:03:54 -0700 (PDT) Received: from 640k.lan (dynamic-adsl-78-12-184-244.clienti.tiscali.it. [78.12.184.244]) by smtp.gmail.com with ESMTPSA id v6-v6sm2608955wmc.43.2018.08.21.10.03.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 10:03:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=iCO6f70CmJd6SjSzlvjcM2rpPXkFri0ODaXNjWYVE+U=; b=jKafF5o7lgH3ZqbM7f877tHm2IiigBrgeu1e2FN2zytJMUVmQku8CPOHzCwYN5S5oU alLj66KzUxpCEr/HxIz1OdF3HVJ7fKl3W39imqTgK9bAau3uewBsNJmf1M5WCgkLJMS3 8oZoyoTUETtIsP5UXXpjEqlVT6dA4QSq1xTdZYDZWDXrAd/O5FJ2BRpZErq9U9D7Tp8r DxzDg7D68m6XkRQRA6sjphmkmGEQNOrymJWTaU0Or6KcdMszIt0f4cnpZkmKS749AGze gqyAr+rrf3ifJ+Vt6Vu88zFOc24a4wSfRBAAsbQtDoSHnTsvV2OsUuCKaJOw+7p6GgGA T+XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=iCO6f70CmJd6SjSzlvjcM2rpPXkFri0ODaXNjWYVE+U=; b=ujRN3lfK7bf8v6fVPY/SHYL1e7tJmUBV5nPED7B392jSWlnaw7lS/3/BU7D+n29RGR R6jF+9KKmVUlsLiJ+405xlx9Dh+NA2u4pEvUIg3QEoImRWz7w4W7Ldx8tUB4qmt/tC+5 wLiSAY1k7Hrei++g1d8OEF3d6+6J+I3bCDBkgZ3pBfywmqFiS4EatZAkuHAoyFpfZ61u jMnP/U93GekGF3Ff18tSTwgqMRHLq2JLUiZjKL5RCWnFkY36e5zgTNhU2LAuv32wFB2z VAbaGhTGk16S4liWgWgQ+DLtagOqtfqwZBy/JzPwW+j85m/RYV4+t5aqH7za5CfOwcFN Up1A== X-Gm-Message-State: APzg51Dbyj2c+EVFrLngurt+q+1cf/d+tVeX9erBDn4/nZFpQHr4odo9 3+QTQZJb8Kr+0fLWxAzQzsasGEIQ X-Google-Smtp-Source: ANB0VdbvT64dSVe6rmqRaMn6p+gXEfZzcdSvkq7JHjYFBXRrsV0tAuOq0Cf+fVuK0HKRbTs2xCOiXQ== X-Received: by 2002:a5d:4648:: with SMTP id j8-v6mr6220168wrs.52.1534871033579; Tue, 21 Aug 2018 10:03:53 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 21 Aug 2018 19:02:18 +0200 Message-Id: <1534870966-9287-47-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1534870966-9287-1-git-send-email-pbonzini@redhat.com> References: <1534870966-9287-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::436 Subject: [Qemu-devel] [PULL 46/74] 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: "Emilio G. Cota" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Emilio G. Cota" 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 Message-Id: <20180819091335.22863-12-cota@braap.org> Signed-off-by: Paolo Bonzini --- cpus-common.c | 4 ++-- cpus.c | 2 +- include/qom/cpu.h | 11 +++++------ 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/cpus-common.c b/cpus-common.c index 59f751e..98dd8c6 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 a5ea3ee..9161349 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/include/qom/cpu.h b/include/qom/cpu.h index ecf6ed5..dc130cd 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/linux-user/main.c b/linux-user/main.c index ea00dd9..923cbb7 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 202aa77..3c3c1ae 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8157,7 +8157,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 12e765b..265d25c 100644 --- a/target/s390x/cpu_models.c +++ b/target/s390x/cpu_models.c @@ -1096,7 +1096,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 1.8.3.1