From patchwork Wed Mar 29 00:21: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: 13191744 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 3C5EBC76196 for ; Wed, 29 Mar 2023 00:22:55 +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=EqbGroeaU9MZ9bjql4fUm25dsIRYLCXlI3uRTqTBacw=; b=oJJ U7a4QqpnecxhDeePgDlhjLA6Wtd1Z+ojnWlM7xt4jbOoZvaYgpG7JPQlhDJhZj3snv0sP1Kr+qdg3 iA4D/qCmwNbyyn/+A5BDprquqsM5L9w/s/LNb62Q0rN/Kje3lYRDfK6z1QZWlJdec8PKZ75dOGSz/ EwF2Gkw/gxOtGMjETJOm4ziT9ktFVRBxyaLPDGMCPnRu8bDoHDurDxUNdzqOcYiRdUYJSHWmvqBEB WEasMkmlVg4QS3VKRYLtCSagtb/FMtnlhto4Sx/txNBzjytVxJk/BNToRDdDdiUaF0Sb5z6f4Lz07 2RMq4w+PqZoqxClMqiNE8+vX1LvOImg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phJZZ-00GBYx-2H; Wed, 29 Mar 2023 00:21:53 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phJZX-00GBYZ-0N for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2023 00:21:52 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5458dde029bso138719497b3.13 for ; Tue, 28 Mar 2023 17:21:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680049309; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=eENpLd222ixV/BEnAXd0sJR92bJ82vOOCby7utTN/Sg=; b=Aa8PAqkWPiWBkQau05fBBTj7M/gFx1Aynw2O/bik1Gs2nRZ+KCIt+jxe8K7YXvPuSQ W1Wt5kni0xN/UeYWmRcoxL/2GaaRe6z0kiD1NxIRNpj3GIakz4LW6YxHpaaImCNac4/C fl8p6fE4xBtuW1MsFWCX34HO/YCxvYmxwdDC8R/70EWIIQ3ZmoZTdpZWr787RJW/kueZ QXt8o1DKgq/YfU9AoF390oIyWkG+IblRUs+kFr5ZcykjQmQX6Kb5cWu3fuLC0cNAKRpS Llob84bCYPw0zLpomfS23S1dHYquDMJfAUbTKeALBoUDrsxrLh16Mm8L7K+H/HI9m0TL Aing== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680049309; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=eENpLd222ixV/BEnAXd0sJR92bJ82vOOCby7utTN/Sg=; b=lbM7/IAxlxVEOQ5WsyR6IHA7z/OpxrOVuMgQe34w1jh30awU4zLuG8BVejTdI1UUoV ZXYOJ/astkXd6NdfBLWZHP5khooay4K44J2S9MU9zF1fdgWrxPt4WuVDC4/a8Iwfcbow UxWdUllWSrVXcjtBqWTOGUUXdBXdLe5h+MNnkjwsh4jd6VUbOB+6/UHn2h7VewIhowt2 Oo22jmUseJapklP5Ii1sSr2dIuEesNA3mmY0Cn5EavwQTZk2F5xYWyY6pad1atVtvSm4 q4HU1OSesPsN0VsnotKTTY6FUQy811KF7vauTIu49QRtwLeP43kC38zQXanJh0PsLL+l ragg== X-Gm-Message-State: AAQBX9cuLAMU6rjEo0s1OyAA+L3PsgvgkfI2GszYWBIjHSSy1wrff3Fc dteMSHa3Kmi96669qK0ilhjrbuBbX1o= X-Google-Smtp-Source: AKy350bYlftYs/VjTtPUhS+UZTqUG+PyB67xAw95S2F9WnlDiwxxGHU3sZYm6ArConjwnrWRT7z6nY2Gezw= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a05:6902:102b:b0:b6c:48c3:3c1c with SMTP id x11-20020a056902102b00b00b6c48c33c1cmr11754883ybt.13.1680049309492; Tue, 28 Mar 2023 17:21:49 -0700 (PDT) Date: Tue, 28 Mar 2023 17:21:34 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329002136.2463442-1-reijiw@google.com> Subject: [PATCH v1 0/2] KVM: arm64: PMU: Correct the handling of PMUSERENR_EL0 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-20230328_172151_159909_FCD949DD X-CRM114-Status: GOOD ( 12.41 ) 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-rc4. [1] https://github.com/torvalds/linux/commit/83a7a4d643d33a8b74a42229346b7ed7139fcef9 Reiji Watanabe (2): KVM: arm64: PMU: Restore the host's PMUSERENR_EL0 KVM: arm64: PMU: Ensure to trap PMU access from EL0 to EL2 arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/hyp/include/hyp/switch.h | 28 +++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) base-commit: 197b6b60ae7bc51dd0814953c562833143b292aa