+static DEFINE_SPINLOCK(avic_vm_id_lock);Allocation is off by one. avic_get_next_vm_id() uses
> +
> static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
> static void svm_flush_tlb(struct kvm_vcpu *vcpu);
> static void svm_complete_interrupts(struct vcpu_svm *svm);
> @@ -1280,10 +1296,61 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu)
> return 0;
> }
>
> +static inline int avic_vm_id_init(void)
> +{
> + if (avic_vm_id_bm)
> + return 0;
> +
> + avic_vm_id_bm = kcalloc(BITS_TO_LONGS(AVIC_VM_ID_MASK),
if (id <= AVIC_VM_ID_MASK)
__set_bit(id, avic_vm_id_bm);
and id=AVIC_VM_ID_MASK is stored in the AVIC_VM_ID_MASK+1 th bit.
> +static inline int avic_get_next_vm_id(void)Why?
> +{
> + int id;
> +
> + spin_lock(&avic_vm_id_lock);
> +
> + /* AVIC VM ID is one-based. */
> + id = find_next_zero_bit(avic_vm_id_bm, 1, 1);The second argument is size, so this should always return 1. :)
> + if (id <= AVIC_VM_ID_MASK)It is not really a problem that can be handled with changing the values,
> + __set_bit(id, avic_vm_id_bm);
> + else
> + id = -EINVAL;
so a temporary error would be nicer ... ENOMEM could be confusing and
EAGAIN lead to a loop, but I still like them better.
> static int __init svm_init(void)This is certainly useless when the CPU doesn't have AVIC, so we could
> {
> + int ret;
> +
> + ret = avic_vm_id_init();
make it conditional.
I would prefer to make the bitmap allocated at module load, though:
static DECLARE_BITMAP(avic_vm_id_bm, AVIC_VM_ID_MASK + 1);
The size is 2 KiB with 24 bit AVIC_VM_ID_MASK, which is IMO much better
than having extra lines of code dealing with allocation and failures.