Received: by 2002:a17:504:1f11:b0:1be9:327d:8ee3 with SMTP id z17csp596989njl; Tue, 8 Oct 2024 20:28:07 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXEX7T9trNsPYd2Vl79MoHDn13BEpD8qsnwIhLOELcQwT+F8kjJLRQsFQatNQoovXUfynP0ELMzYMYHAg==@linaro.org X-Google-Smtp-Source: AGHT+IEpZp1G9g6nBifj2mabFg0t2M5+OH3VIFjPVmV3xAQ6TUQTuNHuKji2UmK/BFxmT0s9kFnw X-Received: by 2002:a05:6000:1bca:b0:374:c64e:3ff1 with SMTP id ffacd0b85a97d-37d29306cecmr3161166f8f.17.1728444487201; Tue, 08 Oct 2024 20:28:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1728444487; cv=none; d=google.com; s=arc-20240605; b=gjllO0qch/4Bo39+6EZpjre8lBNAjpZcElKDI5g9vYouqnUPftMRr1HdEnkYxaboxe 7APlw37Ls0rv/ksfI8AwnlNBT7vgzxdh+dOU67q8tB2GdYskVF4I4NxqQEBFrUIQBYBg OZBSOCY6rvZKLcnGZ61s7qwr/LqUHK2mAR+4zNAdedNuA8tNK3y9Tvyhr+7IzI1qwVSx d83bh5/U/QR0avjeWYwCQKiS9ZLwedHLpjTjM1TYhmX+2qjo0cCnGqbsFJXyds4+RMcB wYWnKjNM1e2Kl8HXUEeFDnUWKlrm4tiJ2SDlemaVuv2I9AHurzykltdTftAZFCIDU9Is MSvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=+rAMZa0mwMX2J0hw4kTVyh6LzQdk2qhhFgWuT8d10R0=; fh=4Dgx8jc1gJCFDIGNl8iT3gucG+cuHYR9XmJ7yDXtNzA=; b=Qsc+KQUfO4PIficJ55j7FQIVkJ3Bwh6F67cJBSpDFw8vYqQJCd6/rBL1q9MZbuQ/jQ mdgulLbtpVRjr9maleBQhN97hEGWmpXp0Bsp9OWM6teUiocgujZFzykM4QZI4xe2axf3 1gBXKhLETYVLWAqY5ypBRD2miUxSEZuGatdKvtNLhVbZSj44Y5uT9Qai/+dYBl2RoXSe g0ISkniIIMtAU3c+QrNZYyZxDTyhH7p2MAN9K+BKthyBrFI4t0Zm+OFY6dtMlFD9ve6E q5yVZqAlxLwdfwO9CNhR5KR/FAwWYHgtgyGWl/QYYNYwhTShHYoB8/4y72Q7Z8F086QK RAwg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of salil.mehta@huawei.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=salil.mehta@huawei.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from frasgout.his.huawei.com (frasgout.his.huawei.com. [185.176.79.56]) by mx.google.com with ESMTPS id ffacd0b85a97d-37d3e8d5a33si80635f8f.351.2024.10.08.20.28.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Oct 2024 20:28:07 -0700 (PDT) Received-SPF: pass (google.com: domain of salil.mehta@huawei.com designates 185.176.79.56 as permitted sender) client-ip=185.176.79.56; Authentication-Results: mx.google.com; spf=pass (google.com: domain of salil.mehta@huawei.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=salil.mehta@huawei.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.18.186.231]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4XNdX33Mjvz6LD6x; Wed, 9 Oct 2024 11:23:47 +0800 (CST) Received: from frapeml500007.china.huawei.com (unknown [7.182.85.172]) by mail.maildlp.com (Postfix) with ESMTPS id 314E8140A34; Wed, 9 Oct 2024 11:28:06 +0800 (CST) Received: from 00293818-MRGF.huawei.com (10.126.173.89) by frapeml500007.china.huawei.com (7.182.85.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Wed, 9 Oct 2024 05:27:47 +0200 From: Salil Mehta To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC V4 26/33] tcg: Update tcg_register_thread() leg to handle region alloc for hotplugged vCPU Date: Wed, 9 Oct 2024 04:18:08 +0100 Message-ID: <20241009031815.250096-27-salil.mehta@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241009031815.250096-1-salil.mehta@huawei.com> References: <20241009031815.250096-1-salil.mehta@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.126.173.89] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To frapeml500007.china.huawei.com (7.182.85.172) X-TUID: 2f883c+OiVjJ The TCG code cache consists of multiple regions shared among vCPUs in multi-threaded TCG mode. For cold-plugged vCPUs, these regions are sized and allocated during initialization in the `tcg_register_thread()` function when the vCPUs are realized. Later, these regions must be reallocated for hot-plugged vCPUs as well. If region allocation fails for hot-plugged vCPUs—due to the code cache being under stress—the TCG code cache must be flushed to create space for the newly hot-plugged vCPU. The only safe way to perform `tb_flush()` is to execute it synchronously within the `cpu_exec()` loop. Reported-by: Miguel Luis Signed-off-by: Miguel Luis Signed-off-by: Salil Mehta --- accel/tcg/tcg-accel-ops-mttcg.c | 2 +- accel/tcg/tcg-accel-ops-rr.c | 2 +- include/tcg/startup.h | 6 ++++++ include/tcg/tcg.h | 1 + tcg/region.c | 14 ++++++++++++++ tcg/tcg.c | 13 ++++++++++++- 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/accel/tcg/tcg-accel-ops-mttcg.c b/accel/tcg/tcg-accel-ops-mttcg.c index 49814ec4af..ab2f79d2c7 100644 --- a/accel/tcg/tcg-accel-ops-mttcg.c +++ b/accel/tcg/tcg-accel-ops-mttcg.c @@ -74,7 +74,7 @@ static void *mttcg_cpu_thread_fn(void *arg) force_rcu.notifier.notify = mttcg_force_rcu; force_rcu.cpu = cpu; rcu_add_force_rcu_notifier(&force_rcu.notifier); - tcg_register_thread(); + tcg_register_thread(cpu); bql_lock(); qemu_thread_get_self(cpu->thread); diff --git a/accel/tcg/tcg-accel-ops-rr.c b/accel/tcg/tcg-accel-ops-rr.c index 8ebadf8e9e..953231837c 100644 --- a/accel/tcg/tcg-accel-ops-rr.c +++ b/accel/tcg/tcg-accel-ops-rr.c @@ -186,7 +186,7 @@ static void *rr_cpu_thread_fn(void *arg) rcu_register_thread(); force_rcu.notify = rr_force_rcu; rcu_add_force_rcu_notifier(&force_rcu); - tcg_register_thread(); + tcg_register_thread(cpu); bql_lock(); qemu_thread_get_self(cpu->thread); diff --git a/include/tcg/startup.h b/include/tcg/startup.h index f71305765c..a565071516 100644 --- a/include/tcg/startup.h +++ b/include/tcg/startup.h @@ -25,6 +25,8 @@ #ifndef TCG_STARTUP_H #define TCG_STARTUP_H +#include "hw/core/cpu.h" + /** * tcg_init: Initialize the TCG runtime * @tb_size: translation buffer size @@ -43,7 +45,11 @@ void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus); * accelerator's init_machine() method) must register with this * function before initiating translation. */ +#ifdef CONFIG_USER_ONLY void tcg_register_thread(void); +#else +void tcg_register_thread(CPUState *cpu); +#endif /** * tcg_prologue_init(): Generate the code for the TCG prologue diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index 21d5884741..e3328cc600 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -518,6 +518,7 @@ struct TCGContext { /* Track which vCPU triggers events */ CPUState *cpu; /* *_trans */ + bool tbflush_pend; /* TB flush pending due to vCPU hotplug */ /* These structures are private to tcg-target.c.inc. */ #ifdef TCG_TARGET_NEED_LDST_LABELS diff --git a/tcg/region.c b/tcg/region.c index 478ec051c4..9007bfd71e 100644 --- a/tcg/region.c +++ b/tcg/region.c @@ -393,6 +393,20 @@ bool tcg_region_alloc(TCGContext *s) static void tcg_region_initial_alloc__locked(TCGContext *s) { bool err = tcg_region_alloc__locked(s); + + /* + * Hotplugged vCPUs may initially fail to find even a single available + * region. This could be due to the TB cache being under stress from the + * existing vCPUs. To mitigate this, the TB cache should be flushed. + * Therefore, the region allocation failure should be ignored, and a flag + * set to mark `tb_flush()` as pending. The flush will be performed later, + * synchronously in the context of `cpu_exec_loop()`/`tb_gen_code()`. + */ + if (err && s->cpu && DEVICE(s->cpu)->hotplugged) { + s->tbflush_pend = true; + return; + } + g_assert(!err); } diff --git a/tcg/tcg.c b/tcg/tcg.c index 34e3056380..5e9c6b2b4b 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -783,12 +783,13 @@ void tcg_register_thread(void) tcg_ctx = &tcg_init_ctx; } #else -void tcg_register_thread(void) +void tcg_register_thread(CPUState *cpu) { TCGContext *s = g_malloc(sizeof(*s)); unsigned int i, n; *s = tcg_init_ctx; + s->cpu = cpu; /* Relink mem_base. */ for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) { @@ -1388,6 +1389,16 @@ TranslationBlock *tcg_tb_alloc(TCGContext *s) TranslationBlock *tb; void *next; + /* + * The hotplugged vCPU's TCG context might not have any regions allocated. + * If this condition is detected, we should flush the TB cache to ensure + * that regions can be allocated for the newly hotplugged vCPU's TCGContext. + */ + if (s->tbflush_pend) { + s->tbflush_pend = false; + return NULL; + } + retry: tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align); next = (void *)ROUND_UP((uintptr_t)(tb + 1), align); -- 2.34.1