Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2260590pxm; Fri, 4 Mar 2022 12:33:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4p53C3glOOOO7J80ed0SI9BpAYhP+qQxLeEMxP6oWR53XTE0mPa/n9MByGxtzn/WrV3HG X-Received: by 2002:a63:1c14:0:b0:36b:28ef:f8ce with SMTP id c20-20020a631c14000000b0036b28eff8cemr169148pgc.96.1646425987764; Fri, 04 Mar 2022 12:33:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646425987; cv=none; d=google.com; s=arc-20160816; b=nFIFEhzsCM9aYz6s1fIuTvlgR3NXBPRjIv7qOgdmJZHO8n6YW25VJknLW3CDgEDslG 0eKd+Tmoqr7cimUO+uloP+PyQqGwAwf2LeX7eVKlfKRV+vJ8dAsZZuYQL7qwTr7TMdCB umXznpPieiMq58mPS0G/y7aVo1Me0r9vdeIrYpCaembY4BhyNmLoJ8NAmfS8cverknod lUBY9tOi80tb0VPOdkwAmghlOfUjVZ/alV88b91mA6B7HrO7Fpu6I2ycoa14baNnMz8p xMFoKvpnGXrQKS2hK60mRRAsBKTg9KX5wZG5DGibaGU8PrQp8AzKtxmeG8GcesE3m4Ug SsTg== 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=sZ/YlC+PPmMCNIqeO/eSEM5T0BksiL4REsLD8YR4Q9k=; b=wfS5NYS82YC9JVxjmzrs/ENqk/OZ2q6tvQVL4R3ISbH3l1zn1n5N1hf+E3Kua1/Oav o9k59yQk0gcdn02cx18VKZ7cz3PmybhyBK+b5VX1buisNluaOfN7XitE+nRE965Hxshy 3a2cU5daebmvYvfe4NtHmXqoLOllcXKF4fl4KZda+eYCgqxHqqgQOKT5IJJFAF4VOlOt mrTtFhx+r7HAMTXCA76eA2+3q9L73wu1ApOkcFqplqobLqBDE4yUKzBg1wYVwwvayqoy fufRMN/Nx0YsegTi9VHnMjOFn1Ka20efxBNImUIMW1jjfx6k4FgxRluKX11tJWTUvdhr 8sVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Cg2oFoFL; 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 c18-20020a170902d49200b00151cea9662bsi82408plg.46.2022.03.04.12.32.52; Fri, 04 Mar 2022 12:33:07 -0800 (PST) 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=Cg2oFoFL; 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 S230227AbiCDUcg (ORCPT + 99 others); Fri, 4 Mar 2022 15:32:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229989AbiCDUcV (ORCPT ); Fri, 4 Mar 2022 15:32:21 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51AC71E6EA8; Fri, 4 Mar 2022 12:31:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646425891; x=1677961891; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TPX0noRaY57suDn5e3z0RNT0TbeJUW+ydejfu9Lr3SQ=; b=Cg2oFoFLF+rS5yG+UaBVZAp8CZ3SDht8szptsWCvOL0O8L1q0DNwwi46 GYGdH8OvvYFcwZ8Zac7xJqx+nSqetIXjsWVLCNPJFc1nCe9Kt9tkIO/Ln Bwq+zKPPd4949eeCPv7S/0LGHLCFN6OKAJ8zXzs/omF6Cn+Oy90T5+6/7 etLWmYl+ZqsjH4jfUdFLbx48mUPoPKhTkEdIAUjlaTl4M18iz/jslrNJE +6cTipO8tNo07mwsgGKPkH7GM4miyQTdJ7MbJyLex1kv0nNyRjCpuWa7G 9Oq8kCY6SzEPbUPI1I6ZI2b7ZeXxzV3QLCj6OTmqLmd3zuym+d3zUBVbH g==; X-IronPort-AV: E=McAfee;i="6200,9189,10276"; a="251624264" X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="251624264" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:33 -0800 X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="552344462" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:33 -0800 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 , Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson Subject: [RFC PATCH v5 067/104] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr Date: Fri, 4 Mar 2022 11:49:23 -0800 Message-Id: <07b46b34aa86658fe8409926c3b8474dd6ff0d8c.1646422845.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, RCVD_IN_MSPIKE_H2,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 --- 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 8406f8b5ab74..b6396d11139e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1894,6 +1894,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 66400810d54f..45e8a02e99bf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -427,6 +427,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(); @@ -441,15 +450,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