Skip to content

Commit 8b21fb4

Browse files
Fuad Tabbaoupton
authored andcommitted
KVM: arm64: Factor out pKVM hyp vcpu creation to separate function
Move the code that creates and initializes the hyp view of a vcpu in pKVM to its own function. This is meant to make the transition to initializing every vcpu individually clearer. Acked-by: Will Deacon <will@kernel.org> Reviewed-by: Marc Zyngier <maz@kernel.org> Signed-off-by: Fuad Tabba <tabba@google.com> Link: https://lore.kernel.org/r/20250314111832.4137161-4-tabba@google.com Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 066daa8 commit 8b21fb4

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

arch/arm64/kvm/pkvm.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ static void __pkvm_destroy_hyp_vm(struct kvm *host_kvm)
114114
free_hyp_memcache(&host_kvm->arch.pkvm.stage2_teardown_mc);
115115
}
116116

117+
static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
118+
{
119+
size_t hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
120+
pkvm_handle_t handle = vcpu->kvm->arch.pkvm.handle;
121+
void *hyp_vcpu;
122+
int ret;
123+
124+
vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
125+
126+
hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
127+
if (!hyp_vcpu)
128+
return -ENOMEM;
129+
130+
ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, vcpu, hyp_vcpu);
131+
if (ret)
132+
free_pages_exact(hyp_vcpu, hyp_vcpu_sz);
133+
134+
return ret;
135+
}
136+
117137
/*
118138
* Allocates and donates memory for hypervisor VM structs at EL2.
119139
*
@@ -126,9 +146,8 @@ static void __pkvm_destroy_hyp_vm(struct kvm *host_kvm)
126146
*/
127147
static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
128148
{
129-
size_t pgd_sz, hyp_vm_sz, hyp_vcpu_sz;
149+
size_t pgd_sz, hyp_vm_sz;
130150
struct kvm_vcpu *host_vcpu;
131-
pkvm_handle_t handle;
132151
void *pgd, *hyp_vm;
133152
unsigned long idx;
134153
int ret;
@@ -162,37 +181,14 @@ static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
162181
if (ret < 0)
163182
goto free_vm;
164183

165-
handle = ret;
166-
167-
host_kvm->arch.pkvm.handle = handle;
184+
host_kvm->arch.pkvm.handle = ret;
168185
host_kvm->arch.pkvm.stage2_teardown_mc.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
169186
kvm_account_pgtable_pages(pgd, pgd_sz / PAGE_SIZE);
170187

171-
/* Donate memory for the vcpus at hyp and initialize it. */
172-
hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
173188
kvm_for_each_vcpu(idx, host_vcpu, host_kvm) {
174-
void *hyp_vcpu;
175-
176-
host_vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
177-
178-
/* Indexing of the vcpus to be sequential starting at 0. */
179-
if (WARN_ON(host_vcpu->vcpu_idx != idx)) {
180-
ret = -EINVAL;
181-
goto destroy_vm;
182-
}
183-
184-
hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
185-
if (!hyp_vcpu) {
186-
ret = -ENOMEM;
187-
goto destroy_vm;
188-
}
189-
190-
ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, host_vcpu,
191-
hyp_vcpu);
192-
if (ret) {
193-
free_pages_exact(hyp_vcpu, hyp_vcpu_sz);
189+
ret = __pkvm_create_hyp_vcpu(host_vcpu);
190+
if (ret)
194191
goto destroy_vm;
195-
}
196192
}
197193

198194
return 0;

0 commit comments

Comments
 (0)