Skip to content

Commit 1e08ec4

Browse files
Gleb Natapovavikivity
Gleb Natapov
authored andcommitted
KVM: optimize apic interrupt delivery
Most interrupt are delivered to only one vcpu. Use pre-build tables to find interrupt destination instead of looping through all vcpus. In case of logical mode loop only through vcpus in a logical cluster irq is sent to. Signed-off-by: Gleb Natapov <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Avi Kivity <[email protected]>
1 parent 1d86b5c commit 1e08ec4

File tree

5 files changed

+199
-13
lines changed

5 files changed

+199
-13
lines changed

arch/x86/include/asm/kvm_host.h

+12
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,16 @@ struct kvm_arch_memory_slot {
525525
struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
526526
};
527527

528+
struct kvm_apic_map {
529+
struct rcu_head rcu;
530+
u8 ldr_bits;
531+
/* fields bellow are used to decode ldr values in different modes */
532+
u32 cid_shift, cid_mask, lid_mask;
533+
struct kvm_lapic *phys_map[256];
534+
/* first index is cluster id second is cpu id in a cluster */
535+
struct kvm_lapic *logical_map[16][16];
536+
};
537+
528538
struct kvm_arch {
529539
unsigned int n_used_mmu_pages;
530540
unsigned int n_requested_mmu_pages;
@@ -542,6 +552,8 @@ struct kvm_arch {
542552
struct kvm_ioapic *vioapic;
543553
struct kvm_pit *vpit;
544554
int vapics_in_nmi_mode;
555+
struct mutex apic_map_lock;
556+
struct kvm_apic_map *apic_map;
545557

546558
unsigned int tss_addr;
547559
struct page *apic_access_page;

arch/x86/kvm/lapic.c

+176-12
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,110 @@ static inline int apic_enabled(struct kvm_lapic *apic)
140140
(LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
141141
APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
142142

143+
static inline int apic_x2apic_mode(struct kvm_lapic *apic)
144+
{
145+
return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
146+
}
147+
143148
static inline int kvm_apic_id(struct kvm_lapic *apic)
144149
{
145150
return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
146151
}
147152

153+
static inline u16 apic_cluster_id(struct kvm_apic_map *map, u32 ldr)
154+
{
155+
u16 cid;
156+
ldr >>= 32 - map->ldr_bits;
157+
cid = (ldr >> map->cid_shift) & map->cid_mask;
158+
159+
BUG_ON(cid >= ARRAY_SIZE(map->logical_map));
160+
161+
return cid;
162+
}
163+
164+
static inline u16 apic_logical_id(struct kvm_apic_map *map, u32 ldr)
165+
{
166+
ldr >>= (32 - map->ldr_bits);
167+
return ldr & map->lid_mask;
168+
}
169+
170+
static void recalculate_apic_map(struct kvm *kvm)
171+
{
172+
struct kvm_apic_map *new, *old = NULL;
173+
struct kvm_vcpu *vcpu;
174+
int i;
175+
176+
new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
177+
178+
mutex_lock(&kvm->arch.apic_map_lock);
179+
180+
if (!new)
181+
goto out;
182+
183+
new->ldr_bits = 8;
184+
/* flat mode is default */
185+
new->cid_shift = 8;
186+
new->cid_mask = 0;
187+
new->lid_mask = 0xff;
188+
189+
kvm_for_each_vcpu(i, vcpu, kvm) {
190+
struct kvm_lapic *apic = vcpu->arch.apic;
191+
u16 cid, lid;
192+
u32 ldr;
193+
194+
if (!kvm_apic_present(vcpu))
195+
continue;
196+
197+
/*
198+
* All APICs have to be configured in the same mode by an OS.
199+
* We take advatage of this while building logical id loockup
200+
* table. After reset APICs are in xapic/flat mode, so if we
201+
* find apic with different setting we assume this is the mode
202+
* OS wants all apics to be in; build lookup table accordingly.
203+
*/
204+
if (apic_x2apic_mode(apic)) {
205+
new->ldr_bits = 32;
206+
new->cid_shift = 16;
207+
new->cid_mask = new->lid_mask = 0xffff;
208+
} else if (kvm_apic_sw_enabled(apic) &&
209+
!new->cid_mask /* flat mode */ &&
210+
kvm_apic_get_reg(apic, APIC_DFR) == APIC_DFR_CLUSTER) {
211+
new->cid_shift = 4;
212+
new->cid_mask = 0xf;
213+
new->lid_mask = 0xf;
214+
}
215+
216+
new->phys_map[kvm_apic_id(apic)] = apic;
217+
218+
ldr = kvm_apic_get_reg(apic, APIC_LDR);
219+
cid = apic_cluster_id(new, ldr);
220+
lid = apic_logical_id(new, ldr);
221+
222+
if (lid)
223+
new->logical_map[cid][ffs(lid) - 1] = apic;
224+
}
225+
out:
226+
old = rcu_dereference_protected(kvm->arch.apic_map,
227+
lockdep_is_held(&kvm->arch.apic_map_lock));
228+
rcu_assign_pointer(kvm->arch.apic_map, new);
229+
mutex_unlock(&kvm->arch.apic_map_lock);
230+
231+
if (old)
232+
kfree_rcu(old, rcu);
233+
}
234+
235+
static inline void kvm_apic_set_id(struct kvm_lapic *apic, u8 id)
236+
{
237+
apic_set_reg(apic, APIC_ID, id << 24);
238+
recalculate_apic_map(apic->vcpu->kvm);
239+
}
240+
241+
static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
242+
{
243+
apic_set_reg(apic, APIC_LDR, id);
244+
recalculate_apic_map(apic->vcpu->kvm);
245+
}
246+
148247
static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
149248
{
150249
return !(kvm_apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
@@ -194,11 +293,6 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu)
194293
apic_set_reg(apic, APIC_LVR, v);
195294
}
196295

197-
static inline int apic_x2apic_mode(struct kvm_lapic *apic)
198-
{
199-
return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
200-
}
201-
202296
static const unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
203297
LVT_MASK , /* part LVTT mask, timer mode mask added at runtime */
204298
LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
@@ -483,6 +577,72 @@ int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
483577
return result;
484578
}
485579

580+
bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
581+
struct kvm_lapic_irq *irq, int *r)
582+
{
583+
struct kvm_apic_map *map;
584+
unsigned long bitmap = 1;
585+
struct kvm_lapic **dst;
586+
int i;
587+
bool ret = false;
588+
589+
*r = -1;
590+
591+
if (irq->shorthand == APIC_DEST_SELF) {
592+
*r = kvm_apic_set_irq(src->vcpu, irq);
593+
return true;
594+
}
595+
596+
if (irq->shorthand)
597+
return false;
598+
599+
rcu_read_lock();
600+
map = rcu_dereference(kvm->arch.apic_map);
601+
602+
if (!map)
603+
goto out;
604+
605+
if (irq->dest_mode == 0) { /* physical mode */
606+
if (irq->delivery_mode == APIC_DM_LOWEST ||
607+
irq->dest_id == 0xff)
608+
goto out;
609+
dst = &map->phys_map[irq->dest_id & 0xff];
610+
} else {
611+
u32 mda = irq->dest_id << (32 - map->ldr_bits);
612+
613+
dst = map->logical_map[apic_cluster_id(map, mda)];
614+
615+
bitmap = apic_logical_id(map, mda);
616+
617+
if (irq->delivery_mode == APIC_DM_LOWEST) {
618+
int l = -1;
619+
for_each_set_bit(i, &bitmap, 16) {
620+
if (!dst[i])
621+
continue;
622+
if (l < 0)
623+
l = i;
624+
else if (kvm_apic_compare_prio(dst[i]->vcpu, dst[l]->vcpu) < 0)
625+
l = i;
626+
}
627+
628+
bitmap = (l >= 0) ? 1 << l : 0;
629+
}
630+
}
631+
632+
for_each_set_bit(i, &bitmap, 16) {
633+
if (!dst[i])
634+
continue;
635+
if (*r < 0)
636+
*r = 0;
637+
*r += kvm_apic_set_irq(dst[i]->vcpu, irq);
638+
}
639+
640+
ret = true;
641+
out:
642+
rcu_read_unlock();
643+
return ret;
644+
}
645+
486646
/*
487647
* Add a pending IRQ into lapic.
488648
* Return 1 if successfully added and 0 if discarded.
@@ -886,7 +1046,7 @@ static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
8861046
switch (reg) {
8871047
case APIC_ID: /* Local APIC ID */
8881048
if (!apic_x2apic_mode(apic))
889-
apic_set_reg(apic, APIC_ID, val);
1049+
kvm_apic_set_id(apic, val >> 24);
8901050
else
8911051
ret = 1;
8921052
break;
@@ -902,15 +1062,16 @@ static int apic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
9021062

9031063
case APIC_LDR:
9041064
if (!apic_x2apic_mode(apic))
905-
apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
1065+
kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
9061066
else
9071067
ret = 1;
9081068
break;
9091069

9101070
case APIC_DFR:
911-
if (!apic_x2apic_mode(apic))
1071+
if (!apic_x2apic_mode(apic)) {
9121072
apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
913-
else
1073+
recalculate_apic_map(apic->vcpu->kvm);
1074+
} else
9141075
ret = 1;
9151076
break;
9161077

@@ -1141,6 +1302,7 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
11411302
static_key_slow_dec_deferred(&apic_hw_disabled);
11421303
else
11431304
static_key_slow_inc(&apic_hw_disabled.key);
1305+
recalculate_apic_map(vcpu->kvm);
11441306
}
11451307

