提交 75ad79cb 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

libbpf: Simplify BPF program auto-attach code

mainline inclusion
from mainline-5.16-rc1
commit 5532dfd4
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5532dfd42e4846e84d346a6dfe01e477e35baa65

-------------------------------------------------

Remove the need to explicitly pass bpf_sec_def for auto-attachable BPF
programs, as it is already recorded at bpf_object__open() time for all
recognized type of BPF programs. This further reduces number of explicit
calls to find_sec_def(), simplifying further refactorings.

No functional changes are done by this patch.
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Acked-by: NMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20210914014733.2768-4-andrii@kernel.org
(cherry picked from commit 5532dfd4)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>

Conflicts:
	tools/lib/bpf/libbpf.c
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 be839f06
...@@ -216,8 +216,7 @@ struct reloc_desc { ...@@ -216,8 +216,7 @@ struct reloc_desc {
struct bpf_sec_def; struct bpf_sec_def;
typedef struct bpf_link *(*attach_fn_t)(const struct bpf_sec_def *sec, typedef struct bpf_link *(*attach_fn_t)(struct bpf_program *prog);
struct bpf_program *prog);
struct bpf_sec_def { struct bpf_sec_def {
const char *sec; const char *sec;
...@@ -7883,20 +7882,13 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog, ...@@ -7883,20 +7882,13 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog,
__VA_ARGS__ \ __VA_ARGS__ \
} }
static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, static struct bpf_link *attach_kprobe(struct bpf_program *prog);
struct bpf_program *prog); static struct bpf_link *attach_tp(struct bpf_program *prog);
static struct bpf_link *attach_tp(const struct bpf_sec_def *sec, static struct bpf_link *attach_raw_tp(struct bpf_program *prog);
struct bpf_program *prog); static struct bpf_link *attach_trace(struct bpf_program *prog);
static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec, static struct bpf_link *attach_lsm(struct bpf_program *prog);
struct bpf_program *prog); static struct bpf_link *attach_iter(struct bpf_program *prog);
static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, static struct bpf_link *attach_sched(struct bpf_program *prog);
struct bpf_program *prog);
static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec,
struct bpf_program *prog);
static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
struct bpf_program *prog);
static struct bpf_link *attach_sched(const struct bpf_sec_def *sec,
struct bpf_program *prog);
static const struct bpf_sec_def section_defs[] = { static const struct bpf_sec_def section_defs[] = {
BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
...@@ -9162,14 +9154,13 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, ...@@ -9162,14 +9154,13 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, static struct bpf_link *attach_kprobe(struct bpf_program *prog)
struct bpf_program *prog)
{ {
const char *func_name; const char *func_name;
bool retprobe; bool retprobe;
func_name = prog->sec_name + sec->len; func_name = prog->sec_name + prog->sec_def->len;
retprobe = strcmp(sec->sec, "kretprobe/") == 0; retprobe = strcmp(prog->sec_def->sec, "kretprobe/") == 0;
return bpf_program__attach_kprobe(prog, retprobe, func_name); return bpf_program__attach_kprobe(prog, retprobe, func_name);
} }
...@@ -9282,8 +9273,7 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog, ...@@ -9282,8 +9273,7 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_tp(const struct bpf_sec_def *sec, static struct bpf_link *attach_tp(struct bpf_program *prog)
struct bpf_program *prog)
{ {
char *sec_name, *tp_cat, *tp_name; char *sec_name, *tp_cat, *tp_name;
struct bpf_link *link; struct bpf_link *link;
...@@ -9293,7 +9283,7 @@ static struct bpf_link *attach_tp(const struct bpf_sec_def *sec, ...@@ -9293,7 +9283,7 @@ static struct bpf_link *attach_tp(const struct bpf_sec_def *sec,
return libbpf_err_ptr(-ENOMEM); return libbpf_err_ptr(-ENOMEM);
/* extract "tp/<category>/<name>" */ /* extract "tp/<category>/<name>" */
tp_cat = sec_name + sec->len; tp_cat = sec_name + prog->sec_def->len;
tp_name = strchr(tp_cat, '/'); tp_name = strchr(tp_cat, '/');
if (!tp_name) { if (!tp_name) {
free(sec_name); free(sec_name);
...@@ -9337,10 +9327,9 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, ...@@ -9337,10 +9327,9 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_raw_tp(const struct bpf_sec_def *sec, static struct bpf_link *attach_raw_tp(struct bpf_program *prog)
struct bpf_program *prog)
{ {
const char *tp_name = prog->sec_name + sec->len; const char *tp_name = prog->sec_name + prog->sec_def->len;
return bpf_program__attach_raw_tracepoint(prog, tp_name); return bpf_program__attach_raw_tracepoint(prog, tp_name);
} }
...@@ -9390,20 +9379,17 @@ struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog) ...@@ -9390,20 +9379,17 @@ struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog)
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog);
} }
static struct bpf_link *attach_trace(const struct bpf_sec_def *sec, static struct bpf_link *attach_trace(struct bpf_program *prog)
struct bpf_program *prog)
{ {
return bpf_program__attach_trace(prog); return bpf_program__attach_trace(prog);
} }
static struct bpf_link *attach_sched(const struct bpf_sec_def *sec, static struct bpf_link *attach_sched(struct bpf_program *prog)
struct bpf_program *prog)
{ {
return bpf_program__attach_sched(prog); return bpf_program__attach_sched(prog);
} }
static struct bpf_link *attach_lsm(const struct bpf_sec_def *sec, static struct bpf_link *attach_lsm(struct bpf_program *prog)
struct bpf_program *prog)
{ {
return bpf_program__attach_lsm(prog); return bpf_program__attach_lsm(prog);
} }
...@@ -9534,21 +9520,17 @@ bpf_program__attach_iter(struct bpf_program *prog, ...@@ -9534,21 +9520,17 @@ bpf_program__attach_iter(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_iter(const struct bpf_sec_def *sec, static struct bpf_link *attach_iter(struct bpf_program *prog)
struct bpf_program *prog)
{ {
return bpf_program__attach_iter(prog, NULL); return bpf_program__attach_iter(prog, NULL);
} }
struct bpf_link *bpf_program__attach(struct bpf_program *prog) struct bpf_link *bpf_program__attach(struct bpf_program *prog)
{ {
const struct bpf_sec_def *sec_def; if (!prog->sec_def || !prog->sec_def->attach_fn)
sec_def = find_sec_def(prog->sec_name);
if (!sec_def || !sec_def->attach_fn)
return libbpf_err_ptr(-ESRCH); return libbpf_err_ptr(-ESRCH);
return sec_def->attach_fn(sec_def, prog); return prog->sec_def->attach_fn(prog);
} }
static int bpf_link__detach_struct_ops(struct bpf_link *link) static int bpf_link__detach_struct_ops(struct bpf_link *link)
...@@ -10627,16 +10609,15 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) ...@@ -10627,16 +10609,15 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
for (i = 0; i < s->prog_cnt; i++) { for (i = 0; i < s->prog_cnt; i++) {
struct bpf_program *prog = *s->progs[i].prog; struct bpf_program *prog = *s->progs[i].prog;
struct bpf_link **link = s->progs[i].link; struct bpf_link **link = s->progs[i].link;
const struct bpf_sec_def *sec_def;
if (!prog->load) if (!prog->load)
continue; continue;
sec_def = find_sec_def(prog->sec_name); /* auto-attaching not supported for this program */
if (!sec_def || !sec_def->attach_fn) if (!prog->sec_def || !prog->sec_def->attach_fn)
continue; continue;
*link = sec_def->attach_fn(sec_def, prog); *link = prog->sec_def->attach_fn(prog);
err = libbpf_get_error(*link); err = libbpf_get_error(*link);
if (err) { if (err) {
pr_warn("failed to auto-attach program '%s': %d\n", pr_warn("failed to auto-attach program '%s': %d\n",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册