From patchwork Sat Apr 8 03:47:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 13205584 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 CA109C7619A for ; Sat, 8 Apr 2023 03:50:27 +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=uPKgkQsQw+dUy2zAjHHG3Z+ddtxu0IXrr5LjtFjx220=; b=tN4 uC9guS/6H2bDV1H8v+zWCAX94gXFB2DMWHAKXiidQ1UW7Lfp1c6vaJzse1EwIRcBnBtWhvj8DykG2 h3msSn0ozpcT05+CNNa6O0rBwbX6ue22TQ/VPJfYnsi7bxG+cRnwtBlos+OXnPxgrOlaTclwBu3Lh QB23RDRQ2KK2bEF82+Ni88vZxJmEWLv2dKS2d7tDG6M/LlJg1ABBdLVo0XGJ8Hg3QYC+mDamYXml3 aPoGWmCcbH8e27g+J36IIezwjlrXGRS+F4qP62D7frN1wtwY1+XDpCtiNVULfgTxPEbfOiXZwn9oW HGjK3rcaPEpeAOP1uzbnU0FXv68tqng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pkza2-00Bfm1-1w; Sat, 08 Apr 2023 03:49:34 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pkzZz-00BflX-1I for linux-arm-kernel@lists.infradead.org; Sat, 08 Apr 2023 03:49:32 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54c0b8ca2d1so71617677b3.17 for ; Fri, 07 Apr 2023 20:49:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680925769; x=1683517769; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=mG6DQun3X6e8Ycs1sxk5r+7eU2NYVjbJa0bqr1sphFc=; b=a7N4k+eWIN2ClbGZHuEu4ZylavHenpxLMS7+ncTPOK+Dgz/TJT+rakguvQnf4r8UMr +sAwlaXxOS5HDwmahTMQ9qvsjBsDdMIn1ezJaSwYH2YaSKDe72mbZxO6kt9EdA1odxKj cxm+2byY8NLxVOwoJMDosZ4Or2SqtYgYAClM6j1UfaP9VGVERyXZjoQo43L5CFGhzO9E 1hIfWsbdAehKuulLfbpHWccJMAhnl6uZY8ZMP6BQiNr78nw/v1jkKLOD+s5F0a/EjOjn w3/YQNPt6PbXh7fcCxohoua1sMi3KpK5DORyHYDnl4ZvW3gQl2z99XMfCyI75+y40WMX SNjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680925769; x=1683517769; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=mG6DQun3X6e8Ycs1sxk5r+7eU2NYVjbJa0bqr1sphFc=; b=ObqsesOJP9IjKDrBGAcHhw3+CNQDPc0aylRhTFdJlOzfOSh1ION0eOajkfs5x6f2TZ dCs2M3Z27YXibOROyS1f98CEOnOaYF4BKXaaKYvgKoTZcNPBuVitbM3Pl5ps5eYHS2UD OfLbLy7rl+2lbvbI53bU5IqQhMC2hxjKA4TaVs8ius/JFX+PVW1yYjK727DhQMJ6vfG5 x2w311M9HcW4gE+7/DEyALXq2t/rMqKHD9bqIglVu3ID8yrYRIYQoQ6GZRP8Uc27gL/M KwzTyrGV2obbwjnFlKBh0ewyVKKwP7ztK6INWd2xKnho29OIzoSWqFHu6HkeQ2Gw0JrO Ediw== X-Gm-Message-State: AAQBX9fE9z7aB0yE1+P/lmjJmhy71ndH9Y8ewQ/7utTLomBhV9kkUNvq BnUaZD4am/w1zU9QlslOke730RbcPvI= X-Google-Smtp-Source: AKy350b2b+kyoeKDHcQVC1s0YQ5cKbQc+CXh/czLHlf4GwnSo/hMjwzF33oizSPx/1fq1ueUiauYfHjnrYw= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a25:c845:0:b0:b4c:9333:2a2 with SMTP id y66-20020a25c845000000b00b4c933302a2mr2288924ybf.9.1680925769001; Fri, 07 Apr 2023 20:49:29 -0700 (PDT) Date: Fri, 7 Apr 2023 20:47:57 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: <20230408034759.2369068-1-reijiw@google.com> Subject: [PATCH v2 0/2] KVM: arm64: PMU: Correct the handling of 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-20230407_204931_459371_B27AD57F X-CRM114-Status: GOOD ( 13.91 ) 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 This series will fix bugs in KVM's handling of PMUSERENR_EL0. With PMU access support from EL0 [1], the perf subsystem would set CR and ER bits of PMUSERENR_EL0 as needed to allow EL0 to have a direct access to PMU counters. However, KVM appears to assume that the register value is always zero for the host EL0, and has the following two problems in handling the register. [A] The host EL0 might lose the direct access to PMU counters, as KVM always clears PMUSERENR_EL0 before returning to userspace. [B] With VHE, the guest EL0 access to PMU counters might be trapped to EL1 instead of to EL2 (even when PMUSERENR_EL0 for the guest indicates that the guest EL0 has an access to the counters). This is because, with VHE, KVM sets ER, CR, SW and EN bits of PMUSERENR_EL0 to 1 on vcpu_load() to ensure to trap PMU access from the guset EL0 to EL2, but those bits might be cleared by the perf subsystem after vcpu_load() (when PMU counters are programmed for the vPMU emulation). Patch-1 will fix [A], and Patch-2 will fix [B] respectively. The series is based on v6.3-rc5. v2: - Save the PMUSERENR_EL0 for the host in the sysreg array of kvm_host_data. [Marc] - Don't let armv8pmu_start() overwrite PMUSERENR if the vCPU is loaded, instead have KVM update the saved shadow register value for the host. [Marc, Mark] v1: https://lore.kernel.org/all/20230329002136.2463442-1-reijiw@google.com/ [1] https://github.com/torvalds/linux/commit/83a7a4d643d33a8b74a42229346b7ed7139fcef9 Reiji Watanabe (2): KVM: arm64: PMU: Restore the host's PMUSERENR_EL0 KVM: arm64: PMU: Don't overwrite PMUSERENR with vcpu loaded arch/arm64/include/asm/kvm_host.h | 5 +++++ arch/arm64/kernel/perf_event.c | 21 ++++++++++++++++++--- arch/arm64/kvm/hyp/include/hyp/switch.h | 13 +++++++++++-- arch/arm64/kvm/pmu.c | 20 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) base-commit: 7e364e56293bb98cae1b55fd835f5991c4e96e7d