11461308
if (!kvm_vcpu_is_bsp(apic->vcpu))
@@ -1150,7 +1312,7 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
11501312
if (apic_x2apic_mode(apic)) {
11511313
u32 id = kvm_apic_id(apic);
11521314
u32 ldr = ((id & ~0xf) << 16) | (1 << (id & 0xf));
1153-
apic_set_reg(apic, APIC_LDR, ldr);
1315+
kvm_apic_set_ldr(apic, ldr);
11541316
}
11551317
apic->base_address = apic->vcpu->arch.apic_base &
11561318
MSR_IA32_APICBASE_BASE;
@@ -1175,7 +1337,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu)
11751337
/* Stop the timer in case it's a reset to an active apic */
11761338
hrtimer_cancel(&apic->lapic_timer.timer);
11771339

1178-
apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
1340+
kvm_apic_set_id(apic, vcpu->vcpu_id);
11791341
kvm_apic_set_version(apic->vcpu);
11801342

11811343
for (i = 0; i < APIC_LVT_NUM; i++)
@@ -1186,7 +1348,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu)
11861348
apic_set_reg(apic, APIC_DFR, 0xffffffffU);
11871349
apic_set_spiv(apic, 0xff);
11881350
apic_set_reg(apic, APIC_TASKPRI, 0);
1189-
apic_set_reg(apic, APIC_LDR, 0);
1351+
kvm_apic_set_ldr(apic, 0);
11901352
apic_set_reg(apic, APIC_ESR, 0);
11911353
apic_set_reg(apic, APIC_ICR, 0);
11921354
apic_set_reg(apic, APIC_ICR2, 0);
@@ -1404,6 +1566,8 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
14041566
/* set SPIV separately to get count of SW disabled APICs right */
14051567
apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
14061568
memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1569+
/* call kvm_apic_set_id() to put apic into apic_map */
1570+
kvm_apic_set_id(apic, kvm_apic_id(apic));
14071571
kvm_apic_set_version(vcpu);
14081572

