]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: arm64: Introduce hyp_alloc_private_va_range()
authorKalesh Singh <kaleshsingh@google.com>
Wed, 20 Apr 2022 21:42:52 +0000 (14:42 -0700)
committerMarc Zyngier <maz@kernel.org>
Thu, 28 Apr 2022 19:53:13 +0000 (20:53 +0100)
hyp_alloc_private_va_range() can be used to reserve private VA ranges
in the nVHE hypervisor. Allocations are aligned based on the order of
the requested size.

This will be used to implement stack guard pages for KVM nVHE hypervisor
(nVHE Hyp mode / not pKVM), in a subsequent patch in the series.

Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Tested-by: Fuad Tabba <tabba@google.com>
Reviewed-by: Fuad Tabba <tabba@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220420214317.3303360-2-kaleshsingh@google.com
arch/arm64/include/asm/kvm_mmu.h
arch/arm64/kvm/mmu.c

index 74735a864eeeccbfb2ee46a3d450015a587131d8..a50cbb5ba402d28b68e624b5148042c599fba7b1 100644 (file)
@@ -154,6 +154,7 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
 int kvm_share_hyp(void *from, void *to);
 void kvm_unshare_hyp(void *from, void *to);
 int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot);
+int hyp_alloc_private_va_range(size_t size, unsigned long *haddr);
 int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
                           void __iomem **kaddr,
                           void __iomem **haddr);
index 53ae2c0640bc2ab1458b26863c6ba7388fbf635a..7de1e02ebfd11bfcd92311963bcc1523314ec2ac 100644 (file)
@@ -457,23 +457,22 @@ int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
        return 0;
 }
 
-static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
-                                       unsigned long *haddr,
-                                       enum kvm_pgtable_prot prot)
+
+/**
+ * hyp_alloc_private_va_range - Allocates a private VA range.
+ * @size:      The size of the VA range to reserve.
+ * @haddr:     The hypervisor virtual start address of the allocation.
+ *
+ * The private virtual address (VA) range is allocated below io_map_base
+ * and aligned based on the order of @size.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int hyp_alloc_private_va_range(size_t size, unsigned long *haddr)
 {
        unsigned long base;
        int ret = 0;
 
-       if (!kvm_host_owns_hyp_mappings()) {
-               base = kvm_call_hyp_nvhe(__pkvm_create_private_mapping,
-                                        phys_addr, size, prot);
-               if (IS_ERR_OR_NULL((void *)base))
-                       return PTR_ERR((void *)base);
-               *haddr = base;
-
-               return 0;
-       }
-
        mutex_lock(&kvm_hyp_pgd_mutex);
 
        /*
@@ -484,8 +483,10 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
         *
         * The allocated size is always a multiple of PAGE_SIZE.
         */
-       size = PAGE_ALIGN(size + offset_in_page(phys_addr));
-       base = io_map_base - size;
+       base = io_map_base - PAGE_ALIGN(size);
+
+       /* Align the allocation based on the order of its size */
+       base = ALIGN_DOWN(base, PAGE_SIZE << get_order(size));
 
        /*
         * Verify that BIT(VA_BITS - 1) hasn't been flipped by
@@ -495,19 +496,40 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
        if ((base ^ io_map_base) & BIT(VA_BITS - 1))
                ret = -ENOMEM;
        else
-               io_map_base = base;
+               *haddr = io_map_base = base;
 
        mutex_unlock(&kvm_hyp_pgd_mutex);
 
+       return ret;
+}
+
+static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
+                                       unsigned long *haddr,
+                                       enum kvm_pgtable_prot prot)
+{
+       unsigned long addr;
+       int ret = 0;
+
+       if (!kvm_host_owns_hyp_mappings()) {
+               addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping,
+                                        phys_addr, size, prot);
+               if (IS_ERR_VALUE(addr))
+                       return addr;
+               *haddr = addr;
+
+               return 0;
+       }
+
+       size = PAGE_ALIGN(size + offset_in_page(phys_addr));
+       ret = hyp_alloc_private_va_range(size, &addr);
        if (ret)
-               goto out;
+               return ret;
 
-       ret = __create_hyp_mappings(base, size, phys_addr, prot);
+       ret = __create_hyp_mappings(addr, size, phys_addr, prot);
        if (ret)
-               goto out;
+               return ret;
 
-       *haddr = base + offset_in_page(phys_addr);
-out:
+       *haddr = addr + offset_in_page(phys_addr);
        return ret;
 }