]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: Introduce {get,set}_host_state() helpers
authorQuentin Perret <qperret@google.com>
Wed, 16 Apr 2025 15:26:44 +0000 (15:26 +0000)
committerMarc Zyngier <maz@kernel.org>
Mon, 28 Apr 2025 08:23:46 +0000 (09:23 +0100)
Instead of directly accessing the host_state member in struct hyp_page,
introduce static inline accessors to do it. The future hyp_state member
will follow the same pattern as it will need some logic in the accessors.

Reviewed-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Quentin Perret <qperret@google.com>
Link: https://lore.kernel.org/r/20250416152648.2982950-5-qperret@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/kvm/hyp/include/nvhe/memory.h
arch/arm64/kvm/hyp/nvhe/mem_protect.c
arch/arm64/kvm/hyp/nvhe/setup.c

index bf28f9f9de6536074d31939d463f009d0d4e54fa..a1754aecf8f8bda6ecc89cb4d1159ea84b2b08b2 100644 (file)
@@ -52,7 +52,7 @@ struct hyp_page {
        u8 order;
 
        /* Host state. Guarded by the host stage-2 lock. */
-       enum pkvm_page_state host_state : 8;
+       unsigned __host_state : 8;
 
        u32 host_share_guest_count;
 };
@@ -89,6 +89,16 @@ static inline struct hyp_page *hyp_phys_to_page(phys_addr_t phys)
 #define hyp_page_to_virt(page) __hyp_va(hyp_page_to_phys(page))
 #define hyp_page_to_pool(page) (((struct hyp_page *)page)->pool)
 
+static inline enum pkvm_page_state get_host_state(phys_addr_t phys)
+{
+       return (enum pkvm_page_state)hyp_phys_to_page(phys)->__host_state;
+}
+
+static inline void set_host_state(phys_addr_t phys, enum pkvm_page_state state)
+{
+       hyp_phys_to_page(phys)->__host_state = state;
+}
+
 /*
  * Refcounting for 'struct hyp_page'.
  * hyp_pool::lock must be held if atomic access to the refcount is required.
index 2a5284f749b427927791f0706f66542e90611df9..e9060e6205cbd5f43ab5ad301ac1384207e11c75 100644 (file)
@@ -467,7 +467,7 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)
                return -EAGAIN;
 
        if (pte) {
-               WARN_ON(addr_is_memory(addr) && hyp_phys_to_page(addr)->host_state != PKVM_NOPAGE);
+               WARN_ON(addr_is_memory(addr) && get_host_state(addr) != PKVM_NOPAGE);
                return -EPERM;
        }
 
@@ -496,7 +496,7 @@ static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_
        phys_addr_t end = addr + size;
 
        for (; addr < end; addr += PAGE_SIZE)
-               hyp_phys_to_page(addr)->host_state = state;
+               set_host_state(addr, state);
 }
 
 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
@@ -627,7 +627,7 @@ static int __host_check_page_state_range(u64 addr, u64 size,
 
        hyp_assert_lock_held(&host_mmu.lock);
        for (; addr < end; addr += PAGE_SIZE) {
-               if (hyp_phys_to_page(addr)->host_state != state)
+               if (get_host_state(addr) != state)
                        return -EPERM;
        }
 
@@ -637,7 +637,7 @@ static int __host_check_page_state_range(u64 addr, u64 size,
 static int __host_set_page_state_range(u64 addr, u64 size,
                                       enum pkvm_page_state state)
 {
-       if (hyp_phys_to_page(addr)->host_state == PKVM_NOPAGE) {
+       if (get_host_state(addr) == PKVM_NOPAGE) {
                int ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT);
 
                if (ret)
@@ -911,7 +911,7 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,
                goto unlock;
 
        page = hyp_phys_to_page(phys);
-       switch (page->host_state) {
+       switch (get_host_state(phys)) {
        case PKVM_PAGE_OWNED:
                WARN_ON(__host_set_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_OWNED));
                break;
@@ -964,9 +964,9 @@ static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ip
        if (WARN_ON(ret))
                return ret;
 
-       page = hyp_phys_to_page(phys);
-       if (page->host_state != PKVM_PAGE_SHARED_OWNED)
+       if (get_host_state(phys) != PKVM_PAGE_SHARED_OWNED)
                return -EPERM;
+       page = hyp_phys_to_page(phys);
        if (WARN_ON(!page->host_share_guest_count))
                return -EINVAL;
 
index d62bcb5634a21d66179488c7c0e7710a52a2f4f6..1a414288fe8cd9fc4bc133fe3ec851a68c9a0a07 100644 (file)
@@ -201,10 +201,10 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
        case PKVM_PAGE_OWNED:
                return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
        case PKVM_PAGE_SHARED_OWNED:
-               hyp_phys_to_page(phys)->host_state = PKVM_PAGE_SHARED_BORROWED;
+               set_host_state(phys, PKVM_PAGE_SHARED_BORROWED);
                break;
        case PKVM_PAGE_SHARED_BORROWED:
-               hyp_phys_to_page(phys)->host_state = PKVM_PAGE_SHARED_OWNED;
+               set_host_state(phys, PKVM_PAGE_SHARED_OWNED);
                break;
        default:
                return -EINVAL;