From nobody Thu Nov 28 23:56:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1717105537; cv=none; d=zohomail.com; s=zohoarc; b=e0VLpNRZf3yvGjmPiomaLOBrLgPNs9fMzf4vLRf5sYyxP2ee+6TAoe7gQ91qpp++G6XkN/HCTaUg1WhC4K4ABvxl9L533ifoP70G0j6aOE8TLoXuIZZIePFpH4S07/2ehYFvNrrptQQgEo5GUSeg+y4YQVBFTmKPdbq3+VqlUZE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1717105537; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fPCSTF6BhpGl/BOGWj8t6sMDRxBkBVgL05/HycPv784=; b=WwTQ6U+dqA+SObkNmYtJaU9w/bb0Qs0h6JDKECwvnobmFITkR5PTbGERdjjndVkEJ7R9UjG8PTNbOD9QGlDWbHXteSUS8eRvmKwZAmShwsBMQqHJwCDW0WID8jb3+TuOHNCp8izIMhg3fyleM78MPzwbGyh0fEW/pDLN6eixBQ8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1717105537862129.42435541968348; Thu, 30 May 2024 14:45:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sClfz-0005da-90; Thu, 30 May 2024 15:43:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sClfw-0005bo-0J for qemu-devel@nongnu.org; Thu, 30 May 2024 15:43:00 -0400 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sClft-0006HX-1a for qemu-devel@nongnu.org; Thu, 30 May 2024 15:42:59 -0400 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-57a1fe6392eso1177179a12.0 for ; Thu, 30 May 2024 12:42:55 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a67eab85c8fsm7754766b.183.2024.05.30.12.42.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 12:42:53 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 16D015F8E9; Thu, 30 May 2024 20:42:51 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717098174; x=1717702974; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fPCSTF6BhpGl/BOGWj8t6sMDRxBkBVgL05/HycPv784=; b=Cpr15LOX3D6CRbGYnptBxBZx07KYsY8FhPeQXNXTvuJAedeA5iL8KqBe/gYmn9P0Ra GywApjV6hl5szGirleSwljmgAdf/PUm6aO1wN8HQaP8+yZqTCB8Hnyfus/H5vS5OxSAT KHRJVywwd13mq0H8endRpFu8Xs9gc0QhBXi/N8SgSmgNKwLtzJhZRVueDoZaBdLSEzrS /cCgj8Al4Wp5AvcMtSwtQXiV+VUFOJrdZcwmQEILL7fGuxNRQvpWo5JGIlFf6QGf65lP IWiHpCt2rMZX34vRt+/rBDdyzRNAA18JRLQTLmtZqUZHYuH5hYbp/GX3OjUGvel0Ol1K mL+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717098174; x=1717702974; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fPCSTF6BhpGl/BOGWj8t6sMDRxBkBVgL05/HycPv784=; b=tAPftSMohLf0yFYMQSTOme59OKsu6hlGJxLGM0tNPGBwz3DqhmsUOr4baOOB3r8v62 Is1SIAO5oEkjaKcvcWmTeZKs9/lcA0gSu2hKUZ9kPsvCu/PystzogdMYG9h3/JQPHkat sGXHTNHAhWUqrm872E39nOLeysb2Yel7wEQgizqT1t7KjS1+YD5UcR6OPTwF318lQH7z cJ9oWkl2U2TRaTh0JyQcUFLJ1lM4GCGAA6Ab5IFv0pLGgsqAuDwS+gz6wjZZchZEFdip 4cniQrjsMMKFEo9RyE9AmtVpu6/6I5a485eORYh+RmjuhrwD94kCavDMNXs4AluqiKs0 2rWA== X-Gm-Message-State: AOJu0Yz/5YF6/pBvDuZkmjZbA8WaN5FLsXizzTqMbQZhiWKVx7Pn9nrR w9S7/DD+FNeS9uMYrqSSKZaxG8qjhtLTREGW3AyY6suZPhJwoqXwsIl1p4ixwKQ= X-Google-Smtp-Source: AGHT+IE6R76QAmiPveib3Be1Nxuw357LnoAEk7dKHLZ8AdvRVSDfuYn+KOWdz8CdSrBFOQdmXMpLAA== X-Received: by 2002:a17:906:7714:b0:a67:da88:149a with SMTP id a640c23a62f3a-a67da881f76mr21672966b.55.1717098173884; Thu, 30 May 2024 12:42:53 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Marcel Apfelbaum , Cameron Esfahani , Alexandre Iooss , Yanan Wang , Paolo Bonzini , Eduardo Habkost , Sunil Muthuswamy , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mahmoud Mandour , Reinoud Zandijk , =?UTF-8?q?Alex=20Benn=C3=A9e?= , kvm@vger.kernel.org, Roman Bolshakov Subject: [PATCH 5/5] core/cpu-common: initialise plugin state before thread creation Date: Thu, 30 May 2024 20:42:50 +0100 Message-Id: <20240530194250.1801701-6-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240530194250.1801701-1-alex.bennee@linaro.org> References: <20240530194250.1801701-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52c; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x52c.google.com 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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1717105539783100003 Originally I tried to move where vCPU thread initialisation to later in realize. However pulling that thread (sic) got gnarly really quickly. It turns out some steps of CPU realization need values that can only be determined from the running vCPU thread. However having moved enough out of the thread creation we can now queue work before the thread starts (at least for TCG guests) and avoid the race between vcpu_init and other vcpu states a plugin might subscribe to. Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Pierrick Bouvier --- hw/core/cpu-common.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c index 6cfc01593a..bf1a7b8892 100644 --- a/hw/core/cpu-common.c +++ b/hw/core/cpu-common.c @@ -222,14 +222,6 @@ static void cpu_common_realizefn(DeviceState *dev, Err= or **errp) cpu_resume(cpu); } =20 - /* Plugin initialization must wait until the cpu start executing code = */ -#ifdef CONFIG_PLUGIN - if (tcg_enabled()) { - cpu->plugin_state =3D qemu_plugin_create_vcpu_state(); - async_run_on_cpu(cpu, qemu_plugin_vcpu_init__async, RUN_ON_CPU_NUL= L); - } -#endif - /* NOTE: latest generic point where the cpu is fully realized */ } =20 @@ -273,6 +265,18 @@ static void cpu_common_initfn(Object *obj) QTAILQ_INIT(&cpu->watchpoints); =20 cpu_exec_initfn(cpu); + + /* + * Plugin initialization must wait until the cpu start executing + * code, but we must queue this work before the threads are + * created to ensure we don't race. + */ +#ifdef CONFIG_PLUGIN + if (tcg_enabled()) { + cpu->plugin_state =3D qemu_plugin_create_vcpu_state(); + async_run_on_cpu(cpu, qemu_plugin_vcpu_init__async, RUN_ON_CPU_NUL= L); + } +#endif } =20 static void cpu_common_finalize(Object *obj) --=20 2.39.2