public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jon Kohler <jon@nutanix.com>
To: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Chang S. Bae" <chang.seok.bae@intel.com>,
	Kyle Huey <me@kylehuey.com>,
	neelnatu@google.com, Andrew Cooper <andrew.cooper3@citrix.com>,
	Jon Kohler <jon@nutanix.com>,
	linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [PATCH] x86/fpu/xstate: clear XSAVE features if DISABLED_MASK set
Date: Tue, 30 May 2023 16:01:50 -0400	[thread overview]
Message-ID: <20230530200152.18961-1-jon@nutanix.com> (raw)

Respect DISABLED_MASK when clearing XSAVE features, such that features
that are disabled do not appear in the xfeatures mask.

This is important for kvm_load_{guest|host}_xsave_state, which look
at host_xcr0 and will do an expensive xsetbv when the guest and host
do not match.

A prime example if CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS is disabled,
the guest OS will not see PKU masked; however, the guest will incur
xsetbv since the host mask will never match the guest, even though
DISABLED_MASK16 has DISABLE_PKU set.

Signed-off-by: Jon Kohler <jon@nutanix.com>
CC: kvm@vger.kernel.org
CC: Sean Christopherson <seanjc@google.com>
CC: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kernel/fpu/xstate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index 0bab497c9436..211ef82b53e3 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -798,7 +798,8 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
 		unsigned short cid = xsave_cpuid_features[i];

 		/* Careful: X86_FEATURE_FPU is 0! */
-		if ((i != XFEATURE_FP && !cid) || !boot_cpu_has(cid))
+		if ((i != XFEATURE_FP && !cid) || !boot_cpu_has(cid) ||
+		    DISABLED_MASK_BIT_SET(cid))
 			fpu_kernel_cfg.max_features &= ~BIT_ULL(i);
 	}

--
2.30.1 (Apple Git-130)


             reply	other threads:[~2023-05-30 20:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-30 20:01 Jon Kohler [this message]
2023-05-30 22:22 ` [PATCH] x86/fpu/xstate: clear XSAVE features if DISABLED_MASK set Dave Hansen
2023-05-31  3:04   ` Jon Kohler
2023-05-31 16:30     ` Sean Christopherson
     [not found]       ` <E17EFDD7-C54A-4532-B1D3-D567557FC54B@nutanix.com>
     [not found]         ` <ZHermsSGQBcDD07R@google.com>
2023-05-31 21:29           ` Borislav Petkov
2023-05-31 22:02             ` Sean Christopherson
     [not found]           ` <CBFC095A-10D1-4925-9F28-DEDEBBB38EF8@nutanix.com>
2023-06-05 13:23             ` Jon Kohler
2023-06-06 23:40               ` Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230530200152.18961-1-jon@nutanix.com \
    --to=jon@nutanix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=bp@alien8.de \
    --cc=chang.seok.bae@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=me@kylehuey.com \
    --cc=mingo@redhat.com \
    --cc=neelnatu@google.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox