Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp832099rwb; Mon, 26 Sep 2022 06:28:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4MlVjxfKl8cge2q46CybAP1lBWtC5pVXF/G7Wo6OwrOSAnWfn8KnQAoc+G/qE8iGhX24gR X-Received: by 2002:a17:907:c25:b0:782:9dc8:eef1 with SMTP id ga37-20020a1709070c2500b007829dc8eef1mr14540026ejc.667.1664198910451; Mon, 26 Sep 2022 06:28:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664198910; cv=none; d=google.com; s=arc-20160816; b=Zojyuys9UWSIRgeeFr3PEG+YytsWqO5Za0fwLcg1VgejeCrKzYcMqgIBKWH3fCQL/m gs1slR48Tb/nLTPZa28Fs005ZQFInWk2xONHQ5mJ1+zoUQh8ZD595cWSlTQQO39qXHE8 3/2QCkuHGDue3x7LbdWGasUYmCbrsFG10L2axWOpZlduqSlp4OVqZ3I8avPm4hxgbTl0 WxronbAk4gMXNtHCALqffcZ7NmvwcMZ3RRcF/k20Vt5qoXciDNL8sY5jYiu8le8/S4jl 2OUfCujTv771UOQW/DOCflipN4u+gkX0wYWLEY2gpF5ofsmnVmD8zyMBpWLfz77yefmQ lsKw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AjiQuNRypYeLb5E8wPGnaw1H24GYwzkK5r1+G+YkD2k=; b=TG98QLFoWRnAH7S7cxti9uR7x4jXihTkz4Nf9HFQH5aFWaT0vrhBrjJe9MAh1PT7Ve jsKy1LVdMlQTEGnL0wn7G7QK9arZYLIf5ASSYi7cfROHeGRyqBrbK6QhJ1CbUYFnFNQN ueAV5iSPyASAwcdnZTenSiikmqcgB3XMVr0fqvh+a8qPzOBPVL7thNEB2LLzBy18lYhM ycC1tZX7D9BvgJOPLEgUDYU5R9Akj44l4ew4TIILCV/9Kpjt7uPy/y8WI9xPNRBB4YcO 13abdj1YdD+HDhL5BrwZ4nJr0MsvMMRUbmmoQQKggKtytM25bOldrR4seJiNnSd+P5+K TMyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zECb0Hu1; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f17-20020a0564021e9100b004511d552be3si19097813edf.4.2022.09.26.06.28.04; Mon, 26 Sep 2022 06:28:30 -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=@linuxfoundation.org header.s=korg header.b=zECb0Hu1; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238772AbiIZLrV (ORCPT + 99 others); Mon, 26 Sep 2022 07:47:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238673AbiIZLoT (ORCPT ); Mon, 26 Sep 2022 07:44:19 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8354372FF4; Mon, 26 Sep 2022 03:46:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id F3B5D6091B; Mon, 26 Sep 2022 10:46:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3DAEC433D6; Mon, 26 Sep 2022 10:46:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664189198; bh=ug9Ge9ICIjyQ5FTTGPTaKvIClq9/OIhcNwJxA9pxu+c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zECb0Hu1ItCpbcpso7LWpSB56Rz5OV5cpYqcDuBTg6+QfEuvwfmr6cOs6SNBQ0qnY W7y0HKQdsnddMWmamtna9+/DWoeUGwy3aO9wjqJ7vm7jb1tl9VEkO1TPaIDBA9lzjs dXse07AU4eHrimd1UftNRH60gvQRysQcnNfolBj0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Leonardo Bras , Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.19 064/207] KVM: x86: Reinstate kvm_vcpu_arch.guest_supported_xcr0 Date: Mon, 26 Sep 2022 12:10:53 +0200 Message-Id: <20220926100809.463971916@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100806.522017616@linuxfoundation.org> References: <20220926100806.522017616@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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: Sean Christopherson commit ee519b3a2ae3027c341bce829ee8c51f4f494f5b upstream. Reinstate the per-vCPU guest_supported_xcr0 by partially reverting commit 988896bb6182; the implicit assessment that guest_supported_xcr0 is always the same as guest_fpu.fpstate->user_xfeatures was incorrect. kvm_vcpu_after_set_cpuid() isn't the only place that sets user_xfeatures, as user_xfeatures is set to fpu_user_cfg.default_features when guest_fpu is allocated via fpu_alloc_guest_fpstate() => __fpstate_reset(). guest_supported_xcr0 on the other hand is zero-allocated. If userspace never invokes KVM_SET_CPUID2, supported XCR0 will be '0', whereas the allowed user XFEATURES will be non-zero. Practically speaking, the edge case likely doesn't matter as no sane userspace will live migrate a VM without ever doing KVM_SET_CPUID2. The primary motivation is to prepare for KVM intentionally and explicitly setting bits in user_xfeatures that are not set in guest_supported_xcr0. Because KVM_{G,S}ET_XSAVE can be used to svae/restore FP+SSE state even if the host doesn't support XSAVE, KVM needs to set the FP+SSE bits in user_xfeatures even if they're not allowed in XCR0, e.g. because XCR0 isn't exposed to the guest. At that point, the simplest fix is to track the two things separately (allowed save/restore vs. allowed XCR0). Fixes: 988896bb6182 ("x86/kvm/fpu: Remove kvm_vcpu_arch.guest_supported_xcr0") Cc: stable@vger.kernel.org Cc: Leonardo Bras Signed-off-by: Sean Christopherson Message-Id: <20220824033057.3576315-2-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 5 ++--- arch/x86/kvm/x86.c | 9 ++------- 3 files changed, 5 insertions(+), 10 deletions(-) --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -713,6 +713,7 @@ struct kvm_vcpu_arch { struct fpu_guest guest_fpu; u64 xcr0; + u64 guest_supported_xcr0; struct kvm_pio_request pio; void *pio_data; --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -283,7 +283,6 @@ static void kvm_vcpu_after_set_cpuid(str { struct kvm_lapic *apic = vcpu->arch.apic; struct kvm_cpuid_entry2 *best; - u64 guest_supported_xcr0; best = kvm_find_cpuid_entry(vcpu, 1, 0); if (best && apic) { @@ -295,10 +294,10 @@ static void kvm_vcpu_after_set_cpuid(str kvm_apic_set_version(vcpu); } - guest_supported_xcr0 = + vcpu->arch.guest_supported_xcr0 = cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); - vcpu->arch.guest_fpu.fpstate->user_xfeatures = guest_supported_xcr0; + vcpu->arch.guest_fpu.fpstate->user_xfeatures = vcpu->arch.guest_supported_xcr0; kvm_update_pv_runtime(vcpu); --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1025,15 +1025,10 @@ void kvm_load_host_xsave_state(struct kv } EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); -static inline u64 kvm_guest_supported_xcr0(struct kvm_vcpu *vcpu) -{ - return vcpu->arch.guest_fpu.fpstate->user_xfeatures; -} - #ifdef CONFIG_X86_64 static inline u64 kvm_guest_supported_xfd(struct kvm_vcpu *vcpu) { - return kvm_guest_supported_xcr0(vcpu) & XFEATURE_MASK_USER_DYNAMIC; + return vcpu->arch.guest_supported_xcr0 & XFEATURE_MASK_USER_DYNAMIC; } #endif @@ -1056,7 +1051,7 @@ static int __kvm_set_xcr(struct kvm_vcpu * saving. However, xcr0 bit 0 is always set, even if the * emulated CPU does not support XSAVE (see kvm_vcpu_reset()). */ - valid_bits = kvm_guest_supported_xcr0(vcpu) | XFEATURE_MASK_FP; + valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; if (xcr0 & ~valid_bits) return 1;