From patchwork Thu Dec 16 17:13:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12682221 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13715C433FE for ; Thu, 16 Dec 2021 17:16:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbhLPRQl (ORCPT ); Thu, 16 Dec 2021 12:16:41 -0500 Received: from mail-dm6nam12on2079.outbound.protection.outlook.com ([40.107.243.79]:47328 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S233283AbhLPRQl (ORCPT ); Thu, 16 Dec 2021 12:16:41 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LKLNMWBPo7zUZo7V/xw34v4Zc9UxV1AODt3Oh+0q+sLH5N+7WVCX/RCjwXzOQeHoK1/WMrPx5f16dUO8dRhyLacAhnDpmoZ12aNl+Yg8AfovOng3slm68YWWpZyiSGvtLAqnhEO/3qkQhsi5ji516wPGQ7oQMqP4uv3aZJhlZ6NsQLXOuAjWL8wT98QANMaeA1Arb7A9jIzQdSOoEZbG4kzKMJVl/iNk87OT8mjCZVLDHNpo3joxnpkiS0BTdGoTV4HuW7Xo+kdtQRwpulz3SHXzaiP3mMtRw3B6uYQL5joSvMwJRAs7ZUjREMAYXtDGVADEO0T2CTq8VvB7pjmAyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=r4d3EJiV7+G8vrnfvVnAUGXj1uHWx1uaA+2immzxeB8=; b=LXJLvoqAK9hFJNPlbiWxueadktF6t9vbS3vfc5a9IG+fJWpFpZyz1O/T5L7QU/+f5+8b8Y16DJQaOJhl9ICJyN0MtRSIU3ecWjVY6Lc6Lt/hBIYQl57HxmE1Rwlvl3nZ6sihSy2yYdS+uPfNImZYWfV9zfbXvliEUYYkuwvSti8O3mcwgK+iPeFaYNuN3SwQgjAwjIQoHWKKASvwNfMx+mTCskrQojJi5d5W5mfec7Rsa0Uccy68CSKHhc7oBcAM1KFFqVl3NZ2g2JTb5X5Vhl49elhMaeMA8cKflo23n1B5T2YiN8LsA2ie3mFZU9pK/MMyJaI4SSpb3qD5YRLfYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=r4d3EJiV7+G8vrnfvVnAUGXj1uHWx1uaA+2immzxeB8=; b=bZSqr9taixZzwq7k1vryhVeS9w5Cn9ira9zQJKib5x0oLdOhvphqtLDFjmTX3Ir7+PPY0ewv+VT0+UUR3gxdyEmw3rJ7B424tXDni7ZHxECODP6qyR5jfKPiYQAxKQBGz93CxBO+8yq5BvA92gGgRiJQOS+ZNzN9MkWi8pz4r3U= Received: from MWHPR19CA0060.namprd19.prod.outlook.com (2603:10b6:300:94::22) by MWHPR12MB1471.namprd12.prod.outlook.com (2603:10b6:301:e::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12; Thu, 16 Dec 2021 17:16:38 +0000 Received: from CO1NAM11FT023.eop-nam11.prod.protection.outlook.com (2603:10b6:300:94:cafe::3a) by MWHPR19CA0060.outlook.office365.com (2603:10b6:300:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4801.15 via Frontend Transport; Thu, 16 Dec 2021 17:16:38 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by CO1NAM11FT023.mail.protection.outlook.com (10.13.175.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4801.14 via Frontend Transport; Thu, 16 Dec 2021 17:16:38 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Thu, 16 Dec 2021 11:16:37 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , Krish Sadhukhan , Peter Gonda Subject: [PATCH v2 01/13] KVM: selftests: move vm_phy_pages_alloc() earlier in file Date: Thu, 16 Dec 2021 11:13:46 -0600 Message-ID: <20211216171358.61140-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211216171358.61140-1-michael.roth@amd.com> References: <20211216171358.61140-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3ed10bdb-8c16-417b-c0df-08d9c0b7d234 X-MS-TrafficTypeDiagnostic: MWHPR12MB1471:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +66sks8HOlpMBB22RhLvpcMqI1nJcY2FaG5GOqF/uMg1M18SMqbP1TB/5QI8hCOHVv4NkwggW7sh7pM0Yzhh4rnRcZkmuJ7IVGSV+0Q68Ao6tFMMYFr/ee6LbpKcSaXIZOCX236tAu0HuE19+jvV1CpX9bPhwa0TbPjb6DAT57fmB/47e5yXE/skpddfdmmj8I2I0LpejfMesV2AHZayleLKa2mLqMiWkyftq5v0Z/AahJQw7rKZO8oYM5VM3iJDhbtkqZUKTZwco6N8ihQbv644ogcWvroRcUnw3UBob+peDG9BkCLaSZD5qVtcOxUekXg0HgZAPgkM1W/oZlD0SVSz0kMVZYVTJ/XpkNJIRL9H7dJzujbSJPjQvZgMpY30CtgP9XUtLYJ+zqWSh9cfMX8jn5UZwasifRwnmKTh25XhTtZT+NhD3vD187eyHkbX7h1siqoBB+O0bK6Rz4MTrARvRcu7uPahB4gYxPH3lnTGYqtf3t3kh21B0Xub3VJkTBm7kHhMMFd7KaO3dls7hBWgB1loSTlk7zU88unVmRKOzNxB8efJj0ziWCRL+BCcaxRFvFe9jabBsAxZ+JNe7xtKPvWWwvTyupmYXSA5ZR2MkcLUAIbnFvSmY+f8wIpAf37+lT5J+JIIHibam6CL8nCrlaw/ZzH9v/IUUHZdRGO+pELDiuOUA008PeVeU0lQzlSG1xbE6Hwvwv1hucnOd+XijxBONSw9LPvBBdynLOa8GFEwRhkIcBSJyka9yVVe9xB8jsu+iPnXFtG3p8rG5LCpnkTITyK0aLZ8hPtxJ3w= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(70586007)(2616005)(336012)(81166007)(6916009)(82310400004)(26005)(8676002)(7416002)(6666004)(86362001)(44832011)(36860700001)(83380400001)(508600001)(186003)(8936002)(316002)(2906002)(16526019)(426003)(4326008)(36756003)(40460700001)(5660300002)(54906003)(1076003)(356005)(47076005)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 17:16:38.6294 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3ed10bdb-8c16-417b-c0df-08d9c0b7d234 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT023.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1471 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Subsequent patches will break some of this code out into file-local helper functions, which will be used by functions like vm_vaddr_alloc(), which currently are defined earlier in the file, so a forward declaration would be needed. Instead, move it earlier in the file, just above vm_vaddr_alloc() and and friends, which are the main users. Reviewed-by: Mingwei Zhang Signed-off-by: Michael Roth --- tools/testing/selftests/kvm/lib/kvm_util.c | 146 ++++++++++----------- 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 8f2e0bb1ef96..f0f0250847f3 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1153,6 +1153,79 @@ void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid) list_add(&vcpu->list, &vm->vcpus); } +/* + * Physical Contiguous Page Allocator + * + * Input Args: + * vm - Virtual Machine + * num - number of pages + * paddr_min - Physical address minimum + * memslot - Memory region to allocate page from + * + * Output Args: None + * + * Return: + * Starting physical address + * + * Within the VM specified by vm, locates a range of available physical + * pages at or above paddr_min. If found, the pages are marked as in use + * and their base address is returned. A TEST_ASSERT failure occurs if + * not enough pages are available at or above paddr_min. + */ +vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, + vm_paddr_t paddr_min, uint32_t memslot) +{ + struct userspace_mem_region *region; + sparsebit_idx_t pg, base; + + TEST_ASSERT(num > 0, "Must allocate at least one page"); + + TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address " + "not divisible by page size.\n" + " paddr_min: 0x%lx page_size: 0x%x", + paddr_min, vm->page_size); + + region = memslot2region(vm, memslot); + base = pg = paddr_min >> vm->page_shift; + + do { + for (; pg < base + num; ++pg) { + if (!sparsebit_is_set(region->unused_phy_pages, pg)) { + base = pg = sparsebit_next_set(region->unused_phy_pages, pg); + break; + } + } + } while (pg && pg != base + num); + + if (pg == 0) { + fprintf(stderr, "No guest physical page available, " + "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n", + paddr_min, vm->page_size, memslot); + fputs("---- vm dump ----\n", stderr); + vm_dump(stderr, vm, 2); + abort(); + } + + for (pg = base; pg < base + num; ++pg) + sparsebit_clear(region->unused_phy_pages, pg); + + return base * vm->page_size; +} + +vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, + uint32_t memslot) +{ + return vm_phy_pages_alloc(vm, 1, paddr_min, memslot); +} + +/* Arbitrary minimum physical address used for virtual translation tables. */ +#define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 + +vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm) +{ + return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); +} + /* * VM Virtual Address Unused Gap * @@ -2195,79 +2268,6 @@ const char *exit_reason_str(unsigned int exit_reason) return "Unknown"; } -/* - * Physical Contiguous Page Allocator - * - * Input Args: - * vm - Virtual Machine - * num - number of pages - * paddr_min - Physical address minimum - * memslot - Memory region to allocate page from - * - * Output Args: None - * - * Return: - * Starting physical address - * - * Within the VM specified by vm, locates a range of available physical - * pages at or above paddr_min. If found, the pages are marked as in use - * and their base address is returned. A TEST_ASSERT failure occurs if - * not enough pages are available at or above paddr_min. - */ -vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, - vm_paddr_t paddr_min, uint32_t memslot) -{ - struct userspace_mem_region *region; - sparsebit_idx_t pg, base; - - TEST_ASSERT(num > 0, "Must allocate at least one page"); - - TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address " - "not divisible by page size.\n" - " paddr_min: 0x%lx page_size: 0x%x", - paddr_min, vm->page_size); - - region = memslot2region(vm, memslot); - base = pg = paddr_min >> vm->page_shift; - - do { - for (; pg < base + num; ++pg) { - if (!sparsebit_is_set(region->unused_phy_pages, pg)) { - base = pg = sparsebit_next_set(region->unused_phy_pages, pg); - break; - } - } - } while (pg && pg != base + num); - - if (pg == 0) { - fprintf(stderr, "No guest physical page available, " - "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n", - paddr_min, vm->page_size, memslot); - fputs("---- vm dump ----\n", stderr); - vm_dump(stderr, vm, 2); - abort(); - } - - for (pg = base; pg < base + num; ++pg) - sparsebit_clear(region->unused_phy_pages, pg); - - return base * vm->page_size; -} - -vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, - uint32_t memslot) -{ - return vm_phy_pages_alloc(vm, 1, paddr_min, memslot); -} - -/* Arbitrary minimum physical address used for virtual translation tables. */ -#define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 - -vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm) -{ - return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0); -} - /* * Address Guest Virtual to Host Virtual *