From patchwork Mon Mar 28 17:50:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12794065 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 556D0C433FE for ; Mon, 28 Mar 2022 17:55:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244920AbiC1R5g (ORCPT ); Mon, 28 Mar 2022 13:57:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245130AbiC1R4u (ORCPT ); Mon, 28 Mar 2022 13:56:50 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2E271F619; Mon, 28 Mar 2022 10:55:03 -0700 (PDT) Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.206]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4KS0fb2wNQz67y8J; Tue, 29 Mar 2022 01:53:11 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 28 Mar 2022 19:55:00 +0200 From: Roberto Sassu To: , , , , , , , , , CC: , , , , , , , , , , Roberto Sassu Subject: [PATCH 15/18] bpf-preload: Generate code of kernel module to preload Date: Mon, 28 Mar 2022 19:50:30 +0200 Message-ID: <20220328175033.2437312-16-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220328175033.2437312-1-roberto.sassu@huawei.com> References: <20220328175033.2437312-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml754-chm.china.huawei.com (10.201.108.204) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Since every function is automatically generated and placed to the light skeleton, the kernel module for preloading an eBPF program is very small and with a well-defined structure. The only variable part is the path of the light skeleton. Introduce the new 'subcommand' module of the 'gen' bpftool command, which takes the path of the light skeleton to be included in the #include directive and generates the code of the kernel module to preload the eBPF program. Signed-off-by: Roberto Sassu --- kernel/bpf/preload/bpf_preload_kern.c | 1 + kernel/bpf/preload/iterators/Makefile | 7 +++-- .../bpf/bpftool/Documentation/bpftool-gen.rst | 8 +++++ tools/bpf/bpftool/bash-completion/bpftool | 4 +++ tools/bpf/bpftool/gen.c | 31 +++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/preload/bpf_preload_kern.c b/kernel/bpf/preload/bpf_preload_kern.c index c6d97872225b..048bca3ba499 100644 --- a/kernel/bpf/preload/bpf_preload_kern.c +++ b/kernel/bpf/preload/bpf_preload_kern.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 +/* THIS FILE IS AUTOGENERATED! */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include #include diff --git a/kernel/bpf/preload/iterators/Makefile b/kernel/bpf/preload/iterators/Makefile index d36a822d3e16..9dcad1c5c44b 100644 --- a/kernel/bpf/preload/iterators/Makefile +++ b/kernel/bpf/preload/iterators/Makefile @@ -35,17 +35,20 @@ endif .PHONY: all clean -all: iterators.lskel.h +all: iterators.lskel.h bpf_preload_kern.c clean: $(call msg,CLEAN) $(Q)rm -rf $(OUTPUT) iterators +bpf_preload_kern.c: iterators.lskel.h $(BPFTOOL) + $(call msg,GEN-PRELOAD,$@) + $(Q)$(BPFTOOL) gen module iterators/iterators.lskel.h $< > ../$@ + iterators.lskel.h: $(OUTPUT)/iterators.bpf.o | $(BPFTOOL) $(call msg,GEN-SKEL,$@) $(Q)$(BPFTOOL) gen skeleton -L -P $< > $@ - $(OUTPUT)/iterators.bpf.o: iterators.bpf.c $(BPFOBJ) | $(OUTPUT) $(call msg,BPF,$@) $(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES) \ diff --git a/tools/bpf/bpftool/Documentation/bpftool-gen.rst b/tools/bpf/bpftool/Documentation/bpftool-gen.rst index 74bbefa28212..6d29d2b1e4e2 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-gen.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-gen.rst @@ -27,6 +27,7 @@ GEN COMMANDS | **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*] | **bpftool** **gen subskeleton** *FILE* [**name** *OBJECT_NAME*] | **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...] +| **bpftool** **gen module** *FILE* | **bpftool** **gen help** DESCRIPTION @@ -195,6 +196,13 @@ DESCRIPTION Check examples bellow for more information how to use it. + **bpftool** **gen module** *FILE* + Generate the code of a kernel module including the light + skeleton of an eBPF program to preload. The only variable part + is the path of the light skeleton. All kernel modules call + load_skel() and free_objs_and_skel() respectively in the init + and fini module entrypoints. + **bpftool gen help** Print short help message. diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index 6e433e86fb26..82e8716fd3ad 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -1019,6 +1019,10 @@ _bpftool() _filedir return 0 ;; + module) + _filedir + return 0 + ;; *) [[ $prev == $object ]] && \ COMPREPLY=( $( compgen -W 'object skeleton subskeleton help min_core_btf' -- "$cur" ) ) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index af939183f57a..77ab78884285 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -1898,6 +1898,35 @@ static int do_object(int argc, char **argv) return err; } +static int do_module(int argc, char **argv) +{ + const char *skeleton_file; + + if (!REQ_ARGS(1)) { + usage(); + return -1; + } + + skeleton_file = GET_ARG(); + + codegen("\ + \n\ + // SPDX-License-Identifier: GPL-2.0 \n\ + /* THIS FILE IS AUTOGENERATED! */ \n\ + #define pr_fmt(fmt) KBUILD_MODNAME \": \" fmt \n\ + #include \n\ + #include \n\ + #include \n\ + #include \"%s\" \n\ + \n\ + late_initcall(load_skel); \n\ + module_exit(free_objs_and_skel); \n\ + MODULE_LICENSE(\"GPL\"); \n\ + ", skeleton_file); + + return 0; +} + static int do_help(int argc, char **argv) { if (json_output) { @@ -1910,6 +1939,7 @@ static int do_help(int argc, char **argv) " %1$s %2$s skeleton FILE [name OBJECT_NAME]\n" " %1$s %2$s subskeleton FILE [name OBJECT_NAME]\n" " %1$s %2$s min_core_btf INPUT OUTPUT OBJECT [OBJECT...]\n" + " %1$s %2$s module SKELETON_FILE\n" " %1$s %2$s help\n" "\n" " " HELP_SPEC_OPTIONS " |\n" @@ -2508,6 +2538,7 @@ static const struct cmd cmds[] = { { "skeleton", do_skeleton }, { "subskeleton", do_subskeleton }, { "min_core_btf", do_min_core_btf}, + { "module", do_module}, { "help", do_help }, { 0 } };