From patchwork Sat Jun 3 02:50:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 13266054 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 4BD95C7EE2A for ; Sat, 3 Jun 2023 02:51:50 +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=dbuddA/qN2oGcK5JOq1IrTWf4WjiMo0DP8ZkA6SEVGY=; b=kChoHNyfbJ4bNpLS9gwQisH1xd lfwxod6OR4cLtZTev11PXr3dTC5b8Hs+Ywa86GgMGwXm5uLZ+ieZBjLcovPTvXzcyvpO9dEdBEIRG 4vjNRCwehhvUCEZUqdWG21uhuylMVFYNXQFnlyyr7u0SLFcR7xR+a4YKOKjQsOMg0ahwDXpiE9rBW kwUrWgrSqRF8RHnciB4re71wK1Xxnssx/I696WISy83Xb5DmJsEHYN2Ipeh2mAvKOqTFUWFUJQMBm O8O8Zz/KITwvryI7Efmis9kkaMjWlopPTj8Qhw75g9GO4X5yWeOJoLL6bZZ+ONwaDNSuUL9Pm9aB3 HIiXZPVg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q5HMS-008aM6-2y; Sat, 03 Jun 2023 02:51:24 +0000 Received: from mail-pg1-x549.google.com ([2607:f8b0:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q5HMR-008aLA-02 for linux-arm-kernel@lists.infradead.org; Sat, 03 Jun 2023 02:51:24 +0000 Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-53f6f7d1881so2496065a12.3 for ; Fri, 02 Jun 2023 19:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685760680; x=1688352680; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2SBYW84L/m+RDCA19ekjkbKA/bg9Rb+RSkEF96qBDgs=; b=1BcDQfkzkfe0i2bUABNsoY/fv0w3MChr1jJqEU1n5t/4gQTnzoRWB7gRfTWMfu0/mq StQhrK4POlXN0TeajosvdJpgLIDh4y/h3gR+xD8ja8duhQea+WGjU8/WBf0nC/hI8xjz gp9KhTAp/Ox67+UgDbbucgvc1UU3yHRhe2N+1cnIf6IfyR4VlJ4SlEfgKgqzMEsjClqC W7OoNYZpeUqhnNmZGwpmqeH2+Xv6OHIEh8QTSoEqgZHKONTkBsh53tUOEsZSonpd6Ekv P4OP3+yr4H0UCyMlwnpABk0ceKHRM9J/c4j61GlTNidkXbvJf+dnWXsyxRQDlgjl2oFI WD+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685760680; x=1688352680; 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=2SBYW84L/m+RDCA19ekjkbKA/bg9Rb+RSkEF96qBDgs=; b=RYgGwoESG8Yn+uHJRNNNnjN65SbuuAZhrBOPJSdffRpS0Av1TwOfnvoVF1lOyPMXKV V/1orxiKweTGBwrfPZq8zAWYgywhjmq5rZ7k91qSefAGDURM5zEU/tqqH5fQjk38JJf/ e7IHkEuv3dkvzBWDEbdaxXGwqAHHtMrFC289qYu32KaaWCiKViWKnj8YtUq4QDVX535F 4ivoTLGmwSXyOgINGyEvQ23s2TfyYojFen5vm3JWBWsNnDvJs+Hc96xlXPmrt5IjOmFf GCvHJEBBSovk9+ZSIbutV/XRxgrkLpo9+iaz5GNlRNiV3/PqIfUqVEZ550chHH6nGmw2 hIbg== X-Gm-Message-State: AC+VfDwgZeEjHqJruVNdtiMPNd1I+lpDfcyAdVzzRg0XZeVg6TPwY0ti XFzVcqDcjBNSVDMGIT3z7KXw11F2T+I= X-Google-Smtp-Source: ACHHUZ7F41GX4kI7XUmhvJo2WeUg7o8g2JM34l5sTdU0OOY63gxJLffhrx6c9AFBJ63aEnrO/XTy+PrLYBc= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a65:47c9:0:b0:53f:6f7c:554e with SMTP id f9-20020a6547c9000000b0053f6f7c554emr2728965pgs.12.1685760680506; Fri, 02 Jun 2023 19:51:20 -0700 (PDT) Date: Fri, 2 Jun 2023 19:50:34 -0700 In-Reply-To: <20230603025035.3781797-1-reijiw@google.com> Mime-Version: 1.0 References: <20230603025035.3781797-1-reijiw@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230603025035.3781797-2-reijiw@google.com> Subject: [PATCH v5 1/2] KVM: arm64: PMU: Restore the host's PMUSERENR_EL0 From: Reiji Watanabe To: Marc Zyngier , Mark Rutland , Oliver Upton , Will Deacon , Catalin Marinas , 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 , Shaoqin Huang , Rob Herring , Reiji Watanabe X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230602_195123_072408_81C74035 X-CRM114-Status: GOOD ( 12.90 ) 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 Restore the host's PMUSERENR_EL0 value instead of clearing it, before returning back to userspace, as the host's EL0 might have a direct access to PMU registers (some bits of PMUSERENR_EL0 for might not be zero for the host EL0). Fixes: 83a7a4d643d3 ("arm64: perf: Enable PMU counter userspace access for perf event") Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/hyp/include/hyp/switch.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h index e78a08a72a3c..51229d847103 100644 --- a/arch/arm64/kvm/hyp/include/hyp/switch.h +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h @@ -82,7 +82,12 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu) * EL1 instead of being trapped to EL2. */ if (kvm_arm_support_pmu_v3()) { + struct kvm_cpu_context *hctxt; + write_sysreg(0, pmselr_el0); + + hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt; + ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0); write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0); } @@ -106,8 +111,12 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu) write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2); write_sysreg(0, hstr_el2); - if (kvm_arm_support_pmu_v3()) - write_sysreg(0, pmuserenr_el0); + if (kvm_arm_support_pmu_v3()) { + struct kvm_cpu_context *hctxt; + + hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt; + write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0); + } if (cpus_have_final_cap(ARM64_SME)) { sysreg_clear_set_s(SYS_HFGRTR_EL2, 0,