mttcg: Set jmp_env to handle exit from tb_gen_code
PATCH 1/1 mttcg: Set jmp_env to handle exit from tb_gen_code
tb_gen_code() can exit execution using cpu_exit_loop() when it cannot allocate new tb's. To handle this, we need to properly set the jmp_env pointer ahead of calling tb_gen_code().
--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
cpu-exec.c +11 -12
@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -if (sigsetjmp (cpu->jmp_env,0 ) ==0 ) { + tb =tb_gen_code (cpu, pc, cs_base, flags, +1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); + tb->orig_tb =NULL ; +tb_unlock (); + + cc->cpu_exec_enter (cpu);/* execute the generated code */ trace_exec_tb_nocache (tb, pc);cpu_tb_exec (cpu, tb); - } + cc->cpu_exec_exit (cpu); - cc->cpu_exec_exit (cpu); -tb_lock (); -tb_phys_invalidate (tb,-1 ); -tb_free (tb); +tb_lock (); +tb_phys_invalidate (tb,-1 ); +tb_free (tb); + }tb_unlock (); }
Signature · 1 line
2.11.0
Quoted 16 lines · Pranith Kumar
tb_gen_code()can exit execution usingcpu_exit_loop()when it cannot allocate new tb's. To handle this, we need to properly set thejmp_envpointer ahead of callingtb_gen_code().CC Alex Bennée <[email protected]>CC Richard Henderson <[email protected]>Signed-off-by Pranith Kumar <[email protected]>--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)cpu-exec.c +0
-0
cpu-exec.c static void cpu_exec_step(CPUState *cpu) · line 245
Quoted 10 lines
cpu-exec.c +0
-7@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -
It occurs to me we are also diverging in our locking pattern from tb_find which takes mmap_lock first. This is a NOP for system emulation but needed for user-emulation (for which we can do cpu_exec_step but not cpu_exec_nocache).
Quoted 13 lines
cpu-exec.c +7
-1if (sigsetjmp (cpu->jmp_env,0 ) ==0 ) { + tb =tb_gen_code (cpu, pc, cs_base, flags, +1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); + tb->orig_tb =NULL ; +tb_unlock (); + + cc->cpu_exec_enter (cpu);/* execute the generated code */ trace_exec_tb_nocache (tb, pc);cpu_tb_exec (cpu, tb); - } + cc->cpu_exec_exit (cpu);
cpu-exec.c static void cpu_exec_step(CPUState *cpu) · line 256
Quoted 10 lines
cpu-exec.c +4
-4- cc->cpu_exec_exit (cpu); -tb_lock (); -tb_phys_invalidate (tb,-1 ); -tb_free (tb); +tb_lock (); +tb_phys_invalidate (tb,-1 ); +tb_free (tb); + }tb_unlock (); }
--
Alex Bennée
Quoted 19 lines · Alex Bennée
Quoted 17 lines · Pranith Kumar
tb_gen_code()can exit execution usingcpu_exit_loop()when it cannot allocate new tb's. To handle this, we need to properly set thejmp_envpointer ahead of callingtb_gen_code().CC Alex Bennée <[email protected]>CC Richard Henderson <[email protected]>Signed-off-by Pranith Kumar <[email protected]>--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)cpu-exec.c +0
-0@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)
Quoted 14 lines
cpu-exec.c static void cpu_exec_step(CPUState *cpu) · line 245
Quoted 9 lines
cpu-exec.c +0
-7cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -It occurs to me we are also diverging in our locking pattern from
tb_findwhich takesmmap_lockfirst. This is a NOP for system emulation but needed for user-emulation (for which we can docpu_exec_stepbut notcpu_exec_nocache).
Right. So we have to take the mmap_lock() before calling tb_gen_code(). However, this lock is released in the error path before calling cpu_loop_exit() if allocation of a new tb fails. The following is what I have after merging with the previous EXCP_ATOMIC handling patch.
cpu-exec.c +19 -10
@@ -228,6 +228,7 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,static void cpu_exec_step (CPUState *cpu) { +CPUClass *cc =CPU_GET_CLASS (cpu);CPUArchState *env = (CPUArchState *)cpu->env_ptr;TranslationBlock *tb;target_ulong cs_base, pc;
@@ -235,16 +236,24 @@ static void cpu_exec_step(CPUState *cpu)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); -/* execute the generated code */ -trace_exec_tb_nocache (tb, pc); -cpu_tb_exec (cpu, tb); -tb_lock (); -tb_phys_invalidate (tb,-1 ); -tb_free (tb); +if (sigsetjmp (cpu->jmp_env,0 ) ==0 ) { +mmap_lock (); + tb =tb_gen_code (cpu, pc, cs_base, flags, +1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); + tb->orig_tb =NULL ; +mmap_unlock (); +tb_unlock (); + + cc->cpu_exec_enter (cpu); +/* execute the generated code */ +trace_exec_tb_nocache (tb, pc); +cpu_tb_exec (cpu, tb); + cc->cpu_exec_exit (cpu); + +tb_lock (); +tb_phys_invalidate (tb,-1 ); +tb_free (tb); + }tb_unlock (); }
cpus.c +9 -0
@@ -1347,6 +1347,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu);break ; + }else if (r ==EXCP_ATOMIC ) { +qemu_mutex_unlock_iothread (); +cpu_exec_step_atomic (cpu); +qemu_mutex_lock_iothread (); +break ; } }else if (cpu->stop) {if (cpu->unplug) {
@@ -1457,6 +1462,10 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)*/ g_assert (cpu->halted);break ; +case EXCP_ATOMIC : +qemu_mutex_unlock_iothread (); +cpu_exec_step_atomic (cpu); +qemu_mutex_lock_iothread ();default :/* Ignore everything else? */ break ;
--
Pranith
Quoted 36 lines · Pranith Kumar
Quoted 19 lines · Alex Bennée
Quoted 17 lines · Pranith Kumar
tb_gen_code()can exit execution usingcpu_exit_loop()when it cannot allocate new tb's. To handle this, we need to properly set thejmp_envpointer ahead of callingtb_gen_code().CC Alex Bennée <[email protected]>CC Richard Henderson <[email protected]>Signed-off-by Pranith Kumar <[email protected]>--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)cpu-exec.c +0
-0@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)Quoted 14 lines
cpu-exec.c static void cpu_exec_step(CPUState *cpu)
Quoted 9 lines
cpu-exec.c +0
-7cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -It occurs to me we are also diverging in our locking pattern from
tb_findwhich takesmmap_lockfirst. This is a NOP for system emulation but needed for user-emulation (for which we can docpu_exec_stepbut notcpu_exec_nocache).
Quoted 4 lines
Right. So we have to take the
mmap_lock()before callingtb_gen_code(). However, this lock is released in the error path before callingcpu_loop_exit()if allocation of a new tb fails. The following is what I have after merging with the previousEXCP_ATOMIChandling patch.
Hmm we are a start/end_exclusive() though so what could we be racing against that also wants the mmap_lock()? Could it be held by anything at this point as every user needs to be woken up?
Quoted 65 lines
cpu-exec.c +19
-10@@ -228,6 +228,7 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,static void cpu_exec_step (CPUState *cpu) { +CPUClass *cc =CPU_GET_CLASS (cpu);CPUArchState *env = (CPUArchState *)cpu->env_ptr;TranslationBlock *tb;target_ulong cs_base, pc;@@ -235,16 +236,24 @@ static void cpu_exec_step(CPUState *cpu)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); -/* execute the generated code */ -trace_exec_tb_nocache (tb, pc); -cpu_tb_exec (cpu, tb); -tb_lock (); -tb_phys_invalidate (tb,-1 ); -tb_free (tb); +if (sigsetjmp (cpu->jmp_env,0 ) ==0 ) { +mmap_lock (); + tb =tb_gen_code (cpu, pc, cs_base, flags, +1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); + tb->orig_tb =NULL ; +mmap_unlock (); +tb_unlock (); + + cc->cpu_exec_enter (cpu); +/* execute the generated code */ +trace_exec_tb_nocache (tb, pc); +cpu_tb_exec (cpu, tb); + cc->cpu_exec_exit (cpu); + +tb_lock (); +tb_phys_invalidate (tb,-1 ); +tb_free (tb); + }tb_unlock (); }cpus.c +5
-0@@ -1347,6 +1347,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu);break ; + }else if (r ==EXCP_ATOMIC ) { +qemu_mutex_unlock_iothread (); +cpu_exec_step_atomic (cpu); +qemu_mutex_lock_iothread (); +break ; } }else if (cpu->stop) {if (cpu->unplug) {
cpus.c static void *qemu_tcg_cpu_thread_fn(void *arg) · line 1471
Quoted 10 lines
cpus.c +4
-0@@ -1457,6 +1462,10 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)*/ g_assert (cpu->halted);break ; +case EXCP_ATOMIC : +qemu_mutex_unlock_iothread (); +cpu_exec_step_atomic (cpu); +qemu_mutex_lock_iothread ();default :/* Ignore everything else? */ break ;
--
Alex Bennée
Quoted 39 lines · Alex Bennée
Quoted 36 lines · Pranith Kumar
Quoted 33 lines · Alex Bennée
Quoted 16 lines · Pranith Kumar
tb_gen_code()can exit execution usingcpu_exit_loop()when it cannot allocate new tb's. To handle this, we need to properly set thejmp_envpointer ahead of callingtb_gen_code().CC Alex Bennée <[email protected]>CC Richard Henderson <[email protected]>Signed-off-by Pranith Kumar <[email protected]>--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)cpu-exec.c +0
-0cpu-exec.c static void cpu_exec_step(CPUState *cpu) · line 245
Quoted 10 lines
cpu-exec.c +0
-7@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -It occurs to me we are also diverging in our locking pattern from
tb_findwhich takesmmap_lockfirst. This is a NOP for system emulation but needed for user-emulation (for which we can docpu_exec_stepbut notcpu_exec_nocache).
Quoted 9 lines
Quoted 4 lines
Right. So we have to take the
mmap_lock()before callingtb_gen_code(). However, this lock is released in the error path before callingcpu_loop_exit()if allocation of a new tb fails. The following is what I have after merging with the previousEXCP_ATOMIChandling patch.Hmm we are a start/end_exclusive() though so what could we be racing against that also wants the
mmap_lock()? Could it be held by anything at this point as every user needs to be woken up?
No, I don't think any other thread holds the mmap/tb lock at this point. However, we still need to take this lock since the functions we are calling expect us to hold it and assert otherwise.
Signature · 1 line
Pranith
Quoted 59 lines · Pranith Kumar
Quoted 19 lines · Alex Bennée
Quoted 17 lines · Pranith Kumar
tb_gen_code()can exit execution usingcpu_exit_loop()when it cannot allocate new tb's. To handle this, we need to properly set thejmp_envpointer ahead of callingtb_gen_code().CC Alex Bennée <[email protected]>CC Richard Henderson <[email protected]>Signed-off-by Pranith Kumar <[email protected]>--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)cpu-exec.c +0
-0@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)Quoted 14 lines
cpu-exec.c static void cpu_exec_step(CPUState *cpu)
Quoted 9 lines
cpu-exec.c +0
-7cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -It occurs to me we are also diverging in our locking pattern from
tb_findwhich takesmmap_lockfirst. This is a NOP for system emulation but needed for user-emulation (for which we can docpu_exec_stepbut notcpu_exec_nocache).Right. So we have to take the
mmap_lock()before callingtb_gen_code(). However, this lock is released in the error path before callingcpu_loop_exit()if allocation of a new tb fails. The following is what I have after merging with the previousEXCP_ATOMIChandling patch.cpu-exec.c +1
-2@@ -228,6 +228,7 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,static void cpu_exec_step (CPUState *cpu) { +CPUClass *cc =CPU_GET_CLASS (cpu);CPUArchState *env = (CPUArchState *)cpu->env_ptr;TranslationBlock *tb;target_ulong cs_base, pc;@@ -235,16 +236,24 @@ static void cpu_exec_step(CPUState *cpu)cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT );
cpu-exec.c static void cpu_exec_step(CPUState *cpu) · line 240
Quoted 10 lines
cpu-exec.c +2
-8- tb->orig_tb =NULL ; -tb_unlock (); -/* execute the generated code */ -trace_exec_tb_nocache (tb, pc); -cpu_tb_exec (cpu, tb); -tb_lock (); -tb_phys_invalidate (tb,-1 ); -tb_free (tb); +if (sigsetjmp (cpu->jmp_env,0 ) ==0 ) { +mmap_lock ();
That gets the locking order the wrong way around - I'm wary of that.
Quoted 36 lines
cpu-exec.c +16
-0+ tb =tb_gen_code (cpu, pc, cs_base, flags, +1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); + tb->orig_tb =NULL ; +mmap_unlock (); +tb_unlock (); + + cc->cpu_exec_enter (cpu); +/* execute the generated code */ +trace_exec_tb_nocache (tb, pc); +cpu_tb_exec (cpu, tb); + cc->cpu_exec_exit (cpu); + +tb_lock (); +tb_phys_invalidate (tb,-1 ); +tb_free (tb); + }tb_unlock (); }cpus.c +5
-0@@ -1347,6 +1347,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu);break ; + }else if (r ==EXCP_ATOMIC ) { +qemu_mutex_unlock_iothread (); +cpu_exec_step_atomic (cpu); +qemu_mutex_lock_iothread (); +break ; } }else if (cpu->stop) {if (cpu->unplug) {
cpus.c static void *qemu_tcg_cpu_thread_fn(void *arg) · line 1471
Quoted 10 lines
cpus.c +4
-0@@ -1457,6 +1462,10 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)*/ g_assert (cpu->halted);break ; +case EXCP_ATOMIC : +qemu_mutex_unlock_iothread (); +cpu_exec_step_atomic (cpu); +qemu_mutex_lock_iothread ();default :/* Ignore everything else? */ break ;
--
Alex Bennée
Quoted 57 lines · Alex Bennée
Quoted 55 lines · Pranith Kumar
Quoted 19 lines · Alex Bennée
Quoted 17 lines · Pranith Kumar
tb_gen_code()can exit execution usingcpu_exit_loop()when it cannot allocate new tb's. To handle this, we need to properly set thejmp_envpointer ahead of callingtb_gen_code().CC Alex Bennée <[email protected]>CC Richard Henderson <[email protected]>Signed-off-by Pranith Kumar <[email protected]>--- cpu-exec.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)cpu-exec.c +0
-0@@ -236,23 +236,22 @@ static void cpu_exec_step(CPUState *cpu)Quoted 14 lines
cpu-exec.c static void cpu_exec_step(CPUState *cpu)
Quoted 9 lines
cpu-exec.c +0
-7cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT ); - tb->orig_tb =NULL ; -tb_unlock (); - - cc->cpu_exec_enter (cpu); -It occurs to me we are also diverging in our locking pattern from
tb_findwhich takesmmap_lockfirst. This is a NOP for system emulation but needed for user-emulation (for which we can docpu_exec_stepbut notcpu_exec_nocache).Right. So we have to take the
mmap_lock()before callingtb_gen_code(). However, this lock is released in the error path before callingcpu_loop_exit()if allocation of a new tb fails. The following is what I have after merging with the previousEXCP_ATOMIChandling patch.cpu-exec.c +1
-0@@ -228,6 +228,7 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,static void cpu_exec_step (CPUState *cpu) { +CPUClass *cc =CPU_GET_CLASS (cpu);CPUArchState *env = (CPUArchState *)cpu->env_ptr;TranslationBlock *tb;target_ulong cs_base, pc;@@ -235,16 +236,24 @@ static void cpu_exec_step(CPUState *cpu)
Quoted 17 lines
Quoted 4 lines
cpu-exec.c +0
-2cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags);tb_lock (); - tb =tb_gen_code (cpu, pc, cs_base, flags, -1 |CF_NOCACHE |CF_IGNORE_ICOUNT );cpu-exec.c static void cpu_exec_step(CPUState *cpu) · line 240
Quoted 10 lines
cpu-exec.c +2
-8- tb->orig_tb =NULL ; -tb_unlock (); -/* execute the generated code */ -trace_exec_tb_nocache (tb, pc); -cpu_tb_exec (cpu, tb); -tb_lock (); -tb_phys_invalidate (tb,-1 ); -tb_free (tb); +if (sigsetjmp (cpu->jmp_env,0 ) ==0 ) { +mmap_lock ();That gets the locking order the wrong way around - I'm wary of that.
But we are in exclusive execution now, and we are releasing all the lock taken before coming out of it. I think that should be OK. If your concern is that this is not the normal locking pattern, then I agree. Otherwise, it should be fine.
Signature · 1 line
Pranith