qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Frederic Konrad <fred.konrad@greensocs.com>
To: "Alex Bennée" <alex.bennee@linaro.org>
Cc: mttcg@listserver.greensocs.com, peter.maydell@linaro.org,
	a.spyridakis@virtualopensystems.com, mark.burton@greensocs.com,
	agraf@suse.de, qemu-devel@nongnu.org,
	guillaume.delbergue@greensocs.com, pbonzini@redhat.com,
	alistair.francis@xilinx.com
Subject: Re: [Qemu-devel] [RFC PATCH V6 02/18] replace spinlock by QemuMutex.
Date: Tue, 07 Jul 2015 13:46:32 +0200	[thread overview]
Message-ID: <559BBC18.50002@greensocs.com> (raw)
In-Reply-To: <87r3ok9sur.fsf@linaro.org>

On 07/07/2015 12:15, Alex Bennée wrote:
> fred.konrad@greensocs.com writes:
>
>> From: KONRAD Frederic <fred.konrad@greensocs.com>
>>
>> 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.
>>
>> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
>> ---
>>   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(-)
>>
>> diff --git a/cpu-exec.c b/cpu-exec.c
>> index 2ffeb6e..d6336d9 100644
>> --- a/cpu-exec.c
>> +++ b/cpu-exec.c
>> @@ -362,7 +362,9 @@ int cpu_exec(CPUArchState *env)
>>       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)) {
>> @@ -480,8 +482,10 @@ int cpu_exec(CPUArchState *env)
>>                       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
> Why are the locking rules different for CONFIG_USER versus system
> emulation? Looking at the final tree:
>
>>                   tb = tb_find_fast(env);
> this eventually ends up doing a tb_lock on the find_slow path which IIRC
> is when might end up doing the actual code generation.

I didn't looked at the user code. But yes we should probably end with 
the same
thing for both user mode code and system mode code. That's what Peter was
suggesting before but I didn't have time to look at this right now.

>
>>                   /* Note: we do it here to avoid a gcc bug on Mac OS X when
>>                      doing it in tb_find_slow */
>> @@ -503,9 +507,10 @@ int cpu_exec(CPUArchState *env)
>>                       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
>> @@ -572,10 +577,12 @@ int cpu_exec(CPUArchState *env)
>>   #ifdef TARGET_I386
>>               x86_cpu = X86_CPU(cpu);
>>   #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(;;) */
>>   
>> diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
>> index 2573e8c..44f3336 100644
>> --- a/include/exec/exec-all.h
>> +++ b/include/exec/exec-all.h
>> @@ -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;
>> diff --git a/linux-user/main.c b/linux-user/main.c
>> index c855bcc..bce3a98 100644
>> --- a/linux-user/main.c
>> +++ b/linux-user/main.c
>> @@ -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((CPUArchState *)thread_cpu->env_ptr);
>>       } else {
>>           pthread_mutex_unlock(&exclusive_lock);
>> -        pthread_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
>> +        qemu_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
>>       }
>>   }
>>   
>> diff --git a/target-i386/mem_helper.c b/target-i386/mem_helper.c
>> index 1aec8a5..7106cc3 100644
>> --- a/target-i386/mem_helper.c
>> +++ b/target-i386/mem_helper.c
>> @@ -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)
>>   {
>> diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
>> index ff4d9cf..0d7c99c 100644
>> --- a/tcg/i386/tcg-target.c
>> +++ b/tcg/i386/tcg-target.c
>> @@ -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 {
> I wonder if it would be better splitting the patches:
>
>   - Convert tb spinlocks to use tb_lock
>   - i386: convert lock helpers to QemuMutex
>
> before the final
>
>    - Remove spinlocks

Yes that makes sense I think.

Fred
>

  parent reply	other threads:[~2015-07-07 11:46 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26 14:47 [Qemu-devel] [RFC PATCH V6 00/18] Multithread TCG fred.konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 01/18] cpu: make cpu_thread_is_idle public fred.konrad
2015-07-07  9:47   ` Alex Bennée
2015-07-07 11:43     ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 02/18] replace spinlock by QemuMutex fred.konrad
2015-07-07 10:15   ` Alex Bennée
2015-07-07 10:22     ` Paolo Bonzini
2015-07-07 11:48       ` Frederic Konrad
2015-07-07 12:34         ` Paolo Bonzini
2015-07-07 13:06           ` Frederic Konrad
2015-07-07 11:46     ` Frederic Konrad [this message]
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 03/18] remove unused spinlock fred.konrad
2015-06-26 14:53   ` Paolo Bonzini
2015-06-26 15:29     ` Frederic Konrad
2015-06-26 15:46       ` Paolo Bonzini
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 04/18] add support for spin lock on POSIX systems exclusively fred.konrad
2015-06-26 14:55   ` Paolo Bonzini
2015-06-26 15:31     ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 05/18] protect TBContext with tb_lock fred.konrad
2015-06-26 14:56   ` Paolo Bonzini
2015-06-26 15:39     ` Frederic Konrad
2015-06-26 15:45       ` Paolo Bonzini
2015-06-26 16:20   ` Paolo Bonzini
2015-07-07 12:22   ` Alex Bennée
2015-07-07 13:16     ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 06/18] tcg: remove tcg_halt_cond global variable fred.konrad
2015-06-26 15:02   ` Paolo Bonzini
2015-06-26 15:41     ` Frederic Konrad
2015-07-07 12:27       ` Alex Bennée
2015-07-07 13:17         ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 07/18] Drop global lock during TCG code execution fred.konrad
2015-06-26 14:56   ` Jan Kiszka
2015-06-26 15:08     ` Paolo Bonzini
2015-06-26 15:36     ` Frederic Konrad
2015-06-26 15:42       ` Jan Kiszka
2015-06-26 16:11         ` Frederic Konrad
2015-07-07 12:33       ` Alex Bennée
2015-07-07 13:18         ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 08/18] cpu: remove exit_request global fred.konrad
2015-06-26 15:03   ` Paolo Bonzini
2015-07-07 13:04   ` Alex Bennée
2015-07-07 13:25     ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 09/18] cpu: add a tcg_executing flag fred.konrad
2015-07-07 13:23   ` Alex Bennée
2015-07-07 13:30     ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 10/18] tcg: switch on multithread fred.konrad
2015-07-07 13:40   ` Alex Bennée
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 11/18] cpus: make qemu_cpu_kick_thread public fred.konrad
2015-07-07 15:11   ` Alex Bennée
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 12/18] Use atomic cmpxchg to atomically check the exclusive value in a STREX fred.konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 13/18] cpu: introduce async_run_safe_work_on_cpu fred.konrad
2015-06-26 15:35   ` Paolo Bonzini
2015-06-26 16:09     ` Frederic Konrad
2015-06-26 16:23       ` Paolo Bonzini
2015-06-26 16:36         ` Frederic Konrad
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 14/18] add a callback when tb_invalidate is called fred.konrad
2015-06-26 16:20   ` Paolo Bonzini
2015-06-26 16:40     ` Frederic Konrad
2015-07-07 15:32   ` Alex Bennée
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 15/18] cpu: introduce tlb_flush*_all fred.konrad
2015-06-26 15:15   ` Paolo Bonzini
2015-06-26 15:54     ` Frederic Konrad
2015-06-26 16:01       ` Paolo Bonzini
2015-06-26 16:08         ` Peter Maydell
2015-06-26 16:30           ` Frederic Konrad
2015-06-26 16:31             ` Paolo Bonzini
2015-06-26 16:35               ` Frederic Konrad
2015-06-26 16:39                 ` Paolo Bonzini
2015-07-06 14:29             ` Mark Burton
2015-07-07 16:12               ` Alex Bennée
2015-06-26 16:54           ` Paolo Bonzini
2015-07-08 15:35           ` Frederic Konrad
2015-07-07 15:52   ` Alex Bennée
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 16/18] arm: use tlb_flush*_all fred.konrad
2015-07-07 16:14   ` Alex Bennée
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 17/18] translate-all: introduces tb_flush_safe fred.konrad
2015-07-07 16:16   ` Alex Bennée
2015-06-26 14:47 ` [Qemu-devel] [RFC PATCH V6 18/18] translate-all: (wip) use tb_flush_safe when we can't alloc more tb fred.konrad
2015-06-26 16:21   ` Paolo Bonzini
2015-06-26 16:38     ` Frederic Konrad
2015-07-07 16:17   ` Alex Bennée
2015-07-07 16:23     ` Frederic Konrad

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=559BBC18.50002@greensocs.com \
    --to=fred.konrad@greensocs.com \
    --cc=a.spyridakis@virtualopensystems.com \
    --cc=agraf@suse.de \
    --cc=alex.bennee@linaro.org \
    --cc=alistair.francis@xilinx.com \
    --cc=guillaume.delbergue@greensocs.com \
    --cc=mark.burton@greensocs.com \
    --cc=mttcg@listserver.greensocs.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

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

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