[Devel] [PATCH vz8 6/7] kvm: fix race between mmu_shrink_scan() and VM destroy

Konstantin Khorenko khorenko at virtuozzo.com
Thu Jun 10 16:36:18 MSK 2021


merged into
[PATCH vz8 5/7] kvm: move actual VM memory shrink out of kvm_lock

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

On 06/08/2021 08:58 PM, Valeriy Vdovin wrote:
> From: Konstantin Khorenko <khorenko at virtuozzo.com>
>
> Honor the race between VM memory shrink and VM destroy,
> just skip dying VMs, shrink alive ones.
>
> Fixes: 05a623470d4b ("kvm: move actual VM memory shrink out of kvm_lock")
> https://jira.sw.ru/browse/PSBM-95077
>
> Reported-by: Kirill Tkhai <ktkhai at virtuozzo.com>
> Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
> (cherry-picked from f0eae7e0c99f258f4c41e3d098b67732845ce80e)
> https://jira.sw.ru/browse/PSBM-127849
> Signed-off-by: Valeriy Vdovin <valeriy.vdovin at virtuozzo.com>
> ---
>  arch/x86/kvm/mmu/mmu.c   | 7 ++++++-
>  include/linux/kvm_host.h | 1 +
>  virt/kvm/kvm_main.c      | 6 ++++++
>  3 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 78f0d1d19c94..79ec523a29c9 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -6114,7 +6114,12 @@ mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
>  		    !kvm_has_zapped_obsolete_pages(kvm))
>  			continue;
>
> -		kvm_get_kvm(kvm);
> +		/*
> +		 * If try_get fails, we race with last kvm_put_kvm(),
> +		 * so skip the VM, it will die soon anyway.
> +		 */
> +		if (!kvm_try_get_kvm(kvm))
> +			continue;
>  		mutex_unlock(&kvm_lock);
>
>  		idx = srcu_read_lock(&kvm->srcu);
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index fcee0b5522d1..cad5f2cec162 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -626,6 +626,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
>  void kvm_exit(void);
>
>  void kvm_get_kvm(struct kvm *kvm);
> +int kvm_try_get_kvm(struct kvm *kvm);
>  void kvm_put_kvm(struct kvm *kvm);
>  void kvm_put_kvm_no_destroy(struct kvm *kvm);
>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 51572037eb0c..477c67fcd41e 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -845,6 +845,12 @@ void kvm_get_kvm(struct kvm *kvm)
>  }
>  EXPORT_SYMBOL_GPL(kvm_get_kvm);
>
> +int kvm_try_get_kvm(struct kvm *kvm)
> +{
> +	return refcount_inc_not_zero(&kvm->users_count);
> +}
> +EXPORT_SYMBOL_GPL(kvm_try_get_kvm);
> +
>  void kvm_put_kvm(struct kvm *kvm)
>  {
>  	if (refcount_dec_and_test(&kvm->users_count))
>


More information about the Devel mailing list