Skip to content

Commit 79ea662

Browse files
vdonnefortoupton
authored andcommitted
KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats
Count the pages used by pKVM for the guest stage-2 in memory stats under secondary pagetable, similarly to what the VHE mode does. Signed-off-by: Vincent Donnefort <vdonnefort@google.com> Acked-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20250313114038.1502357-4-vdonnefort@google.com Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 8c0d7d1 commit 79ea662

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ struct kvm_hyp_memcache {
8686
phys_addr_t head;
8787
unsigned long nr_pages;
8888
struct pkvm_mapping *mapping; /* only used from EL1 */
89+
90+
#define HYP_MEMCACHE_ACCOUNT_STAGE2 BIT(1)
8991
unsigned long flags;
9092
};
9193

arch/arm64/kvm/mmu.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,24 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
10881088

10891089
static void hyp_mc_free_fn(void *addr, void *mc)
10901090
{
1091+
struct kvm_hyp_memcache *memcache = mc;
1092+
1093+
if (memcache->flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
1094+
kvm_account_pgtable_pages(addr, -1);
1095+
10911096
free_page((unsigned long)addr);
10921097
}
10931098

10941099
static void *hyp_mc_alloc_fn(void *mc)
10951100
{
1096-
return (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
1101+
struct kvm_hyp_memcache *memcache = mc;
1102+
void *addr;
1103+
1104+
addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
1105+
if (addr && memcache->flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
1106+
kvm_account_pgtable_pages(addr, 1);
1107+
1108+
return addr;
10971109
}
10981110

10991111
void free_hyp_memcache(struct kvm_hyp_memcache *mc)

arch/arm64/kvm/pkvm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,16 @@ static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
165165
handle = ret;
166166

167167
host_kvm->arch.pkvm.handle = handle;
168+
host_kvm->arch.pkvm.stage2_teardown_mc.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
169+
kvm_account_pgtable_pages(pgd, pgd_sz / PAGE_SIZE);
168170

169171
/* Donate memory for the vcpus at hyp and initialize it. */
170172
hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
171173
kvm_for_each_vcpu(idx, host_vcpu, host_kvm) {
172174
void *hyp_vcpu;
173175

176+
host_vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
177+
174178
/* Indexing of the vcpus to be sequential starting at 0. */
175179
if (WARN_ON(host_vcpu->vcpu_idx != idx)) {
176180
ret = -EINVAL;

0 commit comments

Comments
 (0)