From: Miguel Luis <miguel.l...@oracle.com> [This patch is just for reference. It has problems as it does not takes care of the TranslationBlocks and their assigned regions during CPU unrealize]
When using TCG acceleration in a multi-threaded context each vCPU has its own thread registered in tcg_ctxs[] upon creation and tcg_cur_ctxs stores the current number of threads that got created. Although, the lack of a mechanism to unregister these threads is a problem when exercising vCPU hotplug/unplug due to the fact that tcg_cur_ctxs gets incremented everytime a vCPU gets hotplugged but never gets decremented everytime a vCPU gets unplugged, therefore breaking the assert stating tcg_cur_ctxs < tcg_max_ctxs after a certain amount of vCPU hotplugs. Suggested-by: Salil Mehta <salil.me...@huawei.com> [SM: Check Things To Do Section, https://lore.kernel.org/all/20200613213629.21984-1-salil.me...@huawei.com/] Signed-off-by: Miguel Luis <miguel.l...@oracle.com> --- accel/tcg/tcg-accel-ops-mttcg.c | 1 + include/tcg/tcg.h | 1 + tcg/tcg.c | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/accel/tcg/tcg-accel-ops-mttcg.c b/accel/tcg/tcg-accel-ops-mttcg.c index b276262007..5cf9747ef2 100644 --- a/accel/tcg/tcg-accel-ops-mttcg.c +++ b/accel/tcg/tcg-accel-ops-mttcg.c @@ -127,6 +127,7 @@ static void *mttcg_cpu_thread_fn(void *arg) qemu_mutex_unlock_iothread(); rcu_remove_force_rcu_notifier(&force_rcu.notifier); rcu_unregister_thread(); + tcg_unregister_thread(); return NULL; } diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index 0875971719..6c1cd2a618 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -785,6 +785,7 @@ static inline void *tcg_malloc(int size) void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus); void tcg_register_thread(void); +void tcg_unregister_thread(void); void tcg_prologue_init(TCGContext *s); void tcg_func_start(TCGContext *s); diff --git a/tcg/tcg.c b/tcg/tcg.c index ddfe9a96cb..6760f40823 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -742,6 +742,14 @@ static void alloc_tcg_plugin_context(TCGContext *s) #endif } +static void free_tcg_plugin_context(TCGContext *s) +{ +#ifdef CONFIG_PLUGIN + g_ptr_array_unref(s->plugin_tb->insns); + g_free(s->plugin_tb); +#endif +} + /* * All TCG threads except the parent (i.e. the one that called tcg_context_init * and registered the target's TCG globals) must register with this function @@ -791,6 +799,21 @@ void tcg_register_thread(void) tcg_ctx = s; } + +void tcg_unregister_thread(void) +{ + TCGContext *s = tcg_ctx; + unsigned int n; + + /* Unclaim an entry in tcg_ctxs */ + n = qatomic_fetch_dec(&tcg_cur_ctxs); + g_assert(n > 1); + qatomic_store_release(&tcg_ctxs[n - 1], 0); + + free_tcg_plugin_context(s); + + g_free(s); +} #endif /* !CONFIG_USER_ONLY */ /* pool based memory allocation */ -- 2.34.1