Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1257746rwn; Thu, 8 Sep 2022 16:27:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR7h+LT+K2/yOtlGYg7oiufKQIXhYZdzk19Eftp+izp5s9jLMICSc1sCYBxO9vVHCq5VCgAw X-Received: by 2002:a63:2b57:0:b0:434:eabe:b4e with SMTP id r84-20020a632b57000000b00434eabe0b4emr8563287pgr.173.1662679641772; Thu, 08 Sep 2022 16:27:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662679641; cv=none; d=google.com; s=arc-20160816; b=IQ6i3KmYlEu4hwJpFvUaszIEgB4/f6oAWWNFhy+LVXHp/adPjkdimtQoXn2I541sBH KwfA7tydo17c7Vcya2ueD8aOkyulYoOXX6XGJUqK/diw8ZVGCjfunFqiRuLRtSMlDmGJ vv0MTx0Bde07I7+JvJ/wpZzQwJ/mntbrbFfofh8zQpMIBDj+ur3wgJZeO09K3D7D92ds QloRAUASdMrUcDUJ2HhhOZ7xEWJUoG1BBtoaDnOLa9iA+v9FxVNY4Rur8JY5QExpDvzb 07xbtd7/pN6NPB8PvfzTSXViUZbvz+kx0mDrxsUS8HzjSml6JlaYymdYEAgYaxC8JOX/ Iklg== 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=TgVBlPEGulOxen5Pt1r/KJVBqD1/XWdSBhZC21dGaoo=; b=mXBcX8e+GHBfdKluHeXt/j+wkOQuak6hbJo4lif537wkO+UmQQgIRqoCKaR7HUNbTD Vyh2BnD7vhD8hk/UgJuctZe31KoFchRXqdEn/Ft7NkXh++yCPqGErOxzcqOm2dlY1Qky BDQCCzz0tMx1PaLahLMVBOOIyAoG5ZGJuvQRWCu934ee/3wE0gqkBhuEM16VpUQW8jXs BnjRJhHrrp4e8UCYk/WvK8OuuBC+w1uDH5QF3DRnET3o6pt2nOp57fLz12kEH7Wbb2ZC jbXraV72KRc2kcDiTA/i2ZE5Y+FgY8RNRAWsvKDGgndhBz3jqYEn4LZS33nBvZ0OmaAH tcxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="hWj/WLck"; 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 b29-20020a631b5d000000b0043634245eb4si110906pgm.7.2022.09.08.16.27.09; Thu, 08 Sep 2022 16:27:21 -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="hWj/WLck"; 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 S229715AbiIHX0H (ORCPT + 99 others); Thu, 8 Sep 2022 19:26:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbiIHX0F (ORCPT ); Thu, 8 Sep 2022 19:26:05 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7501E3D68; Thu, 8 Sep 2022 16:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662679563; x=1694215563; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4yHIwH86LwwyESKSGChTujh8Vj2N18i6tKI9EL0jUr4=; b=hWj/WLckaVvfPS8PJAkkKNfiTLQ8WqKqjK5bsJjbuxgrPf22FSa7W+0z TqLf7ykZmmFP6Xv+CTwv1Iey4UScmaMxvw/o4hlfs4FSU2kfI4M+X18R1 PRF9Skm/EEZFxNlDVmVdASaXt3SDOrULO7OKHDoZk+KJXIDHTyIuRl55+ 3FG9thx5tjp8WRgGsigSuTRx91o0pzsspLOzpmjWRaNQ7Qec4arW9Dwbl QyXtweQprPbwHnhhvYeLLRWYD6pddAauPyOLSX0hUVlavsmuegWrKGykC Av+zAx1eCUmhXc7fRKeqcKm7OE8MM5LUyguDLt1O7PZaI+MFmvTvwqPeg w==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="298686970" X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="298686970" 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:03 -0700 X-IronPort-AV: E=Sophos;i="5.93,300,1654585200"; d="scan'208";a="610863138" 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:02 -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 , Yuan Yao Subject: [PATCH v4 01/26] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Thu, 8 Sep 2022 16:25:17 -0700 Message-Id: <066b3d43586bab4ae1f6175951c3c7af945f9107.1662679124.git.isaku.yamahata@intel.com> 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: Sean Christopherson KVM/X86 uses user return notifier to switch MSR for guest or user space. Snapshot host values on CPU online, change MSR values for guest, and restore them on returning to user space. The current code abuses kvm_arch_hardware_enable() which is called on kvm module initialization or CPU online. Remove such the abuse of kvm_arch_hardware_enable() by capturing the host value on the first change of the MSR value to guest VM instead of CPU online. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Chao Gao Reviewed-by: Yuan Yao --- arch/x86/kvm/x86.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 205ebdc2b11b..73dccc952dd1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -196,6 +196,7 @@ module_param(eager_page_split, bool, 0644); struct kvm_user_return_msrs { struct user_return_notifier urn; + bool initialized; bool registered; struct kvm_user_return_msr_values { u64 host; @@ -409,18 +410,20 @@ int kvm_find_user_return_msr(u32 msr) } EXPORT_SYMBOL_GPL(kvm_find_user_return_msr); -static void kvm_user_return_msr_cpu_online(void) +static void kvm_user_return_msr_init_cpu(struct kvm_user_return_msrs *msrs) { - unsigned int cpu = smp_processor_id(); - struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); u64 value; int i; + if (msrs->initialized) + return; + for (i = 0; i < kvm_nr_uret_msrs; ++i) { rdmsrl_safe(kvm_uret_msrs_list[i], &value); msrs->values[i].host = value; msrs->values[i].curr = value; } + msrs->initialized = true; } int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) @@ -429,6 +432,8 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); int err; + kvm_user_return_msr_init_cpu(msrs); + value = (value & mask) | (msrs->values[slot].host & ~mask); if (value == msrs->values[slot].curr) return 0; @@ -9212,7 +9217,12 @@ int kvm_arch_init(void *opaque) return -ENOMEM; } - user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); + /* + * __GFP_ZERO to ensure user_return_msrs.initialized = false. + * See kvm_user_return_msr_init_cpu(). + */ + user_return_msrs = alloc_percpu_gfp(struct kvm_user_return_msrs, + GFP_KERNEL | __GFP_ZERO); if (!user_return_msrs) { printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); r = -ENOMEM; @@ -11836,7 +11846,6 @@ int kvm_arch_hardware_enable(void) u64 max_tsc = 0; bool stable, backwards_tsc = false; - kvm_user_return_msr_cpu_online(); ret = static_call(kvm_x86_hardware_enable)(); if (ret != 0) return ret; -- 2.25.1