KVM: Use macro to iterate over vcpus.

[christian: remove unused variables on s390]

Signed-off-by: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
This commit is contained in:
Gleb Natapov 2009-06-09 15:56:29 +03:00 committed by Avi Kivity
parent 73880c80aa
commit 988a2cae6a
9 changed files with 76 additions and 74 deletions

View File

@ -337,13 +337,12 @@ static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
{ {
union ia64_lid lid; union ia64_lid lid;
int i; int i;
struct kvm_vcpu *vcpu;
for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) { kvm_for_each_vcpu(i, vcpu, kvm) {
if (kvm->vcpus[i]) { lid.val = VCPU_LID(vcpu);
lid.val = VCPU_LID(kvm->vcpus[i]); if (lid.id == id && lid.eid == eid)
if (lid.id == id && lid.eid == eid) return vcpu;
return kvm->vcpus[i];
}
} }
return NULL; return NULL;
@ -409,21 +408,21 @@ static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
struct kvm *kvm = vcpu->kvm; struct kvm *kvm = vcpu->kvm;
struct call_data call_data; struct call_data call_data;
int i; int i;
struct kvm_vcpu *vcpui;
call_data.ptc_g_data = p->u.ptc_g_data; call_data.ptc_g_data = p->u.ptc_g_data;
for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) { kvm_for_each_vcpu(i, vcpui, kvm) {
if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state == if (vcpui->arch.mp_state == KVM_MP_STATE_UNINITIALIZED ||
KVM_MP_STATE_UNINITIALIZED || vcpu == vcpui)
vcpu == kvm->vcpus[i])
continue; continue;
if (waitqueue_active(&kvm->vcpus[i]->wq)) if (waitqueue_active(&vcpui->wq))
wake_up_interruptible(&kvm->vcpus[i]->wq); wake_up_interruptible(&vcpui->wq);
if (kvm->vcpus[i]->cpu != -1) { if (vcpui->cpu != -1) {
call_data.vcpu = kvm->vcpus[i]; call_data.vcpu = vcpui;
smp_call_function_single(kvm->vcpus[i]->cpu, smp_call_function_single(vcpui->cpu,
vcpu_global_purge, &call_data, 1); vcpu_global_purge, &call_data, 1);
} else } else
printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n"); printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");

View File

@ -122,13 +122,17 @@ struct kvm *kvm_arch_create_vm(void)
static void kvmppc_free_vcpus(struct kvm *kvm) static void kvmppc_free_vcpus(struct kvm *kvm)
{ {
unsigned int i; unsigned int i;
struct kvm_vcpu *vcpu;
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm)
if (kvm->vcpus[i]) { kvm_arch_vcpu_free(vcpu);
kvm_arch_vcpu_free(kvm->vcpus[i]);
kvm->vcpus[i] = NULL; mutex_lock(&kvm->lock);
} for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
} kvm->vcpus[i] = NULL;
atomic_set(&kvm->online_vcpus, 0);
mutex_unlock(&kvm->lock);
} }
void kvm_arch_sync_events(struct kvm *kvm) void kvm_arch_sync_events(struct kvm *kvm)

View File

@ -211,13 +211,17 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
static void kvm_free_vcpus(struct kvm *kvm) static void kvm_free_vcpus(struct kvm *kvm)
{ {
unsigned int i; unsigned int i;
struct kvm_vcpu *vcpu;
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm)
if (kvm->vcpus[i]) { kvm_arch_vcpu_destroy(vcpu);
kvm_arch_vcpu_destroy(kvm->vcpus[i]);
kvm->vcpus[i] = NULL; mutex_lock(&kvm->lock);
} for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
} kvm->vcpus[i] = NULL;
atomic_set(&kvm->online_vcpus, 0);
mutex_unlock(&kvm->lock);
} }
void kvm_arch_sync_events(struct kvm *kvm) void kvm_arch_sync_events(struct kvm *kvm)
@ -314,8 +318,6 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
BUG_ON(!kvm->arch.sca); BUG_ON(!kvm->arch.sca);
if (!kvm->arch.sca->cpu[id].sda) if (!kvm->arch.sca->cpu[id].sda)
kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block; kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
else
BUG_ON(!kvm->vcpus[id]); /* vcpu does already exist */
vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32); vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32);
vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca; vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
@ -683,6 +685,7 @@ int kvm_arch_set_memory_region(struct kvm *kvm,
int user_alloc) int user_alloc)
{ {
int i; int i;
struct kvm_vcpu *vcpu;
/* A few sanity checks. We can have exactly one memory slot which has /* A few sanity checks. We can have exactly one memory slot which has
to start at guest virtual zero and which has to be located at a to start at guest virtual zero and which has to be located at a
@ -707,14 +710,10 @@ int kvm_arch_set_memory_region(struct kvm *kvm,
return -EINVAL; return -EINVAL;
/* request update of sie control block for all available vcpus */ /* request update of sie control block for all available vcpus */
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm) {
if (kvm->vcpus[i]) { if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
if (test_and_set_bit(KVM_REQ_MMU_RELOAD, continue;
&kvm->vcpus[i]->requests)) kvm_s390_inject_sigp_stop(vcpu, ACTION_RELOADVCPU_ON_STOP);
continue;
kvm_s390_inject_sigp_stop(kvm->vcpus[i],
ACTION_RELOADVCPU_ON_STOP);
}
} }
return 0; return 0;

View File

@ -669,11 +669,8 @@ static void __inject_pit_timer_intr(struct kvm *kvm)
* VCPU0, and only if its LVT0 is in EXTINT mode. * VCPU0, and only if its LVT0 is in EXTINT mode.
*/ */
if (kvm->arch.vapics_in_nmi_mode > 0) if (kvm->arch.vapics_in_nmi_mode > 0)
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm)
vcpu = kvm->vcpus[i]; kvm_apic_nmi_wd_deliver(vcpu);
if (vcpu)
kvm_apic_nmi_wd_deliver(vcpu);
}
} }
void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu) void kvm_inject_pit_timer_irqs(struct kvm_vcpu *vcpu)

