From patchwork Tue May 7 22:10:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 13657883 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CF8A155A34; Tue, 7 May 2024 22:11:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715119861; cv=none; b=Jw9+sFvL1oikh1/acxRcFJbYkoRhibQ6/W6uBUcRNDpl/ZWKS5JBm/+1loxSAfmuX6JMSXl6D2sW2bi6P1Zo1mm80hR8fhYhR049h+WEZ91hYdeK/G/J5AkAN8eOBO7L441xhDSFqMFO6tG7YggYsMEcc3VE66qL7iSTe4/oNnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715119861; c=relaxed/simple; bh=K+lADN8+8te0U6GGFL5Z7aSuGApQDoMThtNo4JVVGeI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZMPAY865DxMCnMM1ET+z/KECNPgRP3D3fl1pIhunn85/rNKxDA1ZRhRgBUpaotvuKR7rZVDpGH+QWq+US530Kq01WHYbNMPuZgRmSTCyynOk0xV0U45HsDt4Oa1AC4/LNP6/sSH6ju29xeLnaUBZYHefV0iI7l7qn8CotpGxlcs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SMSbKwsw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SMSbKwsw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B987FC4AF63; Tue, 7 May 2024 22:10:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715119861; bh=K+lADN8+8te0U6GGFL5Z7aSuGApQDoMThtNo4JVVGeI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SMSbKwswQmIMfxblP4aaYMu3qQWkhq9IKmv6Qp7M+RvaOiCOs9X1Q7D8aEGZp2bXK 1uUG46DSLzUAlNWNN4TqhgqZoJY96fuEyXMEgPn+E7fOwqfMgJYgaJI+3JPw92LqEM 6/xPCsfV8N4fEg9PtMrzAAPii75eqcaLqqu5PNRmvHPcnvXOaUiwD2RAo1neHvVfap 30zrfHIkbK/LhevV7pYLCf7LTlcAVX7+sARqLbKIDBobvzg+Gq1XSV4evvMQQ/xk5B Sp1F4BwMDvdZ1BYWwunKCiDo+o3Sqi/vMP3LA7IIsBornZLOmT08FvWAwhl5rCxzTi u0+kKKurR8NJA== From: KP Singh To: linux-security-module@vger.kernel.org, bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, jackmanb@google.com, renauld@google.com, paul@paul-moore.com, casey@schaufler-ca.com, song@kernel.org, revest@chromium.org, keescook@chromium.org, Andrii Nakryiko , KP Singh Subject: [PATCH bpf-next v10 1/5] kernel: Add helper macros for loop unrolling Date: Wed, 8 May 2024 00:10:41 +0200 Message-ID: <20240507221045.551537-2-kpsingh@kernel.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240507221045.551537-1-kpsingh@kernel.org> References: <20240507221045.551537-1-kpsingh@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This helps in easily initializing blocks of code (e.g. static calls and keys). UNROLL(N, MACRO, __VA_ARGS__) calls MACRO N times with the first argument as the index of the iteration. This allows string pasting to create unique tokens for variable names, function calls etc. As an example: #include #define MACRO(N, a, b) \ int add_##N(int a, int b) \ { \ return a + b + N; \ } UNROLL(2, MACRO, x, y) expands to: int add_0(int x, int y) { return x + y + 0; } int add_1(int x, int y) { return x + y + 1; } Reviewed-by: Kees Cook Reviewed-by: Casey Schaufler Acked-by: Song Liu Acked-by: Andrii Nakryiko Signed-off-by: KP Singh --- include/linux/unroll.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 include/linux/unroll.h diff --git a/include/linux/unroll.h b/include/linux/unroll.h new file mode 100644 index 000000000000..d42fd6366373 --- /dev/null +++ b/include/linux/unroll.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (C) 2023 Google LLC. + */ + +#ifndef __UNROLL_H +#define __UNROLL_H + +#include + +#define UNROLL(N, MACRO, args...) CONCATENATE(__UNROLL_, N)(MACRO, args) + +#define __UNROLL_0(MACRO, args...) +#define __UNROLL_1(MACRO, args...) __UNROLL_0(MACRO, args) MACRO(0, args) +#define __UNROLL_2(MACRO, args...) __UNROLL_1(MACRO, args) MACRO(1, args) +#define __UNROLL_3(MACRO, args...) __UNROLL_2(MACRO, args) MACRO(2, args) +#define __UNROLL_4(MACRO, args...) __UNROLL_3(MACRO, args) MACRO(3, args) +#define __UNROLL_5(MACRO, args...) __UNROLL_4(MACRO, args) MACRO(4, args) +#define __UNROLL_6(MACRO, args...) __UNROLL_5(MACRO, args) MACRO(5, args) +#define __UNROLL_7(MACRO, args...) __UNROLL_6(MACRO, args) MACRO(6, args) +#define __UNROLL_8(MACRO, args...) __UNROLL_7(MACRO, args) MACRO(7, args) +#define __UNROLL_9(MACRO, args...) __UNROLL_8(MACRO, args) MACRO(8, args) +#define __UNROLL_10(MACRO, args...) __UNROLL_9(MACRO, args) MACRO(9, args) +#define __UNROLL_11(MACRO, args...) __UNROLL_10(MACRO, args) MACRO(10, args) +#define __UNROLL_12(MACRO, args...) __UNROLL_11(MACRO, args) MACRO(11, args) +#define __UNROLL_13(MACRO, args...) __UNROLL_12(MACRO, args) MACRO(12, args) +#define __UNROLL_14(MACRO, args...) __UNROLL_13(MACRO, args) MACRO(13, args) +#define __UNROLL_15(MACRO, args...) __UNROLL_14(MACRO, args) MACRO(14, args) +#define __UNROLL_16(MACRO, args...) __UNROLL_15(MACRO, args) MACRO(15, args) +#define __UNROLL_17(MACRO, args...) __UNROLL_16(MACRO, args) MACRO(16, args) +#define __UNROLL_18(MACRO, args...) __UNROLL_17(MACRO, args) MACRO(17, args) +#define __UNROLL_19(MACRO, args...) __UNROLL_18(MACRO, args) MACRO(18, args) +#define __UNROLL_20(MACRO, args...) __UNROLL_19(MACRO, args) MACRO(19, args) + +#endif /* __UNROLL_H */