14091573
apic_update_ppr(apic);

arch/x86/kvm/lapic.h

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
5252
int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
5353
int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
5454

55+
bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
56+
struct kvm_lapic_irq *irq, int *r);
57+
5558
u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
5659
void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data);
5760
void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,

arch/x86/kvm/x86.c

+2
Original file line numberDiff line numberDiff line change
@@ -6270,6 +6270,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
62706270
set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
62716271

62726272
raw_spin_lock_init(&kvm->arch.tsc_write_lock);
6273+
mutex_init(&kvm->arch.apic_map_lock);
62736274

62746275
return 0;
62756276
}
@@ -6322,6 +6323,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
63226323
put_page(kvm->arch.apic_access_page);
63236324
if (kvm->arch.ept_identity_pagetable)
63246325
put_page(kvm->arch.ept_identity_pagetable);
6326+
kfree(rcu_dereference_check(kvm->arch.apic_map, 1));
63256327
}
63266328

63276329
void kvm_arch_free_memslot(struct kvm_memory_slot *free,

virt/kvm/irq_comm.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
6868
struct kvm_vcpu *vcpu, *lowest = NULL;
6969

7070
if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
71-
kvm_is_dm_lowest_prio(irq))
71+
kvm_is_dm_lowest_prio(irq)) {
7272
printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
73+
irq->delivery_mode = APIC_DM_FIXED;
74+
}
75+
76+
if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r))
77+
return r;
7378

7479
kvm_for_each_vcpu(i, vcpu, kvm) {
7580
if (!kvm_apic_present(vcpu))

0 commit comments

Comments
 (0)