From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 08E5E3FE37C for ; Mon, 11 May 2026 14:49:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778510983; cv=none; b=OCDOJGEtYCFfaPWrUGVnienRyrZSM74F4jUmdlNlWziEJxcJJOuqzAWb7o/pai2uTpZWhXgGZlwele61ZeHcTtZy3jCx88lKLpYDtAeZGNeEWTvr1O9l6zBuB/2syfJDDgrXohhOjcJ22hs3ZMFR0n5EDD1i/1XuF1AFMinZkzY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778510983; c=relaxed/simple; bh=gMZcBmHoLH5063KYF9NKsy9Tpdrk0FyoPinO83KeiGA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=cfK1ZkzaoSnP/weDNGbORhKnak6gh6wtf7zx8B9nYSWUPEig146svAOMVlkN171sKWkU8MV91wu3ToYjtIzokOhZA8JEkadSRVJh7pQL/t0yyJN7FGdBv63U3EnMZ2z4mKDZWYuMN5seTXc2LPp8mUEyA8W0daTLAU+Im7hCLiU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=ViGST4HI; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ViGST4HI" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-44a5174670eso2466247f8f.1 for ; Mon, 11 May 2026 07:49:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1778510979; x=1779115779; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=6uflrulgEFIn9x44XDb2ne6l2kjOQ5bKLrwMyk01QUg=; b=ViGST4HIk3bqHQpVU1NidNJgHUA7QR/K7zLAxKPbGDovJXBv6frHXSZSafPu3XfoN5 eVcUWw+NOOzJJ0TWepVR+XhsG+hVDlgynlhz1GDLKwwJVITQjkjQpmt698kFrNTh0Rom p5PumOiWWzMLdPFsgmsDagw2TMsDOxghxUrJ70SCfd76lQ1n0O+GS7HdWrpM9WcBocuH SSqYSsK8SEcox/L/J2IrFTW77UoPSH51yr7wUagNTM/jaBue0c5KuuhhKsjeZty18ahW 2fBYVX8iyj5cwLCnDvSocnjAZpZ/s40w+G7KYWQDQGDZ4gNHTR1XxTMTq58VIkLIEpwK 9hPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778510979; x=1779115779; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6uflrulgEFIn9x44XDb2ne6l2kjOQ5bKLrwMyk01QUg=; b=Dro5z/HtgDIP/81r1T0Ksh/af2z6BdknIFz2fgqx4UgLhMWLMsgTN/ezO22WboynsZ essMC3SUczvJgNICTNtFDcZNptGtFtDaUARjFeiK+myrC5zO04yKGhvpUJY3EhfNHrdA 9lE6z0atS6zamvdOad8pd3OA8ryVobEkCgsN8IghZybyYIVNl5nKjdzuWrPE/yd6PO6B Ne1lOkYBgZ75KClJxYg+7hGoKGRy1FgioCNGGNH7h9fz/Pr0W2NF4ZvFMzO/jCz7dnoy 4i4sA4GZyRUZDa7uty7+eJ5XdHfn5imbQ4QdGLOMvKkAqNsxnb2hDcv9MrYGGnK1bitf BlYw== X-Forwarded-Encrypted: i=1; AFNElJ9QS3FCjsqO9pRghaYnaik3Ioiq0dTyHeuS5U87xUXU4sD51HSoP4W981rCtPZgFM3M31xRwxuLnkCu3OA=@vger.kernel.org X-Gm-Message-State: AOJu0Yz77YY1vsUSrg7ZLBUef4fMgZJHWf078vGcrKIasXiH0Yrnks0M T0o5KlF56bQ6xrxCQ1Cr5hzbcQbHiTUvcCZ+SNOdR9Ir7QC6ItlQTWx4pS8ASmj2eEA= X-Gm-Gg: Acq92OEclVU1STfHQ86i0G5szei0S/bEja78GFKaS1N/HOoLFo0Tf4rq4jARm6pBHJe Qub9e+c433AtYEWYe/dHz2ATCItKEx1Qpmo0sryNgZFFRKRJa3xQ8L1QYwk0clD0U73pn+oD2T9 ektyTTfIb+MW3NuBtGmh+zmtXc+yroRdQ8EYdJvPjvwRC3PPZ/SF/DYmtI29TF0f+gjlnD41tmW Cn3oCd+jqY6R9/y4xojxSGAUC+J+A5oV1WarjLUSMg0uSTPO3AOngk8Xv25xM+FOXgmobXJBFYT Bm9BtZgC5CFDrarumwKfeHIDznvAmCq52KdWv879ktoBqXf8S/EApLIzfzkFlJnQ0HFTVdqv9xQ OSU9ibI4PNGa3JZvAbIbLA4hRxXc8rp8mFmBVOU0U+ao/GHRTVM6byxBr3T4sFV46ceLlphqGz7 V/NxJTGPdrrLM4WZY0T/w9u1RtNeenOcR9vDos0NDltxju2jqK6A== X-Received: by 2002:a05:600c:33a9:b0:487:2439:b7c8 with SMTP id 5b1f17b1804b1-48e51e0bb9amr250632005e9.1.1778510979404; Mon, 11 May 2026 07:49:39 -0700 (PDT) Received: from [192.168.1.3] ([185.48.77.170]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e702e5516sm293146665e9.7.2026.05.11.07.49.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 May 2026 07:49:38 -0700 (PDT) Message-ID: Date: Mon, 11 May 2026 15:49:37 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v7 10/20] KVM: arm64: Context swap Partitioned PMU guest registers To: Colton Lewis Cc: Alexandru Elisei , Paolo Bonzini , Jonathan Corbet , Russell King , Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Mingwei Zhang , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Mark Rutland , Shuah Khan , Ganapatrao Kulkarni , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, kvm@vger.kernel.org References: <20260504211813.1804997-1-coltonlewis@google.com> <20260504211813.1804997-11-coltonlewis@google.com> Content-Language: en-US From: James Clark In-Reply-To: <20260504211813.1804997-11-coltonlewis@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 04/05/2026 10:18 pm, Colton Lewis wrote: > Save and restore newly untrapped registers that can be directly > accessed by the guest when the PMU is partitioned. > > * PMEVCNTRn_EL0 > * PMCCNTR_EL0 > * PMSELR_EL0 > * PMCR_EL0 > * PMCNTEN_EL0 > * PMINTEN_EL1 > > If we know we are not partitioned (that is, using the emulated vPMU), > then return immediately. A later patch will make this lazy so the > context swaps don't happen unless the guest has accessed the PMU. > > PMEVTYPER is handled in a following patch since we must apply the KVM > event filter before writing values to hardware. > > PMOVS guest counters are cleared to avoid the possibility of > generating spurious interrupts when PMINTEN is written. This is fine > because the virtual register for PMOVS is always the canonical value. > > Signed-off-by: Colton Lewis > --- > arch/arm/include/asm/arm_pmuv3.h | 4 + > arch/arm64/kvm/arm.c | 2 + > arch/arm64/kvm/pmu-direct.c | 169 +++++++++++++++++++++++++++++++ > include/kvm/arm_pmu.h | 16 +++ > 4 files changed, 191 insertions(+) > > diff --git a/arch/arm/include/asm/arm_pmuv3.h b/arch/arm/include/asm/arm_pmuv3.h > index 42d62aa48d0a6..eebc89bdab7a1 100644 > --- a/arch/arm/include/asm/arm_pmuv3.h > +++ b/arch/arm/include/asm/arm_pmuv3.h > @@ -235,6 +235,10 @@ static inline bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) > { > return false; > } > +static inline u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu) > +{ > + return ~0; > +} > > /* PMU Version in DFR Register */ > #define ARMV8_PMU_DFR_VER_NI 0 > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 410ffd41fd73a..a942f2bc13fc4 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -680,6 +680,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > kvm_vcpu_load_vhe(vcpu); > kvm_arch_vcpu_load_fp(vcpu); > kvm_vcpu_pmu_restore_guest(vcpu); > + kvm_pmu_load(vcpu); > if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) > kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); > > @@ -721,6 +722,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) > kvm_timer_vcpu_put(vcpu); > kvm_vgic_put(vcpu); > kvm_vcpu_pmu_restore_host(vcpu); > + kvm_pmu_put(vcpu); > if (vcpu_has_nv(vcpu)) > kvm_vcpu_put_hw_mmu(vcpu); > kvm_arm_vmid_clear_active(); > diff --git a/arch/arm64/kvm/pmu-direct.c b/arch/arm64/kvm/pmu-direct.c > index 63ac72910e4b5..360d022d918d5 100644 > --- a/arch/arm64/kvm/pmu-direct.c > +++ b/arch/arm64/kvm/pmu-direct.c > @@ -9,6 +9,7 @@ > #include > > #include > +#include > > /** > * has_host_pmu_partition_support() - Determine if partitioning is possible > @@ -98,3 +99,171 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) > > return *host_data_ptr(nr_event_counters); > } > + > +/** > + * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters > + * @pmu: Pointer to arm_pmu struct > + * > + * Compute the bitmask that selects the host-reserved counters in the > + * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters > + * in HPMN..N > + * > + * Return: Bitmask > + */ > +u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu) > +{ > + u8 nr_counters = *host_data_ptr(nr_event_counters); > + > + if (kvm_pmu_is_partitioned(pmu)) > + return GENMASK(nr_counters - 1, pmu->max_guest_counters); > + > + return ARMV8_PMU_CNT_MASK_ALL; > +} > + > +/** > + * kvm_pmu_guest_counter_mask() - Compute bitmask of guest-reserved counters > + * @pmu: Pointer to arm_pmu struct > + * > + * Compute the bitmask that selects the guest-reserved counters in the > + * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters > + * in 0..HPMN and the cycle and instruction counters. > + * > + * Return: Bitmask > + */ > +u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu) > +{ > + if (kvm_pmu_is_partitioned(pmu)) > + return ARMV8_PMU_CNT_MASK_C | GENMASK(pmu->max_guest_counters - 1, 0); > + > + return 0; > +} Minor nit: slightly inconsistent use of types. Returns a u64 but doesn't use GENMASK_ULL and is also usually saved into a long when it's called.