Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp982537pxb; Fri, 15 Apr 2022 17:21:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8ROrGV/d/jgnjpocJnU/7oWL4omFlVj/l+l1mvR0Xga+SJZw8T7opoc1LGQ19aScj7kJu X-Received: by 2002:a63:4a0d:0:b0:382:aad5:bbe8 with SMTP id x13-20020a634a0d000000b00382aad5bbe8mr1083902pga.535.1650068492189; Fri, 15 Apr 2022 17:21:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650068492; cv=none; d=google.com; s=arc-20160816; b=igfi6MD5B0ZA+SFr8FDB1dEeOPjM/y8w393c/ZqefDgFRei1cylLFKTYPBpGchTqWE S3FPPXLMudK0mvwTz7aESfUyyVsgWuAXJZOCLHbeaxgxXUIPucOjRrMdQ4ZYZ511qyfX fgZYH9U7A+jTND26Dz4XvXgXnXI5XGxt93fTEyn+yUnoA/zX+ToGORM/wPsSN6NxBXet sBj6BfkuyQHKWaminJu2PXkBqK2JbEEb5OTXXEqO+PUyf5ewkV4YZZX9nGVfX3b9K8S4 exMHA5Y4byaoCiPo9fjiBHZhhGRQPhCNbr36IjebaHDDSJQYr5PbtDDl6+nqUue/rQFT liYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:sender:dkim-signature; bh=EzLDOe3HtxFMzXcfhfNSXtlOQrT0bfGxcqzSe9ECixQ=; b=XZS9tgnUs+wMj328rvtaQddqiHJkQXKcGpBULcwjix13sPXkorAqeAGPMjmOH7P5I7 YYRc9DkEBf/9brwn9y/vTWnqDxQ0IRfUPWfcCE3h4bgaVD95vvHNFHznK7m2qAv7p2r7 W8sakhPQ7BWpN4/yEXI0NfGboQ/FsXSrbVYV2IPPE7rl9x5Qu0QCan09kolhHvbFGc+g fGQGe2ChLT/SazMH0SToyex9ym5QCUgkgf202cO7fEq76Thg5zawCelJA+nfCJe99Yub 3kAJLvAOpN2o2YLivP+TdLllJwUjS+/dnikR6sDX1p09wNr2FjxvFEVlAXRxEUzSoJ2X 5efA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GAcjlr6z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s15-20020a63214f000000b0039d3228a8c6si2947951pgm.73.2022.04.15.17.21.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 17:21:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GAcjlr6z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 13001DB2DC; Fri, 15 Apr 2022 17:20:21 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354255AbiDOOIj (ORCPT + 99 others); Fri, 15 Apr 2022 10:08:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239553AbiDOOIh (ORCPT ); Fri, 15 Apr 2022 10:08:37 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C29A8167CE; Fri, 15 Apr 2022 07:06:08 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id l3-20020a05600c1d0300b0038ff89c938bso1562399wms.0; Fri, 15 Apr 2022 07:06:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=EzLDOe3HtxFMzXcfhfNSXtlOQrT0bfGxcqzSe9ECixQ=; b=GAcjlr6zA0G75G4NNXlH4fSIT3Idsieq5lqpMkUkAv0bKbY53A8uH846+fTRx48xkj 9Go+wfLzEzwLV3NEZC85tAdaIpW6ioel9Ow9oKIDZjNVXFlndNPIgpZOxJ2Qr5fAeK5+ PF/N6Q0lYVDkutISYrECTIUqMXHdh/Ti2OZfg+PDPImb6xPGBnl6QFwrdsxNBlfz5gAb DTbhJriEEpIRAnWqxoQaPyLuFeOTe+NIYWwUCymXuN0qNF00yNcOGm83s8X8ZW23gLIe z3qiEu61HELI9hP2Pz+M9JZSldqXNuwtc023a3RpjHT27I5ITWSrBsW8IDMGgKYzEhSy Xw6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:message-id:date:mime-version:user-agent :subject:content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=EzLDOe3HtxFMzXcfhfNSXtlOQrT0bfGxcqzSe9ECixQ=; b=ZQx0SPxfMtsewA7rHP+4hnEoyUkeLzR8hTd6yQJ1S4XFHc2XCtvdzhcvfpLHTDpSML l1drjCXgVEcFZGV6WnkCCPEjHdOTPoBh+EddDyDdlY+J5W78FhhYQqpr5FtgyXxFdN8f E1kzySMAhTKCCQWrNV439pmwJSIjn6nAepgbbzbRUfXQUu3Leme9STHU7e+zFwz0qBgf 0JvJHH5QRC2mkCaapfu/cKYFEhYbir1pD8ayzZ6+CdKWGA8GKDwWR6y1VKUvyWpvkUee zQqqHUUJIDvU+wdwkB9tL2xJT50K244mX+3g41GPkgav0bfRtLuMt3CDHpzK3xiQ67v9 3ATw== X-Gm-Message-State: AOAM532tRGC4tedToDFppAWLwxbCrKRy0wkaMspZg6IpLMcYFa//Ygom ySkJJquGFTLcY0bEDUU7alM= X-Received: by 2002:a05:600c:3b28:b0:38e:bb86:d68d with SMTP id m40-20020a05600c3b2800b0038ebb86d68dmr3436638wms.135.1650031567338; Fri, 15 Apr 2022 07:06:07 -0700 (PDT) Received: from ?IPV6:2001:b07:add:ec09:c399:bc87:7b6c:fb2a? ([2001:b07:add:ec09:c399:bc87:7b6c:fb2a]) by smtp.googlemail.com with ESMTPSA id y15-20020a05600015cf00b00203e324347bsm4772786wry.102.2022.04.15.07.06.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Apr 2022 07:06:06 -0700 (PDT) Sender: Paolo Bonzini Message-ID: Date: Fri, 15 Apr 2022 16:06:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [RFC PATCH v5 068/104] KVM: TDX: restore user ret MSRs Content-Language: en-US To: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@gmail.com, Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson References: <7c421339d5b2bd5d2e29f79ac1cdf5d269c5cf96.1646422845.git.isaku.yamahata@intel.com> From: Paolo Bonzini In-Reply-To: <7c421339d5b2bd5d2e29f79ac1cdf5d269c5cf96.1646422845.git.isaku.yamahata@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 On 3/4/22 20:49, isaku.yamahata@intel.com wrote: > From: Isaku Yamahata > > Several user ret MSRs are clobbered on TD exit. Restore those values on > TD exit and before returning to ring 3. > > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++++ > 1 file changed, 33 insertions(+) > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > index 54be5be1a06c..c1366aac7d96 100644 > --- a/arch/x86/kvm/vmx/tdx.c > +++ b/arch/x86/kvm/vmx/tdx.c > @@ -550,6 +550,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) > vcpu->kvm->vm_bugged = true; > } > > +struct tdx_uret_msr { > + u32 msr; > + unsigned int slot; > + u64 defval; > +}; > + > +static struct tdx_uret_msr tdx_uret_msrs[] = { > + {.msr = MSR_SYSCALL_MASK,}, > + {.msr = MSR_STAR,}, > + {.msr = MSR_LSTAR,}, > + {.msr = MSR_TSC_AUX,}, > +}; > + > +static void tdx_user_return_update_cache(void) > +{ > + int i; > + > + for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) > + kvm_user_return_update_cache(tdx_uret_msrs[i].slot, > + tdx_uret_msrs[i].defval); > +} > + > static void tdx_restore_host_xsave_state(struct kvm_vcpu *vcpu) > { > struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); > @@ -589,6 +611,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) > > tdx_vcpu_enter_exit(vcpu, tdx); > > + tdx_user_return_update_cache(); > tdx_restore_host_xsave_state(vcpu); > tdx->host_state_need_restore = true; > > @@ -1371,6 +1394,16 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) > if (WARN_ON_ONCE(x86_ops->tlb_remote_flush)) > return -EIO; > > + for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) { > + tdx_uret_msrs[i].slot = kvm_find_user_return_msr(tdx_uret_msrs[i].msr); > + if (tdx_uret_msrs[i].slot == -1) { > + /* If any MSR isn't supported, it is a KVM bug */ > + pr_err("MSR %x isn't included by kvm_find_user_return_msr\n", > + tdx_uret_msrs[i].msr); > + return -EIO; > + } > + } > + > max_pkgs = topology_max_packages(); > tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock), > GFP_KERNEL); I wonder if you only need to do this if !this_cpu_ptr(user_return_msrs)->registered, but not a big deal. Reviewed-by: Paolo Bonzini