From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH 2/9] KVM: x86: dynamic kvm_apic_map Date: Fri, 6 May 2016 22:53:58 +0200 Message-ID: <1462568045-31085-3-git-send-email-rkrcmar@redhat.com> References: <1462568045-31085-1-git-send-email-rkrcmar@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Paolo Bonzini , "Lan, Tianyu" , Igor Mammedov , Jan Kiszka , Peter Xu To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:58229 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758275AbcEFUyZ (ORCPT ); Fri, 6 May 2016 16:54:25 -0400 In-Reply-To: <1462568045-31085-1-git-send-email-rkrcmar@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: x2APIC supports up to 2^32-1 LAPICs, but most guest in coming years wil= l have slighly less VCPUs. Dynamic size saves memory at the cost of turning one constant into a variable. apic_map mutex had to be moved before allocation to avoid races with cp= u hotplug. Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 --- arch/x86/include/asm/kvm_host.h | 9 +++-- arch/x86/kvm/lapic.c | 87 ++++++++++++++++++++++-----------= -------- arch/x86/kvm/lapic.h | 2 +- 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm= _host.h index b7e394485a5f..c93c4a7877ed 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -682,9 +682,12 @@ struct kvm_arch_memory_slot { struct kvm_apic_map { struct rcu_head rcu; u8 mode; - struct kvm_lapic *phys_map[256]; - /* first index is cluster id second is cpu id in a cluster */ - struct kvm_lapic *logical_map[16][16]; + u32 size; + union { + struct kvm_lapic *xapic_flat_map[8]; + struct kvm_lapic *xapic_cluster_map[16][4]; + }; + struct kvm_lapic *phys_map[]; }; =20 /* Hyper-V emulation context */ diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 6812e61c12d4..90e07c109dfe 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -129,26 +129,32 @@ static inline int apic_enabled(struct kvm_lapic *= apic) (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \ APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER) =20 -/* The logical map is definitely wrong if we have multiple - * modes at the same time. (Physical map is always right.) - */ -static inline bool kvm_apic_logical_map_valid(struct kvm_apic_map *map= ) -{ - return !(map->mode & (map->mode - 1)); -} +static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *= map, + u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) { + switch (map->mode) { + case KVM_APIC_MODE_X2APIC: { + u32 offset =3D (dest_id >> 16) * 16; + // XXX: phys_map must be allocated as multiples of 16 + if (offset < map->size) { + *cluster =3D &map->phys_map[offset]; + *mask =3D dest_id & 0xffff; + } else + *mask =3D 0; =20 -static inline void -apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *= lid) -{ - unsigned lid_bits; - - BUILD_BUG_ON(KVM_APIC_MODE_XAPIC_CLUSTER !=3D 4); - BUILD_BUG_ON(KVM_APIC_MODE_XAPIC_FLAT !=3D 8); - BUILD_BUG_ON(KVM_APIC_MODE_X2APIC !=3D 16); - lid_bits =3D map->mode; - - *cid =3D dest_id >> lid_bits; - *lid =3D dest_id & ((1 << lid_bits) - 1); + return true; + } + case KVM_APIC_MODE_XAPIC_FLAT: + *cluster =3D map->xapic_flat_map; + *mask =3D dest_id & 0xff; + return true; + case KVM_APIC_MODE_XAPIC_CLUSTER: + *cluster =3D map->xapic_cluster_map[dest_id >> 4]; + *mask =3D dest_id & 0xf; + return true; + default: + /* Not optimized. */ + return false; + } } =20 static void recalculate_apic_map(struct kvm *kvm) @@ -156,17 +162,28 @@ static void recalculate_apic_map(struct kvm *kvm) struct kvm_apic_map *new, *old =3D NULL; struct kvm_vcpu *vcpu; int i; - - new =3D kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL); + u32 size =3D 255; =20 mutex_lock(&kvm->arch.apic_map_lock); =20 + kvm_for_each_vcpu(i, vcpu, kvm) + if (kvm_apic_present(vcpu)) + size =3D max(size, kvm_apic_id(vcpu->arch.apic)); + + size++; + size +=3D (16 - size) & 16; + new =3D kzalloc(sizeof(struct kvm_apic_map) + + sizeof(struct kvm_lapic) * size, GFP_KERNEL); + if (!new) goto out; =20 + new->size =3D size; + kvm_for_each_vcpu(i, vcpu, kvm) { struct kvm_lapic *apic =3D vcpu->arch.apic; - u16 cid, lid; + struct kvm_lapic **cluster; + u16 mask; u32 ldr, aid; =20 if (!kvm_apic_present(vcpu)) @@ -175,7 +192,7 @@ static void recalculate_apic_map(struct kvm *kvm) aid =3D kvm_apic_id(apic); ldr =3D kvm_apic_get_reg(apic, APIC_LDR); =20 - if (aid < ARRAY_SIZE(new->phys_map)) + if (aid < size) new->phys_map[aid] =3D apic; =20 if (apic_x2apic_mode(apic)) { @@ -188,13 +205,11 @@ static void recalculate_apic_map(struct kvm *kvm) new->mode |=3D KVM_APIC_MODE_XAPIC_CLUSTER; } =20 - if (!kvm_apic_logical_map_valid(new)) + if (!kvm_apic_map_get_logical_dest(new, ldr, &cluster, &mask)) continue; =20 - apic_logical_id(new, ldr, &cid, &lid); - - if (lid && cid < ARRAY_SIZE(new->logical_map)) - new->logical_map[cid][ffs(lid) - 1] =3D apic; + if (mask) + cluster[ffs(mask) - 1] =3D apic; } out: old =3D rcu_dereference_protected(kvm->arch.apic_map, @@ -703,7 +718,6 @@ static inline bool kvm_apic_map_get_dest_lapic(stru= ct kvm *kvm, struct kvm_lapic { int i, lowest; bool x2apic_ipi; - u16 cid; =20 if (irq->shorthand =3D=3D APIC_DEST_SELF) { *dst =3D &src; @@ -720,7 +734,7 @@ static inline bool kvm_apic_map_get_dest_lapic(stru= ct kvm *kvm, struct kvm_lapic return false; =20 if (irq->dest_mode =3D=3D APIC_DEST_PHYSICAL) { - if (irq->dest_id >=3D ARRAY_SIZE(map->phys_map)) { + if (irq->dest_id >=3D map->size) { *bitmap =3D 0; } else { *dst =3D &map->phys_map[irq->dest_id]; @@ -729,18 +743,11 @@ static inline bool kvm_apic_map_get_dest_lapic(st= ruct kvm *kvm, struct kvm_lapic return true; } =20 - if (!kvm_apic_logical_map_valid(map)) + *bitmap =3D 0; + if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst, + (u16 *)bitmap)) return false; =20 - apic_logical_id(map, irq->dest_id, &cid, (u16 *)bitmap); - - if (cid >=3D ARRAY_SIZE(map->logical_map)) { - *bitmap =3D 0; - return true; - } - - *dst =3D map->logical_map[cid]; - if (!kvm_lowest_prio_delivery(irq)) return true; =20 diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index f71183e502ee..d818a36ce24e 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -167,7 +167,7 @@ static inline int kvm_lapic_latched_init(struct kvm= _vcpu *vcpu) return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.a= pic->pending_events); } =20 -static inline int kvm_apic_id(struct kvm_lapic *apic) +static inline u32 kvm_apic_id(struct kvm_lapic *apic) { return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff; } --=20 2.8.2