diff mbox

[v3,04/14] KVM: x86: dynamic kvm_apic_map

Message ID 20160712200930.32143-5-rkrcmar@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Radim Krčmář July 12, 2016, 8:09 p.m. UTC
x2APIC supports up to 2^32-1 LAPICs, but most guest in coming years will
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 cpu
hotplug.

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
 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(-)

Comments

Paolo Bonzini July 13, 2016, 8:29 a.m. UTC | #1
On 12/07/2016 22:09, Radim Krčmář wrote:
> x2APIC supports up to 2^32-1 LAPICs, but most guest in coming years will
> have slighly less VCPUs.

It's "fewer", not "less" (that's important to make the joke less uncanny
for native speakers :)).  Less goes with uncountable nouns such as milk.

Paolo

> Dynamic size saves memory at the cost of
> turning one constant into a variable.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář July 13, 2016, 2:37 p.m. UTC | #2
2016-07-13 10:29+0200, Paolo Bonzini:
> On 12/07/2016 22:09, Radim Krčmář wrote:
>> x2APIC supports up to 2^32-1 LAPICs, but most guest in coming years will
>> have slighly less VCPUs.
> 
> It's "fewer", not "less" (that's important to make the joke less uncanny
> for native speakers :)).  Less goes with uncountable nouns such as milk.

Thanks.  My syntax should be enough to creep native speakers. :)
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wanpeng Li Aug. 2, 2016, 11:39 a.m. UTC | #3
2016-07-13 4:09 GMT+08:00 Radim Krčmář <rkrcmar@redhat.com>:
[...]
> 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(struct kvm_apic_map *map,
>         switch (map->mode) {
>         case KVM_APIC_MODE_X2APIC: {
>                 u32 offset = (dest_id >> 16) * 16;
> -               u32 max_apic_id = ARRAY_SIZE(map->phys_map) - 1;
> +               u32 max_apic_id = map->max_apic_id;
>
>                 if (offset <= max_apic_id) {
>                         u8 cluster_size = 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 = NULL;
>         struct kvm_vcpu *vcpu;
>         int i;
> -
> -       new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
> +       u32 max_id = 255;

If this should be max_id = KVM_MAX_VCPU_ID? I have a patch on hand to
fix it, but I didn't know whether it is your desired behavior or not.

Regards,
Wanpeng Li
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář Aug. 2, 2016, 12:22 p.m. UTC | #4
2016-08-02 19:39+0800, Wanpeng Li:
> 2016-07-13 4:09 GMT+08:00 Radim Krčmář <rkrcmar@redhat.com>:
> [...]
>> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
>> @@ -152,14 +152,22 @@ static void recalculate_apic_map(struct kvm *kvm)
>>         struct kvm_apic_map *new, *old = NULL;
>>         struct kvm_vcpu *vcpu;
>>         int i;
>> -
>> -       new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
>> +       u32 max_id = 255;
> 
> If this should be max_id = KVM_MAX_VCPU_ID? I have a patch on hand to
> fix it, but I didn't know whether it is your desired behavior or not.

No, we could just have static array then.  KVM_MAX_VCPU_ID is expected
to be raised to INT_MAX and eventually UINT_MAX, so it would not be
practical.  The dynamic array is there to avoid wasting space in the
common case, where VMs have only low APIC IDs.

Inintial patches had 255 to minimize a chance of regressions as the
static array was that big, but starting with max_id = 0 is our goal and
should be ok even without other changes.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Wanpeng Li Aug. 2, 2016, 1:15 p.m. UTC | #5
2016-08-02 20:22 GMT+08:00 Radim Krčmář <rkrcmar@redhat.com>:
> 2016-08-02 19:39+0800, Wanpeng Li:
>> 2016-07-13 4:09 GMT+08:00 Radim Krčmář <rkrcmar@redhat.com>:
>> [...]
>>> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
>>> @@ -152,14 +152,22 @@ static void recalculate_apic_map(struct kvm *kvm)
>>>         struct kvm_apic_map *new, *old = NULL;
>>>         struct kvm_vcpu *vcpu;
>>>         int i;
>>> -
>>> -       new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
>>> +       u32 max_id = 255;
>>
>> If this should be max_id = KVM_MAX_VCPU_ID? I have a patch on hand to
>> fix it, but I didn't know whether it is your desired behavior or not.
>
> No, we could just have static array then.  KVM_MAX_VCPU_ID is expected
> to be raised to INT_MAX and eventually UINT_MAX, so it would not be
> practical.  The dynamic array is there to avoid wasting space in the
> common case, where VMs have only low APIC IDs.
>
> Inintial patches had 255 to minimize a chance of regressions as the
> static array was that big, but starting with max_id = 0 is our goal and
> should be ok even without other changes.

I see, thanks. :)

Regards,
Wanpeng Li
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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[];
 };
 
 /* 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(struct kvm_apic_map *map,
 	switch (map->mode) {
 	case KVM_APIC_MODE_X2APIC: {
 		u32 offset = (dest_id >> 16) * 16;
-		u32 max_apic_id = ARRAY_SIZE(map->phys_map) - 1;
+		u32 max_apic_id = map->max_apic_id;
 
 		if (offset <= max_apic_id) {
 			u8 cluster_size = 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 = NULL;
 	struct kvm_vcpu *vcpu;
 	int i;
-
-	new = kzalloc(sizeof(struct kvm_apic_map), GFP_KERNEL);
+	u32 max_id = 255;
 
 	mutex_lock(&kvm->arch.apic_map_lock);
 
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		if (kvm_apic_present(vcpu))
+			max_id = max(max_id, kvm_apic_id(vcpu->arch.apic));
+
+	new = kzalloc(sizeof(struct kvm_apic_map) +
+	              sizeof(struct kvm_lapic *) * (max_id + 1), GFP_KERNEL);
+
 	if (!new)
 		goto out;
 
+	new->max_apic_id = max_id;
+
 	kvm_for_each_vcpu(i, vcpu, kvm) {
 		struct kvm_lapic *apic = vcpu->arch.apic;
 		struct kvm_lapic **cluster;
@@ -172,7 +180,7 @@  static void recalculate_apic_map(struct kvm *kvm)
 		aid = kvm_apic_id(apic);
 		ldr = kvm_lapic_get_reg(apic, APIC_LDR);
 
-		if (aid < ARRAY_SIZE(new->phys_map))
+		if (aid <= new->max_apic_id)
 			new->phys_map[aid] = apic;
 
 		if (apic_x2apic_mode(apic)) {
@@ -710,7 +718,7 @@  static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
 		return false;
 
 	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
-		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {
+		if (irq->dest_id > map->max_apic_id) {
 			*bitmap = 0;
 		} else {
 			*dst = &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.apic->pending_events);
 }
 
-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;
 }