Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1260016rwb; Thu, 22 Sep 2022 12:21:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6iJrKERyFUC+PC9/GExfj9oSrIONUAAjwPJeX5fkFKr5ZMKCh7o9CYlJUUvk8LTeGCCcXz X-Received: by 2002:a17:907:7209:b0:780:33b4:43a5 with SMTP id dr9-20020a170907720900b0078033b443a5mr4272426ejc.547.1663874488465; Thu, 22 Sep 2022 12:21:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663874488; cv=none; d=google.com; s=arc-20160816; b=UtHixC2CZcv0AGnHgw0ec2rrC23plek8AI0HZHFIrPgSh/2TNW61+VpfPkVNVjKCAw qFxrTHyfw+CzGm9nFSMT+KYmhNbl/3lumKZt61SbUV04cHx1XsY0JtOb6wEEgYyr+o0U ermmJd8catNoqsbHql+uNJJCLsT+UTHi4nh8sJwDzeFjOFUtRHSWDsUtIwuumCEwUbgB nX9y09gp404iuihxvXkjU7Ua0Oc6gO+82PSU/NR87O99eVtgJThFYM5JJS/QrWyeDt4S heaDgxJM8+XgQc0Ql1eEv9uyKPBrdbF14EG6eLezM1uvToauXoorNzrpCx1zhQXCDcqn XXrg== 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=qL0bV1aGtykkLpnJ1bs1xX5jRe2aMjB3ciagL5oo9Us=; b=PvHf4WtpCE5S0nW448TeFD+whRLm05y05C6vJyZGw05jIJbdoshOTMvN8KRlgu8ACB T/TpcarEFlQwTWbl6vsx0ALaLoAPUs7DZqm5uQQQtV7v6BfkqjWQmB74wQ8VPELZgDqY oGSSwbbOIq+SYddmiboT0udCaQX/CZc4nZFmbkF/jeiqjimDV7saRoMUCsXBwEGbkQKx C/4PbU31t8z4cg052GPV78/LNzLES1PV++0yC8q4cm4V09xCnjTfqOY7YHPLZq6onP0S E8M9+YG0wBadWe6uWGf67iC6qigMcQAFdWhE978UJ31nJGRcrX0goVxxA6TNdYySLLde rJ9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EtCKsyQ8; 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 t25-20020a1709060c5900b0072b1964e54esi5027693ejf.1007.2022.09.22.12.21.01; Thu, 22 Sep 2022 12:21:28 -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=EtCKsyQ8; 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 S231748AbiIVSV0 (ORCPT + 99 others); Thu, 22 Sep 2022 14:21:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230196AbiIVSVW (ORCPT ); Thu, 22 Sep 2022 14:21:22 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B704F10AB0E; Thu, 22 Sep 2022 11:21:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663870881; x=1695406881; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Pt6BjpZXYL0Zm6+tvuXv3a61ZSXvTaNg3/pz+MVj3o4=; b=EtCKsyQ883XUlEbV8W1ExAD7O5iUboJHWKD0RW3s/1wmz/+Hy3q84teq sjHQoQKqYnL5OXHdnuwY4+28HrLqkNCuOWO/sFK35wXLFTJ8PTjsIIFn2 0yylW0lyhMkIgzQcqFuAhg8NaonA4x9lyqJsxcuR7xN6kah3P9/Q9q2p2 iO7hzuRHUCwNGY2raMk0iiOh/sbWtzNeFSz+NBpO57STN0MwoB5p6NnXS fgxe0hYED8AzqFtMIgZZp65ZJSG9/iFmVgpXk+Fd9/CJgEcYoplhdRdDY ksDcoP7b+Xs8lL3beAfHYJS4TYP5DLdksHr0iIfjQvjsESbt2lDYXYcFh A==; X-IronPort-AV: E=McAfee;i="6500,9779,10478"; a="386675927" X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="386675927" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,337,1654585200"; d="scan'208";a="653086616" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2022 11:21:12 -0700 From: isaku.yamahata@intel.com To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Marc Zyngier , Will Deacon Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Kai Huang , Chao Gao , Atish Patra , Shaokun Zhang , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov , Yuan Yao Subject: [PATCH v5 01/30] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Thu, 22 Sep 2022 11:20:30 -0700 Message-Id: <38d3b15ede9583300e22380d1517b6ec9b591355.1663869838.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 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 43a6a7efc6ec..9e1b3af4a074 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; @@ -9229,7 +9234,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; @@ -11866,7 +11876,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