[PATCH v5 0/7] Introduce CET supervisor state support

From: Chao Gao
Date: Thu Apr 10 2025 - 03:29:23 EST


Dear maintainers and reviewers,

I would appreciate your feedback on two unresolved issues:

1. Management of guest default features and sizes (patch 3)

The CET supervisor state will be added as a guest-only feature, causing
guest FPU default features and sizes to diverge from those of non-guest
FPUs. Calculating guest FPU default features and sizes at runtime based on
defaults in fpu_kernel/user_cfg is undesirable due to unnecessary runtime
overhead and the need for calculations in multiple places, such as during
vCPU fpstate allocation and FPU permission initialization.

The preferred approach is to calculate guest default features and sizes
at boot time and then cache them.

Versions v3 and earlier used a guest-specific fpu_state_config to hold
guest defaults, but the other fields in the struct were unused, suggesting
that fpu_guest_cfg might not be the best fit. In v4, inspired by the
existing independent_features field, I switched to adding guest defaults to
fpu_kernel/user_cfg. During the review, Chang suggested a cleaner and more
structured alternative by introducing a dedicated struct for guest
defaults. This v5 follows Chang's suggestion.

2. Naming of the guest-only feature mask (patch 6)

Instead of hard-coding the CET supervisor state in various places, we
establish an infrastructure that introduces a mask to abstract the features
necessary for guest FPUs, which are not enabled by non-guest FPUs.

The mask was previously named XFEATURE_MASK_KERNEL_DYNAMIC. It was slightly
preferred because it reflected the XSAVE buffer state (some buffers have
the features while others do not) rather than being tied to its
usage - KVM. However, this name led to confusion. In this v5, it has been
renamed to XFEATURE_MASK_GUEST_SUPERVISOR. Please refer to patch 6 for the
decision-making process.

Aside from these issues, I believe this series is in good shape. However,
since most of the patches have not yet received Reviewed-by/Acked-by tags,
please take a look at them as well.

Also, credit to Chang and Rick for their help with this series.

== Changelog ==
v4->v5:
- Reorder patches to ensure new features come after cleanups and bug fixes
(Chang, Dave, Ingo)
- Add a dedicated structure for default xfeatures and sizes for guest FPUs
(Chang)
- Provide a detailed explanation for choosing XFEATURE_MASK_GUEST_SUPERVISOR
(Chang)
- Summarize the long history of the CET FPU series and the rationale behind
the "guest-only" approach in the cover-letter and the last patch
- Other minor changes; please see each patch for details. (Chang)
- v4: https://lore.kernel.org/kvm/20250318153316.1970147-1-chao.gao@xxxxxxxxx/

== Background ==

CET defines two register states: CET user, which includes user-mode control
registers, and CET supervisor, which consists of shadow-stack pointers for
privilege levels 0-2.

Current kernels disable shadow stacks in kernel mode, making the CET
supervisor state unused and eliminating the need for context switching.

== Problem ==

To virtualize CET for guests, KVM must accurately emulate hardware
behavior. A key challenge arises because there is no CPUID flag to indicate
that shadow stack is supported only in user mode. Therefore, KVM cannot
assume guests will not enable shadow stacks in kernel mode and must
preserve the CET supervisor state of vCPUs.

== Solution ==

An initial proposal to manually save and restore CET supervisor states
using raw RDMSR/WRMSR in KVM was rejected due to performance concerns and
its impact on KVM's ABI. Instead, leveraging the kernel's FPU
infrastructure for context switching was favored [1].

The main question then became whether to enable the CET supervisor state
globally for all processes or restrict it to vCPU processes. This decision
involves a trade-off between a 24-byte XSTATE buffer waste for all non-vCPU
processes and approximately 100 lines of code complexity in the kernel [2].
The agreed approach is to first try this optimal solution [3], i.e.,
restricting the CET supervisor state to guest FPUs only and eliminating
unnecessary space waste.

Key changes in this series are:

1) Fix existing issue regarding enabling guest supervisor states support.
2) Add default features and size for guest FPUs.
3) Add infrastructure to support guest-only features.
4) Add CET supervisor state as the first guest-only feature.

With the series in place, guest FPUs have xstate_bv[12] == xcomp_bv[12] == 1
and CET supervisor state is saved/reloaded when xsaves/xrstors executes on
guest FPUs. non-guest FPUs have xstate_bv[12] == xcomp_bv[12] == 0, then
CET supervisor state is not saved/restored.

== Performance ==

We measured context-switching performance with the benchmark [4] in following
three cases.

case 1: the baseline. i.e., this series isn't applied
case 2: baseline + this series. CET-S space is allocated for guest fpu only.
case 3: baseline + allocate CET-S space for all tasks. Hardware init
optimization avoids writing out CET-S space on each XSAVES.

The performance differences in the three cases are very small and fall within the
run-to-run variation.

[1]: https://lore.kernel.org/kvm/ZM1jV3UPL0AMpVDI@xxxxxxxxxx/
[2]: https://lore.kernel.org/kvm/1c2fd06e-2e97-4724-80ab-8695aa4334e7@xxxxxxxxx/
[3]: https://lore.kernel.org/kvm/2597a87b-1248-b8ce-ce60-94074bc67ea4@xxxxxxxxx/
[4]: https://github.com/antonblanchard/will-it-scale/blob/master/tests/context_switch1.c

Chao Gao (4):
x86/fpu: Drop @perm from guest pseudo FPU container
x86/fpu/xstate: Differentiate default features for host and guest FPUs
x86/fpu: Initialize guest FPU permissions from guest defaults
x86/fpu: Initialize guest fpstate and FPU pseudo container from guest
defaults

Sean Christopherson (1):
x86/fpu/xstate: Always preserve non-user xfeatures/flags in
__state_perm

Yang Weijiang (2):
x86/fpu/xstate: Introduce "guest-only" supervisor xfeature set
x86/fpu/xstate: Add CET supervisor xfeature support as a guest-only
feature

arch/x86/include/asm/fpu/types.h | 81 +++++++++++++++++++++++++------
arch/x86/include/asm/fpu/xstate.h | 9 ++--
arch/x86/kernel/fpu/core.c | 42 ++++++++++------
arch/x86/kernel/fpu/xstate.c | 60 +++++++++++++++++------
arch/x86/kernel/fpu/xstate.h | 5 ++
5 files changed, 149 insertions(+), 48 deletions(-)


base-commit: ed91ad084ef9fa49f6c6dfef2cb10c12ce3786a6
--
2.46.1