diff mbox

[RFC,v2,11/11] tcg: Make tb_flush() thread safe

Message ID 1467839703-11733-12-git-send-email-sergey.fedorov@linaro.org
State New
Headers show

Commit Message

sergey.fedorov@linaro.org July 6, 2016, 9:15 p.m. UTC
From: Sergey Fedorov <serge.fdrv@gmail.com>

Use async_safe_run_on_cpu() to make tb_flush() thread safe.

Signed-off-by: Sergey Fedorov <serge.fdrv@gmail.com>
Signed-off-by: Sergey Fedorov <sergey.fedorov@linaro.org>
---

Changes in v2:
 - stale comment about unsafe tb_flush() removed
---
 translate-all.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

Comments

Sergey Fedorov July 7, 2016, 8:11 p.m. UTC | #1
On 07/07/16 00:15, Sergey Fedorov wrote:
> From: Sergey Fedorov <serge.fdrv@gmail.com>
>
> Use async_safe_run_on_cpu() to make tb_flush() thread safe.

I've just realized that this allows to remove CPUState::tb_flushed as well.

Regards,
Sergey
Alex Bennée July 14, 2016, 8:41 a.m. UTC | #2
Sergey Fedorov <sergey.fedorov@linaro.org> writes:

> From: Sergey Fedorov <serge.fdrv@gmail.com>
>
> Use async_safe_run_on_cpu() to make tb_flush() thread safe.
>
> Signed-off-by: Sergey Fedorov <serge.fdrv@gmail.com>
> Signed-off-by: Sergey Fedorov <sergey.fedorov@linaro.org>
> ---
>
> Changes in v2:
>  - stale comment about unsafe tb_flush() removed
> ---
>  translate-all.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/translate-all.c b/translate-all.c
> index eaa95e4cd7dc..e69b5d4e889e 100644
> --- a/translate-all.c
> +++ b/translate-all.c
> @@ -831,8 +831,7 @@ static void page_flush_tb(void)
>  }
>
>  /* flush all the translation blocks */
> -/* XXX: tb_flush is currently not thread safe */
> -void tb_flush(CPUState *cpu)
> +static void do_tb_flush(CPUState *cpu, void *data)
>  {
>  #if defined(DEBUG_FLUSH)
>      printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
> @@ -861,6 +860,11 @@ void tb_flush(CPUState *cpu)
>      tcg_ctx.tb_ctx.tb_flush_count++;
>  }
>
> +void tb_flush(CPUState *cpu)
> +{
> +    async_safe_run_on_cpu(cpu, do_tb_flush, NULL);
> +}
> +
>  #ifdef DEBUG_TB_CHECK
>
>  static void
> @@ -1163,9 +1167,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
>   buffer_overflow:
>          /* flush must be done */
>          tb_flush(cpu);
> -        /* cannot fail at this point */
> -        tb = tb_alloc(pc);
> -        assert(tb != NULL);
> +        mmap_unlock();
> +        cpu_loop_exit(cpu);

Given our other discussions about lock resetting I wonder if this is
another case where mmap_reset() could be called on cpu_loop_exit?

>      }
>
>      gen_code_buf = tcg_ctx.code_gen_ptr;

Otherwise so far the testing is looking pretty positive in linux-user:

Tested-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>


--
Alex Bennée
Sergey Fedorov July 14, 2016, 8:54 a.m. UTC | #3
On 14/07/16 11:41, Alex Bennée wrote:
> Sergey Fedorov <sergey.fedorov@linaro.org> writes:
>
>> From: Sergey Fedorov <serge.fdrv@gmail.com>
>>
>> Use async_safe_run_on_cpu() to make tb_flush() thread safe.
>>
>> Signed-off-by: Sergey Fedorov <serge.fdrv@gmail.com>
>> Signed-off-by: Sergey Fedorov <sergey.fedorov@linaro.org>
>> ---
>>
>> Changes in v2:
>>  - stale comment about unsafe tb_flush() removed
>> ---
>>  translate-all.c | 13 ++++++++-----
>>  1 file changed, 8 insertions(+), 5 deletions(-)
>>
>> diff --git a/translate-all.c b/translate-all.c
>> index eaa95e4cd7dc..e69b5d4e889e 100644
>> --- a/translate-all.c
>> +++ b/translate-all.c
>> @@ -831,8 +831,7 @@ static void page_flush_tb(void)
>>  }
>>
>>  /* flush all the translation blocks */
>> -/* XXX: tb_flush is currently not thread safe */
>> -void tb_flush(CPUState *cpu)
>> +static void do_tb_flush(CPUState *cpu, void *data)
>>  {
>>  #if defined(DEBUG_FLUSH)
>>      printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
>> @@ -861,6 +860,11 @@ void tb_flush(CPUState *cpu)
>>      tcg_ctx.tb_ctx.tb_flush_count++;
>>  }
>>
>> +void tb_flush(CPUState *cpu)
>> +{
>> +    async_safe_run_on_cpu(cpu, do_tb_flush, NULL);
>> +}
>> +
>>  #ifdef DEBUG_TB_CHECK
>>
>>  static void
>> @@ -1163,9 +1167,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
>>   buffer_overflow:
>>          /* flush must be done */
>>          tb_flush(cpu);
>> -        /* cannot fail at this point */
>> -        tb = tb_alloc(pc);
>> -        assert(tb != NULL);
>> +        mmap_unlock();
>> +        cpu_loop_exit(cpu);
> Given our other discussions about lock resetting I wonder if this is
> another case where mmap_reset() could be called on cpu_loop_exit?

