From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795114348; b=sFOSTB0HzEdaLPbxeOHVDVomGcaBWcPBda81VR5iQAQvYY+fNvU79wSVHjTa5SYjyDr1 TH5acW4lewVWwqVPffEgV9vMBqFbr6vIXDMDMNb+Xu1LjJ6sSB9gK9tts5CHKF22mHRwQ SSw0aSQwT3Ig61WsU8BhLd1sKuIpt7I7so3omaGfvd1y7LA2A/nPBvrIVZrzSwQn3GbJI QGAxGZCGYhDFJHaV/4QfsIeP6EtlxQDjno+opeR5vQEZyHD0C3gt4f6XW+4ekdWftoNZz g+VzwuOIV/bIWceiMv4IdPloCTJqdcPH0Fqao/30OyOPR7QkdC+dUsKuqY0f5udwCJQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795114348; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=6347H7gggToJzZ+Ilw3z4KeuVm8ZnAfGsrXn9eqrzm0=; b=04L1roS/k2k3TUttMhWjeedi9P5QB5e9tkwAkEWQFnORR45qIwHFoFnEVlutcgLY1Aj/ VfOPWCkTPlIW1h7E2/VagcmV0JC80uIeTSFuiMkmrUxyYQEJMPT4rewuC/WpVIYB+5liB AlGSVrnB0Jr362E2SfKjyMARLth7bvY9l93RyRPy4K9WoRlAv3E7o8gAGvG3lNol+1bs0 MTqOoGzVgSYPCgdy6MBcQVdJkNAi2QF0tKblIbqIt9QekDUpMlnmpTWkhxUZ234o1e/Gi nObmdv4QZAAyBXIFsAb6qYawGQpVVYchFtr6UfJtORsL9ZafCbwmmFpw7uJ3uqfvbLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=eXbRoEop; arc=none smtp.remote-ip=209.85.167.200 Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7105606AAA5 for ; Thu, 19 Nov 2026 18:52:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C7105606AAA5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=eXbRoEop Received: by mail-oi1-f200.google.com with SMTP id 5614622812f47-4ab8b4887d4so145925b6e.1 for ; Thu, 19 Nov 2026 10:52:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795114347; x=1795719147; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6347H7gggToJzZ+Ilw3z4KeuVm8ZnAfGsrXn9eqrzm0=; b=eXbRoEopcpGI2+2eiRWItYB7cVvvGQaWF9q7H6B4I4ONHdqnpPqbwRC8O/QVsX2bxd ruxsiTksLeHrBKmKkqExkDdNlL9iZ6WunjkMZoTraoS99hwXQMUWGLvYIsqDkKkEUbt2 i5WiLfz74+X1jXyiPrH3hWY+uVy6mn0dOQq0I4qvWy6iXpZ/LHSkyVlQGyzNeb2Dik1F +9Ayy17qBpK2x/hOAbcdlbpo21L4hC8Hgjyyxa2bRRw6zWq8097PEm9u9E6EdIyrWLYz RwE3K3nl56UiqHr62xVm7WWEd9rWyqDB5am/6rxLaPjt9XVJC9z6Y/Sj5Ns2xCw/66ba OQ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795114347; x=1795719147; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6347H7gggToJzZ+Ilw3z4KeuVm8ZnAfGsrXn9eqrzm0=; b=qsGLjIz5WzniBZrvQAE/HLOp0WCSL90rMkeemgLOXQeTQThZEWfHMwR5hDfV2HQ7iD agKKL7vytskV49bVzAKsi81BUwmwf2KM4JtEPh/YSqffRDFzP6AKSaI5I/BLhLXdz/bY YYF6b5lOMWBzKrcS+wPSdHuVAbdbLV+gLAQBqytGcdKSekDgYqlANV+XSirROZLNEqK8 5BS/wZtVINqxTAKrPUt0YP5xfw3mfQaIw8WXkpnYvxlm8UT0nllzknxvPSIowRbNSdPG 0BO101MCJUCP15E/saa+QyxUzaRDh4Acte8JjN19LqhQZ6Ygi8yaSDQvJZcttnQ8urQc 83uw== X-Forwarded-Encrypted: i=1; AL+hErE0AyhO6TfgxJ5r0F++8NizGvyfr6FhaMH2q8zl260InIwRdawNh70VTKudbL67LelC3rmgX3sZtYsow/Y=@vger.kernel.org X-Gm-Message-State: APuU4Z/rjvOyyZLY6oJAsHseojAFk9LEmoypIaEQbUw0s95RkepVD2z7 ltBbxJLrs2QXAzzp7G2pOg3Zh488rPC3sRN5bb3uQxV2u+eCg4nUCJWGXK5WTK1dp/DAroa/h4Y EviIqdilyu6oo7LlEwCQ0RmvqiA== X-Received: from ilbeb8.prod.google.com ([2002:a05:6e02:4608:b0:544:45eb:2ab]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:660e:b0:4f4:b5ba:a241 with SMTP id 5614622812f47-57a0b048d65mr920500b6e.39.1795114346425; Thu, 19 Nov 2026 10:52:26 -0800 (PST) Date: Thu, 19 Nov 2026 18:51:53 +0000 In-Reply-To: <20261119185215.632334-1-coltonlewis@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261119185215.632334-1-coltonlewis@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261119185215.632334-12-coltonlewis@google.com> Subject: [PATCH v10 11/33] KVM: arm64: Add initial scaffolding for Partitioned PMU From: Colton Lewis To: Marc Zyngier , Oliver Upton Cc: Catalin Marinas , Will Deacon , Mark Rutland , Peter Zijlstra , Ingo Molnar , Paolo Bonzini , Russell King , Jonathan Corbet , Fuad Tabba , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Shuah Khan , James Clark , Alexandru Elisei , Mingwei Zhang , Ganapatrao Kulkarni , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Add the foundational helpers for Partitioned PMU support in arch/arm64/kvm/pmu-direct.c: - has_kvm_pmu_partition_support() to check VHE and PMUv3 availability. - kvm_pmu_partition_enable() and kvm_pmu_is_partitioned() to track per-VM partitioning state via KVM_ARCH_FLAG_PARTITION_PMU_ENABLED. - kvm_pmu_guest_counter_mask() to compute the bitmask of guest-reserved (0..HPMN-1 and PMCCNTR_EL0) hardware counters. Tested-by: James Clark Signed-off-by: Colton Lewis --- arch/arm/include/asm/arm_pmuv3.h | 4 ++ arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/pmu-direct.c | 94 +++++++++++++++++++++++++++++++ include/kvm/arm_pmu.h | 20 +++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kvm/pmu-direct.c diff --git a/arch/arm/include/asm/arm_pmuv3.h b/arch/arm/include/asm/arm_pmuv3.h index ecfede0c03486..71cf03ab58cbb 100644 --- a/arch/arm/include/asm/arm_pmuv3.h +++ b/arch/arm/include/asm/arm_pmuv3.h @@ -227,6 +227,10 @@ static inline bool kvm_set_pmuserenr(u64 val) } static inline void kvm_vcpu_pmu_resync_el0(void) {} +static inline u64 kvm_pmu_guest_counter_mask(void) +{ + return 0; +} /* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0 diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 4683281fcc5d4..89683a690828e 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -96,6 +96,8 @@ enum { KVM_ARCH_FLAG_EXIT_SEA, /* No further ID register changes possible */ KVM_ARCH_FLAG_ID_REGS_FINAL, + /* Partitioned PMU Enabled */ + KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, }; struct vcpu_reset_state { diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 7aaeeb84e788e..79f1602299767 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -27,7 +27,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-v5.o vgic/vgic-v5-tables.o vgic/vgic-irs-v5.o \ vgic-sys-reg-v5.o -kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o +kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu-direct.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o diff --git a/arch/arm64/kvm/pmu-direct.c b/arch/arm64/kvm/pmu-direct.c new file mode 100644 index 0000000000000..b1c180ae79d9d --- /dev/null +++ b/arch/arm64/kvm/pmu-direct.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Google LLC + * Author: Colton Lewis + */ + +#include +#include +#include + +#include +#include +#include + +/** + * has_host_pmu_partition_support() - Determine if partitioning is possible + * + * Partitioning is only supported in VHE mode with PMUv3 + * + * Return: True if partitioning is possible, false otherwise + */ +static bool has_host_pmu_partition_support(void) +{ + return has_vhe() && + system_supports_pmuv3(); +} + +/** + * has_kvm_pmu_partition_support() - If we can enable/disable partition + * + * Return: true if allowed, false otherwise. + */ +bool has_kvm_pmu_partition_support(void) +{ + return has_host_pmu_partition_support() && + kvm_supports_guest_pmuv3(); +} + +/** + * kvm_pmu_partition_enable() - Enable/disable partition flag + * @kvm: Pointer to kvm struct + * @enable: Whether to enable or disable + * + * If we want to enable the partition, the guest is free to grab + * hardware by accessing PMU registers. Otherwise, the host maintains + * control. + */ +void kvm_pmu_partition_enable(struct kvm *kvm, bool enable) +{ + if (enable) + set_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags); + else + clear_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags); +} + +/** + * kvm_pmu_is_partitioned() - Determine if KVM has a partitioned PMU + * @kvm: Pointer to kvm struct + * + * Return: True if the KVM PMU is partitioned, false otherwise + */ +bool kvm_pmu_is_partitioned(struct kvm *kvm) +{ + return kvm && test_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags); +} + +static u64 kvm_vcpu_pmu_guest_counter_mask(struct kvm_vcpu *vcpu) +{ + if (vcpu && kvm_pmu_is_partitioned(vcpu->kvm)) { + u64 mask = ARMV8_PMU_CNT_MASK_C; + unsigned int guest_counters = vcpu->kvm->arch.nr_pmu_counters; + + if (guest_counters > 0) + mask |= GENMASK_ULL(guest_counters - 1, 0); + + return mask; + } + + return 0; +} + +/** + * kvm_pmu_guest_counter_mask() - Compute bitmask of guest-reserved counters + * + * Compute the bitmask that selects the guest-reserved counters in the + * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the event counters + * in 0..HPMN-1 and the cycle counter (PMCCNTR_EL0). + * + * Return: Bitmask + */ +u64 kvm_pmu_guest_counter_mask(void) +{ + return kvm_vcpu_pmu_guest_counter_mask(kvm_get_running_vcpu()); +} diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 8d51cf40182d6..e06255e46a4d8 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -95,6 +95,10 @@ void kvm_vcpu_pmu_resync_el0(void); #define kvm_vcpu_has_pmuv3_strict(vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3_STRICT)) +bool has_kvm_pmu_partition_support(void); +void kvm_pmu_partition_enable(struct kvm *kvm, bool enable); +bool kvm_pmu_is_partitioned(struct kvm *kvm); +u64 kvm_pmu_guest_counter_mask(void); /* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure @@ -129,6 +133,10 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, { return 0; } +static inline bool kvm_pmu_is_partitioned(struct kvm *kvm) +{ + return false; +} static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, @@ -233,6 +241,18 @@ static inline bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int id static inline void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu) {} +static inline u64 kvm_pmu_guest_counter_mask(void) +{ + return 0; +} + +static inline bool has_kvm_pmu_partition_support(void) +{ + return false; +} + +static inline void kvm_pmu_partition_enable(struct kvm *kvm, bool enable) {} + #endif #endif -- 2.56.1.708.g77570a6a1d-goog