Commit 116c14c0 authored by Alex Williamson's avatar Alex Williamson Committed by Gleb Natapov

kvm: Fix memory slot generation updates

Previous patch "kvm: Minor memory slot optimization" (b7f69c55)
overlooked the generation field of the memory slots.  Re-using the
original memory slots left us with with two slightly different memory
slots with the same generation.  To fix this, make update_memslots()
take a new parameter to specify the last generation.  This also makes
generation management more explicit to avoid such problems in the future.
Reported-by: default avatarTakuya Yoshikawa <yoshikawa_takuya_b1@lab.ntt.co.jp>
Signed-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
Signed-off-by: default avatarGleb Natapov <gleb@redhat.com>
parent 871a069d
...@@ -428,7 +428,8 @@ void kvm_exit(void); ...@@ -428,7 +428,8 @@ void kvm_exit(void);
void kvm_get_kvm(struct kvm *kvm); void kvm_get_kvm(struct kvm *kvm);
void kvm_put_kvm(struct kvm *kvm); void kvm_put_kvm(struct kvm *kvm);
void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new); void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new,
u64 last_generation);
static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm) static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
{ {
......
...@@ -672,7 +672,8 @@ static void sort_memslots(struct kvm_memslots *slots) ...@@ -672,7 +672,8 @@ static void sort_memslots(struct kvm_memslots *slots)
slots->id_to_index[slots->memslots[i].id] = i; slots->id_to_index[slots->memslots[i].id] = i;
} }
void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new) void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new,
u64 last_generation)
{ {
if (new) { if (new) {
int id = new->id; int id = new->id;
...@@ -684,7 +685,7 @@ void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new) ...@@ -684,7 +685,7 @@ void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
sort_memslots(slots); sort_memslots(slots);
} }
slots->generation++; slots->generation = last_generation + 1;
} }
static int check_memory_region_flags(struct kvm_userspace_memory_region *mem) static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
...@@ -819,7 +820,7 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -819,7 +820,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
slot = id_to_memslot(slots, mem->slot); slot = id_to_memslot(slots, mem->slot);
slot->flags |= KVM_MEMSLOT_INVALID; slot->flags |= KVM_MEMSLOT_INVALID;
update_memslots(slots, NULL); update_memslots(slots, NULL, kvm->memslots->generation);
old_memslots = kvm->memslots; old_memslots = kvm->memslots;
rcu_assign_pointer(kvm->memslots, slots); rcu_assign_pointer(kvm->memslots, slots);
...@@ -867,7 +868,7 @@ int __kvm_set_memory_region(struct kvm *kvm, ...@@ -867,7 +868,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
memset(&new.arch, 0, sizeof(new.arch)); memset(&new.arch, 0, sizeof(new.arch));
} }
update_memslots(slots, &new); update_memslots(slots, &new, kvm->memslots->generation);
old_memslots = kvm->memslots; old_memslots = kvm->memslots;
rcu_assign_pointer(kvm->memslots, slots); rcu_assign_pointer(kvm->memslots, slots);
synchronize_srcu_expedited(&kvm->srcu); synchronize_srcu_expedited(&kvm->srcu);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment