Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2612000rdg; Mon, 16 Oct 2023 09:21:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+WFXZLhpYKjZKM8WpBcHhbhPUX/fiSVgwxnZiCDZqzikzArsn+ZGbFI1IoiisBSOJaMS5 X-Received: by 2002:a17:90a:f3cc:b0:27d:3296:b3e with SMTP id ha12-20020a17090af3cc00b0027d32960b3emr7579374pjb.41.1697473290586; Mon, 16 Oct 2023 09:21:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697473290; cv=none; d=google.com; s=arc-20160816; b=t2hRgyzuiN+gaSHuEZ6FKPnITlLHANecubN75omPS6/QbSUnNVrIxtR/B+SL4K83yV xbAf1h5gElCQayZqYVkpo3F4+c1BiWAaGAsStczr5tAO+xOTVKjpo1a0Gzhv4sxwxRQG z6Nz/RZO75ZGF/DhEOf67U07Gt+l7vwN1oAiqZ4dLiBDiG8DNwHjF/kxZgbbzl9DosMK uV/7huwmTsf+TLpbgJ1z4I9nnb96o50x9MDwv8VRtBoBn6TdfXQc2ekFBH4+juvqqJQw uHBDm1dQ670E4YCVuc3zAwYJ80MbTEHurlAMsmrHJ6C6nUK1AoupCDa/9hfbiSn1VGTG zBqA== 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=4rqPnbhQJF4gJGfATrJa3F05Ok4j1uK/QBoZhqPMnSo=; fh=WyjNXnowVZ7mVWXVPMK2tIOSbq1wyQigBp90E6vPJMw=; b=VV1i3VOx6K1TGc+ZBwOIb2KNr0MJVHOedxx1rUbLQDP/Sg7RatKnWJzFLBBsy8WXs1 +nN4q09REuZKT3AE4Tm1Rb3ADsvjnjcxiTidefRAoNZhrbDsliCokUrZv4BLo5CZX+SW /9yYi+dDOPywA86u56qeYFlyjKmRcBNy55M0PVttIp8YPrkEId7qiXFjJMu1V5NqcGDU UwhOrGNywTdQ7RBfNqHY2v0x3EokkWhF9Run5RgBo9FO5SUGVqeg3J98KxwIDXB2RVHM nhMJNhGZkXp+2t4J0k38YcU4U2qLuXlXv/726c7GmaaTcDdZ2kQgh+UZjf9uXqw3TNjR oTyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HYZccitx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id q6-20020a17090a9f4600b0026efcf5357csi6321088pjv.126.2023.10.16.09.21.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:21:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HYZccitx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 12FB280784CA; Mon, 16 Oct 2023 09:21:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232539AbjJPQVH (ORCPT + 99 others); Mon, 16 Oct 2023 12:21:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234007AbjJPQUs (ORCPT ); Mon, 16 Oct 2023 12:20:48 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06330D66; Mon, 16 Oct 2023 09:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473075; x=1729009075; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a/joewj42e4sn3t+Pkz5R0bhwRZlioGqjDK6ho8+FW8=; b=HYZccitxgJQ+b9cD7DuUNl3xq/exqzcCiyETuwAb2lIv2Qf56S51kn4L Dl1Pmwhg9kSHU05SPjsr0c3KozcSQV7oNDkz8dZne4Cy40bDS7W1PftkX 4cfa60dBiP/YjD7pzUnkPpu/fQo2usXzPB+T17nFKM2lVN9A9QK2b9dLo pcsvcaoBgnJHh+Ic7WwPm0Q7fBx/cXHdEkgsMhSDnY/yk504ViPJf2zIA ikJhcyd45XVmEhCOZ63x1wYd9JcfasASK1VYzVtAponxRBZna37kEurtK AVA+Sc4Gux00wQsh1CeilkP8qQ6FY6uD4Zz++51ElaScBscjcdNmS9QEk Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364921871" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364921871" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448192" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448192" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:51 -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 v16 065/116] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr Date: Mon, 16 Oct 2023 09:14:17 -0700 Message-Id: <2fdcebb75bf64b33f5b8ea752cfa0eabbdd080b1.1697471314.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=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:21:28 -0700 (PDT) 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 8e6bcafd947b..777981e97f1a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2217,6 +2217,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 f524a7d3fda8..916c462f7d8b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -440,6 +440,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(); @@ -454,15 +463,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