From: Joe Burton <jevburton@xxxxxxxxxx> Add new attach type, BPF_TRACE_MAP. Tracing programs may specify this type when loading. The verifier then checks that the traced function has been registered for map tracing. Signed-off-by: Joe Burton <jevburton@xxxxxxxxxx> --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/map_trace.c | 25 +++++++++++++++++++++++++ kernel/bpf/verifier.c | 6 ++++++ 4 files changed, 33 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index dad62d5571c9..272f0ac49285 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1539,6 +1539,7 @@ void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info); bool bpf_iter_prog_supported(struct bpf_prog *prog); const struct bpf_func_proto * bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog); +bool bpf_map_trace_prog_supported(struct bpf_prog *prog); int bpf_map_trace_reg_target(const struct bpf_map_trace_reg *reg_info); int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog); int bpf_iter_new_fd(struct bpf_link *link); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 17e8f4113369..0883c5dfb5d8 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1003,6 +1003,7 @@ enum bpf_attach_type { BPF_SK_REUSEPORT_SELECT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, BPF_PERF_EVENT, + BPF_TRACE_MAP, __MAX_BPF_ATTACH_TYPE }; diff --git a/kernel/bpf/map_trace.c b/kernel/bpf/map_trace.c index d8f829535f7e..d2c6df20f55c 100644 --- a/kernel/bpf/map_trace.c +++ b/kernel/bpf/map_trace.c @@ -31,3 +31,28 @@ int bpf_map_trace_reg_target(const struct bpf_map_trace_reg *reg_info) return 0; } + +bool bpf_map_trace_prog_supported(struct bpf_prog *prog) +{ + const char *attach_fname = prog->aux->attach_func_name; + u32 prog_btf_id = prog->aux->attach_btf_id; + struct bpf_map_trace_target_info *tinfo; + bool supported = false; + + mutex_lock(&targets_mutex); + list_for_each_entry(tinfo, &targets, list) { + if (tinfo->btf_id && tinfo->btf_id == prog_btf_id) { + supported = true; + break; + } + if (!strcmp(attach_fname, tinfo->reg_info->target)) { + tinfo->btf_id = prog->aux->attach_btf_id; + supported = true; + break; + } + } + mutex_unlock(&targets_mutex); + + return supported; +} + diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1433752db740..babcb135dc0d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -9464,6 +9464,7 @@ static int check_return_code(struct bpf_verifier_env *env) break; case BPF_TRACE_RAW_TP: case BPF_MODIFY_RETURN: + case BPF_TRACE_MAP: return 0; case BPF_TRACE_ITER: break; @@ -13496,6 +13497,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, break; case BPF_TRACE_ITER: + case BPF_TRACE_MAP: if (!btf_type_is_func(t)) { bpf_log(log, "attach_btf_id %u is not a function\n", btf_id); @@ -13672,6 +13674,10 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) if (!bpf_iter_prog_supported(prog)) return -EINVAL; return 0; + } else if (prog->expected_attach_type == BPF_TRACE_MAP) { + if (!bpf_map_trace_prog_supported(prog)) + return -EINVAL; + return 0; } if (prog->type == BPF_PROG_TYPE_LSM) { -- 2.33.0.685.g46640cef36-goog