qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Pierrick Bouvier <pierrick.bouvier@linaro.org>
To: "Alex Bennée" <alex.bennee@linaro.org>, qemu-devel@nongnu.org
Cc: "Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Cameron Esfahani" <dirty@apple.com>,
	"Alexandre Iooss" <erdnaxe@crans.org>,
	"Yanan Wang" <wangyanan55@huawei.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Sunil Muthuswamy" <sunilmut@microsoft.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Mahmoud Mandour" <ma.mandourr@gmail.com>,
	"Reinoud Zandijk" <reinoud@netbsd.org>,
	kvm@vger.kernel.org, "Roman Bolshakov" <rbolshakov@ddn.com>
Subject: Re: [PATCH 2/5] cpu: move Qemu[Thread|Cond] setup into common code
Date: Thu, 30 May 2024 15:29:41 -0700	[thread overview]
Message-ID: <2a20631b-ce2a-4079-87c6-f77c0ba589e3@linaro.org> (raw)
In-Reply-To: <20240530194250.1801701-3-alex.bennee@linaro.org>

On 5/30/24 12:42, Alex Bennée wrote:
> Aside from the round robin threads this is all common code. By
> moving the halt_cond setup we also no longer need hacks to work around
> the race between QOM object creation and thread creation.
> 
> It is a little ugly to free stuff up for the round robin thread but
> better it deal with its own specialises than making the other
> accelerators jump through hoops.
> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   include/hw/core/cpu.h             |  4 ++++
>   accel/dummy-cpus.c                |  3 ---
>   accel/hvf/hvf-accel-ops.c         |  4 ----
>   accel/kvm/kvm-accel-ops.c         |  3 ---
>   accel/tcg/tcg-accel-ops-mttcg.c   |  4 ----
>   accel/tcg/tcg-accel-ops-rr.c      | 14 +++++++-------
>   hw/core/cpu-common.c              |  5 +++++
>   target/i386/nvmm/nvmm-accel-ops.c |  3 ---
>   target/i386/whpx/whpx-accel-ops.c |  3 ---
>   9 files changed, 16 insertions(+), 27 deletions(-)
> 
> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
> index 35d345371b..a405119eda 100644
> --- a/include/hw/core/cpu.h
> +++ b/include/hw/core/cpu.h
> @@ -404,10 +404,14 @@ struct qemu_work_item;
>    * @tcg_cflags: Pre-computed cflags for this cpu.
>    * @nr_cores: Number of cores within this CPU package.
>    * @nr_threads: Number of threads within this CPU core.
> + * @thread: Host thread details, only live once @created is #true
> + * @sem: WIN32 only semaphore used only for qtest
> + * @thread_id: native thread id of vCPU, only live once @created is #true
>    * @running: #true if CPU is currently running (lockless).
>    * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
>    * valid under cpu_list_lock.
>    * @created: Indicates whether the CPU thread has been successfully created.
> + * @halt_cond: condition variable sleeping threads can wait on.
>    * @interrupt_request: Indicates a pending interrupt request.
>    * @halted: Nonzero if the CPU is in suspended state.
>    * @stop: Indicates a pending stop request.
> diff --git a/accel/dummy-cpus.c b/accel/dummy-cpus.c
> index 20519f1ea4..f32d8c8dc3 100644
> --- a/accel/dummy-cpus.c
> +++ b/accel/dummy-cpus.c
> @@ -68,9 +68,6 @@ void dummy_start_vcpu_thread(CPUState *cpu)
>   {
>       char thread_name[VCPU_THREAD_NAME_SIZE];
>   
> -    cpu->thread = g_malloc0(sizeof(QemuThread));
> -    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
> -    qemu_cond_init(cpu->halt_cond);
>       snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
>                cpu->cpu_index);
>       qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,
> diff --git a/accel/hvf/hvf-accel-ops.c b/accel/hvf/hvf-accel-ops.c
> index 40d4187d9d..6f1e27ef46 100644
> --- a/accel/hvf/hvf-accel-ops.c
> +++ b/accel/hvf/hvf-accel-ops.c
> @@ -463,10 +463,6 @@ static void hvf_start_vcpu_thread(CPUState *cpu)
>        */
>       assert(hvf_enabled());
>   
> -    cpu->thread = g_malloc0(sizeof(QemuThread));
> -    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
> -    qemu_cond_init(cpu->halt_cond);
> -
>       snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
>                cpu->cpu_index);
>       qemu_thread_create(cpu->thread, thread_name, hvf_cpu_thread_fn,
> diff --git a/accel/kvm/kvm-accel-ops.c b/accel/kvm/kvm-accel-ops.c
> index 94c828ac8d..c239dfc87a 100644
> --- a/accel/kvm/kvm-accel-ops.c
> +++ b/accel/kvm/kvm-accel-ops.c
> @@ -66,9 +66,6 @@ static void kvm_start_vcpu_thread(CPUState *cpu)
>   {
>       char thread_name[VCPU_THREAD_NAME_SIZE];
>   
> -    cpu->thread = g_malloc0(sizeof(QemuThread));
> -    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
> -    qemu_cond_init(cpu->halt_cond);
>       snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
>                cpu->cpu_index);
>       qemu_thread_create(cpu->thread, thread_name, kvm_vcpu_thread_fn,
> diff --git a/accel/tcg/tcg-accel-ops-mttcg.c b/accel/tcg/tcg-accel-ops-mttcg.c
> index c552b45b8e..49814ec4af 100644
> --- a/accel/tcg/tcg-accel-ops-mttcg.c
> +++ b/accel/tcg/tcg-accel-ops-mttcg.c
> @@ -137,10 +137,6 @@ void mttcg_start_vcpu_thread(CPUState *cpu)
>       g_assert(tcg_enabled());
>       tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);
>   
> -    cpu->thread = g_new0(QemuThread, 1);
> -    cpu->halt_cond = g_malloc0(sizeof(QemuCond));
> -    qemu_cond_init(cpu->halt_cond);
> -
>       /* create a thread per vCPU with TCG (MTTCG) */
>       snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
>                cpu->cpu_index);
> diff --git a/accel/tcg/tcg-accel-ops-rr.c b/accel/tcg/tcg-accel-ops-rr.c
> index 894e73e52c..84c36c1450 100644
> --- a/accel/tcg/tcg-accel-ops-rr.c
> +++ b/accel/tcg/tcg-accel-ops-rr.c
> @@ -317,22 +317,22 @@ void rr_start_vcpu_thread(CPUState *cpu)
>       tcg_cpu_init_cflags(cpu, false);
>   
>       if (!single_tcg_cpu_thread) {
> -        cpu->thread = g_new0(QemuThread, 1);
> -        cpu->halt_cond = g_new0(QemuCond, 1);
> -        qemu_cond_init(cpu->halt_cond);
> +        single_tcg_halt_cond = cpu->halt_cond;
> +        single_tcg_cpu_thread = cpu->thread;
>   
>           /* share a single thread for all cpus with TCG */
>           snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
>           qemu_thread_create(cpu->thread, thread_name,
>                              rr_cpu_thread_fn,
>                              cpu, QEMU_THREAD_JOINABLE);
> -
> -        single_tcg_halt_cond = cpu->halt_cond;
> -        single_tcg_cpu_thread = cpu->thread;
>       } else {
> -        /* we share the thread */
> +        /* we share the thread, dump spare data */
> +        g_free(cpu->thread);
> +        qemu_cond_destroy(cpu->halt_cond);
>           cpu->thread = single_tcg_cpu_thread;
>           cpu->halt_cond = single_tcg_halt_cond;
> +
> +        /* copy the stuff done at start of rr_cpu_thread_fn */
>           cpu->thread_id = first_cpu->thread_id;
>           cpu->neg.can_do_io = 1;
>           cpu->created = true;
> diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
> index 0f0a247f56..6cfc01593a 100644
> --- a/hw/core/cpu-common.c
> +++ b/hw/core/cpu-common.c
> @@ -261,6 +261,11 @@ static void cpu_common_initfn(Object *obj)
>       cpu->nr_threads = 1;
>       cpu->cflags_next_tb = -1;
>   
> +    /* allocate storage for thread info, initialise condition variables */
> +    cpu->thread = g_new0(QemuThread, 1);
> +    cpu->halt_cond = g_new0(QemuCond, 1);
> +    qemu_cond_init(cpu->halt_cond);
> +
>       qemu_mutex_init(&cpu->work_mutex);
>       qemu_lockcnt_init(&cpu->in_ioctl_lock);
>       QSIMPLEQ_INIT(&cpu->work_list);
> diff --git a/target/i386/nvmm/nvmm-accel-ops.c b/target/i386/nvmm/nvmm-accel-ops.c
> index 6b2bfd9b9c..0ba31201e2 100644
> --- a/target/i386/nvmm/nvmm-accel-ops.c
> +++ b/target/i386/nvmm/nvmm-accel-ops.c
> @@ -64,9 +64,6 @@ static void nvmm_start_vcpu_thread(CPUState *cpu)
>   {
>       char thread_name[VCPU_THREAD_NAME_SIZE];
>   
> -    cpu->thread = g_new0(QemuThread, 1);
> -    cpu->halt_cond = g_new0(QemuCond, 1);
> -    qemu_cond_init(cpu->halt_cond);
>       snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/NVMM",
>                cpu->cpu_index);
>       qemu_thread_create(cpu->thread, thread_name, qemu_nvmm_cpu_thread_fn,
> diff --git a/target/i386/whpx/whpx-accel-ops.c b/target/i386/whpx/whpx-accel-ops.c
> index 189ae0f140..1a2b4e1c43 100644
> --- a/target/i386/whpx/whpx-accel-ops.c
> +++ b/target/i386/whpx/whpx-accel-ops.c
> @@ -64,9 +64,6 @@ static void whpx_start_vcpu_thread(CPUState *cpu)
>   {
>       char thread_name[VCPU_THREAD_NAME_SIZE];
>   
> -    cpu->thread = g_new0(QemuThread, 1);
> -    cpu->halt_cond = g_new0(QemuCond, 1);
> -    qemu_cond_init(cpu->halt_cond);
>       snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
>                cpu->cpu_index);
>       qemu_thread_create(cpu->thread, thread_name, whpx_cpu_thread_fn,

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

  reply	other threads:[~2024-05-30 22:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-30 19:42 [PATCH 0/5] cpus: a few tweaks to CPU realization Alex Bennée
2024-05-30 19:42 ` [PATCH 1/5] hw/core: expand on the alignment of CPUState Alex Bennée
2024-05-30 22:28   ` Pierrick Bouvier
2024-06-03 11:32   ` Philippe Mathieu-Daudé
2024-05-30 19:42 ` [PATCH 2/5] cpu: move Qemu[Thread|Cond] setup into common code Alex Bennée
2024-05-30 22:29   ` Pierrick Bouvier [this message]
2024-05-31 13:46     ` Reinoud Zandijk
2024-06-03 11:28   ` Philippe Mathieu-Daudé
2024-05-30 19:42 ` [PATCH 3/5] cpu-target: don't set cpu->thread_id to bogus value Alex Bennée
2024-05-30 22:29   ` Pierrick Bouvier
2024-06-03 11:29   ` Philippe Mathieu-Daudé
2024-05-30 19:42 ` [PATCH 4/5] plugins: remove special casing for cpu->realized Alex Bennée
2024-05-30 22:30   ` Pierrick Bouvier
2024-06-03 11:31   ` Philippe Mathieu-Daudé
2024-06-03 14:11     ` Philippe Mathieu-Daudé
2024-06-03 14:19     ` Alex Bennée
2024-05-30 19:42 ` [PATCH 5/5] core/cpu-common: initialise plugin state before thread creation Alex Bennée
2024-05-30 22:31   ` Pierrick Bouvier
2024-05-31  8:59     ` Alex Bennée
2024-05-31  7:26   ` Philippe Mathieu-Daudé
2024-05-31  8:47     ` Alex Bennée
2024-06-03 14:10       ` Philippe Mathieu-Daudé
2024-06-03 12:47 ` [PATCH 0/5] cpus: a few tweaks to CPU realization Philippe Mathieu-Daudé
2024-06-03 14:11   ` Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2a20631b-ce2a-4079-87c6-f77c0ba589e3@linaro.org \
    --to=pierrick.bouvier@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=dirty@apple.com \
    --cc=eduardo@habkost.net \
    --cc=erdnaxe@crans.org \
    --cc=kvm@vger.kernel.org \
    --cc=ma.mandourr@gmail.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rbolshakov@ddn.com \
    --cc=reinoud@netbsd.org \
    --cc=richard.henderson@linaro.org \
    --cc=sunilmut@microsoft.com \
    --cc=wangyanan55@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).