Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1433581rwe; Thu, 1 Sep 2022 19:31:07 -0700 (PDT) X-Google-Smtp-Source: AA6agR6g8GfYHWcfVgOVyjMoqVL22xx4Jx63Rp49k7raXlsqQ/cZpE4VX0s1hTISMwzSPHSAybAf X-Received: by 2002:a05:6402:1286:b0:447:e8ca:207b with SMTP id w6-20020a056402128600b00447e8ca207bmr27355158edv.95.1662085867271; Thu, 01 Sep 2022 19:31:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662085867; cv=none; d=google.com; s=arc-20160816; b=cs/dTXawy8el77SO/b7sykzzhFvUujQpp21kzuDcbBRcqbtz6tG7wqKPfzxJjLyUtY M4cpXR8HjJTPOIw4gkaWyi+qg0YaNqyLfUdFjKYf6VYhb+nQSz2bkVWnCmVgdeq0p9d4 ekFFyVoGAfQZTuL9SO+anaehrDqtHaIFCGmWiMtKuk6nkiw63bbATrE7pA8vLlLMTlxG qyU1/wMyHp0E4nlYhapMtWgvxl01ezKECBo2t63gfZF5SXSqQe4kaBgw+C5QL9xqEfU4 MOVr/TqJFYvsvHDtFFR0rsrukXTLWyZm5bfuNBZVJLKSIP3r7YDxVqZjvWNkDmSxMvrm Yj9g== 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=CY9cwusFB9cafDi1fqGtkCivNGHS6xc/4RNJHDNBB3E=; b=pag1U3DCLFdfvWOcRtfrxgU4i4u0Zy2JIsKStwVZiNlYn/ZpJ9boH535cWVakgXXBK eMUff79KmbmDUneL31GnohFsozHI4ttwjgZ5L/dG+syskci1k2uxkvlzSju9hR6OU1IK ezYuufqYGSaItmpbdN8QX5vbb5o+pOrgvzEuOA+TmS3unGenqI3Zc0qb73PUSWtCpByQ 01LMYfshjIp1EX+5CnmSLV4nOp61QV+is9/se9dJpvTxRbebMUI0M8j8nJBv9wsndZek vdx1sjzGws/Cz1z3OQ7QEDGjpBfYwxIKL+pgV5NjJsX5ySThQuJcW2XsAYwWL2lg9Wnd ddhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i2NQuDNX; 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 y11-20020a056402358b00b0043df9ca3ff8si790210edc.50.2022.09.01.19.30.41; Thu, 01 Sep 2022 19:31:07 -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=i2NQuDNX; 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 S234832AbiIBCSQ (ORCPT + 99 others); Thu, 1 Sep 2022 22:18:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233716AbiIBCSL (ORCPT ); Thu, 1 Sep 2022 22:18:11 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9824E6CF59; Thu, 1 Sep 2022 19:18:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662085090; x=1693621090; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OO1o5r/T4RF67a3AbNWLuZYBR8DPzBnzS2JrwtFCDRk=; b=i2NQuDNXZUAazBeWJHDY2t6YaEq0QC+TJYV3Dk5z7oethZnUFgxWxoG/ ddxs+CMDjgYPCDD2LpkeaOUwqWGSrsvvvKxvCnJWNHVy3LHwvgJpM1xf3 GzlsbI7NpZ0Th5QfOs2ixzRe7y1/cjA/FkA7kDdOYVOAQ7Lp4jdg51juy 63FqPOAkgeslydAh1S0kH5c+Gy4EiaJifQQdIvN3mdJoUG2lWw0blHnb1 wIGtPLsSoBC+nLTqLyxve1tQKXrCYFgV6JZIz9gqLQK5vGkzCs3ynFKsm xIm2vhq9pjjbGy3a07Sd8MV1QkWFzR35LKylMn7ot5ZsiwNQ9w4FDCOAN w==; X-IronPort-AV: E=McAfee;i="6500,9779,10457"; a="297157827" X-IronPort-AV: E=Sophos;i="5.93,281,1654585200"; d="scan'208";a="297157827" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 19:18:10 -0700 X-IronPort-AV: E=Sophos;i="5.93,281,1654585200"; d="scan'208";a="608835585" 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; 01 Sep 2022 19:18:09 -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 , Qi Liu , John Garry , Daniel Lezcano , Huang Ying , Huacai Chen , Dave Hansen , Borislav Petkov Subject: [PATCH v3 01/22] KVM: x86: Drop kvm_user_return_msr_cpu_online() Date: Thu, 1 Sep 2022 19:17:36 -0700 Message-Id: <25ff8d1bbf41de4bcf93a184826bd57e140a465b.1662084396.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 --- 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..0e200fe44b35 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.values[].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