From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH v3 04/14] KVM: x86: dynamic kvm_apic_map Date: Tue, 12 Jul 2016 22:09:20 +0200 Message-ID: <20160712200930.32143-5-rkrcmar@redhat.com> References: <20160712200930.32143-1-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 , Yang Zhang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Return-path: In-Reply-To: <20160712200930.32143-1-rkrcmar@redhat.com> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org 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 | 3 ++- arch/x86/kvm/lapic.c | 18 +++++++++++++----- arch/x86/kvm/lapic.h | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm= _host.h index 3194b19b9c7b..643e3dffcd85 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -682,11 +682,12 @@ struct kvm_arch_memory_slot { struct kvm_apic_map { struct rcu_head rcu; u8 mode; - struct kvm_lapic *phys_map[256]; + u32 max_apic_id; 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 9880d03f533d..224fc1c5fcc6 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -120,7 +120,7 @@ static inline bool kvm_apic_map_get_logical_dest(st= ruct kvm_apic_map *map, switch (map->mode) { case KVM_APIC_MODE_X2APIC: { u32 offset =3D (dest_id >> 16) * 16; - u32 max_apic_id =3D ARRAY_SIZE(map->phys_map) - 1; + u32 max_apic_id =3D map->max_apic_id; =20 if (offset <=3D max_apic_id) { u8 cluster_size =3D min(max_apic_id - offset + 1, 16U); @@ -152,14 +152,22 @@ 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 max_id =3D 255; =20 mutex_lock(&kvm->arch.apic_map_lock); =20 + kvm_for_each_vcpu(i, vcpu, kvm) + if (kvm_apic_present(vcpu)) + max_id =3D max(max_id, kvm_apic_id(vcpu->arch.apic)); + + new =3D kzalloc(sizeof(struct kvm_apic_map) + + sizeof(struct kvm_lapic *) * (max_id + 1), GFP_KERNEL); + if (!new) goto out; =20 + new->max_apic_id =3D max_id; + kvm_for_each_vcpu(i, vcpu, kvm) { struct kvm_lapic *apic =3D vcpu->arch.apic; struct kvm_lapic **cluster; @@ -172,7 +180,7 @@ static void recalculate_apic_map(struct kvm *kvm) aid =3D kvm_apic_id(apic); ldr =3D kvm_lapic_get_reg(apic, APIC_LDR); =20 - if (aid < ARRAY_SIZE(new->phys_map)) + if (aid <=3D new->max_apic_id) new->phys_map[aid] =3D apic; =20 if (apic_x2apic_mode(apic)) { @@ -710,7 +718,7 @@ static inline bool kvm_apic_map_get_dest_lapic(stru= ct kvm *kvm, 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 > map->max_apic_id) { *bitmap =3D 0; } else { *dst =3D &map->phys_map[irq->dest_id]; diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 336ba51bb16e..8d811139d2b3 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -200,7 +200,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_lapic_get_reg(apic, APIC_ID) >> 24) & 0xff; } --=20 2.9.0