Skip to content

Commit 9f70688

Browse files
alan-maguireSomasundaram Krishnasamy
authored andcommitted
bpf: remove the verifier ops from program structure
Since the verifier ops don't have to be associated with the program for its entire lifetime we can move it to verifier's struct bpf_verifier_env. Signed-off-by: Jakub Kicinski <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]> (cherry picked from commit 00176a3) Orabug: 31667601 Signed-off-by: Alan Maguire <[email protected]> Reviewed-by: Mark Haywood <[email protected]> Conflicts: kernel/bpf/verifier.c include/linux/bpf.h verifier.c context issue; disasm.h not needed to be included. For bpf.h removing the verifier ops involved removing the KABI-appended field so context differed from upstream due to the difference in the previous patch, and also leave the test_run field in "struct bpf_verifier_ops" to preserve KABI size (even though it is unused). Signed-off-by: Somasundaram Krishnasamy <[email protected]>
1 parent c464a3d commit 9f70688

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ struct bpf_verifier_ops {
187187
const struct bpf_insn *src,
188188
struct bpf_insn *dst,
189189
struct bpf_prog *prog, u32 *target_size);
190+
int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
191+
union bpf_attr __user *uattr);
190192
};
191193

192194
struct bpf_prog_aux {
@@ -206,7 +208,6 @@ struct bpf_prog_aux {
206208
struct work_struct work;
207209
struct rcu_head rcu;
208210
};
209-
UEK_KABI_EXTEND(const struct bpf_verifier_ops *vops)
210211
};
211212

212213
struct bpf_array {

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ struct bpf_verifier_env {
148148
u32 insn_idx;
149149
u32 prev_insn_idx;
150150
struct bpf_prog *prog; /* eBPF program being verified */
151+
const struct bpf_verifier_ops *ops;
151152
struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
152153
int stack_size; /* number of states to be processed */
153154
bool strict_alignment; /* perform strict pointer alignment checks */

kernel/bpf/syscall.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -725,22 +725,12 @@ static const struct bpf_prog_ops * const bpf_prog_types[] = {
725725
#undef BPF_MAP_TYPE
726726
};
727727

728-
static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
729-
#define BPF_PROG_TYPE(_id, _name) \
730-
[_id] = & _name ## _verifier_ops,
731-
#define BPF_MAP_TYPE(_id, _ops)
732-
#include <linux/bpf_types.h>
733-
#undef BPF_PROG_TYPE
734-
#undef BPF_MAP_TYPE
735-
};
736-
737728
static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
738729
{
739730
if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
740731
return -EINVAL;
741732

742733
prog->aux->ops = bpf_prog_types[type];
743-
prog->aux->vops = bpf_verifier_ops[type];
744734
prog->type = type;
745735
return 0;
746736
}

kernel/bpf/verifier.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
#include <linux/vmalloc.h>
2222
#include <linux/stringify.h>
2323

24+
static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
25+
#define BPF_PROG_TYPE(_id, _name) \
26+
[_id] = & _name ## _verifier_ops,
27+
#define BPF_MAP_TYPE(_id, _ops)
28+
#include <linux/bpf_types.h>
29+
#undef BPF_PROG_TYPE
30+
#undef BPF_MAP_TYPE
31+
};
32+
2433
/* bpf_check() is a static code analyzer that walks eBPF program
2534
* instruction by instruction and updates register/stack state.
2635
* All paths of conditional branches are analyzed until 'bpf_exit' insn.
@@ -1145,8 +1154,8 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,
11451154
*reg_type = info.reg_type;
11461155
return 0;
11471156
}
1148-
} else if (env->prog->aux->vops->is_valid_access &&
1149-
env->prog->aux->vops->is_valid_access(off, size, t, &info)) {
1157+
} else if (env->ops->is_valid_access &&
1158+
env->ops->is_valid_access(off, size, t, &info)) {
11501159
/* A non zero info.ctx_field_size indicates that this field is a
11511160
* candidate for later verifier transformation to load the whole
11521161
* field and then apply a mask when accessed with a narrower
@@ -1882,8 +1891,8 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
18821891
return -EINVAL;
18831892
}
18841893

1885-
if (env->prog->aux->vops->get_func_proto)
1886-
fn = env->prog->aux->vops->get_func_proto(func_id);
1894+
if (env->ops->get_func_proto)
1895+
fn = env->ops->get_func_proto(func_id);
18871896

18881897
if (!fn) {
18891898
verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
@@ -4607,7 +4616,7 @@ static void sanitize_dead_code(struct bpf_verifier_env *env)
46074616
*/
46084617
static int convert_ctx_accesses(struct bpf_verifier_env *env)
46094618
{
4610-
const struct bpf_verifier_ops *ops = env->prog->aux->vops;
4619+
const struct bpf_verifier_ops *ops = env->ops;
46114620
int i, cnt, size, ctx_field_size, delta = 0;
46124621
const int insn_cnt = env->prog->len;
46134622
struct bpf_insn insn_buf[16], *insn;
@@ -4936,7 +4945,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
49364945
insn = new_prog->insnsi + i + delta;
49374946
}
49384947
patch_call_imm:
4939-
fn = prog->aux->vops->get_func_proto(insn->imm);
4948+
fn = env->ops->get_func_proto(insn->imm);
49404949
/* all functions that have prototype and verifier allowed
49414950
* programs to call them, must be real in-kernel functions
49424951
*/
@@ -4993,6 +5002,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
49935002
if (!env->insn_aux_data)
49945003
goto err_free_env;
49955004
env->prog = *prog;
5005+
env->ops = bpf_verifier_ops[env->prog->type];
49965006

49975007
/* grab the mutex to protect few globals used by verifier */
49985008
mutex_lock(&bpf_verifier_lock);
@@ -5128,6 +5138,7 @@ int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
51285138
if (!env->insn_aux_data)
51295139
goto err_free_env;
51305140
env->prog = prog;
5141+
env->ops = bpf_verifier_ops[env->prog->type];
51315142
env->analyzer_ops = ops;
51325143
env->analyzer_priv = priv;
51335144

0 commit comments

Comments
 (0)