1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021. Huawei Technologies Co., Ltd
5 #include <linux/kernel.h>
6 #include <linux/bpf_verifier.h>
10 extern struct bpf_struct_ops bpf_bpf_dummy_ops;
12 /* A common type for test_N with return value in bpf_dummy_ops */
13 typedef int (*dummy_ops_test_ret_fn)(struct bpf_dummy_ops_state *state, ...);
15 struct bpf_dummy_ops_test_args {
16 u64 args[MAX_BPF_FUNC_ARGS];
17 struct bpf_dummy_ops_state state;
20 static struct bpf_dummy_ops_test_args *
21 dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
24 struct bpf_dummy_ops_test_args *args;
28 size_in = kattr->test.ctx_size_in;
29 if (size_in != sizeof(u64) * nr)
30 return ERR_PTR(-EINVAL);
32 args = kzalloc(sizeof(*args), GFP_KERNEL);
34 return ERR_PTR(-ENOMEM);
36 ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
37 if (copy_from_user(args->args, ctx_in, size_in))
40 /* args[0] is 0 means state argument of test_N will be NULL */
41 u_state = u64_to_user_ptr(args->args[0]);
42 if (u_state && copy_from_user(&args->state, u_state,
49 return ERR_PTR(-EFAULT);
52 static int dummy_ops_copy_args(struct bpf_dummy_ops_test_args *args)
56 u_state = u64_to_user_ptr(args->args[0]);
57 if (u_state && copy_to_user(u_state, &args->state, sizeof(args->state)))
63 static int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args)
65 dummy_ops_test_ret_fn test = (void *)image;
66 struct bpf_dummy_ops_state *state = NULL;
68 /* state needs to be NULL if args[0] is 0 */
71 return test(state, args->args[1], args->args[2],
72 args->args[3], args->args[4]);
75 extern const struct bpf_link_ops bpf_struct_ops_link_lops;
77 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
78 union bpf_attr __user *uattr)
80 const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops;
81 const struct btf_type *func_proto;
82 struct bpf_dummy_ops_test_args *args;
83 struct bpf_tramp_links *tlinks;
84 struct bpf_tramp_link *link = NULL;
90 if (prog->aux->attach_btf_id != st_ops->type_id)
93 func_proto = prog->aux->attach_func_proto;
94 args = dummy_ops_init_args(kattr, btf_type_vlen(func_proto));
98 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
104 image = bpf_jit_alloc_exec(PAGE_SIZE);
109 set_vm_flush_reset_perms(image);
111 link = kzalloc(sizeof(*link), GFP_USER);
116 /* prog doesn't take the ownership of the reference from caller */
118 bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_link_lops, prog);
120 op_idx = prog->expected_attach_type;
121 err = bpf_struct_ops_prepare_trampoline(tlinks, link,
122 &st_ops->func_models[op_idx],
123 image, image + PAGE_SIZE);
127 set_memory_ro((long)image, 1);
128 set_memory_x((long)image, 1);
129 prog_ret = dummy_ops_call_op(image, args);
131 err = dummy_ops_copy_args(args);
134 if (put_user(prog_ret, &uattr->test.retval))
138 bpf_jit_free_exec(image);
140 bpf_link_put(&link->link);
145 static int bpf_dummy_init(struct btf *btf)
150 static bool bpf_dummy_ops_is_valid_access(int off, int size,
151 enum bpf_access_type type,
152 const struct bpf_prog *prog,
153 struct bpf_insn_access_aux *info)
155 return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
158 static int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log,
159 const struct btf *btf,
160 const struct btf_type *t, int off,
161 int size, enum bpf_access_type atype,
163 enum bpf_type_flag *flag)
165 const struct btf_type *state;
169 type_id = btf_find_by_name_kind(btf, "bpf_dummy_ops_state",
174 state = btf_type_by_id(btf, type_id);
176 bpf_log(log, "only access to bpf_dummy_ops_state is supported\n");
180 err = btf_struct_access(log, btf, t, off, size, atype, next_btf_id,
185 return atype == BPF_READ ? err : NOT_INIT;
188 static const struct bpf_verifier_ops bpf_dummy_verifier_ops = {
189 .is_valid_access = bpf_dummy_ops_is_valid_access,
190 .btf_struct_access = bpf_dummy_ops_btf_struct_access,
193 static int bpf_dummy_init_member(const struct btf_type *t,
194 const struct btf_member *member,
195 void *kdata, const void *udata)
200 static int bpf_dummy_reg(void *kdata)
205 static void bpf_dummy_unreg(void *kdata)
209 struct bpf_struct_ops bpf_bpf_dummy_ops = {
210 .verifier_ops = &bpf_dummy_verifier_ops,
211 .init = bpf_dummy_init,
212 .init_member = bpf_dummy_init_member,
213 .reg = bpf_dummy_reg,
214 .unreg = bpf_dummy_unreg,
215 .name = "bpf_dummy_ops",