[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220328175033.2437312-7-roberto.sassu@huawei.com>
Date: Mon, 28 Mar 2022 19:50:21 +0200
From: Roberto Sassu <roberto.sassu@...wei.com>
To: <corbet@....net>, <viro@...iv.linux.org.uk>, <ast@...nel.org>,
<daniel@...earbox.net>, <andrii@...nel.org>, <kpsingh@...nel.org>,
<shuah@...nel.org>, <mcoquelin.stm32@...il.com>,
<alexandre.torgue@...s.st.com>, <zohar@...ux.ibm.com>
CC: <linux-doc@...r.kernel.org>, <linux-fsdevel@...r.kernel.org>,
<netdev@...r.kernel.org>, <bpf@...r.kernel.org>,
<linux-kselftest@...r.kernel.org>,
<linux-stm32@...md-mailman.stormreply.com>,
<linux-arm-kernel@...ts.infradead.org>,
<linux-integrity@...r.kernel.org>,
<linux-security-module@...r.kernel.org>,
<linux-kernel@...r.kernel.org>,
Roberto Sassu <roberto.sassu@...wei.com>
Subject: [PATCH 06/18] bpf-preload: Generate free_objs_and_skel()
Generate free_objs_and_skel() (renamed from free_links_and_skel()) to
decrease the reference count of pinned objects, and to destroy the
skeleton.
Signed-off-by: Roberto Sassu <roberto.sassu@...wei.com>
---
kernel/bpf/preload/bpf_preload_kern.c | 13 ++-------
.../bpf/preload/iterators/iterators.lskel.h | 10 +++++++
tools/bpf/bpftool/gen.c | 28 +++++++++++++++++++
3 files changed, 40 insertions(+), 11 deletions(-)
diff --git a/kernel/bpf/preload/bpf_preload_kern.c b/kernel/bpf/preload/bpf_preload_kern.c
index 485589e03bd2..8b49beb2b2d6 100644
--- a/kernel/bpf/preload/bpf_preload_kern.c
+++ b/kernel/bpf/preload/bpf_preload_kern.c
@@ -5,15 +5,6 @@
#include <linux/bpf_preload.h>
#include "iterators/iterators.lskel.h"
-static void free_links_and_skel(void)
-{
- if (!IS_ERR_OR_NULL(dump_bpf_map_link))
- bpf_link_put(dump_bpf_map_link);
- if (!IS_ERR_OR_NULL(dump_bpf_prog_link))
- bpf_link_put(dump_bpf_prog_link);
- iterators_bpf__destroy(skel);
-}
-
static int preload(struct dentry *parent)
{
int err;
@@ -75,7 +66,7 @@ static int load_skel(void)
skel->links.dump_bpf_prog_fd = 0;
return 0;
out:
- free_links_and_skel();
+ free_objs_and_skel();
return err;
}
@@ -93,7 +84,7 @@ static int __init load(void)
static void __exit fini(void)
{
bpf_preload_ops = NULL;
- free_links_and_skel();
+ free_objs_and_skel();
}
late_initcall(load);
module_exit(fini);
diff --git a/kernel/bpf/preload/iterators/iterators.lskel.h b/kernel/bpf/preload/iterators/iterators.lskel.h
index 9794acdfacf9..4afd983ad40e 100644
--- a/kernel/bpf/preload/iterators/iterators.lskel.h
+++ b/kernel/bpf/preload/iterators/iterators.lskel.h
@@ -438,4 +438,14 @@ static struct bpf_link *dump_bpf_map_link;
static struct bpf_link *dump_bpf_prog_link;
static struct iterators_bpf *skel;
+static void free_objs_and_skel(void)
+{
+ if (!IS_ERR_OR_NULL(dump_bpf_map_link))
+ bpf_link_put(dump_bpf_map_link);
+ if (!IS_ERR_OR_NULL(dump_bpf_prog_link))
+ bpf_link_put(dump_bpf_prog_link);
+
+ iterators_bpf__destroy(skel);
+}
+
#endif /* __ITERATORS_BPF_SKEL_H__ */
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index c62c4c65b631..e167aa092b7f 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -674,6 +674,33 @@ static void codegen_preload_vars(struct bpf_object *obj, const char *obj_name)
", obj_name);
}
+static void codegen_preload_free(struct bpf_object *obj, const char *obj_name)
+{
+ struct bpf_program *prog;
+
+ codegen("\
+ \n\
+ \n\
+ static void free_objs_and_skel(void) \n\
+ { \n\
+ ");
+
+ bpf_object__for_each_program(prog, obj) {
+ codegen("\
+ \n\
+ if (!IS_ERR_OR_NULL(%1$s_link)) \n\
+ bpf_link_put(%1$s_link); \n\
+ ", bpf_program__name(prog));
+ }
+
+ codegen("\
+ \n\
+ \n\
+ %s__destroy(skel); \n\
+ } \n\
+ ", obj_name);
+}
+
static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard)
{
DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
@@ -824,6 +851,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
if (gen_preload_methods) {
codegen_preload_vars(obj, obj_name);
+ codegen_preload_free(obj, obj_name);
}
codegen("\
--
2.32.0
Powered by blists - more mailing lists