From patchwork Mon Nov 15 16:57:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12692640 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13297C433EF for ; Mon, 15 Nov 2021 16:58:28 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id D97C061BD2 for ; Mon, 15 Nov 2021 16:58:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D97C061BD2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xBlyg6VMrnWdteePQ+sUz7eGFzOUSxwFf7GyPAclGlU=; b=pt0e7dROBpysV7 hDEciuxyd0G1HHQBiW5f8KKfc2cdeqenbjl/LLR3SVcJusr13b05M5qIQjpuEKi7nlefuTtjkb1TZ agGDU05S+dCzEiwmngo6NY/SMzw7BpRNxHwDE60oZI6nnwikzCAimQoc3VOO85hnKcjIjlxCcOdnv +crT07vkj2NUrHWFQh0vR99kC92AwEghzHCaFaZonBzaYNuBtWVG26m67VsXVc+aGWgBxolal9Epq ggyZxKAxpDlKPPmQn9R4/HZv7tbpmrcu8fPUsPxSFXoZnmV58o5bkSxTXQ278gwljbVPno1L/+f85 aIfIK+nsJjgi5DjJLcvw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mmfIA-00GRKj-DN; Mon, 15 Nov 2021 16:57:14 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mmfGZ-00GQZf-BS for linux-arm-kernel@lists.infradead.org; Mon, 15 Nov 2021 16:55:36 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4DD241063; Mon, 15 Nov 2021 08:55:34 -0800 (PST) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D0A0B3F766; Mon, 15 Nov 2021 08:55:32 -0800 (PST) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, maz@kernel.org, james.morse@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com Subject: [PATCH kvmtool 5/9] arm64: Rework set_pmu_attr() Date: Mon, 15 Nov 2021 16:57:01 +0000 Message-Id: <20211115165705.195736-6-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211115165705.195736-1-alexandru.elisei@arm.com> References: <20211115165705.195736-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211115_085535_553424_F93819E9 X-CRM114-Status: GOOD ( 15.35 ) 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 By the time kvmtool generates the DTB node for the PMU, the KVM_ARM_VCPU_PMU_V3 VCPU feature is already set by kvm_cpu__arch_init(). KVM refuses to run a VCPU if the PMU hasn't been initialized. A PMU cannot be initialized if the interrupt ID hasn't been set by userspace. As a consequence, kvmtool will get an error if the interrupt ID or if the PMU has not been initialized: KVM_RUN failed: Invalid argument To make debugging easier, exit with an error message as soon as one the PMU ioctls fails, instead of waiting until the VCPU is first run. To avoid the repetition of assigning a new kvm_device_attr struct in the main body of pmu__generate_fdt_nodes(), which hinders readability of the function, move the struct to set_pmu_attr(). Signed-off-by: Alexandru Elisei --- arm/aarch64/pmu.c | 48 ++++++++++++++++------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/arm/aarch64/pmu.c b/arm/aarch64/pmu.c index 6b190c5e2ae5..ac5b7bcd6ca9 100644 --- a/arm/aarch64/pmu.c +++ b/arm/aarch64/pmu.c @@ -7,30 +7,31 @@ #include "asm/pmu.h" -static int set_pmu_attr(struct kvm *kvm, int vcpu_idx, - struct kvm_device_attr *attr) +static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr) { - int ret, fd; - - fd = kvm->cpus[vcpu_idx]->vcpu_fd; + struct kvm_device_attr pmu_attr = { + .group = KVM_ARM_VCPU_PMU_V3_CTRL, + .addr = (u64)addr, + .attr = attr, + }; + int ret; - ret = ioctl(fd, KVM_HAS_DEVICE_ATTR, attr); + ret = ioctl(vcpu->vcpu_fd, KVM_HAS_DEVICE_ATTR, &pmu_attr); if (!ret) { - ret = ioctl(fd, KVM_SET_DEVICE_ATTR, attr); + ret = ioctl(vcpu->vcpu_fd, KVM_SET_DEVICE_ATTR, &pmu_attr); if (ret) - perror("PMU KVM_SET_DEVICE_ATTR failed"); + die_perror("PMU KVM_SET_DEVICE_ATTR"); } else { - pr_err("Unsupported PMU on vcpu%d\n", vcpu_idx); + die_perror("PMU KVM_HAS_DEVICE_ATTR"); } - - return ret; } void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) { const char compatible[] = "arm,armv8-pmuv3"; int irq = KVM_ARM_PMUv3_PPI; - int i, ret; + struct kvm_cpu *vcpu; + int i; u32 cpu_mask = (((1 << kvm->nrcpus) - 1) << GIC_FDT_IRQ_PPI_CPU_SHIFT) \ & GIC_FDT_IRQ_PPI_CPU_MASK; @@ -44,26 +45,9 @@ void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) return; for (i = 0; i < kvm->nrcpus; i++) { - struct kvm_device_attr pmu_attr; - - pmu_attr = (struct kvm_device_attr){ - .group = KVM_ARM_VCPU_PMU_V3_CTRL, - .addr = (u64)(unsigned long)&irq, - .attr = KVM_ARM_VCPU_PMU_V3_IRQ, - }; - - ret = set_pmu_attr(kvm, i, &pmu_attr); - if (ret) - return; - - pmu_attr = (struct kvm_device_attr){ - .group = KVM_ARM_VCPU_PMU_V3_CTRL, - .attr = KVM_ARM_VCPU_PMU_V3_INIT, - }; - - ret = set_pmu_attr(kvm, i, &pmu_attr); - if (ret) - return; + vcpu = kvm->cpus[i]; + set_pmu_attr(vcpu, &irq, KVM_ARM_VCPU_PMU_V3_IRQ); + set_pmu_attr(vcpu, NULL, KVM_ARM_VCPU_PMU_V3_INIT); } _FDT(fdt_begin_node(fdt, "pmu"));