diff mbox

[v2] KVM: Disable irq while unregistering user notifier

Message ID 1478286955-108360-1-git-send-email-ikalvarado@google.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ignacio Alvarado Nov. 4, 2016, 7:15 p.m. UTC
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 <ikalvarado@google.com>
---
Changelog since v1:
- Move unregistration to the beginning of kvm_on_user_return

 arch/x86/kvm/x86.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Paolo Bonzini Nov. 17, 2016, 12:42 p.m. UTC | #1
On 04/11/2016 20:15, 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 <ikalvarado@google.com>

Cc: stable@vger.kernel.org
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

> ---
> Changelog since v1:
> - Move unregistration to the beginning of kvm_on_user_return
> 
>  arch/x86/kvm/x86.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index e375235..952e19d 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -210,7 +210,18 @@ 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) {
> +		locals->registered = false;
> +		user_return_notifier_unregister(urn);
> +	}
> +	local_irq_restore(flags);
>  	for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
>  		values = &locals->values[slot];
>  		if (values->host != values->curr) {
> @@ -218,8 +229,6 @@ static void kvm_on_user_return(struct user_return_notifier *urn)
>  			values->curr = values->host;
>  		}
>  	}
> -	locals->registered = false;
> -	user_return_notifier_unregister(urn);
>  }
>  
>  static void shared_msr_update(unsigned slot, u32 msr)
> 
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář Nov. 19, 2016, 7:35 p.m. UTC | #2
2016-11-17 13:42+0100, Paolo Bonzini:
> On 04/11/2016 20:15, 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 <ikalvarado@google.com>
> 
> Cc: stable@vger.kernel.org
> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

Applied to kvm/master, thanks.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e375235..952e19d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -210,7 +210,18 @@  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) {
+		locals->registered = false;
+		user_return_notifier_unregister(urn);
+	}
+	local_irq_restore(flags);
 	for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
 		values = &locals->values[slot];
 		if (values->host != values->curr) {
@@ -218,8 +229,6 @@  static void kvm_on_user_return(struct user_return_notifier *urn)
 			values->curr = values->host;
 		}
 	}
-	locals->registered = false;
-	user_return_notifier_unregister(urn);
 }
 
 static void shared_msr_update(unsigned slot, u32 msr)