kvmarm.lists.cs.columbia.edu archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Wanpeng Li" <wanpengli@tencent.com>,
	kvm@vger.kernel.org, "David Hildenbrand" <david@redhat.com>,
	linux-mips@vger.kernel.org, "Paul Mackerras" <paulus@ozlabs.org>,
	kvmarm@lists.cs.columbia.edu,
	"Janosch Frank" <frankja@linux.ibm.com>,
	"Marc Zyngier" <maz@kernel.org>, "Joerg Roedel" <joro@8bytes.org>,
	"Christian Borntraeger" <borntraeger@de.ibm.com>,
	kvm-ppc@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	"Jim Mattson" <jmattson@google.com>,
	"Cornelia Huck" <cohuck@redhat.com>,
	"Sean Christopherson" <sean.j.christopherson@intel.com>,
	linux-kernel@vger.kernel.org,
	"Vitaly Kuznetsov" <vkuznets@redhat.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>
Subject: [PATCH v6 18/22] KVM: Dynamically size memslot array based on number of used slots
Date: Tue, 18 Feb 2020 13:07:32 -0800	[thread overview]
Message-ID: <20200218210736.16432-19-sean.j.christopherson@intel.com> (raw)
In-Reply-To: <20200218210736.16432-1-sean.j.christopherson@intel.com>

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.

Tested-by: Christoffer Dall <christoffer.dall@arm.com>
Tested-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 include/linux/kvm_host.h |  2 +-
 virt/kvm/kvm_main.c      | 31 ++++++++++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 72b46d6f7f4a..f85cc9771ade 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -431,11 +431,11 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
  */
 struct kvm_memslots {
 	u64 generation;
-	struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
 	/* The mapping table from slot id to the index in memslots[]. */
 	short id_to_index[KVM_MEM_SLOTS_NUM];
 	atomic_t lru_slot;
 	int used_slots;
+	struct kvm_memory_slot memslots[];
 };
 
 struct kvm {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2f2c434ab2c9..0583143aca90 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -566,7 +566,7 @@ static struct kvm_memslots *kvm_alloc_memslots(void)
 		return NULL;
 
 	for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
-		slots->id_to_index[i] = slots->memslots[i].id = -1;
+		slots->id_to_index[i] = -1;
 
 	return slots;
 }
@@ -1078,6 +1078,32 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
 	return old_memslots;
 }
 
+/*
+ * Note, at a minimum, the current number of used slots must be allocated, even
+ * when deleting a memslot, as we need a complete duplicate of the memslots for
+ * use when invalidating a memslot prior to deleting/moving the memslot.
+ */
+static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old,
+					     enum kvm_mr_change change)
+{
+	struct kvm_memslots *slots;
+	size_t old_size, new_size;
+
+	old_size = sizeof(struct kvm_memslots) +
+		   (sizeof(struct kvm_memory_slot) * old->used_slots);
+
+	if (change == KVM_MR_CREATE)
+		new_size = old_size + sizeof(struct kvm_memory_slot);
+	else
+		new_size = old_size;
+
+	slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT);
+	if (likely(slots))
+		memcpy(slots, old, old_size);
+
+	return slots;
+}
+
 static int kvm_set_memslot(struct kvm *kvm,
 			   const struct kvm_userspace_memory_region *mem,
 			   struct kvm_memory_slot *old,
@@ -1088,10 +1114,9 @@ static int kvm_set_memslot(struct kvm *kvm,
 	struct kvm_memslots *slots;
 	int r;
 
-	slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT);
+	slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change);
 	if (!slots)
 		return -ENOMEM;
-	memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots));
 
 	if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
 		/*
-- 
2.24.1

_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

  parent reply	other threads:[~2020-02-18 21:08 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-18 21:07 [PATCH v6 00/22] KVM: Dynamically size memslot arrays Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 01/22] KVM: x86: Allocate new rmap and large page tracking when moving memslot Sean Christopherson
2020-02-19 19:10   ` Peter Xu
2020-02-18 21:07 ` [PATCH v6 02/22] KVM: Reinstall old memslots if arch preparation fails Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 03/22] KVM: Don't free new memslot if allocation of said memslot fails Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 04/22] KVM: PPC: Move memslot memory allocation into prepare_memory_region() Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 05/22] KVM: x86: Allocate memslot resources during prepare_memory_region() Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 06/22] KVM: Drop kvm_arch_create_memslot() Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 07/22] KVM: Explicitly free allocated-but-unused dirty bitmap Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 08/22] KVM: Refactor error handling for setting memory region Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 09/22] KVM: Move setting of memslot into helper routine Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 10/22] KVM: Drop "const" attribute from old memslot in commit_memory_region() Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 11/22] KVM: x86: Free arrays for old memslot when moving memslot's base gfn Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 12/22] KVM: Move memslot deletion to helper function Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 13/22] KVM: Simplify kvm_free_memslot() and all its descendents Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 14/22] KVM: Clean up local variable usage in __kvm_set_memory_region() Sean Christopherson
2020-02-21 17:43   ` Paolo Bonzini
2020-02-18 21:07 ` [PATCH v6 15/22] KVM: Provide common implementation for generic dirty log functions Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 16/22] KVM: Ensure validity of memslot with respect to kvm_get_dirty_log() Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 17/22] KVM: Terminate memslot walks via used_slots Sean Christopherson
2020-02-21 17:47   ` Paolo Bonzini
2020-02-18 21:07 ` Sean Christopherson [this message]
2020-02-18 21:07 ` [PATCH v6 19/22] KVM: selftests: Add test for KVM_SET_USER_MEMORY_REGION Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 20/22] KVM: x86/mmu: Move kvm_arch_flush_remote_tlbs_memslot() to mmu.c Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 21/22] KVM: x86/mmu: Use ranged-based TLB flush for dirty log memslot flush Sean Christopherson
     [not found]   ` <fdb72ab9-18d4-5719-2863-78cde4e97fae@cogentembedded.com>
2020-02-19 15:18     ` Sean Christopherson
2020-02-18 21:07 ` [PATCH v6 22/22] KVM: x86/mmu: Consolidate open coded variants of memslot TLB flushes Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200218210736.16432-19-sean.j.christopherson@intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=borntraeger@de.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=f4bug@amsat.org \
    --cc=frankja@linux.ibm.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=paulus@ozlabs.org \
    --cc=pbonzini@redhat.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).