diff mbox

[RFC,1/4] spapr-hcall: take iothread lock during handler call

Message ID 1472797976-24210-2-git-send-email-nikunj@linux.vnet.ibm.com
State New
Headers show

Commit Message

Nikunj A Dadhania Sept. 2, 2016, 6:32 a.m. UTC
Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
---
 hw/ppc/spapr_hcall.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Greg Kurz Sept. 2, 2016, 8:53 a.m. UTC | #1
On Fri,  2 Sep 2016 12:02:53 +0530
Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:

> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
> ---
>  hw/ppc/spapr_hcall.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
> index e5eca67..daea7a0 100644
> --- a/hw/ppc/spapr_hcall.c
> +++ b/hw/ppc/spapr_hcall.c
> @@ -1075,20 +1075,27 @@ target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
>                               target_ulong *args)
>  {
>      sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
> +    target_ulong ret;
>  
>      if ((opcode <= MAX_HCALL_OPCODE)
>          && ((opcode & 0x3) == 0)) {
>          spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
>  
>          if (fn) {
> -            return fn(cpu, spapr, opcode, args);
> +            qemu_mutex_lock_iothread();
> +            ret = fn(cpu, spapr, opcode, args);
> +            qemu_mutex_unlock_iothread();
> +            return ret;
>          }
>      } else if ((opcode >= KVMPPC_HCALL_BASE) &&
>                 (opcode <= KVMPPC_HCALL_MAX)) {
>          spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
>  
>          if (fn) {
> -            return fn(cpu, spapr, opcode, args);
> +            qemu_mutex_lock_iothread();
> +            ret = fn(cpu, spapr, opcode, args);
> +            qemu_mutex_unlock_iothread();
> +            return ret;
>          }
>      }
>  

This will serialize all hypercalls, even when it is not needed... Isn't that
too much coarse grain locking ?

Cheers.

--
Greg
Nikunj A Dadhania Sept. 2, 2016, 9:28 a.m. UTC | #2
Greg Kurz <groug@kaod.org> writes:

> On Fri,  2 Sep 2016 12:02:53 +0530
> Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:
>
>> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
>> ---
>>  hw/ppc/spapr_hcall.c | 11 +++++++++--
>>  1 file changed, 9 insertions(+), 2 deletions(-)
>> 
>> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
>> index e5eca67..daea7a0 100644
>> --- a/hw/ppc/spapr_hcall.c
>> +++ b/hw/ppc/spapr_hcall.c
>> @@ -1075,20 +1075,27 @@ target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
>>                               target_ulong *args)
>>  {
>>      sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
>> +    target_ulong ret;
>>  
>>      if ((opcode <= MAX_HCALL_OPCODE)
>>          && ((opcode & 0x3) == 0)) {
>>          spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
>>  
>>          if (fn) {
>> -            return fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_lock_iothread();
>> +            ret = fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_unlock_iothread();
>> +            return ret;
>>          }
>>      } else if ((opcode >= KVMPPC_HCALL_BASE) &&
>>                 (opcode <= KVMPPC_HCALL_MAX)) {
>>          spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
>>  
>>          if (fn) {
>> -            return fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_lock_iothread();
>> +            ret = fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_unlock_iothread();
>> +            return ret;
>>          }
>>      }
>>  
>
> This will serialize all hypercalls, even when it is not needed... Isn't that
> too much coarse grain locking ?

You are right, I was thinking to do this only for emulation case, as
this is not needed for hardware acceleration.

Regards
Nikunj
Greg Kurz Sept. 2, 2016, 9:57 a.m. UTC | #3
On Fri, 02 Sep 2016 14:58:12 +0530
Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:

> Greg Kurz <groug@kaod.org> writes:
> 
> > On Fri,  2 Sep 2016 12:02:53 +0530
> > Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:
> >  
> >> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
> >> ---
> >>  hw/ppc/spapr_hcall.c | 11 +++++++++--
> >>  1 file changed, 9 insertions(+), 2 deletions(-)
> >> 
> >> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
> >> index e5eca67..daea7a0 100644
> >> --- a/hw/ppc/spapr_hcall.c
> >> +++ b/hw/ppc/spapr_hcall.c
> >> @@ -1075,20 +1075,27 @@ target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
> >>                               target_ulong *args)
> >>  {
> >>      sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
> >> +    target_ulong ret;
> >>  
> >>      if ((opcode <= MAX_HCALL_OPCODE)
> >>          && ((opcode & 0x3) == 0)) {
> >>          spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
> >>  
> >>          if (fn) {
> >> -            return fn(cpu, spapr, opcode, args);
> >> +            qemu_mutex_lock_iothread();
> >> +            ret = fn(cpu, spapr, opcode, args);
> >> +            qemu_mutex_unlock_iothread();
> >> +            return ret;
> >>          }
> >>      } else if ((opcode >= KVMPPC_HCALL_BASE) &&
> >>                 (opcode <= KVMPPC_HCALL_MAX)) {
> >>          spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
> >>  
> >>          if (fn) {
> >> -            return fn(cpu, spapr, opcode, args);
> >> +            qemu_mutex_lock_iothread();
> >> +            ret = fn(cpu, spapr, opcode, args);
> >> +            qemu_mutex_unlock_iothread();
> >> +            return ret;
> >>          }
> >>      }
> >>    
> >
> > This will serialize all hypercalls, even when it is not needed... Isn't that
> > too much coarse grain locking ?  
> 
> You are right, I was thinking to do this only for emulation case, as
> this is not needed for hardware acceleration.
> 

Yes, at the very least. And even in the MTTCG case, shouldn't we serialize only
when we know I/O will actually happen ?

> Regards
> Nikunj
> 
> 

Cheers.

--
Greg
Thomas Huth Sept. 2, 2016, 10:06 a.m. UTC | #4
On 02.09.2016 08:32, Nikunj A Dadhania wrote:
> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
> ---
>  hw/ppc/spapr_hcall.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
> index e5eca67..daea7a0 100644
> --- a/hw/ppc/spapr_hcall.c
> +++ b/hw/ppc/spapr_hcall.c
> @@ -1075,20 +1075,27 @@ target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
>                               target_ulong *args)
>  {
>      sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
> +    target_ulong ret;
>  
>      if ((opcode <= MAX_HCALL_OPCODE)
>          && ((opcode & 0x3) == 0)) {
>          spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
>  
>          if (fn) {
> -            return fn(cpu, spapr, opcode, args);
> +            qemu_mutex_lock_iothread();
> +            ret = fn(cpu, spapr, opcode, args);
> +            qemu_mutex_unlock_iothread();
> +            return ret;
>          }
>      } else if ((opcode >= KVMPPC_HCALL_BASE) &&
>                 (opcode <= KVMPPC_HCALL_MAX)) {
>          spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
>  
>          if (fn) {
> -            return fn(cpu, spapr, opcode, args);
> +            qemu_mutex_lock_iothread();
> +            ret = fn(cpu, spapr, opcode, args);
> +            qemu_mutex_unlock_iothread();
> +            return ret;
>          }
>      }

I think this will cause a deadlock when running on KVM since the lock is
already taken in kvm_arch_handle_exit() - which calls spapr_hypercall()!

 Thomas
Nikunj A Dadhania Sept. 3, 2016, 4:31 p.m. UTC | #5
Greg Kurz <groug@kaod.org> writes:

> On Fri, 02 Sep 2016 14:58:12 +0530
> Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:
>
>> Greg Kurz <groug@kaod.org> writes:
>> 
>> > On Fri,  2 Sep 2016 12:02:53 +0530
>> > Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> wrote:
>> >  
>> >> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
>> >> ---
>> >>  hw/ppc/spapr_hcall.c | 11 +++++++++--
>> >>  1 file changed, 9 insertions(+), 2 deletions(-)
>> >> 
>> >> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
>> >> index e5eca67..daea7a0 100644
>> >> --- a/hw/ppc/spapr_hcall.c
>> >> +++ b/hw/ppc/spapr_hcall.c
>> >> @@ -1075,20 +1075,27 @@ target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
>> >>                               target_ulong *args)
>> >>  {
>> >>      sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
>> >> +    target_ulong ret;
>> >>  
>> >>      if ((opcode <= MAX_HCALL_OPCODE)
>> >>          && ((opcode & 0x3) == 0)) {
>> >>          spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
>> >>  
>> >>          if (fn) {
>> >> -            return fn(cpu, spapr, opcode, args);
>> >> +            qemu_mutex_lock_iothread();
>> >> +            ret = fn(cpu, spapr, opcode, args);
>> >> +            qemu_mutex_unlock_iothread();
>> >> +            return ret;
>> >>          }
>> >>      } else if ((opcode >= KVMPPC_HCALL_BASE) &&
>> >>                 (opcode <= KVMPPC_HCALL_MAX)) {
>> >>          spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
>> >>  
>> >>          if (fn) {
>> >> -            return fn(cpu, spapr, opcode, args);
>> >> +            qemu_mutex_lock_iothread();
>> >> +            ret = fn(cpu, spapr, opcode, args);
>> >> +            qemu_mutex_unlock_iothread();
>> >> +            return ret;
>> >>          }
>> >>      }
>> >>    
>> >
>> > This will serialize all hypercalls, even when it is not needed... Isn't that
>> > too much coarse grain locking ?  
>> 
>> You are right, I was thinking to do this only for emulation case, as
>> this is not needed for hardware acceleration.
>> 
>
> Yes, at the very least. And even in the MTTCG case, shouldn't we serialize only
> when we know I/O will actually happen ?

Yes, haven't figured out what all would need protection apart from I/O.
I have started with coarse grain locking and will start fine tuning,
once other issues are sorted out.

Regards,
Nikunj
Nikunj A Dadhania Sept. 3, 2016, 4:33 p.m. UTC | #6
Thomas Huth <thuth@redhat.com> writes:

> On 02.09.2016 08:32, Nikunj A Dadhania wrote:
>> Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
>> ---
>>  hw/ppc/spapr_hcall.c | 11 +++++++++--
>>  1 file changed, 9 insertions(+), 2 deletions(-)
>> 
>> diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
>> index e5eca67..daea7a0 100644
>> --- a/hw/ppc/spapr_hcall.c
>> +++ b/hw/ppc/spapr_hcall.c
>> @@ -1075,20 +1075,27 @@ target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
>>                               target_ulong *args)
>>  {
>>      sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
>> +    target_ulong ret;
>>  
>>      if ((opcode <= MAX_HCALL_OPCODE)
>>          && ((opcode & 0x3) == 0)) {
>>          spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
>>  
>>          if (fn) {
>> -            return fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_lock_iothread();
>> +            ret = fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_unlock_iothread();
>> +            return ret;
>>          }
>>      } else if ((opcode >= KVMPPC_HCALL_BASE) &&
>>                 (opcode <= KVMPPC_HCALL_MAX)) {
>>          spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
>>  
>>          if (fn) {
>> -            return fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_lock_iothread();
>> +            ret = fn(cpu, spapr, opcode, args);
>> +            qemu_mutex_unlock_iothread();
>> +            return ret;
>>          }
>>      }
>
> I think this will cause a deadlock when running on KVM since the lock is
> already taken in kvm_arch_handle_exit() - which calls spapr_hypercall()!

Ouch, havent tried this branch yet on KVM :(
Will change to emulation only as suggested in my previous mails.

Regards,
Nikunj
diff mbox

Patch

diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c
index e5eca67..daea7a0 100644
--- a/hw/ppc/spapr_hcall.c
+++ b/hw/ppc/spapr_hcall.c
@@ -1075,20 +1075,27 @@  target_ulong spapr_hypercall(PowerPCCPU *cpu, target_ulong opcode,
                              target_ulong *args)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    target_ulong ret;
 
     if ((opcode <= MAX_HCALL_OPCODE)
         && ((opcode & 0x3) == 0)) {
         spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
 
         if (fn) {
-            return fn(cpu, spapr, opcode, args);
+            qemu_mutex_lock_iothread();
+            ret = fn(cpu, spapr, opcode, args);
+            qemu_mutex_unlock_iothread();
+            return ret;
         }
     } else if ((opcode >= KVMPPC_HCALL_BASE) &&
                (opcode <= KVMPPC_HCALL_MAX)) {
         spapr_hcall_fn fn = kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
 
         if (fn) {
-            return fn(cpu, spapr, opcode, args);
+            qemu_mutex_lock_iothread();
+            ret = fn(cpu, spapr, opcode, args);
+            qemu_mutex_unlock_iothread();
+            return ret;
         }
     }