From patchwork Sat May 27 04:02: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: 13257542 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 AE68BC77B7E for ; Sat, 27 May 2023 04:05:04 +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=ngVgq1Fk1EsjP2FV+FV02gxraKvhoeX5gVruuqNbMos=; b=36nH4iwO02y5hFw+1nyyyT6Nn+ SipQM5JATQ85jz0ZjZmdFEYa0AAB1QnsNpVNk4x5bpH0UBGXCS7vMR99HxBrWWtb1TSq/g0XPQg1d fiH7sHvlXRNpkmc/Uq0S4rTNJDFrwwxLnOzlskWoTdwJeEn8GfexLsMNv0fUtiwb9YyakhnaTEHzq yUBV6BIch16M3h0zDqfXxW2QvV1X+s19YpzMgBKoWoDG8EZO/XE44yzDS4jq63xnMAoWx2oRKNqON W3RENdHwFEM/W8T7g9gJGuUTcxc2s++lrTvlPoyWx4xdhVFHbylnsISvGERawPAuUrbBXpAT2vouZ wM+iSImQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q2lAZ-004o0s-1H; Sat, 27 May 2023 04:04:43 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q2lAW-004nzi-1S for linux-arm-kernel@lists.infradead.org; Sat, 27 May 2023 04:04:41 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bacfa4ef059so3277832276.2 for ; Fri, 26 May 2023 21:04:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685160278; x=1687752278; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uUrVHxQ1VjsIKZJ0cYmjatxxlv1RidcUPcAwfkxMDeQ=; b=mnKD5djePan6cHUiRAmZh38Jmo+ropUJ0h0VmSE6uXDB/X12f01Q1L295u/FPmsJe7 psC4a9zcrCkyAa0xQFqKrV9FLzwyA7ATKehSlM0c7ERZJii79G9dsfEwowCoo0nNDTF3 5NCRrlT59GgmTp/pVXGlfeSkIkv5YFtnrW0wlZi2s2rqz21oT09bP6qfK4fb5hBCSC09 PxuvQeOOnXn4blCcFNbFRNEXN7GPF+KeUXP2ym9XDgMEyPD8icS7IKN2yqxz5oVLyYu7 cUUYIHqVISmSIVQvx8JAolK7vvPmc8Lg/sCVUk2mHRfx0V9b2DSRQEcl4CWhrALeA4cm HmOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685160278; x=1687752278; 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=uUrVHxQ1VjsIKZJ0cYmjatxxlv1RidcUPcAwfkxMDeQ=; b=kYDBBRW7BMzpJ7X3hTQh9pd7rqQhG2y2FE6PgfLUbmosXkaBewoLvOyV6H2fZE8KQe mmcvPYDeZCgAASC8c3Pk38Ur6EAP1bDALuBQaw8L+ZylhQAaEEuH/i6qtS8wMMKGbloB e6Dh1jlZz9LAId35Ej3Gon+l0+neZ1BuU8952373aSB4d3CZm8kFbpJLCNgCeBUyGIRb 5C60eu/z65ZcNFDTIdJgXgm/+s+zdCL7vJNCh+fdc3QbnUB1QYpan+1KVrInzm70iZo2 5zaXbj3ACLwAz7ZM8ohTt6+IRJAiMNGNmn9ZCG0nlWgP79Waiq2ApvGti++W3JtvXjUv fFEQ== X-Gm-Message-State: AC+VfDwbs9Rm+8qjlSLnXZg7Y0r8eKWPKgYSEEmAiJhUSAnVYfcn0rGv /4zTBfSVkOZZWOM2HlGzjMW5J9qhJuQ= X-Google-Smtp-Source: ACHHUZ40X1JtVnbU0yW1OlIarBd66lox6g/g0byncu954rsHLASWFxfD7mYTWnHgIRpIDTHXydpwbi5LsS4= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a25:2646:0:b0:bac:f582:ef18 with SMTP id m67-20020a252646000000b00bacf582ef18mr2153443ybm.5.1685160278549; Fri, 26 May 2023 21:04:38 -0700 (PDT) Date: Fri, 26 May 2023 21:02:34 -0700 In-Reply-To: <20230527040236.1875860-1-reijiw@google.com> Mime-Version: 1.0 References: <20230527040236.1875860-1-reijiw@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230527040236.1875860-3-reijiw@google.com> Subject: [PATCH 2/4] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset 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-20230526_210440_492956_E86A4F3C X-CRM114-Status: GOOD ( 17.08 ) 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 Set the default PMU for the guest on the first vCPU reset, not when userspace initially uses KVM_ARM_VCPU_PMU_V3_CTRL. The following patches will use the PMUVer of the PMU as the default value of the ID_AA64DFR0_EL1.PMUVer for vCPUs with PMU configured. Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/pmu-emul.c | 10 +--------- arch/arm64/kvm/reset.c | 20 +++++++++++++------- include/kvm/arm_pmu.h | 6 ++++++ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index d50c8f7a2410..0194a94c4bae 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -869,7 +869,7 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq) return true; } -static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) +int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) { lockdep_assert_held(&kvm->arch.config_lock); @@ -926,14 +926,6 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) if (vcpu->arch.pmu.created) return -EBUSY; - if (!kvm->arch.arm_pmu) { - /* No PMU set, get the default one */ - int ret = kvm_arm_set_vm_pmu(kvm, NULL); - - if (ret) - return ret; - } - switch (attr->attr) { case KVM_ARM_VCPU_PMU_V3_IRQ: { int __user *uaddr = (int __user *)(long)attr->addr; diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index b5dee8e57e77..f5e24492926c 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -258,13 +258,24 @@ static int kvm_set_vm_width(struct kvm_vcpu *vcpu) int kvm_reset_vcpu(struct kvm_vcpu *vcpu) { struct vcpu_reset_state reset_state; + struct kvm *kvm = vcpu->kvm; int ret; bool loaded; u32 pstate; - mutex_lock(&vcpu->kvm->arch.config_lock); + mutex_lock(&kvm->arch.config_lock); ret = kvm_set_vm_width(vcpu); - mutex_unlock(&vcpu->kvm->arch.config_lock); + if (!ret && kvm_vcpu_has_pmu(vcpu)) { + if (!kvm_arm_support_pmu_v3()) + ret = -EINVAL; + else if (unlikely(!kvm->arch.arm_pmu)) + /* + * As no PMU is set for the guest yet, + * set the default one. + */ + ret = kvm_arm_set_vm_pmu(kvm, NULL); + } + mutex_unlock(&kvm->arch.config_lock); if (ret) return ret; @@ -315,11 +326,6 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) } else { pstate = VCPU_RESET_PSTATE_EL1; } - - if (kvm_vcpu_has_pmu(vcpu) && !kvm_arm_support_pmu_v3()) { - ret = -EINVAL; - goto out; - } break; } diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 1a6a695ca67a..5ece2a3c1858 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -96,6 +96,7 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5) u8 kvm_arm_pmu_get_pmuver_limit(void); +int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu); #else struct kvm_pmu { @@ -168,6 +169,11 @@ static inline u8 kvm_arm_pmu_get_pmuver_limit(void) return 0; } +static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) +{ + return 0; +} + #endif #endif