Commit 3c32cc1b authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov

bpf: Enable bpf_iter targets registering ctx argument types

Commit b121b341 ("bpf: Add PTR_TO_BTF_ID_OR_NULL
support") adds a field btf_id_or_null_non0_off to
bpf_prog->aux structure to indicate that the
first ctx argument is PTR_TO_BTF_ID reg_type and
all others are PTR_TO_BTF_ID_OR_NULL.
This approach does not really scale if we have
other different reg types in the future, e.g.,
a pointer to a buffer.

This patch enables bpf_iter targets registering ctx argument
reg types which may be different from the default one.
For example, for pointers to structures, the default reg_type
is PTR_TO_BTF_ID for tracing program. The target can register
a particular pointer type as PTR_TO_BTF_ID_OR_NULL which can
be used by the verifier to enforce accesses.
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200513180221.2949882-1-yhs@fb.com
parent ab2ee4fc
...@@ -643,6 +643,12 @@ struct bpf_jit_poke_descriptor { ...@@ -643,6 +643,12 @@ struct bpf_jit_poke_descriptor {
u16 reason; u16 reason;
}; };
/* reg_type info for ctx arguments */
struct bpf_ctx_arg_aux {
u32 offset;
enum bpf_reg_type reg_type;
};
struct bpf_prog_aux { struct bpf_prog_aux {
atomic64_t refcnt; atomic64_t refcnt;
u32 used_map_cnt; u32 used_map_cnt;
...@@ -654,12 +660,13 @@ struct bpf_prog_aux { ...@@ -654,12 +660,13 @@ struct bpf_prog_aux {
u32 func_cnt; /* used by non-func prog as the number of func progs */ u32 func_cnt; /* used by non-func prog as the number of func progs */
u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
u32 attach_btf_id; /* in-kernel BTF type id to attach to */ u32 attach_btf_id; /* in-kernel BTF type id to attach to */
u32 ctx_arg_info_size;
const struct bpf_ctx_arg_aux *ctx_arg_info;
struct bpf_prog *linked_prog; struct bpf_prog *linked_prog;
bool verifier_zext; /* Zero extensions has been inserted by verifier. */ bool verifier_zext; /* Zero extensions has been inserted by verifier. */
bool offload_requested; bool offload_requested;
bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */ bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
bool func_proto_unreliable; bool func_proto_unreliable;
bool btf_id_or_null_non0_off;
enum bpf_tramp_prog_type trampoline_prog_type; enum bpf_tramp_prog_type trampoline_prog_type;
struct bpf_trampoline *trampoline; struct bpf_trampoline *trampoline;
struct hlist_node tramp_hlist; struct hlist_node tramp_hlist;
...@@ -1139,12 +1146,15 @@ int bpf_obj_get_user(const char __user *pathname, int flags); ...@@ -1139,12 +1146,15 @@ int bpf_obj_get_user(const char __user *pathname, int flags);
typedef int (*bpf_iter_init_seq_priv_t)(void *private_data); typedef int (*bpf_iter_init_seq_priv_t)(void *private_data);
typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data); typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
#define BPF_ITER_CTX_ARG_MAX 2
struct bpf_iter_reg { struct bpf_iter_reg {
const char *target; const char *target;
const struct seq_operations *seq_ops; const struct seq_operations *seq_ops;
bpf_iter_init_seq_priv_t init_seq_private; bpf_iter_init_seq_priv_t init_seq_private;
bpf_iter_fini_seq_priv_t fini_seq_private; bpf_iter_fini_seq_priv_t fini_seq_private;
u32 seq_priv_size; u32 seq_priv_size;
u32 ctx_arg_info_size;
struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
}; };
struct bpf_iter_meta { struct bpf_iter_meta {
......
...@@ -540,6 +540,13 @@ static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric) ...@@ -540,6 +540,13 @@ static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric)); return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
} }
#if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
struct bpf_iter__ipv6_route {
__bpf_md_ptr(struct bpf_iter_meta *, meta);
__bpf_md_ptr(struct fib6_info *, rt);
};
#endif
#ifdef CONFIG_IPV6_MULTIPLE_TABLES #ifdef CONFIG_IPV6_MULTIPLE_TABLES
static inline bool fib6_has_custom_rules(const struct net *net) static inline bool fib6_has_custom_rules(const struct net *net)
{ {
......
...@@ -308,6 +308,11 @@ bool bpf_iter_prog_supported(struct bpf_prog *prog) ...@@ -308,6 +308,11 @@ bool bpf_iter_prog_supported(struct bpf_prog *prog)
} }
mutex_unlock(&targets_mutex); mutex_unlock(&targets_mutex);
if (supported) {
prog->aux->ctx_arg_info_size = tinfo->reg_info->ctx_arg_info_size;
prog->aux->ctx_arg_info = tinfo->reg_info->ctx_arg_info;
}
return supported; return supported;
} }
......
...@@ -3694,7 +3694,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, ...@@ -3694,7 +3694,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
struct bpf_verifier_log *log = info->log; struct bpf_verifier_log *log = info->log;
const struct btf_param *args; const struct btf_param *args;
u32 nr_args, arg; u32 nr_args, arg;
int ret; int i, ret;
if (off % 8) { if (off % 8) {
bpf_log(log, "func '%s' offset %d is not multiple of 8\n", bpf_log(log, "func '%s' offset %d is not multiple of 8\n",
...@@ -3790,10 +3790,15 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, ...@@ -3790,10 +3790,15 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
return true; return true;
/* this is a pointer to another type */ /* this is a pointer to another type */
if (off != 0 && prog->aux->btf_id_or_null_non0_off) info->reg_type = PTR_TO_BTF_ID;
info->reg_type = PTR_TO_BTF_ID_OR_NULL; for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
else const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
info->reg_type = PTR_TO_BTF_ID;
if (ctx_arg_info->offset == off) {
info->reg_type = ctx_arg_info->reg_type;
break;
}
}
if (tgt_prog) { if (tgt_prog) {
ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg); ret = btf_translate_to_vmlinux(log, btf, t, tgt_prog->type, arg);
......
...@@ -87,6 +87,11 @@ static const struct bpf_iter_reg bpf_map_reg_info = { ...@@ -87,6 +87,11 @@ static const struct bpf_iter_reg bpf_map_reg_info = {
.init_seq_private = NULL, .init_seq_private = NULL,
.fini_seq_private = NULL, .fini_seq_private = NULL,
.seq_priv_size = sizeof(struct bpf_iter_seq_map_info), .seq_priv_size = sizeof(struct bpf_iter_seq_map_info),
.ctx_arg_info_size = 1,
.ctx_arg_info = {
{ offsetof(struct bpf_iter__bpf_map, map),
PTR_TO_BTF_ID_OR_NULL },
},
}; };
static int __init bpf_map_iter_init(void) static int __init bpf_map_iter_init(void)
......
...@@ -312,6 +312,11 @@ static const struct bpf_iter_reg task_reg_info = { ...@@ -312,6 +312,11 @@ static const struct bpf_iter_reg task_reg_info = {
.init_seq_private = init_seq_pidns, .init_seq_private = init_seq_pidns,
.fini_seq_private = fini_seq_pidns, .fini_seq_private = fini_seq_pidns,
.seq_priv_size = sizeof(struct bpf_iter_seq_task_info), .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
.ctx_arg_info_size = 1,
.ctx_arg_info = {
{ offsetof(struct bpf_iter__task, task),
PTR_TO_BTF_ID_OR_NULL },
},
}; };
static const struct bpf_iter_reg task_file_reg_info = { static const struct bpf_iter_reg task_file_reg_info = {
...@@ -320,6 +325,13 @@ static const struct bpf_iter_reg task_file_reg_info = { ...@@ -320,6 +325,13 @@ static const struct bpf_iter_reg task_file_reg_info = {
.init_seq_private = init_seq_pidns, .init_seq_private = init_seq_pidns,
.fini_seq_private = fini_seq_pidns, .fini_seq_private = fini_seq_pidns,
.seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info), .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
.ctx_arg_info_size = 2,
.ctx_arg_info = {
{ offsetof(struct bpf_iter__task_file, task),
PTR_TO_BTF_ID_OR_NULL },
{ offsetof(struct bpf_iter__task_file, file),
PTR_TO_BTF_ID_OR_NULL },
},
}; };
static int __init task_iter_init(void) static int __init task_iter_init(void)
......
...@@ -10652,7 +10652,6 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) ...@@ -10652,7 +10652,6 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
prog->aux->attach_func_proto = t; prog->aux->attach_func_proto = t;
if (!bpf_iter_prog_supported(prog)) if (!bpf_iter_prog_supported(prog))
return -EINVAL; return -EINVAL;
prog->aux->btf_id_or_null_non0_off = true;
ret = btf_distill_func_proto(&env->log, btf, t, ret = btf_distill_func_proto(&env->log, btf, t,
tname, &fmodel); tname, &fmodel);
return ret; return ret;
......
...@@ -2638,11 +2638,6 @@ static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v) ...@@ -2638,11 +2638,6 @@ static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v)
} }
#if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL) #if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
struct bpf_iter__ipv6_route {
__bpf_md_ptr(struct bpf_iter_meta *, meta);
__bpf_md_ptr(struct fib6_info *, rt);
};
static int ipv6_route_prog_seq_show(struct bpf_prog *prog, static int ipv6_route_prog_seq_show(struct bpf_prog *prog,
struct bpf_iter_meta *meta, struct bpf_iter_meta *meta,
void *v) void *v)
......
...@@ -6403,6 +6403,11 @@ static const struct bpf_iter_reg ipv6_route_reg_info = { ...@@ -6403,6 +6403,11 @@ static const struct bpf_iter_reg ipv6_route_reg_info = {
.init_seq_private = bpf_iter_init_seq_net, .init_seq_private = bpf_iter_init_seq_net,
.fini_seq_private = bpf_iter_fini_seq_net, .fini_seq_private = bpf_iter_fini_seq_net,
.seq_priv_size = sizeof(struct ipv6_route_iter), .seq_priv_size = sizeof(struct ipv6_route_iter),
.ctx_arg_info_size = 1,
.ctx_arg_info = {
{ offsetof(struct bpf_iter__ipv6_route, rt),
PTR_TO_BTF_ID_OR_NULL },
},
}; };
static int __init bpf_iter_register(void) static int __init bpf_iter_register(void)
......
...@@ -2809,6 +2809,11 @@ static const struct bpf_iter_reg netlink_reg_info = { ...@@ -2809,6 +2809,11 @@ static const struct bpf_iter_reg netlink_reg_info = {
.init_seq_private = bpf_iter_init_seq_net, .init_seq_private = bpf_iter_init_seq_net,
.fini_seq_private = bpf_iter_fini_seq_net, .fini_seq_private = bpf_iter_fini_seq_net,
.seq_priv_size = sizeof(struct nl_seq_iter), .seq_priv_size = sizeof(struct nl_seq_iter),
.ctx_arg_info_size = 1,
.ctx_arg_info = {
{ offsetof(struct bpf_iter__netlink, sk),
PTR_TO_BTF_ID_OR_NULL },
},
}; };
static int __init bpf_iter_register(void) static int __init bpf_iter_register(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment