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 <vdonnefort@google.com>
Signed-off-by: Quentin Perret <qperret@google.com>
This commit is contained in:
Vincent Donnefort
2022-10-26 12:42:51 +01:00
committed by Quentin Perret
parent 781b6882ba
commit b97d00514a
3 changed files with 14 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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 = {

View File

@@ -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;