View File

@ -1347,10 +1347,10 @@ static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm) static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
{ {
int i; int i;
struct kvm_vcpu *vcpu;
for (i = 0; i < KVM_MAX_VCPUS; ++i) kvm_for_each_vcpu(i, vcpu, kvm)
if (kvm->vcpus[i]) vcpu->arch.last_pte_updated = NULL;
kvm->vcpus[i]->arch.last_pte_updated = NULL;
} }
static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp) static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)

View File

@ -2946,10 +2946,7 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va
spin_lock(&kvm_lock); spin_lock(&kvm_lock);
list_for_each_entry(kvm, &vm_list, vm_list) { list_for_each_entry(kvm, &vm_list, vm_list) {
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm) {
vcpu = kvm->vcpus[i];
if (!vcpu)
continue;
if (vcpu->cpu != freq->cpu) if (vcpu->cpu != freq->cpu)
continue; continue;
if (!kvm_request_guest_time_update(vcpu)) if (!kvm_request_guest_time_update(vcpu))
@ -4678,20 +4675,22 @@ static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
static void kvm_free_vcpus(struct kvm *kvm) static void kvm_free_vcpus(struct kvm *kvm)
{ {
unsigned int i; unsigned int i;
struct kvm_vcpu *vcpu;
/* /*
* Unpin any mmu pages first. * Unpin any mmu pages first.
*/ */
for (i = 0; i < KVM_MAX_VCPUS; ++i) kvm_for_each_vcpu(i, vcpu, kvm)
if (kvm->vcpus[i]) kvm_unload_vcpu_mmu(vcpu);
kvm_unload_vcpu_mmu(kvm->vcpus[i]); kvm_for_each_vcpu(i, vcpu, kvm)
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_arch_vcpu_free(vcpu);
if (kvm->vcpus[i]) {
kvm_arch_vcpu_free(kvm->vcpus[i]);
kvm->vcpus[i] = NULL;
}
}
mutex_lock(&kvm->lock);
for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
kvm->vcpus[i] = NULL;
atomic_set(&kvm->online_vcpus, 0);
mutex_unlock(&kvm->lock);
} }
void kvm_arch_sync_events(struct kvm *kvm) void kvm_arch_sync_events(struct kvm *kvm)

View File

@ -179,6 +179,17 @@ struct kvm {
#define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt) #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
#define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt) #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
{
smp_rmb();
return kvm->vcpus[i];
}
#define kvm_for_each_vcpu(idx, vcpup, kvm) \
for (idx = 0, vcpup = kvm_get_vcpu(kvm, idx); \
idx < atomic_read(&kvm->online_vcpus) && vcpup; \
vcpup = kvm_get_vcpu(kvm, ++idx))
int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id); int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
void kvm_vcpu_uninit(struct kvm_vcpu *vcpu); void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);

View File

@ -68,10 +68,8 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
kvm_is_dm_lowest_prio(irq)) kvm_is_dm_lowest_prio(irq))
printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n"); printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
for (i = 0; i < KVM_MAX_VCPUS; i++) { kvm_for_each_vcpu(i, vcpu, kvm) {
vcpu = kvm->vcpus[i]; if (!kvm_apic_present(vcpu))
if (!vcpu || !kvm_apic_present(vcpu))
continue; continue;
if (!kvm_apic_match_dest(vcpu, src, irq->shorthand, if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,

View File

@ -738,10 +738,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
me = get_cpu(); me = get_cpu();
spin_lock(&kvm->requests_lock); spin_lock(&kvm->requests_lock);
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm) {
vcpu = kvm->vcpus[i];
if (!vcpu)
continue;
if (test_and_set_bit(req, &vcpu->requests)) if (test_and_set_bit(req, &vcpu->requests))
continue; continue;
cpu = vcpu->cpu; cpu = vcpu->cpu;
@ -1718,7 +1715,7 @@ static int create_vcpu_fd(struct kvm_vcpu *vcpu)
static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
{ {
int r; int r;
struct kvm_vcpu *vcpu; struct kvm_vcpu *vcpu, *v;
vcpu = kvm_arch_vcpu_create(kvm, id); vcpu = kvm_arch_vcpu_create(kvm, id);
if (IS_ERR(vcpu)) if (IS_ERR(vcpu))
@ -1736,8 +1733,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
goto vcpu_destroy; goto vcpu_destroy;
} }
for (r = 0; r < atomic_read(&kvm->online_vcpus); r++) kvm_for_each_vcpu(r, v, kvm)
if (kvm->vcpus[r]->vcpu_id == id) { if (v->vcpu_id == id) {
r = -EEXIST; r = -EEXIST;
goto vcpu_destroy; goto vcpu_destroy;
} }
@ -2526,11 +2523,9 @@ static int vcpu_stat_get(void *_offset, u64 *val)
*val = 0; *val = 0;
spin_lock(&kvm_lock); spin_lock(&kvm_lock);
list_for_each_entry(kvm, &vm_list, vm_list) list_for_each_entry(kvm, &vm_list, vm_list)
for (i = 0; i < KVM_MAX_VCPUS; ++i) { kvm_for_each_vcpu(i, vcpu, kvm)
vcpu = kvm->vcpus[i]; *val += *(u32 *)((void *)vcpu + offset);
if (vcpu)
*val += *(u32 *)((void *)vcpu + offset);
}
spin_unlock(&kvm_lock); spin_unlock(&kvm_lock);
return 0; return 0;
} }