Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp2794622rwb; Mon, 5 Sep 2022 01:19:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR4gp/avjaC+6C7EW6y3cJ4ao5I05iDfhG0x40TTSyH35hekhWXYbCaFYJ5QeNEdo2gdUe6o X-Received: by 2002:a17:906:4bd3:b0:731:3bdf:b95c with SMTP id x19-20020a1709064bd300b007313bdfb95cmr35214704ejv.677.1662365996951; Mon, 05 Sep 2022 01:19:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662365996; cv=none; d=google.com; s=arc-20160816; b=ieHaUjxlEk5hVQczs/ToOOLb/nTV+/74PKJTfTs7jjh+AJeSAuZcyw7VQzn75LtelZ ylf8pPUjfiPpw7H5fww9rsSWmIQAMgt3b4uBxlGkiFTehaKRE6TfDXQEoO3yAsNczF38 u38VG11HYMk6Sxx6yFXcOMU6RVbhOGQUefm8Q8z2JtUi5Ekwq1bpMAQDfFoUiHkVQ0zd XnTZw1b1kQExwnSpYBkj1Pa9QKTFBtyiwr+uupafj6OzGNFuPU/G3lIBoxaODjmEDjP1 hUaQjxea4InChL3rIDnn5u15KqDDPjdnQE3IIJ/aUmpWlhef/2tRCbH+dPefvvH7AdLU pIRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=SNit+Coj1s/lsqYArXIbjYUHWcBWTORCUVPePtSIvH0=; b=RRa2N+KRFgkXtpAmpDLa6uUIGiBlU3IPDlNLmdlU3/WNagKh/YQvubVvV3DIYhUH24 3Qm/7bGC877XVDUGgoLHqxDucfg1uSS5f0W/dSigGK9kCYSR8fEsmg4OYSuwRuK7wdAb 0AFs7z8GSfxwA9WG4jr5TPznNX5oCMdNdMJ84+9OOR/DQ6DkImvxLk3u8HreZmuE+Q0y nu1Mp9C2n66yLoQCDtiXwuyMq822U9++wuTyZOEeQd6tVJ3T0CCDygNAqbr7cIH6esMb mJXin5eQXp4+IhVmXqBk7jZWg8dXQmH1HzxguVP6Ozj018pJHAGMhCw02X/US6r8eWOX 0qXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XfpOsFT4; 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 b19-20020aa7df93000000b0044877f26882si6256365edy.154.2022.09.05.01.19.32; Mon, 05 Sep 2022 01:19:56 -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=XfpOsFT4; 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 S236954AbiIEHtv (ORCPT + 99 others); Mon, 5 Sep 2022 03:49:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236921AbiIEHts (ORCPT ); Mon, 5 Sep 2022 03:49:48 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83059642D; Mon, 5 Sep 2022 00:49:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662364187; x=1693900187; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=FAccm/ScXdQpvE2J7OirVwkh6ImZ/zcbX6R20Q7tnVE=; b=XfpOsFT40GBQKW/w2575WIffurQnqELKFh1Z+j0V8XvDzekzmv5XCivJ DcbSQQ05MV/bir4xvhmdIomlGolfLY9lEHCM3PVPmaWoZz0ZpX8Wycpy3 hPfWuFdFuNV2DR3zw/4J/jHI4LAX3J8FAAdRzWSZqVg0l9qYZvm5dHwGa CSWLY2mYC+uNcHHL7LaocxI0cxk/6f5lu8Sx2gq9lwwVwEUFKI29rodO7 lw+O2RXascDsa7ya4Xi+xayK6PHwZcEjBzp+y0QBAWz+3SdfzSjDcD5/Y 5flB/5lY4YMPObTl9BZOc21CIK9RwBHSJFY1gGPNl+p6naeFGSWwgOmlG A==; X-IronPort-AV: E=McAfee;i="6500,9779,10460"; a="279347696" X-IronPort-AV: E=Sophos;i="5.93,290,1654585200"; d="scan'208";a="279347696" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2022 00:49:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,290,1654585200"; d="scan'208";a="675176865" Received: from yy-desk-7060.sh.intel.com (HELO localhost) ([10.239.159.76]) by fmsmga008.fm.intel.com with ESMTP; 05 Sep 2022 00:49:42 -0700 Date: Mon, 5 Sep 2022 15:49:41 +0800 From: Yuan Yao To: isaku.yamahata@intel.com Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon , isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: Re: [PATCH v3 07/22] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section Message-ID: <20220905074941.apb7wmkhz47zx3c6@yy-desk-7060> References: <04733398cea6ec59827e886a2430482fc258933a.1662084396.git.isaku.yamahata@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <04733398cea6ec59827e886a2430482fc258933a.1662084396.git.isaku.yamahata@intel.com> User-Agent: NeoMutt/20171215 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 On Thu, Sep 01, 2022 at 07:17:42PM -0700, isaku.yamahata@intel.com wrote: > 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 > --- > 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 278eb6cc7cbe..db1303e2abc9 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -5011,13 +5011,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)); Reviewed-by: Yuan Yao > + > 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) > @@ -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 >