Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp190290iob; Tue, 3 May 2022 14:47:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysLiePSATbRBDC/KRqzfcC8XXcbXeX2R7c9tNACGId90d2Q8TdFTZAskbosTPj3ZARdywJ X-Received: by 2002:a17:906:4fc3:b0:6e0:66a5:d59c with SMTP id i3-20020a1709064fc300b006e066a5d59cmr17240083ejw.131.1651614469884; Tue, 03 May 2022 14:47:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651614469; cv=none; d=google.com; s=arc-20160816; b=bns4t1+K2900NVDNrCW2kxYwJv77Mcs/fX3zqFGN6ci6u07xd1eE+G7qQaTriZeZ2B Wd98MCSRsCoE26Fr+ke6BbVeKhcpuCp6G2C61gjeOWTErnFguiUyIA8izQnHpIFI6RXn RgsjHxOkqgVrzSucskIIwgtUQMfgnEMaWu1EDt/7JmtG7qFhn3kLU4gibt1qau7O5ZY3 z+qQSlcaG53D41sBkfFy5ZCAYytvmXr2n3qRA5PyOKrtO0oU1PGHp/UyOBMco8EUtM3+ fMXrUzgdawSfrb4U6rNqQxg68SsmTCy/30QHdCV422zn7Wx07vgEXGHO6mQC4xpM0zi2 mkTQ== 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=UI0i04LOeDv306YCyysgbmYsjYLKqLtrpgIp2tMXlhg=; b=WqL+NTqoI5IEXpIx8K8ZFnn+ZpdlYRSnZEhZCC7+ZGodOVM1EjOwYPI6Y0G3hnC/sK O40g65ENQQvH+J4ZeOrOOmUaxABp48dqW1lIHWAK4lCBMigJvn1+j1ROJgz9vxzJ5wY5 u76PAN8uBnxHAdrulu5Jji2xpJCpHtiOexF+jJhQRt1sInMiF0oSnz4pQQUSPvoTPB1k wghPSoOKSTR3ViqVNPW+rKDUQSf8X6y+/8j8D3Ve0hD41XmFyhcJYNjWWdOwnJKF5bU9 gahDTuLQgVrPkRc0IEvtcSNgr8WJ33cTrMZT+/u80okzA3KfGyP66WHRz4OYECVI1iL3 BHoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BaTAJBbt; 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 dv18-20020a170906b81200b006f3aca672ebsi13724108ejb.1.2022.05.03.14.47.25; Tue, 03 May 2022 14:47:49 -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=BaTAJBbt; 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 S241454AbiECSfF (ORCPT + 99 others); Tue, 3 May 2022 14:35:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241382AbiECSep (ORCPT ); Tue, 3 May 2022 14:34:45 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8865A3F89D for ; Tue, 3 May 2022 11:31:03 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id u18-20020a170902e21200b0015e5e660618so6841929plb.5 for ; Tue, 03 May 2022 11:31:03 -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=UI0i04LOeDv306YCyysgbmYsjYLKqLtrpgIp2tMXlhg=; b=BaTAJBbtgdle1GlFYIxOKqwCwV9qPPsN0DN3CdhUWT+UQKmDWmL5/2GR7zcx9m227i /iD+cqmiMcA3jCvEQSuHz1e6ySu0Xin2ah6AuoRg3nHzO6M6LLj0W7FUxZOgksKTkUCg 4pkYK+VFcAclXZ5/crt9hNnrdM1KW4irzX3r+yZd0zjOWe6/I8bKUmWmIQXc7yQXBiNn vJS8rXZzzxdn8DMNwi9QLz7vmoM2mpvaRXlzA/EmSTeZAbp7QbI1+TnQj6aupPRYdPSZ n+60zANuUT3WGGIVyGYIv9lUBJ6JcDRhgWSwWmW1A1wAjb1OD92dhRjcLy5uqfgaXo2d VOPQ== 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=UI0i04LOeDv306YCyysgbmYsjYLKqLtrpgIp2tMXlhg=; b=2dBpbLNnnCVMK1c1oZYjgl6BgHgBF+C9cp6DHciOVTph2wvpVCQyTLNb3DZIEfN0u6 IZ/QSTHF3HyF4kIDXApxot7OePLnBVCDI1HeNJzOjKWbxB99dOmtqfpygZKUJKi1EQ4Y NCWm85rnGF9kRJ4FUpMjo6XZoT8fcJz1aHgl9gzslNOeb+de3WrPwGw6f4QZQt5LDFw0 ULTCQMPfVt71zNS07jamzCcPoy5xKyDJCtGc42b0ZaKrT/wG5Js+Be/wLDvKgYtFRePr 7jVJv/H/DxmDIgChHVazmF0kiKUnPfRbgpfWDek9b06r7uBqG+NEvkhAJQ6IhM6EvJtU gTaQ== X-Gm-Message-State: AOAM531DnKtoC9Zid4PKL6n15+T1tuXPTJld3JR0vYfeLm3mfU0Yq8Ky KkvQn6hLcf84GLkaOh7CZ1ex6dB94vMAiy/kqKpp4zZFjnIs9q9Yj0Vs9SGcIJJ5v4ke/0jPzDB qva/gW1gdUVDbNwCrFHY1ScNuiwBp8HL6uPppEukLJBPSbwqPFnlzfvoH+8y5hKbLx4jfVpis X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a17:90a:e510:b0:1d9:ee23:9fa1 with SMTP id t16-20020a17090ae51000b001d9ee239fa1mr95259pjy.0.1651602662631; Tue, 03 May 2022 11:31:02 -0700 (PDT) Date: Tue, 3 May 2022 18:30:43 +0000 In-Reply-To: <20220503183045.978509-1-bgardon@google.com> Message-Id: <20220503183045.978509-10-bgardon@google.com> Mime-Version: 1.0 References: <20220503183045.978509-1-bgardon@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v7 09/11] 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=unavailable 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. Reviewed-by: David Matlack Reviewed-by: Peter Xu 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 6930ba298170..27ffd2537df6 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.36.0.464.gb9c8b46e94-goog