Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1223662rwb; Thu, 22 Sep 2022 11:50:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM67cob4HkKgzvWcr4z9eomxT4vNQ8MnjH+akcHoCAcpbXgNmBUWFYkTrcUgJf4CYRebhueb X-Received: by 2002:a17:906:9be4:b0:77a:a1c:a624 with SMTP id de36-20020a1709069be400b0077a0a1ca624mr4076283ejc.172.1663872606920; Thu, 22 Sep 2022 11:50:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663872606; cv=none; d=google.com; s=arc-20160816; b=Qqx5n3/qsfCHsIbTMurhgdyx1iLAvUVkHcneTbfCYz1m4SNAVJE1M4dX59yDZc6j0b QbcsfFZpwqTP+nglZdGkJxMWBSn9fAqpTRMIgcb+id66dj7dMkgeWeGbmeXVkGDuz675 26TZqlHtXGT+hcyrAEX+ryOnryr0bR8mdHUs/gBgosBw+YjzcBEtd3yHK4ef5leHrd1J hA1bwWHvsY6hAQ87lds08Wd6JQt0kP0LYjyW7FVCzCmsf0XMDidHALMUkiNr7xiplx1D 47izwr9ARaE2yzmnfv5aO811mexJVm76KyfJrCEjGUvJgePH5osA/9Hj/5PsfDOioqQY NXpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Mm+FGTDvuTZucIEsUPIgmTkSG6PI3oLrYxI4jtzXNQY=; b=YhA5RrLIoFOLmlmvRrbgmf/ygtjeZK0tEXnqLwiyjbs38jyUMdMgPnWz7Hsf3XGLDk 6lOUg5WT+5boBHQSUdNrPC86m63gNd1Z+625C8GmRAYcSquJWCtlERauNiocR5hzDUcY rmyQGVVLGFwOw9KLnWD1hPMcHutxaTNBiNYhyAgqh2/reppXH9fX4GortzYu70d1rKsA qDn9EVcf6HTeiSjbcKmKuaOF6BOPFyRK8bd4BaD79iGqOPkqOM8dBiQnU+tjLiicVp7f oX8TUmYVHqdie4QPGGGDcCgZ7pg/JVcRS5wwv/49ZiniaM0B/ODxxZrfW1ryzlf1GF4M QUmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=J7W3ya0Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g15-20020a056402320f00b0045515cd7e54si2566069eda.350.2022.09.22.11.49.40; Thu, 22 Sep 2022 11:50:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=J7W3ya0Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232499AbiIVSWJ (ORCPT + 99 others); Thu, 22 Sep 2022 14:22:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231555AbiIVSVZ (ORCPT ); Thu, 22 Sep 2022 14:21:25 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AED1010AB10; Thu, 22 Sep 2022 11:21:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870884; x=1695406884; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=drh4+aWR2QC13n5vYRunnLDCblNzsS8rJFTrdTBFtzk=; b=J7W3ya0YIOvYRyqx58ZU6khijaUjffuLSYTqFZHipg8BaC9sWfRN/6ML LUhQYMB/6tQvNYL8YDYYG88KizoDikmaW6rTbjW4u6zoSbU0rs95TMzX6 UYMXYzlHemgBX0XbSARb1PABceK5H8cCrkhdwo9qYxdra9x2KMFlSrw+i JqFvhoKG5BLGNBrgJLSdKXxInzOpGmAa5iT9Pm7kDRnQ0bhLrxot4qxk5 udj5G7p6+etXFAXUP0p7UH98X9Bo15LJlrZbpJhz2abIWIv8NYcvEb6A5 oX6C1WwlU3ky4ggpOdLYgSi+2hPmDPde2Jlzwd0tTYP7UhKpX5oEnG0cB w==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675967" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675967" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:16 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086673" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:16 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 07/30] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section Date: Thu, 22 Sep 2022 11:20:36 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chao Gao The CPU STARTING section doesn't allow callbacks to fail. Move KVM's hotplug callback to ONLINE section so that it can abort onlining a CPU in certain cases to avoid potentially breaking VMs running on existing CPUs. For example, when kvm fails to enable hardware virtualization on the hotplugged CPU. Place KVM's hotplug state before CPUHP_AP_SCHED_WAIT_EMPTY as it ensures when offlining a CPU, all user tasks and non-pinned kernel tasks have left the CPU, i.e. there cannot be a vCPU task around. So, it is safe for KVM's CPU offline callback to disable hardware virtualization at that point. Likewise, KVM's online callback can enable hardware virtualization before any vCPU task gets a chance to run on hotplugged CPUs. KVM's CPU hotplug callbacks are renamed as well. Suggested-by: Thomas Gleixner Signed-off-by: Chao Gao Link: https://lore.kernel.org/r/20220216031528.92558-6-chao.gao@intel.com Reviewed-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Yuan Yao --- include/linux/cpuhotplug.h | 2 +- virt/kvm/kvm_main.c | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index 7337414e4947..de45be38dd27 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -185,7 +185,6 @@ enum cpuhp_state { CPUHP_AP_CSKY_TIMER_STARTING, CPUHP_AP_TI_GP_TIMER_STARTING, CPUHP_AP_HYPERV_TIMER_STARTING, - CPUHP_AP_KVM_STARTING, /* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING, CPUHP_AP_ARM_XEN_STARTING, @@ -200,6 +199,7 @@ enum cpuhp_state { /* Online section invoked on the hotplugged CPU from the hotplug thread */ CPUHP_AP_ONLINE_IDLE, + CPUHP_AP_KVM_ONLINE, CPUHP_AP_SCHED_WAIT_EMPTY, CPUHP_AP_SMPBOOT_THREADS, CPUHP_AP_X86_VDSO_VMA_ONLINE, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4f19c47aab1c..1bb7038e1ecf 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5011,13 +5011,27 @@ static void hardware_enable_nolock(void *junk) } } -static int kvm_starting_cpu(unsigned int cpu) +static int kvm_online_cpu(unsigned int cpu) { + int ret = 0; + raw_spin_lock(&kvm_count_lock); - if (kvm_usage_count) + /* + * Abort the CPU online process if hardware virtualization cannot + * be enabled. Otherwise running VMs would encounter unrecoverable + * errors when scheduled to this CPU. + */ + if (kvm_usage_count) { + WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); + hardware_enable_nolock(NULL); + if (atomic_read(&hardware_enable_failed)) { + atomic_set(&hardware_enable_failed, 0); + ret = -EIO; + } + } raw_spin_unlock(&kvm_count_lock); - return 0; + return ret; } static void hardware_disable_nolock(void *junk) @@ -5030,7 +5044,7 @@ static void hardware_disable_nolock(void *junk) kvm_arch_hardware_disable(); } -static int kvm_dying_cpu(unsigned int cpu) +static int kvm_offline_cpu(unsigned int cpu) { raw_spin_lock(&kvm_count_lock); if (kvm_usage_count) @@ -5841,8 +5855,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, goto out_free_2; } - r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting", - kvm_starting_cpu, kvm_dying_cpu); + r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online", + kvm_online_cpu, kvm_offline_cpu); if (r) goto out_free_2; register_reboot_notifier(&kvm_reboot_notifier); @@ -5903,7 +5917,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, kmem_cache_destroy(kvm_vcpu_cache); out_free_3: unregister_reboot_notifier(&kvm_reboot_notifier); - cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); + cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); out_free_2: kvm_arch_hardware_unsetup(); out_free_1: @@ -5929,7 +5943,7 @@ void kvm_exit(void) kvm_async_pf_deinit(); unregister_syscore_ops(&kvm_syscore_ops); unregister_reboot_notifier(&kvm_reboot_notifier); - cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); + cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE); on_each_cpu(hardware_disable_nolock, NULL, 1); kvm_arch_hardware_unsetup(); kvm_arch_exit(); -- 2.25.1