Re: [PATCH] x86/fpu: Remove dynamic features from xcomp_bv for init_fpstate

From: Chang S. Bae
Date: Mon Oct 17 2022 - 18:39:50 EST


On 10/11/2022 3:24 PM, Dave Hansen wrote:

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 3b28c5b25e12..4d64de34da12 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -526,6 +526,9 @@ static void __fpstate_reset(struct fpstate *fpstate, u64 xfd)
fpstate->xfeatures = fpu_kernel_cfg.default_features;
fpstate->user_xfeatures = fpu_user_cfg.default_features;
fpstate->xfd = xfd;
+
+ /* Ensure that xcomp_bv matches ->xfeatures */
+ xstate_init_xcomp_bv(&fpstate->regs.xsave, fpstate->xfeatures);
}

I have some difficulty understanding the problem without this. Maybe I'm missing something here:

We have two call sites for this -- (a) one for the guest fpstate allocation [1] and (b) the other for the reset [2].

(a) The former has a call chain to init xcomp_bv:
fpu_alloc_guest_fpstate()->fpstate_init_user()->xstate_init_xcomp_bv()

(b) And the latter picks up the default area:
void fpstate_reset(struct fpu *fpu)
{
/* Set the fpstate pointer to the default fpstate */
fpu->fpstate = &fpu->__fpstate;
__fpstate_reset(fpu->fpstate, init_fpstate.xfd);
...
}
Then, xcomp_bv looks to be subsequently written by XSAVE* or by copying from init_fpstate.

There are three distinct call sites for fpstate_reset():
* fpu_clone() [3]: the child will either copy from init_fpstate or do XSAVE* that will update xcomp_bv according to ->xfeatures.
* fpu__init_system_xstate() [4]: When the init task switches away, xcomp_bv will be updated by XSAVE*.
* fpu_flush_thread() [5]: xcomp_bv will be copied from init_fpstate via fpu_reset_fpregs().

Thanks,
Chang

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/fpu/core.c#n229
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/fpu/core.c#n535
[3]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/fpu/core.c#n567
[4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/fpu/xstate.c#n869
[5]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/fpu/core.c#n744