[Devel,1/1] KVM: Disable irq while unregistering user notifier

Submitted by Evgeny Yakovlev on Nov. 22, 2016, 9:09 a.m.

Details

Message ID 1479805775-550176-2-git-send-email-eyakovlev@virtuozzo.com
State New
Series "KVM: Disable irq while unregistering user notifier"
Headers show

Commit Message

Evgeny Yakovlev Nov. 22, 2016, 9:09 a.m.
From: Ignacio Alvarado <ikalvarado@google.com>

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>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

Backport of https://patchwork.kernel.org/patch/9413131/
Fixes https://jira.sw.ru/browse/PSBM-55099
Signed-off-by: Evgeny Yakovlev <eyakovlev@virtuozzo.com>
---
 arch/x86/kvm/x86.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ea058a2..4b2754b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -198,7 +198,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) {
@@ -206,8 +217,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)

Comments

Konstantin Khorenko Nov. 23, 2016, 3:35 p.m.
Please next time mention mainstream commit like:

ms commit: 1650b4e ("KVM: Disable irq while unregistering user notifier")

Thank you.

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

On 11/22/2016 12:09 PM, Evgeny Yakovlev wrote:
> From: Ignacio Alvarado <ikalvarado@google.com>
>
> 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>
> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
>
> Backport of https://patchwork.kernel.org/patch/9413131/
> Fixes https://jira.sw.ru/browse/PSBM-55099
> Signed-off-by: Evgeny Yakovlev <eyakovlev@virtuozzo.com>
> ---
>  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 ea058a2..4b2754b 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -198,7 +198,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) {
> @@ -206,8 +217,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)
>