Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp281570rwb; Tue, 25 Jul 2023 16:02:17 -0700 (PDT) X-Google-Smtp-Source: APBJJlHFasHLkfgKhM677e6RLDR9kEfS2kuiO67jd1vPACGSmkGsUwOrzl8sxQBSA7y8arTcZeDi X-Received: by 2002:a17:906:19b:b0:993:6382:6e34 with SMTP id 27-20020a170906019b00b0099363826e34mr180135ejb.72.1690326137561; Tue, 25 Jul 2023 16:02:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690326137; cv=none; d=google.com; s=arc-20160816; b=q1hDVRkfFd94v4zeaZzrIaqdPbf+zsYGZRwwIQkByi4TotuF6vNaqSQSgyt7MPkVOx a4nTQaCglEujVWWSAr+8dGfpMzown1qIlLRzq2sd9FqnKd85yMTKqhZ2uEacZZ+dhhQP Fbdf0z2pT4D53DhwLxzRxiYMuJTiCzcgCXEbdiZlV7hqfGhp96lD3ff8spfFzenV+YXk 4+CDt4W2CQNbTPUNtPvfOsjC7gWPjHzIV0NS2dyYsKO97gqW6NA1RGKmW6ft3qjiOyfJ NoLkMKvLaMnxENNftmKbiZIc71McS79GsUGDBcaCWwAD0vIRk2ZVhL/BH3YX/S/HmGaT ZUJw== 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=rPJPgiubKMXndBLASaIw4zjWU5Ht0rJvoS0offtJuY4=; fh=UJL44GK56ky0KzxbJVmu+rqKQ/SR3Yf5PGdMxNbAuG4=; b=zUXckDEMEULF78Dw6hvEmI/dYSdClq77AfXICJucnObxYEVFQ70Iq6x8Mx0L9Q2xy7 3/JoT9Zu5elr7sd7T/Cay5/KdKNrMIPUzAFvozrfWhzTQH1qLtxwg4FYLmQXPxdq8DJK IJtrKyPPtNUyCEl8XQUqvZ5xSaFs97jLqaCL3rwEqCUMvX48ko4DpY/bB8yB/khCyAtJ 0qtUUFJaxbFhEa6lt5uTlTD4GMqdqUdsEOuESp5HaBl7hrlfD9n3NXpJdWUR8p8fG9K2 8LX2U+tdcpNpwTPWAo+9ogfdDJz+iXYG14Wbgmcnx7c9LH/is454LpZuYhlApSihlkm5 6QLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VTnMnoUI; 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 c14-20020a170906340e00b00992bf74c314si7906680ejb.983.2023.07.25.16.01.51; Tue, 25 Jul 2023 16:02:17 -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=VTnMnoUI; 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 S232388AbjGYWUQ (ORCPT + 99 others); Tue, 25 Jul 2023 18:20:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232373AbjGYWTX (ORCPT ); Tue, 25 Jul 2023 18:19:23 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4141F448B; Tue, 25 Jul 2023 15:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323393; x=1721859393; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oh5NZENZHav3qekkLXbYrNrfmD/wAIeiCaDNA2VvmoU=; b=VTnMnoUIN++xNLqsVf6yCBgUFRYjG1/G5ceo+t91TUh+uPurjVo8a71q h6PG/0TH6FoEbQpS//aOoob/Irw1/ebLYyKycOhjrFWJQUqgQPYpZLpVP XOpr9oRwNyNEbRYhxs4OqWoAjepkwcNKXcIoR9rReUcsA4p+i29VpkRK1 c1YQextpRHnyDclSVgrqNmwFkeY99gWJrojtgjWbJq05DRkyAfsMOsJXk EufLGTq8l5qXEmiNwXp5n+1CcmUGbINm5t25/zON25Dpgnu/gCr4gQCiA IMFEgz1sy6nf624qoc9kU2ZUzUGJeFn6whxBEpO4Ew1J2tFr6yIeAnTOA A==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882552" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882552" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001799" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001799" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:52 -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 , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Chao Gao Subject: [PATCH v15 066/115] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr Date: Tue, 25 Jul 2023 15:14:17 -0700 Message-Id: <6be95f9c821a7d8475926bdc3163e0684181ac11.1690322424.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,URIBL_BLOCKED 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 9705e9f30068..95c2ed8fdcd6 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2200,6 +2200,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 bc8e6531b3f3..7805987d891d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -436,6 +436,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(); @@ -450,15 +459,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