提交 29c9c9de 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

libbpf: Constify all high-level program attach APIs

mainline inclusion
from mainline-5.16-rc1
commit 942025c9
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=942025c9f37ee45e69eb5f39a2877afab66d9555

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

Attach APIs shouldn't need to modify bpf_program/bpf_map structs, so
change all struct bpf_program and struct bpf_map pointers to const
pointers. This is completely backwards compatible with no functional
change.
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Acked-by: NYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210916015836.1248906-8-andrii@kernel.org
(cherry picked from commit 942025c9)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>

Conflicts:
	tools/lib/bpf/libbpf.c
	tools/lib/bpf/libbpf.h
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 c8f83b22
...@@ -218,7 +218,7 @@ struct reloc_desc { ...@@ -218,7 +218,7 @@ struct reloc_desc {
struct bpf_sec_def; struct bpf_sec_def;
typedef struct bpf_link *(*attach_fn_t)(struct bpf_program *prog); typedef struct bpf_link *(*attach_fn_t)(const struct bpf_program *prog);
struct bpf_sec_def { struct bpf_sec_def {
const char *sec; const char *sec;
...@@ -7910,13 +7910,13 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog, ...@@ -7910,13 +7910,13 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog,
__VA_ARGS__ \ __VA_ARGS__ \
} }
static struct bpf_link *attach_kprobe(struct bpf_program *prog); static struct bpf_link *attach_kprobe(const struct bpf_program *prog);
static struct bpf_link *attach_tp(struct bpf_program *prog); static struct bpf_link *attach_tp(const struct bpf_program *prog);
static struct bpf_link *attach_raw_tp(struct bpf_program *prog); static struct bpf_link *attach_raw_tp(const struct bpf_program *prog);
static struct bpf_link *attach_trace(struct bpf_program *prog); static struct bpf_link *attach_trace(const struct bpf_program *prog);
static struct bpf_link *attach_lsm(struct bpf_program *prog); static struct bpf_link *attach_lsm(const struct bpf_program *prog);
static struct bpf_link *attach_iter(struct bpf_program *prog); static struct bpf_link *attach_iter(const struct bpf_program *prog);
static struct bpf_link *attach_sched(struct bpf_program *prog); static struct bpf_link *attach_sched(const 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),
...@@ -8993,7 +8993,7 @@ static int bpf_link__detach_perf_event(struct bpf_link *link) ...@@ -8993,7 +8993,7 @@ static int bpf_link__detach_perf_event(struct bpf_link *link)
return libbpf_err(err); return libbpf_err(err);
} }
struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, int pfd) struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
{ {
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
struct bpf_link *link; struct bpf_link *link;
...@@ -9142,7 +9142,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, ...@@ -9142,7 +9142,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
return pfd; return pfd;
} }
struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
bool retprobe, bool retprobe,
const char *func_name) const char *func_name)
{ {
...@@ -9170,7 +9170,7 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, ...@@ -9170,7 +9170,7 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_kprobe(struct bpf_program *prog) static struct bpf_link *attach_kprobe(const struct bpf_program *prog)
{ {
const char *func_name; const char *func_name;
bool retprobe; bool retprobe;
...@@ -9181,7 +9181,7 @@ static struct bpf_link *attach_kprobe(struct bpf_program *prog) ...@@ -9181,7 +9181,7 @@ static struct bpf_link *attach_kprobe(struct bpf_program *prog)
return bpf_program__attach_kprobe(prog, retprobe, func_name); return bpf_program__attach_kprobe(prog, retprobe, func_name);
} }
struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
bool retprobe, pid_t pid, bool retprobe, pid_t pid,
const char *binary_path, const char *binary_path,
size_t func_offset) size_t func_offset)
...@@ -9262,7 +9262,7 @@ static int perf_event_open_tracepoint(const char *tp_category, ...@@ -9262,7 +9262,7 @@ static int perf_event_open_tracepoint(const char *tp_category,
return pfd; return pfd;
} }
struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog, struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
const char *tp_category, const char *tp_category,
const char *tp_name) const char *tp_name)
{ {
...@@ -9289,7 +9289,7 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog, ...@@ -9289,7 +9289,7 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_tp(struct bpf_program *prog) static struct bpf_link *attach_tp(const 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;
...@@ -9313,7 +9313,7 @@ static struct bpf_link *attach_tp(struct bpf_program *prog) ...@@ -9313,7 +9313,7 @@ static struct bpf_link *attach_tp(struct bpf_program *prog)
return link; return link;
} }
struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
const char *tp_name) const char *tp_name)
{ {
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
...@@ -9343,7 +9343,7 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, ...@@ -9343,7 +9343,7 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_raw_tp(struct bpf_program *prog) static struct bpf_link *attach_raw_tp(const struct bpf_program *prog)
{ {
const char *tp_name = prog->sec_name + prog->sec_def->len; const char *tp_name = prog->sec_name + prog->sec_def->len;
...@@ -9351,7 +9351,7 @@ static struct bpf_link *attach_raw_tp(struct bpf_program *prog) ...@@ -9351,7 +9351,7 @@ static struct bpf_link *attach_raw_tp(struct bpf_program *prog)
} }
/* Common logic for all BPF program types that attach to a btf_id */ /* Common logic for all BPF program types that attach to a btf_id */
static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog) static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog)
{ {
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
struct bpf_link *link; struct bpf_link *link;
...@@ -9380,38 +9380,38 @@ static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog) ...@@ -9380,38 +9380,38 @@ static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
return (struct bpf_link *)link; return (struct bpf_link *)link;
} }
struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog) struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog);
} }
struct bpf_link *bpf_program__attach_sched(struct bpf_program *prog) struct bpf_link *bpf_program__attach_sched(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog);
} }
struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog) struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog);
} }
static struct bpf_link *attach_trace(struct bpf_program *prog) static struct bpf_link *attach_trace(const struct bpf_program *prog)
{ {
return bpf_program__attach_trace(prog); return bpf_program__attach_trace(prog);
} }
static struct bpf_link *attach_sched(struct bpf_program *prog) static struct bpf_link *attach_sched(const struct bpf_program *prog)
{ {
return bpf_program__attach_sched(prog); return bpf_program__attach_sched(prog);
} }
static struct bpf_link *attach_lsm(struct bpf_program *prog) static struct bpf_link *attach_lsm(const struct bpf_program *prog)
{ {
return bpf_program__attach_lsm(prog); return bpf_program__attach_lsm(prog);
} }
static struct bpf_link * static struct bpf_link *
bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id, bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id,
const char *target_name) const char *target_name)
{ {
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts, DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
...@@ -9447,24 +9447,24 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id, ...@@ -9447,24 +9447,24 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id,
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd) bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
{ {
return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup"); return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd) bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
{ {
return bpf_program__attach_fd(prog, netns_fd, 0, "netns"); return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
} }
struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex) struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
{ {
/* target_fd/target_ifindex use the same field in LINK_CREATE */ /* target_fd/target_ifindex use the same field in LINK_CREATE */
return bpf_program__attach_fd(prog, ifindex, 0, "xdp"); return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
} }
struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog, struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, int target_fd,
const char *attach_func_name) const char *attach_func_name)
{ {
...@@ -9497,7 +9497,7 @@ struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog, ...@@ -9497,7 +9497,7 @@ struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_iter(struct bpf_program *prog, bpf_program__attach_iter(const struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts) const struct bpf_iter_attach_opts *opts)
{ {
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
...@@ -9536,12 +9536,12 @@ bpf_program__attach_iter(struct bpf_program *prog, ...@@ -9536,12 +9536,12 @@ bpf_program__attach_iter(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_iter(struct bpf_program *prog) static struct bpf_link *attach_iter(const 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(const struct bpf_program *prog)
{ {
if (!prog->sec_def || !prog->sec_def->attach_fn) if (!prog->sec_def || !prog->sec_def->attach_fn)
return libbpf_err_ptr(-ESRCH); return libbpf_err_ptr(-ESRCH);
...@@ -9559,7 +9559,7 @@ static int bpf_link__detach_struct_ops(struct bpf_link *link) ...@@ -9559,7 +9559,7 @@ static int bpf_link__detach_struct_ops(struct bpf_link *link)
return 0; return 0;
} }
struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map) struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
{ {
struct bpf_struct_ops *st_ops; struct bpf_struct_ops *st_ops;
struct bpf_link *link; struct bpf_link *link;
...@@ -10644,7 +10644,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) ...@@ -10644,7 +10644,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
if (!prog->sec_def || !prog->sec_def->attach_fn) if (!prog->sec_def || !prog->sec_def->attach_fn)
continue; continue;
*link = prog->sec_def->attach_fn(prog); *link = bpf_program__attach(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",
......
...@@ -246,42 +246,42 @@ LIBBPF_API int bpf_link__detach(struct bpf_link *link); ...@@ -246,42 +246,42 @@ LIBBPF_API int bpf_link__detach(struct bpf_link *link);
LIBBPF_API int bpf_link__destroy(struct bpf_link *link); LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach(struct bpf_program *prog); bpf_program__attach(const struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_perf_event(struct bpf_program *prog, int pfd); bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe, bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
const char *func_name); const char *func_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe, bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
pid_t pid, const char *binary_path, pid_t pid, const char *binary_path,
size_t func_offset); size_t func_offset);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_tracepoint(struct bpf_program *prog, bpf_program__attach_tracepoint(const struct bpf_program *prog,
const char *tp_category, const char *tp_category,
const char *tp_name); const char *tp_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_raw_tracepoint(struct bpf_program *prog, bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
const char *tp_name); const char *tp_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_trace(struct bpf_program *prog); bpf_program__attach_trace(const struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_lsm(struct bpf_program *prog); bpf_program__attach_lsm(const struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd); bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd); bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_xdp(struct bpf_program *prog, int ifindex); bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_freplace(struct bpf_program *prog, bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, const char *attach_func_name); int target_fd, const char *attach_func_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_sched(struct bpf_program *prog); bpf_program__attach_sched(const struct bpf_program *prog);
struct bpf_map; struct bpf_map;
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map); LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
struct bpf_iter_attach_opts { struct bpf_iter_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */ size_t sz; /* size of this struct for forward/backward compatibility */
...@@ -291,7 +291,7 @@ struct bpf_iter_attach_opts { ...@@ -291,7 +291,7 @@ struct bpf_iter_attach_opts {
#define bpf_iter_attach_opts__last_field link_info_len #define bpf_iter_attach_opts__last_field link_info_len
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_iter(struct bpf_program *prog, bpf_program__attach_iter(const struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts); const struct bpf_iter_attach_opts *opts);
struct bpf_insn; struct bpf_insn;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册