Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1282474rwn; Thu, 8 Sep 2022 17:00:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR7Q2V9dqvHjb5pb7V6LrR8jKeMNpAnfajKXIAZUA/Yro61RUb4nQp9STi5tnNFl3QBfCl1y X-Received: by 2002:a2e:9a88:0:b0:26a:c4d2:3418 with SMTP id p8-20020a2e9a88000000b0026ac4d23418mr3418443lji.81.1662681613036; Thu, 08 Sep 2022 17:00:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662681613; cv=none; d=google.com; s=arc-20160816; b=mKJALBbq4Ps9qscfTiPx/bSDGTTzyvEZQfRO23Iw/8dLJhi7df7KEJWPmMiKKBPtev lZKcboz0tpGl3+9+JOoI0M5hPnqEe1YPDyO02njTWOB908IKD3WJnEM8Z1NhW7h0eBhL axyLZe0LgkrsaX7tmvX0b3H0UU/3rxGvOkZG8Uxi9ERU7qpeUITBENUR/zZex5SKd0rs Pb5cfTFyDBJcKgq9ldSmuV/+DKJj/2twvpFnwZIfJnuZrjd0VePIIFGlZYJIyF5uz+5+ xOcu9E/0ZQbcQSU0Aa1ywyuJ8dBgdG0VsvInNLoBnmOs90k9kBxp3ZxtdZ7isfxz2GFr ThHA== 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=37JjrV/ZHeVDkJvA81Qoq8MG0w7nzP6HjOsNd0X2c/U=; b=YcN2jKzWcmh/Bb8Hdy/A7Wv6PNC3bT+4WKgoa6AM5XSMcxQXHtmNGp5uhoKrzYHtSb KE/xK6elQclKpARhfkpc7TisBwRtv5tWlwPxzpOdYVhAER8Kn/cjoKAZ1buAsseA/1Ji 75CL+Dv0G4Z9KbaA2a3oBI08CxOusnK+vmv8uvNKQNofiZm87xgaykt8VIc4L7IqHnIr P78iiKiYyy4Jfq+XxuXAEhIMDYJdQ3RXl++hzu9HE4NxqOPt08LfuBUsu80Hb/lfXfJL Enc1pxCb0zFou8ja+hn1+VJs0ZgvOoF4BDJdjhT0R1fsxBrtOfzrLk2GaAedAnlIFd// dEEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AqVvACoA; 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 d10-20020a0565123d0a00b0048b38f379e4si119115lfv.323.2022.09.08.16.59.44; Thu, 08 Sep 2022 17:00:13 -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=AqVvACoA; 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 S230164AbiIHX1Z (ORCPT + 99 others); Thu, 8 Sep 2022 19:27:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230179AbiIHX0g (ORCPT ); Thu, 8 Sep 2022 19:26:36 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 928211098E1; Thu, 8 Sep 2022 16:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679576; x=1694215576; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BCW0b+RqTHG/HSgdegqcNoUHAj5WKFiSkIrcMdx/w4M=; b=AqVvACoAXGyMS6XxsSjLWnIVwdrcBw/iKxj7K92fxGhSEJHI8a6/kAFt qmXCkQSGkX+k0c20Zam8aqTG/mqThtxgDAafXuZIQqBKmknTtgSQ+LNKQ ELQ3Ku9hQCeAp4LpGu8ZSe9pGezWm7gnvRsl+KT8A79iBCNPnYkhiWiUQ UYYLin+R9eqTQkZuDiYMc1Ns9BkWJkm/tpi9T7n9DozS1lpW/oDV+TKIf YP2r/jO266ZJtCqTqCDIyVw0Y9oPnP9NsCAqdDAlEJgU68lYLFhBlw5gW UxEJAr66VWoK0Jb9MqfkBOIAbHXz35Qm+f2a8lx9BkkUEiPZI+EYab1D5 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298687023" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298687023" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863256" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Sep 2022 16:26:12 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Will Deacon , Yuan Yao Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen Subject: [PATCH v4 18/26] KVM: x86: Duplicate arch callbacks related to pm events and compat check Date: Thu, 8 Sep 2022 16:25:34 -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,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 From: Isaku Yamahata KVM/X86 can change those callbacks without worrying about breaking other archs. Suggested-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/x86.c | 168 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 163 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 23623b6a789b..feee7739219e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11845,6 +11845,169 @@ void kvm_arch_hardware_disable(void) drop_user_return_notifiers(); } +static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE; + +int kvm_arch_post_init_vm(struct kvm *kvm) +{ + return kvm_mmu_post_init_vm(kvm); +} + +static int __hardware_enable(void *caller_name) +{ + int cpu = raw_smp_processor_id(); + int r; + + WARN_ON_ONCE(preemptible()); + + if (cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return 0; + r = kvm_arch_hardware_enable(); + if (r) + pr_warn("kvm: enabling virtualization on CPU%d failed during %s()\n", + cpu, (const char *)caller_name); + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed = arg; + + if (__hardware_enable((void *)__func__)) + atomic_inc(failed); +} + +static void hardware_disable(void *junk) +{ + int cpu = raw_smp_processor_id(); + + WARN_ON_ONCE(preemptible()); + + if (!cpumask_test_cpu(cpu, &cpus_hardware_enabled)) + return; + cpumask_clear_cpu(cpu, &cpus_hardware_enabled); + kvm_arch_hardware_disable(); +} + +void kvm_arch_pre_hardware_unsetup(void) +{ + on_each_cpu(hardware_disable, NULL, 1); +} + +/* + * Called after the VM is otherwise initialized, but just before adding it to + * the vm_list. + */ +int kvm_arch_add_vm(struct kvm *kvm, int usage_count) +{ + atomic_t failed; + int r = 0; + + if (usage_count != 1) + return 0; + + atomic_set(&failed, 0); + on_each_cpu(hardware_enable, &failed, 1); + + if (atomic_read(&failed)) { + r = -EBUSY; + goto err; + } + + r = kvm_arch_post_init_vm(kvm); +err: + if (r) + on_each_cpu(hardware_disable, NULL, 1); + return r; +} + +int kvm_arch_del_vm(int usage_count) +{ + if (usage_count) + return 0; + + on_each_cpu(hardware_disable, NULL, 1); + return 0; +} + +static void check_processor_compat(void *rtn) +{ + *(int *)rtn = kvm_arch_check_processor_compat(); +} + +int kvm_arch_check_processor_compat_all(void) +{ + int cpu; + int r; + + for_each_online_cpu(cpu) { + smp_call_function_single(cpu, check_processor_compat, &r, 1); + if (r < 0) + return r; + } + return 0; +} + +int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int ret; + + ret = kvm_arch_check_processor_compat(); + if (ret) + return ret; + + if (!usage_count) + return 0; + + /* + * arch callback kvm_arch_hardware_eanble() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + /* + * Abort the CPU online process if hardware virtualization cannot + * be enabled. Otherwise running VMs would encounter unrecoverable + * errors when scheduled to this CPU. + */ + ret = __hardware_enable((void *)__func__); + preempt_enable(); + + return ret; +} + +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) { + /* + * arch callback kvm_arch_hardware_disable() assumes that + * preemption is disabled for historical reason. Disable + * preemption until all arch callbacks are fixed. + */ + preempt_disable(); + hardware_disable(NULL); + preempt_enable(); + } + return 0; +} + +int kvm_arch_reboot(int val) +{ + on_each_cpu(hardware_disable, NULL, 1); + return NOTIFY_OK; +} + +int kvm_arch_suspend(int usage_count) +{ + if (usage_count) { + preempt_disable(); + hardware_disable(NULL); + preempt_enable(); + } + return 0; +} + void kvm_arch_resume(int usage_count) { struct kvm *kvm; @@ -12122,11 +12285,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) return ret; } -int kvm_arch_post_init_vm(struct kvm *kvm) -{ - return kvm_mmu_post_init_vm(kvm); -} - static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) { vcpu_load(vcpu); -- 2.25.1