As I can see, this is the only place mmap_unlock() have to be called
right before cpu_loop_exit(). As I remember, all the other cased in
user-mode emulation were restructured by Peter M. in his syscall/signal
handling series. However, I like the idea to ensure that 'mmap_lock' is
released on any cpu_loop_exit(). What do maintainers think?

Kind regards,
Sergey

>
>>      }
>>
>>      gen_code_buf = tcg_ctx.code_gen_ptr;
> Otherwise so far the testing is looking pretty positive in linux-user:
>
> Tested-by: Alex Bennée <alex.bennee@linaro.org>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>
>
> --
> Alex Bennée
Alex Bennée July 14, 2016, 9:49 a.m. UTC | #4
Sergey Fedorov <serge.fdrv@gmail.com> writes:

> On 14/07/16 11:41, Alex Bennée wrote:
>> Sergey Fedorov <sergey.fedorov@linaro.org> writes:
>>
>>> From: Sergey Fedorov <serge.fdrv@gmail.com>
>>>
>>> Use async_safe_run_on_cpu() to make tb_flush() thread safe.
>>>
>>> Signed-off-by: Sergey Fedorov <serge.fdrv@gmail.com>
>>> Signed-off-by: Sergey Fedorov <sergey.fedorov@linaro.org>
>>> ---
>>>
>>> Changes in v2:
>>>  - stale comment about unsafe tb_flush() removed
>>> ---
>>>  translate-all.c | 13 ++++++++-----
>>>  1 file changed, 8 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/translate-all.c b/translate-all.c
>>> index eaa95e4cd7dc..e69b5d4e889e 100644
>>> --- a/translate-all.c
>>> +++ b/translate-all.c
>>> @@ -831,8 +831,7 @@ static void page_flush_tb(void)
>>>  }
>>>
>>>  /* flush all the translation blocks */
>>> -/* XXX: tb_flush is currently not thread safe */
>>> -void tb_flush(CPUState *cpu)
>>> +static void do_tb_flush(CPUState *cpu, void *data)
>>>  {
>>>  #if defined(DEBUG_FLUSH)
>>>      printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
>>> @@ -861,6 +860,11 @@ void tb_flush(CPUState *cpu)
>>>      tcg_ctx.tb_ctx.tb_flush_count++;
>>>  }
>>>
>>> +void tb_flush(CPUState *cpu)
>>> +{
>>> +    async_safe_run_on_cpu(cpu, do_tb_flush, NULL);
>>> +}
>>> +
>>>  #ifdef DEBUG_TB_CHECK
>>>
>>>  static void
>>> @@ -1163,9 +1167,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
>>>   buffer_overflow:
>>>          /* flush must be done */
>>>          tb_flush(cpu);
>>> -        /* cannot fail at this point */
>>> -        tb = tb_alloc(pc);
>>> -        assert(tb != NULL);
>>> +        mmap_unlock();
>>> +        cpu_loop_exit(cpu);
>> Given our other discussions about lock resetting I wonder if this is
>> another case where mmap_reset() could be called on cpu_loop_exit?
>
> As I can see, this is the only place mmap_unlock() have to be called
> right before cpu_loop_exit(). As I remember, all the other cased in
> user-mode emulation were restructured by Peter M. in his syscall/signal
> handling series. However, I like the idea to ensure that 'mmap_lock' is
> released on any cpu_loop_exit(). What do maintainers think?
>
> Kind regards,
> Sergey
>
>>
>>>      }
>>>
>>>      gen_code_buf = tcg_ctx.code_gen_ptr;
>> Otherwise so far the testing is looking pretty positive in linux-user:
>>
>> Tested-by: Alex Bennée <alex.bennee@linaro.org>
>> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

I should add for the testing to fail without this series I had to apply
the hot-path fixes otherwise lock contention has a serialising affect on
the flushes anyway.

>>
>>
>> --
>> Alex Bennée


--
Alex Bennée
diff mbox

Patch

diff --git a/translate-all.c b/translate-all.c
index eaa95e4cd7dc..e69b5d4e889e 100644
--- a/translate-all.c
+++ b/translate-all.c
@@ -831,8 +831,7 @@  static void page_flush_tb(void)
 }
 
 /* flush all the translation blocks */
-/* XXX: tb_flush is currently not thread safe */
-void tb_flush(CPUState *cpu)
+static void do_tb_flush(CPUState *cpu, void *data)
 {
 #if defined(DEBUG_FLUSH)
     printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
@@ -861,6 +860,11 @@  void tb_flush(CPUState *cpu)
     tcg_ctx.tb_ctx.tb_flush_count++;
 }
 
+void tb_flush(CPUState *cpu)
+{
+    async_safe_run_on_cpu(cpu, do_tb_flush, NULL);
+}
+
 #ifdef DEBUG_TB_CHECK
 
 static void
@@ -1163,9 +1167,8 @@  TranslationBlock *tb_gen_code(CPUState *cpu,
  buffer_overflow:
         /* flush must be done */
         tb_flush(cpu);
-        /* cannot fail at this point */
-        tb = tb_alloc(pc);
-        assert(tb != NULL);
+        mmap_unlock();
+        cpu_loop_exit(cpu);
     }
 
     gen_code_buf = tcg_ctx.code_gen_ptr;