History

KVM: Dynamically size memslot array based on number of used slots

Now that the memslot logic doesn't assume memslots are always non-NULL, dynamically size the array of memslots instead of unconditionally allocating memory for the maximum number of memslots. Note, because a to-be-deleted memslot must first be invalidated, the array size cannot be immediately reduced when deleting a memslot. However, consecutive deletions will realize the memory savings, i.e. a second deletion will trim the entry.

Sean Christopherson committed 6 years ago

Commit 36947251 parent 0577d1a2 files · +29 −4Browse files

Unified · Split

2 files
  1. include/linux/kvm_host.h +1 −1
  2. virt/kvm/kvm_main.c +28 −3
include/linux/kvm_host.h+1−1
@@ -431,11 +431,11 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) · 430 unmodified lines
431431 */
432432 struct kvm_memslots {
433433 u64 generation;
434− struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
435434 /* The mapping table from slot id to the index in memslots[]. */
436435 short id_to_index[KVM_MEM_SLOTS_NUM];
437436 atomic_t lru_slot;
438437 int used_slots;
438+ struct kvm_memory_slot memslots[];
439439 };
440440
441441 struct kvm {
virt/kvm/kvm_main.c+28−3
@@ -566,7 +566,7 @@ static struct kvm_memslots *kvm_alloc_memslots(void) · 565 unmodified lines
566566 return NULL;
567567
568568 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
569− slots->id_to_index[i] = slots->memslots[i].id = -1;
569+ slots->id_to_index[i] = -1;
570570
571571 return slots;
572572 }
@@ -1078,6 +1078,32 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, · 505 unmodified lines
10781078 return old_memslots;
10791079 }
10801080
1081+/*
1082+ * Note, at a minimum, the current number of used slots must be allocated, even
1083+ * when deleting a memslot, as we need a complete duplicate of the memslots for
1084+ * use when invalidating a memslot prior to deleting/moving the memslot.
1085+ */
1086+static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old,
1087+ enum kvm_mr_change change)
1088+{
1089+ struct kvm_memslots *slots;
1090+ size_t old_size, new_size;
1091+
1092+ old_size = sizeof(struct kvm_memslots) +
1093+ (sizeof(struct kvm_memory_slot) * old->used_slots);
1094+
1095+ if (change == KVM_MR_CREATE)
1096+ new_size = old_size + sizeof(struct kvm_memory_slot);
1097+ else
1098+ new_size = old_size;
1099+
1100+ slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT);
1101+ if (likely(slots))
1102+ memcpy(slots, old, old_size);
1103+
1104+ return slots;
1105+}
1106+
10811107 static int kvm_set_memslot(struct kvm *kvm,
10821108 const struct kvm_userspace_memory_region *mem,
10831109 struct kvm_memory_slot *old,
@@ -1088,10 +1114,9 @@ static int kvm_set_memslot(struct kvm *kvm, · 4 unmodified lines
10881114 struct kvm_memslots *slots;
10891115 int r;
10901116
1091− slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT);
1117+ slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change);
10921118 if (!slots)
10931119 return -ENOMEM;
1094− memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots));
10951120
10961121 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
10971122 /*