From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 176734B1275 for ; Thu, 24 Sep 2026 17:29:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790270992; cv=none; b=mo6dPfRdH9KkPLA+tdt0WJTyoqwRP16DUA/p5Bgy8qM4vEoFYXbHkSPW+AE6ecUBdKk6Jf3ZDqUZ7QQf4stdlSf5LmUuG/rhXAbVGbtShkZLCybrtHNkka17rN0cz0gsUitVNig/s5xKLa/7Y0EIadFaI/vjnIFhBFYeEIzD/9c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790270992; c=relaxed/simple; bh=Y6adSMCNNnG6doPX3ymtb78tuo7YkMMcjAEG5UNk6Mk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pp7NwsSoAp/KjIrSM7ATf1pTq68RBuYQ4pizKEhS80jP7M624sxl+MhTKaomVqUne5DlEHtaB/gyxYB7q0MshfTk1yDTVr2GJdBradjQ5ZB72B28I/Tx50Lk4DvNslaMHg+0gglEAvuerrU5bvQlWitrpVCcJLWNXMp/OhMs1dA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tduBl/MO; arc=none smtp.client-ip=209.85.167.198 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.b="tduBl/MO" Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-4b597558c4cso212560b6e.1 for ; Thu, 24 Sep 2026 10:29:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790270987; x=1790875787; 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=O/Sjq5h51RyI3GHB4u4jBTaGi29xukntF3qhviZ3NXg=; b=tduBl/MOfOfagDy2icDkaoFNCLVSHj5lNVPv/kQLdptKdQqBVAxb2caubTYX85ZVXq 0GNr/Fxr1n9ltJuCfnHkvmmFMZsulU1Whx9jN5ND+UQRTv7LFKn2HHVRqSzALtikB4wz S1YMPOs+gX4OsjAWrhlE5k1rsYFH5KBQBEkn00KpU0ErTgB4iCLco00ML8DzYFXCF1+c Svo5KJLPBl12uJP3wywS7L+4d3PnF1dCnpfifaSEu46m+MnOREU/Q9o6gR+XeUfrILwA Yhaj6OiWDaUrM2LzZfpQ5i61WpoyeMNFI1s6ZUKY3jh9mbMhMpCbNztHRwyDqbbb/Vi2 Sjlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790270987; x=1790875787; 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=O/Sjq5h51RyI3GHB4u4jBTaGi29xukntF3qhviZ3NXg=; b=xp5NOYTL7UirI0FQjMJp3HufJzYz0KioauOg9Lwwr7Qm2iClfUZsExXDAtXD8bNQ0O dQR040iP0MqNzHyn0kkr1hKAuJTy/l13aCzzdaLYKZFs3jNuzIDrQ/AYzezJ6RRj2Qt7 tcX3dWgGEZL06CsvFtvmaQxOr3aU1IJr0IDVCVWOmDSGc1EYauPqKJ+y0KOTsilO1X6a tjZ8RbQFVqrxoJ/op05I+1WDvcflElLia8LV43eLQu9xMRSsT/ClrLtIrL9jQJupPlC3 Ee0yKwiKJLcgzimaOWS+zJR/IsmmWTMHQD06sPt4N464ZJNmKVpvygVRPGajfgesAjSg LLcw== X-Forwarded-Encrypted: i=1; AKwUvBzGzgVigXzoTrfbgmlRaXTodEprX5QdkX3SrXL9omk3YAuaCwcgvH/7h3GcVTs1F41/QkiZs4kzuBo=@vger.kernel.org X-Gm-Message-State: AFuF++ngwx6bdc1QZgnc7+PPTiryz5XRzgI1wOxIguwG4V3gHmTG0Z37 y1LTrGK2MIPT2+/l78m2A/Jr4JfAi3DblFb0Kj37GC9fmYZRKKDcGFeq8Tq3VBN74KqLH9Afn02 SeondyZXinikocDayzCGgnzPVaw== X-Received: from ilbda5.prod.google.com ([2002:a05:6e02:3cc5:b0:509:8793:a27a]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:2222:b0:4d6:9133:23fa with SMTP id 5614622812f47-4d72b43c64emr3766576b6e.48.1790270986738; Thu, 24 Sep 2026 10:29:46 -0700 (PDT) Date: Thu, 24 Sep 2026 17:29:17 +0000 In-Reply-To: <20260924172928.2110956-1-coltonlewis@google.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260924172928.2110956-1-coltonlewis@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20260924172928.2110956-12-coltonlewis@google.com> Subject: [PATCH v9 11/22] KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU From: Colton Lewis To: kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: Marc Zyngier , Oliver Upton , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Fuad Tabba , Catalin Marinas , Will Deacon , Mark Rutland , Paolo Bonzini , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , James Clark , Robin Murphy , Zide Chen , Alexandru Elisei , Ganapatrao Kulkarni , Mingwei Zhang , Jonathan Corbet , Russell King , Shuah Khan , linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Colton Lewis Content-Type: text/plain; charset="UTF-8" Set up MDCR_EL2 to handle a Partitioned PMU. If partitioned, set the HPME, HPMD, and HCCD bits. If we have the ability to use Fine Grain Traps (FEAT_FGT) also, unset the TPM and TPMCR bits that trap all PMU accesses and set HPMN to the correct number of guest counters so hardware enforces the right values. Protect vcpu->arch.mdcr_el2 updates with local_irq_save() to prevent races against PMU interrupt handlers modifying MDCR_EL2.HPME. Signed-off-by: Colton Lewis --- arch/arm64/kvm/debug.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c index f4d7b12045e8f..a722fd4594e09 100644 --- a/arch/arm64/kvm/debug.c +++ b/arch/arm64/kvm/debug.c @@ -37,14 +37,16 @@ static int cpu_has_spe(u64 dfr0) */ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) { - preempt_disable(); + unsigned long flags; + + local_irq_save(flags); /* * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK * to disable guest access to the profiling and trace buffers */ - vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN, - *host_data_ptr(nr_event_counters)); + + vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN, *host_data_ptr(nr_event_counters)); vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM | MDCR_EL2_TPMS | MDCR_EL2_TTRF | @@ -52,6 +54,35 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) MDCR_EL2_TDRA | MDCR_EL2_TDOSA); + if (kvm_pmu_is_partitioned(vcpu->kvm)) { + u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters; + u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, read_sysreg(mdcr_el2)); + bool hpme; + + if (hpmn < *host_data_ptr(nr_event_counters)) + hpme = !!(read_sysreg(mdcr_el2) & MDCR_EL2_HPME); + else + hpme = !!(read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_E); + + vcpu->arch.mdcr_el2 |= (MDCR_EL2_HPMD | MDCR_EL2_HCCD); + if (hpme) + vcpu->arch.mdcr_el2 |= MDCR_EL2_HPME; + + /* + * Take out the coarse grain traps if we are using + * fine grain traps and enforce counter access with + * HPMN. + */ + if (!vcpu_on_unsupported_cpu(vcpu) && + (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) { + vcpu->arch.mdcr_el2 &= ~MDCR_EL2_HPMN; + vcpu->arch.mdcr_el2 |= FIELD_PREP(MDCR_EL2_HPMN, nr_guest_cntr); + + if (cpus_have_final_cap(ARM64_HAS_FGT)) + vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM | MDCR_EL2_TPMCR); + } + } + /* Is the VM being debugged by userspace? */ if (vcpu->guest_debug) /* Route all software debug exceptions to EL2 */ @@ -70,7 +101,7 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) if (has_vhe()) write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2); - preempt_enable(); + local_irq_restore(flags); } void kvm_init_host_debug_data(void) -- 2.56.0.rc1.310.g51773c2048-goog