Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1179980rbb; Mon, 26 Feb 2024 00:54:35 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX7WmYhURfHKgE1afatQRboq+rS2e2v0jIK+Z5t33YkceblR0aJpCEFSdS9MsM+6y5NWdiukBRexbE/Ft0zh52w32CxgWcgBN9ydT3GZw== X-Google-Smtp-Source: AGHT+IHQcdnfb8sTcuAp5OgvcduCSXdGyA8k6R6tQ25vcyy0ij5oKl8s9Bz2GO8h8Tfn7TA1mRyt X-Received: by 2002:a05:6402:1607:b0:565:7d4a:1d4c with SMTP id f7-20020a056402160700b005657d4a1d4cmr4022642edv.9.1708937675719; Mon, 26 Feb 2024 00:54:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937675; cv=pass; d=google.com; s=arc-20160816; b=U7G5oVCCjkeoRwzr/LCsKH+CjVM2COtxPmvQsUvM4AUYVj+qTZ7zLatj0R6tDRRWpj LBULULtC5Gx/W43RBnFhhZhVPvqFwKhv9BO4bTHfdohwivefdepUjdtgK5i+oM4xW27k nr3AL5WQOYvNc29CLcQamZ6nTOBYsssrczUXbI4rDDtQPdccZq7SHn6M+W39zubfqiLU l8L342ypXmJfFEb49zvJPFKfIU3cRcAJq9/YLqk+Mne/MPvWDPZvasZBdlvRX2qUAL54 eFn3g5TAaWapXoWzXNLgRwm7CB9/UctRTlpmAwzDse+R5gdZxhRY9ZWBLolHHpPPBy6G T7Cw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=H2FUA0RqFOJs4//cLSwWjzKDvVrXPitMjzSw0J06A4U=; fh=1gGDAeF21c6AITri72SbLgKrk/wFaPJiWExhgpdJOYA=; b=hZVLt4zwqUOA1ETyYYIT2qcrgBwfQ9juANup25zt2ymFYbpgkR/yQqaYqRTFaJ9Hgk 4CxNu2jpNC/oVagqLaAbrizB8xtYvVON0qDY3CBt1Xde7ghdJxf6LsCeaUKqwM0cy4Wq 7m6g5GjqLPQESiHTvPQE6uMKhES07ogcbn3j1ZPtX5NhTZt355gzO5rtKjO0Een2WZDH YmJGhVsxbnOZNXYzrBDlCCahb0wEdCwnmgIrRJG5S3ItHpqgjGRUAfizRKS3LH3z7hWX GhsQSEWLxQ2CMBe3gDXFY0jB8XW1vf6yDhUhRNqt+FubJ2GDRuu/xwlAG3T9bQqF43h4 4ljg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=highW9cf; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80844-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80844-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i22-20020a508716000000b005642ee620fasi1925319edb.114.2024.02.26.00.54.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:54:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80844-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=highW9cf; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80844-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80844-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 544E31F21F2C for ; Mon, 26 Feb 2024 08:54:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED0A177A14; Mon, 26 Feb 2024 08:28:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="highW9cf" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D25296F071; Mon, 26 Feb 2024 08:28:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936121; cv=none; b=u0VTbfOy/e0mmTT+hCdTv/H2X18+zXQSBYZI8NMpSRSkSFKlHQP0C6X4IdD19DlH1mQgBchOZDdbdv1/nWDe9sG/wkEv8yj2r6jSKLA/Vtl4HRCDCVDIYcgcAiyw0OSQ2gd1Fvtxe6AhVIFfnsn/Tzy6R4QnAQowZ1KxPjWVdQI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936121; c=relaxed/simple; bh=aeEZd97eChWMmsHRdCg4ghs1LM1YPE3cIvcpKx5Pzcc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BJTBzNNTW36bZqDMor7IhAM96b94KRSv0szgo4E8XWhjPZeRvIi7rMtWyT6gPkrKPx1ruPuruamxLfl+VqCHEw553SUkLYyu1ANVpL5t0zACGNaajOM4Jf/O2M7SrL96D8Ngvs0RTo6wUIGzIZM6aNkA2536puB/VwXcvmoFtV8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=highW9cf; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936120; x=1740472120; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aeEZd97eChWMmsHRdCg4ghs1LM1YPE3cIvcpKx5Pzcc=; b=highW9cfKl1Y64namPsCx6lYBITBSvEGMeuvayvO49Sovvpahr57/JZg nuaQBVfV04Ego92BR1z5uPGv5D9j43Zob44qfeQAxodrEU0IUll0pyBGr pSlFdt0SMgikhgc1oULtm1067Oh78sjiIv6mctcIBymHWQwpNWSNUiIPg ePbLdAE/7/YCuXUfyuaLtzf2L+hUBv9WzSNCLN7ynlRETjCtxbkDLFDNl Bfwg56dmwxINDxyi7sEdkQyMxX1ERDPN+nd7zcxxrFp6T+VM/VWkYBIOk 1srTmGDAefOFQq4A/XhfTJuB0YZsvUJarFB8ztifhnk1fDwxw6FtZM+8Y Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069511" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069511" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272548" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:38 -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 , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Chao Gao Subject: [PATCH v19 081/130] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr Date: Mon, 26 Feb 2024 00:26:23 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 36694e784c27..3ab85c3d86ee 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2259,6 +2259,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 b361d948140f..1b189e86a1f1 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