Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3405860iog; Mon, 27 Jun 2022 15:39:26 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sdxhPDEIxy5TvELc7WMzAxWXPhHGqIR6YUJI67bbLUKOiudYIH9tr7DDSqyJvAgk/MtLQP X-Received: by 2002:a17:906:11d:b0:712:abf:3210 with SMTP id 29-20020a170906011d00b007120abf3210mr14972665eje.292.1656369566192; Mon, 27 Jun 2022 15:39:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656369566; cv=none; d=google.com; s=arc-20160816; b=n2P4mj3vgK6PacVe2N/zhcA3WJUkHG6Vz8SADdF+PrGpySmQFH/V9J5wAeNZZ9MaXS SQoKtHBDRs5DerSfUo9ne0T+P2PXUocDfxDvpjoYd1YAS3PYyTlkwAYC/iWTS8xGCkE9 byOET01Rnv28M9ubsltMAI+q5ht6CqBTSvnm16mftYlqgZPL5NJlP90u5L6rzNAo2L+e R5sfJzpdK4LUKf7qlyi+t560JMouys0gmes1M7oBVishsDCE53nlyfg1fyyui8hlISmu 2D1R6nVreUKgPNpXIB6GBLl1jlu8aYJMOEqsRjfckEZsXdYiKk1DMHdpwZmvUV9kAxj/ RaLA== 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=aBSsWADxtXRFe64jSaeTcUFKMxBjfmtpqkpY5yZEA00=; b=wXgpglxcAGAWSBL+vS0jUzbu5J/wbksCcj2zVMc9hhnGYTi7/fECP1Ax+ifIB7fvFG Og1B1c793o28JRlLSYmNlJC+t8ou8FDHgK3kPvJQAKyrtqaRVnKct5mBp+beWt0phEm6 4olVQtetuH3jEA/Nay2Q/l24Nwh0N/URlcP5wUyqt4ASmR1aO/rGv+VeUb0z2b8k9Ll7 LHBUt8IAOrHseNZk0XDBUOgdkEZPLwdApB4Nhz2CU6aN5UJMwge8L1scDxMDOHQpZWCR xBq31OIP/osKymp1xZvwjzVhdl+2apN7ZDp8JXrp/5qsrQFbFOeD83VIHSrGkbC5eAo7 oz9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=E9ohfRQw; 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 dd14-20020a1709069b8e00b0070eb188249bsi1219573ejc.358.2022.06.27.15.39.01; Mon, 27 Jun 2022 15:39:26 -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=E9ohfRQw; 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 S242274AbiF0WAV (ORCPT + 99 others); Mon, 27 Jun 2022 18:00:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241507AbiF0Vz3 (ORCPT ); Mon, 27 Jun 2022 17:55:29 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED3697658; Mon, 27 Jun 2022 14:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656366900; x=1687902900; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cDBeRCZadrH83G5MHzSnP4cdrRwgDDMNstiBJT15pKg=; b=E9ohfRQwwnKQmB+o3PTp+aJJVQ5xfvFuR5aPCMpSNw8RZVv1ZktE+p36 5zp7IJvCyOfkm7WVIb7gwUQPrFa9J5sbg5eoYUR9E/R7fE428lFN2T4K/ ReCEtYZVUPN1QaNGj3sY+SeU4IqIomJpi8BJe24Ng49zCV+0b9tqY/Qt+ 1WEVe9/XLDfAkTZ53JJEo3GUixwDXrrUo2lXeYRkUjh1H6f0u587Hqpkj oUciJqcjweb5BjUamzbIX3U1BewyYBkPA8o6u9HFOxphesHsIT+QzKOow QFsmHiaWYEm3//I0el+m3SYF6ZInKcFrnsKZAViwQBJh1+YlKla/M+QI5 Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10391"; a="279116107" X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="279116107" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:57 -0700 X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="657863653" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:57 -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 , Chao Gao Subject: [PATCH v7 067/102] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr Date: Mon, 27 Jun 2022 14:53:59 -0700 Message-Id: <8b4cf41619f349695b855e9d57ea2e04ab17c5ef.1656366338.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=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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: Chao Gao Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses kvm_set_user_return_msr() to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time. TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in kvm_user_return_msrs are inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead kvm_on_user_return() to skip restoring some MSRs to the host's values. kvm_set_user_return_msr() can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation of kvm_set_user_return_msr() to update cached values and skip that wrmsr. Signed-off-by: Chao Gao Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f2a4d5a18851..a1d186190287 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2025,6 +2025,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, int kvm_add_user_return_msr(u32 msr); int kvm_find_user_return_msr(u32 msr); int kvm_set_user_return_msr(unsigned index, u64 val, u64 mask); +void kvm_user_return_update_cache(unsigned int index, u64 val); static inline bool kvm_is_supported_user_return_msr(u32 msr) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 70312e195f36..ce0ef32c2619 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -425,6 +425,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier(struct kvm_user_return_msrs *msrs) +{ + if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; + user_return_notifier_register(&msrs->urn); + msrs->registered = true; + } +} + int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) { unsigned int cpu = smp_processor_id(); @@ -439,15 +448,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) return 1; msrs->values[slot].curr = value; - if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; - user_return_notifier_register(&msrs->urn); - msrs->registered = true; - } + kvm_user_return_register_notifier(msrs); return 0; } EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache(unsigned int slot, u64 value) +{ + struct kvm_user_return_msrs *msrs = this_cpu_ptr(user_return_msrs); + + msrs->values[slot].curr = value; + kvm_user_return_register_notifier(msrs); +} +EXPORT_SYMBOL_GPL(kvm_user_return_update_cache); + static void drop_user_return_notifiers(void) { unsigned int cpu = smp_processor_id(); -- 2.25.1