kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, borntraeger@de.ibm.com,
	frankja@linux.ibm.com, bgardon@google.com, peterx@redhat.com
Subject: [PATCH 10/11] KVM: selftests: Introduce vm_create_[default_]vcpus
Date: Wed,  4 Nov 2020 22:23:56 +0100	[thread overview]
Message-ID: <20201104212357.171559-11-drjones@redhat.com> (raw)
In-Reply-To: <20201104212357.171559-1-drjones@redhat.com>

Introduce new vm_create variants that also takes a number of vcpus,
an amount of per-vcpu pages, and optionally a list of vcpuids. These
variants will create default VMs with enough additional pages to
cover the vcpu stacks, per-vcpu pages, and pagetable pages for all.
The new 'default' variant uses VM_MODE_DEFAULT, whereas the other
new variant accepts the mode as a parameter.

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 .../testing/selftests/kvm/include/kvm_util.h  | 10 ++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 36 ++++++++++++++++---
 2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index ebf7f87d72df..0d652de57e6e 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -242,6 +242,16 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
 struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
 				 void *guest_code);
 
+/* Same as vm_create_default, but can be used for more than one vcpu */
+struct kvm_vm *vm_create_default_with_vcpus(uint32_t nr_vcpus, uint64_t extra_mem_pages,
+					    uint32_t num_percpu_pages, void *guest_code,
+					    uint32_t vcpuids[]);
+
+/* Like vm_create_default_with_vcpus, but accepts mode as a parameter */
+struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
+				    uint64_t extra_mem_pages, uint32_t num_percpu_pages,
+				    void *guest_code, uint32_t vcpuids[]);
+
 /*
  * Adds a vCPU with reasonable defaults (e.g. a stack)
  *
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 0d9d2242af2e..e56ed247b71e 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -244,8 +244,9 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
 	return vm;
 }
 
-struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
-				 void *guest_code)
+struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus,
+				    uint64_t extra_mem_pages, uint32_t num_percpu_pages,
+				    void *guest_code, uint32_t vcpuids[])
 {
 	/* The maximum page table size for a memory region will be when the
 	 * smallest pages are used. Considering each page contains x page
@@ -253,10 +254,19 @@ struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
 	 * N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller
 	 * than N/x*2.
 	 */
-	uint64_t extra_pg_pages = (extra_mem_pages / PTRS_PER_PAGE(min_page_size())) * 2;
+	uint64_t vcpu_pages = (DEFAULT_STACK_PGS + num_percpu_pages) * nr_vcpus;
+	uint64_t extra_pg_pages = (extra_mem_pages + vcpu_pages) /
+				  PTRS_PER_PAGE(min_page_size()) * 2;
+	uint64_t pages = vm_adjust_num_guest_pages(mode,
+				DEFAULT_GUEST_PHY_PAGES + vcpu_pages + extra_pg_pages);
 	struct kvm_vm *vm;
+	int i;
+
+	TEST_ASSERT(nr_vcpus <= kvm_check_cap(KVM_CAP_MAX_VCPUS),
+		    "nr_vcpus = %d too large for host, max-vcpus = %d",
+		    nr_vcpus, kvm_check_cap(KVM_CAP_MAX_VCPUS));
 
-	vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
+	vm = vm_create(mode, pages, O_RDWR);
 
 	kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
 
@@ -264,11 +274,27 @@ struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
 	vm_create_irqchip(vm);
 #endif
 
-	vm_vcpu_add_default(vm, vcpuid, guest_code);
+	for (i = 0; i < nr_vcpus; ++i)
+		vm_vcpu_add_default(vm, vcpuids ? vcpuids[i] : i, guest_code);
 
 	return vm;
 }
 
+struct kvm_vm *vm_create_default_with_vcpus(uint32_t nr_vcpus, uint64_t extra_mem_pages,
+					    uint32_t num_percpu_pages, void *guest_code,
+					    uint32_t vcpuids[])
+{
+	return vm_create_with_vcpus(VM_MODE_DEFAULT, nr_vcpus, extra_mem_pages,
+				    num_percpu_pages, guest_code, vcpuids);
+}
+
+struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages,
+				 void *guest_code)
+{
+	return vm_create_default_with_vcpus(1, extra_mem_pages, 0, guest_code,
+					    (uint32_t []){ vcpuid });
+}
+
 /*
  * VM Restart
  *
-- 
2.26.2


  parent reply	other threads:[~2020-11-04 21:24 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 21:23 [PATCH 00/11] KVM: selftests: Cleanups Andrew Jones
2020-11-04 21:23 ` [PATCH 01/11] KVM: selftests: Add x86_64/tsc_msrs_test to .gitignore Andrew Jones
2020-11-04 21:23 ` [PATCH 02/11] KVM: selftests: Drop pointless vm_create wrapper Andrew Jones
2020-11-04 21:23 ` [PATCH 03/11] KVM: selftests: Always clear dirty bitmap after iteration Andrew Jones
2020-11-04 21:23 ` [PATCH 04/11] KVM: selftests: Use a single binary for dirty/clear log test Andrew Jones
2020-11-04 21:23 ` [PATCH 05/11] KVM: selftests: Introduce after_vcpu_run hook for dirty " Andrew Jones
2020-11-04 21:23 ` [PATCH 06/11] KVM: selftests: Make the per vcpu memory size global Andrew Jones
2020-11-04 21:23 ` [PATCH 07/11] KVM: selftests: Make the number of vcpus global Andrew Jones
2020-11-04 21:23 ` [PATCH 08/11] KVM: selftests: Factor out guest mode code Andrew Jones
2020-11-04 21:23 ` [PATCH 09/11] KVM: selftests: Make vm_create_default common Andrew Jones
2020-11-04 21:36   ` Andrew Jones
2020-11-05  7:18     ` Christian Borntraeger
2020-11-05  9:59       ` Andrew Jones
2020-11-05 18:45         ` Peter Xu
2020-11-05 18:54           ` Christian Borntraeger
2020-11-04 21:23 ` Andrew Jones [this message]
2020-11-04 21:23 ` [PATCH 11/11] KVM: selftests: Remove create_vm Andrew Jones
2020-11-05  7:08 ` [PATCH 00/11] KVM: selftests: Cleanups Christian Borntraeger
2020-11-05 18:55 ` Peter Xu
2020-11-05 19:41   ` Ben Gardon
2020-11-06  9:45     ` Andrew Jones
2020-11-06 15:04       ` Peter Xu
2020-11-09 17:11         ` Ben Gardon
2020-11-06 13:01 ` Paolo Bonzini

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=20201104212357.171559-11-drjones@redhat.com \
    --to=drjones@redhat.com \
    --cc=bgardon@google.com \
    --cc=borntraeger@de.ibm.com \
    --cc=frankja@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.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).