Skip to content

Commit 6be3ae4

Browse files
suomilewissean-jc
authored andcommitted
KVM: x86: Add a helper to handle filtering of unpermitted XCR0 features
Add a helper, kvm_get_filtered_xcr0(), to dedup code that needs to account for XCR0 features that require explicit opt-in on a per-process basis. In addition to documenting when KVM should/shouldn't consult xstate_get_guest_group_perm(), the helper will also allow sanitizing the filtered XCR0 to avoid enumerating architecturally illegal XCR0 values, e.g. XTILE_CFG without XTILE_DATA. No functional changes intended. Signed-off-by: Aaron Lewis <aaronlewis@google.com> Reviewed-by: Mingwei Zhang <mizhang@google.com> [sean: rename helper, move to x86.h, massage changelog] Reviewed-by: Aaron Lewis <aaronlewis@google.com> Tested-by: Aaron Lewis <aaronlewis@google.com> Link: https://lore.kernel.org/r/20230405004520.421768-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7712145 commit 6be3ae4

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

arch/x86/kvm/cpuid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
10021002
entry->eax = entry->ebx = entry->ecx = 0;
10031003
break;
10041004
case 0xd: {
1005-
u64 permitted_xcr0 = kvm_caps.supported_xcr0 & xstate_get_guest_group_perm();
1005+
u64 permitted_xcr0 = kvm_get_filtered_xcr0();
10061006
u64 permitted_xss = kvm_caps.supported_xss;
10071007

10081008
entry->eax &= permitted_xcr0;

arch/x86/kvm/x86.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,9 +4531,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
45314531
r = 0;
45324532
break;
45334533
case KVM_CAP_XSAVE2: {
4534-
u64 guest_perm = xstate_get_guest_group_perm();
4535-
4536-
r = xstate_required_size(kvm_caps.supported_xcr0 & guest_perm, false);
4534+
r = xstate_required_size(kvm_get_filtered_xcr0(), false);
45374535
if (r < sizeof(struct kvm_xsave))
45384536
r = sizeof(struct kvm_xsave);
45394537
break;

arch/x86/kvm/x86.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ extern struct kvm_caps kvm_caps;
315315

316316
extern bool enable_pmu;
317317

318+
/*
319+
* Get a filtered version of KVM's supported XCR0 that strips out dynamic
320+
* features for which the current process doesn't (yet) have permission to use.
321+
* This is intended to be used only when enumerating support to userspace,
322+
* e.g. in KVM_GET_SUPPORTED_CPUID and KVM_CAP_XSAVE2, it does NOT need to be
323+
* used to check/restrict guest behavior as KVM rejects KVM_SET_CPUID{2} if
324+
* userspace attempts to enable unpermitted features.
325+
*/
326+
static inline u64 kvm_get_filtered_xcr0(void)
327+
{
328+
return kvm_caps.supported_xcr0 & xstate_get_guest_group_perm();
329+
}
330+
318331
static inline bool kvm_mpx_supported(void)
319332
{
320333
return (kvm_caps.supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))

0 commit comments

Comments
 (0)