From patchwork Mon Oct 31 22:01:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 620259 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp3127859pvb; Mon, 31 Oct 2022 15:03:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7bSErEVV3Rz2NDNGa8YqtfdHiYMqIzKhuepV240mb//znEdYC+Wu9kV2MdzloAyIgyAeXh X-Received: by 2002:ac8:4e17:0:b0:3a5:318a:2625 with SMTP id c23-20020ac84e17000000b003a5318a2625mr2387633qtw.268.1667253781680; Mon, 31 Oct 2022 15:03:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667253781; cv=none; d=google.com; s=arc-20160816; b=ZnF3KPb7wAK/ADwG5sRkTKNsOq+EgzXo1JBkGiAQYPueRhMvEN/Pq8/J8qxGSOZaVn UlcRkiZkVm6yEx9XFZMbi5BiUB77n6wfQfMVrL8JCuzsU1NrOY88sZryCqmd/2zDDrJG ueAOFa0I10VtB5uBAyTkpKJxl57EgAdy6r6/0eWQOD4ape2WInp7AtzksbVQmI5wJCu4 498Z4NKQNV2G0wp9+NlXt1SJgxW6SwKZeUrOUAQFq9eNJTNORPLCbtKNnnsi5A2B+YjK rRRdjq1U3Tx4pz9c/a0aLwYOB6/qSQ9k+V2vy2O0UZhgGrqoib2BWYk9HwnpBJYc4+BG r0ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender: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:cc:to :from:dkim-signature; bh=h1dVA8sOI3Rlc4k5D7aWCLPuYTG55wXodxs0RRcAfpY=; b=aZjFTZw9je3ohA3ZfH3xZNsniq4EHAMPGH3c3d1XuCe61DxRRm2rA2feh2zRLaZH/F mbzYmIlDuT9nIsjclRhjk3Kc6oVTMWUlpJ4Mc191wOWUtFlfe5orWYGFl07bvOji/DZ6 LFzNwZ+NBmwaS16HNG7JI2B7qqzEqMEFq+HYhMKyDHCeVMfTdMsUC0mF/7x610Zr6xAX 8Io5U0toacT+/fZwNMjmceMziN1+xq5NNwKtreAlpMGgKR2YG3KlM5dPsenesm/uMhkR ++09SquSOsMPvgdV8b7JOAkIjYuLBkDEerszGzBvMXJLgJixfr/7fZgmVhJ2V+1NCDr4 QYOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="X/iDyu81"; spf=pass (google.com: domain of qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org"; dmarc=pass (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 q4-20020a056214194400b004bb59b842aasi5224330qvk.312.2022.10.31.15.03.01 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 31 Oct 2022 15:03:01 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+peter.griffin=linaro.org+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=pass header.i=@linaro.org header.s=google header.b="X/iDyu81"; spf=pass (google.com: domain of qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opcrF-0005wI-L3; Mon, 31 Oct 2022 18:02:13 -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 1opcqg-0005I8-GV for qemu-devel@nongnu.org; Mon, 31 Oct 2022 18:01:42 -0400 Received: from mail-il1-x136.google.com ([2607:f8b0:4864:20::136]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1opcqe-0001D8-PM for qemu-devel@nongnu.org; Mon, 31 Oct 2022 18:01:38 -0400 Received: by mail-il1-x136.google.com with SMTP id x16so6910154ilm.5 for ; Mon, 31 Oct 2022 15:01:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=h1dVA8sOI3Rlc4k5D7aWCLPuYTG55wXodxs0RRcAfpY=; b=X/iDyu81C6GoEteaC/cPycLSD1WjUnfh8OJ2d9LMsZIL0x4PB5EsFSKqWMIV1PQlei w/9xENa6o0w2rfPYuirjps0oN2Jd/EOao+dBStHoEw87X8eFaswWhvvNLP1bomnhDJim t77gGHVVhvZmjixG7zD6rX29XgbJQIx/4xYXVy7rQQB2vy97uga6ZY6MXsws3erPZNXW hkU8AjcVwo5QRvrd7pFcLN+Qsbyry+eBFWqxvCLgkHcFcAQvImXfymAqK1WJFOsJYg1X s1sW+HjyFmxYWJ7lRFwp5p2k0w1LyzbusHTxawshxCvd8ic+Yaj1/3kH7k4s0ezYypQM jZ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=h1dVA8sOI3Rlc4k5D7aWCLPuYTG55wXodxs0RRcAfpY=; b=buRC46F5P/RtywUK+XTAFBjjAO64W4d5+UWxKwKqWjYSNORDBOJ+mPDpAsx+knOVjb K8KH7HX9gdFksqZeKz4/Ilz00jDwomBRYA9GRef90DdV3tJoYgFrIsm1j5ZaCyju8+o/ JyQQTf771OpUEXwjY7nlOb0Fz2PmfbOKqw9XBEcoS/Bg4MH6PYAUeyr/1gXQVpA4cPKL 5rn9t1MYYdZ0UH5KPSzsxvypOGPxTjZQEkYtFwz/Rv/a1AOmIPpQ9drEuQPIcmojEeWf uZfm564wc6HO1cD4khcJJrhoV6lj6SPfHjxD1lkZsY2lnoCSZkREjuLK4zooiohx73wT fkAQ== X-Gm-Message-State: ACrzQf1rqn9lL0bo/MK3OTIcM2EWNbHVlE0LR10j4BWtDkVnTnfBfhK8 NeuuR9uExDwDyzbCzZddZxFax30R/Idh4Q== X-Received: by 2002:a05:6e02:1607:b0:300:60df:7cc7 with SMTP id t7-20020a056e02160700b0030060df7cc7mr8253596ilu.306.1667253695662; Mon, 31 Oct 2022 15:01:35 -0700 (PDT) Received: from stoup.. ([172.58.176.58]) by smtp.gmail.com with ESMTPSA id s7-20020a02c507000000b00363ec4dcaacsm3203039jam.22.2022.10.31.15.01.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 15:01:35 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: stefanha@gmail.com, Ilya Leoshkevich Subject: [PULL v2 12/13] accel/tcg: Complete cpu initialization before registration Date: Tue, 1 Nov 2022 09:01:12 +1100 Message-Id: <20221031220113.414796-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221031220113.414796-1-richard.henderson@linaro.org> References: <20221031215723.414467-1-richard.henderson@linaro.org> <20221031220113.414796-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::136; envelope-from=richard.henderson@linaro.org; helo=mail-il1-x136.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 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: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org Delay cpu_list_add until realize is complete, so that cross-cpu interaction does not happen with incomplete cpu state. For this, we must delay plugin initialization out of tcg_exec_realizefn, because no cpu_index has been assigned. Fixes a problem with cross-cpu jump cache flushing, when the jump cache has not yet been allocated. Fixes: a976a99a2975 ("include/hw/core: Create struct CPUJumpCache") Acked-by: Ilya Leoshkevich Reported-by: Ilya Leoshkevich Signed-off-by: Richard Henderson --- accel/tcg/cpu-exec.c | 8 +++++--- accel/tcg/translate-all.c | 16 +++++++--------- cpu.c | 10 +++++++++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 82b06c1824..356fe348de 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -1052,23 +1052,25 @@ void tcg_exec_realizefn(CPUState *cpu, Error **errp) cc->tcg_ops->initialize(); tcg_target_initialized = true; } - tlb_init(cpu); - qemu_plugin_vcpu_init_hook(cpu); + cpu->tb_jmp_cache = g_new0(CPUJumpCache, 1); + tlb_init(cpu); #ifndef CONFIG_USER_ONLY tcg_iommu_init_notifier_list(cpu); #endif /* !CONFIG_USER_ONLY */ + /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */ } /* undo the initializations in reverse order */ void tcg_exec_unrealizefn(CPUState *cpu) { + qemu_plugin_vcpu_exit_hook(cpu); #ifndef CONFIG_USER_ONLY tcg_iommu_free_notifier_list(cpu); #endif /* !CONFIG_USER_ONLY */ - qemu_plugin_vcpu_exit_hook(cpu); tlb_destroy(cpu); + g_free(cpu->tb_jmp_cache); } #ifndef CONFIG_USER_ONLY diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 0089578f8f..921944a5ab 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1580,15 +1580,13 @@ void tcg_flush_jmp_cache(CPUState *cpu) { CPUJumpCache *jc = cpu->tb_jmp_cache; - if (likely(jc)) { - for (int i = 0; i < TB_JMP_CACHE_SIZE; i++) { - qatomic_set(&jc->array[i].tb, NULL); - } - } else { - /* This should happen once during realize, and thus never race. */ - jc = g_new0(CPUJumpCache, 1); - jc = qatomic_xchg(&cpu->tb_jmp_cache, jc); - assert(jc == NULL); + /* During early initialization, the cache may not yet be allocated. */ + if (unlikely(jc == NULL)) { + return; + } + + for (int i = 0; i < TB_JMP_CACHE_SIZE; i++) { + qatomic_set(&jc->array[i].tb, NULL); } } diff --git a/cpu.c b/cpu.c index 2a09b05205..4a7d865427 100644 --- a/cpu.c +++ b/cpu.c @@ -134,15 +134,23 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp) /* cache the cpu class for the hotpath */ cpu->cc = CPU_GET_CLASS(cpu); - cpu_list_add(cpu); if (!accel_cpu_realizefn(cpu, errp)) { return; } + /* NB: errp parameter is unused currently */ if (tcg_enabled()) { tcg_exec_realizefn(cpu, errp); } + /* Wait until cpu initialization complete before exposing cpu. */ + cpu_list_add(cpu); + + /* Plugin initialization must wait until cpu_index assigned. */ + if (tcg_enabled()) { + qemu_plugin_vcpu_init_hook(cpu); + } + #ifdef CONFIG_USER_ONLY assert(qdev_get_vmsd(DEVICE(cpu)) == NULL || qdev_get_vmsd(DEVICE(cpu))->unmigratable);