From patchwork Tue Sep 8 18:54:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 11763951 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 66DC5618 for ; Tue, 8 Sep 2020 18:56:39 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 371E42068D for ; Tue, 8 Sep 2020 18:56:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="sZB+g4nQ"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="JRI6HfFB" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 371E42068D Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:To:From: 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=H7m+opWBdf8FYnhGY4g2KqX2wKILAynPPJYy5ImKhrQ=; b=sZB+g4nQzC3kF9r1HOG8aWn0H4 /V5itub6Rg+zeNYP7EOHyQbZkAa1vV3BlG3l6zZpdT8ip52ekpIjboVQ3xep77QDHCwood63Mughd /+pfFc15pPcV1D1s6Xd/8jtzezjzuZqKNuI2BOp+w/ZHdYELXpdq+EwxjRIqwdTsEkuqS99y4eC7P 5NwVLgP0RQRVVB31jGYDoqCmPK5uDWTDreOcyLZMWp7O66Pg4taxjB8w7E4dAYxvE+N4WtHNZWlwq J2BwFIdLmyBEADQUeee29EbBjsQ/kF0q3obSCx4ZNktfHgyfU/HQKO5RqD8DHizTXz8m9ZFjXmzat FZh/zS7Q==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFilt-0000SB-Uv; Tue, 08 Sep 2020 18:55:13 +0000 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFilr-0000Qq-2H for linux-arm-kernel@lists.infradead.org; Tue, 08 Sep 2020 18:55:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1599591312; x=1631127312; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=x5jEF96sYCAIy62XD2CkEvqSoI0X9CvEDr0LQID4QSM=; b=JRI6HfFBs+ZAkonQfREJzY8S5sMYcj4nWN21gNoQ3CKYQEiIWhwEQsZA LgFHfkZgY3lGXMbNKk4tH6sen6YuX60eIrpmcbW6sXrA40tKw11EcQLYS F6Rmw+87BloXx0Dj1QzuNwfv8WVnuBqgIw5h9DKmDSCiecEi4sTzY2T49 w=; X-IronPort-AV: E=Sophos;i="5.76,406,1592870400"; d="scan'208";a="66374160" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-c7131dcf.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 08 Sep 2020 18:55:00 +0000 Received: from EX13MTAUWC001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-c7131dcf.us-west-2.amazon.com (Postfix) with ESMTPS id 8E7E7A2582; Tue, 8 Sep 2020 18:54:58 +0000 (UTC) Received: from EX13D20UWC001.ant.amazon.com (10.43.162.244) by EX13MTAUWC001.ant.amazon.com (10.43.162.135) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 8 Sep 2020 18:54:56 +0000 Received: from u79c5a0a55de558.ant.amazon.com (10.43.161.85) by EX13D20UWC001.ant.amazon.com (10.43.162.244) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 8 Sep 2020 18:54:53 +0000 From: Alexander Graf To: Subject: [PATCH] KVM: arm64: Allow to limit number of PMU counters Date: Tue, 8 Sep 2020 20:54:45 +0200 Message-ID: <20200908185445.22561-1-graf@amazon.com> X-Mailer: git-send-email 2.28.0.394.ge197136389 MIME-Version: 1.0 X-Originating-IP: [10.43.161.85] X-ClientProxiedBy: EX13D12UWC004.ant.amazon.com (10.43.162.182) To EX13D20UWC001.ant.amazon.com (10.43.162.244) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200908_145511_223359_C6D53FFC X-CRM114-Status: GOOD ( 20.30 ) X-Spam-Score: -4.8 (----) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-4.8 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different -0.0 SPF_PASS SPF: sender matches SPF record 0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [207.171.184.25 listed in wl.mailspike.net] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at https://www.dnswl.org/, high trust [207.171.184.25 listed in list.dnswl.org] 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders -0.0 DKIMWL_WL_HIGH DKIMwl.org - Whitelisted High sender X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , kvm@vger.kernel.org, Suzuki K Poulose , Marc Zyngier , Eric Auger , James Morse , linux-arm-kernel@lists.infradead.org, Robin Murphy , Julien Thierry Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org We currently pass through the number of PMU counters that we have available in hardware to guests. So if my host supports 10 concurrently active PMU counters, my guest will be able to spawn 10 counters as well. This is undesireable if we also want to use the PMU on the host for monitoring. In that case, we want to split the PMU between guest and host. To help that case, let's add a PMU attr that allows us to limit the number of PMU counters that we expose. With this patch in place, user space can keep some counters free for host use. Signed-off-by: Alexander Graf --- Because this patch touches the same code paths as the vPMU filtering one and the vPMU filtering generalized a few conditions in the attr path, I've based it on top. Please let me know if you want it independent instead. --- arch/arm64/include/uapi/asm/kvm.h | 7 ++++--- arch/arm64/kvm/pmu-emul.c | 22 ++++++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 5 +++++ include/kvm/arm_pmu.h | 1 + 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index 7b1511d6ce44..db025c0b5a40 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -342,9 +342,10 @@ struct kvm_vcpu_events { /* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0 -#define KVM_ARM_VCPU_PMU_V3_IRQ 0 -#define KVM_ARM_VCPU_PMU_V3_INIT 1 -#define KVM_ARM_VCPU_PMU_V3_FILTER 2 +#define KVM_ARM_VCPU_PMU_V3_IRQ 0 +#define KVM_ARM_VCPU_PMU_V3_INIT 1 +#define KVM_ARM_VCPU_PMU_V3_FILTER 2 +#define KVM_ARM_VCPU_PMU_V3_NUM_EVENTS 3 #define KVM_ARM_VCPU_TIMER_CTRL 1 #define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0 #define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 9f0fd0224d5b..40848e17d0cb 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -238,6 +238,8 @@ void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) pmu->pmc[i].idx = i; + + pmu->num_events = perf_num_counters() - 1; } /** @@ -875,6 +877,25 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) return 0; } + case KVM_ARM_VCPU_PMU_V3_NUM_EVENTS: { + u64 mask = ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT; + int __user *uaddr = (int __user *)(long)attr->addr; + unsigned int num_events; + + if (get_user(num_events, uaddr)) + return -EFAULT; + + if (num_events >= perf_num_counters()) + return -EINVAL; + + vcpu->arch.pmu.num_events = num_events; + + num_events <<= ARMV8_PMU_PMCR_N_SHIFT; + __vcpu_sys_reg(vcpu, SYS_PMCR_EL0) &= ~mask; + __vcpu_sys_reg(vcpu, SYS_PMCR_EL0) |= num_events; + + return 0; + } case KVM_ARM_VCPU_PMU_V3_INIT: return kvm_arm_pmu_v3_init(vcpu); } @@ -912,6 +933,7 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) case KVM_ARM_VCPU_PMU_V3_IRQ: case KVM_ARM_VCPU_PMU_V3_INIT: case KVM_ARM_VCPU_PMU_V3_FILTER: + case KVM_ARM_VCPU_PMU_V3_NUM_EVENTS: if (kvm_arm_support_pmu_v3() && test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features)) return 0; diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 077293b5115f..fca0bba6d97b 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -672,6 +672,11 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E); if (!system_supports_32bit_el0()) val |= ARMV8_PMU_PMCR_LC; + + /* Override number of event selectors */ + val &= ~(ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT); + val |= (u32)vcpu->arch.pmu.num_events << ARMV8_PMU_PMCR_N_SHIFT; + __vcpu_sys_reg(vcpu, r->reg) = val; } diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 6db030439e29..5557024edce5 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -27,6 +27,7 @@ struct kvm_pmu { bool ready; bool created; bool irq_level; + u8 num_events; }; #define kvm_arm_pmu_v3_ready(v) ((v)->arch.pmu.ready)