Skip to content

Commit f1b9509

Browse files
Alexei Starovoitovborkmann
Alexei Starovoitov
authored andcommitted
bpf: Replace prog_raw_tp+btf_id with prog_tracing
The bpf program type raw_tp together with 'expected_attach_type' was the most appropriate api to indicate BTF-enabled raw_tp programs. But during development it became apparent that 'expected_attach_type' cannot be used and new 'attach_btf_id' field had to be introduced. Which means that the information is duplicated in two fields where one of them is ignored. Clean it up by introducing new program type where both 'expected_attach_type' and 'attach_btf_id' fields have specific meaning. In the future 'expected_attach_type' will be extended with other attach points that have similar semantics to raw_tp. This patch is replacing BTF-enabled BPF_PROG_TYPE_RAW_TRACEPOINT with prog_type = BPF_RPOG_TYPE_TRACING expected_attach_type = BPF_TRACE_RAW_TP attach_btf_id = btf_id of raw tracepoint inside the kernel Future patches will add expected_attach_type = BPF_TRACE_FENTRY or BPF_TRACE_FEXIT where programs have the same input context and the same helpers, but different attach points. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent af91acb commit f1b9509

File tree

6 files changed

+71
-21
lines changed

6 files changed

+71
-21
lines changed

Diff for: include/linux/bpf.h

+5
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ enum bpf_cgroup_storage_type {
373373

374374
#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
375375

376+
/* The longest tracepoint has 12 args.
377+
* See include/trace/bpf_probe.h
378+
*/
379+
#define MAX_BPF_FUNC_ARGS 12
380+
376381
struct bpf_prog_stats {
377382
u64 cnt;
378383
u64 nsecs;

Diff for: include/linux/bpf_types.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_TRACEPOINT, tracepoint)
2626
BPF_PROG_TYPE(BPF_PROG_TYPE_PERF_EVENT, perf_event)
2727
BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT, raw_tracepoint)
2828
BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, raw_tracepoint_writable)
29+
BPF_PROG_TYPE(BPF_PROG_TYPE_TRACING, tracing)
2930
#endif
3031
#ifdef CONFIG_CGROUP_BPF
3132
BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_DEVICE, cg_dev)

Diff for: include/uapi/linux/bpf.h

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ enum bpf_prog_type {
173173
BPF_PROG_TYPE_CGROUP_SYSCTL,
174174
BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
175175
BPF_PROG_TYPE_CGROUP_SOCKOPT,
176+
BPF_PROG_TYPE_TRACING,
176177
};
177178

