From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ignacio Alvarado Subject: Re: [PATCH] KVM: Disable irq while unregistering user notifier Date: Fri, 4 Nov 2016 11:56:15 -0700 Message-ID: References: <1477087712-175064-1-git-send-email-ikalvarado@google.com> <61b5a6c3-9a30-feef-869b-bf9251454381@redhat.com> <91cb7682-cf2a-b90c-b4c6-ea50f58acdc4@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Cc: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , kvm@vger.kernel.org, =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , David Matlack To: Paolo Bonzini Return-path: Received: from mail-it0-f54.google.com ([209.85.214.54]:37524 "EHLO mail-it0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932299AbcKDS4Q (ORCPT ); Fri, 4 Nov 2016 14:56:16 -0400 Received: by mail-it0-f54.google.com with SMTP id u205so1771044itc.0 for ; Fri, 04 Nov 2016 11:56:16 -0700 (PDT) In-Reply-To: <91cb7682-cf2a-b90c-b4c6-ea50f58acdc4@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Fri, Nov 4, 2016 at 2:48 AM, Paolo Bonzini wrote: > > > On 03/11/2016 23:26, Ignacio Alvarado wrote: >> The risk would still be present since fire_user_return_notifiers could >> potentially run at any time and kvm_on_user return could be called >> from drop_user_return_notifiers either from the kvm_put_kvm path or >> the kvm_arch_exit path. > > So, do the unregistration > > locals->registered = false; > user_return_notifier_unregister(urn); > > first in kvm_on_user_return? Nothing else needs to run with IRQs > disabled, and doing it first prevents reentering the code from > kvm_arch_hardware_disable. > Will do. > Paolo > >> Regards >> >> On Wed, Nov 2, 2016 at 10:25 AM, Paolo Bonzini wrote: >>> >>> >>> On 22/10/2016 00:08, Ignacio Alvarado wrote: >>>> Function user_notifier_unregister should be called only once for each >>>> registered user notifier. >>>> >>>> Function kvm_arch_hardware_disable can be executed from an IPI context >>>> which could cause a race condition with a VCPU returning to user mode >>>> and attempting to unregister the notifier. >>>> >>>> Signed-off-by: Ignacio Alvarado >>> >>> Should drop_user_return_notifiers instead be moved to kvm_arch_exit? >>> >>> Thanks, >>> >>> Paolo >>> >>>> --- >>>> arch/x86/kvm/x86.c | 10 ++++++++++ >>>> 1 file changed, 10 insertions(+) >>>> >>>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >>>> index e375235..51f87f0 100644 >>>> --- a/arch/x86/kvm/x86.c >>>> +++ b/arch/x86/kvm/x86.c >>>> @@ -210,7 +210,15 @@ static void kvm_on_user_return(struct user_return_notifier *urn) >>>> struct kvm_shared_msrs *locals >>>> = container_of(urn, struct kvm_shared_msrs, urn); >>>> struct kvm_shared_msr_values *values; >>>> + unsigned long flags; >>>> >>>> + /* >>>> + * Disabling irqs at this point since the following code could be >>>> + * interrupted and executed through kvm_arch_hardware_disable() >>>> + */ >>>> + local_irq_save(flags); >>>> + if (!locals->registered) >>>> + goto out; >>>> for (slot = 0; slot < shared_msrs_global.nr; ++slot) { >>>> values = &locals->values[slot]; >>>> if (values->host != values->curr) { >>>> @@ -220,6 +228,8 @@ static void kvm_on_user_return(struct user_return_notifier *urn) >>>> } >>>> locals->registered = false; >>>> user_return_notifier_unregister(urn); >>>> +out: >>>> + local_irq_restore(flags); >>>> } >>>> >>>> static void shared_msr_update(unsigned slot, u32 msr) >>>> >> >> >> -- Ignacio Alvarado