summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Christopherson <seanjc@google.com>2021-12-06 20:54:22 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2021-12-08 04:24:28 -0500
commit7cd08553ab103a7ebca79035eb35b73418b2f475 (patch)
tree5af0292c8f44367b477b9a2050258c4200eee7d1
parentec5c86976674d2f5c0f389903d956eda1dc54a78 (diff)
downloadlinux-7cd08553ab103a7ebca79035eb35b73418b2f475.tar.bz2
KVM: Don't make a full copy of the old memslot in __kvm_set_memory_region()
Stop making a full copy of the old memslot in __kvm_set_memory_region() now that metadata updates are handled by kvm_set_memslot(), i.e. now that the old memslot's dirty bitmap doesn't need to be referenced after the memslot and its pointer is modified/invalidated by kvm_set_memslot(). No functional change intended. Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> Message-Id: <5dce0946b41bba8c83f6e3424c6955c56bcc9f86.1638817640.git.maciej.szmigiero@oracle.com>
-rw-r--r--virt/kvm/kvm_main.c35
1 files changed, 13 insertions, 22 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index ec5567e8442b..8ccb1ac82d38 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1730,8 +1730,8 @@ out_slots:
int __kvm_set_memory_region(struct kvm *kvm,
const struct kvm_userspace_memory_region *mem)
{
- struct kvm_memory_slot old, new;
- struct kvm_memory_slot *tmp;
+ struct kvm_memory_slot *old, *tmp;
+ struct kvm_memory_slot new;
enum kvm_mr_change change;
int as_id, id;
int r;
@@ -1761,25 +1761,16 @@ int __kvm_set_memory_region(struct kvm *kvm,
return -EINVAL;
/*
- * Make a full copy of the old memslot, the pointer will become stale
- * when the memslots are re-sorted by update_memslots(), and the old
- * memslot needs to be referenced after calling update_memslots(), e.g.
- * to free its resources and for arch specific behavior.
+ * Note, the old memslot (and the pointer itself!) may be invalidated
+ * and/or destroyed by kvm_set_memslot().
*/
- tmp = id_to_memslot(__kvm_memslots(kvm, as_id), id);
- if (tmp) {
- old = *tmp;
- tmp = NULL;
- } else {
- memset(&old, 0, sizeof(old));
- old.id = id;
- }
+ old = id_to_memslot(__kvm_memslots(kvm, as_id), id);
if (!mem->memory_size) {
- if (!old.npages)
+ if (!old || !old->npages)
return -EINVAL;
- if (WARN_ON_ONCE(kvm->nr_memslot_pages < old.npages))
+ if (WARN_ON_ONCE(kvm->nr_memslot_pages < old->npages))
return -EIO;
memset(&new, 0, sizeof(new));
@@ -1799,7 +1790,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
if (new.npages > KVM_MEM_MAX_NR_PAGES)
return -EINVAL;
- if (!old.npages) {
+ if (!old || !old->npages) {
change = KVM_MR_CREATE;
/*
@@ -1809,14 +1800,14 @@ int __kvm_set_memory_region(struct kvm *kvm,
if ((kvm->nr_memslot_pages + new.npages) < kvm->nr_memslot_pages)
return -EINVAL;
} else { /* Modify an existing slot. */
- if ((new.userspace_addr != old.userspace_addr) ||
- (new.npages != old.npages) ||
- ((new.flags ^ old.flags) & KVM_MEM_READONLY))
+ if ((new.userspace_addr != old->userspace_addr) ||
+ (new.npages != old->npages) ||
+ ((new.flags ^ old->flags) & KVM_MEM_READONLY))
return -EINVAL;
- if (new.base_gfn != old.base_gfn)
+ if (new.base_gfn != old->base_gfn)
change = KVM_MR_MOVE;
- else if (new.flags != old.flags)
+ else if (new.flags != old->flags)
change = KVM_MR_FLAGS_ONLY;
else /* Nothing to change. */
return 0;