From patchwork Thu Mar 2 05:50:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 13156739 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 62129C678D4 for ; Thu, 2 Mar 2023 05:52:11 +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=mp/qiaHQoPNEdpDC23RZWYTACkv4tEVQbHGy0lmg/Iw=; b=5CILnp4XqKaAC6dGR0wBTo5TbY dxMBe4DBUC7nR0n0t8yG3HsE65M4EbnFWyVenfDaWPTq5VAZEGwq84Z0s6oFLFb6TYSzMA4ZKtykR Fxhhlvcjw8ncXUHFiLjl5flfsVfcL6BOIvVgmcy0iIKzlr2NqnDxx2XRjxTi1eeYwOz+/H/1hhjzZ PqtdrlK6OR0zpHHgwRJsubhqfVZiuCqQt6xIOppDbSEa2wOwA8unE+ClQBXP9Q/lM5y4USkgF+3AT iPT5ua8XFDY6BZ92kPoN8AkXt0L7ExlSlEvqJOHqD8/BkDJFkaF7vSOxF1KjXQ3fXf5UgFbilc1il QYyjtOTw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXbqY-000w46-M5; Thu, 02 Mar 2023 05:51:18 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pXbqU-000w1Z-Pg for linux-arm-kernel@lists.infradead.org; Thu, 02 Mar 2023 05:51:16 +0000 Received: by mail-yb1-xb49.google.com with SMTP id w192-20020a25dfc9000000b009fe14931caaso2988499ybg.7 for ; Wed, 01 Mar 2023 21:51:13 -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=JIeZqQDsplqWeP29HSexHYJ737JRXcOhQ7J6+HeNN2w=; b=G7n5xRm8RuF8DWUa4l/dLwnWK40+IWpfuZf0ysJNlcCfBoklRxlEITp3YycBMOORKJ B1TCLExmdY4QQO8YBfGSb1K8/NdSEuqGiPOpnbvcVa0XUuYZOTsXKHWJkZiy5ydEippp d5dLg66/P6IfBlW1CMH5eFq8MQhHQhFkD1S27UABk5qRXTdCPMWuoeiqxwgOdbW3agzZ xcDn9pMYTT/2TcSno3TOuLAZL3SIzukVCHBGqoQ6PFhGe8HOVYcfwSUe82Jlsorf50zg eRRIKDMiN94VfqUUSM2An1jKG22Q8worBPPp0YTpyxoXdwi8jl5E9oD+BQPNFMpSsJrt L+MQ== 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=JIeZqQDsplqWeP29HSexHYJ737JRXcOhQ7J6+HeNN2w=; b=kcQbOwlPJl73p+iY/2lCBCvgZIpIRbuDbvkIEfaP9ZcnKmePVWhc5VVkAunmp2rVkV tsC/OO/1AcZoqv1Irl0Lz1SX/NewgINO65gKGMZYWqFlRMRTf3iELRpKSSsMU2jpp2qW o3/XcVN/eegoz7nSZIqPGPHKN6YhErwoJUYrkaMBjYwiPft8QbOy2e/DjyZA4E5Vef9J zo4ZEt7Bx3lRVHPwiFOBIUFbElivakRrUK6sY6pjISnPqSq3TIcyrqRswGsSRhmwpfk3 3fEZI1DeZjB1GYamXFBUx/38WH/guEM/BUXB5nTALnuoM5G+odXZDt6grVzaA2W2BVgT MK/w== X-Gm-Message-State: AO0yUKVqWbPuCa7nOoNovvC3hM7drZFPzQgheRww1j6dClZ2+Y3B/EZK vqiq4JWuY9fc3SMQmEVpVADUEMa1HOE= X-Google-Smtp-Source: AK7set8kXeijwpd5IrzgWApLjro6tbe3Ft4KHgEVuEL6iPTP37OQY2fZkgCjZ3ALNfhQIYq/8IZYFpXUm4U= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a81:ac4e:0:b0:535:5e8c:65ef with SMTP id z14-20020a81ac4e000000b005355e8c65efmr5482065ywj.6.1677736273146; Wed, 01 Mar 2023 21:51:13 -0800 (PST) Date: Wed, 1 Mar 2023 21:50:32 -0800 In-Reply-To: <20230302055033.3081456-1-reijiw@google.com> Mime-Version: 1.0 References: <20230302055033.3081456-1-reijiw@google.com> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog Message-ID: <20230302055033.3081456-2-reijiw@google.com> Subject: [PATCH 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230301_215114_874001_DA209A48 X-CRM114-Status: GOOD ( 16.61 ) 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. Signed-off-by: Reiji Watanabe Reviewed-by: Marc Zyngier --- 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 c6cbfe6b854b..c48c053d6146 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -765,6 +765,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) @@ -981,7 +997,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 */ @@ -1745,7 +1761,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),