Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1844827pxb; Wed, 9 Feb 2022 05:49:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJxSGv+L+VzWcqM9WOUWvdZS2l2VYC95THfWcaNwl4lKWIvSEvM3rm61x2gMqXiV7EoyZpA4 X-Received: by 2002:a63:2c87:: with SMTP id s129mr1931253pgs.327.1644414595149; Wed, 09 Feb 2022 05:49:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644414595; cv=none; d=google.com; s=arc-20160816; b=BRE8RENLc6ibZEv6r2HDIf3ViFxScnejMoPe35T6AENa7y3trlfOs7yz9QXab8LtUr WCVibAJJZdSv2xXDZGOe4uKWnzNI9bo3gjoa+qhIM+/0WXRXT4+w9RhuQdxX3m9+VhMM Hezoe0MJYQbzph4ZaLPJ0SpHEkQZ8ZzT/VDTFGqUKZmD+j3AjAwT+5aPg9DmvFpT535Z 6KMyv7n9UlvCvke4S5U0qDO2a5mm1B8o4/9UYvi/9hWfjPndLqp7gX/6lWAOFAh3RhX0 DJBHWfnnLBQjRHrSYLLTBfNfNaX+QDo2Kbn897FWKr6LTxUJuNm/prvnEXBWu2V5kh35 mcIA== 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=eejJVIgKd/l3I5PF+Nc4Hj3X4+cAVMOhNQxIlpNCj8Q=; b=P3GWrMumodi8vDl0gEW6wtjCighe7arAPpUb+iP/3tGq7pCUGo3kac+lp1N0lR6deI pPzLm70tXEHY1NM1ZDOZGsNEHCR/uuLOOzmyhjYJeEERaHHxWNdviLmfAYDuI11v5beC 3xjj62F8bVQQ0fEc5nLiGpTPxtR+GVR/l8O89DobxovP+VuClm6RbGYH+CWb6OO5Q9dd sjXGocAy/xq3Pz0emF+7YNUUdJPAIKgOTM2LKuZdHFH7JC588FjAbX92pdTlT12idurl UgxKiJ9DrQJ/XyRcdGbB9vTkOYuwUOawIoIosuiM5JsoafSRv0VXKrvMLYR2cpDUcynC oZ6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HXM8DYgI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id i1si9110908pla.113.2022.02.09.05.49.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 05:49:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HXM8DYgI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 305F9E159E95; Wed, 9 Feb 2022 02:30:14 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239479AbiBIHm5 (ORCPT + 99 others); Wed, 9 Feb 2022 02:42:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239052AbiBIHm0 (ORCPT ); Wed, 9 Feb 2022 02:42:26 -0500 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F269C05CB8C; Tue, 8 Feb 2022 23:42:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644392550; x=1675928550; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7oQ6ZbaKyf2t7+Jw+hgZ1jRXBsKBASTYCG0C9ywN06s=; b=HXM8DYgIgu+HrFUd0SQ/W/J2jWWah/qW4cZV1v9IwuCvgkddlLoet0wq s/B+zBwvdmLF5UYMOMix13Ko3YcDoIs8DivM373dCv39/geZ3Ib48fBhD R0GLY8jcOQ7i3L4L6IMMc5U0tWYu1ITR4f2y72R/lteFrXCedWRRJHtEy Fb3bDsf+dzT4wxaxiA4mFNkQQFrHJzxQAit5A9Q8LZ5LwrAv984Dvs1dz WNEdW2MXj1kdvM7I8g9cyarjvvF1W1ST3JJbmEdGE4QKZ6xAPWlpojG86 dE1h1PjH788r/iFKdYuviQzuXmdK0Rhxf4ErQsMRf/54O+oD2FLpTH+7s w==; X-IronPort-AV: E=McAfee;i="6200,9189,10252"; a="248907359" X-IronPort-AV: E=Sophos;i="5.88,355,1635231600"; d="scan'208";a="248907359" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2022 23:41:58 -0800 X-IronPort-AV: E=Sophos;i="5.88,355,1635231600"; d="scan'208";a="540984736" Received: from hyperv-sh4.sh.intel.com ([10.239.48.22]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2022 23:41:54 -0800 From: Chao Gao To: kvm@vger.kernel.org, seanjc@google.com, pbonzini@redhat.com, kevin.tian@intel.com, tglx@linutronix.de Cc: Chao Gao , John Garry , Will Deacon , Qi Liu , Thomas Richter , Shaokun Zhang , Hector Martin , Huang Ying , linux-kernel@vger.kernel.org Subject: [PATCH v3 4/5] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section Date: Wed, 9 Feb 2022 15:41:05 +0800 Message-Id: <20220209074109.453116-5-chao.gao@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220209074109.453116-1-chao.gao@intel.com> References: <20220209074109.453116-1-chao.gao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 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 --- 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 773c83730906..14d354c8ce35 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -182,7 +182,6 @@ enum cpuhp_state { CPUHP_AP_CSKY_TIMER_STARTING, CPUHP_AP_TI_GP_TIMER_STARTING, CPUHP_AP_HYPERV_TIMER_STARTING, - CPUHP_AP_KVM_STARTING, CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING, CPUHP_AP_KVM_ARM_VGIC_STARTING, CPUHP_AP_KVM_ARM_TIMER_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 23481fd746aa..f60724736cb1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4853,13 +4853,27 @@ static void hardware_enable_nolock(void *caller_name) } } -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((void *)__func__); + 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) @@ -4872,7 +4886,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) @@ -5641,8 +5655,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); @@ -5705,7 +5719,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: @@ -5731,7 +5745,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