Skip to content

Commit ba5b2e5

Browse files
Quentin PerretMarc Zyngier
authored andcommitted
KVM: arm64: Introduce {get,set}_host_state() helpers
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>
1 parent cd4b039 commit ba5b2e5

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

arch/arm64/kvm/hyp/include/nvhe/memory.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct hyp_page {
5252
u8 order;
5353

5454
/* Host state. Guarded by the host stage-2 lock. */
55-
enum pkvm_page_state host_state : 8;
55+
unsigned __host_state : 8;
5656

5757
u32 host_share_guest_count;
5858
};
@@ -89,6 +89,16 @@ static inline struct hyp_page *hyp_phys_to_page(phys_addr_t phys)
8989
#define hyp_page_to_virt(page) __hyp_va(hyp_page_to_phys(page))
9090
#define hyp_page_to_pool(page) (((struct hyp_page *)page)->pool)
9191

92+
static inline enum pkvm_page_state get_host_state(phys_addr_t phys)
93+
{
94+
return (enum pkvm_page_state)hyp_phys_to_page(phys)->__host_state;
95+
}
96+
97+
static inline void set_host_state(phys_addr_t phys, enum pkvm_page_state state)
98+
{
99+
hyp_phys_to_page(phys)->__host_state = state;
100+
}
101+
92102
/*
93103
* Refcounting for 'struct hyp_page'.
94104
* hyp_pool::lock must be held if atomic access to the refcount is required.

arch/arm64/kvm/hyp/nvhe/mem_protect.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)
467467
return -EAGAIN;
468468

469469
if (pte) {
470-
WARN_ON(addr_is_memory(addr) && hyp_phys_to_page(addr)->host_state != PKVM_NOPAGE);
470+
WARN_ON(addr_is_memory(addr) && get_host_state(addr) != PKVM_NOPAGE);
471471
return -EPERM;
472472
}
473473

@@ -496,7 +496,7 @@ static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_
496496
phys_addr_t end = addr + size;
497497

498498
for (; addr < end; addr += PAGE_SIZE)
499-
hyp_phys_to_page(addr)->host_state = state;
499+
set_host_state(addr, state);
500500
}
501501

502502
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,
627627

628628
hyp_assert_lock_held(&host_mmu.lock);
629629
for (; addr < end; addr += PAGE_SIZE) {
630-
if (hyp_phys_to_page(addr)->host_state != state)
630+
if (get_host_state(addr) != state)
631631
return -EPERM;
632632
}
633633

@@ -637,7 +637,7 @@ static int __host_check_page_state_range(u64 addr, u64 size,
637637
static int __host_set_page_state_range(u64 addr, u64 size,
638638
enum pkvm_page_state state)
639639
{
640-
if (hyp_phys_to_page(addr)->host_state == PKVM_NOPAGE) {
640+
if (get_host_state(addr) == PKVM_NOPAGE) {
641641
int ret = host_stage2_idmap_locked(addr, size, PKVM_HOST_MEM_PROT);
642642

643643
if (ret)
@@ -911,7 +911,7 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,
911911
goto unlock;
912912

913913
page = hyp_phys_to_page(phys);
914-
switch (page->host_state) {
914+
switch (get_host_state(phys)) {
915915
case PKVM_PAGE_OWNED:
916916
WARN_ON(__host_set_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_OWNED));
917917
break;
@@ -964,9 +964,9 @@ static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ip
964964
if (WARN_ON(ret))
965965
return ret;
966966

967-
page = hyp_phys_to_page(phys);
968-
if (page->host_state != PKVM_PAGE_SHARED_OWNED)
967+
if (get_host_state(phys) != PKVM_PAGE_SHARED_OWNED)
969968
return -EPERM;
969+
page = hyp_phys_to_page(phys);
970970
if (WARN_ON(!page->host_share_guest_count))
971971
return -EINVAL;
972972

arch/arm64/kvm/hyp/nvhe/setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
201201
case PKVM_PAGE_OWNED:
202202
return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
203203
case PKVM_PAGE_SHARED_OWNED:
204-
hyp_phys_to_page(phys)->host_state = PKVM_PAGE_SHARED_BORROWED;
204+
set_host_state(phys, PKVM_PAGE_SHARED_BORROWED);
205205
break;
206206
case PKVM_PAGE_SHARED_BORROWED:
207-
hyp_phys_to_page(phys)->host_state = PKVM_PAGE_SHARED_OWNED;
207+
set_host_state(phys, PKVM_PAGE_SHARED_OWNED);
208208
break;
209209
default:
210210
return -EINVAL;

0 commit comments

Comments
 (0)