178179
enum bpf_attach_type {
@@ -199,6 +200,7 @@ enum bpf_attach_type {
199200
BPF_CGROUP_UDP6_RECVMSG,
200201
BPF_CGROUP_GETSOCKOPT,
201202
BPF_CGROUP_SETSOCKOPT,
203+
BPF_TRACE_RAW_TP,
202204
__MAX_BPF_ATTACH_TYPE
203205
};
204206

Diff for: kernel/bpf/syscall.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
15711571
u32 btf_id)
15721572
{
15731573
switch (prog_type) {
1574-
case BPF_PROG_TYPE_RAW_TRACEPOINT:
1574+
case BPF_PROG_TYPE_TRACING:
15751575
if (btf_id > BTF_MAX_TYPE)
15761576
return -EINVAL;
15771577
break;
@@ -1833,13 +1833,13 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
18331833
return PTR_ERR(prog);
18341834

18351835
if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1836+
prog->type != BPF_PROG_TYPE_TRACING &&
18361837
prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
18371838
err = -EINVAL;
18381839
goto out_put_prog;
18391840
}
18401841

1841-
if (prog->type == BPF_PROG_TYPE_RAW_TRACEPOINT &&
1842-
prog->aux->attach_btf_id) {
1842+
if (prog->type == BPF_PROG_TYPE_TRACING) {
18431843
if (attr->raw_tracepoint.name) {
18441844
/* raw_tp name should not be specified in raw_tp
18451845
* programs that were verified via in-kernel BTF info

Diff for: kernel/bpf/verifier.c

+24-10
Original file line numberDiff line numberDiff line change
@@ -9381,24 +9381,36 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
93819381
{
93829382
struct bpf_prog *prog = env->prog;
93839383
u32 btf_id = prog->aux->attach_btf_id;
9384+
const char prefix[] = "btf_trace_";
93849385
const struct btf_type *t;
93859386
const char *tname;
93869387

9387-
if (prog->type == BPF_PROG_TYPE_RAW_TRACEPOINT && btf_id) {
9388-
const char prefix[] = "btf_trace_";
9388+
if (prog->type != BPF_PROG_TYPE_TRACING)
9389+
return 0;
93899390

9390-
t = btf_type_by_id(btf_vmlinux, btf_id);
9391-
if (!t) {
9392-
verbose(env, "attach_btf_id %u is invalid\n", btf_id);
9393-
return -EINVAL;
9394-
}
9391+
if (!btf_id) {
9392+
verbose(env, "Tracing programs must provide btf_id\n");
9393+
return -EINVAL;
9394+
}
9395+
t = btf_type_by_id(btf_vmlinux, btf_id);
9396+
if (!t) {
9397+
verbose(env, "attach_btf_id %u is invalid\n", btf_id);
9398+
return -EINVAL;
9399+
}
9400+
tname = btf_name_by_offset(btf_vmlinux, t->name_off);
9401+
if (!tname) {
9402+
verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id);
9403+
return -EINVAL;
9404+
}
9405+
9406+
switch (prog->expected_attach_type) {
9407+
case BPF_TRACE_RAW_TP:
93959408
if (!btf_type_is_typedef(t)) {
93969409
verbose(env, "attach_btf_id %u is not a typedef\n",
93979410
btf_id);
93989411
return -EINVAL;
93999412
}
9400-
tname = btf_name_by_offset(btf_vmlinux, t->name_off);
9401-
if (!tname || strncmp(prefix, tname, sizeof(prefix) - 1)) {
9413+
if (strncmp(prefix, tname, sizeof(prefix) - 1)) {
94029414
verbose(env, "attach_btf_id %u points to wrong type name %s\n",
94039415
btf_id, tname);
94049416
return -EINVAL;
@@ -9419,8 +9431,10 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
94199431
prog->aux->attach_func_name = tname;
94209432
prog->aux->attach_func_proto = t;
94219433
prog->aux->attach_btf_trace = true;
9434+
return 0;
9435+
default:
9436+
return -EINVAL;
94229437
}
9423-
return 0;
94249438
}
94259439

94269440
int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,

Diff for: kernel/trace/bpf_trace.c

+36-8
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,6 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10551055
switch (func_id) {
10561056
case BPF_FUNC_perf_event_output:
10571057
return &bpf_perf_event_output_proto_raw_tp;
1058-
#ifdef CONFIG_NET
1059-
case BPF_FUNC_skb_output:
1060-
return &bpf_skb_output_proto;
1061-
#endif
10621058
case BPF_FUNC_get_stackid:
10631059
return &bpf_get_stackid_proto_raw_tp;
10641060
case BPF_FUNC_get_stack:
@@ -1068,20 +1064,44 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10681064
}
10691065
}
10701066

1067+
static const struct bpf_func_proto *
1068+
tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1069+
{
1070+
switch (func_id) {
1071+
#ifdef CONFIG_NET
1072+
case BPF_FUNC_skb_output:
1073+
return &bpf_skb_output_proto;
1074+
#endif
1075+
default:
1076+
return raw_tp_prog_func_proto(func_id, prog);
1077+
}
1078+
}
1079+
10711080
static bool raw_tp_prog_is_valid_access(int off, int size,
10721081
enum bpf_access_type type,
10731082
const struct bpf_prog *prog,
10741083
struct bpf_insn_access_aux *info)
10751084
{
1076-
/* largest tracepoint in the kernel has 12 args */
1077-
if (off < 0 || off >= sizeof(__u64) * 12)
1085+
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1086+
return false;
1087+
if (type != BPF_READ)
1088+
return false;
1089+
if (off % size != 0)
1090+
return false;
1091+
return true;
1092+
}
1093+
1094+
static bool tracing_prog_is_valid_access(int off, int size,
1095+
enum bpf_access_type type,
1096+
const struct bpf_prog *prog,
1097+
struct bpf_insn_access_aux *info)
1098+
{
1099+
if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
10781100
return false;
10791101
if (type != BPF_READ)
10801102
return false;
10811103
if (off % size != 0)
10821104
return false;
1083-
if (!prog->aux->attach_btf_id)
1084-
return true;
10851105
return btf_ctx_access(off, size, type, prog, info);
10861106
}
10871107

@@ -1093,6 +1113,14 @@ const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
10931113
const struct bpf_prog_ops raw_tracepoint_prog_ops = {
10941114
};
10951115

1116+
const struct bpf_verifier_ops tracing_verifier_ops = {
1117+
.get_func_proto = tracing_prog_func_proto,
1118+
.is_valid_access = tracing_prog_is_valid_access,
1119+
};
1120+
1121+
const struct bpf_prog_ops tracing_prog_ops = {
1122+
};
1123+
10961124
static bool raw_tp_writable_prog_is_valid_access(int off, int size,
10971125
enum bpf_access_type type,
10981126
const struct bpf_prog *prog,

0 commit comments

Comments
 (0)