Skip to content

Commit c281794

Browse files
sean-jcbonzini
authored andcommitted
KVM: SVM: WARN if GATag generation drops VM or vCPU ID information
WARN if generating a GATag given a VM ID and vCPU ID doesn't yield the same IDs when pulling the IDs back out of the tag. Don't bother adding error handling to callers, this is very much a paranoid sanity check as KVM fully controls the VM ID and is supposed to reject too-big vCPU IDs. Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Suravee Suthikulpanit <[email protected]> Tested-by: Suravee Suthikulpanit <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 5999715 commit c281794

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

arch/x86/kvm/svm/avic.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,21 @@
4343
#define AVIC_VM_ID_SHIFT HWEIGHT32(AVIC_PHYSICAL_MAX_INDEX_MASK)
4444
#define AVIC_VM_ID_MASK (GENMASK(31, AVIC_VM_ID_SHIFT) >> AVIC_VM_ID_SHIFT)
4545

46-
#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VM_ID_SHIFT) | \
47-
(y & AVIC_VCPU_ID_MASK))
4846
#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VM_ID_SHIFT) & AVIC_VM_ID_MASK)
4947
#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
5048

51-
static_assert(AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) == -1u);
49+
#define __AVIC_GATAG(vm_id, vcpu_id) ((((vm_id) & AVIC_VM_ID_MASK) << AVIC_VM_ID_SHIFT) | \
50+
((vcpu_id) & AVIC_VCPU_ID_MASK))
51+
#define AVIC_GATAG(vm_id, vcpu_id) \
52+
({ \
53+
u32 ga_tag = __AVIC_GATAG(vm_id, vcpu_id); \
54+
\
55+
WARN_ON_ONCE(AVIC_GATAG_TO_VCPUID(ga_tag) != (vcpu_id)); \
56+
WARN_ON_ONCE(AVIC_GATAG_TO_VMID(ga_tag) != (vm_id)); \
57+
ga_tag; \
58+
})
59+
60+
static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) == -1u);
5261

5362
static bool force_avic;
5463
module_param_unsafe(force_avic, bool, 0444);

0 commit comments

Comments
 (0)