Multithread TCG.
v7 cover Multithread TCG.
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branch multi_tcg_v7.
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
* current_cpu doesn't make sense in mttcg so a tcg_executing flag is added to the CPUState.
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the async_run_safe_work_on_cpu function introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (except tb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.
== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop the global_mutex as soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call to async_run_on_cpu. However tb_invalidate also writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence the async_run_safe_work_on_cpu is introduced to handle this case.
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores). For now this case is not correctly handled. The existing atomic patch will attempt to execute the 64 bit cmpxchg functionality in a non thread safe fashion. Our intention is to provide a new multi-thread ARM atomic patch for 64bit ARM on effective 32bit hosts.
This atomic instruction part has been tested with Alexander's atomic stress repo available here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html
The execution is a little slower than upstream probably because of the different VCPU fight for the mutex. Swaping arm_exclusive_lock from mutex to spin_lock reduce considerably the difference.
== Testing ==
A simple double dhrystone test in SMP 2 with vexpress-a15 in a linux guest show a good performance progression: it takes basically 18s upstream to complete vs 10s with MTTCG.
Testing image is available here:
https://cloud.greensocs.com/index.php/s/CfHSLzDH5pmTkW3
Then simply:
./configure --target-list=arm-softmmu
make -j8
./arm-softmmu/qemu-system-arm -M vexpress-a15 -smp 2 -kernel zImage -initrd rootfs.ext2 -dtb vexpress-v2p-ca15-tc1.dtb --nographic --append "console=ttyAMA0"
login: root
The dhrystone command is the last one in the history. "dhrystone 10000000 & dhrystone 10000000"
The atomic spinlock benchmark from Alexander shows that atomic basically work. Just follow the instruction here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html
== Known issues ==
* GDB stub:
GDB stub is not tested right now it will probably requires some changes to work.
* deadlock on exit:
When exiting QEMU Ctrl-C some VCPU's thread are not able to exit and continue execution.
http://git.greensocs.com/fkonrad/mttcg/issues/1
* memory_region_rom_device_set_romd from pflash01 just crashes the TCG code. Strangely this happen only with "-smp 4" and 2 in the DTB.
http://git.greensocs.com/fkonrad/mttcg/issues/2
Changes V6 -> V7:
* global_lock:
* Don't protect softmmu read/write helper as it's now done in adress_space_rw.
* tcg_exec_flag:
* Make the flag atomically test and set through an API.
* introduce async_safe_work:
* move qemu_cpu_kick_thread to avoid prototype declaration.
* use the work_mutex.
* async_work:
* protect it with a mutex (work_mutex) against concurent access.
* tb_lock:
* protect tcg_malloc_internal as well.
* signal the VCPU even of current_cpu is NULL.
* added PSCI patch.
* rebased on v2.4.0-rc0 (6169b60285fe1ff730d840a49527e721bfb30899).
Changes V5 -> V6:
* Introduce async_safe_work to do the tb_flush and some part of tb_invalidate.
* Introduce QemuSpin from Guillaume which allow a faster atomic instruction (6s to pass Alexander's atomic test instead of 30s before).
* Don't take tb_lock before tb_find_fast.
* Handle tb_flush with async_safe_work.
* Handle tb_invalidate with async_work and async_safe_work.
* Drop the tlb_flush_request mechanism and use async_work as well.
* Fix the wrong lenght in atomic patch.
* Fix the wrong return address for exception in atomic patch.
Alex Bennée (1):
target-arm/psci.c: wake up sleeping CPUs (MTTCG)
Guillaume Delbergue (1):
add support for spin lock on POSIX systems exclusively
KONRAD Frederic (17):
cpus: protect queued_work_* with work_mutex.
cpus: add tcg_exec_flag.
cpus: introduce async_run_safe_work_on_cpu.
replace spinlock by QemuMutex.
remove unused spinlock.
protect TBContext with tb_lock.
tcg: remove tcg_halt_cond global variable.
Drop global lock during TCG code execution
cpu: remove exit_request global.
tcg: switch on multithread.
Use atomic cmpxchg to atomically check the exclusive value in a STREX add a callback when tb_invalidate is called.
cpu: introduce tlb_flush*_all.
arm: use tlb_flush*_all
translate-all: introduces tb_flush_safe.
translate-all: (wip) use tb_flush_safe when we can't alloc more tb.
mttcg: signal the associated cpu anyway.
cpu-exec.c | 98 +++++++++------ cpus.c | 295 +++++++++++++++++++++++++------------------- cputlb.c | 81 ++++++++++++ include/exec/exec-all.h | 8 +- include/exec/spinlock.h | 49 -------- include/qemu/thread-posix.h | 4 + include/qemu/thread-win32.h | 4 + include/qemu/thread.h | 7 ++ include/qom/cpu.h | 57 +++++++++ linux-user/main.c | 6 +- qom/cpu.c | 20 +++ target-arm/cpu.c | 21 ++++ target-arm/cpu.h | 6 + target-arm/helper.c | 58 +++------ target-arm/helper.h | 4 + target-arm/op_helper.c | 128 ++++++++++++++++++- target-arm/psci.c | 2 + target-arm/translate.c | 101 +++------------ target-i386/mem_helper.c | 16 ++- target-i386/misc_helper.c | 27 +++- tcg/i386/tcg-target.c | 8 ++ tcg/tcg.h | 14 ++- translate-all.c | 217 +++++++++++++++++++++++++++----- util/qemu-thread-posix.c | 45 +++++++ util/qemu-thread-win32.c | 30 +++++ vl.c | 6 + 26 files changed, 934 insertions(+), 378 deletions(-) delete mode 100644 include/exec/spinlock.h
Signature · 1 line
1.9.0
RFC PATCH v7 1/19 cpus: protect queued_work_* with work_mutex.
From: KONRAD Frederic <[email protected]>
This protects queued_work_* used by async_run_on_cpu, run_on_cpu and flush_queued_work with a new lock (work_mutex) to prevent multiple (concurrent) access.
Changes V1 -> V2:
* Unlock the mutex while running the callback.
--- cpus.c | 11 +++++++++++ include/qom/cpu.h | 3 +++ qom/cpu.c | 1 + 3 files changed, 15 insertions(+)
cpus.c +11 -0
@@ -845,6 +845,8 @@ void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) wi.func = func; wi.data = data; wi.free = false; + +qemu_mutex_lock (&cpu->work_mutex);if (cpu->queued_work_first ==NULL ) { cpu->queued_work_first = &wi; }else {
@@ -853,6 +855,7 @@ void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) cpu->queued_work_last = &wi; wi.next =NULL ; wi.done = false; +qemu_mutex_unlock (&cpu->work_mutex);qemu_cpu_kick (cpu);while (!wi.done) {
@@ -876,6 +879,8 @@ void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) wi->func = func; wi->data = data; wi->free = true; + +qemu_mutex_lock (&cpu->work_mutex);if (cpu->queued_work_first ==NULL ) { cpu->queued_work_first = wi; }else {
@@ -884,6 +889,7 @@ void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) cpu->queued_work_last = wi; wi->next =NULL ; wi->done = false; +qemu_mutex_unlock (&cpu->work_mutex);qemu_cpu_kick (cpu); }
@@ -896,15 +902,20 @@ static void flush_queued_work(CPUState *cpu)return ; } +qemu_mutex_lock (&cpu->work_mutex);while ((wi = cpu->queued_work_first)) { cpu->queued_work_first = wi->next; +qemu_mutex_unlock (&cpu->work_mutex); wi->func (wi->data); +qemu_mutex_lock (&cpu->work_mutex); wi->done = true;if (wi->free) {g_free (wi); } } cpu->queued_work_last =NULL ; +qemu_mutex_unlock (&cpu->work_mutex); +qemu_cond_broadcast (&qemu_work_cond); }
include/qom/cpu.h +3 -0
@@ -242,6 +242,8 @@ struct kvm_run;* @mem_io_pc: Host Program Counter at which the memory was accessed. * @mem_io_vaddr: Target virtual address at which the memory was accessed. * @kvm_fd: vCPU file descriptor for KVM. +* @work_mutex: Lock to prevent multiple access to queued_work_*. +* @queued_work_first: First asynchronous work pending. * * State of one CPU core or thread. */
@@ -262,6 +264,7 @@ struct CPUState {uint32_t host_tid;bool running;struct QemuCond *halt_cond; +QemuMutex work_mutex;struct qemu_work_item *queued_work_first, *queued_work_last;bool thread_kicked;bool created;
qom/cpu.c +1 -0
@@ -316,6 +316,7 @@ static void cpu_common_initfn(Object *obj) cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;QTAILQ_INIT (&cpu->breakpoints);QTAILQ_INIT (&cpu->watchpoints); +qemu_mutex_init (&cpu->work_mutex); }static void cpu_common_finalize (Object *obj)
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 01/19] cpus: protect queued_work_* with work_mutex.
cpus.c static void flush_queued_work(CPUState *cpu) · line 911
Quoted 8 lines · On 10/08/2015 17:26, [email protected] wrote:
cpus.c +3
-0+qemu_mutex_lock (&cpu->work_mutex);while ((wi = cpu->queued_work_first)) { cpu->queued_work_first = wi->next; +qemu_mutex_unlock (&cpu->work_mutex); wi->func (wi->data); +qemu_mutex_lock (&cpu->work_mutex); wi->done = true;
This should be atomic_mb_set
Quoted 5 lines
if (wi->free) {
g_free(wi);
}
}
cpu->queued_work_last = NULL;... and I'm a bit afraid of leaving the state of the list inconsistent, so I'd move this after the cpu->queued_work_first assignment. Otherwise the patch looks good, I'm queuing it for 2.5.
Paolo
Quoted 2 lines
(quoted diff) +2
-0+ qemu_mutex_unlock(&cpu->work_mutex); +
Re: [Qemu-devel] [RFC PATCH V7 01/19] cpus: protect queued_work_* with work_mutex.
Quoted 11 lines · Paolo Bonzini
cpus.c static void flush_queued_work(CPUState *cpu) · line 911
Quoted 8 lines · On 10/08/2015 17:26, [email protected] wrote:
cpus.c +3
-0+qemu_mutex_lock (&cpu->work_mutex);while ((wi = cpu->queued_work_first)) { cpu->queued_work_first = wi->next; +qemu_mutex_unlock (&cpu->work_mutex); wi->func (wi->data); +qemu_mutex_lock (&cpu->work_mutex); wi->done = true;This should be
atomic_mb_set
Isn't that protected by the mutex? Or maybe it's used somewhere else?
Quoted 14 lines
Quoted 6 lines
if (wi->free) { g_free(wi); } } cpu->queued_work_last = NULL;... and I'm a bit afraid of leaving the state of the list inconsistent, so I'd move this after the
cpu->queued_work_firstassignment. Otherwise the patch looks good, I'm queuing it for 2.5.Paolo
Quoted 2 lines
(quoted diff) +2
-0+ qemu_mutex_unlock(&cpu->work_mutex); +
Re: [Qemu-devel] [RFC PATCH V7 01/19] cpus: protect queued_work_* with work_mutex.
Quoted 10 lines · Frederic Konrad
Quoted 9 lines · Paolo Bonzini
Quoted 7 lines · On 10/08/2015 17:26, [email protected] wrote:
+ qemu_mutex_lock(&cpu->work_mutex); while ((wi = cpu->queued_work_first)) { cpu->queued_work_first = wi->next;cpus.c +2
-0+qemu_mutex_unlock (&cpu->work_mutex); wi->func (wi->data); +qemu_mutex_lock (&cpu->work_mutex); wi->done = true;
Quoted 3 lines
Quoted 1 line
This should be
atomic_mb_setIsn't that protected by the mutex?
This use is not protected by the mutex:
@@ -853,6 +855,7 @@ void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
cpu->queued_work_last = &wi;
wi.next = NULL;
wi.done = false;+ qemu_mutex_unlock(&cpu->work_mutex);
qemu_cpu_kick(cpu);
while (!wi.done) {Paolo
Or maybe it's used somewhere else?
Quoted 15 lines
Quoted 14 lines
Quoted 6 lines
if (wi->free) { g_free(wi); } } cpu->queued_work_last = NULL;... and I'm a bit afraid of leaving the state of the list inconsistent, so I'd move this after the
cpu->queued_work_firstassignment. Otherwise the patch looks good, I'm queuing it for 2.5.Paolo
Quoted 2 lines
(quoted diff) +2
-0+ qemu_mutex_unlock(&cpu->work_mutex); +
RFC PATCH v7 2/19 cpus: add tcg_exec_flag.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This flag indicates the state of the VCPU thread:
* 0 if the VCPU is allowed to execute code.
* 1 if the VCPU is currently executing code.
* -1 if the VCPU is not allowed to execute code.
This allows to atomically check and run safe work or check and continue the TCG execution.
Changes V2 -> V3:
* introduce a third state which allow or not the execution.
* atomically check and set the flag when starting or blocking the code execution.
Changes V1 -> V2:
* do both tcg_executing = 0 or 1 in cpu_exec().
--- cpu-exec.c | 5 +++++ include/qom/cpu.h | 32 ++++++++++++++++++++++++++++++++ qom/cpu.c | 19 +++++++++++++++++++ 3 files changed, 56 insertions(+)
cpu-exec.c +5 -0
@@ -371,6 +371,10 @@ int cpu_exec(CPUState *cpu) cpu->halted =0 ; } +if (!tcg_cpu_try_start_execution (cpu)) { + cpu->exit_request =1 ; +return 0 ; + } current_cpu = cpu;/* As long as current_cpu is null, up to the assignment just above,
@@ -583,5 +587,6 @@ int cpu_exec(CPUState *cpu)/* fail safe : never use current_cpu outside cpu_exec() */ current_cpu =NULL ; +tcg_cpu_allow_execution (cpu);return ret; }
include/qom/cpu.h +32 -0
@@ -226,6 +226,7 @@ struct kvm_run;* @stopped: Indicates the CPU has been artificially stopped. * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this * CPU and return to its top level loop. +* @tcg_exec_flag: See tcg_cpu_flag_* function. * @singlestep_enabled: Flags for single-stepping. * @icount_extra: Instructions until next timer event. * @icount_decr: Number of cycles left, with interrupt flag in high bit.
@@ -322,6 +323,8 @@ struct CPUState {(absolute value) offset as small as possible. This reduces code size, especially for hosts without large memory offsets. */ volatile sig_atomic_t tcg_exit_req; + +int tcg_exec_flag; };QTAILQ_HEAD (CPUTailQ, CPUState);
@@ -337,6 +340,35 @@ extern struct CPUTailQ cpus; DECLARE_TLS(CPUState *, current_cpu);#define current_cpu tls_var(current_cpu) + +/** +* tcg_cpu_try_block_execution +* @cpu: The CPU to block the execution +* +* Try to set the tcg_exec_flag to -1 saying the CPU can't execute code if the +* CPU is not executing code. +* Returns true if the cpu execution is blocked, false otherwise. +*/ +bool tcg_cpu_try_block_execution (CPUState *cpu); + +/** +* tcg_cpu_allow_execution +* @cpu: The CPU to allow the execution. +* +* Just reset the state of tcg_exec_flag, and allow the execution of some code. +*/ +void tcg_cpu_allow_execution (CPUState *cpu); + +/** +* tcg_cpu_try_start_execution +* @cpu: The CPU to start the execution. +* +* Just set the tcg_exec_flag to 1 saying the CPU is executing code if the CPU +* is allowed to run some code. +* Returns true if the cpu can execute, false otherwise. +*/ +bool tcg_cpu_try_start_execution (CPUState *cpu); +/** * cpu_paging_enabled: * @cpu: The CPU whose state is to be inspected.
qom/cpu.c +19 -0
@@ -26,6 +26,23 @@#include "qemu/error-report.h" #include "sysemu/sysemu.h" +bool tcg_cpu_try_block_execution (CPUState *cpu) +{ +return (atomic_cmpxchg (&cpu->tcg_exec_flag,0 ,-1 ) + || (cpu->tcg_exec_flag ==-1 )); +} + +void tcg_cpu_allow_execution (CPUState *cpu) +{ + cpu->tcg_exec_flag =0 ; +} + +bool tcg_cpu_try_start_execution (CPUState *cpu) +{ +return (atomic_cmpxchg (&cpu->tcg_exec_flag,0 ,1 ) + || (cpu->tcg_exec_flag ==1 )); +} +bool cpu_exists (int64_t id) {CPUState *cpu;
@@ -249,6 +266,8 @@ static void cpu_common_reset(CPUState *cpu) cpu->icount_decr.u32 =0 ; cpu->can_do_io =0 ; cpu->exception_index =-1 ; + +tcg_cpu_allow_execution (cpu);memset (cpu->tb_jmp_cache,0 ,TB_JMP_CACHE_SIZE *sizeof (void *)); }
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 02/19] cpus: add tcg_exec_flag.
cpu-exec.c int cpu_exec(CPUState *cpu) · line 590
Quoted 5 lines · On 10/08/2015 17:27, [email protected] wrote:
cpu-exec.c +1
-0@@ -583,5 +587,6 @@ int cpu_exec(CPUState *cpu)/* fail safe : never use current_cpu outside cpu_exec() */ current_cpu =NULL ; +tcg_cpu_allow_execution (cpu);
I don't think this is correct; safe_work_pending() is a much clearer test. I'll revert locally to the previous version to play more with the code.
Paolo
Quoted 3 lines
cpu-exec.c +0
-0return ret; }include/qom/cpu.h +0
-0
Re: [Qemu-devel] [RFC PATCH V7 02/19] cpus: add tcg_exec_flag.
Quoted 4 lines · Paolo Bonzini
Quoted 2 lines · On 10/08/2015 17:27, [email protected] wrote:
(quoted diff) +0
-0@@ -583,5 +587,6 @@ int cpu_exec(CPUState *cpu)
Quoted 8 lines
cpu-exec.c int cpu_exec(CPUState *cpu) · line 590
Quoted 3 lines
cpu-exec.c +1
-0/* fail safe : never use current_cpu outside cpu_exec() */ current_cpu =NULL ; +tcg_cpu_allow_execution (cpu);I don't think this is correct;
safe_work_pending()is a much clearer test. I'll revert locally to the previous version to play more with the code.Paolo
Yes definitely but we might have a race if we just use safe_work_pending().
Fred
Quoted 4 lines
Quoted 4 lines
cpu-exec.c +0
-0return ret; }include/qom/cpu.h +0
-0
Re: [Qemu-devel] [RFC PATCH V7 02/19] cpus: add tcg_exec_flag.
Quoted 11 lines · Frederic Konrad
Quoted 10 lines · Paolo Bonzini
cpu-exec.c int cpu_exec(CPUState *cpu) · line 589
Quoted 4 lines · On 10/08/2015 17:27, [email protected] wrote:
cpu-exec.c +1
-0@@ -583,5 +587,6 @@ int cpu_exec(CPUState *cpu)/* fail safe : never use current_cpu outside cpu_exec() */ current_cpu =NULL ; +tcg_cpu_allow_execution (cpu);I don't think this is correct;
safe_work_pending()is a much clearer test. I'll revert locally to the previous version to play more with the code.
Quoted 3 lines
Quoted 1 line
Paolo
Yes definitely but we might have a race if we just use
safe_work_pending().
The trick is to order the accesses correctly. For example, cpu_exec will check tcg_exit_req, then clear exit_request, then check queued_work_first. On the write side the order is the opposite:queued_work_first must be written first, then exit_request, then tcg_exit_req.
Here it is the same. safe_work_pending must be incremented first to prevent threads from entering cpu-exec.c; for those that are already in there you write queued_safe_work_first, then exit_request, then tcg_exit_req. Similarly safe_work_pending must be decremented last.
Paolo
RFC PATCH v7 3/19 cpus: introduce async_run_safe_work_on_cpu.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
We already had async_run_on_cpu but we need all VCPUs outside their execution loop to execute some tb_flush/invalidate task:
async_run_on_cpu_safe schedule a work on a VCPU but the work start when no more VCPUs are executing code.
When a safe work is pending cpu_has_work returns true, so cpu_exec returns and the VCPUs can't enters execution loop. cpu_thread_is_idle returns false so at the moment where all VCPUs are stop || stopped the safe work queue can be flushed.
Changes V3 -> V4:
* Use tcg_cpu_try_block_execution.
* Use a counter to know how many safe work are pending.
Changes V2 -> V3:
* Unlock the mutex while executing the callback.
Changes V1 -> V2:
* Move qemu_cpu_kick_thread to avoid prototype declaration.
* Use the work_mutex lock to protect the queued_safe_work_* structures.
--- cpu-exec.c | 5 ++ cpus.c | 149 +++++++++++++++++++++++++++++++++++++++--------------- include/qom/cpu.h | 24 ++++++++- 3 files changed, 137 insertions(+), 41 deletions(-)
cpu-exec.c +5 -0
@@ -363,6 +363,11 @@ int cpu_exec(CPUState *cpu)/* This must be volatile so it is not trashed by longjmp() */ volatile bool have_tb_lock = false; +if (async_safe_work_pending ()) { + cpu->exit_request =1 ; +return 0 ; + } +if (cpu->halted) {if (!cpu_has_work (cpu)) {return EXCP_HALTED ;
cpus.c +109 -40
185 lines not on this page · raw message
include/qom/cpu.h +23 -1
@@ -243,8 +243,9 @@ struct kvm_run;* @mem_io_pc: Host Program Counter at which the memory was accessed. * @mem_io_vaddr: Target virtual address at which the memory was accessed. * @kvm_fd: vCPU file descriptor for KVM. -* @work_mutex: Lock to prevent multiple access to queued_work_*. +* @work_mutex: Lock to prevent multiple access to queued_* qemu_work_item. * @queued_work_first: First asynchronous work pending. +* @queued_safe_work_first: First item of safe work pending. * * State of one CPU core or thread. */
@@ -267,6 +268,7 @@ struct CPUState {struct QemuCond *halt_cond;QemuMutex work_mutex;struct qemu_work_item *queued_work_first, *queued_work_last; +struct qemu_work_item *queued_safe_work_first, *queued_safe_work_last;bool thread_kicked;bool created;bool stop;
@@ -575,6 +577,26 @@ void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);void async_run_on_cpu (CPUState *cpu,void (*func)(void *data),void *data);/** +* async_run_safe_work_on_cpu: +* @cpu: The vCPU to run on. +* @func: The function to be executed. +* @data: Data to pass to the function. +* +* Schedules the function @func for execution on the vCPU @cpu asynchronously +* when all the VCPUs are outside their loop. +*/ +void async_run_safe_work_on_cpu (CPUState *cpu,void (*func)(void *data), +void *data); + +/** +* async_safe_work_pending: +* +* Check whether any safe work is pending on any VCPUs. +* Returns: @true if a safe work is pending, @false otherwise. +*/ +bool async_safe_work_pending (void ); + +/** * qemu_get_cpu: * @index: The CPUState@cpu_index value of the CPU to obtain. *
Signature · 1 line
1.9.0
RFC PATCH v7 4/19 replace spinlock by QemuMutex.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
spinlock is only used in two cases:
* cpu-exec.c: to protect TranslationBlock
* mem_helper.c: for lock helper in target-i386 (which seems broken).
It's a pthread_mutex_t in user-mode so better using QemuMutex directly in this case.
It allows as well to reuse tb_lock mutex of TBContext in case of multithread TCG.
--- cpu-exec.c | 15 +++++++++++---- include/exec/exec-all.h | 4 ++-- linux-user/main.c | 6 +++--- target-i386/mem_helper.c | 16 +++++++++++++--- tcg/i386/tcg-target.c | 8 ++++++++ 5 files changed, 37 insertions(+), 12 deletions(-)
cpu-exec.c +11 -4
@@ -361,7 +361,9 @@ int cpu_exec(CPUState *cpu)SyncClocks sc;/* This must be volatile so it is not trashed by longjmp() */ +#if defined(CONFIG_USER_ONLY )volatile bool have_tb_lock = false; +#endif if (async_safe_work_pending ()) { cpu->exit_request =1 ;
@@ -488,8 +490,10 @@ int cpu_exec(CPUState *cpu) cpu->exception_index =EXCP_INTERRUPT ;cpu_loop_exit (cpu); } -spin_lock (&tcg_ctx.tb_ctx.tb_lock); +#if defined(CONFIG_USER_ONLY ) +qemu_mutex_lock (&tcg_ctx.tb_ctx.tb_lock); have_tb_lock = true; +#endif tb =tb_find_fast (cpu);/* Note: we do it here to avoid a gcc bug on Mac OS X when doing it in tb_find_slow */
@@ -511,9 +515,10 @@ int cpu_exec(CPUState *cpu)tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb); } +#if defined(CONFIG_USER_ONLY ) have_tb_lock = false; -spin_unlock (&tcg_ctx.tb_ctx.tb_lock); - +qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); +#endif /* cpu_interrupt might be called while translating the TB, but before it is linked into a potentially infinite loop and becomes env->current_tb. Avoid
@@ -580,10 +585,12 @@ int cpu_exec(CPUState *cpu) x86_cpu =X86_CPU (cpu); env = &x86_cpu->env;#endif +#if defined(CONFIG_USER_ONLY )if (have_tb_lock) { -spin_unlock (&tcg_ctx.tb_ctx.tb_lock); +qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); have_tb_lock = false; } +#endif } }/* for(;;) */
include/exec/exec-all.h +2 -2
@@ -176,7 +176,7 @@ struct TranslationBlock {struct TranslationBlock *jmp_first; }; -#include "exec/spinlock.h" +#include "qemu/thread.h" typedef struct TBContext TBContext ;
@@ -186,7 +186,7 @@ struct TBContext {TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE ];int nb_tbs;/* any access to the tbs or the page table must use this lock */ -spinlock_t tb_lock; +QemuMutex tb_lock;/* statistics */ int tb_flush_count;
linux-user/main.c +3 -3
@@ -107,7 +107,7 @@ static int pending_cpus;/* Make sure everything is in a consistent state for calling fork(). */ void fork_start (void ) { -pthread_mutex_lock (&tcg_ctx.tb_ctx.tb_lock); +qemu_mutex_lock (&tcg_ctx.tb_ctx.tb_lock);pthread_mutex_lock (&exclusive_lock);mmap_fork_start (); }
@@ -129,11 +129,11 @@ void fork_end(int child)pthread_mutex_init (&cpu_list_mutex,NULL );pthread_cond_init (&exclusive_cond,NULL );pthread_cond_init (&exclusive_resume,NULL ); -pthread_mutex_init (&tcg_ctx.tb_ctx.tb_lock,NULL ); +qemu_mutex_init (&tcg_ctx.tb_ctx.tb_lock);gdbserver_fork (thread_cpu); }else {pthread_mutex_unlock (&exclusive_lock); -pthread_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); +qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); } }
target-i386/mem_helper.c +13 -3
@@ -23,17 +23,27 @@/* broken thread support */ -static spinlock_t global_cpu_lock =SPIN_LOCK_UNLOCKED ; +#if defined(CONFIG_USER_ONLY ) +QemuMutex global_cpu_lock;void helper_lock (void ) { -spin_lock (&global_cpu_lock); +qemu_mutex_lock (&global_cpu_lock); }void helper_unlock (void ) { -spin_unlock (&global_cpu_lock); +qemu_mutex_unlock (&global_cpu_lock); } +#else +void helper_lock (void ) +{ +} + +void helper_unlock (void ) +{ +} +#endif void helper_cmpxchg8b (CPUX86State *env,target_ulong a0) {
tcg/i386/tcg-target.c +8 -0
@@ -24,6 +24,10 @@#include "tcg-be-ldst.h" +#if defined(CONFIG_USER_ONLY ) +extern QemuMutex global_cpu_lock; +#endif +#ifndef NDEBUG static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS ] = { #if TCG_TARGET_REG_BITS == 64
@@ -2342,6 +2346,10 @@ static void tcg_target_init(TCGContext *s)tcg_regset_set_reg (s->reserved_regs,TCG_REG_CALL_STACK );tcg_add_target_add_op_defs (x86_op_defs); + +#if defined(CONFIG_USER_ONLY ) +qemu_mutex_init (global_cpu_lock); +#endif } typedef struct {
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 04/19] replace spinlock by QemuMutex.
Quoted 162 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
spinlock is only used in two cases:
*cpu-exec.c: to protect TranslationBlock
*mem_helper.c: for lock helper in target-i386 (which seems broken).It's a
pthread_mutex_tin user-mode so better using QemuMutex directly in this case.
It allows as well to reusetb_lockmutex of TBContext in case of multithread TCG.Signed-off-by KONRAD Frederic <[email protected]>--- cpu-exec.c | 15 +++++++++++---- include/exec/exec-all.h | 4 ++-- linux-user/main.c | 6 +++--- target-i386/mem_helper.c | 16 +++++++++++++--- tcg/i386/tcg-target.c | 8 ++++++++ 5 files changed, 37 insertions(+), 12 deletions(-)cpu-exec.c +11
-4@@ -361,7 +361,9 @@ int cpu_exec(CPUState *cpu)SyncClocks sc;/* This must be volatile so it is not trashed by longjmp() */ +#if defined(CONFIG_USER_ONLY )volatile bool have_tb_lock = false; +#endif if (async_safe_work_pending ()) { cpu->exit_request =1 ;@@ -488,8 +490,10 @@ int cpu_exec(CPUState *cpu) cpu->exception_index =EXCP_INTERRUPT ;cpu_loop_exit (cpu); } -spin_lock (&tcg_ctx.tb_ctx.tb_lock); +#if defined(CONFIG_USER_ONLY ) +qemu_mutex_lock (&tcg_ctx.tb_ctx.tb_lock); have_tb_lock = true; +#endif tb =tb_find_fast (cpu);/* Note: we do it here to avoid a gcc bug on Mac OS X when doing it in tb_find_slow */ @@ -511,9 +515,10 @@ int cpu_exec(CPUState *cpu)tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb); } +#if defined(CONFIG_USER_ONLY ) have_tb_lock = false; -spin_unlock (&tcg_ctx.tb_ctx.tb_lock); - +qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); +#endif /* cpu_interrupt might be called while translating the TB, but before it is linked into a potentially infinite loop and becomes env->current_tb. Avoid @@ -580,10 +585,12 @@ int cpu_exec(CPUState *cpu) x86_cpu =X86_CPU (cpu); env = &x86_cpu->env;#endif +#if defined(CONFIG_USER_ONLY )if (have_tb_lock) { -spin_unlock (&tcg_ctx.tb_ctx.tb_lock); +qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); have_tb_lock = false; } +#endif } }/* for(;;) */ include/exec/exec-all.h +2
-2@@ -176,7 +176,7 @@ struct TranslationBlock {struct TranslationBlock *jmp_first; }; -#include "exec/spinlock.h" +#include "qemu/thread.h" typedef struct TBContext TBContext ;@@ -186,7 +186,7 @@ struct TBContext {TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE ];int nb_tbs;/* any access to the tbs or the page table must use this lock */ -spinlock_t tb_lock; +QemuMutex tb_lock;/* statistics */ int tb_flush_count;linux-user/main.c +3
-3@@ -107,7 +107,7 @@ static int pending_cpus;/* Make sure everything is in a consistent state for calling fork(). */ void fork_start (void ) { -pthread_mutex_lock (&tcg_ctx.tb_ctx.tb_lock); +qemu_mutex_lock (&tcg_ctx.tb_ctx.tb_lock);pthread_mutex_lock (&exclusive_lock);mmap_fork_start (); }@@ -129,11 +129,11 @@ void fork_end(int child)pthread_mutex_init (&cpu_list_mutex,NULL );pthread_cond_init (&exclusive_cond,NULL );pthread_cond_init (&exclusive_resume,NULL ); -pthread_mutex_init (&tcg_ctx.tb_ctx.tb_lock,NULL ); +qemu_mutex_init (&tcg_ctx.tb_ctx.tb_lock);gdbserver_fork (thread_cpu); }else {pthread_mutex_unlock (&exclusive_lock); -pthread_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); +qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); } }target-i386/mem_helper.c +13
-3@@ -23,17 +23,27 @@/* broken thread support */ -static spinlock_t global_cpu_lock =SPIN_LOCK_UNLOCKED ; +#if defined(CONFIG_USER_ONLY ) +QemuMutex global_cpu_lock;void helper_lock (void ) { -spin_lock (&global_cpu_lock); +qemu_mutex_lock (&global_cpu_lock); }void helper_unlock (void ) { -spin_unlock (&global_cpu_lock); +qemu_mutex_unlock (&global_cpu_lock); } +#else +void helper_lock (void ) +{ +} + +void helper_unlock (void ) +{ +} +#endif void helper_cmpxchg8b (CPUX86State *env,target_ulong a0) {tcg/i386/tcg-target.c +0
-0
tcg/i386/tcg-target.c line 28
Quoted 5 lines
tcg/i386/tcg-target.c +2
-0@@ -24,6 +24,10 @@#include "tcg-be-ldst.h" +#if defined(CONFIG_USER_ONLY ) +extern QemuMutex global_cpu_lock;
This should be in target-i386/, not tcg/i386.
With this change, I think it's okay to put this patch and patch 5 in, separately from the rest of the MTTCG work.
Paolo
Quoted 17 lines
tcg/i386/tcg-target.c +6
-0+#endif +#ifndef NDEBUG static const char *const tcg_target_reg_names[TCG_TARGET_NB_REGS ] = { #if TCG_TARGET_REG_BITS == 64@@ -2342,6 +2346,10 @@ static void tcg_target_init(TCGContext *s)tcg_regset_set_reg (s->reserved_regs,TCG_REG_CALL_STACK );tcg_add_target_add_op_defs (x86_op_defs); + +#if defined(CONFIG_USER_ONLY ) +qemu_mutex_init (global_cpu_lock); +#endif } typedef struct {
RFC PATCH v7 5/19 remove unused spinlock.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This just removes spinlock as it is not used anymore.
Changes V6 -> V7:
* Drop the checkpatch part.
--- include/exec/spinlock.h | 49 ------------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 include/exec/spinlock.h
include/exec/spinlock.h deleted +0 -49
@@ -1,49 +0,0 @@ -/* -* Copyright (c) 2003 Fabrice Bellard -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, see <http://www.gnu.org/licenses/> -*/ - -/* configure guarantees us that we have pthreads on any host except -* mingw32, which doesn't support any of the user-only targets. -* So we can simply assume we have pthread mutexes here. -*/ -#if defined(CONFIG_USER_ONLY ) - -#include <pthread.h> -#define spin_lock pthread_mutex_lock -#define spin_unlock pthread_mutex_unlock -#define spinlock_t pthread_mutex_t -#define SPIN_LOCK_UNLOCKED PTHREAD_MUTEX_INITIALIZER - -#else - -/* Empty implementations, on the theory that system mode emulation -* is single-threaded. This means that these functions should only -* be used from code run in the TCG cpu thread, and cannot protect -* data structures which might also be accessed from the IO thread -* or from signal handlers. -*/ -typedef int spinlock_t ; -#define SPIN_LOCK_UNLOCKED 0 - -static inline void spin_lock (spinlock_t *lock) -{ -} - -static inline void spin_unlock (spinlock_t *lock) -{ -} - -#endif
Signature · 1 line
1.9.0
RFC PATCH v7 6/19 add support for spin lock on POSIX systems exclusively
In reply to [email protected]From: Guillaume Delbergue <[email protected]>
WARNING: spin lock is currently not implemented on WIN32
--- include/qemu/thread-posix.h | 4 ++++ include/qemu/thread-win32.h | 4 ++++ include/qemu/thread.h | 7 +++++++ util/qemu-thread-posix.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-win32.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+)
include/qemu/thread-posix.h +4 -0
@@ -7,6 +7,10 @@ struct QemuMutex {pthread_mutex_t lock; }; +struct QemuSpin { +pthread_spinlock_t lock; +}; +struct QemuCond {pthread_cond_t cond; };
include/qemu/thread-win32.h +4 -0
@@ -7,6 +7,10 @@ struct QemuMutex {LONG owner; }; +struct QemuSpin { +PKSPIN_LOCK lock; +}; +struct QemuCond {LONG waiters, target;HANDLE sema;
include/qemu/thread.h +7 -0
@@ -5,6 +5,7 @@#include <stdbool.h> typedef struct QemuMutex QemuMutex ; +typedef struct QemuSpin QemuSpin ;typedef struct QemuCond QemuCond ;typedef struct QemuSemaphore QemuSemaphore ;typedef struct QemuEvent QemuEvent ;
@@ -25,6 +26,12 @@ void qemu_mutex_lock(QemuMutex *mutex);int qemu_mutex_trylock (QemuMutex *mutex);void qemu_mutex_unlock (QemuMutex *mutex); +void qemu_spin_init (QemuSpin *spin); +void qemu_spin_destroy (QemuSpin *spin); +void qemu_spin_lock (QemuSpin *spin); +int qemu_spin_trylock (QemuSpin *spin); +void qemu_spin_unlock (QemuSpin *spin); +void qemu_cond_init (QemuCond *cond);void qemu_cond_destroy (QemuCond *cond);
util/qemu-thread-posix.c +45 -0
51 lines not on this page · raw message
util/qemu-thread-win32.c +30 -0
@@ -80,6 +80,36 @@ void qemu_mutex_unlock(QemuMutex *mutex)LeaveCriticalSection (&mutex->lock); } +void qemu_spin_init (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +void qemu_spin_destroy (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +void qemu_spin_lock (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +int qemu_spin_trylock (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +void qemu_spin_unlock (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} +void qemu_cond_init (QemuCond *cond) {memset (cond,0 ,sizeof (*cond));
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 06/19] add support for spin lock on POSIX systems exclusively
Quoted 5 lines · On 10/08/2015 17:27, [email protected] wrote:
From: Guillaume Delbergue <[email protected]>
WARNING: spin lock is currently not implemented on WIN32
Signed-off-by Guillaume Delbergue <[email protected]>
Should go before patch 12 (and IIUC Alvise's work should go in first anyway).
Paolo
Quoted 161 lines
--- include/qemu/thread-posix.h | 4 ++++ include/qemu/thread-win32.h | 4 ++++ include/qemu/thread.h | 7 +++++++ util/qemu-thread-posix.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-win32.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+)include/qemu/thread-posix.h +4
-0@@ -7,6 +7,10 @@ struct QemuMutex {pthread_mutex_t lock; }; +struct QemuSpin { +pthread_spinlock_t lock; +}; +struct QemuCond {pthread_cond_t cond; };include/qemu/thread-win32.h +4
-0@@ -7,6 +7,10 @@ struct QemuMutex {LONG owner; }; +struct QemuSpin { +PKSPIN_LOCK lock; +}; +struct QemuCond {LONG waiters, target;HANDLE sema;include/qemu/thread.h +7
-0@@ -5,6 +5,7 @@#include <stdbool.h> typedef struct QemuMutex QemuMutex ; +typedef struct QemuSpin QemuSpin ;typedef struct QemuCond QemuCond ;typedef struct QemuSemaphore QemuSemaphore ;typedef struct QemuEvent QemuEvent ;@@ -25,6 +26,12 @@ void qemu_mutex_lock(QemuMutex *mutex);int qemu_mutex_trylock (QemuMutex *mutex);void qemu_mutex_unlock (QemuMutex *mutex); +void qemu_spin_init (QemuSpin *spin); +void qemu_spin_destroy (QemuSpin *spin); +void qemu_spin_lock (QemuSpin *spin); +int qemu_spin_trylock (QemuSpin *spin); +void qemu_spin_unlock (QemuSpin *spin); +void qemu_cond_init (QemuCond *cond);void qemu_cond_destroy (QemuCond *cond);util/qemu-thread-posix.c +45
-0@@ -89,6 +89,51 @@ void qemu_mutex_unlock(QemuMutex *mutex)error_exit (err, __func__); } +void qemu_spin_init (QemuSpin *spin) +{ +int err; + + err =pthread_spin_init (&spin->lock,0 ); +if (err) { +error_exit (err, __func__); + } +} + +void qemu_spin_destroy (QemuSpin *spin) +{ +int err; + + err =pthread_spin_destroy (&spin->lock); +if (err) { +error_exit (err, __func__); + } +} + +void qemu_spin_lock (QemuSpin *spin) +{ +int err; + + err =pthread_spin_lock (&spin->lock); +if (err) { +error_exit (err, __func__); + } +} + +int qemu_spin_trylock (QemuSpin *spin) +{ +return pthread_spin_trylock (&spin->lock); +} + +void qemu_spin_unlock (QemuSpin *spin) +{ +int err; + + err =pthread_spin_unlock (&spin->lock); +if (err) { +error_exit (err, __func__); + } +} +void qemu_cond_init (QemuCond *cond) {int err;util/qemu-thread-win32.c +30
-0@@ -80,6 +80,36 @@ void qemu_mutex_unlock(QemuMutex *mutex)LeaveCriticalSection (&mutex->lock); } +void qemu_spin_init (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +void qemu_spin_destroy (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +void qemu_spin_lock (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +int qemu_spin_trylock (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} + +void qemu_spin_unlock (QemuSpin *spin) +{ +printf ("spinlock not implemented" ); +abort (); +} +void qemu_cond_init (QemuCond *cond) {memset (cond,0 ,sizeof (*cond));
RFC PATCH v7 7/19 protect TBContext with tb_lock.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This protects TBContext with tb_lock to make tb_* thread safe.
We can still have issue with tb_flush in case of multithread TCG:
An other CPU can be executing code during a flush.
This can be fixed later by making all other TCG thread exiting before calling tb_flush().
tb_find_slow is separated into tb_find_slow and tb_find_physical as the whole tb_find_slow doesn't require to lock the tb.
Changes:
V6 -> V7:
* Drop a tb_lock in already locked restore_state_to_opc.
V5 -> V6:
* Drop a tb_lock arround tb_find_fast in cpu-exec.c.
--- cpu-exec.c | 58 +++++++++++++------- include/exec/exec-all.h | 1 + target-arm/translate.c | 3 ++ tcg/tcg.h | 14 ++++- translate-all.c | 137 +++++++++++++++++++++++++++++++++++++----------- 5 files changed, 162 insertions(+), 51 deletions(-)
cpu-exec.c +38 -20
@@ -131,6 +131,8 @@ static void init_delay_params(SyncClocks *sc, const CPUState *cpu)void cpu_loop_exit (CPUState *cpu) { cpu->current_tb =NULL ; +/* Release those mutex before long jump so other thread can work. */ +tb_lock_reset ();siglongjmp (cpu->jmp_env,1 ); }
@@ -143,6 +145,8 @@ void cpu_resume_from_signal(CPUState *cpu, void *puc)/* XXX: restore cpu registers saved in host registers */ cpu->exception_index =-1 ; +/* Release those mutex before long jump so other thread can work. */ +tb_lock_reset ();siglongjmp (cpu->jmp_env,1 ); }
@@ -253,10 +257,8 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,tb_free (tb); } -static TranslationBlock *tb_find_slow (CPUState *cpu, -target_ulong pc, -target_ulong cs_base, -uint64_t flags) +static TranslationBlock *tb_find_physical (CPUState *cpu,target_ulong pc, +target_ulong cs_base,uint64_t flags) {CPUArchState *env = (CPUArchState *)cpu->env_ptr;TranslationBlock *tb, **ptb1;
@@ -273,8 +275,9 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];for (;;) { tb = *ptb1; -if (!tb) - goto not_found; +if (!tb) { +return tb; + } if (tb->pc == pc && tb->page_addr[0] == phys_page1 && tb->cs_base == cs_base &&
@@ -282,28 +285,42 @@ static TranslationBlock *tb_find_slow(CPUState *cpu,/* check next page if needed */ if (tb->page_addr[1 ] !=-1 ) {tb_page_addr_t phys_page2; - virt_page2 = (pc &TARGET_PAGE_MASK ) +TARGET_PAGE_SIZE ; phys_page2 =get_page_addr_code (env, virt_page2); -if (tb->page_addr[1 ] == phys_page2) - goto found; +if (tb->page_addr[1 ] == phys_page2) { +return tb; + } }else { - goto found; +return tb; } } ptb1 = &tb->phys_hash_next; } - not_found: -/* if no translated code available, then translate it now */ - tb =tb_gen_code (cpu, pc, cs_base, flags,0 ); - - found: -/* Move the last found TB to the head of the list */ -if (likely (*ptb1)) { - *ptb1 = tb->phys_hash_next; - tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h]; - tcg_ctx.tb_ctx.tb_phys_hash[h] = tb; +return tb; +} + +static TranslationBlock *tb_find_slow (CPUState *cpu,target_ulong pc, +target_ulong cs_base,uint64_t flags) +{ +/* +* First try to get the tb if we don't find it we need to lock and compile +* it. +*/ +TranslationBlock *tb; + + tb =tb_find_physical (cpu, pc, cs_base, flags); +if (!tb) { +tb_lock (); +/* +* Retry to get the TB in case a CPU just translate it to avoid having +* duplicated TB in the pool. +*/ + tb =tb_find_physical (cpu, pc, cs_base, flags); +if (!tb) { + tb =tb_gen_code (cpu, pc, cs_base, flags,0 ); + } +tb_unlock (); }/* we add the TB in the virtual pc hash table */ cpu->tb_jmp_cache[tb_jmp_cache_hash_func (pc)] = tb;
@@ -326,6 +343,7 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu) tb->flags != flags)) { tb =tb_find_slow (cpu, pc, cs_base, flags); } +return tb; }
include/exec/exec-all.h +1 -0
@@ -74,6 +74,7 @@ typedef struct TranslationBlock TranslationBlock;void gen_intermediate_code (CPUArchState *env,struct TranslationBlock *tb);void gen_intermediate_code_pc (CPUArchState *env,struct TranslationBlock *tb); +/* Called with tb_lock held. */ void restore_state_to_opc (CPUArchState *env,struct TranslationBlock *tb,int pc_pos);
target-arm/translate.c +3 -0
@@ -11166,6 +11166,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu, dc->tb = tb; +tb_lock (); + dc->is_jmp =DISAS_NEXT ; dc->pc = pc_start; dc->singlestep_enabled = cs->singlestep_enabled;
@@ -11506,6 +11508,7 @@ done_generating: tb->size = dc->pc - pc_start; tb->icount = num_insns; } +tb_unlock (); }void gen_intermediate_code (CPUARMState *env,TranslationBlock *tb)
tcg/tcg.h +13 -1
@@ -590,21 +590,33 @@ static inline bool tcg_op_buf_full(void)/* pool based memory allocation */ +/* tb_lock must be help for tcg_malloc_internal. */ void *tcg_malloc_internal (TCGContext *s,int size); +void tcg_pool_reset (TCGContext *s);void tcg_pool_delete (TCGContext *s); +void tb_lock (void ); +void tb_unlock (void ); +void tb_lock_reset (void ); +static inline void *tcg_malloc (int size) {TCGContext *s = &tcg_ctx;uint8_t *ptr, *ptr_end; +void *ret; + +tb_lock (); size = (size +sizeof (long ) -1 ) & ~(sizeof (long ) -1 ); ptr = s->pool_cur; ptr_end = ptr + size;if (unlikely (ptr_end > s->pool_end)) { -return tcg_malloc_internal (&tcg_ctx, size); + ret =tcg_malloc_internal (&tcg_ctx, size); +tb_unlock (); +return ret; }else { s->pool_cur = ptr_end; +tb_unlock ();return ptr; } }
translate-all.c +107 -30
296 lines not on this page · raw message
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 14 lines · On 10/08/2015 17:27, [email protected] wrote:
cpu-exec.c +2
-0@@ -131,6 +131,8 @@ static void init_delay_params(SyncClocks *sc, const CPUState *cpu)void cpu_loop_exit (CPUState *cpu) { cpu->current_tb =NULL ; +/* Release those mutex before long jump so other thread can work. */ +tb_lock_reset ();siglongjmp (cpu->jmp_env,1 ); }
cpu-exec.c void cpu_resume_from_signal(CPUState *cpu, void *puc) · line 152
Quoted 8 lines
cpu-exec.c +2
-0@@ -143,6 +145,8 @@ void cpu_resume_from_signal(CPUState *cpu, void *puc)/* XXX: restore cpu registers saved in host registers */ cpu->exception_index =-1 ; +/* Release those mutex before long jump so other thread can work. */ +tb_lock_reset ();siglongjmp (cpu->jmp_env,1 ); }
I think you should start easy and reuse the existing tb_lock code in cpu-exec.c:
cpu-exec.c +3 -21
@@ -307,7 +307,6 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, target_ulong pc, tb =tb_find_physical (cpu, pc, cs_base, flags);if (!tb) { -tb_lock ();/* * Retry to get the TB in case a CPU just translate it to avoid having * duplicated TB in the pool.
@@ -316,7 +315,6 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, target_ulong pc,if (!tb) { tb =tb_gen_code (cpu, pc, cs_base, flags,0 ); } -tb_unlock (); }/* we add the TB in the virtual pc hash table */ cpu->tb_jmp_cache[tb_jmp_cache_hash_func (pc)] = tb;
@@ -372,11 +372,6 @@ int cpu_exec(CPUState *cpu)uintptr_t next_tb;SyncClocks sc; -/* This must be volatile so it is not trashed by longjmp() */ -#if defined(CONFIG_USER_ONLY ) -volatile bool have_tb_lock = false; -#endif -if (cpu->halted) {if (!cpu_has_work (cpu)) {return EXCP_HALTED ;
@@ -480,10 +475,7 @@ int cpu_exec(CPUState *cpu) cpu->exception_index =EXCP_INTERRUPT ;cpu_loop_exit (cpu); } -#if defined(CONFIG_USER_ONLY ) -qemu_mutex_lock (&tcg_ctx.tb_ctx.tb_lock); - have_tb_lock = true; -#endif +tb_lock (); tb =tb_find_fast (cpu);/* Note: we do it here to avoid a gcc bug on Mac OS X when doing it in tb_find_slow */
@@ -505,10 +497,7 @@ int cpu_exec(CPUState *cpu)tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb); } -#if defined(CONFIG_USER_ONLY ) - have_tb_lock = false; -qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); -#endif +tb_unlock ();/* cpu_interrupt might be called while translating the TB, but before it is linked into a potentially infinite loop and becomes env->current_tb. Avoid
@@ -575,12 +564,7 @@ int cpu_exec(CPUState *cpu) x86_cpu =X86_CPU (cpu); env = &x86_cpu->env;#endif -#if defined(CONFIG_USER_ONLY ) -if (have_tb_lock) { -qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); - have_tb_lock = false; - } -#endif +tb_lock_reset (); } }/* for(;;) */
Optimizations should then come on top.
target-arm/translate.c static inline void gen_intermediate_code_internal(ARMCPU *cpu, · line 11169
Quoted 9 lines
target-arm/translate.c +1
-0@@ -11166,6 +11166,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu, dc->tb = tb; +tb_lock ();
This locks twice, I think? Both cpu_restore_state_from_tb and tb_gen_code (which calls cpu_gen_code) take the lock. How does it work?
target-arm/translate.c done_generating:
Quoted 13 lines
target-arm/translate.c +3
-0+ dc->is_jmp =DISAS_NEXT ; dc->pc = pc_start; dc->singlestep_enabled = cs->singlestep_enabled;@@ -11506,6 +11508,7 @@ done_generating: tb->size = dc->pc - pc_start; tb->icount = num_insns; } +tb_unlock (); } +/* tb_lock must be help for tcg_malloc_internal. */
"Held", not "help".
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 6 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
target-arm/translate.c +0
-0@@ -11166,6 +11166,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,
Quoted 6 lines
Quoted 3 lines
target-arm/translate.c static inline void gen_intermediate_code_internal(ARMCPU *cpu, · line 11169
Quoted 3 lines
target-arm/translate.c +1
-0dc->tb = tb; +tb_lock ();This locks twice, I think? Both
cpu_restore_state_from_tbandtb_gen_code(which callscpu_gen_code) take the lock. How does it work?
... ah, the lock is recursive!
I think this can be avoided. Let's look at it next week.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Paolo Bonzini
Quoted 8 lines
Quoted 8 lines
target-arm/translate.c +0
-0@@ -11166,6 +11166,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu, dc->tb = tb;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
target-arm/translate.c static inline void gen_intermediate_code_internal(ARMCPU *cpu, · line 11169
Quoted 1 line
target-arm/translate.c +1
-0+tb_lock ();This locks twice, I think? Both
cpu_restore_state_from_tbandtb_gen_code(which callscpu_gen_code) take the lock. How does it work?... ah, the lock is recursive!
I think this can be avoided. Let's look at it next week.
I take it your around on the Tuesday (Fred and I arrive Monday evening). Shall we pick a time or hunt for each other in the hacking room?
Quoted 2 lines
Paolo
Signature · 1 line
Alex Bennée
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 6 lines · Alex Bennée
Quoted 3 lines
... ah, the lock is recursive!
I think this can be avoided. Let's look at it next week.
I take it your around on the Tuesday (Fred and I arrive Monday evening). Shall we pick a time or hunt for each other in the hacking room?
Yes, I will. Early afternoon, since I will be at CloudOpen at 4pm and then at QEMU Summit.
I hope to post something this week too.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
In reply to Paolo BonziniQuoted 18 lines · Paolo Bonzini
Quoted 16 lines · On 10/08/2015 17:27, [email protected] wrote:
cpu-exec.c +2
-0@@ -131,6 +131,8 @@ static void init_delay_params(SyncClocks *sc, const CPUState *cpu)void cpu_loop_exit (CPUState *cpu) { cpu->current_tb =NULL ; +/* Release those mutex before long jump so other thread can work. */ +tb_lock_reset ();siglongjmp (cpu->jmp_env,1 ); }@@ -143,6 +145,8 @@ void cpu_resume_from_signal(CPUState *cpu, void *puc)/* XXX: restore cpu registers saved in host registers */
Quoted 8 lines
cpu-exec.c int cpu_exec(CPUState *cpu) · line 570
Quoted 6 lines
cpu-exec.c +2
-0cpu->exception_index =-1 ; +/* Release those mutex before long jump so other thread can work. */ +tb_lock_reset ();siglongjmp (cpu->jmp_env,1 ); }I think you should start easy and reuse the existing
tb_lockcode incpu-exec.c:
I think it's definitely not sufficient. Is user-mode multithread still working today?
Quoted 80 lines
cpu-exec.c +3
-21@@ -307,7 +307,6 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, target_ulong pc, tb =tb_find_physical (cpu, pc, cs_base, flags);if (!tb) { -tb_lock ();/* * Retry to get the TB in case a CPU just translate it to avoid having * duplicated TB in the pool. @@ -316,7 +315,6 @@ static TranslationBlock *tb_find_slow(CPUState *cpu, target_ulong pc,if (!tb) { tb =tb_gen_code (cpu, pc, cs_base, flags,0 ); } -tb_unlock (); }/* we add the TB in the virtual pc hash table */ cpu->tb_jmp_cache[tb_jmp_cache_hash_func (pc)] = tb;@@ -372,11 +372,6 @@ int cpu_exec(CPUState *cpu)uintptr_t next_tb;SyncClocks sc; -/* This must be volatile so it is not trashed by longjmp() */ -#if defined(CONFIG_USER_ONLY ) -volatile bool have_tb_lock = false; -#endif -if (cpu->halted) {if (!cpu_has_work (cpu)) {return EXCP_HALTED ;@@ -480,10 +475,7 @@ int cpu_exec(CPUState *cpu) cpu->exception_index =EXCP_INTERRUPT ;cpu_loop_exit (cpu); } -#if defined(CONFIG_USER_ONLY ) -qemu_mutex_lock (&tcg_ctx.tb_ctx.tb_lock); - have_tb_lock = true; -#endif +tb_lock (); tb =tb_find_fast (cpu);/* Note: we do it here to avoid a gcc bug on Mac OS X when doing it in tb_find_slow */ @@ -505,10 +497,7 @@ int cpu_exec(CPUState *cpu)tb_add_jump ((TranslationBlock *)(next_tb & ~TB_EXIT_MASK ), next_tb &TB_EXIT_MASK , tb); } -#if defined(CONFIG_USER_ONLY ) - have_tb_lock = false; -qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); -#endif +tb_unlock ();/* cpu_interrupt might be called while translating the TB, but before it is linked into a potentially infinite loop and becomes env->current_tb. Avoid @@ -575,12 +564,7 @@ int cpu_exec(CPUState *cpu) x86_cpu =X86_CPU (cpu); env = &x86_cpu->env;#endif -#if defined(CONFIG_USER_ONLY ) -if (have_tb_lock) { -qemu_mutex_unlock (&tcg_ctx.tb_ctx.tb_lock); - have_tb_lock = false; - } -#endif +tb_lock_reset (); } }/* for(;;) */ Optimizations should then come on top.
Quoted 6 lines
target-arm/translate.c +0
-0@@ -11166,6 +11166,8 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,
Quoted 6 lines
target-arm/translate.c static inline void gen_intermediate_code_internal(ARMCPU *cpu, · line 11169
Quoted 3 lines
target-arm/translate.c +1
-0dc->tb = tb; +tb_lock ();This locks twice, I think? Both
cpu_restore_state_from_tbandtb_gen_code(which callscpu_gen_code) take the lock. How does it work?
Yes it's recursive we might not need that though. I probably locked too much some function.
Thanks,
Fred
Quoted 16 lines
target-arm/translate.c done_generating: · line 11514
Quoted 12 lines
target-arm/translate.c +3
-0+ dc->is_jmp =DISAS_NEXT ; dc->pc = pc_start; dc->singlestep_enabled = cs->singlestep_enabled;@@ -11506,6 +11508,7 @@ done_generating: tb->size = dc->pc - pc_start; tb->icount = num_insns; } +tb_unlock (); } +/* tb_lock must be help for tcg_malloc_internal. */ "Held", not "help".
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 5 lines · Frederic Konrad
Quoted 2 lines
I think you should start easy and reuse the existing
tb_lockcode incpu-exec.c:I think it's definitely not sufficient. Is user-mode multithread still working today?
For some definition of "working", yes. It's not sufficient, but it's a good start.
The main problem with user-mode multithreading is that there is no clear lock hierarchy between mmap_lock and tb_lock. But this is not a problem for softmmu.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Frederic Konrad
Quoted 2 lines
I think you should start easy and reuse the existing
tb_lockcode incpu-exec.c:I think it's definitely not sufficient. Is user-mode multithread still working today?
Quoted 6 lines
For some definition of "working", yes. It's not sufficient, but it's a good start.
The main problem with user-mode multithreading is that there is no clear lock hierarchy between
mmap_lockandtb_lock. But this is not a problem for softmmu.
And also that we don't have a serious design for the locking at all. I was hoping this would be something that would come out of the multithreaded-TCG work...
-- PMM
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 6 lines · Peter Maydell
Quoted 6 lines
Quoted 2 lines
I think it's definitely not sufficient. Is user-mode multithread still working today?
For some definition of "working", yes. It's not sufficient, but it's a good start.
Quoted 7 lines
Quoted 3 lines
The main problem with user-mode multithreading is that there is no clear lock hierarchy between
mmap_lockandtb_lock. But this is not a problem for softmmu.And also that we don't have a serious design for the locking at all. I was hoping this would be something that would come out of the multithreaded-TCG work...
Indeed. Fred made an effort to identify the translate-all.c functions that need tb_lock, and there are only a few that need mmap_lock. I'm now trying to document it so that the patches are actually reviewable...
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
In reply to [email protected]Quoted 16 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This protects TBContext with
tb_lockto make tb_* thread safe.We can still have issue with
tb_flushin case of multithread TCG:
An other CPU can be executing code during a flush.This can be fixed later by making all other TCG thread exiting before calling
tb_flush().
tb_find_slowis separated intotb_find_slowandtb_find_physicalas the wholetb_find_slowdoesn't require to lock the tb.Signed-off-by KONRAD Frederic <[email protected]>Changes:
[...]
Quoted 23 lines
translate-all.c +7
-2@@ -675,6 +710,7 @@ static inline void code_gen_alloc(size_t tb_size)CODE_GEN_AVG_BLOCK_SIZE ; tcg_ctx.tb_ctx.tbs =g_malloc (tcg_ctx.code_gen_max_blocks *sizeof (TranslationBlock)); +qemu_mutex_init (&tcg_ctx.tb_ctx.tb_lock); }/* Must be called before using the QEMU cpus. 'tb_size' is the size @@ -699,16 +735,22 @@ bool tcg_enabled(void)return tcg_ctx.code_gen_buffer !=NULL ; } -/* Allocate a new translation block. Flush the translation buffer if -too many translation blocks or too much generated code. */ +/* +* Allocate a new translation block. Flush the translation buffer if +* too many translation blocks or too much generated code. +* tb_alloc is not thread safe but tb_gen_code is protected by a mutex so this +* function is called only by one thread. +*/ static TranslationBlock *tb_alloc (target_ulong pc) {
translate-all.c bool tcg_enabled(void) · line 753
Quoted 10 lines
translate-all.c +4
-2-TranslationBlock *tb; +TranslationBlock *tb =NULL ;if (tcg_ctx.tb_ctx.nb_tbs >= tcg_ctx.code_gen_max_blocks || (tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer) >= tcg_ctx.code_gen_buffer_max_size) { -return NULL ; + tb = &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++]; + tb->pc = pc; + tb->cflags =0 ;
Missed this wrong unreverted part which in the end doesn't do a tb_flush when required and crashes! Fixing that allows me to boot with jessie and virt.
Fred
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 44 lines · Frederic Konrad
Quoted 16 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This protects TBContext with
tb_lockto make tb_* thread safe.We can still have issue with
tb_flushin case of multithread TCG:
An other CPU can be executing code during a flush.This can be fixed later by making all other TCG thread exiting before calling
tb_flush().
tb_find_slowis separated intotb_find_slowandtb_find_physicalas the wholetb_find_slowdoesn't require to lock the tb.Signed-off-by KONRAD Frederic <[email protected]>Changes:
[...]
Quoted 26 lines
translate-all.c +8
-3@@ -675,6 +710,7 @@ static inline void code_gen_alloc(size_t tb_size)CODE_GEN_AVG_BLOCK_SIZE ; tcg_ctx.tb_ctx.tbs =g_malloc (tcg_ctx.code_gen_max_blocks *sizeof (TranslationBlock)); +qemu_mutex_init (&tcg_ctx.tb_ctx.tb_lock); }/* Must be called before using the QEMU cpus. 'tb_size' is the size @@ -699,16 +735,22 @@ bool tcg_enabled(void)return tcg_ctx.code_gen_buffer !=NULL ; } -/* Allocate a new translation block. Flush the translation buffer if -too many translation blocks or too much generated code. */ +/* +* Allocate a new translation block. Flush the translation buffer if +* too many translation blocks or too much generated code. +* tb_alloc is not thread safe but tb_gen_code is protected by a mutex so this +* function is called only by one thread. +*/ static TranslationBlock *tb_alloc (target_ulong pc) { -TranslationBlock *tb; +TranslationBlock *tb =NULL ;
Quoted 11 lines
translate-all.c bool tcg_enabled(void) · line 753
Quoted 7 lines
translate-all.c +3
-1if (tcg_ctx.tb_ctx.nb_tbs >= tcg_ctx.code_gen_max_blocks || (tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer) >= tcg_ctx.code_gen_buffer_max_size) { -return NULL ; + tb = &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++]; + tb->pc = pc; + tb->cflags =0 ;Missed this wrong unreverted part which in the end doesn't do a
tb_flushwhen required and crashes!
Fixing that allows me to boot with jessie and virt.
\o/
Do you see crashes while it is running?
It's interesting that I've not had a problem booting jessie with virt though - just crashes while hanging.
Are you likely to push a v8 this week (or a temp branch?) with this and any other obvious fixes? I appreciate Paolo has given you a not-so-small pile of review comments as well so I wasn't looking for a complete new patch set!
Quoted 2 lines
Fred
Signature · 1 line
Alex Bennée
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 5 lines
Are you likely to push a v8 this week (or a temp branch?) with this and any other obvious fixes? I appreciate Paolo has given you a not-so-small pile of review comments as well so I wasn't looking for a complete new patch set!
FWIW, reviews of the patches I posted a hour or two ago are welcome. Most of them are either very simple, just documentation, or straight out of v7.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
In reply to Alex BennéeQuoted 60 lines · Alex Bennée
Quoted 44 lines · Frederic Konrad
Quoted 16 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This protects TBContext with
tb_lockto make tb_* thread safe.We can still have issue with
tb_flushin case of multithread TCG:
An other CPU can be executing code during a flush.This can be fixed later by making all other TCG thread exiting before calling
tb_flush().
tb_find_slowis separated intotb_find_slowandtb_find_physicalas the wholetb_find_slowdoesn't require to lock the tb.Signed-off-by KONRAD Frederic <[email protected]>Changes:
[...]
Quoted 26 lines
translate-all.c +8
-3@@ -675,6 +710,7 @@ static inline void code_gen_alloc(size_t tb_size)CODE_GEN_AVG_BLOCK_SIZE ; tcg_ctx.tb_ctx.tbs =g_malloc (tcg_ctx.code_gen_max_blocks *sizeof (TranslationBlock)); +qemu_mutex_init (&tcg_ctx.tb_ctx.tb_lock); }/* Must be called before using the QEMU cpus. 'tb_size' is the size @@ -699,16 +735,22 @@ bool tcg_enabled(void)return tcg_ctx.code_gen_buffer !=NULL ; } -/* Allocate a new translation block. Flush the translation buffer if -too many translation blocks or too much generated code. */ +/* +* Allocate a new translation block. Flush the translation buffer if +* too many translation blocks or too much generated code. +* tb_alloc is not thread safe but tb_gen_code is protected by a mutex so this +* function is called only by one thread. +*/ static TranslationBlock *tb_alloc (target_ulong pc) { -TranslationBlock *tb; +TranslationBlock *tb =NULL ;Quoted 10 lines
translate-all.c bool tcg_enabled(void) · line 753
Quoted 7 lines
translate-all.c +3
-1if (tcg_ctx.tb_ctx.nb_tbs >= tcg_ctx.code_gen_max_blocks || (tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer) >= tcg_ctx.code_gen_buffer_max_size) { -return NULL ; + tb = &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++]; + tb->pc = pc; + tb->cflags =0 ;Missed this wrong unreverted part which in the end doesn't do a
tb_flushwhen required and crashes!
Fixing that allows me to boot with jessie and virt.\o/
Do you see crashes while it is running?
Quoted 7 lines
It's interesting that I've not had a problem booting jessie with virt though - just crashes while hanging.
Are you likely to push a v8 this week (or a temp branch?) with this and any other obvious fixes? I appreciate Paolo has given you a not-so-small pile of review comments as well so I wasn't looking for a complete new patch set!
here is something I did yesterday:multi_tcg_v7_bugfixed
The patch-set is a mess and not re-based on the patch-set sent by Paolo.
Fred
Quoted 2 lines
Quoted 2 lines
Fred
Re: [Qemu-devel] [RFC PATCH V7 07/19] protect TBContext with tb_lock.
Quoted 8 lines · Frederic Konrad
Quoted 4 lines
Are you likely to push a v8 this week (or a temp branch?) with this and any other obvious fixes? I appreciate Paolo has given you a not-so-small pile of review comments as well so I wasn't looking for a complete new patch set!
here is something I did yesterday:
multi_tcg_v7_bugfixedThe patch-set is a mess and not re-based on the patch-set sent by Paolo.
My own mttcg branch is probably pretty close to a v8, except that some of the TODO/FIXMEs I added should not be there. :)
Paolo
RFC PATCH v7 8/19 tcg: remove tcg_halt_cond global variable.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This removes tcg_halt_cond global variable.
We need one QemuCond per virtual cpu for multithread TCG.
--- cpus.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
cpus.c +7 -11
@@ -815,7 +815,6 @@ static unsigned iothread_requesting_mutex;static QemuThread io_thread;static QemuThread *tcg_cpu_thread; -static QemuCond *tcg_halt_cond;/* cpu creation */ static QemuCond qemu_cpu_cond;
@@ -1038,15 +1037,13 @@ static void qemu_wait_io_event_common(CPUState *cpu) cpu->thread_kicked = false; } -static void qemu_tcg_wait_io_event (void ) +static void qemu_tcg_wait_io_event (CPUState *cpu) { -CPUState *cpu; -while (all_cpu_threads_idle ()) {/* Start accounting real time to the virtual clock if the CPUs are idle. */ qemu_clock_warp (QEMU_CLOCK_VIRTUAL ); -qemu_cond_wait (tcg_halt_cond, &qemu_global_mutex); +qemu_cond_wait (cpu->halt_cond, &qemu_global_mutex); }while (iothread_requesting_mutex) {
@@ -1166,7 +1163,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)/* wait for initial kick-off after machine start */ while (first_cpu->stopped) { -qemu_cond_wait (tcg_halt_cond, &qemu_global_mutex); +qemu_cond_wait (first_cpu->halt_cond, &qemu_global_mutex);/* process any pending work */ CPU_FOREACH (cpu) {
@@ -1187,7 +1184,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_clock_notify (QEMU_CLOCK_VIRTUAL ); } } -qemu_tcg_wait_io_event (); +qemu_tcg_wait_io_event (QTAILQ_FIRST (&cpus)); }return NULL ;
@@ -1328,12 +1325,12 @@ static void qemu_tcg_init_vcpu(CPUState *cpu)tcg_cpu_address_space_init (cpu, cpu->as); + cpu->halt_cond =g_malloc0 (sizeof (QemuCond)); +qemu_cond_init (cpu->halt_cond); +/* share a single thread for all cpus with TCG */ if (!tcg_cpu_thread) { cpu->thread =g_malloc0 (sizeof (QemuThread)); - cpu->halt_cond =g_malloc0 (sizeof (QemuCond)); -qemu_cond_init (cpu->halt_cond); - tcg_halt_cond = cpu->halt_cond;snprintf (thread_name,VCPU_THREAD_NAME_SIZE ,"CPU %d/TCG" , cpu->cpu_index); qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
@@ -1347,7 +1344,6 @@ static void qemu_tcg_init_vcpu(CPUState *cpu) tcg_cpu_thread = cpu->thread; }else { cpu->thread = tcg_cpu_thread; - cpu->halt_cond = tcg_halt_cond; } }
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 08/19] tcg: remove tcg_halt_cond global variable.
Quoted 76 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This removes
tcg_halt_condglobal variable.
We need one QemuCond per virtual cpu for multithread TCG.Signed-off-by KONRAD Frederic <[email protected]>--- cpus.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)cpus.c +7
-10@@ -815,7 +815,6 @@ static unsigned iothread_requesting_mutex;static QemuThread io_thread;static QemuThread *tcg_cpu_thread; -static QemuCond *tcg_halt_cond;/* cpu creation */ static QemuCond qemu_cpu_cond;@@ -1038,15 +1037,13 @@ static void qemu_wait_io_event_common(CPUState *cpu) cpu->thread_kicked = false; } -static void qemu_tcg_wait_io_event (void ) +static void qemu_tcg_wait_io_event (CPUState *cpu) { -CPUState *cpu; -while (all_cpu_threads_idle ()) {/* Start accounting real time to the virtual clock if the CPUs are idle. */ qemu_clock_warp (QEMU_CLOCK_VIRTUAL ); -qemu_cond_wait (tcg_halt_cond, &qemu_global_mutex); +qemu_cond_wait (cpu->halt_cond, &qemu_global_mutex); }while (iothread_requesting_mutex) {@@ -1166,7 +1163,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)/* wait for initial kick-off after machine start */ while (first_cpu->stopped) { -qemu_cond_wait (tcg_halt_cond, &qemu_global_mutex); +qemu_cond_wait (first_cpu->halt_cond, &qemu_global_mutex);/* process any pending work */ CPU_FOREACH (cpu) {@@ -1187,7 +1184,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_clock_notify (QEMU_CLOCK_VIRTUAL ); } } -qemu_tcg_wait_io_event (); +qemu_tcg_wait_io_event (QTAILQ_FIRST (&cpus)); }return NULL ;@@ -1328,12 +1325,12 @@ static void qemu_tcg_init_vcpu(CPUState *cpu)tcg_cpu_address_space_init (cpu, cpu->as); + cpu->halt_cond =g_malloc0 (sizeof (QemuCond)); +qemu_cond_init (cpu->halt_cond); +/* share a single thread for all cpus with TCG */ if (!tcg_cpu_thread) { cpu->thread =g_malloc0 (sizeof (QemuThread)); - cpu->halt_cond =g_malloc0 (sizeof (QemuCond)); -qemu_cond_init (cpu->halt_cond); - tcg_halt_cond = cpu->halt_cond;snprintf (thread_name,VCPU_THREAD_NAME_SIZE ,"CPU %d/TCG" , cpu->cpu_index); qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
cpus.c static void qemu_tcg_init_vcpu(CPUState *cpu) · line 1349
Quoted 7 lines
cpus.c +0
-1@@ -1347,7 +1344,6 @@ static void qemu_tcg_init_vcpu(CPUState *cpu) tcg_cpu_thread = cpu->thread; }else { cpu->thread = tcg_cpu_thread; - cpu->halt_cond = tcg_halt_cond; } }
This should be squashed in "tcg: switch on multithread".
Paolo
RFC PATCH v7 9/19 Drop global lock during TCG code execution
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This finally allows TCG to benefit from the iothread introduction: Drop the global mutex while running pure TCG CPU code. Reacquire the lock when entering MMIO or PIO emulation, or when leaving the TCG loop.
We have to revert a few optimization for the current TCG threading model, namely kicking the TCG thread in qemu_mutex_lock_iothread and not kicking it in qemu_cpu_kick. We also need to disable RAM block reordering until we have a more efficient locking mechanism at hand.
I'm pretty sure some cases are still broken, definitely SMP (we no longer perform round-robin scheduling "by chance"). Still, a Linux x86 UP guest and my Musicpal ARM model boot fine here. These numbers demonstrate where we gain something:
20338 jan 20 0 331m 75m 6904 R 99 0.9 0:50.95 qemu-system-arm 20337 jan 20 0 331m 75m 6904 S 20 0.9 0:26.50 qemu-system-arm
The guest CPU was fully loaded, but the iothread could still run mostly independent on a second core. Without the patch we don't get beyond
32206 jan 20 0 330m 73m 7036 R 82 0.9 1:06.00 qemu-system-arm 32204 jan 20 0 330m 73m 7036 S 21 0.9 0:17.03 qemu-system-arm
We don't benefit significantly, though, when the guest is not fully loading a host CPU.
Note that this patch depends on
http://thread.gmane.org/gmane.comp.emulators.qemu/118657
Changes from Fred Konrad:
* Rebase on the current HEAD.
* Fixes a deadlock in qemu_devices_reset().
* Remove the mutex in address_space_*
--- cpus.c | 20 +++----------------- cputlb.c | 5 +++++ target-i386/misc_helper.c | 27 ++++++++++++++++++++++++--- translate-all.c | 2 ++ vl.c | 6 ++++++ 5 files changed, 40 insertions(+), 20 deletions(-)
cpus.c +3 -17
@@ -1232,23 +1232,7 @@ bool qemu_mutex_iothread_locked(void)void qemu_mutex_lock_iothread (void ) { -atomic_inc (&iothread_requesting_mutex); -/* In the simple case there is no need to bump the VCPU thread out of -* TCG code execution. -*/ -if (!tcg_enabled () ||qemu_in_vcpu_thread () || - !first_cpu || !first_cpu->thread) { -qemu_mutex_lock (&qemu_global_mutex); -atomic_dec (&iothread_requesting_mutex); - }else { -if (qemu_mutex_trylock (&qemu_global_mutex)) { -qemu_cpu_kick_thread (first_cpu); -qemu_mutex_lock (&qemu_global_mutex); - } -atomic_dec (&iothread_requesting_mutex); -qemu_cond_broadcast (&qemu_io_proceeded_cond); - } - iothread_locked = true; +qemu_mutex_lock (&qemu_global_mutex); }void qemu_mutex_unlock_iothread (void )
@@ -1469,7 +1453,9 @@ static int tcg_cpu_exec(CPUState *cpu) cpu->icount_decr.u16.low = decr; cpu->icount_extra = count; } +qemu_mutex_unlock_iothread (); ret =cpu_exec (cpu); +qemu_mutex_lock_iothread ();#ifdef CONFIG_PROFILER tcg_time +=profile_getclock () - ti;#endif
cputlb.c +5 -0
@@ -30,6 +30,9 @@#include "exec/ram_addr.h" #include "tcg/tcg.h" +void qemu_mutex_lock_iothread (void ); +void qemu_mutex_unlock_iothread (void ); +//#define DEBUG_TLB //#define DEBUG_TLB_CHECK
@@ -125,8 +128,10 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)can be detected */ void tlb_protect_code (ram_addr_t ram_addr) { +qemu_mutex_lock_iothread ();cpu_physical_memory_test_and_clear_dirty (ram_addr,TARGET_PAGE_SIZE ,DIRTY_MEMORY_CODE ); +qemu_mutex_unlock_iothread (); }/* update the TLB so that writes in physical page 'phys_addr' are no longer
target-i386/misc_helper.c +24 -3
@@ -27,8 +27,10 @@ void helper_outb(CPUX86State *env, uint32_t port, uint32_t data)#ifdef CONFIG_USER_ONLY fprintf (stderr,"outb: port=0x%04x, data=%02x\n" , port, data);#else +qemu_mutex_lock_iothread ();address_space_stb (&address_space_io, port, data,cpu_get_mem_attrs (env),NULL ); +qemu_mutex_unlock_iothread ();#endif }
@@ -38,8 +40,13 @@ target_ulong helper_inb(CPUX86State *env, uint32_t port)fprintf (stderr,"inb: port=0x%04x\n" , port);return 0 ;#else -return address_space_ldub (&address_space_io, port, +target_ulong ret; + +qemu_mutex_lock_iothread (); + ret =address_space_ldub (&address_space_io, port,cpu_get_mem_attrs (env),NULL ); +qemu_mutex_unlock_iothread (); +return ret;#endif }
@@ -48,8 +55,10 @@ void helper_outw(CPUX86State *env, uint32_t port, uint32_t data)#ifdef CONFIG_USER_ONLY fprintf (stderr,"outw: port=0x%04x, data=%04x\n" , port, data);#else +qemu_mutex_lock_iothread ();address_space_stw (&address_space_io, port, data,cpu_get_mem_attrs (env),NULL ); +qemu_mutex_unlock_iothread ();#endif }
@@ -59,8 +68,13 @@ target_ulong helper_inw(CPUX86State *env, uint32_t port)fprintf (stderr,"inw: port=0x%04x\n" , port);return 0 ;#else -return address_space_lduw (&address_space_io, port, +target_ulong ret; + +qemu_mutex_lock_iothread (); + ret =address_space_lduw (&address_space_io, port,cpu_get_mem_attrs (env),NULL ); +qemu_mutex_unlock_iothread (); +return ret;#endif }
@@ -69,8 +83,10 @@ void helper_outl(CPUX86State *env, uint32_t port, uint32_t data)#ifdef CONFIG_USER_ONLY fprintf (stderr,"outw: port=0x%04x, data=%08x\n" , port, data);#else +qemu_mutex_lock_iothread ();address_space_stl (&address_space_io, port, data,cpu_get_mem_attrs (env),NULL ); +qemu_mutex_unlock_iothread ();#endif }
@@ -80,8 +96,13 @@ target_ulong helper_inl(CPUX86State *env, uint32_t port)fprintf (stderr,"inl: port=0x%04x\n" , port);return 0 ;#else -return address_space_ldl (&address_space_io, port, +target_ulong ret; + +qemu_mutex_lock_iothread (); + ret =address_space_ldl (&address_space_io, port,cpu_get_mem_attrs (env),NULL ); +qemu_mutex_unlock_iothread (); +return ret;#endif }
translate-all.c +2 -0
@@ -1223,6 +1223,7 @@ void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,#endif #ifdef TARGET_HAS_PRECISE_SMC if (current_tb_modified) { +qemu_mutex_unlock_iothread ();/* we generate a block containing just the instruction modifying the memory. It will ensure that it cannot modify itself */
@@ -1327,6 +1328,7 @@ static void tb_invalidate_phys_page(tb_page_addr_t addr, p->first_tb =NULL ;#ifdef TARGET_HAS_PRECISE_SMC if (current_tb_modified) { +qemu_mutex_unlock_iothread ();/* we generate a block containing just the instruction modifying the memory. It will ensure that it cannot modify itself */
vl.c +6 -0
@@ -1717,10 +1717,16 @@ void qemu_devices_reset(void) {QEMUResetEntry *re, *nre; +/* +* Some device's reset needs to grab the global_mutex. So just release it +* here. +*/ +qemu_mutex_unlock_iothread ();/* reset all devices */ QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { re->func (re->opaque); } +qemu_mutex_lock_iothread (); }void qemu_system_reset (bool report)
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 09/19] Drop global lock during TCG code execution
Quoted 9 lines · On 10/08/2015 17:27, [email protected] wrote:
void
qemu_mutex_lock_iothread(void)
{cpus.c +0
-7-atomic_inc (&iothread_requesting_mutex); -/* In the simple case there is no need to bump the VCPU thread out of -* TCG code execution. -*/ -if (!tcg_enabled () ||qemu_in_vcpu_thread () || - !first_cpu || !first_cpu->thread) { -qemu_mutex_lock (&qemu_global_mutex);
cpus.c bool qemu_mutex_iothread_locked(void) · line 1251
Quoted 10 lines
cpus.c +0
-10-atomic_dec (&iothread_requesting_mutex); - }else { -if (qemu_mutex_trylock (&qemu_global_mutex)) { -qemu_cpu_kick_thread (first_cpu); -qemu_mutex_lock (&qemu_global_mutex); - } -atomic_dec (&iothread_requesting_mutex); -qemu_cond_broadcast (&qemu_io_proceeded_cond); - } - iothread_locked = true;
"iothread_locked = true" must be kept. Otherwise... yay! :)
cputlb.c void tlb_flush_page(CPUState *cpu, target_ulong addr) · line 136
Quoted 10 lines
cputlb.c +2
-0@@ -125,8 +128,10 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)can be detected */ void tlb_protect_code (ram_addr_t ram_addr) { +qemu_mutex_lock_iothread ();cpu_physical_memory_test_and_clear_dirty (ram_addr,TARGET_PAGE_SIZE ,DIRTY_MEMORY_CODE ); +qemu_mutex_unlock_iothread (); }
Not needed anymore.
target-i386/misc_helper.c
Quoted 4 lines
target-i386/misc_helper.c +0
-0
None of this is needed anymore either! :)
target-i386/misc_helper.c
Quoted 10 lines
target-i386/misc_helper.c +6
-0+/* +* Some device's reset needs to grab the global_mutex. So just release it +* here. +*/ +qemu_mutex_unlock_iothread ();/* reset all devices */ QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { re->func (re->opaque); } +qemu_mutex_lock_iothread ();
Should never have been true? (And, I think, it was pointed out in a previous version too).
Paolo
Re: [Qemu-devel] [RFC PATCH V7 09/19] Drop global lock during TCG code execution
Quoted 21 lines · Paolo Bonzini
Quoted 9 lines · On 10/08/2015 17:27, [email protected] wrote:
void qemu_mutex_lock_iothread(void) {cpus.c +0
-7-atomic_inc (&iothread_requesting_mutex); -/* In the simple case there is no need to bump the VCPU thread out of -* TCG code execution. -*/ -if (!tcg_enabled () ||qemu_in_vcpu_thread () || - !first_cpu || !first_cpu->thread) { -qemu_mutex_lock (&qemu_global_mutex);cpus.c bool qemu_mutex_iothread_locked(void) · line 1251
Quoted 10 lines
cpus.c +0
-10-atomic_dec (&iothread_requesting_mutex); - }else { -if (qemu_mutex_trylock (&qemu_global_mutex)) { -qemu_cpu_kick_thread (first_cpu); -qemu_mutex_lock (&qemu_global_mutex); - } -atomic_dec (&iothread_requesting_mutex); -qemu_cond_broadcast (&qemu_io_proceeded_cond); - } - iothread_locked = true;
Quoted 1 line
"
iothread_locked= true" must be kept. Otherwise... yay! :)
oops :).
Quoted 29 lines
cputlb.c void tlb_flush_page(CPUState *cpu, target_ulong addr) · line 136
Quoted 11 lines
cputlb.c +2
-0@@ -125,8 +128,10 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)can be detected */ void tlb_protect_code (ram_addr_t ram_addr) { +qemu_mutex_lock_iothread ();cpu_physical_memory_test_and_clear_dirty (ram_addr,TARGET_PAGE_SIZE ,DIRTY_MEMORY_CODE ); +qemu_mutex_unlock_iothread (); }Not needed anymore.
target-i386/misc_helper.c
Quoted 4 lines
target-i386/misc_helper.c +0
-0None of this is needed anymore either! :)
target-i386/misc_helper.c
Quoted 10 lines
target-i386/misc_helper.c +6
-0+/* +* Some device's reset needs to grab the global_mutex. So just release it +* here. +*/ +qemu_mutex_unlock_iothread ();/* reset all devices */ QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { re->func (re->opaque); } +qemu_mutex_lock_iothread ();
Quoted 2 lines
Should never have been true? (And, I think, it was pointed out in a previous version too).
I had a double lock with the reset handler from vexpress-a15. I don't really remember why. But I hacked that. It's fixed now :)
Thanks,
Fred
Quoted 2 lines
Paolo
Re: [Qemu-devel] [RFC PATCH V7 09/19] Drop global lock during TCG code execution
In reply to Paolo BonziniQuoted 21 lines · Paolo Bonzini
Quoted 9 lines · On 10/08/2015 17:27, [email protected] wrote:
void
qemu_mutex_lock_iothread(void)
{cpus.c +0
-7-atomic_inc (&iothread_requesting_mutex); -/* In the simple case there is no need to bump the VCPU thread out of -* TCG code execution. -*/ -if (!tcg_enabled () ||qemu_in_vcpu_thread () || - !first_cpu || !first_cpu->thread) { -qemu_mutex_lock (&qemu_global_mutex);cpus.c bool qemu_mutex_iothread_locked(void) · line 1251
Quoted 10 lines
cpus.c +0
-10-atomic_dec (&iothread_requesting_mutex); - }else { -if (qemu_mutex_trylock (&qemu_global_mutex)) { -qemu_cpu_kick_thread (first_cpu); -qemu_mutex_lock (&qemu_global_mutex); - } -atomic_dec (&iothread_requesting_mutex); -qemu_cond_broadcast (&qemu_io_proceeded_cond); - } - iothread_locked = true;
Quoted 2 lines
"
iothread_locked= true" must be kept. Otherwise... yay! :)
Also if qemu_cond_broadcast(&qemu_io_proceeded_cond) is being dropped there is no point keeping the guff around in qemu_tcg_wait_io_event.
Signature · 1 line
Alex Bennée
Re: [Qemu-devel] [RFC PATCH V7 09/19] Drop global lock during TCG code execution
Quoted 22 lines · Alex Bennée
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · On 10/08/2015 17:27, [email protected] wrote:
void qemu_mutex_lock_iothread(void) {cpus.c +0
-17-atomic_inc (&iothread_requesting_mutex); -/* In the simple case there is no need to bump the VCPU thread out of -* TCG code execution. -*/ -if (!tcg_enabled () ||qemu_in_vcpu_thread () || - !first_cpu || !first_cpu->thread) { -qemu_mutex_lock (&qemu_global_mutex); -atomic_dec (&iothread_requesting_mutex); - }else { -if (qemu_mutex_trylock (&qemu_global_mutex)) { -qemu_cpu_kick_thread (first_cpu); -qemu_mutex_lock (&qemu_global_mutex); - } -atomic_dec (&iothread_requesting_mutex); -qemu_cond_broadcast (&qemu_io_proceeded_cond); - } - iothread_locked = true;
Quoted 5 lines
Quoted 2 lines
"
iothread_locked= true" must be kept. Otherwise... yay! :)Also if
qemu_cond_broadcast(&qemu_io_proceeded_cond)is being dropped there is no point keeping the guff around inqemu_tcg_wait_io_event.
Yes good point.
BTW this leads to high consumption of host CPU eg: 100% per VCPU thread as the VCPUs thread are no longer waiting for qemu_io_proceeded_cond.
Fred
Re: [Qemu-devel] [RFC PATCH V7 09/19] Drop global lock during TCG code execution
Quoted 8 lines · Frederic Konrad
Quoted 4 lines
Quoted 1 line
Also if
qemu_cond_broadcast(&qemu_io_proceeded_cond)is being dropped there is no point keeping the guff around inqemu_tcg_wait_io_event.Yes good point.
BTW this leads to high consumption of host CPU eg: 100% per VCPU thread as the VCPUs thread are no longer waiting for
qemu_io_proceeded_cond.
If the guest CPU is busy waiting, that's expected. But if the guest CPU is halted, it should not have 100% host CPU consumption.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 09/19] Drop global lock during TCG code execution
Quoted 5 lines · Paolo Bonzini
Quoted 3 lines · Frederic Konrad
Quoted 3 lines
Also if
qemu_cond_broadcast(&qemu_io_proceeded_cond)is being dropped there is no point keeping the guff around inqemu_tcg_wait_io_event.
Quoted 9 lines
Quoted 4 lines
Yes good point.
BTW this leads to high consumption of host CPU eg: 100% per VCPU thread as the VCPUs thread are no longer waiting for
qemu_io_proceeded_cond.If the guest CPU is busy waiting, that's expected. But if the guest CPU is halted, it should not have 100% host CPU consumption.
Paolo
Hmm so that's definitely strange... I mean theorically it's the same as before?
An other thing. It seems that we need to signal the VCPU when the iothread take the lock eg:
if (tcg_enabled() && qemu_thread_is_self(&io_thread)) {
CPU_FOREACH(cpu) {
cpu_exit(cpu);
}
}To make this patch working without MTTCG.
Fred
RFC PATCH v7 10/19 cpu: remove exit_request global.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This removes exit_request global and adds a variable in CPUState for this. Only the flag for the first cpu is used for the moment as we are still with one TCG thread.
--- cpu-exec.c | 15 --------------- cpus.c | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 18 deletions(-)
cpu-exec.c +0 -15
@@ -363,8 +363,6 @@ static void cpu_handle_debug_exception(CPUState *cpu)/* main execution loop */ -volatile sig_atomic_t exit_request; -int cpu_exec (CPUState *cpu) {CPUClass *cc =CPU_GET_CLASS (cpu);
@@ -402,20 +400,8 @@ int cpu_exec(CPUState *cpu) } current_cpu = cpu; -/* As long as current_cpu is null, up to the assignment just above, -* requests by other threads to exit the execution loop are expected to -* be issued using the exit_request global. We must make sure that our -* evaluation of the global value is performed past the current_cpu -* value transition point, which requires a memory barrier as well as -* an instruction scheduling constraint on modern architectures. */ -smp_mb (); -rcu_read_lock (); -if (unlikely (exit_request)) { - cpu->exit_request =1 ; - } - cc->cpu_exec_enter (cpu);/* Calculate difference between guest clock and host clock.
@@ -504,7 +490,6 @@ int cpu_exec(CPUState *cpu) } }if (unlikely (cpu->exit_request)) { - cpu->exit_request =0 ; cpu->exception_index =EXCP_INTERRUPT ;cpu_loop_exit (cpu); }
cpus.c +14 -3
@@ -139,6 +139,8 @@ typedef struct TimersState { } TimersState;static TimersState timers_state; +/* CPU associated to this thread. */ +static __threadCPUState *tcg_thread_cpu;int64_t cpu_get_icount_raw (void ) {
@@ -663,12 +665,18 @@ static void cpu_handle_guest_debug(CPUState *cpu) cpu->stopped = true; } +/** +* cpu_signal +* Signal handler when using TCG. +*/ static void cpu_signal (int sig) {if (current_cpu) {cpu_exit (current_cpu); } - exit_request =1 ; + +/* FIXME: We might want to check if the cpu is running? */ + tcg_thread_cpu->exit_request = true; }#ifdef CONFIG_LINUX
@@ -1151,6 +1159,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)CPUState *cpu = arg;qemu_mutex_lock_iothread (); + tcg_thread_cpu = cpu;qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread);
@@ -1480,7 +1489,8 @@ static void tcg_exec_all(void)if (next_cpu ==NULL ) { next_cpu = first_cpu; } -for (; next_cpu !=NULL && !exit_request; next_cpu =CPU_NEXT (next_cpu)) { +for (; next_cpu !=NULL && !first_cpu->exit_request; + next_cpu =CPU_NEXT (next_cpu)) {CPUState *cpu = next_cpu; qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
@@ -1496,7 +1506,8 @@ static void tcg_exec_all(void)break ; } } - exit_request =0 ; + + first_cpu->exit_request =0 ; }void list_cpus (FILE *f,fprintf_function cpu_fprintf,const char *optarg)
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 10/19] cpu: remove exit_request global.
Quoted 12 lines · On 10/08/2015 17:27, [email protected] wrote:
{ if (current_cpu) { cpu_exit(current_cpu); }cpus.c +3
-1- exit_request =1 ; + +/* FIXME: We might want to check if the cpu is running? */ + tcg_thread_cpu->exit_request = true; }#ifdef CONFIG_LINUX
cpus.c static void *qemu_tcg_cpu_thread_fn(void *arg) · line 1164
Quoted 6 lines
cpus.c +1
-0@@ -1151,6 +1159,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)CPUState *cpu = arg;qemu_mutex_lock_iothread (); + tcg_thread_cpu = cpu;qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread);
This only makes sense for MTTCG, so it should be squashed in patch 11, I think.
Paolo
RFC PATCH v7 11/19 tcg: switch on multithread.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This switches on multithread.
Changes V5 -> V6:
* make qemu_cpu_kick calling qemu_cpu_kick_thread in case of TCG.
--- cpus.c | 93 ++++++++++++++++++++++++------------------------------------------ 1 file changed, 33 insertions(+), 60 deletions(-)
cpus.c +33 -60
@@ -65,7 +65,6 @@#endif /* CONFIG_LINUX */ -static CPUState *next_cpu;int64_t max_delay;int64_t max_advance;
@@ -822,8 +821,6 @@ static unsigned iothread_requesting_mutex;static QemuThread io_thread; -static QemuThread *tcg_cpu_thread; -/* cpu creation */ static QemuCond qemu_cpu_cond;/* system init */
@@ -1047,10 +1044,13 @@ static void qemu_wait_io_event_common(CPUState *cpu)static void qemu_tcg_wait_io_event (CPUState *cpu) { -while (all_cpu_threads_idle ()) { -/* Start accounting real time to the virtual clock if the CPUs -are idle. */ -qemu_clock_warp (QEMU_CLOCK_VIRTUAL ); +while (cpu_thread_is_idle (cpu)) { +/* Start accounting real time to the virtual clock if the CPUs +* are idle. +*/ +if ((all_cpu_threads_idle ()) && (cpu->cpu_index ==0 )) { +qemu_clock_warp (QEMU_CLOCK_VIRTUAL ); + }qemu_cond_wait (cpu->halt_cond, &qemu_global_mutex); }
@@ -1058,9 +1058,7 @@ static void qemu_tcg_wait_io_event(CPUState *cpu)qemu_cond_wait (&qemu_io_proceeded_cond, &qemu_global_mutex); } -CPU_FOREACH (cpu) { -qemu_wait_io_event_common (cpu); - } +qemu_wait_io_event_common (cpu); }static void qemu_kvm_wait_io_event (CPUState *cpu)
@@ -1152,7 +1150,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg)#endif } -static void tcg_exec_all (void ); +static void tcg_exec_all (CPUState *cpu);static void *qemu_tcg_cpu_thread_fn (void *arg) {
@@ -1163,37 +1161,26 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)qemu_tcg_init_cpu_signals ();qemu_thread_get_self (cpu->thread); -CPU_FOREACH (cpu) { - cpu->thread_id =qemu_get_thread_id (); - cpu->created = true; - cpu->can_do_io =1 ; - } -qemu_cond_signal (&qemu_cpu_cond); - -/* wait for initial kick-off after machine start */ -while (first_cpu->stopped) { -qemu_cond_wait (first_cpu->halt_cond, &qemu_global_mutex); - -/* process any pending work */ -CPU_FOREACH (cpu) { -qemu_wait_io_event_common (cpu); - } - } + cpu->thread_id =qemu_get_thread_id (); + cpu->created = true; + cpu->can_do_io =1 ; -/* process any pending work */ - exit_request =1 ; +qemu_cond_signal (&qemu_cpu_cond);while (1 ) { -tcg_exec_all (); +if (!cpu->stopped) { +tcg_exec_all (cpu); -if (use_icount) { -int64_t deadline =qemu_clock_deadline_ns_all (QEMU_CLOCK_VIRTUAL ); +if (use_icount) { +int64_t deadline = +qemu_clock_deadline_ns_all (QEMU_CLOCK_VIRTUAL ); -if (deadline ==0 ) { -qemu_clock_notify (QEMU_CLOCK_VIRTUAL ); +if (deadline ==0 ) { +qemu_clock_notify (QEMU_CLOCK_VIRTUAL ); + } } } -qemu_tcg_wait_io_event (QTAILQ_FIRST (&cpus)); +qemu_tcg_wait_io_event (cpu); }return NULL ;
@@ -1202,7 +1189,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)void qemu_cpu_kick (CPUState *cpu) {qemu_cond_broadcast (cpu->halt_cond); -if (!tcg_enabled () && !cpu->thread_kicked) { +if (!cpu->thread_kicked) {qemu_cpu_kick_thread (cpu); cpu->thread_kicked = true; }
@@ -1320,23 +1307,15 @@ static void qemu_tcg_init_vcpu(CPUState *cpu) cpu->halt_cond =g_malloc0 (sizeof (QemuCond));qemu_cond_init (cpu->halt_cond); - -/* share a single thread for all cpus with TCG */ -if (!tcg_cpu_thread) { - cpu->thread =g_malloc0 (sizeof (QemuThread)); -snprintf (thread_name,VCPU_THREAD_NAME_SIZE ,"CPU %d/TCG" , - cpu->cpu_index); -qemu_thread_create (cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, - cpu,QEMU_THREAD_JOINABLE ); + cpu->thread =g_malloc0 (sizeof (QemuThread)); +snprintf (thread_name,VCPU_THREAD_NAME_SIZE ,"CPU %d/TCG" , cpu->cpu_index); +qemu_thread_create (cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, cpu, +QEMU_THREAD_JOINABLE );#ifdef _WIN32 - cpu->hThread =qemu_thread_get_handle (cpu->thread); + cpu->hThread =qemu_thread_get_handle (cpu->thread);#endif -while (!cpu->created) { -qemu_cond_wait (&qemu_cpu_cond, &qemu_global_mutex); - } - tcg_cpu_thread = cpu->thread; - }else { - cpu->thread = tcg_cpu_thread; +while (!cpu->created) { +qemu_cond_wait (&qemu_cpu_cond, &qemu_global_mutex); } }
@@ -1479,20 +1458,14 @@ static int tcg_cpu_exec(CPUState *cpu)return ret; } -static void tcg_exec_all (void ) +static void tcg_exec_all (CPUState *cpu) {int r;/* Account partial waits to QEMU_CLOCK_VIRTUAL. */ qemu_clock_warp (QEMU_CLOCK_VIRTUAL ); -if (next_cpu ==NULL ) { - next_cpu = first_cpu; - } -for (; next_cpu !=NULL && !first_cpu->exit_request; - next_cpu =CPU_NEXT (next_cpu)) { -CPUState *cpu = next_cpu; - +while (!cpu->exit_request) {qemu_clock_enable (QEMU_CLOCK_VIRTUAL , (cpu->singlestep_enabled &SSTEP_NOTIMER ) ==0 );
@@ -1507,7 +1480,7 @@ static void tcg_exec_all(void) } } - first_cpu->exit_request =0 ; + cpu->exit_request =0 ; }void list_cpus (FILE *f,fprintf_function cpu_fprintf,const char *optarg)
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 11/19] tcg: switch on multithread.
cpus.c static void tcg_exec_all(void) · line 1483
Quoted 10 lines · On 10/08/2015 17:27, [email protected] wrote:
cpus.c +2
-1+while (!cpu->exit_request) {qemu_clock_enable (QEMU_CLOCK_VIRTUAL , (cpu->singlestep_enabled &SSTEP_NOTIMER ) ==0 );@@ -1507,7 +1480,7 @@ static void tcg_exec_all(void) } } - first_cpu->exit_request =0 ; + cpu->exit_request =0 ;
One issue here is that when tcg_cpu_exec returns EXCP_HALTED, the function keeps looping. There is no need to set cpu->exit_request in that case, since in fact there is no request pending, so the while loop probably should be an "if".
Also, cpu->interrupt_request is not protected by any mutex, so everything apart from the non-zero test must take the iothread mutex.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 11/19] tcg: switch on multithread.
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · On 10/08/2015 17:27, [email protected] wrote:
cpus.c +1
-0+while (!cpu->exit_request) {qemu_clock_enable (QEMU_CLOCK_VIRTUAL , (cpu->singlestep_enabled &SSTEP_NOTIMER ) ==0 );@@ -1507,7 +1480,7 @@ static void tcg_exec_all(void) } }
Quoted 6 lines
cpus.c static void tcg_exec_all(void) · line 1483
Quoted 2 lines
cpus.c +1
-1- first_cpu->exit_request =0 ; + cpu->exit_request =0 ;One issue here is that when
tcg_cpu_execreturnsEXCP_HALTED, the function keeps looping. There is no need to setcpu->exit_requestin that case, since in fact there is no request pending, so the while loop probably should be an "if".
Nice catch thanks!
I missed the fact that it was running through the list of VCPUs and exited the for(;;) loop.
I should rework this patch a little.. Maybe it's better to keep this loop and exit it when necessary eg: when icount elapse or cpu halted.
Fred
Quoted 6 lines
Also,
cpu->interrupt_requestis not protected by any mutex, so everything apart from the non-zero test must take the iothread mutex.Paolo
Re: [Qemu-devel] [RFC PATCH V7 11/19] tcg: switch on multithread.
Quoted 7 lines · Frederic Konrad
Quoted 5 lines
Quoted 1 line
One issue here is that when
tcg_cpu_execreturnsEXCP_HALTED, the function keeps looping. There is no need to setcpu->exit_requestin that case, since in fact there is no request pending, so the while loop probably should be an "if".Nice catch thanks!
Quoted 7 lines
I missed the fact that it was running through the list of VCPUs and exited the
for(;;) loop.I should rework this patch a little.. Maybe it's better to keep this loop and exit it
when necessary eg: when icount elapse or cpu halted.
Yeah, I don't have a particularly strong opinion on that. You can look at my mttcg github branch for my rebase on top of yesterday's series. It seems to work at least on the small GreenSoCs buildroot image.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 11/19] tcg: switch on multithread.
Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Frederic Konrad
Quoted 4 lines
One issue here is that when
tcg_cpu_execreturnsEXCP_HALTED, the function keeps looping. There is no need to setcpu->exit_requestin that case, since in fact there is no request pending, so the while loop probably should be an "if".Nice catch thanks!
I missed the fact that it was running through the list of VCPUs and exited the
for(;;) loop.
Quoted 8 lines
Quoted 3 lines
I should rework this patch a little.. Maybe it's better to keep this loop and exit it
when necessary eg: when icount elapse or cpu halted.Yeah, I don't have a particularly strong opinion on that. You can look at my mttcg github branch for my rebase on top of yesterday's series. It seems to work at least on the small GreenSoCs buildroot image.
Paolo
Their still seems to be something wrong with memory_region_rom_device_set_romd or something more general.
I'm trying to find this.
Fred
RFC PATCH v7 12/19 Use atomic cmpxchg to atomically check the exclusive value in a STREX
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This mechanism replaces the existing load/store exclusive mechanism which seems to be broken for multithread.
It follows the intention of the existing mechanism and stores the target address and data values during a load operation and checks that they remain unchanged before a store.
In common with the older approach, this provides weaker semantics than required in that it could be that a different processor writes the same value as a non-exclusive write, however in practise this seems to be irrelevant.
The old implementation didn’t correctly store it’s values as globals, but rather kept a local copy per CPU.
This new mechanism stores the values globally and also uses the atomic cmpxchg macros to ensure atomicity - it is therefore very efficient and threadsafe.
Changes:
V5 -> V6:
* Use spinlock instead of mutex.
* Fix the length for address map.
* Fix the return address for tlb_fill.
V4 -> V5:
* Remove atomic_check and atomic_release which were unused.
--- target-arm/cpu.c | 21 ++++++++ target-arm/cpu.h | 6 +++ target-arm/helper.c | 13 +++++ target-arm/helper.h | 4 ++ target-arm/op_helper.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++- target-arm/translate.c | 98 +++++++------------------------------ 6 files changed, 188 insertions(+), 82 deletions(-)
target-arm/cpu.c +21 -0
@@ -30,6 +30,26 @@#include "sysemu/kvm.h" #include "kvm_arm.h" +/* Protect cpu_exclusive_* variable .*/ +__threadbool cpu_have_exclusive_lock; +QemuSpin cpu_exclusive_lock; + +inline void arm_exclusive_lock (void ) +{ +if (!cpu_have_exclusive_lock) { +qemu_spin_lock (&cpu_exclusive_lock); + cpu_have_exclusive_lock = true; + } +} + +inline void arm_exclusive_unlock (void ) +{ +if (cpu_have_exclusive_lock) { + cpu_have_exclusive_lock = false; +qemu_spin_unlock (&cpu_exclusive_lock); + } +} +static void arm_cpu_set_pc (CPUState *cs,vaddr value) {ARMCPU *cpu =ARM_CPU (cs);
@@ -469,6 +489,7 @@ static void arm_cpu_initfn(Object *obj) cpu->psci_version =2 ;/* TCG implements PSCI 0.2 */ if (!inited) { inited = true; +qemu_spin_init (&cpu_exclusive_lock);arm_translate_init (); } }
target-arm/cpu.h +6 -0
@@ -515,6 +515,9 @@ static inline bool is_a64(CPUARMState *env)int cpu_arm_signal_handler (int host_signum,void *pinfo,void *puc); +bool arm_get_phys_addr (CPUARMState *env,target_ulong address,int access_type, +hwaddr *phys_ptr,int *prot,target_ulong *page_size); +/** * pmccntr_sync * @env: CPUARMState
@@ -1933,4 +1936,7 @@ enum { QEMU_PSCI_CONDUIT_HVC = 2, }; +void arm_exclusive_lock (void ); +void arm_exclusive_unlock (void ); +#endif
target-arm/helper.c +13 -0
@@ -24,6 +24,15 @@ static inline bool get_phys_addr(CPUARMState *env, target_ulong address,#define PMCRE 0x1#endif +bool arm_get_phys_addr (CPUARMState *env,target_ulong address,int access_type, +hwaddr *phys_ptr,int *prot,target_ulong *page_size) +{ +MemTxAttrs attrs = {}; +uint32_t fsr; +return get_phys_addr (env, address, access_type,cpu_mmu_index (env), + phys_ptr, &attrs, prot, page_size, &fsr); +} +static int vfp_gdb_get_reg (CPUARMState *env,uint8_t *buf,int reg) {int nregs;
@@ -4824,6 +4833,10 @@ void arm_cpu_do_interrupt(CPUState *cs)arm_log_exception (cs->exception_index); +arm_exclusive_lock (); + env->exclusive_addr =-1 ; +arm_exclusive_unlock (); +if (arm_is_psci_call (cpu, cs->exception_index)) {arm_handle_psci_call (cpu);qemu_log_mask (CPU_LOG_INT ,"...handled as PSCI call\n" );
target-arm/helper.h +4 -0
@@ -530,6 +530,10 @@ DEF_HELPER_2(dc_zva, void, env, i64) DEF_HELPER_FLAGS_2(neon_pmull_64_lo, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(neon_pmull_64_hi, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_4(atomic_cmpxchg64, i32, env, i32, i64, i32) +DEF_HELPER_1(atomic_clear, void, env) +DEF_HELPER_3(atomic_claim, void, env, i32, i64) +#ifdef TARGET_AARCH64 #include "helper-a64.h" #endif
target-arm/op_helper.c +127 -1
146 lines not on this page · raw message
target-arm/translate.c +17 -81
135 lines not on this page · raw message
Signature · 1 line
1.9.0
RFC PATCH v7 13/19 add a callback when tb_invalidate is called.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
Instead of doing the jump cache invalidation directly in tb_invalidate delay it after the exit so we don't have an other CPU trying to execute the code being invalidated.
--- translate-all.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-)
translate-all.c +59 -2
@@ -62,6 +62,7 @@#include "translate-all.h" #include "qemu/bitmap.h" #include "qemu/timer.h" +#include "sysemu/cpus.h" //#define DEBUG_TB_INVALIDATE //#define DEBUG_FLUSH
@@ -967,14 +968,58 @@ static inline void tb_reset_jump(TranslationBlock *tb, int n)tb_set_jmp_target (tb, n, (uintptr_t )(tb->tc_ptr + tb->tb_next_offset[n])); } +struct CPUDiscardTBParams { +CPUState *cpu; +TranslationBlock *tb; +}; + +static void cpu_discard_tb_from_jmp_cache (void *opaque) +{ +unsigned int h; +struct CPUDiscardTBParams *params = opaque; + + h =tb_jmp_cache_hash_func (params->tb->pc); +if (params->cpu->tb_jmp_cache[h] == params->tb) { + params->cpu->tb_jmp_cache[h] =NULL ; + } + +g_free (opaque); +} + +static void tb_invalidate_jmp_remove (void *opaque) +{ +TranslationBlock *tb = opaque; +TranslationBlock *tb1, *tb2; +unsigned int n1; + +/* suppress this TB from the two jump lists */ +tb_jmp_remove (tb,0 ); +tb_jmp_remove (tb,1 ); + +/* suppress any remaining jumps to this TB */ + tb1 = tb->jmp_first; +for (;;) { + n1 = (uintptr_t )tb1 &3 ; +if (n1 ==2 ) { +break ; + } + tb1 = (TranslationBlock *)((uintptr_t )tb1 & ~3 ); + tb2 = tb1->jmp_next[n1]; +tb_reset_jump (tb1, n1); + tb1->jmp_next[n1] =NULL ; + tb1 = tb2; + } + tb->jmp_first = (TranslationBlock *)((uintptr_t )tb |2 );/* fail safe */ +} +/* invalidate one TB */ void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr) {CPUState *cpu;PageDesc *p; -unsigned int h, n1; +unsigned int h;tb_page_addr_t phys_pc; -TranslationBlock *tb1, *tb2; +struct CPUDiscardTBParams *params;tb_lock ();
@@ -997,6 +1042,9 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) tcg_ctx.tb_ctx.tb_invalidated_flag =1 ; +#if 0 /*MTTCG*/ +TranslationBlock *tb1, *tb2; +unsigned int n1;/* remove the TB from the hash list */ h =tb_jmp_cache_hash_func (tb->pc);CPU_FOREACH (cpu) {
@@ -1023,6 +1071,15 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) tb1 = tb2; } tb->jmp_first = (TranslationBlock *)((uintptr_t )tb |2 );/* fail safe */ +#else +CPU_FOREACH (cpu) { + params =g_malloc (sizeof (struct CPUDiscardTBParams )); + params->cpu = cpu; + params->tb = tb; +async_run_on_cpu (cpu, cpu_discard_tb_from_jmp_cache, params); + } +async_run_safe_work_on_cpu (first_cpu, tb_invalidate_jmp_remove, tb); +#endif /* MTTCG */ tcg_ctx.tb_ctx.tb_phys_invalidate_count++;tb_unlock ();
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 13/19] add a callback when tb_invalidate is called.
Quoted 10 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
Instead of doing the jump cache invalidation directly in
tb_invalidatedelay it after the exit so we don't have an other CPU trying to execute the code being invalidated.Signed-off-by KONRAD Frederic <[email protected]>--- translate-all.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-)
If you take the easy way and avoid the optimizations in patch 7, this is not necessary: tb_find_fast and tb_add_jump are only called from within tb_lock, so all of tb_jmp_cache/jmp_first/jmp_next are protected by tb_lock.
Let's get everything in and then optimize; the order should be:
- Alvise's LL/SC implementation
- conversion of atomics to LL/SC for all front-ends
- the main MTTCG series, reusing the locking already in-place for user-mode emulation (with some audit...)
- any further push-downs of tb_lock
Paolo
Quoted 100 lines
translate-all.c +59
-2@@ -62,6 +62,7 @@#include "translate-all.h" #include "qemu/bitmap.h" #include "qemu/timer.h" +#include "sysemu/cpus.h" //#define DEBUG_TB_INVALIDATE //#define DEBUG_FLUSH @@ -967,14 +968,58 @@ static inline void tb_reset_jump(TranslationBlock *tb, int n)tb_set_jmp_target (tb, n, (uintptr_t )(tb->tc_ptr + tb->tb_next_offset[n])); } +struct CPUDiscardTBParams { +CPUState *cpu; +TranslationBlock *tb; +}; + +static void cpu_discard_tb_from_jmp_cache (void *opaque) +{ +unsigned int h; +struct CPUDiscardTBParams *params = opaque; + + h =tb_jmp_cache_hash_func (params->tb->pc); +if (params->cpu->tb_jmp_cache[h] == params->tb) { + params->cpu->tb_jmp_cache[h] =NULL ; + } + +g_free (opaque); +} + +static void tb_invalidate_jmp_remove (void *opaque) +{ +TranslationBlock *tb = opaque; +TranslationBlock *tb1, *tb2; +unsigned int n1; + +/* suppress this TB from the two jump lists */ +tb_jmp_remove (tb,0 ); +tb_jmp_remove (tb,1 ); + +/* suppress any remaining jumps to this TB */ + tb1 = tb->jmp_first; +for (;;) { + n1 = (uintptr_t )tb1 &3 ; +if (n1 ==2 ) { +break ; + } + tb1 = (TranslationBlock *)((uintptr_t )tb1 & ~3 ); + tb2 = tb1->jmp_next[n1]; +tb_reset_jump (tb1, n1); + tb1->jmp_next[n1] =NULL ; + tb1 = tb2; + } + tb->jmp_first = (TranslationBlock *)((uintptr_t )tb |2 );/* fail safe */ +} +/* invalidate one TB */ void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr) {CPUState *cpu;PageDesc *p; -unsigned int h, n1; +unsigned int h;tb_page_addr_t phys_pc; -TranslationBlock *tb1, *tb2; +struct CPUDiscardTBParams *params;tb_lock ();@@ -997,6 +1042,9 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) tcg_ctx.tb_ctx.tb_invalidated_flag =1 ; +#if 0 /*MTTCG*/ +TranslationBlock *tb1, *tb2; +unsigned int n1;/* remove the TB from the hash list */ h =tb_jmp_cache_hash_func (tb->pc);CPU_FOREACH (cpu) {@@ -1023,6 +1071,15 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) tb1 = tb2; } tb->jmp_first = (TranslationBlock *)((uintptr_t )tb |2 );/* fail safe */ +#else +CPU_FOREACH (cpu) { + params =g_malloc (sizeof (struct CPUDiscardTBParams )); + params->cpu = cpu; + params->tb = tb; +async_run_on_cpu (cpu, cpu_discard_tb_from_jmp_cache, params); + } +async_run_safe_work_on_cpu (first_cpu, tb_invalidate_jmp_remove, tb); +#endif /* MTTCG */ tcg_ctx.tb_ctx.tb_phys_invalidate_count++;tb_unlock ();
Re: [Qemu-devel] [RFC PATCH V7 13/19] add a callback when tb_invalidate is called.
Quoted 16 lines · Paolo Bonzini
Quoted 10 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
Instead of doing the jump cache invalidation directly in
tb_invalidatedelay it after the exit so we don't have an other CPU trying to execute the code being invalidated.Signed-off-by KONRAD Frederic <[email protected]>--- translate-all.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-)If you take the easy way and avoid the optimizations in patch 7, this is not necessary:
tb_find_fastandtb_add_jumpare only called from withintb_lock, so all of tb_jmp_cache/jmp_first/jmp_next are protected bytb_lock.
Quoted 8 lines
Let's get everything in and then optimize; the order should be:
- Alvise's LL/SC implementation
- conversion of atomics to LL/SC for all front-ends
- the main MTTCG series, reusing the locking already in-place for user-mode emulation (with some audit...)
- including dropping the cmpxchg fix and including Alvise's MTTCG aware patches that build on top of LL/SC work.
Quoted 105 lines
- any further push-downs of
tb_lockPaolo
Quoted 100 lines
translate-all.c +59
-2@@ -62,6 +62,7 @@#include "translate-all.h" #include "qemu/bitmap.h" #include "qemu/timer.h" +#include "sysemu/cpus.h" //#define DEBUG_TB_INVALIDATE //#define DEBUG_FLUSH @@ -967,14 +968,58 @@ static inline void tb_reset_jump(TranslationBlock *tb, int n)tb_set_jmp_target (tb, n, (uintptr_t )(tb->tc_ptr + tb->tb_next_offset[n])); } +struct CPUDiscardTBParams { +CPUState *cpu; +TranslationBlock *tb; +}; + +static void cpu_discard_tb_from_jmp_cache (void *opaque) +{ +unsigned int h; +struct CPUDiscardTBParams *params = opaque; + + h =tb_jmp_cache_hash_func (params->tb->pc); +if (params->cpu->tb_jmp_cache[h] == params->tb) { + params->cpu->tb_jmp_cache[h] =NULL ; + } + +g_free (opaque); +} + +static void tb_invalidate_jmp_remove (void *opaque) +{ +TranslationBlock *tb = opaque; +TranslationBlock *tb1, *tb2; +unsigned int n1; + +/* suppress this TB from the two jump lists */ +tb_jmp_remove (tb,0 ); +tb_jmp_remove (tb,1 ); + +/* suppress any remaining jumps to this TB */ + tb1 = tb->jmp_first; +for (;;) { + n1 = (uintptr_t )tb1 &3 ; +if (n1 ==2 ) { +break ; + } + tb1 = (TranslationBlock *)((uintptr_t )tb1 & ~3 ); + tb2 = tb1->jmp_next[n1]; +tb_reset_jump (tb1, n1); + tb1->jmp_next[n1] =NULL ; + tb1 = tb2; + } + tb->jmp_first = (TranslationBlock *)((uintptr_t )tb |2 );/* fail safe */ +} +/* invalidate one TB */ void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr) {CPUState *cpu;PageDesc *p; -unsigned int h, n1; +unsigned int h;tb_page_addr_t phys_pc; -TranslationBlock *tb1, *tb2; +struct CPUDiscardTBParams *params;tb_lock ();@@ -997,6 +1042,9 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) tcg_ctx.tb_ctx.tb_invalidated_flag =1 ; +#if 0 /*MTTCG*/ +TranslationBlock *tb1, *tb2; +unsigned int n1;/* remove the TB from the hash list */ h =tb_jmp_cache_hash_func (tb->pc);CPU_FOREACH (cpu) {@@ -1023,6 +1071,15 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) tb1 = tb2; } tb->jmp_first = (TranslationBlock *)((uintptr_t )tb |2 );/* fail safe */ +#else +CPU_FOREACH (cpu) { + params =g_malloc (sizeof (struct CPUDiscardTBParams )); + params->cpu = cpu; + params->tb = tb; +async_run_on_cpu (cpu, cpu_discard_tb_from_jmp_cache, params); + } +async_run_safe_work_on_cpu (first_cpu, tb_invalidate_jmp_remove, tb); +#endif /* MTTCG */ tcg_ctx.tb_ctx.tb_phys_invalidate_count++;tb_unlock ();
Signature · 1 line
Alex Bennée
RFC PATCH v7 14/19 cpu: introduce tlb_flush*_all.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
Some architectures allow to flush the tlb of other VCPUs. This is not a problem when we have only one thread for all VCPUs but it definitely needs to be an asynchronous work when we are in true multithreaded work.
TODO: Some test case, I fear some bad results in case a VCPUs execute a barrier
or something like that.
--- cputlb.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ include/exec/exec-all.h | 2 ++ 2 files changed, 78 insertions(+)
cputlb.c +76 -0
@@ -72,6 +72,45 @@ void tlb_flush(CPUState *cpu, int flush_global) tlb_flush_count++; } +struct TLBFlushParams { +CPUState *cpu; +int flush_global; +}; + +static void tlb_flush_async_work (void *opaque) +{ +struct TLBFlushParams *params = opaque; + +tlb_flush (params->cpu, params->flush_global); +g_free (params); +} + +void tlb_flush_all (int flush_global) +{ +CPUState *cpu; +struct TLBFlushParams *params; + +#if 0 /* MTTCG */ +CPU_FOREACH (cpu) { +tlb_flush (cpu, flush_global); + } +#else +CPU_FOREACH (cpu) { +if (qemu_cpu_is_self (cpu)) { +/* async_run_on_cpu handle this case but this just avoid a malloc +* here. +*/ +tlb_flush (cpu, flush_global); + }else { + params =g_malloc (sizeof (struct TLBFlushParams )); + params->cpu = cpu; + params->flush_global = flush_global; +async_run_on_cpu (cpu, tlb_flush_async_work, params); + } + } +#endif /* MTTCG */ +} +static inline void tlb_flush_entry (CPUTLBEntry *tlb_entry,target_ulong addr) { if (addr == (tlb_entry->addr_read &
@@ -124,6 +163,43 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)tb_flush_jmp_cache (cpu, addr); } +struct TLBFlushPageParams { +CPUState *cpu; +target_ulong addr; +}; + +static void tlb_flush_page_async_work (void *opaque) +{ +struct TLBFlushPageParams *params = opaque; + +tlb_flush_page (params->cpu, params->addr); +g_free (params); +} + +void tlb_flush_page_all (target_ulong addr) +{ +CPUState *cpu; +struct TLBFlushPageParams *params; + +CPU_FOREACH (cpu) { +#if 0 /* !MTTCG */ +tlb_flush_page (cpu, addr); +#else +if (qemu_cpu_is_self (cpu)) { +/* async_run_on_cpu handle this case but this just avoid a malloc +* here. +*/ +tlb_flush_page (cpu, addr); + }else { + params =g_malloc (sizeof (struct TLBFlushPageParams )); + params->cpu = cpu; + params->addr = addr; +async_run_on_cpu (cpu, tlb_flush_page_async_work, params); + } +#endif /* MTTCG */ + } +} +/* update the TLBs so that writes to code in the virtual page 'addr' can be detected */ void tlb_protect_code (ram_addr_t ram_addr)
include/exec/exec-all.h +2 -0
@@ -97,7 +97,9 @@ bool qemu_in_vcpu_thread(void);void cpu_reload_memory_map (CPUState *cpu);void tcg_cpu_address_space_init (CPUState *cpu,AddressSpace *as);/* cputlb.c */ +void tlb_flush_page_all (target_ulong addr);void tlb_flush_page (CPUState *cpu,target_ulong addr); +void tlb_flush_all (int flush_global);void tlb_flush (CPUState *cpu,int flush_global); void tlb_set_page(CPUState *cpu, target_ulong vaddr, hwaddr paddr, int prot,
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 14/19] cpu: introduce tlb_flush*_all.
Quoted 6 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
Some architectures allow to flush the tlb of other VCPUs. This is not a problem when we have only one thread for all VCPUs but it definitely needs to be an asynchronous work when we are in true multithreaded work.
Quoted 8 lines
TODO: Some test case, I fear some bad results in case a VCPUs execute a barrier
or something like that.Signed-off-by KONRAD Frederic <[email protected]>--- cputlb.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ include/exec/exec-all.h | 2 ++ 2 files changed, 78 insertions(+)
I still believe this should be a target-specific change. This would also make it easier to do the remote TLB flush synchronously, as is the case on ARM (if I understand correctly).
Paolo
Quoted 109 lines
cputlb.c +76
-0@@ -72,6 +72,45 @@ void tlb_flush(CPUState *cpu, int flush_global) tlb_flush_count++; } +struct TLBFlushParams { +CPUState *cpu; +int flush_global; +}; + +static void tlb_flush_async_work (void *opaque) +{ +struct TLBFlushParams *params = opaque; + +tlb_flush (params->cpu, params->flush_global); +g_free (params); +} + +void tlb_flush_all (int flush_global) +{ +CPUState *cpu; +struct TLBFlushParams *params; + +#if 0 /* MTTCG */ +CPU_FOREACH (cpu) { +tlb_flush (cpu, flush_global); + } +#else +CPU_FOREACH (cpu) { +if (qemu_cpu_is_self (cpu)) { +/* async_run_on_cpu handle this case but this just avoid a malloc +* here. +*/ +tlb_flush (cpu, flush_global); + }else { + params =g_malloc (sizeof (struct TLBFlushParams )); + params->cpu = cpu; + params->flush_global = flush_global; +async_run_on_cpu (cpu, tlb_flush_async_work, params); + } + } +#endif /* MTTCG */ +} +static inline void tlb_flush_entry (CPUTLBEntry *tlb_entry,target_ulong addr) { if (addr == (tlb_entry->addr_read &@@ -124,6 +163,43 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)tb_flush_jmp_cache (cpu, addr); } +struct TLBFlushPageParams { +CPUState *cpu; +target_ulong addr; +}; + +static void tlb_flush_page_async_work (void *opaque) +{ +struct TLBFlushPageParams *params = opaque; + +tlb_flush_page (params->cpu, params->addr); +g_free (params); +} + +void tlb_flush_page_all (target_ulong addr) +{ +CPUState *cpu; +struct TLBFlushPageParams *params; + +CPU_FOREACH (cpu) { +#if 0 /* !MTTCG */ +tlb_flush_page (cpu, addr); +#else +if (qemu_cpu_is_self (cpu)) { +/* async_run_on_cpu handle this case but this just avoid a malloc +* here. +*/ +tlb_flush_page (cpu, addr); + }else { + params =g_malloc (sizeof (struct TLBFlushPageParams )); + params->cpu = cpu; + params->addr = addr; +async_run_on_cpu (cpu, tlb_flush_page_async_work, params); + } +#endif /* MTTCG */ + } +} +/* update the TLBs so that writes to code in the virtual page 'addr' can be detected */ void tlb_protect_code (ram_addr_t ram_addr)include/exec/exec-all.h +2
-0@@ -97,7 +97,9 @@ bool qemu_in_vcpu_thread(void);void cpu_reload_memory_map (CPUState *cpu);void tcg_cpu_address_space_init (CPUState *cpu,AddressSpace *as);/* cputlb.c */ +void tlb_flush_page_all (target_ulong addr);void tlb_flush_page (CPUState *cpu,target_ulong addr); +void tlb_flush_all (int flush_global);void tlb_flush (CPUState *cpu,int flush_global); void tlb_set_page(CPUState *cpu, target_ulong vaddr, hwaddr paddr, int prot,
Re: [Qemu-devel] [RFC PATCH V7 14/19] cpu: introduce tlb_flush*_all.
Quoted 16 lines · Paolo Bonzini
Quoted 6 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
Some architectures allow to flush the tlb of other VCPUs. This is not a problem when we have only one thread for all VCPUs but it definitely needs to be an asynchronous work when we are in true multithreaded work.
Quoted 8 lines
TODO: Some test case, I fear some bad results in case a VCPUs execute a barrier
or something like that.Signed-off-by KONRAD Frederic <[email protected]>--- cputlb.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ include/exec/exec-all.h | 2 ++ 2 files changed, 78 insertions(+)
Quoted 3 lines
I still believe this should be a target-specific change. This would also make it easier to do the remote TLB flush synchronously, as is the case on ARM (if I understand correctly).
ARM TLB flushes have to complete by the next barrier instruction (or equivalent thing); so they're asynchronous but with a guest-controlled synchronization point.
Also, compare the series I posted recently for adding missing TLB operations:
https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg00945.html
which adds support for flush-specific-mmuidx operations, which would increase the number of primitives you're trying to support here. That might argue for making this target-specific.
thanks
-- PMM
RFC PATCH v7 15/19 arm: use tlb_flush*_all
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This just use the new mechanism to ensure that each VCPU thread flush its own VCPU.
--- target-arm/helper.c | 45 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 38 deletions(-)
target-arm/helper.c +7 -38
@@ -411,41 +411,25 @@ static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,static void tlbiall_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; - -CPU_FOREACH (other_cs) { -tlb_flush (other_cs,1 ); - } +tlb_flush_all (1 ); }static void tlbiasid_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; - -CPU_FOREACH (other_cs) { -tlb_flush (other_cs, value ==0 ); - } +tlb_flush_all (value ==0 ); }static void tlbimva_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; - -CPU_FOREACH (other_cs) { -tlb_flush_page (other_cs, value &TARGET_PAGE_MASK ); - } +tlb_flush_page_all (value &TARGET_PAGE_MASK ); }static void tlbimvaa_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; - -CPU_FOREACH (other_cs) { -tlb_flush_page (other_cs, value &TARGET_PAGE_MASK ); - } +tlb_flush_page_all (value &TARGET_PAGE_MASK ); }static const ARMCPRegInfo cp_reginfo[] = {
@@ -2281,34 +2265,19 @@ static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,static void tlbi_aa64_va_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; -uint64_t pageaddr =sextract64 (value <<12 ,0 ,56 ); - -CPU_FOREACH (other_cs) { -tlb_flush_page (other_cs, pageaddr); - } +tlb_flush_page_all (sextract64 (value <<12 ,0 ,56 )); }static void tlbi_aa64_vaa_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; -uint64_t pageaddr =sextract64 (value <<12 ,0 ,56 ); - -CPU_FOREACH (other_cs) { -tlb_flush_page (other_cs, pageaddr); - } +tlb_flush_page_all (sextract64 (value <<12 ,0 ,56 )); }static void tlbi_aa64_asid_is_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { -CPUState *other_cs; -int asid =extract64 (value,48 ,16 ); - -CPU_FOREACH (other_cs) { -tlb_flush (other_cs, asid ==0 ); - } +tlb_flush_all (extract64 (value,48 ,16 ) ==0 ); }static CPAccessResult aa64_zva_access (CPUARMState *env,const ARMCPRegInfo *ri)
Signature · 1 line
1.9.0
RFC PATCH v7 16/19 translate-all: introduces tb_flush_safe.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
tb_flush is not thread safe we definitely need to exit VCPUs to do that. This introduces tb_flush_safe which just creates an async safe work which will do a tb_flush later.
--- include/exec/exec-all.h | 1 + translate-all.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+)
include/exec/exec-all.h +1 -0
@@ -200,6 +200,7 @@ struct TBContext {void tb_free (TranslationBlock *tb);void tb_flush (CPUState *cpu); +void tb_flush_safe (CPUState *cpu);void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr);#if defined(USE_DIRECT_JUMP )
translate-all.c +15 -0
@@ -818,6 +818,21 @@ static void page_flush_tb(void) } } +static void tb_flush_work (void *opaque) +{ +CPUState *cpu = opaque; +tb_flush (cpu); +} + +void tb_flush_safe (CPUState *cpu) +{ +#if 0 /* !MTTCG */ +tb_flush (cpu); +#else +async_run_safe_work_on_cpu (cpu, tb_flush_work, cpu); +#endif /* MTTCG */ +} +/* flush all the translation blocks */ /* XXX: tb_flush is currently not thread safe */ void tb_flush (CPUState *cpu)
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 16/19] translate-all: introduces tb_flush_safe.
translate-all.c static void page_flush_tb(void) · line 835
Quoted 10 lines · On 10/08/2015 17:27, [email protected] wrote:
translate-all.c +10
-0+ +void tb_flush_safe (CPUState *cpu) +{ +#if 0 /* !MTTCG */ +tb_flush (cpu); +#else +async_run_safe_work_on_cpu (cpu, tb_flush_work, cpu); +#endif /* MTTCG */ +} +
I think this can use first_cpu unconditionally; tb_flush only uses its argument for an error message.
In fact, I think that by definition async_run_safe_work_on_cpu can use any CPU for its work; it locks out everyone else, so it does not matter which thread you're on. So async_run_safe_work_on_cpu could drop the @cpu argument (becoming async_run_safe_cpu_work) and use first_cpu unconditionally.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 16/19] translate-all: introduces tb_flush_safe.
In reply to [email protected]Quoted 7 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
tb_flushis not thread safe we definitely need to exit VCPUs to do that. This introducestb_flush_safewhich just creates an async safe work which will do atb_flushlater.Signed-off-by KONRAD Frederic <[email protected]>
You could also allocate a new code buffer and free the old one with call_rcu. This should simplify things a lot.
Paolo
Re: [Qemu-devel] [RFC PATCH V7 16/19] translate-all: introduces tb_flush_safe.
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · On 10/08/2015 17:27, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
tb_flushis not thread safe we definitely need to exit VCPUs to do that. This introducestb_flush_safewhich just creates an async safe work which will do atb_flushlater.
Quoted 5 lines
Quoted 1 line
Signed-off-by KONRAD Frederic <[email protected]>You could also allocate a new code buffer and free the old one with
call_rcu. This should simplify things a lot.Paolo
Depending the size of the code buffer this might be a good idea. :).
Fred
Re: [Qemu-devel] [RFC PATCH V7 16/19] translate-all: introduces tb_flush_safe.
Quoted 4 lines · Frederic Konrad
Quoted 2 lines
You could also allocate a new code buffer and free the old one with
call_rcu. This should simplify things a lot.Depending the size of the code buffer this might be a good idea. :).
32 megabytes.
Paolo
RFC PATCH v7 17/19 translate-all: (wip) use tb_flush_safe when we can't alloc more tb.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
This changes just the tb_flush called from tb_alloc.
TODO:
* changes the other tb_flush.
--- translate-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
translate-all.c +1 -1
@@ -1148,7 +1148,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, tb =tb_alloc (pc);if (!tb) {/* flush must be done */ -tb_flush (cpu); +tb_flush_safe (cpu);/* cannot fail at this point */ tb =tb_alloc (pc);/* Don't forget to invalidate previous TB info. */
Signature · 1 line
1.9.0
RFC PATCH v7 18/19 mttcg: signal the associated cpu anyway.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
We might have a race here. If current_cpu is about to be set then cpu_exit won't be called and we don't exit TCG. This was probably an issue with old implementation as well.
--- cpus.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
cpus.c +1 -2
@@ -674,8 +674,7 @@ static void cpu_signal(int sig)cpu_exit (current_cpu); } -/* FIXME: We might want to check if the cpu is running? */ - tcg_thread_cpu->exit_request = true; +cpu_exit (tcg_thread_cpu); }#ifdef CONFIG_LINUX
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 18/19] mttcg: signal the associated cpu anyway.
cpus.c static void cpu_signal(int sig) · line 677
Quoted 11 lines · On 10/08/2015 17:27, [email protected] wrote:
cpus.c +1
-2@@ -674,8 +674,7 @@ static void cpu_signal(int sig)cpu_exit (current_cpu); } -/* FIXME: We might want to check if the cpu is running? */ - tcg_thread_cpu->exit_request = true; +cpu_exit (tcg_thread_cpu);
If you do this, you can remove the first "if" too, because current_cpu is always either tcg_thread_cpu or NULL.
I think it's okay to do that and squash this patch into patch 11 as well.
Paolo
RFC PATCH v7 19/19 target-arm/psci.c: wake up sleeping CPUs (MTTCG)
In reply to [email protected]From: Alex Bennée <[email protected]>
Testing with Alexander's bare metal syncronisation tests fails in MTTCG leaving one CPU spinning forever waiting for the second CPU to wake up. We simply need to poke the halt_cond once we have processed the PSCI power on call.
--- target-arm/psci.c | 2 ++ 1 file changed, 2 insertions(+)
target-arm/psci.c +2 -0
@@ -211,6 +211,8 @@ void arm_handle_psci_call(ARMCPU *cpu) } target_cpu_class->set_pc (target_cpu_state, entry); +qemu_cond_signal (target_cpu_state->halt_cond); + ret =0 ;break ;case QEMU_PSCI_0_1_FN_CPU_OFF :
Signature · 1 line
1.9.0
Re: [Qemu-devel] [RFC PATCH V7 19/19] target-arm/psci.c: wake up sleeping CPUs (MTTCG)
Quoted 18 lines · On 10/08/2015 17:27, [email protected] wrote:
From: Alex Bennée <[email protected]>
Testing with Alexander's bare metal syncronisation tests fails in MTTCG leaving one CPU spinning forever waiting for the second CPU to wake up. We simply need to poke the
halt_condonce we have processed the PSCI power on call.Tested-by Alex Bennée <[email protected]>CC Alexander Spyridakis <[email protected]>--- target-arm/psci.c | 2 ++ 1 file changed, 2 insertions(+)target-arm/psci.c +0
-0
target-arm/psci.c void arm_handle_psci_call(ARMCPU *cpu) · line 215
Quoted 5 lines
target-arm/psci.c +2
-0@@ -211,6 +211,8 @@ void arm_handle_psci_call(ARMCPU *cpu) } target_cpu_class->set_pc (target_cpu_state, entry); +qemu_cond_signal (target_cpu_state->halt_cond); +
qemu_cpu_kick, not qemu_cond_signal.
Paolo
Quoted 4 lines
target-arm/psci.c +0
-0ret =0 ;break ;case QEMU_PSCI_0_1_FN_CPU_OFF :
Alex Bennée
Tested-byRe: [Qemu-devel] [RFC PATCH V7 19/19] target-arm/psci.c: wake up sleeping CPUs (MTTCG)
Quoted 22 lines · Paolo Bonzini
Quoted 21 lines · On 10/08/2015 17:27, [email protected] wrote:
From: Alex Bennée <[email protected]>
Testing with Alexander's bare metal syncronisation tests fails in MTTCG leaving one CPU spinning forever waiting for the second CPU to wake up. We simply need to poke the
halt_condonce we have processed the PSCI power on call.Tested-by Alex Bennée <[email protected]>CC Alexander Spyridakis <[email protected]>--- target-arm/psci.c | 2 ++ 1 file changed, 2 insertions(+)target-arm/psci.c +0
-0@@ -211,6 +211,8 @@ void arm_handle_psci_call(ARMCPU *cpu) } target_cpu_class->set_pc (target_cpu_state, entry);
Quoted 4 lines
target-arm/psci.c void arm_handle_psci_call(ARMCPU *cpu) · line 215
Quoted 2 lines
target-arm/psci.c +2
-0+qemu_cond_signal (target_cpu_state->halt_cond); +
qemu_cpu_kick, notqemu_cond_signal.
I did a v2 in my branch and didn't realise Fred had picked it up into his tree. I hadn't sent it up list because it didn't seem to be worth it for non-MTTCG builds. I can send it if you want?
Fred,
Use the attached on your next rebase.
Quoted 1 line
From
bb8aabadc0880a21bfe5821af172c047474841d6Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <[email protected]>
Date: Tue, 7 Jul 2015 08:28:05 +0100
Subject: [PATCH] target-arm/psci.c: wake up sleeping CPUs (MTTCG)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Testing with Alexander's bare metal syncronisation tests fails in MTTCG leaving one CPU spinning forever waiting for the second CPU to wake up. We simply need to poke the halt_cond once we have processed the PSCI power on call.
---
v2
- use qemu_cpu_kick()
--- target-arm/psci.c | 2 ++ 1 file changed, 2 insertions(+)
target-arm/psci.c +2 -0
@@ -211,6 +211,8 @@ void arm_handle_psci_call(ARMCPU *cpu) } target_cpu_class->set_pc (target_cpu_state, entry); +qemu_cpu_kick (target_cpu_state); + ret =0 ;break ;case QEMU_PSCI_0_1_FN_CPU_OFF :
Signature · 1 line
2.5.0
Quoted 9 lines
Paolo
Quoted 4 lines
target-arm/psci.c +0
-0ret =0 ;break ;case QEMU_PSCI_0_1_FN_CPU_OFF :
Signature · 1 line
Alex Bennée
Quoted 7 lines · [email protected] writes:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.
I'm not seeing this yet, did you remember to push?
Quoted 181 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with theasync_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores). For now this case is not correctly handled. The existing atomic patch will attempt to execute the 64 bit cmpxchg functionality in a non thread safe fashion. Our intention is to provide a new multi-thread ARM atomic patch for 64bit ARM on effective 32bit hosts.
This atomic instruction part has been tested with Alexander's atomic stress repo available here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.htmlThe execution is a little slower than upstream probably because of the different VCPU fight for the mutex. Swaping
arm_exclusive_lockfrom mutex tospin_lockreduce considerably the difference.== Testing ==
A simple double dhrystone test in SMP 2 with vexpress-a15 in a linux guest show a good performance progression: it takes basically 18s upstream to complete vs 10s with MTTCG.
Testing image is available here:
https://cloud.greensocs.com/index.php/s/CfHSLzDH5pmTkW3Then simply:
./configure --target-list=arm-softmmu
make -j8
./arm-softmmu/qemu-system-arm -M vexpress-a15 -smp 2 -kernel zImage -initrd rootfs.ext2 -dtb vexpress-v2p-ca15-tc1.dtb --nographic --append "console=ttyAMA0"login: root
The dhrystone command is the last one in the history. "dhrystone 10000000 & dhrystone 10000000"
The atomic spinlock benchmark from Alexander shows that atomic basically work. Just follow the instruction here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html== Known issues ==
* GDB stub:
GDB stub is not tested right now it will probably requires some changes to work.* deadlock on exit:
When exiting QEMU Ctrl-C some VCPU's thread are not able to exit and continue execution.
http://git.greensocs.com/fkonrad/mttcg/issues/1*
memory_region_rom_device_set_romdfrom pflash01 just crashes the TCG code. Strangely this happen only with "-smp 4" and 2 in the DTB.
http://git.greensocs.com/fkonrad/mttcg/issues/2Changes V6 -> V7:
*global_lock:
* Don't protect softmmu read/write helper as it's now done inadress_space_rw.
*tcg_exec_flag:
* Make the flag atomically test and set through an API.
* introduceasync_safe_work:
* moveqemu_cpu_kick_threadto avoid prototype declaration.
* use thework_mutex.
*async_work:
* protect it with a mutex (work_mutex) against concurent access.
*tb_lock:
* protecttcg_malloc_internalas well.
* signal the VCPU even ofcurrent_cpuis NULL.
* added PSCI patch.
* rebased on v2.4.0-rc0 (6169b60285fe1ff730d840a49527e721bfb30899).Changes V5 -> V6:
* Introduceasync_safe_workto do thetb_flushand some part oftb_invalidate.
* Introduce QemuSpin from Guillaume which allow a faster atomic instruction (6s to pass Alexander's atomic test instead of 30s before).
* Don't taketb_lockbeforetb_find_fast.
* Handletb_flushwithasync_safe_work.
* Handletb_invalidatewithasync_workandasync_safe_work.
* Drop thetlb_flush_requestmechanism and useasync_workas well.
* Fix the wrong lenght in atomic patch.
* Fix the wrong return address for exception in atomic patch.Alex Bennée (1):
target-arm/psci.c: wake up sleeping CPUs (MTTCG)Guillaume Delbergue (1):
add support for spin lock on POSIX systems exclusivelyKONRAD Frederic (17):
cpus: protect queued_work_* withwork_mutex.
cpus: addtcg_exec_flag.
cpus: introduceasync_run_safe_work_on_cpu.
replace spinlock by QemuMutex.
remove unused spinlock.
protect TBContext withtb_lock.
tcg: removetcg_halt_condglobal variable.
Drop global lock during TCG code execution
cpu: removeexit_requestglobal.
tcg: switch on multithread.
Use atomic cmpxchg to atomically check the exclusive value in a STREX add a callback whentb_invalidateis called.
cpu: introduce tlb_flush*_all.
arm: use tlb_flush*_all
translate-all: introducestb_flush_safe.
translate-all: (wip) usetb_flush_safewhen we can't alloc more tb.
mttcg: signal the associated cpu anyway.cpu-exec.c | 98 +++++++++------ cpus.c | 295 +++++++++++++++++++++++++------------------- cputlb.c | 81 ++++++++++++ include/exec/exec-all.h | 8 +- include/exec/spinlock.h | 49 -------- include/qemu/thread-posix.h | 4 + include/qemu/thread-win32.h | 4 + include/qemu/thread.h | 7 ++ include/qom/cpu.h | 57 +++++++++ linux-user/main.c | 6 +- qom/cpu.c | 20 +++ target-arm/cpu.c | 21 ++++ target-arm/cpu.h | 6 + target-arm/helper.c | 58 +++------ target-arm/helper.h | 4 + target-arm/op_helper.c | 128 ++++++++++++++++++- target-arm/psci.c | 2 + target-arm/translate.c | 101 +++------------ target-i386/mem_helper.c | 16 ++- target-i386/misc_helper.c | 27 +++- tcg/i386/tcg-target.c | 8 ++ tcg/tcg.h | 14 ++- translate-all.c | 217 +++++++++++++++++++++++++++----- util/qemu-thread-posix.c | 45 +++++++ util/qemu-thread-win32.c | 30 +++++ vl.c | 6 + 26 files changed, 934 insertions(+), 378 deletions(-) delete mode 100644 include/exec/spinlock.h
Signature · 1 line
Alex Bennée
Quoted 10 lines · Alex Bennée
Quoted 7 lines · [email protected] writes:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.I'm not seeing this yet, did you remember to push?
oops sorry done!
Quoted 181 lines
Quoted 181 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with theasync_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores). For now this case is not correctly handled. The existing atomic patch will attempt to execute the 64 bit cmpxchg functionality in a non thread safe fashion. Our intention is to provide a new multi-thread ARM atomic patch for 64bit ARM on effective 32bit hosts.
This atomic instruction part has been tested with Alexander's atomic stress repo available here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.htmlThe execution is a little slower than upstream probably because of the different VCPU fight for the mutex. Swaping
arm_exclusive_lockfrom mutex tospin_lockreduce considerably the difference.== Testing ==
A simple double dhrystone test in SMP 2 with vexpress-a15 in a linux guest show a good performance progression: it takes basically 18s upstream to complete vs 10s with MTTCG.
Testing image is available here:
https://cloud.greensocs.com/index.php/s/CfHSLzDH5pmTkW3Then simply:
./configure --target-list=arm-softmmu
make -j8
./arm-softmmu/qemu-system-arm -M vexpress-a15 -smp 2 -kernel zImage -initrd rootfs.ext2 -dtb vexpress-v2p-ca15-tc1.dtb --nographic --append "console=ttyAMA0"login: root
The dhrystone command is the last one in the history. "dhrystone 10000000 & dhrystone 10000000"
The atomic spinlock benchmark from Alexander shows that atomic basically work. Just follow the instruction here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html== Known issues ==
* GDB stub:
GDB stub is not tested right now it will probably requires some changes to work.* deadlock on exit:
When exiting QEMU Ctrl-C some VCPU's thread are not able to exit and continue execution.
http://git.greensocs.com/fkonrad/mttcg/issues/1*
memory_region_rom_device_set_romdfrom pflash01 just crashes the TCG code. Strangely this happen only with "-smp 4" and 2 in the DTB.
http://git.greensocs.com/fkonrad/mttcg/issues/2Changes V6 -> V7:
*global_lock:
* Don't protect softmmu read/write helper as it's now done inadress_space_rw.
*tcg_exec_flag:
* Make the flag atomically test and set through an API.
* introduceasync_safe_work:
* moveqemu_cpu_kick_threadto avoid prototype declaration.
* use thework_mutex.
*async_work:
* protect it with a mutex (work_mutex) against concurent access.
*tb_lock:
* protecttcg_malloc_internalas well.
* signal the VCPU even ofcurrent_cpuis NULL.
* added PSCI patch.
* rebased on v2.4.0-rc0 (6169b60285fe1ff730d840a49527e721bfb30899).Changes V5 -> V6:
* Introduceasync_safe_workto do thetb_flushand some part oftb_invalidate.
* Introduce QemuSpin from Guillaume which allow a faster atomic instruction (6s to pass Alexander's atomic test instead of 30s before).
* Don't taketb_lockbeforetb_find_fast.
* Handletb_flushwithasync_safe_work.
* Handletb_invalidatewithasync_workandasync_safe_work.
* Drop thetlb_flush_requestmechanism and useasync_workas well.
* Fix the wrong lenght in atomic patch.
* Fix the wrong return address for exception in atomic patch.Alex Bennée (1):
target-arm/psci.c: wake up sleeping CPUs (MTTCG)Guillaume Delbergue (1):
add support for spin lock on POSIX systems exclusivelyKONRAD Frederic (17):
cpus: protect queued_work_* withwork_mutex.
cpus: addtcg_exec_flag.
cpus: introduceasync_run_safe_work_on_cpu.
replace spinlock by QemuMutex.
remove unused spinlock.
protect TBContext withtb_lock.
tcg: removetcg_halt_condglobal variable.
Drop global lock during TCG code execution
cpu: removeexit_requestglobal.
tcg: switch on multithread.
Use atomic cmpxchg to atomically check the exclusive value in a STREX add a callback whentb_invalidateis called.
cpu: introduce tlb_flush*_all.
arm: use tlb_flush*_all
translate-all: introducestb_flush_safe.
translate-all: (wip) usetb_flush_safewhen we can't alloc more tb.
mttcg: signal the associated cpu anyway.cpu-exec.c | 98 +++++++++------ cpus.c | 295 +++++++++++++++++++++++++------------------- cputlb.c | 81 ++++++++++++ include/exec/exec-all.h | 8 +- include/exec/spinlock.h | 49 -------- include/qemu/thread-posix.h | 4 + include/qemu/thread-win32.h | 4 + include/qemu/thread.h | 7 ++ include/qom/cpu.h | 57 +++++++++ linux-user/main.c | 6 +- qom/cpu.c | 20 +++ target-arm/cpu.c | 21 ++++ target-arm/cpu.h | 6 + target-arm/helper.c | 58 +++------ target-arm/helper.h | 4 + target-arm/op_helper.c | 128 ++++++++++++++++++- target-arm/psci.c | 2 + target-arm/translate.c | 101 +++------------ target-i386/mem_helper.c | 16 ++- target-i386/misc_helper.c | 27 +++- tcg/i386/tcg-target.c | 8 ++ tcg/tcg.h | 14 ++- translate-all.c | 217 +++++++++++++++++++++++++++----- util/qemu-thread-posix.c | 45 +++++++ util/qemu-thread-win32.c | 30 +++++ vl.c | 6 + 26 files changed, 934 insertions(+), 378 deletions(-)delete mode 100644
include/exec/spinlock.h
Quoted 8 lines · On Mon, 2015-08-10 at 17:26 +0200, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.
Quoted 8 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.
Can't you just make it a TLS ?
Quoted 5 lines
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the
async_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.
How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.
Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.
What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Quoted 6 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
Cheers,
Ben.
Quoted 12 lines · Benjamin Herrenschmidt
Quoted 11 lines · On Mon, 2015-08-10 at 17:26 +0200, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
Quoted 6 lines
Quoted 5 lines
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.Can't you just make it a TLS ?
True that can be done as well. But the tcg_exec_flags has a second meaning saying "you can't start executing code right now because I want to do a safe_work".
Quoted 10 lines
Quoted 6 lines
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the
async_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
For the moment we are trying to do the first case.
Quoted 44 lines
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Quoted 3 lines
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Yes that's what the safe_work is doing. Ask everybody to exit prevent VCPUs to resume (tcg_exec_flag) and do the work when everybody is outside cpu-exec.
Quoted 7 lines
Quoted 7 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Quoted 5 lines
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
VOS did a "Slow path for atomic instruction translation" series you can find here:
https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg00971.html
Which will be used in the end.
Thanks,
Fred
Quoted 6 lines
Cheers,
Ben.
Hello Frederic,
Quoted 9 lines · Frederic Konrad
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 7 lines · On Mon, 2015-08-10 at 17:26 +0200, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.
would it be possible to rebase on latest qemu? I wonder if mttcg is diverging a bit too much from mainline, which will make it more difficult to rebase later..(Or did I get confused about all these repos?)
Thank you!
Claudio
Quoted 97 lines
Quoted 10 lines
Quoted 9 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.Can't you just make it a TLS ?
True that can be done as well. But the
tcg_exec_flagshas a second meaning saying "you can't start executing code right now because I want to do asafe_work".Quoted 10 lines
Quoted 6 lines
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the
async_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
For the moment we are trying to do the first case.
Quoted 44 lines
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Quoted 3 lines
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Yes that's what the
safe_workis doing. Ask everybody to exit prevent VCPUs to resume (tcg_exec_flag) and do the work when everybody is outside cpu-exec.Quoted 7 lines
Quoted 7 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Quoted 5 lines
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
VOS did a "Slow path for atomic instruction translation" series you can find here:
https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg00971.htmlWhich will be used in the end.
Thanks,
FredQuoted 3 lines
Cheers,
Ben.
Hi Claudio,
I'll rebase soon tomorrow with a bit of luck ;).
Thanks,
Fred
Quoted 109 lines · Claudio Fontana
Hello Frederic,
Quoted 9 lines · Frederic Konrad
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 7 lines · On Mon, 2015-08-10 at 17:26 +0200, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.would it be possible to rebase on latest qemu? I wonder if mttcg is diverging a bit too much from mainline, which will make it more difficult to rebase later..(Or did I get confused about all these repos?)
Thank you!
Claudio
Quoted 90 lines
Quoted 9 lines
Quoted 8 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.Can't you just make it a TLS ?
True that can be done as well. But the
tcg_exec_flagshas a second meaning saying "you can't start executing code right now because I want to do asafe_work".Quoted 9 lines
Quoted 5 lines
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the
async_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
For the moment we are trying to do the first case.
Quoted 43 lines
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Quoted 3 lines
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Yes that's what the
safe_workis doing. Ask everybody to exit prevent VCPUs to resume (tcg_exec_flag) and do the work when everybody is outside cpu-exec.Quoted 11 lines
Quoted 6 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
VOS did a "Slow path for atomic instruction translation" series you can find here:
https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg00971.htmlWhich will be used in the end.
Thanks,
FredQuoted 2 lines
Cheers,
Ben.
Quoted 6 lines · Frederic Konrad
Hi Claudio,
I'll rebase soon tomorrow with a bit of luck ;).
Thanks,
Fred
a respectful ping on this one :-)
I am looking at http://git.greensocs.com/fkonrad/mttcg.git
branch multi_tcg_v7_bugfixed,
is there something new?
Ciao,
Claudio
Quoted 112 lines · Claudio Fontana
Quoted 109 lines
Hello Frederic,
Quoted 9 lines · Frederic Konrad
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 7 lines · On Mon, 2015-08-10 at 17:26 +0200, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.would it be possible to rebase on latest qemu? I wonder if mttcg is diverging a bit too much from mainline, which will make it more difficult to rebase later..(Or did I get confused about all these repos?)
Thank you!
Claudio
Quoted 90 lines
Quoted 9 lines
Quoted 8 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.Can't you just make it a TLS ?
True that can be done as well. But the
tcg_exec_flagshas a second meaning saying "you can't start executing code right now because I want to do asafe_work".Quoted 9 lines
Quoted 5 lines
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the
async_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
For the moment we are trying to do the first case.
Quoted 43 lines
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Quoted 3 lines
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Yes that's what the
safe_workis doing. Ask everybody to exit prevent VCPUs to resume (tcg_exec_flag) and do the work when everybody is outside cpu-exec.Quoted 11 lines
Quoted 6 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
VOS did a "Slow path for atomic instruction translation" series you can find here:
https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg00971.htmlWhich will be used in the end.
Thanks,
FredQuoted 2 lines
Cheers,
Ben.
Signature · 4 lines
Claudio Fontana Server Virtualization Architect Huawei Technologies Duesseldorf GmbH Riesstraße 25 - 80992 München
Quoted 26 lines · Benjamin Herrenschmidt
Quoted 8 lines · On Mon, 2015-08-10 at 17:26 +0200, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.Quoted 8 lines
This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.Can't you just make it a TLS ?
Quoted 5 lines
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with the
async_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.
Quoted 4 lines
How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
This is the next chunk of work. We have Alvise's LL/SC patches which allow us to do proper emulation of ARMs Load/store exclusive behaviour and any weak order target will have to use such constructs.
Currently the plan is to introduce a barrier TCG op which will translate to the strongest backend barrier available. Even x86 should be using barriers to ensure cross-core visibility which then leaves LS re-ordering on the same core.
Quoted 63 lines
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Quoted 6 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
Cheers,
Ben.
Signature · 1 line
Alex Bennée
Quoted 9 lines · Alex Bennée
Quoted 5 lines
How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
This is the next chunk of work. We have Alvise's LL/SC patches which allow us to do proper emulation of ARMs Load/store exclusive behaviour and any weak order target will have to use such constructs.
God no ! You don't want to use ll/sc for dealing with weak ordering, you want to use barriers... ll/sc will allow you to deal with front-end things such as atomic inc/dec etc...
Quoted 2 lines
Currently the plan is to introduce a barrier TCG op which will translate to the strongest backend barrier available.
I would advocate at least two barriers, full barrier and write barrier, so at least when emulating ARM or PPC on x86, we don't actually send fences on every load/stores.
IE. the x86 memory model is *not* fully ordered, so a ARM or PPC full barrier must translate into a x86 fence afaik (or whatever is the x86 name of its full barrier), but you don't want to translate all ARM/PPC weaker barriers into those.
Quoted 2 lines
Even x86 should be using barriers to ensure cross-core visibility which
then leaves LS re-ordering on the same core.
Only for store + load, which is afaik the only case where x86 re-orders.
But in any case, expose to the target (TGC target) the ordering expectations of the source so that we can use whatever facilities might be at hand to avoid some of those barriers, for example the SAO mapping attribute I mentioned.
I'll try to look at your patch more closely when I get a chance and see if I can produce a ppc target but don't hold your breath, I'm a bit swamped at the moment.
Ben.
Quoted 63 lines
Quoted 63 lines
At least on POWER7 and later on PPC we have the possibility of setting the attribute "Strong Access Ordering" with mremap/mprotect (I dont' remember which one) which gives us x86-like memory semantics...
I don't know if ARM supports something similar. On the other hand, when emulating ARM on PPC or vice-versa, we can probably get away with no barriers.
Do you expose some kind of guest memory model info to the TCG backend so it can decide how to handle these things ?
Quoted 19 lines
== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Quoted 7 lines
Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.What about the host MMU emulation ? Is that multithreaded ? It has potential issues when doing things like dirty bit updates into guest memory, those need to be done atomically. Also TLB invalidations on ARM and PPC are global, so they will need to invalidate the remote SW TLBs as well.
Do you have a mechanism to synchronize with another thread ? IE, make it pop out of TCG if already in and prevent it from getting in ? That way you can "remotely" invalidate its TLB...
Quoted 6 lines
== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores).
Right, on the other hand 64-bit will do fine. But then x86 has 2-value atomics nowadays, doesn't it ? And that will be hard to emulate on anything. You might need to have some kind of global hashed lock list used by atomics (hash the physical address) as a fallback if you don't have a 1:1 match between host and guest capabilities.
Cheers,
Ben.
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 6 lines · Alex Bennée
Quoted 5 lines
How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
Quoted 7 lines
Quoted 3 lines
This is the next chunk of work. We have Alvise's LL/SC patches which allow us to do proper emulation of ARMs Load/store exclusive behaviour and any weak order target will have to use such constructs.
God no ! You don't want to use ll/sc for dealing with weak ordering, you want to use barriers... ll/sc will allow you to deal with front-end things such as atomic inc/dec etc...
Agreed. ll/sc is atomicity, not ordering. (Conversely, barriers and load-acquire/store-release are ordering, not atomicity...)
Quoted 6 lines
Quoted 2 lines
Currently the plan is to introduce a barrier TCG op which will translate to the strongest backend barrier available.
I would advocate at least two barriers, full barrier and write barrier, so at least when emulating ARM or PPC on x86, we don't actually send fences on every load/stores.
Is it possible in some of these combinations to use the load-acquire and store-release instructions rather than explicit barriers? (ARMv8 has those, which I think should be slightly less heavyweight than explicit barriers everywhere, if the semantics line up.)
thanks
-- PMM
Quoted 4 lines · Peter Maydell
Is it possible in some of these combinations to use the load-acquire and store-release instructions rather than explicit barriers? (ARMv8 has those, which I think should be slightly less heavyweight than explicit barriers everywhere, if the semantics line up.)
Possibly though I suppose starting with a simple barrier and then adding more fine-grained ones to TCG might be a workable path. IE, it shouldn't be too hard.
The problem of course is that not 2 architecture have exactly the same semantics :-)
Cheers,
Ben.
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 6 lines · Alex Bennée
Quoted 5 lines
How do you handle the memory model ? IE , ARM and PPC are OO while x86 is (mostly) in order, so emulating ARM/PPC on x86 is fine but emulating x86 on ARM or PPC will lead to problems unless you generate memory barriers with every load/store ..
Quoted 7 lines
Quoted 3 lines
This is the next chunk of work. We have Alvise's LL/SC patches which allow us to do proper emulation of ARMs Load/store exclusive behaviour and any weak order target will have to use such constructs.
God no ! You don't want to use ll/sc for dealing with weak ordering, you want to use barriers... ll/sc will allow you to deal with front-end things such as atomic inc/dec etc...
Sorry I wasn't clear - ll/sc is required to properly support weak ordered system atomic-like ops. So while it doesn't offer guarantees on memory ordering it does ensure you can safely do atomic operations.
Quoted 6 lines
Quoted 2 lines
Currently the plan is to introduce a barrier TCG op which will translate to the strongest backend barrier available.
I would advocate at least two barriers, full barrier and write barrier, so at least when emulating ARM or PPC on x86, we don't actually send fences on every load/stores.
I was considering finer grained barriers as an optimisation step.
Quoted 11 lines
IE. the x86 memory model is *not* fully ordered, so a ARM or PPC full barrier must translate into a x86 fence afaik (or whatever is the x86 name of its full barrier), but you don't want to translate all ARM/PPC weaker barriers into those.
Quoted 2 lines
Even x86 should be using barriers to ensure cross-core visibility which
then leaves LS re-ordering on the same core.Only for store + load, which is afaik the only case where x86 re-orders.
To be clear this is non-dependant stores. A store to an address that is then loaded in the same CPU shouldn't overtake the load.
I note that ARM, PPC and SPARC RMO are all broadly similar in their models. Alpha seems to be the funky one but as we don't have a backend for it I think we are OK.
Quoted 8 lines
But in any case, expose to the target (TGC target) the ordering expectations of the source so that we can use whatever facilities might be at hand to avoid some of those barriers, for example the SAO mapping attribute I mentioned.
I'll try to look at your patch more closely when I get a chance and see if I can produce a ppc target but don't hold your breath, I'm a bit swamped at the moment.
We haven't done anything on barriers yet. I've mostly been concentrating writing up the test cases to demonstrate the failures but given the x86 backend it is surprisingly hard to come up with a test case that will fail. I suspect I need to port my ARM tests to x86 and run on the ARM backend.
Signature · 1 line
Alex Bennée
Quoted 3 lines · On 10/08/2015 17:26, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
Here is a list of issues that I found:
- tb_lock usage in tb_find_fast is complicated and introduces the need for other complicated code such as the tb_invalidate callback. Instead, the tb locking should reuse the cpu-exec.c code for user-mode emulation, with additional locking in the spots identified by Fred.
- tb_lock uses a recursive lock, but this is not necessary. Did I ever say I dislike recursive mutexes? :) The wrappers tb_lock()/tb_unlock()/tb_lock_reset() can catch recursive locking for us, so it's not hard to do without it.
- code_bitmap is not protected by any mutex (tb_invalidate_phys_page_fast is called with the iothread mutex taken, but other users of code_bitmap do not use it). Writes should be protected by the tb_lock, reads by either tb_lock or RCU.
- memory barriers are probably requested around accesses to ->exit_request. ->thread_kicked also needs to be accessed with atomics, because async_run_{,safe_}on_cpu can be called outside the big QEMU lock.
- the whole signal-based qemu_cpu_kick can just go away. Just setting tcg_exit_req and exit_request will kick the TCG thread. The hairy Win32 SuspendThread/ResumeThread goes away too. I suggest doing it now, because proving it unnecessary is easier than proving it correct.
- user-mode emulation is broken (does not compile)
- the big QEMU lock is not taken anywhere for MMIO accesses that require it (i.e. basically all of them)
- some code wants to be called _outside_ the big QEMU lock, for example because it longjmps back to cpu_exec. For example, I suspect that the notdirty callbacks must be marked with memory_region_clear_global_locking.
I've started looking at them (and documenting the locking conventions for functions), and I hope to post it to some git repo later this week.
Paolo
Quoted 185 lines
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with theasync_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores). For now this case is not correctly handled. The existing atomic patch will attempt to execute the 64 bit cmpxchg functionality in a non thread safe fashion. Our intention is to provide a new multi-thread ARM atomic patch for 64bit ARM on effective 32bit hosts.
This atomic instruction part has been tested with Alexander's atomic stress repo available here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.htmlThe execution is a little slower than upstream probably because of the different VCPU fight for the mutex. Swaping
arm_exclusive_lockfrom mutex tospin_lockreduce considerably the difference.== Testing ==
A simple double dhrystone test in SMP 2 with vexpress-a15 in a linux guest show a good performance progression: it takes basically 18s upstream to complete vs 10s with MTTCG.
Testing image is available here:
https://cloud.greensocs.com/index.php/s/CfHSLzDH5pmTkW3Then simply:
./configure --target-list=arm-softmmu
make -j8
./arm-softmmu/qemu-system-arm -M vexpress-a15 -smp 2 -kernel zImage -initrd rootfs.ext2 -dtb vexpress-v2p-ca15-tc1.dtb --nographic --append "console=ttyAMA0"login: root
The dhrystone command is the last one in the history. "dhrystone 10000000 & dhrystone 10000000"
The atomic spinlock benchmark from Alexander shows that atomic basically work. Just follow the instruction here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html== Known issues ==
* GDB stub:
GDB stub is not tested right now it will probably requires some changes to work.* deadlock on exit:
When exiting QEMU Ctrl-C some VCPU's thread are not able to exit and continue execution.
http://git.greensocs.com/fkonrad/mttcg/issues/1*
memory_region_rom_device_set_romdfrom pflash01 just crashes the TCG code. Strangely this happen only with "-smp 4" and 2 in the DTB.
http://git.greensocs.com/fkonrad/mttcg/issues/2Changes V6 -> V7:
*global_lock:
* Don't protect softmmu read/write helper as it's now done inadress_space_rw.
*tcg_exec_flag:
* Make the flag atomically test and set through an API.
* introduceasync_safe_work:
* moveqemu_cpu_kick_threadto avoid prototype declaration.
* use thework_mutex.
*async_work:
* protect it with a mutex (work_mutex) against concurent access.
*tb_lock:
* protecttcg_malloc_internalas well.
* signal the VCPU even ofcurrent_cpuis NULL.
* added PSCI patch.
* rebased on v2.4.0-rc0 (6169b60285fe1ff730d840a49527e721bfb30899).Changes V5 -> V6:
* Introduceasync_safe_workto do thetb_flushand some part oftb_invalidate.
* Introduce QemuSpin from Guillaume which allow a faster atomic instruction (6s to pass Alexander's atomic test instead of 30s before).
* Don't taketb_lockbeforetb_find_fast.
* Handletb_flushwithasync_safe_work.
* Handletb_invalidatewithasync_workandasync_safe_work.
* Drop thetlb_flush_requestmechanism and useasync_workas well.
* Fix the wrong lenght in atomic patch.
* Fix the wrong return address for exception in atomic patch.Alex Bennée (1):
target-arm/psci.c: wake up sleeping CPUs (MTTCG)Guillaume Delbergue (1):
add support for spin lock on POSIX systems exclusivelyKONRAD Frederic (17):
cpus: protect queued_work_* withwork_mutex.
cpus: addtcg_exec_flag.
cpus: introduceasync_run_safe_work_on_cpu.
replace spinlock by QemuMutex.
remove unused spinlock.
protect TBContext withtb_lock.
tcg: removetcg_halt_condglobal variable.
Drop global lock during TCG code execution
cpu: removeexit_requestglobal.
tcg: switch on multithread.
Use atomic cmpxchg to atomically check the exclusive value in a STREX add a callback whentb_invalidateis called.
cpu: introduce tlb_flush*_all.
arm: use tlb_flush*_all
translate-all: introducestb_flush_safe.
translate-all: (wip) usetb_flush_safewhen we can't alloc more tb.
mttcg: signal the associated cpu anyway.cpu-exec.c | 98 +++++++++------ cpus.c | 295 +++++++++++++++++++++++++------------------- cputlb.c | 81 ++++++++++++ include/exec/exec-all.h | 8 +- include/exec/spinlock.h | 49 -------- include/qemu/thread-posix.h | 4 + include/qemu/thread-win32.h | 4 + include/qemu/thread.h | 7 ++ include/qom/cpu.h | 57 +++++++++ linux-user/main.c | 6 +- qom/cpu.c | 20 +++ target-arm/cpu.c | 21 ++++ target-arm/cpu.h | 6 + target-arm/helper.c | 58 +++------ target-arm/helper.h | 4 + target-arm/op_helper.c | 128 ++++++++++++++++++- target-arm/psci.c | 2 + target-arm/translate.c | 101 +++------------ target-i386/mem_helper.c | 16 ++- target-i386/misc_helper.c | 27 +++- tcg/i386/tcg-target.c | 8 ++ tcg/tcg.h | 14 ++- translate-all.c | 217 +++++++++++++++++++++++++++----- util/qemu-thread-posix.c | 45 +++++++ util/qemu-thread-win32.c | 30 +++++ vl.c | 6 + 26 files changed, 934 insertions(+), 378 deletions(-) delete mode 100644 include/exec/spinlock.h
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · On 10/08/2015 17:26, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
Thanks to look at this.
Quoted 6 lines
Here is a list of issues that I found:
-
tb_lockusage intb_find_fastis complicated and introduces the need for other complicated code such as thetb_invalidatecallback. Instead, the tb locking should reuse thecpu-exec.ccode for user-mode emulation, with additional locking in the spots identified by Fred.
The reason for this is that locking around tb_find_fast just kills the performance.
Quoted 5 lines
-
tb_lockuses a recursive lock, but this is not necessary. Did I ever say I dislike recursive mutexes? :) The wrappers tb_lock()/tb_unlock()/tb_lock_reset() can catch recursive locking for us, so it's not hard to do without it.
True this is definitely not nice but seems some code eg: tb_invalidate are called from different place with or without the tb_lock. We might probably be able to lock the caller.
Quoted 5 lines
-
code_bitmapis not protected by any mutex (tb_invalidate_phys_page_fastis called with the iothread mutex taken, but other users ofcode_bitmapdo not use it). Writes should be protected by thetb_lock, reads by eithertb_lockor RCU.
Yes good point I missed this one.
Quoted 8 lines
- memory barriers are probably requested around accesses to ->exit_request. ->thread_kicked also needs to be accessed with atomics, because async_run_{,safe_}on_cpu can be called outside the big QEMU lock.
- the whole signal-based
qemu_cpu_kickcan just go away. Just settingtcg_exit_reqandexit_requestwill kick the TCG thread. The hairy Win32 SuspendThread/ResumeThread goes away too. I suggest doing it now, because proving it unnecessary is easier than proving it correct.
Just setting tcg_exit_req and exit_request and signal the cpu->halt_cond I guess?
Quoted 4 lines
- user-mode emulation is broken (does not compile)
- the big QEMU lock is not taken anywhere for MMIO accesses that require it (i.e. basically all of them)
Isn't that handled by prepare_mmio_access?
Quoted 4 lines
- some code wants to be called _outside_ the big QEMU lock, for example because it longjmps back to
cpu_exec. For example, I suspect that the notdirty callbacks must be marked withmemory_region_clear_global_locking.
Fred
Quoted 190 lines
I've started looking at them (and documenting the locking conventions for functions), and I hope to post it to some git repo later this week.
Paolo
Quoted 184 lines
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag is added to the CPUState.
* We need to run some work safely when all VCPUs are outside their execution loop. This is done with theasync_run_safe_work_on_cpufunction introduced in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a faster handling of atomic instruction.== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores). For now this case is not correctly handled. The existing atomic patch will attempt to execute the 64 bit cmpxchg functionality in a non thread safe fashion. Our intention is to provide a new multi-thread ARM atomic patch for 64bit ARM on effective 32bit hosts.
This atomic instruction part has been tested with Alexander's atomic stress repo available here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.htmlThe execution is a little slower than upstream probably because of the different VCPU fight for the mutex. Swaping
arm_exclusive_lockfrom mutex tospin_lockreduce considerably the difference.== Testing ==
A simple double dhrystone test in SMP 2 with vexpress-a15 in a linux guest show a good performance progression: it takes basically 18s upstream to complete vs 10s with MTTCG.
Testing image is available here:
https://cloud.greensocs.com/index.php/s/CfHSLzDH5pmTkW3Then simply:
./configure --target-list=arm-softmmu
make -j8
./arm-softmmu/qemu-system-arm -M vexpress-a15 -smp 2 -kernel zImage -initrd rootfs.ext2 -dtb vexpress-v2p-ca15-tc1.dtb --nographic --append "console=ttyAMA0"login: root
The dhrystone command is the last one in the history. "dhrystone 10000000 & dhrystone 10000000"
The atomic spinlock benchmark from Alexander shows that atomic basically work. Just follow the instruction here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html== Known issues ==
* GDB stub:
GDB stub is not tested right now it will probably requires some changes to work.* deadlock on exit:
When exiting QEMU Ctrl-C some VCPU's thread are not able to exit and continue execution.
http://git.greensocs.com/fkonrad/mttcg/issues/1*
memory_region_rom_device_set_romdfrom pflash01 just crashes the TCG code. Strangely this happen only with "-smp 4" and 2 in the DTB.
http://git.greensocs.com/fkonrad/mttcg/issues/2Changes V6 -> V7:
*global_lock:
* Don't protect softmmu read/write helper as it's now done inadress_space_rw.
*tcg_exec_flag:
* Make the flag atomically test and set through an API.
* introduceasync_safe_work:
* moveqemu_cpu_kick_threadto avoid prototype declaration.
* use thework_mutex.
*async_work:
* protect it with a mutex (work_mutex) against concurent access.
*tb_lock:
* protecttcg_malloc_internalas well.
* signal the VCPU even ofcurrent_cpuis NULL.
* added PSCI patch.
* rebased on v2.4.0-rc0 (6169b60285fe1ff730d840a49527e721bfb30899).Changes V5 -> V6:
* Introduceasync_safe_workto do thetb_flushand some part oftb_invalidate.
* Introduce QemuSpin from Guillaume which allow a faster atomic instruction (6s to pass Alexander's atomic test instead of 30s before).
* Don't taketb_lockbeforetb_find_fast.
* Handletb_flushwithasync_safe_work.
* Handletb_invalidatewithasync_workandasync_safe_work.
* Drop thetlb_flush_requestmechanism and useasync_workas well.
* Fix the wrong lenght in atomic patch.
* Fix the wrong return address for exception in atomic patch.Alex Bennée (1):
target-arm/psci.c: wake up sleeping CPUs (MTTCG)Guillaume Delbergue (1):
add support for spin lock on POSIX systems exclusivelyKONRAD Frederic (17):
cpus: protect queued_work_* withwork_mutex.
cpus: addtcg_exec_flag.
cpus: introduceasync_run_safe_work_on_cpu.
replace spinlock by QemuMutex.
remove unused spinlock.
protect TBContext withtb_lock.
tcg: removetcg_halt_condglobal variable.
Drop global lock during TCG code execution
cpu: removeexit_requestglobal.
tcg: switch on multithread.
Use atomic cmpxchg to atomically check the exclusive value in a STREX add a callback whentb_invalidateis called.
cpu: introduce tlb_flush*_all.
arm: use tlb_flush*_all
translate-all: introducestb_flush_safe.
translate-all: (wip) usetb_flush_safewhen we can't alloc more tb.
mttcg: signal the associated cpu anyway.cpu-exec.c | 98 +++++++++------ cpus.c | 295 +++++++++++++++++++++++++------------------- cputlb.c | 81 ++++++++++++ include/exec/exec-all.h | 8 +- include/exec/spinlock.h | 49 -------- include/qemu/thread-posix.h | 4 + include/qemu/thread-win32.h | 4 + include/qemu/thread.h | 7 ++ include/qom/cpu.h | 57 +++++++++ linux-user/main.c | 6 +- qom/cpu.c | 20 +++ target-arm/cpu.c | 21 ++++ target-arm/cpu.h | 6 + target-arm/helper.c | 58 +++------ target-arm/helper.h | 4 + target-arm/op_helper.c | 128 ++++++++++++++++++- target-arm/psci.c | 2 + target-arm/translate.c | 101 +++------------ target-i386/mem_helper.c | 16 ++- target-i386/misc_helper.c | 27 +++- tcg/i386/tcg-target.c | 8 ++ tcg/tcg.h | 14 ++- translate-all.c | 217 +++++++++++++++++++++++++++----- util/qemu-thread-posix.c | 45 +++++++ util/qemu-thread-win32.c | 30 +++++ vl.c | 6 + 26 files changed, 934 insertions(+), 378 deletions(-)delete mode 100644
include/exec/spinlock.h
Quoted 7 lines · Frederic Konrad
Quoted 4 lines
-
tb_lockusage intb_find_fastis complicated and introduces the need for other complicated code such as thetb_invalidatecallback. Instead, the tb locking should reuse thecpu-exec.ccode for user-mode emulation, with additional locking in the spots identified by Fred.The reason for this is that locking around
tb_find_fastjust kills the performance.
Let's make it correct first. :)
Quoted 7 lines
Quoted 4 lines
- the whole signal-based
qemu_cpu_kickcan just go away. Just settingtcg_exit_reqandexit_requestwill kick the TCG thread. The hairy Win32 SuspendThread/ResumeThread goes away too. I suggest doing it now, because proving it unnecessary is easier than proving it correct.Just setting
tcg_exit_reqandexit_requestand signal thecpu->halt_condI guess?
Yes.
Quoted 3 lines
Quoted 2 lines
- the big QEMU lock is not taken anywhere for MMIO accesses that require it (i.e. basically all of them)
Isn't that handled by
prepare_mmio_access?
That's not used on the TCG path (which calls
memory_region_dispatch_read/write directly).
Paolo
Quoted 34 lines · Frederic Konrad
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · On 10/08/2015 17:26, [email protected] wrote:
From: KONRAD Frederic <[email protected]>
This is the 7th round of the MTTCG patch series.
Thanks to look at this.
Quoted 6 lines
Here is a list of issues that I found:
-
tb_lockusage intb_find_fastis complicated and introduces the need for other complicated code such as thetb_invalidatecallback. Instead, the tb locking should reuse thecpu-exec.ccode for user-mode emulation, with additional locking in the spots identified by Fred.The reason for this is that locking around
tb_find_fastjust kills the performance.Quoted 5 lines
-
tb_lockuses a recursive lock, but this is not necessary. Did I ever say I dislike recursive mutexes? :) The wrappers tb_lock()/tb_unlock()/tb_lock_reset() can catch recursive locking for us, so it's not hard to do without it.True this is definitely not nice but seems some code eg:
tb_invalidateare called from different place with or without thetb_lock. We might probably be able to lock the caller.Quoted 5 lines
-
code_bitmapis not protected by any mutex (tb_invalidate_phys_page_fastis called with the iothread mutex taken, but other users ofcode_bitmapdo not use it). Writes should be protected by thetb_lock, reads by eithertb_lockor RCU.Yes good point I missed this one.
Quoted 4 lines
- memory barriers are probably requested around accesses to ->exit_request. ->thread_kicked also needs to be accessed with atomics, because async_run_{,safe_}on_cpu can be called outside the big QEMU lock.
Quoted 5 lines
Quoted 4 lines
- the whole signal-based
qemu_cpu_kickcan just go away. Just settingtcg_exit_reqandexit_requestwill kick the TCG thread. The hairy Win32 SuspendThread/ResumeThread goes away too. I suggest doing it now, because proving it unnecessary is easier than proving it correct.Just setting
tcg_exit_reqandexit_requestand signal thecpu->halt_condI guess?
BTW that affect KVM as well. Seems this mechanism is used as well with qemu_cpu_kick_self().. Which is a little strange as it seems the SIGIPI trigger a dummy signal handler?
memset(&sigact, 0, sizeof(sigact));
sigact.sa_handler = dummy_signal;
sigaction(SIG_IPI, &sigact, NULL);Quoted 215 lines
Quoted 5 lines
- user-mode emulation is broken (does not compile)
- the big QEMU lock is not taken anywhere for MMIO accesses that require it (i.e. basically all of them)
Isn't that handled by
prepare_mmio_access?Quoted 4 lines
- some code wants to be called _outside_ the big QEMU lock, for example because it longjmps back to
cpu_exec. For example, I suspect that the notdirty callbacks must be marked withmemory_region_clear_global_locking.Fred
Quoted 200 lines
I've started looking at them (and documenting the locking conventions for functions), and I hope to post it to some git repo later this week.
Paolo
Quoted 194 lines
It can be cloned from:
[email protected]:fkonrad/mttcg.git branchmulti_tcg_v7.This patch-set try to address the different issues in the global picture of MTTCG, presented on the wiki.
== Needed patch for our work ==
Some preliminaries are needed for our work:
*current_cpudoesn't make sense in mttcg so atcg_executingflag
is added to
the CPUState.
* We need to run some work safely when all VCPUs are outside their
execution
loop. This is done with theasync_run_safe_work_on_cpufunction
introduced
in this series.
* QemuSpin lock is introduced (on posix only yet) to allow a
faster handling of
atomic instruction.== Code generation and cache ==
As Qemu stands, there is no protection at all against two threads attempting to generate code at the same time or modifying a TranslationBlock. The "protect TBContext with
tb_lock" patch address the issue of code generation and makes all the tb_* function thread safe (excepttb_flush). This raised the question of one or multiple caches. We choosed to use one unified cache because it's easier as a first step and since the structure of QEMU effectively has a ‘local’ cache per CPU in the form of the jump cache, we don't see the benefit of having two pools of tbs.== Dirty tracking ==
Protecting the IOs:
To allows all VCPUs threads to run at the same time we need to drop theglobal_mutexas soon as possible. The io access need to take the mutex. This is likely to change when http://thread.gmane.org/gmane.comp.emulators.qemu/345258
will be upstreamed.Invalidation of TranslationBlocks:
We can have all VCPUs running during an invalidation. Each VCPU is able to clean it's jump cache itself as it is in CPUState so that can be handled by a simple call toasync_run_on_cpu. Howevertb_invalidatealso writes to the TranslationBlock which is shared as we have only one pool.
Hence this part of invalidate requires all VCPUs to exit before it can be done. Hence theasync_run_safe_work_on_cpuis introduced to handle this case.== Atomic instruction ==
For now only ARM on x64 is supported by using an cmpxchg instruction. Specifically the limitation of this approach is that it is harder to support 64bit ARM on a host architecture that is multi-core, but only supports 32 bit cmpxchg (we believe this could be the case for some PPC cores). For now this case is not correctly handled. The existing atomic patch will attempt to execute the 64 bit cmpxchg functionality in a non thread safe fashion. Our intention is to provide a new multi-thread ARM atomic patch for 64bit ARM on effective 32bit hosts.
This atomic instruction part has been tested with Alexander's atomic stress repo available here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.htmlThe execution is a little slower than upstream probably because of the different VCPU fight for the mutex. Swaping
arm_exclusive_lockfrom mutex tospin_lockreduce considerably the difference.== Testing ==
A simple double dhrystone test in SMP 2 with vexpress-a15 in a linux guest show a good performance progression: it takes basically 18s upstream to complete vs 10s with MTTCG.
Testing image is available here:
https://cloud.greensocs.com/index.php/s/CfHSLzDH5pmTkW3Then simply:
./configure --target-list=arm-softmmu
make -j8
./arm-softmmu/qemu-system-arm -M vexpress-a15 -smp 2 -kernel zImage -initrd rootfs.ext2 -dtb vexpress-v2p-ca15-tc1.dtb --nographic --append "console=ttyAMA0"login: root
The dhrystone command is the last one in the history. "dhrystone 10000000 & dhrystone 10000000"
The atomic spinlock benchmark from Alexander shows that atomic basically work. Just follow the instruction here:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg05585.html== Known issues ==
* GDB stub:
GDB stub is not tested right now it will probably requires some
changes to
work.* deadlock on exit:
When exiting QEMU Ctrl-C some VCPU's thread are not able to exit
and continue
execution.
http://git.greensocs.com/fkonrad/mttcg/issues/1*
memory_region_rom_device_set_romdfrom pflash01 just crashes the TCG code. Strangely this happen only with "-smp 4" and 2 in the DTB.
http://git.greensocs.com/fkonrad/mttcg/issues/2Changes V6 -> V7:
*global_lock:
* Don't protect softmmu read/write helper as it's now done inadress_space_rw.
*tcg_exec_flag:
* Make the flag atomically test and set through an API.
* introduceasync_safe_work:
* moveqemu_cpu_kick_threadto avoid prototype declaration.
* use thework_mutex.
*async_work:
* protect it with a mutex (work_mutex) against concurent access.
*tb_lock:
* protecttcg_malloc_internalas well.
* signal the VCPU even ofcurrent_cpuis NULL.
* added PSCI patch.
* rebased on v2.4.0-rc0 (6169b60285fe1ff730d840a49527e721bfb30899).Changes V5 -> V6:
* Introduceasync_safe_workto do thetb_flushand some part oftb_invalidate.
* Introduce QemuSpin from Guillaume which allow a faster atomic
instruction
(6s to pass Alexander's atomic test instead of 30s before).
* Don't taketb_lockbeforetb_find_fast.
* Handletb_flushwithasync_safe_work.
* Handletb_invalidatewithasync_workandasync_safe_work.
* Drop thetlb_flush_requestmechanism and useasync_workas well.
* Fix the wrong lenght in atomic patch.
* Fix the wrong return address for exception in atomic patch.Alex Bennée (1):
target-arm/psci.c: wake up sleeping CPUs (MTTCG)Guillaume Delbergue (1):
add support for spin lock on POSIX systems exclusivelyKONRAD Frederic (17):
cpus: protect queued_work_* withwork_mutex.
cpus: addtcg_exec_flag.
cpus: introduceasync_run_safe_work_on_cpu.
replace spinlock by QemuMutex.
remove unused spinlock.
protect TBContext withtb_lock.
tcg: removetcg_halt_condglobal variable.
Drop global lock during TCG code execution
cpu: removeexit_requestglobal.
tcg: switch on multithread.
Use atomic cmpxchg to atomically check the exclusive value in a
STREX
add a callback whentb_invalidateis called.
cpu: introduce tlb_flush*_all.
arm: use tlb_flush*_all
translate-all: introducestb_flush_safe.
translate-all: (wip) usetb_flush_safewhen we can't alloc more tb.
mttcg: signal the associated cpu anyway.cpu-exec.c | 98 +++++++++------ cpus.c | 295 +++++++++++++++++++++++++------------------- cputlb.c | 81 ++++++++++++ include/exec/exec-all.h | 8 +- include/exec/spinlock.h | 49 -------- include/qemu/thread-posix.h | 4 + include/qemu/thread-win32.h | 4 + include/qemu/thread.h | 7 ++ include/qom/cpu.h | 57 +++++++++ linux-user/main.c | 6 +- qom/cpu.c | 20 +++ target-arm/cpu.c | 21 ++++ target-arm/cpu.h | 6 + target-arm/helper.c | 58 +++------ target-arm/helper.h | 4 + target-arm/op_helper.c | 128 ++++++++++++++++++- target-arm/psci.c | 2 + target-arm/translate.c | 101 +++------------ target-i386/mem_helper.c | 16 ++- target-i386/misc_helper.c | 27 +++- tcg/i386/tcg-target.c | 8 ++ tcg/tcg.h | 14 ++- translate-all.c | 217 +++++++++++++++++++++++++++----- util/qemu-thread-posix.c | 45 +++++++ util/qemu-thread-win32.c | 30 +++++ vl.c | 6 + 26 files changed, 934 insertions(+), 378 deletions(-)delete mode 100644
include/exec/spinlock.h
Quoted 8 lines · Frederic Konrad
BTW that affect KVM as well. Seems this mechanism is used as well with
qemu_cpu_kick_self().. Which is a little strange as it seems the SIGIPI trigger a
dummy signal handler?memset(&sigact, 0, sizeof(sigact)); sigact.sa_handler = dummy_signal; sigaction(SIG_IPI, &sigact, NULL);
KVM is different, the signal handler is used to kick the VM out of KVM_RUN. We're going to add another path (a ioctl) but it cannot use the same code as TCG.
qemu_cpu_kick_self is needed in some special cases where KVM tells you "call KVM_RUN asap" but you know you have more work to do in userspace.
Calling qemu_cpu_kick_self lets you call KVM_RUN work and immediately
do the userspace work.
Paolo