Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1447942pxb; Thu, 14 Apr 2022 06:31:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhMWfwrfpQobjeVu0wzTlCMCOEnAsaNlTu0W2+Qkj7pK6+07mHJ9P8/uWGXb44gW68soCJ X-Received: by 2002:a17:902:eb81:b0:158:937e:adf with SMTP id q1-20020a170902eb8100b00158937e0adfmr11125182plg.123.1649943064914; Thu, 14 Apr 2022 06:31:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649943064; cv=none; d=google.com; s=arc-20160816; b=Zz5stARRrVnZs3/FoLi9y3R6vUi6Db/zAUcw7MzbuNYhiGvpbMpyUdzZWClO7XApXY aD7bKu/MvIhDi4DgfjVjDif7oSVaewztWo9tpoe5Kf8S9cJQRI+8S2ek0c5XkTuoLIrl eNoxQ9O41ZLdNYSDQ9i1c/R1cABYohrYFgRt7WQKAKrrZUsG2simoWdeuawTT/glR2dx 6247/EqvkNs6SnJrIoucQtugCyBJIZTx+kzj+FNk9SjocHq6vNr+PkL21uY8R6B9hV1d eX8eLPGNk3dioJLeZgc3OX7HP2RqJTNr1G4VqRkHDefSN1PyHSKgw66ZVdbgvVHsUqk7 abew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=3UKcgaouWDljoBhDJNavl7iHJ9K4UWQ5k+7D01IUkUU=; b=UoxwkiLSBhHRVxbe5sxzoZV9reYXZ0eyqVL+fGxApLbO+UKnQ7Zv4IT2qJEz9ZTj+n 1kY/IToGt9Bxj5tdOHNnMzfdXG5AXX1BXSqYEoHqSLT0nrnYoUoeI3A8o9jjfpDQXSmF RcQ6p412owt98MTFo0cBf6MsKOPMEheTAqtq8r5R2hPzqCz80ioDK75p1E6+yc4qDmYA G5LZk/EKxDoUdEiLMX9Qd1mY+vmcxHqshlWqu6pSTJwdIjiPLZP/Y+iVXmfw8HGc/OAn nRwH/BugDOBIbPPjSMvvg7LyglYovo2qbwGtfOUM219rEOM0nKhdMFDPf4IV1wnJBIUt qSYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=i1LjNyj0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y7-20020a634b07000000b003842921930bsi8325931pga.808.2022.04.14.06.30.50; Thu, 14 Apr 2022 06:31:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=i1LjNyj0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230100AbiDMSE3 (ORCPT + 99 others); Wed, 13 Apr 2022 14:04:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237606AbiDMSC2 (ORCPT ); Wed, 13 Apr 2022 14:02:28 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E939241611 for ; Wed, 13 Apr 2022 11:00:04 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id m11-20020a170902f64b00b0015820f8038fso1519346plg.23 for ; Wed, 13 Apr 2022 11:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3UKcgaouWDljoBhDJNavl7iHJ9K4UWQ5k+7D01IUkUU=; b=i1LjNyj0MfWmClJT9PfmfWxlCGWG1mhzWBvkSRz4Hwmt2mE12WeqUlJ2ktSPAWym7p 2lceXs2Ff5HmbmubdwUFXPqbvPDNnD26dUUz3C7nKYW7Ol8ZGz+TbasHddOngbsv7M22 Rm/Y0C4ulAOrpjWMCxARkkPhjEhweZ4zzLmh8WlRzPD1u3nnikEzmmsLLOjl55/0cX7q iHd2HBP9IcAQB5JXMMA0yM38wgkXg5F5vQEyZB/0/z5egSRt9khEg/oS6WW8O7J+ofNb Lrg9o+7uJpBVNv+QEYewY6Okig2iZRfkaYE+KIPpM+aWuc5zOYfbj7Hy6R0awrlbI1tW B4JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3UKcgaouWDljoBhDJNavl7iHJ9K4UWQ5k+7D01IUkUU=; b=53XOC/3hJvcH1+aoJlfnctkmxcmlH7odjEwBH1P5CR7251MHuqdib7RcmA7LLKv7rk oMs+wxAdDMhi8hlM4R7+iQqOoHDUQl/RNjBntbvOxcbJpKXnFKm3Wp9+ucdXzobIA5C6 lvGT81lM2xi73nChkwRzIb7aVdB/I2mc9CukhQ5c7xUrwNQ95QUmlKlZnQYZJqFHwkET jUPfhaJT30af/8qBFov54g9wLdnWejiBk6jA3UVW8+cMC09clQldlQFgrhBRpXGpCbJ6 MzU4w6r/2fidvBhd8CRRTIj460kMO04Mf6A2KXY6GzvhtYRom7R1qllaDnmcy37RlYgO Kh3w== X-Gm-Message-State: AOAM533+M3ZdJfQ1Tsxe9BriPh8cW7HM5Jpl0dh5XPfp9zbF5Pvl0VoW 75s71BWrhFfotYJWL0xrru+znJogqy4VY2vjUPssypP6yUO6DdPQG8dhIRgschNQ2jLWwdzPdD/ m9Cu5f9Lyr1Wima0CB+pKIO/emwwHhp3auqHol4KvxUM2fkkb3EWlnFplcx7KyK6xJm0/h526 X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:c087:f2f2:f5f0:f73]) (user=bgardon job=sendgmr) by 2002:a05:6a00:22c7:b0:505:9c6e:de39 with SMTP id f7-20020a056a0022c700b005059c6ede39mr23398922pfj.79.1649872804260; Wed, 13 Apr 2022 11:00:04 -0700 (PDT) Date: Wed, 13 Apr 2022 10:59:43 -0700 In-Reply-To: <20220413175944.71705-1-bgardon@google.com> Message-Id: <20220413175944.71705-10-bgardon@google.com> Mime-Version: 1.0 References: <20220413175944.71705-1-bgardon@google.com> X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: [PATCH v5 09/10] KVM: selftests: Factor out calculation of pages needed for a VM From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Jim Mattson , David Dunn , Jing Zhang , Junaid Shahid , Ben Gardon Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Factor out the calculation of the number of pages needed for a VM to make it easier to separate creating the VM and adding vCPUs. Signed-off-by: Ben Gardon --- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 59 ++++++++++++++----- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 001b55ae25f8..1dac3c6607f1 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -312,6 +312,10 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, vm_paddr_t paddr_min, uint32_t memslot); vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); +uint64_t vm_pages_needed(enum vm_guest_mode mode, uint32_t nr_vcpus, + uint64_t slot0_mem_pages, uint64_t extra_mem_pages, + uint32_t num_percpu_pages); + /* * Create a VM with reasonable defaults * diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index d332f02370d1..5ffed44ab328 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -377,7 +377,7 @@ struct kvm_vm *vm_create_without_vcpus(enum vm_guest_mode mode, uint64_t pages) } /* - * VM Create with customized parameters + * Get the number of pages needed for a VM * * Input Args: * mode - VM Mode (e.g. VM_MODE_P52V48_4K) @@ -385,27 +385,17 @@ struct kvm_vm *vm_create_without_vcpus(enum vm_guest_mode mode, uint64_t pages) * slot0_mem_pages - Slot0 physical memory size * extra_mem_pages - Non-slot0 physical memory total size * num_percpu_pages - Per-cpu physical memory pages - * guest_code - Guest entry point - * vcpuids - VCPU IDs * * Output Args: None * * Return: - * Pointer to opaque structure that describes the created VM. - * - * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K), - * with customized slot0 memory size, at least 512 pages currently. - * extra_mem_pages is only used to calculate the maximum page table size, - * no real memory allocation for non-slot0 memory in this function. + * The number of pages needed for the VM. */ -struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, - uint64_t slot0_mem_pages, uint64_t extra_mem_pages, - uint32_t num_percpu_pages, void *guest_code, - uint32_t vcpuids[]) +uint64_t vm_pages_needed(enum vm_guest_mode mode, uint32_t nr_vcpus, + uint64_t slot0_mem_pages, uint64_t extra_mem_pages, + uint32_t num_percpu_pages) { uint64_t vcpu_pages, extra_pg_pages, pages; - struct kvm_vm *vm; - int i; /* Force slot0 memory size not small than DEFAULT_GUEST_PHY_PAGES */ if (slot0_mem_pages < DEFAULT_GUEST_PHY_PAGES) @@ -421,11 +411,48 @@ struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, extra_pg_pages = (slot0_mem_pages + extra_mem_pages + vcpu_pages) / PTES_PER_MIN_PAGE * 2; pages = slot0_mem_pages + vcpu_pages + extra_pg_pages; + pages = vm_adjust_num_guest_pages(mode, pages); + + return pages; +} + +/* + * VM Create with customized parameters + * + * Input Args: + * mode - VM Mode (e.g. VM_MODE_P52V48_4K) + * nr_vcpus - VCPU count + * slot0_mem_pages - Slot0 physical memory size + * extra_mem_pages - Non-slot0 physical memory total size + * num_percpu_pages - Per-cpu physical memory pages + * guest_code - Guest entry point + * vcpuids - VCPU IDs + * + * Output Args: None + * + * Return: + * Pointer to opaque structure that describes the created VM. + * + * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K), + * with customized slot0 memory size, at least 512 pages currently. + * extra_mem_pages is only used to calculate the maximum page table size, + * no real memory allocation for non-slot0 memory in this function. + */ +struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, + uint64_t slot0_mem_pages, uint64_t extra_mem_pages, + uint32_t num_percpu_pages, void *guest_code, + uint32_t vcpuids[]) +{ + uint64_t 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)); - pages = vm_adjust_num_guest_pages(mode, pages); + pages = vm_pages_needed(mode, nr_vcpus, slot0_mem_pages, + extra_mem_pages, num_percpu_pages); vm = vm_create_without_vcpus(mode, pages); -- 2.35.1.1178.g4f1659d476-goog