From patchwork Mon Mar 13 03:32:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 13171931 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 9918AC61DA4 for ; Mon, 13 Mar 2023 03:33:29 +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: Mime-Version:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=N8BoljeS6UIZTCyWfjFn5RTOt8b7eeNfUrYNyoWyCxc=; b=flJ S7oTxZogjMgN65b69dq/XKWCLx1JNTfnoD8AqGRWxHLfyZnhGH3mHtxiG+AIIV3iR2ba2Im5q5UC1 2plVRHFbADCv2J4gUCC38vd4es62uFMcEu84RhZzmA7/UpMD1oyQr9vaRQG5mNqL86Fez5N0BrD6S 7puAK1EwU10STz+66Q7MR0i5JlfgmT9o+RhfpifnDt8G9WKYQc9WY3ZJfN/YOf7mUXS+HvONZkBgV JnZ0Jj9UoGip86vDJUwNgQtuBKprP9WdFgvD0lLniAtm7nh8HSqzitnLLWeaUy1cnTV5xnE7HVdln GPf361pTIgCgXErLURZEAbMuAIPEMAA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pbYv9-0049g3-6p; Mon, 13 Mar 2023 03:32:23 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pbYv2-0049ed-Ht for linux-arm-kernel@lists.infradead.org; Mon, 13 Mar 2023 03:32:20 +0000 Received: by mail-pj1-x1049.google.com with SMTP id n33-20020a17090a5aa400b0023b4f444476so521792pji.3 for ; Sun, 12 Mar 2023 20:32:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678678334; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=nr9tcr5LV12ufzOmQGswUuR6NF1d70rt+DF2oLQIx/o=; b=apH4r5wNHPpZ5CWLQC1fVNzyZyD2uZsNpLprREbMqFfn1aaVdqg0k6gsB3Wu86L+X8 LlhyePAcswCC0G6Al9xkDwaChQ2CEjGuk2KhZpMJBWm6kpnh9v2ijxrYmft2Gw1Z5ZQ8 w5zI8E/F69m3d+wuw9uUhUwBE1WOrZ+uNhjTU2OO4XQnSaQEpt1Ex5KI1U7IsykGQa0N K/C89t1pxoBcgIk6nll5KwxNOktMkbdNatLeySfctKU2IpPABQIEtJ+AH1SnihnQsyV+ 1OSdcvMfAZSy4psffFT5qkY5I+76CKqsmWGR+BIYFS91MJYoTWYmhEYCuyzG5tDXNK4V GHjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678678334; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=nr9tcr5LV12ufzOmQGswUuR6NF1d70rt+DF2oLQIx/o=; b=ZydjNPoU5PaGOJWSgHWQzhE8i5Fzd9cyUOUR53EyZweIA3meIZ0gyUvQxEnjIZ8i0p o8Ng1Yll07b8AYNeuE6J5Rjwscbx0WsJXUss5ogAxuje+ecG9OdDtDXVtiBcrPsU7ubt ZNq7yz3JXtqvHlsvKh6B5g18t9DBlozbJf6upaZIdqXesE5tkIJca2NefXlOeM5SsAHT 3iT34OQkAiNV2L9ANgy7L+38MA9312Q3ALl8+JFPjBaYUcdHpQaeWPuLt93vL3tw6ZLx bOKseC4yHmraLW3vNXZvULm/MSIE/vuayI8ZMUHhb4Tqj6fCwkHQNsqjoX7uvkVaIF0s yKkw== X-Gm-Message-State: AO0yUKXAiVxZBpuyWhuoBEJvD5czhjh/k0jPM45mVCPuvOIsv1BuDHBl wCskhmLYF3XOozhM2VcM/XFUPtXoSi8= X-Google-Smtp-Source: AK7set8lnq+TDeHTGVE7DhLCdqN+7xuqVd/ZZqS+fE3fJxKZcQkLEbPPK+zKol9LZJSRGg5SyT5VtxdQFqk= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a17:90a:b798:b0:22c:3ee1:db3b with SMTP id m24-20020a17090ab79800b0022c3ee1db3bmr3251880pjr.3.1678678334056; Sun, 12 Mar 2023 20:32:14 -0700 (PDT) Date: Sun, 12 Mar 2023 20:32:08 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230313033208.1475499-1-reijiw@google.com> Subject: [PATCH v2 1/2] KVM: arm64: PMU: Fix GET_ONE_REG for vPMC regs to return the current value From: Reiji Watanabe To: Marc Zyngier , Oliver Upton , 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 , Jing Zhang , Raghavendra Rao Anata , Will Deacon , Reiji Watanabe , stable@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230312_203216_636956_5A718649 X-CRM114-Status: GOOD ( 16.20 ) 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 Have KVM_GET_ONE_REG for vPMU counter (vPMC) registers (PMCCNTR_EL0 and PMEVCNTR_EL0) return the sum of the register value in the sysreg file and the current perf event counter value. Values of vPMC registers are saved in sysreg files on certain occasions. These saved values don't represent the current values of the vPMC registers if the perf events for the vPMCs count events after the save. The current values of those registers are the sum of the sysreg file value and the current perf event counter value. But, when userspace reads those registers (using KVM_GET_ONE_REG), KVM returns the sysreg file value to userspace (not the sum value). Fix this to return the sum value for KVM_GET_ONE_REG. Fixes: 051ff581ce70 ("arm64: KVM: Add access handler for event counter register") Cc: stable@vger.kernel.org Reviewed-by: Marc Zyngier Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/sys_regs.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 53749d3a0996..1b2c161120be 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -856,6 +856,22 @@ static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx) return true; } +static int get_pmu_evcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, + u64 *val) +{ + u64 idx; + + if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 0) + /* PMCCNTR_EL0 */ + idx = ARMV8_PMU_CYCLE_IDX; + else + /* PMEVCNTRn_EL0 */ + idx = ((r->CRm & 3) << 3) | (r->Op2 & 7); + + *val = kvm_pmu_get_counter_value(vcpu, idx); + return 0; +} + static bool access_pmu_evcntr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) @@ -1072,7 +1088,7 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, /* Macro to expand the PMEVCNTRn_EL0 register */ #define PMU_PMEVCNTR_EL0(n) \ { PMU_SYS_REG(SYS_PMEVCNTRn_EL0(n)), \ - .reset = reset_pmevcntr, \ + .reset = reset_pmevcntr, .get_user = get_pmu_evcntr, \ .access = access_pmu_evcntr, .reg = (PMEVCNTR0_EL0 + n), } /* Macro to expand the PMEVTYPERn_EL0 register */ @@ -1982,7 +1998,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { { PMU_SYS_REG(SYS_PMCEID1_EL0), .access = access_pmceid, .reset = NULL }, { PMU_SYS_REG(SYS_PMCCNTR_EL0), - .access = access_pmu_evcntr, .reset = reset_unknown, .reg = PMCCNTR_EL0 }, + .access = access_pmu_evcntr, .reset = reset_unknown, + .reg = PMCCNTR_EL0, .get_user = get_pmu_evcntr}, { PMU_SYS_REG(SYS_PMXEVTYPER_EL0), .access = access_pmu_evtyper, .reset = NULL }, { PMU_SYS_REG(SYS_PMXEVCNTR_EL0),