]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: x86: Use a proper bitmap for tracking available/dirty registers
authorSean Christopherson <seanjc@google.com>
Thu, 9 Apr 2026 22:42:35 +0000 (15:42 -0700)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 13 May 2026 16:38:06 +0000 (12:38 -0400)
Define regs_{avail,dirty} as bitmaps instead of U32s to harden against
overflow, and to allow for dynamically sizing the bitmaps when APX comes
along, which will add 16 more GPRs (R16-R31) and thus increase the total
number of registers beyond 32.

Open code writes in the "reset" APIs, as the writes are hot paths and
bitmap_write() is complete overkill for what KVM needs.  Even better,
hardcoding writes to entry '0' in the array is a perfect excuse to assert
that the array contains exactly one entry, e.g. to effectively add guard
against defining R16-R31 in 32-bit kernels.

For all intents and purposes, no functional change intended even though
using bitmap_fill() will mean "undefined" registers are no longer marked
available and dirty (KVM should never be querying those bits).

Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Tested-by: Kai Huang <kai.huang@intel.com>
Message-ID: <20260409224236.2021562-7-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/kvm_cache_regs.h
arch/x86/kvm/x86.c

index c47eb294c0660f0691b332319a48e3833f9012e5..ef0c368676c50cbcab8fa5f47d4791cd66a372fe 100644 (file)
@@ -211,6 +211,8 @@ enum kvm_reg {
        VCPU_REG_SEGMENTS,
        VCPU_REG_EXIT_INFO_1,
        VCPU_REG_EXIT_INFO_2,
+
+       NR_VCPU_TOTAL_REGS,
 };
 
 enum {
@@ -802,8 +804,8 @@ struct kvm_vcpu_arch {
         */
        unsigned long regs[NR_VCPU_GENERAL_PURPOSE_REGS];
        unsigned long rip;
-       unsigned long regs_avail;
-       unsigned long regs_dirty;
+       DECLARE_BITMAP(regs_avail, NR_VCPU_TOTAL_REGS);
+       DECLARE_BITMAP(regs_dirty, NR_VCPU_TOTAL_REGS);
 
        unsigned long cr0;
        unsigned long cr0_guest_owned_bits;
index 171e6bc2e1696e74f0a7f84f090b9d24278b36b9..2ae492ad6412bb50f2a5a0791de265bcd64c09eb 100644 (file)
@@ -67,29 +67,29 @@ static inline bool kvm_register_is_available(struct kvm_vcpu *vcpu,
                                             enum kvm_reg reg)
 {
        kvm_assert_register_caching_allowed(vcpu);
-       return test_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
+       return test_bit(reg, vcpu->arch.regs_avail);
 }
 
 static inline bool kvm_register_is_dirty(struct kvm_vcpu *vcpu,
                                         enum kvm_reg reg)
 {
        kvm_assert_register_caching_allowed(vcpu);
-       return test_bit(reg, (unsigned long *)&vcpu->arch.regs_dirty);
+       return test_bit(reg, vcpu->arch.regs_dirty);
 }
 
 static inline void kvm_register_mark_available(struct kvm_vcpu *vcpu,
                                               enum kvm_reg reg)
 {
        kvm_assert_register_caching_allowed(vcpu);
-       __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
+       __set_bit(reg, vcpu->arch.regs_avail);
 }
 
 static inline void kvm_register_mark_dirty(struct kvm_vcpu *vcpu,
                                           enum kvm_reg reg)
 {
        kvm_assert_register_caching_allowed(vcpu);
-       __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
-       __set_bit(reg, (unsigned long *)&vcpu->arch.regs_dirty);
+       __set_bit(reg, vcpu->arch.regs_avail);
+       __set_bit(reg, vcpu->arch.regs_dirty);
 }
 
 /*
@@ -102,12 +102,15 @@ static __always_inline bool kvm_register_test_and_mark_available(struct kvm_vcpu
                                                                 enum kvm_reg reg)
 {
        kvm_assert_register_caching_allowed(vcpu);
-       return arch___test_and_set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
+       return arch___test_and_set_bit(reg, vcpu->arch.regs_avail);
 }
 
 static __always_inline void kvm_clear_available_registers(struct kvm_vcpu *vcpu,
                                                          unsigned long clear_mask)
 {
+       BUILD_BUG_ON(sizeof(clear_mask) != sizeof(vcpu->arch.regs_avail[0]));
+       BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.regs_avail) != 1);
+
        /*
         * Note the bitwise-AND!  In practice, a straight write would also work
         * as KVM initializes the mask to all ones and never clears registers
@@ -115,12 +118,13 @@ static __always_inline void kvm_clear_available_registers(struct kvm_vcpu *vcpu,
         * sanity checking as incorrectly marking an eagerly sync'd register
         * unavailable will generate a WARN due to an unexpected cache request.
         */
-       vcpu->arch.regs_avail &= ~clear_mask;
+       vcpu->arch.regs_avail[0] &= ~clear_mask;
 }
 
 static __always_inline void kvm_reset_dirty_registers(struct kvm_vcpu *vcpu)
 {
-       vcpu->arch.regs_dirty = 0;
+       BUILD_BUG_ON(ARRAY_SIZE(vcpu->arch.regs_dirty) != 1);
+       vcpu->arch.regs_dirty[0] = 0;
 }
 
 /*
index ac05cc289b566ed23d060e7d544f0b72f74666a7..b8a91feec8e1d900ca67c574f8523d7aebf60082 100644 (file)
@@ -12836,8 +12836,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
        int r;
 
        vcpu->arch.last_vmentry_cpu = -1;
-       vcpu->arch.regs_avail = ~0;
-       vcpu->arch.regs_dirty = ~0;
+       bitmap_fill(vcpu->arch.regs_avail, NR_VCPU_TOTAL_REGS);
+       bitmap_fill(vcpu->arch.regs_dirty, NR_VCPU_TOTAL_REGS);
 
        kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm);