From b97d00514a10dae1210d03ab2bd7d20576e01a53 Mon Sep 17 00:00:00 2001 From: Vincent Donnefort Date: Wed, 26 Oct 2022 12:42:51 +0100 Subject: [PATCH] ANDROID: KVM: arm64: Add protected_shared_mem statistic When using nVHE in protected mode, protected memory can be between host and a guest. Tracking this value is interesting from a debug perspective, to identify potential leaks. Keeping the count of memory sharing is easy, each share/unshare will return to the host where the accounting will take place. Bug: 222044477 Change-Id: I43dcd258789f79dbfe489e5bf721e606c5e6e022 Signed-off-by: Vincent Donnefort Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e3b13f4967e0..a5aece891f59 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -824,6 +824,7 @@ static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg) struct kvm_vm_stat { struct kvm_vm_stat_generic generic; atomic64_t protected_hyp_mem; + atomic64_t protected_shared_mem; }; struct kvm_vcpu_stat { diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index ee2bd0c0ad90..89cb03e2ed06 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -31,6 +31,7 @@ const struct _kvm_stats_desc kvm_vm_stats_desc[] = { KVM_GENERIC_VM_STATS(), STATS_DESC_ICOUNTER(VM, protected_hyp_mem), + STATS_DESC_ICOUNTER(VM, protected_shared_mem), }; const struct kvm_stats_header kvm_vm_stats_header = { diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index a92428ce1912..fc64e8358928 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -83,6 +83,8 @@ static bool kvm_hvc_call_default_allowed(u32 func_id) */ case ARM_SMCCC_VERSION_FUNC_ID: case ARM_SMCCC_ARCH_FEATURES_FUNC_ID: + case ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID: + case ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID: return true; default: /* PSCI 0.2 and up is in the 0:0x1f range */ @@ -224,6 +226,16 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID: kvm_ptp_get_time(vcpu, val); break; + case ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID: + case ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID: + if (!kvm_vm_is_protected(vcpu->kvm)) + break; + atomic64_add( + func_id == ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID ? + PAGE_SIZE : -PAGE_SIZE, + &vcpu->kvm->stat.protected_shared_mem); + val[0] = SMCCC_RET_SUCCESS; + break; case ARM_SMCCC_VENDOR_HYP_KVM_MEM_RELINQUISH_FUNC_ID: pkvm_host_reclaim_page(vcpu->kvm, smccc_get_arg1(vcpu)); val[0] = SMCCC_RET_SUCCESS;