KVM: arm64: Add a range to __pkvm_host_wrprotect_guest()
authorVincent Donnefort <vdonnefort@google.com>
Wed, 21 May 2025 12:48:29 +0000 (13:48 +0100)
committerMarc Zyngier <maz@kernel.org>
Wed, 21 May 2025 13:33:51 +0000 (14:33 +0100)
In preparation for supporting stage-2 huge mappings for np-guest. Add a
nr_pages argument to the __pkvm_host_wrprotect_guest hypercall. This
range supports only two values: 1 or PMD_SIZE / PAGE_SIZE (that is 512
on a 4K-pages system).

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Link: https://lore.kernel.org/r/20250521124834.1070650-6-vdonnefort@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
arch/arm64/kvm/hyp/nvhe/hyp-main.c
arch/arm64/kvm/hyp/nvhe/mem_protect.c
arch/arm64/kvm/pkvm.c

index 19671edbe18fac25d7248222cbdbf0155ade9b63..64d4f3bf6269454759de7f6060de0cee785c0688 100644 (file)
@@ -43,8 +43,8 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu
                            enum kvm_pgtable_prot prot);
 int __pkvm_host_unshare_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *hyp_vm);
 int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kvm_pgtable_prot prot);
-int __pkvm_host_wrprotect_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm);
 int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm *vm);
+int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *hyp_vm);
 int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu);
 
 bool addr_is_memory(phys_addr_t phys);
index 5c03bd1db87373eff169423751730f592cbc1a6d..fa7e2421d3594dc08cdc90a1b041c0f7d9b9698c 100644 (file)
@@ -310,6 +310,7 @@ static void handle___pkvm_host_wrprotect_guest(struct kvm_cpu_context *host_ctxt
 {
        DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
        DECLARE_REG(u64, gfn, host_ctxt, 2);
+       DECLARE_REG(u64, nr_pages, host_ctxt, 3);
        struct pkvm_hyp_vm *hyp_vm;
        int ret = -EINVAL;
 
@@ -320,7 +321,7 @@ static void handle___pkvm_host_wrprotect_guest(struct kvm_cpu_context *host_ctxt
        if (!hyp_vm)
                goto out;
 
-       ret = __pkvm_host_wrprotect_guest(gfn, hyp_vm);
+       ret = __pkvm_host_wrprotect_guest(gfn, nr_pages, hyp_vm);
        put_pkvm_hyp_vm(hyp_vm);
 out:
        cpu_reg(host_ctxt, 1) = ret;
index 884e2316aa48e863a49c4f42a45c758379f386d1..a6c45202aa856d1843f0c793c569930e3fde97bc 100644 (file)
@@ -1052,7 +1052,7 @@ unlock:
        return ret;
 }
 
-static void assert_host_shared_guest(struct pkvm_hyp_vm *vm, u64 ipa)
+static void assert_host_shared_guest(struct pkvm_hyp_vm *vm, u64 ipa, u64 size)
 {
        u64 phys;
        int ret;
@@ -1063,7 +1063,7 @@ static void assert_host_shared_guest(struct pkvm_hyp_vm *vm, u64 ipa)
        host_lock_component();
        guest_lock_component(vm);
 
-       ret = __check_host_shared_guest(vm, &phys, ipa, PAGE_SIZE);
+       ret = __check_host_shared_guest(vm, &phys, ipa, size);
 
        guest_unlock_component(vm);
        host_unlock_component();
@@ -1083,7 +1083,7 @@ int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kvm_
        if (prot & ~KVM_PGTABLE_PROT_RWX)
                return -EINVAL;
 
-       assert_host_shared_guest(vm, ipa);
+       assert_host_shared_guest(vm, ipa, PAGE_SIZE);
        guest_lock_component(vm);
        ret = kvm_pgtable_stage2_relax_perms(&vm->pgt, ipa, prot, 0);
        guest_unlock_component(vm);
@@ -1091,17 +1091,21 @@ int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kvm_
        return ret;
 }
 
-int __pkvm_host_wrprotect_guest(u64 gfn, struct pkvm_hyp_vm *vm)
+int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
 {
-       u64 ipa = hyp_pfn_to_phys(gfn);
+       u64 size, ipa = hyp_pfn_to_phys(gfn);
        int ret;
 
        if (pkvm_hyp_vm_is_protected(vm))
                return -EPERM;
 
-       assert_host_shared_guest(vm, ipa);
+       ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
+       if (ret)
+               return ret;
+
+       assert_host_shared_guest(vm, ipa, size);
        guest_lock_component(vm);
-       ret = kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, PAGE_SIZE);
+       ret = kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, size);
        guest_unlock_component(vm);
 
        return ret;
@@ -1115,7 +1119,7 @@ int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm *
        if (pkvm_hyp_vm_is_protected(vm))
                return -EPERM;
 
-       assert_host_shared_guest(vm, ipa);
+       assert_host_shared_guest(vm, ipa, PAGE_SIZE);
        guest_lock_component(vm);
        ret = kvm_pgtable_stage2_test_clear_young(&vm->pgt, ipa, PAGE_SIZE, mkold);
        guest_unlock_component(vm);
@@ -1131,7 +1135,7 @@ int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu)
        if (pkvm_hyp_vm_is_protected(vm))
                return -EPERM;
 
-       assert_host_shared_guest(vm, ipa);
+       assert_host_shared_guest(vm, ipa, PAGE_SIZE);
        guest_lock_component(vm);
        kvm_pgtable_stage2_mkyoung(&vm->pgt, ipa, 0);
        guest_unlock_component(vm);
index 0c5733be6bf400ebb44fbbe33779948baa248a69..02bebf0a17557a8ad0f32daa407e82f9abc789a2 100644 (file)
@@ -409,7 +409,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 
        lockdep_assert_held(&kvm->mmu_lock);
        for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
-               ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn);
+               ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn, 1);
                if (WARN_ON(ret))
                        break;
        }