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.
Unified · Split
| @@ -431,11 +431,11 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) · 430 unmodified lines | |||
| 431 | */ | 431 | */ |
| 432 | struct kvm_memslots { | 432 | struct kvm_memslots { |
| 433 | u64 generation; | 433 | u64 generation; |
| 434 | struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM]; | ||
| 435 | /* The mapping table from slot id to the index in memslots[]. */ | 434 | /* The mapping table from slot id to the index in memslots[]. */ |
| 436 | short id_to_index[KVM_MEM_SLOTS_NUM]; | 435 | short id_to_index[KVM_MEM_SLOTS_NUM]; |
| 437 | atomic_t lru_slot; | 436 | atomic_t lru_slot; |
| 438 | int used_slots; | 437 | int used_slots; |
| 438 | struct kvm_memory_slot memslots[]; | ||
| 439 | }; | 439 | }; |
| 440 | 440 | ||
| 441 | struct kvm { | 441 | struct kvm { |
| @@ -566,7 +566,7 @@ static struct kvm_memslots *kvm_alloc_memslots(void) · 565 unmodified lines | |||
| 566 | return NULL; | 566 | return NULL; |
| 567 | 567 | ||
| 568 | for (i = 0; i < KVM_MEM_SLOTS_NUM; i++) | 568 | 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; |
| 570 | 570 | ||
| 571 | return slots; | 571 | return slots; |
| 572 | } | 572 | } |
| @@ -1078,6 +1078,32 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, · 505 unmodified lines | |||
| 1078 | return old_memslots; | 1078 | return old_memslots; |
| 1079 | } | 1079 | } |
| 1080 | 1080 | ||
| 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 | |||
| 1081 | static int kvm_set_memslot(struct kvm *kvm, | 1107 | static int kvm_set_memslot(struct kvm *kvm, |
| 1082 | const struct kvm_userspace_memory_region *mem, | 1108 | const struct kvm_userspace_memory_region *mem, |
| 1083 | struct kvm_memory_slot *old, | 1109 | struct kvm_memory_slot *old, |
| @@ -1088,10 +1114,9 @@ static int kvm_set_memslot(struct kvm *kvm, · 4 unmodified lines | |||
| 1088 | struct kvm_memslots *slots; | 1114 | struct kvm_memslots *slots; |
| 1089 | int r; | 1115 | int r; |
| 1090 | 1116 | ||
| 1091 | slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT); | 1117 | slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change); |
| 1092 | if (!slots) | 1118 | if (!slots) |
| 1093 | return -ENOMEM; | 1119 | return -ENOMEM; |
| 1094 | memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots)); | ||
| 1095 | 1120 | ||
| 1096 | if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { | 1121 | if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { |
| 1097 | /* | 1122 | /* |