Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2686441rwd; Sun, 28 May 2023 22:12:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4HHVT4BcGUxjRSUbyCE5NMSHbPlY5igWGOOix4UE3ab1QZWZWpdV4sRNMca3Ck25hz9L7q X-Received: by 2002:a17:90b:a54:b0:256:6c75:b512 with SMTP id gw20-20020a17090b0a5400b002566c75b512mr3537744pjb.46.1685337141247; Sun, 28 May 2023 22:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685337141; cv=none; d=google.com; s=arc-20160816; b=peh5ydqfbKdsO6S/a9bnz+Z0nL4OvtWYHcJvQRZrY8tVyv7hfZeLHgtQ/OonyEyeiB 1SvrMLl8qqQawhUaPJOJGgGjumalUgfpqP8MXb+67+wKehGt8slmp3lGnyaDfBCHIVkN Jfi2kJIavVKYlsNgEvfSgsaeTZu4Qtx79cAm5RwbMxp/qfNrhZnEQP/+BmE2Ot163h2I x0m9tWObeGWS7P3kHVF4RbYNmC2SVj02UxRKG1OB6VQv6Qqd8QuqNMt+3CSxeMq3PR4N AOdXelwBAHYC8n2DAB9mqE1/TBJEr44Aycs5s+HsvlhndCWuc8128wfTVeKBQdI1Fyya 8liw== 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=3VDa2457MyWuoS400Bj+imwayYQMiCccPeHHsCtEZAA=; b=OdhCPTuSCxhXyL0KWD1oFCkZKOjatHqfNbgbKsg26xjG/ipMXASgQcs51x/njcwTU0 RKaXfPtpl0iYuZB7xWcFjiBwnl/cq1Q053GXLqDBiuBEV1SHILEnxBAaHgDjnZcteciU gKDrxksfaF+WI358UZ8A7Lyk1ubdG5SOL5WBpQDQQR2NgTct9XfdThwXBOvWk1ys7vNw LUrzIdpUJEiZ316PGDlo1WCrkb/TSZta13kMeyEVCy4WGFXcRFcigV55q3CTh1lX5qBB mC0CtbU+R8aoALYe/iRmrbV/i6CJZ+Wi5Y7kpkWg4376mU4L5faCF7DQBEjg8RZzSP9D r3sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=C0Fn3e3j; 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 f91-20020a17090a28e400b00256917c7851si378764pjd.107.2023.05.28.22.12.09; Sun, 28 May 2023 22:12:21 -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=C0Fn3e3j; 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 S232048AbjE2E2a (ORCPT + 99 others); Mon, 29 May 2023 00:28:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231992AbjE2E1d (ORCPT ); Mon, 29 May 2023 00:27:33 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62C12114; Sun, 28 May 2023 21:24:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334284; x=1716870284; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8vcelMYkC8rAt6CzIHpOV/l2WpfyMaXFq9o2mQoDD4A=; b=C0Fn3e3jA5cYnyxgNYheMnCG2XX65EUEqNTaDz8IR485RDBAgMnMyWs4 Ktl1M+zrOJ8/+WqSD+va5a1bfeLB8o+b3DAcbT9UesEcPmhzcBjltevlT PDyMHgZTX5hUZ8j8q+5Zu/p6kZFYZXSb7kvTzRPXgibay7f2INs3T3wYO agOXKmTE+VX5n71VczVC4OuaHnPDEX7Pyc5w3jqMR3B7njJPkemcGx9Bv LhnEAky/+9EkZBdZbQ7KtS0P9lOD1vByPTQ/zQIdmZrN+Boay8/mRRLZo chfgh/EnBThb02pJIu6KW27H5wN+wE6n1jgZOuadi+l7MhlQC5H0qA8tV A==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="334966136" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="334966136" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="775784418" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="775784418" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:25 -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, Chao Gao Subject: [PATCH v14 066/113] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr Date: Sun, 28 May 2023 21:19:48 -0700 Message-Id: <69794e1813b2517e40db9a8e62263e3618df76b7.1685333728.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.6 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 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 021b971c9aba..0fb7c6224c8b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2192,6 +2192,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 f8b3dda26753..efb523c43800 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