[PATCH v12 02/23] KVM: selftests: Allocate pgd in virt_map() as necessary

Sagi Shahar posted 23 patches 1 month, 2 weeks ago
[PATCH v12 02/23] KVM: selftests: Allocate pgd in virt_map() as necessary
Posted by Sagi Shahar 1 month, 2 weeks ago
If virt_map() is called before any call to ____vm_vaddr_alloc() it
will create the mapping using an invalid pgd.

Add call to virt_pgd_alloc() as part of virt_map() before creating the
mapping, similarly to ____vm_vaddr_alloc()

Reviewed-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Signed-off-by: Sagi Shahar <sagis@google.com>
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 1a93d6361671..0e6a487ca7a4 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1569,6 +1569,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 	TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
 	TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
 
+	virt_pgd_alloc(vm);
 	while (npages--) {
 		virt_pg_map(vm, vaddr, paddr);
 		sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
-- 
2.51.1.851.g4ebd6896fd-goog
Re: [PATCH v12 02/23] KVM: selftests: Allocate pgd in virt_map() as necessary
Posted by Reinette Chatre 1 month, 2 weeks ago
Hi Sagi,

On 10/28/25 2:20 PM, Sagi Shahar wrote:
> If virt_map() is called before any call to ____vm_vaddr_alloc() it
> will create the mapping using an invalid pgd.

If virt_map() is currently called before any call to ____vm_vaddr_alloc()
then this patch would be a bugfix ... but it is not a bugfix so this
"if" does not seem relevant to existing code but a future change for which
this changelog does not provide information.
> Add call to virt_pgd_alloc() as part of virt_map() before creating the
> mapping, similarly to ____vm_vaddr_alloc()

The changelog is missing "why" this change is needed. Yes, it mentions the
hypothetical scenario that it aims to address but it would be helpful to
be specific about what this is a preparatory change for. What scenario would
require virt_map() to be called before ____vm_vaddr_alloc()?

Alternatively, would this patch not be unnecessary if
vm_tdx_setup_boot_code_region() is moved to be after kvm_vm_elf_load()?
As it is written (looking ahead at patch #16) it looks like the TDX boot region
creation and initialization is between slot 0's creation and initialization.

Reinette