From patchwork Tue Apr 12 13:32:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810836 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 22786C433EF for ; Tue, 12 Apr 2022 13:34:20 +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: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=69jZDjZaCqpcLDahwDROGQpm9D8/ozKVvBd1yRmEuaQ=; b=d9QnGdN45ss/BI 7LLURbyc06KBJDksnjVLGEandg8Ipbn+YbG7+3tXJ18/dPeAql9ZNJHwNvreKHr0GMbQOGj/tUVbp isG99D6UT31a6+6/SrZ1wX8VfF2LdPlWykNOlGifrXmdtWiVSrKKGqWcbpXK/nrVh/LtnrW5AxCJL JgxT9Y5UqTBub2Jl+26eeUdWDOpgE4w8xKEji4myRvBQkXFe5ue9BNBRbJOqz3AwuhRwh4ZTGaf23 mo0vDcca5Dzr4AY8OORxCQGSQwRL1hz6MaNwNLm6cA+PCmvVbYnEWnV7yDngPNQH9BSRN5B2dIJP9 07AmYWG95V4rU5IVNcGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGdl-00Ebp5-Un; Tue, 12 Apr 2022 13:33:06 +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 1neGdQ-00Ebdm-Fc for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:46 +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 8F9FF1516; Tue, 12 Apr 2022 06:32:38 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2AA0A3F70D; Tue, 12 Apr 2022 06:32:37 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 01/11] linux/err.h: Add missing stdbool.h include Date: Tue, 12 Apr 2022 14:32:21 +0100 Message-Id: <20220412133231.35355-2-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063244_633857_3BBE4759 X-CRM114-Status: UNSURE ( 8.26 ) X-CRM114-Notice: Please train this message. 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 Add missing header stdbool.h to avoid errors like this one, which can happen if the including file doesn't include stdbool.h: include/linux/err.h:33:15: error: type defaults to ‘int’ in declaration of ‘bool’ [-Werror=implicit-int] 33 | static inline bool __must_check IS_ERR(__force const void *ptr) | ^~~~ include/linux/err.h:33:15: error: variable ‘bool’ declared ‘inline’ [-Werror] include/linux/err.h:33:1: error: ‘warn_unused_result’ attribute only applies to function types [-Werror=attributes] 33 | static inline bool __must_check IS_ERR(__force const void *ptr) | ^~~~~~ include/linux/err.h:33:33: error: expected ‘,’ or ‘;’ before ‘IS_ERR’ 33 | static inline bool __must_check IS_ERR(__force const void *ptr) | ^~~~~~ include/linux/err.h:38:15: error: type defaults to ‘int’ in declaration of ‘bool’ [-Werror=implicit-int] 38 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) | ^~~~ include/linux/err.h:38:15: error: variable ‘bool’ declared ‘inline’ [-Werror] include/linux/err.h:38:1: error: ‘warn_unused_result’ attribute only applies to function types [-Werror=attributes] 38 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) | ^~~~~~ include/linux/err.h:38:15: error: redundant redeclaration of ‘bool’ [-Werror=redundant-decls] 38 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) | ^~~~ include/linux/err.h:33:15: note: previous declaration of ‘bool’ was here 33 | static inline bool __must_check IS_ERR(__force const void *ptr) | ^~~~ include/linux/err.h:38:33: error: expected ‘,’ or ‘;’ before ‘IS_ERR_OR_NULL’ 38 | static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) | ^~~~~~~~~~~~~~ include/linux/err.h: In function ‘PTR_ERR_OR_ZERO’: include/linux/err.h:58:6: error: implicit declaration of function ‘IS_ERR’ [-Werror=implicit-function-declaration] 58 | if (IS_ERR(ptr)) | ^~~~~~ include/linux/err.h:58:6: error: nested extern declaration of ‘IS_ERR’ [-Werror=nested-externs] Signed-off-by: Alexandru Elisei --- include/linux/err.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/err.h b/include/linux/err.h index a729120644d5..1256c7abfefa 100644 --- a/include/linux/err.h +++ b/include/linux/err.h @@ -1,6 +1,8 @@ #ifndef _LINUX_ERR_H #define _LINUX_ERR_H +#include + #include #include From patchwork Tue Apr 12 13:32:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810833 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 BA47BC433EF for ; Tue, 12 Apr 2022 13:33:54 +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: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=Q604ZWbATYVQKyq2ikBpuSEacyaE1V56N+OF6JWYQKk=; b=i7ZJv8RAJAi8FF 1euSA7ot8J+IxbF3PksGS5VmOpwjIXJcs9COmebD98S9TmWScCTmFMX5Fkzuxs3jykq6VZwofuPPn YKuGzsw06txbTvgLQ6wYyoOMIHI1vqjAiuHDRpe4fCAE/tRBDaFuYA5pMeAvp8Mb+BZo35oDffTEd bMQ816m/FKTcSeAC8D6g+C9DrY6RTC7soVpTQzaPmNIzUaZoIdXCug8FubpahAhgTojAh1/Zir4v+ XNLT+uWwsqDNb0N8XETH/LPKiR485CzMdxg0tqKDtJ+u2hG1Hr3CHweC2OUmQFKmHV4by0BXDKC4v bVgBjmRFYRRW8Gc3SVfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGdT-00Ebim-VE; Tue, 12 Apr 2022 13:32:48 +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 1neGdQ-00Ebe3-GG for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:46 +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 4A1BD153B; Tue, 12 Apr 2022 06:32:40 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id CDEC53F70D; Tue, 12 Apr 2022 06:32:38 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 02/11] linux/bitops.h: Include wordsize.h to provide the __WORDSIZE define Date: Tue, 12 Apr 2022 14:32:22 +0100 Message-Id: <20220412133231.35355-3-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063244_627919_0BD5B2F5 X-CRM114-Status: UNSURE ( 9.62 ) X-CRM114-Notice: Please train this message. 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 Trying to build a source file which included bitops.h, but didn't also bring in the definition for __WORDSIZE (by including limits.h, for example) would result in the following error: include/linux/bitops.h:8:23: error: ‘__WORDSIZE’ undeclared (first use in this function) 8 | #define BITS_PER_LONG __WORDSIZE | ^~~~~~~~~~ The symbol is defined in the bits/wordsize.h header file, include it. Signed-off-by: Alexandru Elisei --- include/linux/bitops.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 56448b71ebbf..3d31f0acf48e 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -1,6 +1,8 @@ #ifndef _KVM_LINUX_BITOPS_H_ #define _KVM_LINUX_BITOPS_H_ +#include + #include #include #include From patchwork Tue Apr 12 13:32:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810834 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 0149FC433F5 for ; Tue, 12 Apr 2022 13:34:01 +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: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=VABp8XHnVARJRagn6Z1kN2N1FXIimAl6Y+kz8CJfzow=; b=i5xFq2n/XIr7ZG xOJ084RkK1sPIY1Cbr9vnxpqKZh/9wRwltu+mVdsE20Fe1SbbUCp0HLqfI0BSB/+0903D6U32zvbI mv1zrTfU+M+HcmYfZ1UXdGQmteL/jsGilN/FPEv8GRZFSuaJvc/iVHLoXVPA6vHYB7w5L+SbLnU/h 1y7PyTR6qOtwM/RO6wbYJ3HK8uIwNx7BXPai2GEdG0lgYBdzZs4tt/TknFctk9t9vP9I8ScYpPTPF 6tbcJmCEh8szoychtbl/xy+PWHIAbCgZXpQk6TMzhNH5qJR10JkJcMMFpZ1bVMOIVJdjdSKR7/K/3 31kb3nlSsRBCqAnry4rQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGdd-00Eblf-Br; Tue, 12 Apr 2022 13:32:57 +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 1neGdQ-00Eben-Hq for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:46 +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 0227F1570; Tue, 12 Apr 2022 06:32:42 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 890403F70D; Tue, 12 Apr 2022 06:32:40 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 03/11] arm/arm64: pmu.h: Add missing header guards Date: Tue, 12 Apr 2022 14:32:23 +0100 Message-Id: <20220412133231.35355-4-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063244_658462_F7C1A973 X-CRM114-Status: UNSURE ( 7.00 ) X-CRM114-Notice: Please train this message. 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 Signed-off-by: Alexandru Elisei --- arm/include/arm-common/pmu.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arm/include/arm-common/pmu.h b/arm/include/arm-common/pmu.h index 7a170a56dadd..0085f2545553 100644 --- a/arm/include/arm-common/pmu.h +++ b/arm/include/arm-common/pmu.h @@ -1,4 +1,8 @@ +#ifndef ARM_COMMON__PMU_H +#define ARM_COMMON__PMU_H #define KVM_ARM_PMUv3_PPI 23 void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm); + +#endif /* ARM_COMMON__PMU_H */ From patchwork Tue Apr 12 13:32:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810837 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 F0117C433F5 for ; Tue, 12 Apr 2022 13:34:23 +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: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=gHlTynGsooK+l0A7V2+X1krljARE9SqQYMUuU+1cXks=; b=rw0BASQoNi2TTQ CkcCePudiHrsUOxt41hfTd7tOPuUMmu9wW4iWaqP9v/kl6o3q6nZBXsS9RlFlRGkU3aIbNp1qkn5v HtdXlxScUFEG86yQXOGCULauWuJwWHdQT7SAnJwIatSKRTvWkB6mnOA9Z1yEg3ExWuBuhzBU/NfjH hVk6p/YmjMjVwMBmE7zOPi4nr9dSUlxbU2ZwzrIyVcxosAGFGwqk5lrSy6lRLv2zsNEhfk5RJltXY ab+eJKpRrL5iEPhN049De+w1wwXD+6iNmEX+KH/DdHP7ps0LfppOTja6D8fRTrWkclNQTjTRb/1hR oC8Dy6xIRW1hhn8nURLw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGe4-00Ebw5-Jk; Tue, 12 Apr 2022 13:33:24 +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 1neGdS-00Ebes-8t for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:47 +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 A6413169C; Tue, 12 Apr 2022 06:32:43 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 421EA3F70D; Tue, 12 Apr 2022 06:32:42 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 04/11] arm: Move arch specific VCPU features to the arch specific function Date: Tue, 12 Apr 2022 14:32:24 +0100 Message-Id: <20220412133231.35355-5-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063246_392596_2EE64EF1 X-CRM114-Status: GOOD ( 15.59 ) 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 KVM_CAP_ARM_EL1_32BIT and KVM_CAP_ARM_PMU_V3 are arm64 specific features. They are set based on arm64 specific command line options and they target arm64 hardware features. It makes little sense for kvmtool to set the features in the code that is shared between arm and arm64. Move the logic to set the feature bits to the arch specific function kvm_cpu__select_features(), which is already used by arm64 to set other arm64 specific features. Signed-off-by: Alexandru Elisei --- arm/aarch64/include/kvm/kvm-cpu-arch.h | 4 +--- arm/aarch64/kvm-cpu.c | 12 ++++++++++++ arm/kvm-cpu.c | 8 -------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/arm/aarch64/include/kvm/kvm-cpu-arch.h b/arm/aarch64/include/kvm/kvm-cpu-arch.h index 35996dc0a0d6..4cbadf9175d8 100644 --- a/arm/aarch64/include/kvm/kvm-cpu-arch.h +++ b/arm/aarch64/include/kvm/kvm-cpu-arch.h @@ -6,9 +6,7 @@ #include "arm-common/kvm-cpu-arch.h" #define ARM_VCPU_FEATURE_FLAGS(kvm, cpuid) { \ - [0] = ((!!(cpuid) << KVM_ARM_VCPU_POWER_OFF) | \ - (!!(kvm)->cfg.arch.aarch32_guest << KVM_ARM_VCPU_EL1_32BIT) | \ - (!!(kvm)->cfg.arch.has_pmuv3 << KVM_ARM_VCPU_PMU_V3)) \ + [0] = (!!(cpuid) << KVM_ARM_VCPU_POWER_OFF), \ } #define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFUL diff --git a/arm/aarch64/kvm-cpu.c b/arm/aarch64/kvm-cpu.c index 9f3e8586880c..3b6224a599c8 100644 --- a/arm/aarch64/kvm-cpu.c +++ b/arm/aarch64/kvm-cpu.c @@ -130,6 +130,18 @@ static void reset_vcpu_aarch64(struct kvm_cpu *vcpu) void kvm_cpu__select_features(struct kvm *kvm, struct kvm_vcpu_init *init) { + if (kvm->cfg.arch.aarch32_guest) { + if (!kvm__supports_extension(kvm, KVM_CAP_ARM_EL1_32BIT)) + die("32bit guests are not supported\n"); + init->features[0] |= 1UL << KVM_ARM_VCPU_EL1_32BIT; + } + + if (kvm->cfg.arch.has_pmuv3) { + if (!kvm__supports_extension(kvm, KVM_CAP_ARM_PMU_V3)) + die("PMUv3 is not supported"); + init->features[0] |= 1UL << KVM_ARM_VCPU_PMU_V3; + } + /* Enable pointer authentication if available */ if (kvm__supports_extension(kvm, KVM_CAP_ARM_PTRAUTH_ADDRESS) && kvm__supports_extension(kvm, KVM_CAP_ARM_PTRAUTH_GENERIC)) { diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index 00660d6e7a97..3a5c8a5a8e31 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -46,14 +46,6 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) .features = ARM_VCPU_FEATURE_FLAGS(kvm, cpu_id) }; - if (kvm->cfg.arch.aarch32_guest && - !kvm__supports_extension(kvm, KVM_CAP_ARM_EL1_32BIT)) - die("32bit guests are not supported\n"); - - if (kvm->cfg.arch.has_pmuv3 && - !kvm__supports_extension(kvm, KVM_CAP_ARM_PMU_V3)) - die("PMUv3 is not supported"); - vcpu = calloc(1, sizeof(struct kvm_cpu)); if (!vcpu) return NULL; From patchwork Tue Apr 12 13:32:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810838 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 2A990C433F5 for ; Tue, 12 Apr 2022 13:34:45 +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: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=9RY3oe9tWkOzG6xfR6yz6t6TeJK/YF7aebXpwi/pUUU=; b=HN5IzKaRHUkveE aUbT0Wkb7hKhE+Ra7YJwGgQbBeJOah8wHKAX/8fmmV08BYZpXnkk5Op/CMYr5s8TsCOhM4JHpheoI tq57MBzkbi+DxSkgG1ZUXARuZyTDkPG4abVusMLplKNnJMdklec3ccRIyBvvcyXih7X3En24o0Dv2 sEltZRQn9H9ZYEc3nHsdg6NS3XRcxfMDiZ1TWG6lPCtG7c+++1+WsEvNm7QuIVeOtXiPSNyrnWLu9 ZD8K2qa3SCjZ41qlLOqntsdPHakCQFPpXGaoY9qfRZHv0UeptaU40XK4+j/IDJR8AQ1PBQgX80OIF qn6INRbSAY8C62nWalkQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGeL-00Ec6v-JY; Tue, 12 Apr 2022 13:33:41 +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 1neGdS-00Ebhl-Oh for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:48 +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 56C2B16F3; Tue, 12 Apr 2022 06:32:45 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E5A0A3F70D; Tue, 12 Apr 2022 06:32:43 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 05/11] arm: Get rid of the ARM_VCPU_FEATURE_FLAGS() macro Date: Tue, 12 Apr 2022 14:32:25 +0100 Message-Id: <20220412133231.35355-6-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063246_923886_B04C4B43 X-CRM114-Status: GOOD ( 13.93 ) 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 The ARM_VCPU_FEATURE_FLAGS() macro sets a feature bit in a rather convoluted way: if cpu_id is 0, then bit KVM_ARM_VCPU_POWER_OFF is 0, otherwise is set to 1. There's really no need for this indirection, especially considering that the macro has been changed to return the same value for both the arm and arm64 architectures. Replace it with a simple conditional statement in kvm_cpu__arch_init(), which makes it clearer to understand. Signed-off-by: Alexandru Elisei --- arm/aarch32/include/kvm/kvm-cpu-arch.h | 4 ---- arm/aarch64/include/kvm/kvm-cpu-arch.h | 4 ---- arm/kvm-cpu.c | 6 +++++- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/arm/aarch32/include/kvm/kvm-cpu-arch.h b/arm/aarch32/include/kvm/kvm-cpu-arch.h index 6fe020689c28..fd0b387cb539 100644 --- a/arm/aarch32/include/kvm/kvm-cpu-arch.h +++ b/arm/aarch32/include/kvm/kvm-cpu-arch.h @@ -5,10 +5,6 @@ #include "arm-common/kvm-cpu-arch.h" -#define ARM_VCPU_FEATURE_FLAGS(kvm, cpuid) { \ - [0] = (!!(cpuid) << KVM_ARM_VCPU_POWER_OFF), \ -} - #define ARM_MPIDR_HWID_BITMASK 0xFFFFFF #define ARM_CPU_ID 0, 0, 0 #define ARM_CPU_ID_MPIDR 5 diff --git a/arm/aarch64/include/kvm/kvm-cpu-arch.h b/arm/aarch64/include/kvm/kvm-cpu-arch.h index 4cbadf9175d8..aeae8c138bba 100644 --- a/arm/aarch64/include/kvm/kvm-cpu-arch.h +++ b/arm/aarch64/include/kvm/kvm-cpu-arch.h @@ -5,10 +5,6 @@ #include "arm-common/kvm-cpu-arch.h" -#define ARM_VCPU_FEATURE_FLAGS(kvm, cpuid) { \ - [0] = (!!(cpuid) << KVM_ARM_VCPU_POWER_OFF), \ -} - #define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFUL #define ARM_CPU_ID 3, 0, 0, 0 #define ARM_CPU_ID_MPIDR 5 diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c index 3a5c8a5a8e31..98bc5fdf0418 100644 --- a/arm/kvm-cpu.c +++ b/arm/kvm-cpu.c @@ -43,7 +43,7 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) unsigned int i; struct kvm_vcpu_init preferred_init; struct kvm_vcpu_init vcpu_init = { - .features = ARM_VCPU_FEATURE_FLAGS(kvm, cpu_id) + .features = {}, }; vcpu = calloc(1, sizeof(struct kvm_cpu)); @@ -63,6 +63,10 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) if (vcpu->kvm_run == MAP_FAILED) die("unable to mmap vcpu fd"); + /* VCPU 0 is the boot CPU, the others start in a poweroff state. */ + if (cpu_id > 0) + vcpu_init.features[0] |= (1UL << KVM_ARM_VCPU_POWER_OFF); + /* Set KVM_ARM_VCPU_PSCI_0_2 if available */ if (kvm__supports_extension(kvm, KVM_CAP_ARM_PSCI_0_2)) { vcpu_init.features[0] |= (1UL << KVM_ARM_VCPU_PSCI_0_2); From patchwork Tue Apr 12 13:32:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810839 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 B1755C433F5 for ; Tue, 12 Apr 2022 13:34:57 +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: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=cqMhwYKPDvJ9RulPvsCDoP/cgLkAgQ+a670lMJH/E3o=; b=Hu1zqvKRr98DP7 EUBk1foL5KAYeGFbX145gq7bedO4b8RM3DBpfTm8wL7ApnNy16rjDECUghswhrYs+9nu2ro5U8EYe q3MTL0kv1EFDw7ZyK29y2gw6rAPgEgwZ3ANCHlI53WjuZQ5VZK1WDPLfuwRHqjsKUky8LusaCQpFw QRQC/B68SGxCh5xHRgm/suN3qyOWFO6LgH+UuHzPrwH929ocFNz6INL6LBRqAYqXd7PUf4GX59key 8A2sZsNBVMya7H+nyPJUDQBh/MfMvZHnXEQzHhZh1voZXOAu5RrzL0M6JVk1AabKnRomzs9HFGLpg dE89kIcHwhk/TVCLJFZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGeZ-00EcGB-W7; Tue, 12 Apr 2022 13:33:56 +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 1neGdU-00Ebiw-9A for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:50 +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 068C11516; Tue, 12 Apr 2022 06:32:47 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 96E1C3F70D; Tue, 12 Apr 2022 06:32:45 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 06/11] arm: Make the PMUv3 emulation code arm64 specific Date: Tue, 12 Apr 2022 14:32:26 +0100 Message-Id: <20220412133231.35355-7-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063248_454982_3AC45307 X-CRM114-Status: GOOD ( 11.83 ) 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 KVM for aarch32 does not exist anymore, PMUv3 is a hardware feature present only on aarch64 CPUs, the command line option to enable the feature for a VCPU is aarch64 specific, the PMU code is called only from an aarch64 function and it compiles to an empty stub when ARCH=arm. There is no reason to have the PMUv3 emulation code in the common code area for arm and arm64, so move it to the arm64 directory, where it can be expanded in the future without fear of breaking aarch32 support. Signed-off-by: Alexandru Elisei --- Makefile | 6 +++--- arm/aarch64/arm-cpu.c | 3 ++- arm/{include/arm-common => aarch64/include/asm}/pmu.h | 6 +++--- arm/{ => aarch64}/pmu.c | 7 ++----- 4 files changed, 10 insertions(+), 12 deletions(-) rename arm/{include/arm-common => aarch64/include/asm}/pmu.h (52%) rename arm/{ => aarch64}/pmu.c (93%) diff --git a/Makefile b/Makefile index e9121dc39d02..31274353656d 100644 --- a/Makefile +++ b/Makefile @@ -159,8 +159,7 @@ endif # ARM OBJS_ARM_COMMON := arm/fdt.o arm/gic.o arm/gicv2m.o arm/ioport.o \ - arm/kvm.o arm/kvm-cpu.o arm/pci.o arm/timer.o \ - arm/pmu.o + arm/kvm.o arm/kvm-cpu.o arm/pci.o arm/timer.o HDRS_ARM_COMMON := arm/include ifeq ($(ARCH), arm) DEFINES += -DCONFIG_ARM @@ -183,6 +182,7 @@ ifeq ($(ARCH), arm64) OBJS += arm/aarch64/kvm-cpu.o OBJS += arm/aarch64/kvm.o OBJS += arm/aarch64/pvtime.o + OBJS += arm/aarch64/pmu.o ARCH_INCLUDE := $(HDRS_ARM_COMMON) ARCH_INCLUDE += -Iarm/aarch64/include @@ -583,4 +583,4 @@ ifneq ($(MAKECMDGOALS),clean) KVMTOOLS-VERSION-FILE: @$(SHELL_PATH) util/KVMTOOLS-VERSION-GEN $(OUTPUT) -endif \ No newline at end of file +endif diff --git a/arm/aarch64/arm-cpu.c b/arm/aarch64/arm-cpu.c index 7e4a3c17c38f..f5c8e1e7a06b 100644 --- a/arm/aarch64/arm-cpu.c +++ b/arm/aarch64/arm-cpu.c @@ -5,7 +5,8 @@ #include "arm-common/gic.h" #include "arm-common/timer.h" -#include "arm-common/pmu.h" + +#include "asm/pmu.h" #include #include diff --git a/arm/include/arm-common/pmu.h b/arm/aarch64/include/asm/pmu.h similarity index 52% rename from arm/include/arm-common/pmu.h rename to arm/aarch64/include/asm/pmu.h index 0085f2545553..38e3154b11b7 100644 --- a/arm/include/arm-common/pmu.h +++ b/arm/aarch64/include/asm/pmu.h @@ -1,8 +1,8 @@ -#ifndef ARM_COMMON__PMU_H -#define ARM_COMMON__PMU_H +#ifndef __ARM_PMU_H__ +#define __ARM_PMU_H__ #define KVM_ARM_PMUv3_PPI 23 void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm); -#endif /* ARM_COMMON__PMU_H */ +#endif /* __ARM_PMU_H__ */ diff --git a/arm/pmu.c b/arm/aarch64/pmu.c similarity index 93% rename from arm/pmu.c rename to arm/aarch64/pmu.c index 5b058eabb49d..6b190c5e2ae5 100644 --- a/arm/pmu.c +++ b/arm/aarch64/pmu.c @@ -4,9 +4,9 @@ #include "kvm/util.h" #include "arm-common/gic.h" -#include "arm-common/pmu.h" -#ifdef CONFIG_ARM64 +#include "asm/pmu.h" + static int set_pmu_attr(struct kvm *kvm, int vcpu_idx, struct kvm_device_attr *attr) { @@ -71,6 +71,3 @@ void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) _FDT(fdt_property(fdt, "interrupts", irq_prop, sizeof(irq_prop))); _FDT(fdt_end_node(fdt)); } -#else -void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) { } -#endif From patchwork Tue Apr 12 13:32:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810840 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 98FF8C433FE for ; Tue, 12 Apr 2022 13:35:10 +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: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=YKZYjzoddn6+SUBjtCSPb9/+zN+eAJzMfWj9k9SNDeQ=; b=TJEFso8WcTr8uo tbWMsrrKZwy0W5Jxr/8ch/dt1fiCRP82IQvXEPVPJLfMQQft8tOe9cP3IUJ9JiuYuBwoLABKiIEcC 3TDlDyRaizEHjREOzigTysxCEkujcrd9nSIbbyIZthuOdafDhyUGS4O9ELOaF5B3HJZmfTgrk89Oo Ix275dZUbsc8EaEHBMZb4LdfEJH8diTLm0zP92v3C+6LSVFUDtpwGLo/VMistLcgFePg2j2Hr9qCU fmAcHLfcqkNoP7gZtpHkTD1AUGhrNIymrgkrjmYFtMgS3xyMnSHkqijpoLoL4PM0B1af4N7rfQlrc isrWYKPkuv8eYwnEAUtg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGel-00EcN5-Ro; Tue, 12 Apr 2022 13:34:07 +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 1neGdV-00Ebhl-22 for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:50 +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 B04A8150C; Tue, 12 Apr 2022 06:32:48 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 48B6A3F70D; Tue, 12 Apr 2022 06:32:47 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 07/11] arm64: Rework set_pmu_attr() Date: Tue, 12 Apr 2022 14:32:27 +0100 Message-Id: <20220412133231.35355-8-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063249_210920_72897B81 X-CRM114-Status: GOOD ( 15.29 ) 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")); From patchwork Tue Apr 12 13:32:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810841 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 5E905C433FE for ; Tue, 12 Apr 2022 13:35:47 +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: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=mVfEcnGljDzDfls9VtSH4OVGeBKPfaxK7zk+co7gCcw=; b=hOeMDbNFvm4lxD kQ7Fl5LbIb4k6S5KLENbnZOfENd8Bs0Cvuf3al7M79ptb63VSsqGp13vjTeaHdQf8OXncXFNOtHvi r/R+ol9NXBc6Y9Hrrez6oCbg+OPM/Jk2vYbZFmaWeOOSCSH/FQIk4SO2X24dACZSXxXbfLVYHlaJT gxEgR54ytgwM1xKF4h8V3K3ipH2p0zP6oW1qvI/86KqDYCfvegA9hMpOaaNBHXv/FDQs7c95XcGFC V+3dUeLJfhT93goU5Nbd/Wpl9nO0DYVjFeXLGGt2hMpdlQ0HzTGaNPNNmETaNFdYi46QVTDFQHy5u KPdG8xudmZaqGOpJWD9w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGf6-00EcbF-TK; Tue, 12 Apr 2022 13:34:29 +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 1neGdW-00Ebiw-LC for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:53 +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 81E281516; Tue, 12 Apr 2022 06:32:50 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id F10033F70D; Tue, 12 Apr 2022 06:32:48 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 08/11] Add cpumask functions Date: Tue, 12 Apr 2022 14:32:28 +0100 Message-Id: <20220412133231.35355-9-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063250_837282_15DDA35A X-CRM114-Status: GOOD ( 28.45 ) 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 Add a handful of cpumask functions, some of which will be used when dealing with different PMUs on heterogeneous systems. The maximum number of CPUs in a system, NR_CPUS, which dictates the size of the cpumask, has been taken from the Kconfig file for each architecture, from Linux version 5.16. Signed-off-by: Alexandru Elisei --- Makefile | 2 + arm/aarch32/include/asm/kernel.h | 8 + arm/aarch64/include/asm/kernel.h | 8 + include/linux/bitmap.h | 71 +++++++++ include/linux/bitops.h | 2 + include/linux/bits.h | 8 + include/linux/cpumask.h | 62 ++++++++ include/linux/find.h | 30 ++++ include/linux/kernel.h | 6 + mips/include/asm/kernel.h | 8 + powerpc/include/asm/kernel.h | 8 + util/bitmap.c | 256 +++++++++++++++++++++++++++++++ util/find.c | 40 +++++ x86/include/asm/kernel.h | 8 + 14 files changed, 517 insertions(+) create mode 100644 arm/aarch32/include/asm/kernel.h create mode 100644 arm/aarch64/include/asm/kernel.h create mode 100644 include/linux/bitmap.h create mode 100644 include/linux/bits.h create mode 100644 include/linux/cpumask.h create mode 100644 include/linux/find.h create mode 100644 mips/include/asm/kernel.h create mode 100644 powerpc/include/asm/kernel.h create mode 100644 util/bitmap.c create mode 100644 util/find.c create mode 100644 x86/include/asm/kernel.h diff --git a/Makefile b/Makefile index 31274353656d..9e67c7637b1e 100644 --- a/Makefile +++ b/Makefile @@ -89,6 +89,8 @@ OBJS += net/uip/buf.o OBJS += net/uip/csum.o OBJS += net/uip/dhcp.o OBJS += kvm-cmd.o +OBJS += util/bitmap.o +OBJS += util/find.o OBJS += util/init.o OBJS += util/iovec.o OBJS += util/rbtree.o diff --git a/arm/aarch32/include/asm/kernel.h b/arm/aarch32/include/asm/kernel.h new file mode 100644 index 000000000000..61296094deb1 --- /dev/null +++ b/arm/aarch32/include/asm/kernel.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_KERNEL_H +#define __ASM_KERNEL_H + +#define NR_CPUS 32 + +#endif /* __ASM_KERNEL_H */ diff --git a/arm/aarch64/include/asm/kernel.h b/arm/aarch64/include/asm/kernel.h new file mode 100644 index 000000000000..a2a8d9ed4059 --- /dev/null +++ b/arm/aarch64/include/asm/kernel.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_KERNEL_H +#define __ASM_KERNEL_H + +#define NR_CPUS 4096 + +#endif /* __ASM_KERNEL_H */ diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h new file mode 100644 index 000000000000..3b203461415a --- /dev/null +++ b/include/linux/bitmap.h @@ -0,0 +1,71 @@ +#ifndef KVM__BITMAP_H +#define KVM__BITMAP_H + +#include +#include + +#include "linux/bitops.h" + +#define DECLARE_BITMAP(name,bits) \ + unsigned long name[BITS_TO_LONGS(bits)] + +#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) +#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) + +static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) +{ + unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); + memset(dst, 0, len); +} + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define BITMAP_MEM_ALIGNMENT 8 +#else +#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long)) +#endif +#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1) + +void __bitmap_set(unsigned long *map, unsigned int start, int len); + +static inline void bitmap_set(unsigned long *map, unsigned int start, + unsigned int nbits) +{ + if (__builtin_constant_p(nbits) && nbits == 1) + set_bit(start, map); + else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && + IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && + __builtin_constant_p(nbits & BITMAP_MEM_MASK) && + IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) + memset((char *)map + start / 8, 0xff, nbits / 8); + else + __bitmap_set(map, start, nbits); +} + +bool __bitmap_and(unsigned long *dst, const unsigned long *src1, + const unsigned long *src2, unsigned int nbits); + +static inline bool bitmap_and(unsigned long *dst, const unsigned long *src1, + const unsigned long *src2, unsigned int nbits) +{ + if (nbits >= 0 && nbits <= BITS_PER_LONG) + return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0; + + return __bitmap_and(dst, src1, src2, nbits); +} + +int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits); + +bool __bitmap_subset(const unsigned long *bitmap1, const unsigned long *bitmap2, + unsigned int nbits); + +static inline bool bitmap_subset(const unsigned long *src1, + const unsigned long *src2, unsigned int nbits) +{ + if (nbits >= 0 && nbits <= BITS_PER_LONG) + return !((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits)); + + return __bitmap_subset(src1, src2, nbits); +} + + +#endif /* KVM__BITMAP_H */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 3d31f0acf48e..ae33922f5743 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -11,6 +11,8 @@ #define BITS_PER_BYTE 8 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) +#define BIT_WORD(nr) ((nr) / BITS_PER_LONG) + static inline void set_bit(int nr, unsigned long *addr) { addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG); diff --git a/include/linux/bits.h b/include/linux/bits.h new file mode 100644 index 000000000000..37271dd32633 --- /dev/null +++ b/include/linux/bits.h @@ -0,0 +1,8 @@ +#ifndef LINUX__BITS_H_ +#define LINUX__BITS_H_ + +#define GENMASK(h, l) \ + ((~0UL - (1UL << (l)) + 1) & \ + (~0UL >> (BITS_PER_LONG - 1 - (h)))) + +#endif /* LINUX__BITS_H */ diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h new file mode 100644 index 000000000000..c4be06621cf4 --- /dev/null +++ b/include/linux/cpumask.h @@ -0,0 +1,62 @@ +#ifndef LINUX__CPUMASK_H +#define LINUX__CPUMASK_H + +#include + +#include "linux/bitmap.h" +#include "linux/find.h" +#include "linux/kernel.h" + +typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; + +#define cpumask_bits(maskp) ((maskp)->bits) + +static inline void cpumask_set_cpu(int cpu, cpumask_t *dstp) +{ + set_bit(cpu, cpumask_bits(dstp)); +} + +static inline void cpumask_clear_cpu(int cpu, cpumask_t *dstp) +{ + clear_bit(cpu, cpumask_bits(dstp)); +} + +static inline bool cpumask_test_cpu(int cpu, const cpumask_t *cpumask) +{ + return test_bit(cpu, cpumask_bits((cpumask))); +} + +static inline void cpumask_clear(cpumask_t *dstp) +{ + bitmap_zero(cpumask_bits(dstp), NR_CPUS); +} + +static inline bool cpumask_and(cpumask_t *dstp, cpumask_t *src1p, + cpumask_t *src2p) +{ + return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p), + cpumask_bits(src2p), NR_CPUS); +} + +static inline unsigned int cpumask_next(int n, const struct cpumask *srcp) +{ + return find_next_bit(cpumask_bits(srcp), NR_CPUS, n + 1); +} + +#define for_each_cpu(cpu, maskp) \ + for ((cpu) = -1; \ + (cpu) = cpumask_next((cpu), (maskp)), \ + (cpu) < NR_CPUS;) + +static inline int cpulist_parse(const char *buf, cpumask_t *dstp) +{ + return bitmap_parselist(buf, cpumask_bits(dstp), NR_CPUS); +} + +static inline bool cpumask_subset(const cpumask_t *src1p, + const cpumask_t *src2p) +{ + return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p), NR_CPUS); +} + +#endif /* LINUX__CPUMASK_H */ diff --git a/include/linux/find.h b/include/linux/find.h new file mode 100644 index 000000000000..22819150d146 --- /dev/null +++ b/include/linux/find.h @@ -0,0 +1,30 @@ +#ifndef LINUX__FIND_H +#define LINUX__FIND_H + +#include + +#include "linux/bitops.h" +#include "linux/bits.h" + +unsigned long _find_next_bit(const unsigned long *addr1, + const unsigned long *addr2, unsigned long nbits, + unsigned long start, unsigned long invert); + +static inline +unsigned long find_next_bit(const unsigned long *addr, unsigned long size, + unsigned long offset) +{ + if (size >= 0 && size <= BITS_PER_LONG) { + unsigned long val; + + if (offset >= size) + return size; + + val = *addr & GENMASK(size - 1, offset); + return val ? (unsigned long)__builtin_ctzl(val) : size; + } + + return _find_next_bit(addr, NULL, size, offset, 0); +} + +#endif /* LINUX__FIND_H */ diff --git a/include/linux/kernel.h b/include/linux/kernel.h index f2bff5f12b61..6c22f1c06f6d 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -2,10 +2,16 @@ #ifndef KVM__LINUX_KERNEL_H_ #define KVM__LINUX_KERNEL_H_ +#include "asm/kernel.h" + +#define __round_mask(x, y) ((__typeof__(x))((y)-1)) +#define round_down(x, y) ((x) & ~__round_mask(x, y)) + #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) +#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) diff --git a/mips/include/asm/kernel.h b/mips/include/asm/kernel.h new file mode 100644 index 000000000000..cbceffd02acd --- /dev/null +++ b/mips/include/asm/kernel.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_KERNEL_H +#define __ASM_KERNEL_H + +#define NR_CPUS 256 + +#endif /* __ASM_KERNEL_H */ diff --git a/powerpc/include/asm/kernel.h b/powerpc/include/asm/kernel.h new file mode 100644 index 000000000000..7b4fe88efd65 --- /dev/null +++ b/powerpc/include/asm/kernel.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_KERNEL_H +#define __ASM_KERNEL_H + +#define NR_CPUS 2048 + +#endif /* __ASM_KERNEL_H */ diff --git a/util/bitmap.c b/util/bitmap.c new file mode 100644 index 000000000000..8bae1c91499d --- /dev/null +++ b/util/bitmap.c @@ -0,0 +1,256 @@ +/* + * Taken from Linux kernel version v5.15. + */ +#include +#include +#include +#include + +#include "linux/bitmap.h" +#include "linux/bitops.h" +#include "linux/err.h" + +/* + * Region 9-38:4/10 describes the following bitmap structure: + * 0 9 12 18 38 N + * .........****......****......****.................. + * ^ ^ ^ ^ ^ + * start off group_len end nbits + */ +struct region { + unsigned int start; + unsigned int off; + unsigned int group_len; + unsigned int end; + unsigned int nbits; +}; + +void __bitmap_set(unsigned long *map, unsigned int start, int len) +{ + unsigned long *p = map + BIT_WORD(start); + const unsigned int size = start + len; + int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); + unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); + + while (len - bits_to_set >= 0) { + *p |= mask_to_set; + len -= bits_to_set; + bits_to_set = BITS_PER_LONG; + mask_to_set = ~0UL; + p++; + } + if (len) { + mask_to_set &= BITMAP_LAST_WORD_MASK(size); + *p |= mask_to_set; + } +} + +static void bitmap_set_region(const struct region *r, unsigned long *bitmap) +{ + unsigned int start; + + for (start = r->start; start <= r->end; start += r->group_len) + bitmap_set(bitmap, start, min(r->end - start + 1, r->off)); +} + +static inline bool __end_of_region(char c) +{ + return isspace(c) || c == ','; +} + +static inline bool end_of_str(char c) +{ + return c == '\0' || c == '\n'; +} + +static inline bool end_of_region(char c) +{ + return __end_of_region(c) || end_of_str(c); +} + +/* + * The format allows commas and whitespaces at the beginning + * of the region. + */ +static const char *bitmap_find_region(const char *str) +{ + while (__end_of_region(*str)) + str++; + + return end_of_str(*str) ? NULL : str; +} + +static int bitmap_check_region(const struct region *r) +{ + if (r->start > r->end || r->group_len == 0 || r->off > r->group_len) + return -EINVAL; + + if (r->end >= r->nbits) + return -ERANGE; + + return 0; +} + +static const char *bitmap_getnum(const char *str, unsigned int *num, + unsigned int lastbit) +{ + unsigned long long n; + char *endptr; + + if (str[0] == 'N') { + *num = lastbit; + return str + 1; + } + + n = strtoll(str, &endptr, 10); + /* No digits found. */ + if (n == 0 && endptr == str) + return ERR_PTR(-EINVAL); + /* Check for overflows and negative numbers. */ + if (n == ULLONG_MAX || n != (unsigned long)n || n != (unsigned int)n) + return ERR_PTR(-EOVERFLOW); + + *num = n; + return endptr; +} + +static const char *bitmap_parse_region(const char *str, struct region *r) +{ + unsigned int lastbit = r->nbits - 1; + + if (!strncasecmp(str, "all", 3)) { + r->start = 0; + r->end = lastbit; + str += 3; + + goto check_pattern; + } + + str = bitmap_getnum(str, &r->start, lastbit); + if (IS_ERR(str)) + return str; + + if (end_of_region(*str)) + goto no_end; + + if (*str != '-') + return ERR_PTR(-EINVAL); + + str = bitmap_getnum(str + 1, &r->end, lastbit); + if (IS_ERR(str)) + return str; + +check_pattern: + if (end_of_region(*str)) + goto no_pattern; + + if (*str != ':') + return ERR_PTR(-EINVAL); + + str = bitmap_getnum(str + 1, &r->off, lastbit); + if (IS_ERR(str)) + return str; + + if (*str != '/') + return ERR_PTR(-EINVAL); + + return bitmap_getnum(str + 1, &r->group_len, lastbit); + +no_end: + r->end = r->start; +no_pattern: + r->off = r->end + 1; + r->group_len = r->end + 1; + + return end_of_str(*str) ? NULL : str; +} + +/** + * bitmap_parselist - convert list format ASCII string to bitmap + * @buf: read user string from this buffer; must be terminated + * with a \0 or \n. + * @maskp: write resulting mask here + * @nmaskbits: number of bits in mask to be written + * + * Input format is a comma-separated list of decimal numbers and + * ranges. Consecutively set bits are shown as two hyphen-separated + * decimal numbers, the smallest and largest bit numbers set in + * the range. + * Optionally each range can be postfixed to denote that only parts of it + * should be set. The range will divided to groups of specific size. + * From each group will be used only defined amount of bits. + * Syntax: range:used_size/group_size + * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769 + * The value 'N' can be used as a dynamically substituted token for the + * maximum allowed value; i.e (nmaskbits - 1). Keep in mind that it is + * dynamic, so if system changes cause the bitmap width to change, such + * as more cores in a CPU list, then any ranges using N will also change. + * + * Returns: 0 on success, -errno on invalid input strings. Error values: + * + * - ``-EINVAL``: wrong region format + * - ``-EINVAL``: invalid character in string + * - ``-ERANGE``: bit number specified too large for mask + * - ``-EOVERFLOW``: integer overflow in the input parameters + */ +int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits) +{ + struct region r; + long ret; + + r.nbits = nmaskbits; + bitmap_zero(maskp, r.nbits); + + while (buf) { + buf = bitmap_find_region(buf); + if (buf == NULL) + return 0; + + buf = bitmap_parse_region(buf, &r); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + ret = bitmap_check_region(&r); + if (ret) + return ret; + + bitmap_set_region(&r, maskp); + } + + return 0; +} + +bool __bitmap_and(unsigned long *dst, const unsigned long *src1, + const unsigned long *src2, unsigned int nbits) +{ + unsigned int lim = nbits / BITS_PER_LONG; + unsigned long result = 0; + unsigned int k; + + for (k = 0; k < lim; k++) + result |= (dst[k] = src1[k] & src2[k]); + + if (nbits % BITS_PER_LONG) { + result |= (dst[k] = src1[k] & src2[k] & + BITMAP_LAST_WORD_MASK(nbits)); + } + + return result != 0; +} + +bool __bitmap_subset(const unsigned long *bitmap1, const unsigned long *bitmap2, + unsigned int nbits) +{ + unsigned int k, lim = nbits / BITS_PER_LONG; + + for (k = 0; k < lim; k++) + if (bitmap1[k] & ~bitmap2[k]) + return false; + + if (nbits % BITS_PER_LONG) { + if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(nbits)) + return false; + } + + return true; +} diff --git a/util/find.c b/util/find.c new file mode 100644 index 000000000000..a438f2388e00 --- /dev/null +++ b/util/find.c @@ -0,0 +1,40 @@ +/* + * Taken from Linux kernel version v5.16. + */ +#include "linux/bitmap.h" +#include "linux/find.h" +#include "linux/kernel.h" + +unsigned long _find_next_bit(const unsigned long *addr1, + const unsigned long *addr2, unsigned long nbits, + unsigned long start, unsigned long invert) +{ + unsigned long tmp, mask; + + if (start >= nbits) + return nbits; + + tmp = addr1[start / BITS_PER_LONG]; + if (addr2) + tmp &= addr2[start / BITS_PER_LONG]; + tmp ^= invert; + + /* Handle 1st word. */ + mask = BITMAP_FIRST_WORD_MASK(start); + tmp &= mask; + + start = round_down(start, BITS_PER_LONG); + + while (!tmp) { + start += BITS_PER_LONG; + if (start >= nbits) + return nbits; + + tmp = addr1[start / BITS_PER_LONG]; + if (addr2) + tmp &= addr2[start / BITS_PER_LONG]; + tmp ^= invert; + } + + return min(start + __builtin_ctzl(tmp), nbits); +} diff --git a/x86/include/asm/kernel.h b/x86/include/asm/kernel.h new file mode 100644 index 000000000000..87fad2a0300a --- /dev/null +++ b/x86/include/asm/kernel.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ASM_KERNEL_H +#define _ASM_KERNEL_H + +#define NR_CPUS 8196 + +#endif /* _ASM_KERNEL_H */ From patchwork Tue Apr 12 13:32:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810842 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 767F8C433EF for ; Tue, 12 Apr 2022 13:35:50 +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: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=WNjKdDScLphQ75Mzg4zwNpYGpbeVsW5CSlVItj6cJws=; b=G38GOznu6B0q9R aMWz1VGYCwe5OIsilXWv1M8Ndfc1Tgjuer5ULWtUj55V4DwmLrsR3QaKHnNb9dZIwsTFhw09fctNO 3Z02ftVwuOiwnRNXsuf6gNIa/zfLMl3sinjx7XwYABbMlY3qevXOHbXv4AasLKZ3c2+UGAcS1ysQC ReY1MgiDVGobwF0gRg3Y92O9TzV3yHu7Uq/I4vya7q+3BMm4tjTklBLUKXBcYPuvfz/fYUdnORmyn M1As8fwvBFcgyOGmYifGxvYUVLbSRVQeqbXFmUiVtAFPiAOH3ptz5T1dlTFI9FAOgnXbTJ5FzUNft iHC5iXH46/OsVZK70AaQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGfN-00Eco5-OS; Tue, 12 Apr 2022 13:34:46 +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 1neGdY-00Ebk0-PT for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:54 +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 33FBF150C; Tue, 12 Apr 2022 06:32:52 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C3C1D3F70D; Tue, 12 Apr 2022 06:32:50 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 09/11] update_headers.sh: Sync ABI headers with Linux v5.18-rc2 Date: Tue, 12 Apr 2022 14:32:29 +0100 Message-Id: <20220412133231.35355-10-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063252_958110_7C51D782 X-CRM114-Status: GOOD ( 13.01 ) 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 Signed-off-by: Alexandru Elisei --- arm/aarch64/include/asm/kvm.h | 11 +++++++++++ include/linux/kvm.h | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/arm/aarch64/include/asm/kvm.h b/arm/aarch64/include/asm/kvm.h index 323e251ed37b..c1b6ddc02d2f 100644 --- a/arm/aarch64/include/asm/kvm.h +++ b/arm/aarch64/include/asm/kvm.h @@ -367,6 +367,7 @@ struct kvm_arm_copy_mte_tags { #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_SET_PMU 3 #define KVM_ARM_VCPU_TIMER_CTRL 1 #define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0 #define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 @@ -418,6 +419,16 @@ struct kvm_arm_copy_mte_tags { #define KVM_PSCI_RET_INVAL PSCI_RET_INVALID_PARAMS #define KVM_PSCI_RET_DENIED PSCI_RET_DENIED +/* arm64-specific kvm_run::system_event flags */ +/* + * Reset caused by a PSCI v1.1 SYSTEM_RESET2 call. + * Valid only when the system event has a type of KVM_SYSTEM_EVENT_RESET. + */ +#define KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2 (1ULL << 0) + +/* run->fail_entry.hardware_entry_failure_reason codes. */ +#define KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED (1ULL << 0) + #endif #endif /* __ARM_KVM_H__ */ diff --git a/include/linux/kvm.h b/include/linux/kvm.h index 507ee1f2aa96..91a6fe4e02c0 100644 --- a/include/linux/kvm.h +++ b/include/linux/kvm.h @@ -562,9 +562,12 @@ struct kvm_s390_mem_op { __u32 op; /* type of operation */ __u64 buf; /* buffer in userspace */ union { - __u8 ar; /* the access register number */ + struct { + __u8 ar; /* the access register number */ + __u8 key; /* access key, ignored if flag unset */ + }; __u32 sida_offset; /* offset into the sida */ - __u8 reserved[32]; /* should be set to 0 */ + __u8 reserved[32]; /* ignored */ }; }; /* types for kvm_s390_mem_op->op */ @@ -572,9 +575,12 @@ struct kvm_s390_mem_op { #define KVM_S390_MEMOP_LOGICAL_WRITE 1 #define KVM_S390_MEMOP_SIDA_READ 2 #define KVM_S390_MEMOP_SIDA_WRITE 3 +#define KVM_S390_MEMOP_ABSOLUTE_READ 4 +#define KVM_S390_MEMOP_ABSOLUTE_WRITE 5 /* flags for kvm_s390_mem_op->flags */ #define KVM_S390_MEMOP_F_CHECK_ONLY (1ULL << 0) #define KVM_S390_MEMOP_F_INJECT_EXCEPTION (1ULL << 1) +#define KVM_S390_MEMOP_F_SKEY_PROTECTION (1ULL << 2) /* for KVM_INTERRUPT */ struct kvm_interrupt { @@ -1135,6 +1141,9 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_XSAVE2 208 #define KVM_CAP_SYS_ATTRIBUTES 209 #define KVM_CAP_PPC_AIL_MODE_3 210 +#define KVM_CAP_S390_MEM_OP_EXTENSION 211 +#define KVM_CAP_PMU_CAPABILITY 212 +#define KVM_CAP_DISABLE_QUIRKS2 213 #ifdef KVM_CAP_IRQ_ROUTING @@ -1971,6 +1980,8 @@ struct kvm_dirty_gfn { #define KVM_BUS_LOCK_DETECTION_OFF (1 << 0) #define KVM_BUS_LOCK_DETECTION_EXIT (1 << 1) +#define KVM_PMU_CAP_DISABLE (1 << 0) + /** * struct kvm_stats_header - Header of per vm/vcpu binary statistics data. * @flags: Some extra information for header, always 0 for now. From patchwork Tue Apr 12 13:32:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810843 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 D39D3C433F5 for ; Tue, 12 Apr 2022 13:36:16 +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: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=MtEYbF23lgW+hPcZuftI0km8YRiXrLkpVdBh2ZfxecA=; b=vHJccaYe3NVsVp 80L1+Yn1Qui3isglDBAYhoc7WFGttCuSaZS4JwSEqUVZDyJRdiksnqmlQdKwI/KXPzYLtnFb8zRLT Iy71+7M5qWoJBSIR8Jm63+7+Mc84uz3IEUAmRJ0YoF70RxHfY03TYBzNdB70qZhaGhJfcM4qITVW3 xZ6Z5OxQBUdriaKod/KUgWM37cT1GxgAq+8lcGCTVOumx3cBnt+IQpxKJgJ6blJrg5mnE1933h1Ss xWLxVCUrwBmnGhlpFea056B++2Lum0oZs+PTLs/7b4IVP1+7Y1WB7DnmxghXNJxGEzuc/gQTLYPdC 6q0am6TjSmdI/7GaJCZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGff-00Ed0z-5S; Tue, 12 Apr 2022 13:35:03 +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 1neGda-00Ebiw-8P for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:56 +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 DAE5D1516; Tue, 12 Apr 2022 06:32:53 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 747383F70D; Tue, 12 Apr 2022 06:32:52 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 10/11] arm64: Add support for KVM_ARM_VCPU_PMU_V3_SET_PMU Date: Tue, 12 Apr 2022 14:32:30 +0100 Message-Id: <20220412133231.35355-11-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063254_440225_9D034675 X-CRM114-Status: GOOD ( 25.10 ) 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 The KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU ioctl is used to assign a physical PMU to the events that KVM creates when emulating the PMU for that VCPU. This is useful on heterogeneous systems, when there is more than one hardware PMU present. All VCPUs must have the same PMU assigned. The assumption that is made in the implementation is that the user will pin the kvmtool process on a set of CPUs that share the same PMU. This allows kvmtool to set the same PMU for all VCPUs from the main thread, instead of in the individual VCPU threads. If a VCPU thread migrates to a CPU which has a different a PMU than the CPU on which the main thread was executing when the PMU was set, the KVM_RUN ioctl will fail with kvm_run.exit_reason set to KVM_EXIT_FAIL_ENTRY, and kvm_run.fail_entry will be populated with the physical CPU ID on which the VCPU tried to execute. Signed-off-by: Alexandru Elisei --- arm/aarch64/include/kvm/kvm-config-arch.h | 4 +- arm/aarch64/pmu.c | 147 +++++++++++++++++++++- 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/arm/aarch64/include/kvm/kvm-config-arch.h b/arm/aarch64/include/kvm/kvm-config-arch.h index 3d39cd6d1f4e..b08ee60ee1c4 100644 --- a/arm/aarch64/include/kvm/kvm-config-arch.h +++ b/arm/aarch64/include/kvm/kvm-config-arch.h @@ -5,7 +5,9 @@ OPT_BOOLEAN('\0', "aarch32", &(cfg)->aarch32_guest, \ "Run AArch32 guest"), \ OPT_BOOLEAN('\0', "pmu", &(cfg)->has_pmuv3, \ - "Create PMUv3 device"), \ + "Create PMUv3 device. The emulated PMU will be" \ + " set to the PMU associated with the" \ + " main thread"), \ OPT_BOOLEAN('\0', "disable-mte", &(cfg)->mte_disabled, \ "Disable Memory Tagging Extension"), \ OPT_U64('\0', "kaslr-seed", &(cfg)->kaslr_seed, \ diff --git a/arm/aarch64/pmu.c b/arm/aarch64/pmu.c index ac5b7bcd6ca9..869a150cd6d3 100644 --- a/arm/aarch64/pmu.c +++ b/arm/aarch64/pmu.c @@ -1,3 +1,9 @@ +#include +#include + +#include "linux/cpumask.h" +#include "linux/err.h" + #include "kvm/fdt.h" #include "kvm/kvm.h" #include "kvm/kvm-cpu.h" @@ -7,6 +13,17 @@ #include "asm/pmu.h" +static bool pmu_has_attr(struct kvm_cpu *vcpu, u64 attr) +{ + struct kvm_device_attr pmu_attr = { + .group = KVM_ARM_VCPU_PMU_V3_CTRL, + .attr = attr, + }; + int ret = ioctl(vcpu->vcpu_fd, KVM_HAS_DEVICE_ATTR, &pmu_attr); + + return ret == 0; +} + static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr) { struct kvm_device_attr pmu_attr = { @@ -16,8 +33,7 @@ static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr) }; int ret; - ret = ioctl(vcpu->vcpu_fd, KVM_HAS_DEVICE_ATTR, &pmu_attr); - if (!ret) { + if (pmu_has_attr(vcpu, attr)) { ret = ioctl(vcpu->vcpu_fd, KVM_SET_DEVICE_ATTR, &pmu_attr); if (ret) die_perror("PMU KVM_SET_DEVICE_ATTR"); @@ -26,11 +42,124 @@ static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr) } } +#define SYS_EVENT_SOURCE "/sys/bus/event_source/devices/" +/* + * int is 32 bits and INT_MAX translates in decimal to 2 * 10^9. + * Make room for newline and NUL. + */ +#define PMU_ID_MAXLEN 12 + +/* + * In the case of homogeneous systems, there only one hardware PMU, and all + * VCPUs will use the same PMU, regardless of where the attribute gets set. + * + * For heterogeneous systems, the assumption is that the user has pinned the VM + * (via taskset or similar) to a set of CPUs that share the same hardware PMU. + * This simplifies things for kvmtool, as correctness is not affected by setting + * the PMU for each VCPU from the main thread, instead of setting it from each + * individual VCPU thread. + */ +static int find_pmu(void) +{ + char buf[PMU_ID_MAXLEN]; + struct dirent *dirent; + char *cpulist, *path; + int pmu_id = -ENXIO; + unsigned long val; + cpumask_t cpumask; + ssize_t fd_sz; + int this_cpu; + int fd, ret; + DIR *dir; + + memset(buf, 0, PMU_ID_MAXLEN); + + this_cpu = sched_getcpu(); + if (this_cpu < 0) + return -errno; + + cpulist = calloc(1, PAGE_SIZE); + if (!cpulist) + die_perror("calloc"); + + path = calloc(1, PAGE_SIZE); + if (!path) + die_perror("calloc"); + + dir = opendir(SYS_EVENT_SOURCE); + if (!dir) { + pmu_id = -errno; + goto out_free; + } + + /* Make the compiler happy by copying the NUL terminating byte. */ + strncpy(path, SYS_EVENT_SOURCE, strlen(SYS_EVENT_SOURCE) + 1); + + while ((dirent = readdir(dir))) { + if (dirent->d_type != DT_LNK) + continue; + + strcat(path, dirent->d_name); + strcat(path, "/cpus"); + fd = open(path, O_RDONLY); + if (fd < 0) + goto next_dir; + + fd_sz = read_file(fd, cpulist, PAGE_SIZE); + if (fd_sz < 0) { + pmu_id = -errno; + goto out_free; + } + close(fd); + + ret = cpulist_parse(cpulist, &cpumask); + if (ret) { + pmu_id = ret; + goto out_free; + } + + if (!cpumask_test_cpu(this_cpu, &cpumask)) + goto next_dir; + + strcpy(&path[strlen(path) - 4], "type"); + fd = open(path, O_RDONLY); + if (fd < 0) + goto next_dir; + + fd_sz = read_file(fd, buf, PMU_ID_MAXLEN - 1); + if (fd_sz < 0) { + pmu_id = -errno; + goto out_free; + } + close(fd); + + val = strtoul(buf, NULL, 10); + if (val > INT_MAX) { + pmu_id = -EOVERFLOW; + goto out_free; + } + pmu_id = (int)val; + pr_debug("Using PMU %s (id %d)", dirent->d_name, pmu_id); + break; + +next_dir: + /* Reset path. */ + memset(&path[strlen(SYS_EVENT_SOURCE)], '\0', + strlen(path) - strlen(SYS_EVENT_SOURCE)); + } + +out_free: + free(path); + free(cpulist); + return pmu_id; +} + void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) { const char compatible[] = "arm,armv8-pmuv3"; int irq = KVM_ARM_PMUv3_PPI; struct kvm_cpu *vcpu; + int pmu_id = -ENXIO; int i; u32 cpu_mask = (((1 << kvm->nrcpus) - 1) << GIC_FDT_IRQ_PPI_CPU_SHIFT) \ @@ -44,9 +173,23 @@ void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) if (!kvm->cfg.arch.has_pmuv3) return; + if (pmu_has_attr(kvm->cpus[0], KVM_ARM_VCPU_PMU_V3_SET_PMU)) { + pmu_id = find_pmu(); + if (pmu_id < 0) { + pr_debug("Failed to find a PMU (errno: %d), " + "PMU events might not work", -pmu_id); + } + } + for (i = 0; i < kvm->nrcpus; i++) { vcpu = kvm->cpus[i]; set_pmu_attr(vcpu, &irq, KVM_ARM_VCPU_PMU_V3_IRQ); + /* + * PMU IDs 0-5 are reserved; a positive value means a PMU was + * found. + */ + if (pmu_id > 0) + set_pmu_attr(vcpu, &pmu_id, KVM_ARM_VCPU_PMU_V3_SET_PMU); set_pmu_attr(vcpu, NULL, KVM_ARM_VCPU_PMU_V3_INIT); } From patchwork Tue Apr 12 13:32:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12810844 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 A9365C433FE for ; Tue, 12 Apr 2022 13:36:46 +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: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=DnBzj1/IicGYzGy7hCZTfgKxUeRQGKepyO+f/rZuWM4=; b=E7cOFInFHtaaXA zkgcm0zPX/GD/SXWjdqYj4XjajPOipeaw2IrwsALHlHLfNVwflTxHEpox7a7IXdbf2R7wcLCJQi/K Ilansia7sfj8Y/JL3ypAeNKO0y37ZiRQ0YuUavPbvgT5oBI2mDqhCAk4YvnZ6sLCWRtmsqrT2UWRJ BNogbE5p0x+YWn9ZDfTdiBH1P1ZD3KGDcgiQraiDhiujdeHQgWqL6wlhexO2msWHjx4RFDpJoQxS4 hIEa7uGCiMklxaB/BpaPp8JUXsO9YnqMpJPtQFfq4K1nE3DS3OgMu07YfB0ASB3X54UXABrV5E0mY KRQjqRIt91kWz0iyCmhA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1neGgD-00EdO3-9e; Tue, 12 Apr 2022 13:35:37 +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 1neGdb-00Ebk0-QT for linux-arm-kernel@lists.infradead.org; Tue, 12 Apr 2022 13:32:57 +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 8E412150C; Tue, 12 Apr 2022 06:32:55 -0700 (PDT) Received: from e121798.arm.com (unknown [10.57.11.98]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 288353F70D; Tue, 12 Apr 2022 06:32:54 -0700 (PDT) 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, andre.przywara@arm.com Subject: [PATCH v3 kvmtool 11/11] arm64: Add --vcpu-affinity command line argument Date: Tue, 12 Apr 2022 14:32:31 +0100 Message-Id: <20220412133231.35355-12-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220412133231.35355-1-alexandru.elisei@arm.com> References: <20220412133231.35355-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-20220412_063255_978539_BD4D8E98 X-CRM114-Status: GOOD ( 28.73 ) 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 Add a new command line argument, --vcpu-affinity, to set the CPU affinity for the VCPUs. The affinity is expressed as a cpulist and will apply to all VCPU threads. This gives the user a second option for choosing the PMU on a heterogeneous system. The PMU setup code, when --vcpu-affinity is specified, will search for the PMU associated with the CPUs specified with this command line argument instead of the PMU associated with the CPU on which the main thread is executing. Signed-off-by: Alexandru Elisei --- arm/aarch64/include/kvm/kvm-config-arch.h | 7 +- arm/aarch64/kvm-cpu.c | 9 +++ arm/aarch64/kvm.c | 32 +++++++++ arm/aarch64/pmu.c | 79 +++++++++++++++++------ arm/include/arm-common/kvm-arch.h | 7 ++ arm/include/arm-common/kvm-config-arch.h | 1 + include/linux/cpumask.h | 5 ++ 7 files changed, 118 insertions(+), 22 deletions(-) diff --git a/arm/aarch64/include/kvm/kvm-config-arch.h b/arm/aarch64/include/kvm/kvm-config-arch.h index b08ee60ee1c4..eae8080d3fd9 100644 --- a/arm/aarch64/include/kvm/kvm-config-arch.h +++ b/arm/aarch64/include/kvm/kvm-config-arch.h @@ -1,15 +1,20 @@ #ifndef KVM__KVM_CONFIG_ARCH_H #define KVM__KVM_CONFIG_ARCH_H +int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset); + #define ARM_OPT_ARCH_RUN(cfg) \ OPT_BOOLEAN('\0', "aarch32", &(cfg)->aarch32_guest, \ "Run AArch32 guest"), \ OPT_BOOLEAN('\0', "pmu", &(cfg)->has_pmuv3, \ "Create PMUv3 device. The emulated PMU will be" \ " set to the PMU associated with the" \ - " main thread"), \ + " main thread, unless --vcpu-affinity is set"), \ OPT_BOOLEAN('\0', "disable-mte", &(cfg)->mte_disabled, \ "Disable Memory Tagging Extension"), \ + OPT_CALLBACK('\0', "vcpu-affinity", kvm, "cpulist", \ + "Specify the CPU affinity that will apply to " \ + "all VCPUs", vcpu_affinity_parser, kvm), \ OPT_U64('\0', "kaslr-seed", &(cfg)->kaslr_seed, \ "Specify random seed for Kernel Address Space " \ "Layout Randomization (KASLR)"), \ diff --git a/arm/aarch64/kvm-cpu.c b/arm/aarch64/kvm-cpu.c index 3b6224a599c8..4ac0fafae06b 100644 --- a/arm/aarch64/kvm-cpu.c +++ b/arm/aarch64/kvm-cpu.c @@ -88,7 +88,16 @@ static void reset_vcpu_aarch64(struct kvm_cpu *vcpu) { struct kvm *kvm = vcpu->kvm; struct kvm_one_reg reg; + cpu_set_t *affinity; u64 data; + int ret; + + affinity = kvm->arch.vcpu_affinity_cpuset; + if (affinity) { + ret = sched_setaffinity(0, sizeof(cpu_set_t), affinity); + if (ret == -1) + die_perror("sched_setaffinity"); + } reg.addr = (u64)&data; diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index 28d608d98831..1b992dd592d4 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -3,8 +3,40 @@ #include #include +#include + #include +int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset) +{ + struct kvm *kvm = opt->ptr; + const char *cpulist = arg; + cpumask_t *cpumask; + int cpu, ret; + + kvm->cfg.arch.vcpu_affinity = cpulist; + + cpumask = calloc(1, cpumask_size()); + if (!cpumask) + die_perror("calloc"); + + ret = cpulist_parse(cpulist, cpumask); + if (ret) { + free(cpumask); + return ret; + } + + kvm->arch.vcpu_affinity_cpuset = CPU_ALLOC(NR_CPUS); + if (!kvm->arch.vcpu_affinity_cpuset) + die_perror("CPU_ALLOC"); + CPU_ZERO_S(CPU_ALLOC_SIZE(NR_CPUS), kvm->arch.vcpu_affinity_cpuset); + + for_each_cpu(cpu, cpumask) + CPU_SET(cpu, kvm->arch.vcpu_affinity_cpuset); + + return 0; +} + /* * Return the TEXT_OFFSET value that the guest kernel expects. Note * that pre-3.17 kernels expose this value using the native endianness diff --git a/arm/aarch64/pmu.c b/arm/aarch64/pmu.c index 869a150cd6d3..5f189b32b6d0 100644 --- a/arm/aarch64/pmu.c +++ b/arm/aarch64/pmu.c @@ -49,34 +49,19 @@ static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr) */ #define PMU_ID_MAXLEN 12 -/* - * In the case of homogeneous systems, there only one hardware PMU, and all - * VCPUs will use the same PMU, regardless of where the attribute gets set. - * - * For heterogeneous systems, the assumption is that the user has pinned the VM - * (via taskset or similar) to a set of CPUs that share the same hardware PMU. - * This simplifies things for kvmtool, as correctness is not affected by setting - * the PMU for each VCPU from the main thread, instead of setting it from each - * individual VCPU thread. - */ -static int find_pmu(void) +static int find_pmu_cpumask(struct kvm *kvm, cpumask_t *cpumask) { + cpumask_t pmu_cpumask, tmp; char buf[PMU_ID_MAXLEN]; struct dirent *dirent; char *cpulist, *path; int pmu_id = -ENXIO; unsigned long val; - cpumask_t cpumask; ssize_t fd_sz; - int this_cpu; int fd, ret; DIR *dir; - memset(buf, 0, PMU_ID_MAXLEN); - - this_cpu = sched_getcpu(); - if (this_cpu < 0) - return -errno; + memset(buf, 0, sizeof(buf)); cpulist = calloc(1, PAGE_SIZE); if (!cpulist) @@ -112,15 +97,27 @@ static int find_pmu(void) } close(fd); - ret = cpulist_parse(cpulist, &cpumask); + ret = cpulist_parse(cpulist, &pmu_cpumask); if (ret) { pmu_id = ret; goto out_free; } - if (!cpumask_test_cpu(this_cpu, &cpumask)) + if (!cpumask_and(&tmp, cpumask, &pmu_cpumask)) goto next_dir; + /* + * One CPU cannot more than one PMU, hence the set of CPUs which + * share PMU A and the set of CPUs which share PMU B are + * disjoint. If the target CPUs and the current PMU have at + * least one CPU in common, but the target CPUs is not a subset + * of the current PMU, then a PMU which is associated with all + * the target CPUs does not exist. Stop searching for a PMU when + * this happens. + */ + if (!cpumask_subset(cpumask, &pmu_cpumask)) + goto out_free; + strcpy(&path[strlen(path) - 4], "type"); fd = open(path, O_RDONLY); if (fd < 0) @@ -154,6 +151,46 @@ out_free: return pmu_id; } +/* + * In the case of homogeneous systems, there only one hardware PMU, and all + * VCPUs will use the same PMU, regardless of the physical CPUs on which the + * VCPU threads will be executing. + * + * For heterogeneous systems, there are 2 ways for the user to ensure that the + * VM runs on CPUs that have the same PMU: + * + * 1. By pinning the entire VM to the desired CPUs, in which case kvmtool will + * choose the PMU associated with the CPU on which the main thread is executing + * (the thread that calls find_pmu()). + * + * 2. By setting the affinity mask for the VCPUs with the --vcpu-affinity + * command line argument. All CPUs in the affinity mask must have the same PMU, + * otherwise kvmtool will not be able to set a PMU. + */ +static int find_pmu(struct kvm *kvm) +{ + cpumask_t *cpumask; + int i, this_cpu; + + cpumask = calloc(1, cpumask_size()); + if (!cpumask) + die_perror("calloc"); + + if (!kvm->arch.vcpu_affinity_cpuset) { + this_cpu = sched_getcpu(); + if (this_cpu < 0) + return -errno; + cpumask_set_cpu(this_cpu, cpumask); + } else { + for (i = 0; i < CPU_SETSIZE; i ++) { + if (CPU_ISSET(i, kvm->arch.vcpu_affinity_cpuset)) + cpumask_set_cpu(i, cpumask); + } + } + + return find_pmu_cpumask(kvm, cpumask); +} + void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) { const char compatible[] = "arm,armv8-pmuv3"; @@ -174,7 +211,7 @@ void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm) return; if (pmu_has_attr(kvm->cpus[0], KVM_ARM_VCPU_PMU_V3_SET_PMU)) { - pmu_id = find_pmu(); + pmu_id = find_pmu(kvm); if (pmu_id < 0) { pr_debug("Failed to find a PMU (errno: %d), " "PMU events might not work", -pmu_id); diff --git a/arm/include/arm-common/kvm-arch.h b/arm/include/arm-common/kvm-arch.h index 43b1f77df394..fc55360d4d15 100644 --- a/arm/include/arm-common/kvm-arch.h +++ b/arm/include/arm-common/kvm-arch.h @@ -2,6 +2,11 @@ #define ARM_COMMON__KVM_ARCH_H #include +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include + #include #include @@ -105,6 +110,8 @@ struct kvm_arch { u64 initrd_guest_start; u64 initrd_size; u64 dtb_guest_start; + + cpu_set_t *vcpu_affinity_cpuset; }; #endif /* ARM_COMMON__KVM_ARCH_H */ diff --git a/arm/include/arm-common/kvm-config-arch.h b/arm/include/arm-common/kvm-config-arch.h index cdcbf235ac4e..9949bfe47ba8 100644 --- a/arm/include/arm-common/kvm-config-arch.h +++ b/arm/include/arm-common/kvm-config-arch.h @@ -5,6 +5,7 @@ struct kvm_config_arch { const char *dump_dtb_filename; + const char *vcpu_affinity; unsigned int force_cntfrq; bool virtio_trans_pci; bool aarch32_guest; diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h index c4be06621cf4..e6b764b5dd89 100644 --- a/include/linux/cpumask.h +++ b/include/linux/cpumask.h @@ -11,6 +11,11 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; #define cpumask_bits(maskp) ((maskp)->bits) +static inline unsigned int cpumask_size(void) +{ + return BITS_TO_LONGS(NR_CPUS) * sizeof(long); +} + static inline void cpumask_set_cpu(int cpu, cpumask_t *dstp) { set_bit(cpu, cpumask_bits(dstp));