Re: [PATCH 1/1] x86/fpu: Fix copy_xstate_to_uabi() to copy init states correctly

From: Chang S. Bae
Date: Thu Oct 20 2022 - 14:52:44 EST


On 10/20/2022 9:57 AM, Dave Hansen wrote:
On 10/18/22 15:13, Chang S. Bae wrote:
@@ -1141,10 +1141,14 @@ void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
*/
pkru.pkru = pkru_val;
membuf_write(&to, &pkru, sizeof(pkru));
+ } else if (!(header.xfeatures & BIT_ULL(i))) {
+ /*
+ * Every extended state component has an all zeros
+ * init state.
+ */
+ membuf_zero(&to, xstate_sizes[i]);
} else {
- copy_feature(header.xfeatures & BIT_ULL(i), &to,
- __raw_xsave_addr(xsave, i),
- __raw_xsave_addr(xinit, i),
+ membuf_write(&to, __raw_xsave_addr(xsave, i),
xstate_sizes[i]);
}

Just to add a bit more context, this is inside this loop:

mask = fpstate->user_xfeatures;
for_each_extended_xfeature(i, mask) {
if (zerofrom < xstate_offsets[i])
membuf_zero(&to, xstate_offsets[i] - zerofrom);
...
}
if (to.left)
membuf_zero(&to, to.left);

In other words, the loop and the surrounding code already know how to
membuf_zero() any gaps in the middle or the end of the user buffer.
Would it be simpler to just adjust the 'mask' over which the loop iterates?

Yeah, right!

I think that would end up being something like:

mask = fpstate->user_xfeatures &
(xsave->xfeatures | xinit->xfeatures);

Logically, that makes sense too. We're copying out of either 'xsave' or
'xinit'. If a feature isn't in either one of those we can't do the
copy_feature() on it.

Yes, it is. But, one tricky part here is xinit->xstate_bv is zero. Instead, xinit->xcomp_bv appears to be relevant. Also, we want this for dynamic features that rely on XSAVES. Then, the change can be something like this:

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index e77cabfa802f..3f3286d7e1a8 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -1125,6 +1125,15 @@ void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
*/
mask = fpstate->user_xfeatures;

+ /*
+ * Dynamic features are not present in init_fpstate since they have
+ * an all zeros init state. When they are in init state, instead of
+ * retrieving them from init_fpstate, remove those from 'mask' to
+ * zero the user buffer.
+ */
+ if (fpu_state_size_dynamic())
+ mask &= (header.xfeatures | xinit->header.xcomp_bv);

Thanks,
Chang