Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp86260rwa; Fri, 19 Aug 2022 23:04:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR5AT2lmRNaPgKqP6BSaOtYRLDrNbL64vJPgLNAp4I03eRQlCzmzWOAef3DjDC3aa/g36O/B X-Received: by 2002:a17:903:32cd:b0:16f:c31:7034 with SMTP id i13-20020a17090332cd00b0016f0c317034mr10502467plr.126.1660975449558; Fri, 19 Aug 2022 23:04:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660975449; cv=none; d=google.com; s=arc-20160816; b=tdqCnBT+KOC72Tv70EIjhIcd2nRY8ox1JDpbLugHj1jND2PHcXvCGAQWnOcLHAub+1 qL4oWqdVLdUw11TulR8Sqk+j79Q1Z1fygdAzGa8yw9ezcqC82fWOYFKaDr8z0duoFt4U geMtboHUbweGbYXfajZ8abViXLlamZK6OiRPe3bPP2/jj22m1XqprwlhQvkM3dcb0JgO g7lkqZSaEl/7LP28jcDpogK7JXfGvF8+DGJ2+sC5xX0i5eVaPVzJ1gJtx9XqOPell12C ya3mUu1s2qyPzjzFmdOjsLvBrCT/BkZLotvdd45DfqkbuFfiUvtAQBj3Hs4HUke7zXfq 5S4w== 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=3DBzpgXcDarpCw2UZvBKGAqbfDEkkoZhN9fnXX191qM=; b=G/QJkB41h6kP6acWqTzG8nBE2LLX8CkGXIwfXxbHDt0jNQ+MsAU+gp2ATxLBpw+B+N ECW/mznrDIMkGYpT7y1/EoRsI2FNRnbdH1bBSNJO2mHRx31oYxNSL9xFpuBr4RRrAU3m A2Vpr3wL+tU42GUzKQ3EuLpEvrVNbvTvrx5awSzJVIjFZUWgHmFVpj9UHzwXOR1NlKSR m2Dle/vnn/FbLG+98as/Cgq2kp75ouT/96YoLTQzaXdCC35JgCdGTK6H+8hklszEXDbO alqCRheiKsvp8+2KO8FOtq9MNqYVJBdQCLVKMTYaH8dvZNl3P5JVR5TKZvgSIOczv9oP QaGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jTS7XwU6; 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 s11-20020a170902ea0b00b00170a75247besi6277625plg.346.2022.08.19.23.03.58; Fri, 19 Aug 2022 23:04:09 -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=jTS7XwU6; 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 S245710AbiHTGBv (ORCPT + 99 others); Sat, 20 Aug 2022 02:01:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244498AbiHTGAx (ORCPT ); Sat, 20 Aug 2022 02:00:53 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3915A260A; Fri, 19 Aug 2022 23:00:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1660975252; x=1692511252; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tz2myrWkcxiLhdua2HaqakBscUxnMz1ROup5PGWdHyk=; b=jTS7XwU6MPL3DL0cjYIIm2jSpmI2cUaMipFWtSHdj8X4u50IpshE64gP d/vxNzu+EoBfBUzQkkqkemmJtt7FxlipAh0Xcw2aMpbvnTLQVZg2OQ8LI yQY1YqvThEME123pF9bikpFG3kFyCFZjuiYlQ4gwbX+/Y9v5Wf2bWmQes xDOfqErX1zArVT74qsx02Ilxh8CcleAIua5rj7N8PcYWPRb/jK9vo0Svu nEhZh4aQ9zmz1qAILCzaWRRIh+iHNbwnX5S3eoHa9O9Nq/8c6AAFoG+AV MyaJqp5QMhZwvJRoa2bLCqX7M13BVibg4dzxIryZe6FDS87obTu0mJwDj g==; X-IronPort-AV: E=McAfee;i="6500,9779,10444"; a="379448980" X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="379448980" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2022 23:00:50 -0700 X-IronPort-AV: E=Sophos;i="5.93,250,1654585200"; d="scan'208";a="668857551" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2022 23:00:50 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson , Kai Huang , Chao Gao , Will Deacon Subject: [RFC PATCH 13/18] KVM: x86: Duplicate arch callbacks related to pm events Date: Fri, 19 Aug 2022 23:00:19 -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 | 125 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0b112cd7de58..71e90d0f0da9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11841,6 +11841,124 @@ 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) +{ + 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_info("kvm: enabling virtualization on CPU%d failed\n", cpu); + else + cpumask_set_cpu(cpu, &cpus_hardware_enabled); + return r; +} + +static void hardware_enable(void *arg) +{ + atomic_t *failed = arg; + + if (__hardware_enable()) + 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 && usage_count == 1) + 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; +} + +int kvm_arch_online_cpu(unsigned int cpu, int usage_count) +{ + int r; + + if (!usage_count) + return 0; + + r = kvm_arch_check_processor_compat(); + if (r) + return r; + return __hardware_enable(); +} + +int kvm_arch_offline_cpu(unsigned int cpu, int usage_count) +{ + if (usage_count) + hardware_disable(NULL); + 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) + hardware_disable(NULL); + return 0; +} + void kvm_arch_resume(int usage_count) { struct kvm *kvm; @@ -11853,6 +11971,8 @@ void kvm_arch_resume(int usage_count) if (!usage_count) return; + if (kvm_arch_check_processor_compat()) + return; if (kvm_arch_hardware_enable()) return; @@ -12104,11 +12224,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