Introduce CET supervisor state support
cover Introduce CET supervisor state support
This series spins off from CET KVM virtualization enabling series [1]. The purpose is to get these preparation work resolved ahead of KVM part landing. There was a discussion about introducing CET supervisor state support [2] [3].
CET supervisor state, i.e., IA32_PL{0,1,2}_SSP, are xsave-managed MSRs, it can be enabled via IA32_XSS[bit 12]. KVM relies on host side CET supervisor state support to fully enable guest CET MSR contents storage. The benefits are: 1) No need to manually save/restore the 3 MSRs when vCPU fpu context is sched in/out. 2) Omit manually swapping the three MSRs at VM-Exit/VM-Entry for guest/host. 3) Make guest CET user/supervisor states managed in a consistent manner within host kernel FPU framework.
This series tries to:
1) Fix existing issue regarding enabling guest supervisor states support.
2) Add CET supervisor state support in core kernel.
3) Introduce new FPU config for guest fpstate setup.
With the preparation work landed, for guest fpstate, we have xstate_bv[12] == xcomp_bv[12] == 1 and CET supervisor state is saved/reloaded when xsaves/xrstors executes on guest fpstate.
For non-guest/normal fpstate, we have xstate_bv[12] == xcomp_bv[12] == 0, then HW can optimize xsaves/xrstors operations.
Patch1: Preserve guest supervisor xfeatures in __state_perm.
Patch2: Enable CET supervisor xstate support.
Patch3: Introduce kernel dynamic xfeature set.
Patch4: Initialize fpu_guest_cfg settings.
Patch5: Create guest fpstate with fpu_guest_cfg.
Patch6: Check invalid fpstate config before executes xsaves.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/
Sean Christopherson (1):
x86/fpu/xstate: Always preserve non-user xfeatures/flags in
__state_perm
Yang Weijiang (5):
x86/fpu/xstate: Add CET supervisor mode state support x86/fpu/xstate: Introduce XFEATURE_MASK_KERNEL_DYNAMIC xfeature set x86/fpu/xstate: Introduce fpu_guest_cfg for guest FPU configuration x86/fpu/xstate: Create guest fpstate with guest specific config x86/fpu/xstate: Warn if CET supervisor state is detected in normal
fpstate
arch/x86/include/asm/fpu/types.h | 16 ++++++++-- arch/x86/include/asm/fpu/xstate.h | 11 ++++--- arch/x86/kernel/fpu/core.c | 53 ++++++++++++++++++++++++------- arch/x86/kernel/fpu/xstate.c | 35 +++++++++++++++----- arch/x86/kernel/fpu/xstate.h | 2 ++ 5 files changed, 90 insertions(+), 27 deletions(-)
base-commit: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0
Signature · 1 line
2.43.0
PATCH 1/6 x86/fpu/xstate: Always preserve non-user xfeatures/flags in __state_perm
From: Sean Christopherson <[email protected]>
When granting userspace or a KVM guest access to an xfeature, preserve the entity's existing supervisor and software-defined permissions as tracked by __state_perm, i.e. use __state_perm to track *all* permissions even though all supported supervisor xfeatures are granted to all FPUs and FPU_GUEST_PERM_LOCKED disallows changing permissions.
Effectively clobbering supervisor permissions results in inconsistent behavior, as xstate_get_group_perm() will report supervisor features for process that do NOT request access to dynamic user xfeatures, whereas any and all supervisor features will be absent from the set of permissions for any process that is granted access to one or more dynamic xfeatures (which right now means AMX).
The inconsistency isn't problematic because fpu_xstate_prctl() already strips out everything except user xfeatures:
case ARCH_GET_XCOMP_PERM:
/*
* Lockless snapshot as it can also change right after the
* dropping the lock.
*/
permitted = xstate_get_host_group_perm();
permitted &= XFEATURE_MASK_USER_SUPPORTED;
return put_user(permitted, uptr);
case ARCH_GET_XCOMP_GUEST_PERM:
permitted = xstate_get_guest_group_perm();
permitted &= XFEATURE_MASK_USER_SUPPORTED;
return put_user(permitted, uptr);and similarly KVM doesn't apply the __state_perm to supervisor states (kvm_get_filtered_xcr0() incorporates xstate_get_guest_group_perm()):
case 0xd: {
u64 permitted_xcr0 = kvm_get_filtered_xcr0();
u64 permitted_xss = kvm_caps.supported_xss;But if KVM in particular were to ever change, dropping supervisor permissions would result in subtle bugs in KVM's reporting of supported CPUID settings. And the above behavior also means that having supervisor xfeatures in __state_perm is correctly handled by all users.
Dropping supervisor permissions also creates another landmine for KVM. If more dynamic user xfeatures are ever added, requesting access to multiple xfeatures in separate ARCH_REQ_XCOMP_GUEST_PERM calls will result in the second invocation of __xstate_request_perm() computing the wrong ksize, as as the mask passed to xstate_calculate_size() would not contain *any* supervisor features.
Commit 781c64bfcb73 ("x86/fpu/xstate: Handle supervisor states in XSTATE permissions") fudged around the size issue for userspace FPUs, but for reasons unknown skipped guest FPUs. Lack of a fix for KVM "works" only because KVM doesn't yet support virtualizing features that have supervisor xfeatures, i.e. as of today, KVM guest FPUs will never need the relevant xfeatures.
Simply extending the hack-a-fix for guests would temporarily solve the ksize issue, but wouldn't address the inconsistency issue and would leave another lurking pitfall for KVM. KVM support for virtualizing CET will likely add CET_KERNEL as a guest-only xfeature, i.e. CET_KERNEL will not be set in xfeatures_mask_supervisor() and would again be dropped when granting access to dynamic xfeatures.
Note, the existing clobbering behavior is rather subtle. The @permitted parameter to __xstate_request_perm() comes from:
permitted = xstate_get_group_perm(guest);
which is either fpu->guest_perm.__state_perm or fpu->perm.__state_perm, where __state_perm is initialized to:
fpu->perm.__state_perm = fpu_kernel_cfg.default_features;
and copied to the guest side of things:
/* Same defaults for guests */ fpu->guest_perm = fpu->perm;
fpu_kernel_cfg.default_features contains everything except the dynamic xfeatures, i.e. everything except XFEATURE_MASK_XTILE_DATA:
fpu_kernel_cfg.default_features = fpu_kernel_cfg.max_features;
fpu_kernel_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC;When __xstate_request_perm() restricts the local "mask" variable to compute the user state size:
mask &= XFEATURE_MASK_USER_SUPPORTED; usize = xstate_calculate_size(mask, false);
it subtly overwrites the target __state_perm with "mask" containing only user xfeatures:
perm = guest ? &fpu->guest_perm : &fpu->perm; /* Pairs with the READ_ONCE() in xstate_get_group_perm() */ WRITE_ONCE(perm->__state_perm, mask);
--- arch/x86/kernel/fpu/xstate.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
arch/x86/kernel/fpu/xstate.c +11 -7
@@ -1603,16 +1603,20 @@ static int __xstate_request_perm(u64 permitted, u64 requested, bool guest)if ((permitted & requested) == requested)return 0 ; -/* Calculate the resulting kernel state size */ +/* +* Calculate the resulting kernel state size. Note, @permitted also +* contains supervisor xfeatures even though supervisor are always +* permitted for kernel and guest FPUs, and never permitted for user +* FPUs. +*/ mask = permitted | requested; -/* Take supervisor states into account on the host */ -if (!guest) - mask |=xfeatures_mask_supervisor (); ksize =xstate_calculate_size (mask, compacted); -/* Calculate the resulting user state size */ - mask &=XFEATURE_MASK_USER_SUPPORTED ; - usize =xstate_calculate_size (mask, false); +/* +* Calculate the resulting user state size. Take care not to clobber +* the supervisor xfeatures in the new mask! +*/ + usize =xstate_calculate_size (mask &XFEATURE_MASK_USER_SUPPORTED , false);if (!guest) { ret =validate_sigaltstack (usize);
Signature · 1 line
2.43.0
PATCH 2/6 x86/fpu/xstate: Add CET supervisor mode state support
In reply to Yang WeijiangAdd supervisor mode state support within FPU xstate management framework. Although supervisor shadow stack is not enabled/used today in kernel,KVM requires the support because when KVM advertises shadow stack feature to guest, architecturally it claims the support for both user and supervisor modes for guest OSes(Linux or non-Linux).
CET supervisor states not only includes PL{0,1,2}_SSP but also IA32_S_CET MSR, but the latter is not xsave-managed. In virtualization world, guest IA32_S_CET is saved/stored into/from VM control structure. With supervisor xstate support, guest supervisor mode shadow stack state can be properly saved/restored when 1) guest/host FPU context is swapped 2) vCPU thread is sched out/in.
The alternative is to enable it in KVM domain, but KVM maintainers NAKed the solution. The external discussion can be found at [*], it ended up with adding the support in kernel instead of KVM domain.
Note, in KVM case, guest CET supervisor state i.e., IA32_PL{0,1,2}_MSRs, are preserved after VM-Exit until host/guest fpstates are swapped, but since host supervisor shadow stack is disabled, the preserved MSRs won't hurt host.
[*]: https://lore.kernel.org/all/[email protected]/
--- arch/x86/include/asm/fpu/types.h | 14 ++++++++++++-- arch/x86/include/asm/fpu/xstate.h | 6 +++--- arch/x86/kernel/fpu/xstate.c | 6 +++++- 3 files changed, 20 insertions(+), 6 deletions(-)
arch/x86/include/asm/fpu/types.h +12 -2
@@ -118,7 +118,7 @@ enum xfeature {XFEATURE_PKRU ,XFEATURE_PASID ,XFEATURE_CET_USER , -XFEATURE_CET_KERNEL_UNUSED , +XFEATURE_CET_KERNEL ,XFEATURE_RSRVD_COMP_13 ,XFEATURE_RSRVD_COMP_14 , XFEATURE_LBR,
@@ -141,7 +141,7 @@ enum xfeature {#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU)#define XFEATURE_MASK_PASID (1 << XFEATURE_PASID)#define XFEATURE_MASK_CET_USER (1 << XFEATURE_CET_USER) -#define XFEATURE_MASK_CET_KERNEL (1 << XFEATURE_CET_KERNEL_UNUSED) +#define XFEATURE_MASK_CET_KERNEL (1 << XFEATURE_CET_KERNEL)#define XFEATURE_MASK_LBR (1 << XFEATURE_LBR)#define XFEATURE_MASK_XTILE_CFG (1 << XFEATURE_XTILE_CFG)#define XFEATURE_MASK_XTILE_DATA (1 << XFEATURE_XTILE_DATA)
@@ -266,6 +266,16 @@ struct cet_user_state {u64 user_ssp; }; +/* +* State component 12 is Control-flow Enforcement supervisor states +*/ +struct cet_supervisor_state { +/* supervisor ssp pointers */ +u64 pl0_ssp; +u64 pl1_ssp; +u64 pl2_ssp; +}; +/* * State component 15: Architectural LBR configuration state. * The size of Arch LBR state depends on the number of LBRs (lbr_depth).
arch/x86/include/asm/fpu/xstate.h +3 -3
@@ -51,7 +51,8 @@/* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID | \ - XFEATURE_MASK_CET_USER) + XFEATURE_MASK_CET_USER | \ + XFEATURE_MASK_CET_KERNEL)/* * A supervisor state component may not always contain valuable information,
@@ -78,8 +79,7 @@* Unsupported supervisor features. When a supervisor feature in this mask is * supported in the future, move it to the supported supervisor feature mask. */ -#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT | \ - XFEATURE_MASK_CET_KERNEL) +#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)/* All supervisor states including supported and unsupported states. */ #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
arch/x86/kernel/fpu/xstate.c +5 -1
@@ -51,7 +51,7 @@ static const char *xfeature_names[] ="Protection Keys User registers" ,"PASID state" ,"Control-flow User registers" , -"Control-flow Kernel registers (unused)" , +"Control-flow Kernel registers" ,"unknown xstate feature" ,"unknown xstate feature" , "unknown xstate feature",
@@ -74,6 +74,7 @@ static unsigned short xsave_cpuid_features[] __initdata = { [XFEATURE_PKRU] = X86_FEATURE_OSPKE, [XFEATURE_PASID] = X86_FEATURE_ENQCMD, [XFEATURE_CET_USER] = X86_FEATURE_SHSTK, + [XFEATURE_CET_KERNEL] = X86_FEATURE_SHSTK, [XFEATURE_XTILE_CFG] = X86_FEATURE_AMX_TILE, [XFEATURE_XTILE_DATA] = X86_FEATURE_AMX_TILE, };
@@ -279,6 +280,7 @@ static void __init print_xstate_features(void)print_xstate_feature (XFEATURE_MASK_PKRU );print_xstate_feature (XFEATURE_MASK_PASID );print_xstate_feature (XFEATURE_MASK_CET_USER ); +print_xstate_feature (XFEATURE_MASK_CET_KERNEL );print_xstate_feature (XFEATURE_MASK_XTILE_CFG );print_xstate_feature (XFEATURE_MASK_XTILE_DATA ); }
@@ -348,6 +350,7 @@ static __init void os_xrstor_booting(struct xregs_state *xstate)XFEATURE_MASK_BNDCSR | \XFEATURE_MASK_PASID | \XFEATURE_MASK_CET_USER | \ +XFEATURE_MASK_CET_KERNEL | \ XFEATURE_MASK_XTILE)/*
@@ -548,6 +551,7 @@ static bool __init check_xstate_against_struct(int nr) case XFEATURE_PASID: return XCHECK_SZ(sz, nr, struct ia32_pasid_state); case XFEATURE_XTILE_CFG: return XCHECK_SZ(sz, nr, struct xtile_cfg); case XFEATURE_CET_USER: return XCHECK_SZ(sz, nr, struct cet_user_state); + case XFEATURE_CET_KERNEL: return XCHECK_SZ(sz, nr, struct cet_supervisor_state);case XFEATURE_XTILE_DATA :check_xtile_data_against_struct (sz);return true;default :XSTATE_WARN_ON (1 ,"No structure for xstate: %d\n" , nr);
Signature · 1 line
2.43.0
PATCH 3/6 x86/fpu/xstate: Introduce XFEATURE_MASK_KERNEL_DYNAMIC xfeature set
In reply to Yang WeijiangDefine a new XFEATURE_MASK_KERNEL_DYNAMIC mask to specify the features that can be optionally enabled by kernel components. This is similar to XFEATURE_MASK_USER_DYNAMIC in that it contains optional xfeatures that can allows the FPU buffer to be dynamically sized. The difference is that the KERNEL variant contains supervisor features and will be enabled by kernel components that need them, and not directly by the user. Currently it's used by KVM to configure guest dedicated fpstate for calculating the xfeature and fpstate storage size etc.
The kernel dynamic xfeatures now only contain XFEATURE_CET_KERNEL, which is supported by host as they're enabled in kernel XSS MSR setting but relevant CPU feature, i.e., supervisor shadow stack, is not enabled in host kernel therefore it can be omitted for normal fpstate by default.
Remove the kernel dynamic feature from fpu_kernel_cfg.default_features so that the bits in xstate_bv and xcomp_bv are cleared and xsaves/xrstors can be optimized by HW for normal fpstate.
--- arch/x86/include/asm/fpu/xstate.h | 5 ++++- arch/x86/kernel/fpu/xstate.c | 1 + 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/include/asm/fpu/xstate.h +4 -1
@@ -46,9 +46,12 @@#define XFEATURE_MASK_USER_RESTORE \ (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU) -/* Features which are dynamically enabled for a process on request */ +/* Features which are dynamically enabled per userspace request */ #define XFEATURE_MASK_USER_DYNAMIC XFEATURE_MASK_XTILE_DATA +/* Features which are dynamically enabled per kernel side request */ +#define XFEATURE_MASK_KERNEL_DYNAMIC XFEATURE_MASK_CET_KERNEL +/* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID | \ XFEATURE_MASK_CET_USER | \
arch/x86/kernel/fpu/xstate.c +1 -0
@@ -818,6 +818,7 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)/* Clean out dynamic features from default */ fpu_kernel_cfg.default_features = fpu_kernel_cfg.max_features; fpu_kernel_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ; + fpu_kernel_cfg.default_features &= ~XFEATURE_MASK_KERNEL_DYNAMIC ; fpu_user_cfg.default_features = fpu_user_cfg.max_features; fpu_user_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ;
Signature · 1 line
2.43.0
PATCH 4/6 x86/fpu/xstate: Introduce fpu_guest_cfg for guest FPU configuration
In reply to Yang WeijiangDefine new fpu_guest_cfg to hold all guest FPU settings so that it can differ from generic kernel FPU settings, e.g., enabling CET supervisor xstate by default for guest fpstate while it's remained disabled in kernel FPU config.
The kernel dynamic xfeatures are specifically used by guest fpstate now, add the mask for guest fpstate so that guest_perm.__state_perm == (fpu_kernel_cfg.default_xfeature | XFEATURE_MASK_KERNEL_DYNAMIC). And if guest fpstate is re-allocated to hold user dynamic xfeatures, the resulting permissions are consumed before calculate new guest fpstate.
With new guest FPU config added, there're 3 categories of FPU configs in kernel, the usages and key fields are recapped as below.
kernel FPU config:
@fpu_kernel_cfg.max_features
- all known and CPU supported user and supervisor features except independent kernel features
@fpu_kernel_cfg.default_features
- all known and CPU supported user and supervisor features except dynamic kernel features, independent kernel features and dynamic userspace features.
@fpu_kernel_cfg.max_size
- size of compacted buffer with 'fpu_kernel_cfg.max_features'
@fpu_kernel_cfg.default_size
- size of compacted buffer with 'fpu_kernel_cfg.default_features'
user FPU config:
@fpu_user_cfg.max_features
- all known and CPU supported user features
@fpu_user_cfg.default_features
- all known and CPU supported user features except dynamic userspace features.
@fpu_user_cfg.max_size
- size of non-compacted buffer with 'fpu_user_cfg.max_features'
@fpu_user_cfg.default_size
- size of non-compacted buffer with 'fpu_user_cfg.default_features'
guest FPU config:
@fpu_guest_cfg.max_features
- all known and CPU supported user and supervisor features except independent kernel features.
@fpu_guest_cfg.default_features
- all known and CPU supported user and supervisor features except independent kernel features and dynamic userspace features.
@fpu_guest_cfg.max_size
- size of compacted buffer with 'fpu_guest_cfg.max_features'
@fpu_guest_cfg.default_size
- size of compacted buffer with 'fpu_guest_cfg.default_features'
--- arch/x86/include/asm/fpu/types.h | 2 +- arch/x86/kernel/fpu/core.c | 14 +++++++++++--- arch/x86/kernel/fpu/xstate.c | 10 ++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-)
arch/x86/include/asm/fpu/types.h +1 -1
@@ -604,6 +604,6 @@ struct fpu_state_config { };/* FPU state configuration information */ -extern struct fpu_state_config fpu_kernel_cfg, fpu_user_cfg; +extern struct fpu_state_config fpu_kernel_cfg, fpu_user_cfg, fpu_guest_cfg;#endif /* _ASM_X86_FPU_TYPES_H */
arch/x86/kernel/fpu/core.c +11 -3
@@ -33,9 +33,10 @@ DEFINE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);DEFINE_PER_CPU (u64, xfd_state);#endif -/* The FPU state configuration data for kernel and user space */ +/* The FPU state configuration data for kernel, user space and guest. */ struct fpu_state_config fpu_kernel_cfg__ro_after_init ;struct fpu_state_config fpu_user_cfg__ro_after_init ; +struct fpu_state_config fpu_guest_cfg__ro_after_init ;/* * Represents the initial FPU state. It's mostly (but not completely) zeroes,
@@ -536,8 +537,15 @@ void fpstate_reset(struct fpu *fpu) fpu->perm.__state_perm = fpu_kernel_cfg.default_features; fpu->perm.__state_size = fpu_kernel_cfg.default_size; fpu->perm.__user_state_size = fpu_user_cfg.default_size; -/* Same defaults for guests */ - fpu->guest_perm = fpu->perm; + +/* Guest permission settings */ + fpu->guest_perm.__state_perm = fpu_guest_cfg.default_features; + fpu->guest_perm.__state_size = fpu_guest_cfg.default_size; +/* +* Set guest's __user_state_size to fpu_user_cfg.default_size so that +* existing uAPIs can still work. +*/ + fpu->guest_perm.__user_state_size = fpu_user_cfg.default_size; }static inline void fpu_inherit_perms (struct fpu *dst_fpu)
arch/x86/kernel/fpu/xstate.c +10 -0
@@ -683,6 +683,7 @@ static int __init init_xstate_size(void) {/* Recompute the context size for enabled features: */ unsigned int user_size, kernel_size, kernel_default_size; +unsigned int guest_default_size;bool compacted =cpu_feature_enabled (X86_FEATURE_XCOMPACTED );/* Uncompacted user space size */
@@ -704,13 +705,18 @@ static int __init init_xstate_size(void) kernel_default_size =xstate_calculate_size (fpu_kernel_cfg.default_features, compacted); + guest_default_size = +xstate_calculate_size (fpu_guest_cfg.default_features, compacted); +if (!paranoid_xstate_size_valid (kernel_size))return -EINVAL ; fpu_kernel_cfg.max_size = kernel_size; fpu_user_cfg.max_size = user_size; + fpu_guest_cfg.max_size = kernel_size; fpu_kernel_cfg.default_size = kernel_default_size; + fpu_guest_cfg.default_size = guest_default_size; fpu_user_cfg.default_size =xstate_calculate_size (fpu_user_cfg.default_features, false);
@@ -823,6 +829,10 @@ void __init fpu__init_system_xstate(unsigned int legacy_size) fpu_user_cfg.default_features = fpu_user_cfg.max_features; fpu_user_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ; + fpu_guest_cfg.max_features = fpu_kernel_cfg.max_features; + fpu_guest_cfg.default_features = fpu_guest_cfg.max_features; + fpu_guest_cfg.default_features &= ~XFEATURE_MASK_USER_DYNAMIC ; +/* Store it for paranoia check at the end */ xfeatures = fpu_kernel_cfg.max_features;
Signature · 1 line
2.43.0
PATCH 5/6 x86/fpu/xstate: Create guest fpstate with guest specific config
In reply to Yang WeijiangUse fpu_guest_cfg to calculate guest fpstate settings, open code for __fpstate_reset() to avoid using kernel FPU config.
Below configuration steps are currently enforced to get guest fpstate:
1) Kernel sets up guest FPU settings in fpu__init_system_xstate().
2) User space sets vCPU thread group xstate permits via arch_prctl().
3) User space creates guest fpstate via __fpu_alloc_init_guest_fpstate() for vcpu thread.
4) User space enables guest dynamic xfeatures and re-allocate guest fpstate.
By adding kernel dynamic xfeatures in above #1 and #2, guest xstate area size is expanded to hold (fpu_kernel_cfg.default_features | kernel dynamic xfeatures | user dynamic xfeatures), then host xsaves/xrstors can operate for all guest xfeatures.
The user_* fields remain unchanged for compatibility with KVM uAPIs.
--- arch/x86/kernel/fpu/core.c | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-)
arch/x86/kernel/fpu/core.c +30 -9
@@ -194,8 +194,6 @@ void fpu_reset_from_exception_fixup(void) }#if IS_ENABLED (CONFIG_KVM ) -static void __fpstate_reset (struct fpstate *fpstate,u64 xfd); -static void fpu_init_guest_permissions (struct fpu_guest *gfpu) {struct fpu_state_perm *fpuperm;
@@ -216,25 +214,48 @@ static void fpu_init_guest_permissions(struct fpu_guest *gfpu) gfpu->perm = perm & ~FPU_GUEST_PERM_LOCKED ; } -bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu) +static struct fpstate *__fpu_alloc_init_guest_fpstate (struct fpu_guest *gfpu) {struct fpstate *fpstate;unsigned int size; - size = fpu_user_cfg.default_size +ALIGN (offsetof(struct fpstate , regs),64 ); +/* +* fpu_guest_cfg.default_size is initialized to hold all enabled +* xfeatures except the user dynamic xfeatures. If the user dynamic +* xfeatures are enabled, the guest fpstate will be re-allocated to +* hold all guest enabled xfeatures, so omit user dynamic xfeatures +* here. +*/ + size = fpu_guest_cfg.default_size +ALIGN (offsetof(struct fpstate , regs),64 ); + fpstate =vzalloc (size);if (!fpstate) -return false; +return NULL ; +/* +* Initialize sizes and feature masks, use fpu_user_cfg.* +* for user_* settings for compatibility of exiting uAPIs. +*/ + fpstate->size = fpu_guest_cfg.default_size; + fpstate->xfeatures = fpu_guest_cfg.default_features; + fpstate->user_size = fpu_user_cfg.default_size; + fpstate->user_xfeatures = fpu_user_cfg.default_features; + fpstate->xfd =0 ; -/* Leave xfd to 0 (the reset value defined by spec) */ -__fpstate_reset (fpstate,0 );fpstate_init_user (fpstate); fpstate->is_valloc = true; fpstate->is_guest = true; gfpu->fpstate = fpstate; - gfpu->xfeatures = fpu_user_cfg.default_features; - gfpu->perm = fpu_user_cfg.default_features; + gfpu->xfeatures = fpu_guest_cfg.default_features; + gfpu->perm = fpu_guest_cfg.default_features; + +return fpstate; +} + +bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu) +{ +if (!__fpu_alloc_init_guest_fpstate (gfpu)) +return false;/* * KVM sets the FP+SSE bits in the XSAVE header when copying FPU state
Signature · 1 line
2.43.0
PATCH 6/6 x86/fpu/xstate: Warn if CET supervisor state is detected in normal fpstate
In reply to Yang WeijiangCET supervisor state bit is __ONLY__ enabled for guest fpstate, i.e., never for normal kernel fpstate. The bit is set when guest FPU config is initialized.
For normal fpstate, the bit should have been removed when initializes kernel FPU config settings, WARN_ONCE() if kernel detects normal fpstate xfeatures contains CET supervisor state bit before xsaves operation.
--- arch/x86/kernel/fpu/xstate.h | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kernel/fpu/xstate.h +2 -0
@@ -189,6 +189,8 @@ static inline void os_xsave(struct fpstate *fpstate)WARN_ON_FPU (!alternatives_patched);xfd_validate_state (fpstate, mask, false); +WARN_ON_FPU (!fpstate->is_guest && (mask &XFEATURE_MASK_CET_KERNEL )); +XSTATE_XSAVE (&fpstate->regs.xsave, lmask, hmask, err);/* We should never fault when copying to a kernel buffer: */
Signature · 1 line
2.43.0
Hi, maintainers,
I recently did some tests for this series, the benchmark is here:
https://github.com/antonblanchard/will-it-scale/blob/master/tests/context_switch1.c
The purpose is to check what's the overall performance impact in thread/process context- switch when CET supervisor state bit, i.e., RFBM[12], is fixed with 0 or 1 in xsaves/xrstors.
3 cases are tested:
case 1: stock v6.10-rc1 kernel.
case 2: v6.10-rc1 kernel + this patch series so that RFBM[12] == 0 for normal thread/process. case 3: v6.10-rc1 kernel + this patch series and with patch 3(Introduce XFEATURE_MASK_KERNEL_DYNAMIC xfeature set) reverted so that RFBM[12] == 1 for normal thread process.
Run below command 10 times in each case:
./context_switch1_processes -s 20 -t 50 -n
Trim the results by removing the top and down 10% of the data in each case, and I got below numbers:
case 1:16444675.45Set as 1
case 2:16412285.61~99.80%
case 3:16405716.19~99.76%
As you can see from the results, in case 2 with optimization based on XFEATURE_MASK_KERNEL_DYNAMIC, the performance gain is ~0.2%.
So I'm not sure whether XFEATURE_MASK_KERNEL_DYNAMIC and related changes are worth or not for this series.
Could you share your thoughts?
Thanks a lot!
Quoted 4 lines · Weijiang
So I'm not sure whether
XFEATURE_MASK_KERNEL_DYNAMICand related changes are worth or not for this series.Could you share your thoughts?
First of all, I really do appreciate when folks make the effort to _try_ to draw their own conclusions before asking the maintainers to share theirs. Next time, OK? ;)
But here goes. So we've basically got three cases. Here's a fancy table:
... and the same in ASCII
Case |IA32_XSS[12] | Space | RFBM[12] | Drop%
-----+-------------+-------+----------+------
1 | 0 | None | 0 | 0.0% 2 | 1 | None | 0 | 0.2% 3 | 1 | 24B? | 1 | 0.2%
Case 1 is the baseline of course. Case 2 avoids allocating space for CET and also leans on the kernel to set RFBM[12]==0 and tell the hardware not to write CET-S state. Case 3 wastes the CET-S space in each task and also leans on the hardware init optimization to avoid writing out CET-S space on each XSAVES.
#1 is: 0 lines of code.
#2 is: 5 files changed, 90 insertions(+), 27 deletions(-) #3 is: very few lines of code, nearing zero
#2 and #3 have the same performance.
So we're down to choosing between
* $BYTES space in 'struct fpu' (on hardware supporting CET-S)
or
* ~100 loc
$BYTES is 24, right? Did I get anything wrong?
So, here's my stake in the ground: I think the 100 lines of code is probably worth it. But I also hate complicating the FPU code, so I'm also somewhat drawn to just eating the 24 bytes and moving on.
But I'm still in the "case 2" camp.
Anybody disagree?
Quoted 9 lines · Dave Hansen
So we're down to choosing between
* $BYTES space in 'struct fpu' (on hardware supporting CET-S)
or
* ~100 loc
$BYTES is 24, right? Did I get anything wrong?
Do we know what the actual memory use is? It would increases the size asked of of the allocator by 24 bytes, but what amount of memory actually gets reserved?
It is sometimes a slab allocated buffer, and sometimes a vmalloc, right? I'm not sure about slab sizes, but for vmalloc if the increase doesn't cross a page size, it will be the same size allocation in reality. Or if it is close to a page size already, it might use a whole extra 4096 bytes.
So we might be looking at a situation where some tasks get an entire extra page allocated per task, and some get no difference. And only the average is 24 bytes increase.
Hmm, I was trying to reason out something to use for tie breaking. Not sure how successful it was. The worst case of a whole page size sounds like something someone might care about?
Quoted 11 lines · Rick P
Quoted 9 lines · Dave Hansen
So we're down to choosing between
* $BYTES space in 'struct fpu' (on hardware supporting CET-S)
or
* ~100 loc
$BYTES is 24, right? Did I get anything wrong?
Quoted 7 lines
Do we know what the actual memory use is? It would increases the size asked of of the allocator by 24 bytes, but what amount of memory actually gets reserved?
It is sometimes a slab allocated buffer, and sometimes a vmalloc, right? I'm not sure about slab sizes, but for vmalloc if the increase doesn't cross a page size, it will be the same size allocation in reality. Or if it is close to a page size already, it might use a whole extra 4096 bytes.
Man, I hope I don't have this all mixed up in my head. Wouldn't be the first time. I _think_ you might be confusing thread_info and thread_struct, though. I know I've gotten them confused before.
But we get to the 'struct fpu' via:
current->thread.fpu
Where current is a 'task_struct' which is in /proc/slabinfo and 'struct thread_struct thread' and 'struct fpu' are embedded in 'task_struct', not allocated on their own:
task_struct 2958 3018 10048 3 8 ...
So my current task_struct is 10048 bytes and 3 of them fit in each 8-page slab, leaving 2624 bytes to spare.
I don't think we're too dainty about adding thing to task_struct. Are we?
Quoted 3 lines
So we might be looking at a situation where some tasks get an entire extra page allocated per task, and some get no difference. And only the average is 24 bytes increase.
I think you're right here, at least when it comes to large weirdly-sized slabs. But _so_ many things affect task_struct that I've never seen anyone sweat it too much.
Quoted 17 lines · Dave Hansen
Quoted 7 lines
Quoted 1 line
$BYTES is 24, right? Did I get anything wrong?
Do we know what the actual memory use is? It would increases the size asked of
of the allocator by 24 bytes, but what amount of memory actually gets reserved?Quoted 5 lines
It is sometimes a slab allocated buffer, and sometimes a vmalloc, right? I'm not
sure about slab sizes, but for vmalloc if the increase doesn't cross a page size, it will be the same size allocation in reality. Or if it is close to a page size already, it might use a whole extra 4096 bytes.Man, I hope I don't have this all mixed up in my head. Wouldn't be the first time. I _think_ you might be confusing
thread_infoandthread_struct, though. I know I've gotten them confused before.
Quoted 7 lines
But we get to the 'struct fpu' via:
current->thread.fpu
Where current is a '
task_struct' which is in/proc/slabinfoand 'structthread_structthread' and 'struct fpu' are embedded in 'task_struct', not allocated on their own:
I think thread_struct is always a slab, but the current->thread.fpu.fpstate pointer can be reallocated to point to a vmalloc in fpstate_realloc(), in the case of XFD features.
Quoted 7 lines
task_struct 2958 3018 10048 3 8 ...
So my current
task_structis 10048 bytes and 3 of them fit in each 8-page slab, leaving 2624 bytes to spare.I don't think we're too dainty about adding thing to task_struct. Are we?
So for you there would actually not be any extra memory usage to unconditionally add 24 bytes to the xstate. But, yes, it all could change for a number of reasons.
Quoted 10 lines
Quoted 6 lines
So we might be looking at a situation where some tasks get an entire extra page
allocated per task, and some get no difference. And only the average is 24 bytes
increase.I think you're right here, at least when it comes to large weirdly-sized slabs. But _so_ many things affect
task_structthat I've never seen anyone sweat it too much.
Makes sense. Then I can't think of any argument to move from case 2.
Quoted 6 lines · Rick P
Quoted 3 lines
Where current is a '
task_struct' which is in/proc/slabinfoand 'structthread_structthread' and 'struct fpu' are embedded in 'task_struct', not allocated on their own:I think
thread_structis always a slab, but thecurrent->thread.fpu.fpstatepointer can be reallocated to point to a vmalloc infpstate_realloc(), in the case of XFD features.
Good point. I was ignoring XFD and AMX. They're super rare and (conditionally) add another 8k. -- Well, closer to 11k since we duplicate some of the XSAVE area. -- But honestly, even if the AMX 'struct fpu' fit perfectly into 4k*3 pages and CET-S made it take 4k*4 pages, I'm not sure I'd even care. It would only affect AMX-using apps on AMX-capable hardware. So a small minority of tasks on a small minority of one x86 vendor's CPUs.
The (potential) space consumption from the inline task_struct fpu will matter a lot more across all Linux users than AMX ever will. It would affect all tasks on all CPUs that have CET-S which will hopefully be the majority of x86 CPUs running Linux some day.
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Weijiang
So I'm not sure whether
XFEATURE_MASK_KERNEL_DYNAMICand related changes are worth or not for this series.Could you share your thoughts?
First of all, I really do appreciate when folks make the effort to _try_ to draw their own conclusions before asking the maintainers to share theirs. Next time, OK? ;)
Hi, Dave,
Sorry for not doing that and thanks for making the conclusion clear!
I personally prefer applying the whole series so as to eliminates storage space issue and make guest fpu config on its own settings. But also not sure the changes are worthwhile from kernel's point of view.