From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796333731; b=Rk0rg91HWMHy+ZY4TJmn7aesj3d9NvdGGEHlmyWhPMMIq21fKNv3b6hwYQjrKgBmorIE 2KasrdSKiPCMerpZecHXHgaKnfZCXA4FpnItA2Y9nUZ2OTCqmqGy4LMxynYfDQAMUs7oi VFhwPKxpRNaHFsG9UpT7hGCryiZC9nr/iHphpSGuac0Ds3mdw0mdfwafvPAdvNRobwXi4 dlIp0yrz/Y2g7zh3D9u28Z9/0iUpVyuHqAdJWh+k4oFwCiL7KVJ6Ql3p4tTIY79MsA8Nb ffPVY4YopXxNn5z3ig44tmwDZ79DW5wnmSqI1/jlI3aCnK/PxByyVZe7FjSvRYq0ECQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796333731; 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=1GdFaybE+mWJztt5kk0enGfYjPq1bIJhi+tGxKUstzU=; b=lim6z7PumJBaRVEWv6IeVnHyYQ8XpQ3q9scZcxGGW6Vz+gSrbi08u+GYfwbRnKadLxhV kRb5kQA6BVjIgj+SL1xKBBFAwZy7f5zp71H738DEMeZmLi+MlnehFLcsQy/PwS0uT2B/e KGK94+tFlecbnZ6RS8KFl1CesjaLzod4jB/rkYkDvDyZ2Izj0/qxxSPQunYj32FfRh2vs qXgtMcruIjzZ4lbMVsfgIWGH+LogOa+XlY6k8BI0dUZRnfpFN9+1/X0tbLixiLjL/SDx3 9Zf5kYqbe5QZH/wK+KfvZbUck5MFjC+Ijc6ZUQRxEAQ9MXzNC4jgdjXIT+oHp2GOmKg== 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=FfFl7sqo; arc=none smtp.remote-ip=209.85.210.70 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (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 E54B6606A332 for ; Thu, 3 Dec 2026 21:35:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E54B6606A332 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=FfFl7sqo Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-8615bdcc5cfso463234a34.3 for ; Thu, 03 Dec 2026 13:35:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796333730; x=1796938530; 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=1GdFaybE+mWJztt5kk0enGfYjPq1bIJhi+tGxKUstzU=; b=FfFl7sqoExbrKdT7WB4EdZi86TObVbkVM7GIYdsRzphUNupLIbeF9pxNrdwvYWEX4M B16UeuFgCaSQ2pUjTwlwWm+scOsaMz4E3PKMVFCmvAwswlXxbBLYLAWw/SnVG8dVoZ7r IEz8cpUm0zqp8Hwxw83+kcJJxwLaGhLNeh1+c6frpEJJ/AuNkjuWA7uxn6pmWhpORheO AgsJqFT04CTO8JutSfFCZtSKTmv4C3SumNUV9ugikMGXzDQaVloVnQw+oRCxhcN9L36v KoD55sYJqwUWz/oThMK3TMP9ZOfjvnyZRdwwxuCuBODrsU3RqhAwui/C/1Udu6g5J5Uu NW3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796333730; x=1796938530; 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=1GdFaybE+mWJztt5kk0enGfYjPq1bIJhi+tGxKUstzU=; b=yCcIKDX1WyXb0Du59MCwc7963jiPpvPE8WFkA5mxlu3OBl6s1Xl+bf6FyaDDcJcBhJ 03mFYZngQd3XUIa/Wv/e2a+mIFteBHh1Su1HxW96Wk10OpUZQohfaUq78v6MiTo3obTO 302rTh/dYTS3sg6j08hdPXYCAuD6JSgEmvK2pIINOeQcKTNdMqWAX/wLIRoFv0AEK3s2 y8KFN8h3wI60vy42kb9ri0JPt72R+vKnQksvsvVy8EuUyL10a7mpYSbjnWMpCa5oyfJ2 A71lcVXfr/VGt2fCmYoneSaH21h+oJCpjkgs1BXb73zPjK7tPCZx3r1vK8TFXOIKX36v iGGA== X-Forwarded-Encrypted: i=1; AL+hErGkPHqCDJrngehgghLMvWABvovqiv2AU+pxzHFGr37Jj5tLTqCpSfHxmEnIwUNUHZuPN0+An2+VAvtm/gk=@vger.kernel.org X-Gm-Message-State: APuU4Z8uKnoBZ0ohk2MFaKvSR3e+Rs2yYX59ArHc+4XhiWxDuhx5+IyV Bnrk7gCoRdUMMLiT6xLA8wjD9/JQBDCaZfpn5ZYdcXXO0DKtiXwMdVnByEICBK+cxyNT1b5mP/Y nyZZgLVueolTHQJinGqpqCRWUzw== X-Received: from iobcg20.prod.google.com ([2002:a05:6602:2554:b0:9ea:224b:fb62]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:c44b:10b0:71a:f900:5348 with SMTP id 006d021491bc7-71b394816ddmr25826974eaf.32.1796333729661; Thu, 03 Dec 2026 13:35:29 -0800 (PST) Date: Thu, 3 Dec 2026 21:35:29 +0000 In-Reply-To: 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> <20261119185215.632334-17-coltonlewis@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261203213529.2655236-1-coltonlewis@google.com> Subject: Re: [PATCH v10 16/33] KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU From: Colton Lewis To: Oliver Upton Cc: Colton Lewis , Marc Zyngier , 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" Oliver Upton writes: > On Thu, Nov 19, 2026 at 06:51:58PM +0000, Colton Lewis wrote: >> + if (cpumask_test_cpu(smp_processor_id(), >> + vcpu->kvm->arch.supported_cpus) && >> + nr_guest_cntr < nr_host_cntr && >> + (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) { > > nr_pmu_counters <= *host_data_ptr(nr_event_counters) should be a > precondition of running on a supported CPU... IOW, the number of guest > counters must already be supported by the selected PMU implementation. Ack, KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and kvm_arm_pmu_v3_init() already enforce that guest HPMN is strictly less than nr_event_counters on supported_cpus, so I'll turn that into a KVM_BUG_ON() in v11 instead of silently falling back. >> + u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, read_sysreg(mdcr_el2)); >> + bool hpme; >> + >> + if (hpmn < nr_host_cntr) >> + hpme = !!(read_sysreg(mdcr_el2) & MDCR_EL2_HPME); >> + else >> + hpme = !!(read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_E); > > This bit is extremely confusing. MDCR_EL2.HPME needs to always follow > the host enable/disable of the PMU. We already have the PMU driver > publish information into KVM for percpu state, this might be another > example of where that makes sense. > > It shouldn't ever be the case that on vcpu_load() the canonical bit for > the host PMU is MDCR_EL2.HPME. Agreed. >> + mdcr |= (MDCR_EL2_HPMD | MDCR_EL2_HCCD); >> + if (is_pmuv3p5(read_pmuver())) > > read_pmuver() reads the ID register for every invocation which will be a > trap to the L0 hypervisor every time under nested. If you need this sort > of percpu state, set it up in kvm_init_host_debug_data(). Good catch. kvm_init_host_debug_data() already extracts pmuver from ID_AA64DFR0_EL1, so I'll record a host_data flag there. >> + if (is_nested_ctxt(vcpu)) { >> *mdcr |= (guest_mdcr & NV_MDCR_GUEST_INCLUDE); >> + if (kvm_pmu_is_partitioned(vcpu->kvm) && >> + kvm_pmu_hyp_counter_mask(vcpu)) >> + *mdcr |= MDCR_EL2_TPM | MDCR_EL2_TPMCR; > > This could use a comment, because there's a rather load bearing detail > here: you're emulating the effects of HPMN because the architecture > doesn't nest. Will add a comment in v11. >> + if (kvm_pmu_is_partitioned(vcpu->kvm) && >> + (__vcpu_sys_reg(vcpu, PMUSERENR_EL0) & >> + ARMV8_PMU_USERENR_MASK)) { >> + *mdcr |= guest_mdcr & (MDCR_EL2_TPM | MDCR_EL2_TPMCR); >> + if (kvm_pmu_hyp_counter_mask(vcpu)) >> + *mdcr |= MDCR_EL2_TPM | MDCR_EL2_TPMCR; > > Why do we need to do this in a hyp context? is_hyp_ctxt() covers L1's userspace (EL0 with HCR_EL2.TGE=1) as well as vEL2, and L1 can switch between them without re-running kvm_arm_setup_mdcr_el2() (via kvm_hyp_handle_eret() or FEAT_NV3 on ERET, and natively on SVC). MDCR_EL2.{TPM,TPMCR,HPMN} still apply to L1's userspace (BEHAVE_FORWARD_IN_HOST_EL0). Emulated PMU leaves physical TPM and TPMCR set unconditionally, while Partitioned PMU clears them on FEAT_FGT. If L1 leaves PMUSERENR_EL0 zero, hardware traps EL0 PMU accesses directly to L1 vEL2 so physical TPM/TPMCR can stay clear. Once L1 enables EL0 access in PMUSERENR_EL0, L0 needs to re-enable physical TPM/TPMCR when L1 sets virtual TPM/TPMCR or sub-partitions HPMN, otherwise L1's userspace would bypass L1's traps and access counters >= L1's HPMN directly. I'll add a comment explaining this.