[PATCH v2 3/4] x86/fpu: Disallow legacy states from fpstate_clear_xstate_component()

From: Chang S. Bae
Date: Thu Sep 22 2022 - 16:11:00 EST


Commit 087df48c298c ("x86/fpu: Replace KVMs xstate component clearing")
refactored the MPX state clearing code.

But, legacy states are not warranted in this routine:
- It presumes every state is contiguous but that's not true for the legacy
states. While MXCSR belongs to SSE, the state is located in the XSAVE
buffer as surrounded by FP states.
- Also, zeroing out legacy states is not meaningful as their init state is
non-zero.

It is possible to adjust the code to support them. Then, there is no use
for clearing legacy states yet. To make it simple, explicitly disallow
legacy states.

Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
Cc: x86@xxxxxxxxxx
Cc: kvm@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
Changes from v1 (Sean Christopherson):
* Revert the name change.
* Add a warning.
* Update title/changelog.
---
arch/x86/kernel/fpu/xstate.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index d7676cfc32eb..a3f7045d1f8e 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -1375,6 +1375,15 @@ void fpstate_clear_xstate_component(struct fpstate *fps, unsigned int xfeature)
{
void *addr = get_xsave_addr(&fps->regs.xsave, xfeature);

+ /*
+ * Allow extended states only, because:
+ * (1) Each legacy state is not contiguously located in the buffer.
+ * (2) Zeroing those states is not meaningful as their init states
+ * are not zero.
+ */
+ if (WARN_ON_ONCE(xfeature <= XFEATURE_SSE))
+ return;
+
if (addr)
memset(addr, 0, xstate_sizes[xfeature]);
}
--
2.17.1