From patchwork Tue Dec 12 20:46:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 13489860 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2B38C4167D for ; Tue, 12 Dec 2023 20:47:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 23B636B00CA; Tue, 12 Dec 2023 15:47:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F96D6B02F6; Tue, 12 Dec 2023 15:47:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01C486B00CA; Tue, 12 Dec 2023 15:47:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E21856B0103 for ; Tue, 12 Dec 2023 15:47:03 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B574FA0AA1 for ; Tue, 12 Dec 2023 20:47:03 +0000 (UTC) X-FDA: 81559350726.16.21586DB Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf25.hostedemail.com (Postfix) with ESMTP id B370AA0014 for ; Tue, 12 Dec 2023 20:47:00 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=aUTcHb+R; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3w8Z4ZQUKCGYWEKMWKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--sagis.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3w8Z4ZQUKCGYWEKMWKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--sagis.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702414020; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UNdApJhYNj7CgDTo7NNKspnxbte38oVK+sy/EZvIraM=; b=vHlOzKhVChMnNNo6xqr3K15Iy2IVnjTA/AC5LMTN5KP8Xp3MyjonDu5ylRuek3cyuSw+0K UYlYSG65nyUhd3AJ7xsDQfUrGsROJjuZQ3XvLdPv7qyMe4PkpJzIUP0QyXueV5/wzvfT2m bQsnNldmmrXA/ePerK1rCB7YIq4UM64= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=aUTcHb+R; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3w8Z4ZQUKCGYWEKMWKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--sagis.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3w8Z4ZQUKCGYWEKMWKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--sagis.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702414020; a=rsa-sha256; cv=none; b=HbHfdUd0q1xQAa2OSx412O8Bx/RZbxhxpTiZ5biE3XBYUOn0oKIFg4o0Md3yrik2Qwlq6u HlI3pqPNJ/qW/a1MPGq6ao7pJseThK1fZlcqm8qDvEThnzW4C070BlQf976q7PJINZkImm yq//OSgnaLxEmjcnl3ETdI+/X2wg1f8= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2869cdac540so3534149a91.0 for ; Tue, 12 Dec 2023 12:47:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414019; x=1703018819; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=UNdApJhYNj7CgDTo7NNKspnxbte38oVK+sy/EZvIraM=; b=aUTcHb+R294vuhehTtthoydbB47i+PvL72d+miBGm17Atsqgy8M2QiFESOZFIIBWLC gLPI+1rt905fxckCfzZ9WpqxSDgtwK3B8ql8fg9UfNaKWZPtMjBx17qTLxo9wdXpdMmH DRbCtMxiFaLgu+3xZx+si8Nz5MiFg8LQyxvpZYJmtIb84VbVoWGuneXBOSbYcAQeToxC feOScfdOSVQas2ETuv9m/cfzGJL6WlbzRDgKCtVLvowfHLyOXbV8m5PlhS6gpVwwc2fb 6D/6Dg+SlBT/KqelpePQCK4pxZDNSz/AzRHVvonK7sNtpjnDUyN4Vur7JJgh8tbjlNOi AL0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414019; x=1703018819; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UNdApJhYNj7CgDTo7NNKspnxbte38oVK+sy/EZvIraM=; b=UFITPHDA7ZmgHlzTHCb+YMbNX7XX67SOVSim+kTesnASWzU1Cw1xLEASg9uSrqQPlF W6goohUznOyxFix8jF3URP5YFpwOVS+XvJx0ErBGdW79lF+ygIeD/2+YYZthY+nTESTm Vgg4nABsZI1+xl0x2a7QYzAbgwkGSv7r9DfvsoPYYquN8T+y0ueOrWvDXM9AKGZ75nmB V6Xtuj9szaZbvaAtnBPgAdofnK2syUvu0KtvWvAlgKap28dcEZDcIPuX6vHa2WZSf9tU tHFYj5bp8qcPKGJRp7yOWztJflezAGNLyM5oNSpN9dfoJwLNzECtu5AQ5my9UUl83tuF 4CFg== X-Gm-Message-State: AOJu0YzE4OmakebJv/jki58D0UZUB2AXIghPLNXmt/Jcjo68hBbMHZKy jKlCx5I2TL2ByIH5CBcjajrhanRtFA== X-Google-Smtp-Source: AGHT+IFZJwJRZcoWgNgtpTbykf4LZtBAxjnj8x3+a2qkplSA1n0mJv6jcJdIXWysboMlPxbmymXIpA//uQ== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:db09:b0:1d0:8fff:cd75 with SMTP id m9-20020a170902db0900b001d08fffcd75mr51402plx.10.1702414019454; Tue, 12 Dec 2023 12:46:59 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:16 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-2-sagis@google.com> Subject: [RFC PATCH v5 01/29] KVM: selftests: Add function to allow one-to-one GVA to GPA mappings From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Rspamd-Queue-Id: B370AA0014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: bkzwdkr847x96wayf6k86mw184kayrjj X-HE-Tag: 1702414020-758953 X-HE-Meta: U2FsdGVkX1+ukkm6haBauzvCirRncDh231hyc+/3I+HmihPhy56Xaqu5oLav14GPJZKs+RCEBYer5gzsNGhJIdTurP3xnisrTqW8BLjBLaRE0tT4ymZAH1CyXGEiHBRqLoDDLKtm/dpEIkX4Ee768rI+vsf96vDlIr/5bLr9FR99bg88weHhjfQZzeYU1Ct5HJxfps1hlAi2aaqXEgJloJyt2RiSoEisMevvoDx/YK9qwlB1aLlyLUPRESpsp36Ca1O8A4+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Ontg/7CVdlY29STx/Jvr/mwL3dF88pGYtUHmtnKPs24DEef6DEWh1GEAjdJCVjzTgkUQNwmoSr8RoBxgnMr25/K0zcedWl89lp19pCiEPSKjGmNUb44jfSkferTgj4ggOZID8Aq0O3DAWDJGdUf1caSngHT47BYLlCQz1aJZTrusIt/b3Ze5dm+7ShcqNfJTzUKu88tx+BWvG5wairAkjRfk89OLsuq+hZqmCx4FyajSfRvYSsv5yaVcN8pGI5fMR/IZdQ8f X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Ackerley Tng One-to-one GVA to GPA mappings can be used in the guest to set up boot sequences during which paging is enabled, hence requiring a transition from using physical to virtual addresses in consecutive instructions. Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/include/kvm_util_base.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 63 ++++++++++++++++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 1426e88ebdc7..c2e5c5f25dfc 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -564,6 +564,8 @@ vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type); vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); +vm_vaddr_t vm_vaddr_alloc_1to1(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, uint32_t data_memslot); vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index febc63d7a46b..4f1ae0f1eef0 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1388,17 +1388,37 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, return pgidx_start * vm->page_size; } +/* + * VM Virtual Address Allocate Shared/Encrypted + * + * Input Args: + * vm - Virtual Machine + * sz - Size in bytes + * vaddr_min - Minimum starting virtual address + * paddr_min - Minimum starting physical address + * data_memslot - memslot number to allocate in + * encrypt - Whether the region should be handled as encrypted + * + * Output Args: None + * + * Return: + * Starting guest virtual address + * + * Allocates at least sz bytes within the virtual address space of the vm + * given by vm. The allocated bytes are mapped to a virtual address >= + * the address given by vaddr_min. Note that each allocation uses a + * a unique set of pages, with the minimum real allocation being at least + * a page. + */ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, - vm_vaddr_t vaddr_min, - enum kvm_mem_region_type type, - bool encrypt) + vm_vaddr_t vaddr_min, vm_paddr_t paddr_min, + uint32_t data_memslot, bool encrypt) { uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); virt_pgd_alloc(vm); - vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages, - KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type], encrypt); + vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages, paddr_min, + data_memslot, encrypt); /* * Find an unused range of virtual page addresses of at least @@ -1408,8 +1428,7 @@ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, /* Map the virtual pages. */ for (vm_vaddr_t vaddr = vaddr_start; pages > 0; - pages--, vaddr += vm->page_size, paddr += vm->page_size) { - + pages--, vaddr += vm->page_size, paddr += vm->page_size) { virt_pg_map(vm, vaddr, paddr); sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); @@ -1421,12 +1440,16 @@ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type) { - return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, vm->protected); + return ____vm_vaddr_alloc(vm, sz, vaddr_min, + KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[type], vm->protected); } vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) { - return ____vm_vaddr_alloc(vm, sz, vaddr_min, MEM_REGION_TEST_DATA, false); + return ____vm_vaddr_alloc(vm, sz, vaddr_min, + KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[MEM_REGION_TEST_DATA], false); } /* @@ -1453,6 +1476,26 @@ vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min) return __vm_vaddr_alloc(vm, sz, vaddr_min, MEM_REGION_TEST_DATA); } +/** + * Allocate memory in @vm of size @sz in memslot with id @data_memslot, + * beginning with the desired address of @vaddr_min. + * + * If there isn't enough memory at @vaddr_min, find the next possible address + * that can meet the requested size in the given memslot. + * + * Return the address where the memory is allocated. + */ +vm_vaddr_t vm_vaddr_alloc_1to1(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, uint32_t data_memslot) +{ + vm_vaddr_t gva = ____vm_vaddr_alloc(vm, sz, vaddr_min, + (vm_paddr_t)vaddr_min, data_memslot, + vm->protected); + TEST_ASSERT_EQ(gva, addr_gva2gpa(vm, gva)); + + return gva; +} + /* * VM Virtual Address Allocate Pages *