Re: [PATCH v8 23/30] KVM: selftests: Add functions to allow mapping as shared

From: Ira Weiny
Date: Mon Aug 11 2025 - 14:47:56 EST


Sagi Shahar wrote:
> From: Ackerley Tng <ackerleytng@xxxxxxxxxx>
>
> virt_map() enforces a private mapping for private memory. Introduce
> virt_map_shared() that creates a shared mapping for private as
> well as shared memory. This way, the TD does not have to remap its
> page tables at runtime.
>
> Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
> Signed-off-by: Sagi Shahar <sagis@xxxxxxxxxx>
> ---
> .../testing/selftests/kvm/include/kvm_util.h | 23 +++++++++++++
> tools/testing/selftests/kvm/lib/kvm_util.c | 34 +++++++++++++++++++
> .../testing/selftests/kvm/lib/x86/processor.c | 15 ++++++--
> 3 files changed, 70 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index 0d1f24c9f7c7..2e444c172261 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h

[snip]

> +/*
> + * VM Virtual Page Map as Shared
> + *
> + * Input Args:
> + * vm - Virtual Machine
> + * vaddr - VM Virtual Address
> + * paddr - VM Physical Address
> + *
> + * Output Args: None
> + *
> + * Return: None
> + *
> + * Within @vm, creates a virtual translation for the page starting
> + * at @vaddr to the page starting at @paddr.
> + */
> +void virt_arch_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr);
> +
> +static inline void virt_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
> +{
> + virt_arch_pg_map_shared(vm, vaddr, paddr);
> +}
>
> /*
> * Address Guest Virtual to Guest Physical

[snip]

> diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
> index 3977719c7893..7742c01d48b1 100644
> --- a/tools/testing/selftests/kvm/lib/x86/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86/processor.c
> @@ -181,7 +181,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
> return pte;
> }
>

[snip]

> void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
> {
> __virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K);
> }
>
> +void virt_arch_pg_map_shared(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
> +{
> + ___virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K, false);
> +}

What about non-x86 arch'es? How does this compile for them?

Ira

> +
> void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
> uint64_t nr_bytes, int level)
> {
> --
> 2.51.0.rc0.155.g4a0f42376b-goog
>