From patchwork Tue Jan 17 01:35:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 13104016 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2D80EC46467 for ; Tue, 17 Jan 2023 01:38:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=aF68M8WRDCbmCbGQxbWX2hP2iwErtvTLhbyaoDcuZ74=; b=ArVW9zOaRxbRhhUc1f3jBAvtT2 PAOOZRDSGhXgfmCk0lIPafizXXXy+mq2Zr4XJoo3X6Iq6ev3ZTYZTqBF0DadcfRabl1OUvJ7UZf46 x4QWYl30PX71nH2AUKkLPHSXixtizZQS1XNKqR+qANqvaQIB3eHwhbfy6nSqWJeBYzlGMw9656Hz1 mPmr44THE5AGCfGLoP3INZBxVhF74uso/FvcazS4ObiV2hCt0lOBUA0ZdzNK75hNznItbXSxWzfUS E7+uKvbhZbFxOh+tRwUsauhf72ThFWxu3kUz/4WgG7hbxbQKvkXLuacBZMQs79+8nvv14KCd6DUuP dtRwbtRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHauf-00CUxM-FT; Tue, 17 Jan 2023 01:37:21 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pHauc-00CUwW-GB for linux-arm-kernel@lists.infradead.org; Tue, 17 Jan 2023 01:37:19 +0000 Received: by mail-pj1-x104a.google.com with SMTP id t12-20020a17090a3e4c00b00225cb4e761cso13649119pjm.3 for ; Mon, 16 Jan 2023 17:37:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R/yQ6qOJ9nE5sbZwT5+Hdlb+OS4oscumD9F8iuYsEP0=; b=BIanP+6o+DalVgd3npztrKDyle5vEuGPPH2y24H9sG6gr8WHAqWrGxjc4qJOF3b7Pe pZ0ocJObA8sR1zKG3b4dtjvur3wtGP/F+RP81Lu2vmg/KJG3A5cY61EclFYbzoZmAD9R 2t4xMqS2xPUWqi2ecaKApshFrbENbCi/23GuUYaeL+AQrZrLlr8C9X+eg1PV68Zzp/rJ FrpWTAt69byVLoVLigmqkOdQfbZOb83t1hj2H4gRXV+CPU32Jwi2Sv7wPPlbpjfe7N/r ZshXJAWwiJybZsnx4UMyJrAPpFVwfdEa+QeblTavPTbowIcGOkJ0WFEYxuUoeS2bNEq2 3cOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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; bh=R/yQ6qOJ9nE5sbZwT5+Hdlb+OS4oscumD9F8iuYsEP0=; b=yT46rDzvIpiyp7EoLMPIQP+uK33DNQE6kpZKfCTh9omhgihVxBlyed+TFSx+oO/2xF zoh0GjOjHXJlt9/CZxdi6LvlvP/1ipfYy+kksVF0AwTMotvRHeeFt29BbjUstZqPIL1J OuwzZIFbMjefqnrg6fVVSTkMa4j1JFUWUfVtU6BtjPK2pvD7830h++305yLbfWzwaEsS b00KXyt0y5Z+PS3FRbnzuWf9aGrrJoSqhPHcYO+p0dIzmWmpiKoJxAUl9/F9TNRC9zMU ybFyDIEUE26UMqvOgWAiHoeZZD5S5gxD2aaFbtHeyuUqyQXta5mogIHHPR9hkVLPddMo gF8w== X-Gm-Message-State: AFqh2kqdE0tYGOqSPmuKdEZU4Vzkjx8BROBYeyw7K1+DSUbPvReKIf44 LNeJW0P47lzSJ9/tEF/GaTrtAN9KwVo= X-Google-Smtp-Source: AMrXdXu1BwvGS43nNucWc8njSPNaqgCGpTv5cPH0A8yykvGIC9UMKkE3HnsLwdKjNbWczW0pdOjTVkd832g= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a05:6a00:1401:b0:58b:bf1f:e213 with SMTP id l1-20020a056a00140100b0058bbf1fe213mr110981pfu.50.1673919436912; Mon, 16 Jan 2023 17:37:16 -0800 (PST) Date: Mon, 16 Jan 2023 17:35:37 -0800 In-Reply-To: <20230117013542.371944-1-reijiw@google.com> Mime-Version: 1.0 References: <20230117013542.371944-1-reijiw@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230117013542.371944-4-reijiw@google.com> Subject: [PATCH v2 3/8] KVM: arm64: PMU: Preserve vCPU's PMCR_EL0.N value on vCPU reset From: Reiji Watanabe To: Marc Zyngier , kvmarm@lists.linux.dev Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Alexandru Elisei , Zenghui Yu , Suzuki K Poulose , Paolo Bonzini , Ricardo Koller , Oliver Upton , Jing Zhang , Raghavendra Rao Anata , Reiji Watanabe X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230116_173718_560383_B999C7CA X-CRM114-Status: GOOD ( 15.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The number of PMU event counters is indicated in PMCR_EL0.N. For a vCPU with PMUv3 configured, its value will be the same as the host value by default. Userspace can set PMCR_EL0.N for the vCPU to a lower value than the host value using KVM_SET_ONE_REG. However, it is practically unsupported, as reset_pmcr() resets PMCR_EL0.N to the host value on vCPU reset. Change reset_pmcr() to preserve the vCPU's PMCR_EL0.N value on vCPU reset so that userspace can limit the number of the PMU event counter on the vCPU. Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/pmu-emul.c | 6 ++++++ arch/arm64/kvm/sys_regs.c | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 24908400e190..937a272b00a5 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -213,6 +213,12 @@ void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) pmu->pmc[i].idx = i; + + /* + * Initialize PMCR_EL0 for the vCPU with the host value so that + * the value is available at the very first vCPU reset. + */ + __vcpu_sys_reg(vcpu, PMCR_EL0) = read_sysreg(pmcr_el0); } /** diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 4959658b502c..67c1bd39b478 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -637,8 +637,10 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) if (!kvm_arm_support_pmu_v3()) return; + /* PMCR_EL0 for the vCPU is set to the host value at vCPU creation. */ + /* Only preserve PMCR_EL0.N, and reset the rest to 0 */ - pmcr = read_sysreg(pmcr_el0) & (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT); + pmcr = __vcpu_sys_reg(vcpu, r->reg) & (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT); if (!kvm_supports_32bit_el0()) pmcr |= ARMV8_PMU_PMCR_LC;