1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
5 #include <linux/bpf_verifier.h>
7 #include <linux/filter.h>
8 #include <linux/slab.h>
9 #include <linux/numa.h>
10 #include <linux/seq_file.h>
11 #include <linux/refcount.h>
12 #include <linux/mutex.h>
14 enum bpf_struct_ops_state {
15 BPF_STRUCT_OPS_STATE_INIT,
16 BPF_STRUCT_OPS_STATE_INUSE,
17 BPF_STRUCT_OPS_STATE_TOBEFREE,
20 #define BPF_STRUCT_OPS_COMMON_VALUE \
22 enum bpf_struct_ops_state state
24 struct bpf_struct_ops_value {
25 BPF_STRUCT_OPS_COMMON_VALUE;
26 char data[] ____cacheline_aligned_in_smp;
29 struct bpf_struct_ops_map {
32 const struct bpf_struct_ops *st_ops;
33 /* protect map_update */
35 /* progs has all the bpf_prog that is populated
36 * to the func ptr of the kernel's struct
39 struct bpf_prog **progs;
40 /* image is a page that has all the trampolines
41 * that stores the func args before calling the bpf_prog.
42 * A PAGE_SIZE "image" is enough to store all trampoline for
46 /* uvalue->data stores the kernel struct
47 * (e.g. tcp_congestion_ops) that is more useful
48 * to userspace than the kvalue. For example,
49 * the bpf_prog's id is stored instead of the kernel
50 * address of a func ptr.
52 struct bpf_struct_ops_value *uvalue;
53 /* kvalue.data stores the actual kernel's struct
54 * (e.g. tcp_congestion_ops) that will be
55 * registered to the kernel subsystem.
57 struct bpf_struct_ops_value kvalue;
60 #define VALUE_PREFIX "bpf_struct_ops_"
61 #define VALUE_PREFIX_LEN (sizeof(VALUE_PREFIX) - 1)
63 /* bpf_struct_ops_##_name (e.g. bpf_struct_ops_tcp_congestion_ops) is
64 * the map's value exposed to the userspace and its btf-type-id is
65 * stored at the map->btf_vmlinux_value_type_id.
68 #define BPF_STRUCT_OPS_TYPE(_name) \
69 extern struct bpf_struct_ops bpf_##_name; \
71 struct bpf_struct_ops_##_name { \
72 BPF_STRUCT_OPS_COMMON_VALUE; \
73 struct _name data ____cacheline_aligned_in_smp; \
75 #include "bpf_struct_ops_types.h"
76 #undef BPF_STRUCT_OPS_TYPE
79 #define BPF_STRUCT_OPS_TYPE(_name) BPF_STRUCT_OPS_TYPE_##_name,
80 #include "bpf_struct_ops_types.h"
81 #undef BPF_STRUCT_OPS_TYPE
82 __NR_BPF_STRUCT_OPS_TYPE,
85 static struct bpf_struct_ops * const bpf_struct_ops[] = {
86 #define BPF_STRUCT_OPS_TYPE(_name) \
87 [BPF_STRUCT_OPS_TYPE_##_name] = &bpf_##_name,
88 #include "bpf_struct_ops_types.h"
89 #undef BPF_STRUCT_OPS_TYPE
92 const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {
95 const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
97 .test_run = bpf_struct_ops_test_run,
101 static const struct btf_type *module_type;
103 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
105 s32 type_id, value_id, module_id;
106 const struct btf_member *member;
107 struct bpf_struct_ops *st_ops;
108 const struct btf_type *t;
109 char value_name[128];
113 /* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */
114 #define BPF_STRUCT_OPS_TYPE(_name) BTF_TYPE_EMIT(struct bpf_struct_ops_##_name);
115 #include "bpf_struct_ops_types.h"
116 #undef BPF_STRUCT_OPS_TYPE
118 module_id = btf_find_by_name_kind(btf, "module", BTF_KIND_STRUCT);
120 pr_warn("Cannot find struct module in btf_vmlinux\n");
123 module_type = btf_type_by_id(btf, module_id);
125 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
126 st_ops = bpf_struct_ops[i];
128 if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
129 sizeof(value_name)) {
130 pr_warn("struct_ops name %s is too long\n",
134 sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
136 value_id = btf_find_by_name_kind(btf, value_name,
139 pr_warn("Cannot find struct %s in btf_vmlinux\n",
144 type_id = btf_find_by_name_kind(btf, st_ops->name,
147 pr_warn("Cannot find struct %s in btf_vmlinux\n",
151 t = btf_type_by_id(btf, type_id);
152 if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
153 pr_warn("Cannot support #%u members in struct %s\n",
154 btf_type_vlen(t), st_ops->name);
158 for_each_member(j, t, member) {
159 const struct btf_type *func_proto;
161 mname = btf_name_by_offset(btf, member->name_off);
163 pr_warn("anon member in struct %s is not supported\n",
168 if (btf_member_bitfield_size(t, member)) {
169 pr_warn("bit field member %s in struct %s is not supported\n",
170 mname, st_ops->name);
174 func_proto = btf_type_resolve_func_ptr(btf,
178 btf_distill_func_proto(log, btf,
180 &st_ops->func_models[j])) {
181 pr_warn("Error in parsing func ptr %s in struct %s\n",
182 mname, st_ops->name);
187 if (j == btf_type_vlen(t)) {
188 if (st_ops->init(btf)) {
189 pr_warn("Error in init bpf_struct_ops %s\n",
192 st_ops->type_id = type_id;
194 st_ops->value_id = value_id;
195 st_ops->value_type = btf_type_by_id(btf,
202 extern struct btf *btf_vmlinux;
204 static const struct bpf_struct_ops *
205 bpf_struct_ops_find_value(u32 value_id)
209 if (!value_id || !btf_vmlinux)
212 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
213 if (bpf_struct_ops[i]->value_id == value_id)
214 return bpf_struct_ops[i];
220 const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
224 if (!type_id || !btf_vmlinux)
227 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
228 if (bpf_struct_ops[i]->type_id == type_id)
229 return bpf_struct_ops[i];
235 static int bpf_struct_ops_map_get_next_key(struct bpf_map *map, void *key,
238 if (key && *(u32 *)key == 0)
241 *(u32 *)next_key = 0;
245 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
248 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
249 struct bpf_struct_ops_value *uvalue, *kvalue;
250 enum bpf_struct_ops_state state;
252 if (unlikely(*(u32 *)key != 0))
255 kvalue = &st_map->kvalue;
256 /* Pair with smp_store_release() during map_update */
257 state = smp_load_acquire(&kvalue->state);
258 if (state == BPF_STRUCT_OPS_STATE_INIT) {
259 memset(value, 0, map->value_size);
263 /* No lock is needed. state and refcnt do not need
264 * to be updated together under atomic context.
266 uvalue = (struct bpf_struct_ops_value *)value;
267 memcpy(uvalue, st_map->uvalue, map->value_size);
268 uvalue->state = state;
269 refcount_set(&uvalue->refcnt, refcount_read(&kvalue->refcnt));
274 static void *bpf_struct_ops_map_lookup_elem(struct bpf_map *map, void *key)
276 return ERR_PTR(-EINVAL);
279 static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
281 const struct btf_type *t = st_map->st_ops->type;
284 for (i = 0; i < btf_type_vlen(t); i++) {
285 if (st_map->progs[i]) {
286 bpf_prog_put(st_map->progs[i]);
287 st_map->progs[i] = NULL;
292 static int check_zero_holes(const struct btf_type *t, void *data)
294 const struct btf_member *member;
295 u32 i, moff, msize, prev_mend = 0;
296 const struct btf_type *mtype;
298 for_each_member(i, t, member) {
299 moff = btf_member_bit_offset(t, member) / 8;
300 if (moff > prev_mend &&
301 memchr_inv(data + prev_mend, 0, moff - prev_mend))
304 mtype = btf_type_by_id(btf_vmlinux, member->type);
305 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
307 return PTR_ERR(mtype);
308 prev_mend = moff + msize;
311 if (t->size > prev_mend &&
312 memchr_inv(data + prev_mend, 0, t->size - prev_mend))
318 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_progs *tprogs,
319 struct bpf_prog *prog,
320 const struct btf_func_model *model,
321 void *image, void *image_end)
325 tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
326 tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
327 flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
328 return arch_prepare_bpf_trampoline(NULL, image, image_end,
329 model, flags, tprogs, NULL);
332 static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
333 void *value, u64 flags)
335 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
336 const struct bpf_struct_ops *st_ops = st_map->st_ops;
337 struct bpf_struct_ops_value *uvalue, *kvalue;
338 const struct btf_member *member;
339 const struct btf_type *t = st_ops->type;
340 struct bpf_tramp_progs *tprogs = NULL;
342 int prog_fd, err = 0;
343 void *image, *image_end;
349 if (*(u32 *)key != 0)
352 err = check_zero_holes(st_ops->value_type, value);
356 uvalue = (struct bpf_struct_ops_value *)value;
357 err = check_zero_holes(t, uvalue->data);
361 if (uvalue->state || refcount_read(&uvalue->refcnt))
364 tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
368 uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
369 kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
371 mutex_lock(&st_map->lock);
373 if (kvalue->state != BPF_STRUCT_OPS_STATE_INIT) {
378 memcpy(uvalue, value, map->value_size);
380 udata = &uvalue->data;
381 kdata = &kvalue->data;
382 image = st_map->image;
383 image_end = st_map->image + PAGE_SIZE;
385 for_each_member(i, t, member) {
386 const struct btf_type *mtype, *ptype;
387 struct bpf_prog *prog;
390 moff = btf_member_bit_offset(t, member) / 8;
391 ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
392 if (ptype == module_type) {
393 if (*(void **)(udata + moff))
395 *(void **)(kdata + moff) = BPF_MODULE_OWNER;
399 err = st_ops->init_member(t, member, kdata, udata);
403 /* The ->init_member() has handled this member */
407 /* If st_ops->init_member does not handle it,
408 * we will only handle func ptrs and zero-ed members
409 * here. Reject everything else.
412 /* All non func ptr member must be 0 */
413 if (!ptype || !btf_type_is_func_proto(ptype)) {
416 mtype = btf_type_by_id(btf_vmlinux, member->type);
417 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
419 err = PTR_ERR(mtype);
423 if (memchr_inv(udata + moff, 0, msize)) {
431 prog_fd = (int)(*(unsigned long *)(udata + moff));
432 /* Similar check as the attr->attach_prog_fd */
436 prog = bpf_prog_get(prog_fd);
441 st_map->progs[i] = prog;
443 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
444 prog->aux->attach_btf_id != st_ops->type_id ||
445 prog->expected_attach_type != i) {
450 err = bpf_struct_ops_prepare_trampoline(tprogs, prog,
451 &st_ops->func_models[i],
456 *(void **)(kdata + moff) = image;
459 /* put prog_id to udata */
460 *(unsigned long *)(udata + moff) = prog->aux->id;
463 refcount_set(&kvalue->refcnt, 1);
466 set_memory_ro((long)st_map->image, 1);
467 set_memory_x((long)st_map->image, 1);
468 err = st_ops->reg(kdata);
470 /* Pair with smp_load_acquire() during lookup_elem().
471 * It ensures the above udata updates (e.g. prog->aux->id)
472 * can be seen once BPF_STRUCT_OPS_STATE_INUSE is set.
474 smp_store_release(&kvalue->state, BPF_STRUCT_OPS_STATE_INUSE);
478 /* Error during st_ops->reg(). It is very unlikely since
479 * the above init_member() should have caught it earlier
480 * before reg(). The only possibility is if there was a race
481 * in registering the struct_ops (under the same name) to
482 * a sub-system through different struct_ops's maps.
484 set_memory_nx((long)st_map->image, 1);
485 set_memory_rw((long)st_map->image, 1);
489 bpf_struct_ops_map_put_progs(st_map);
490 memset(uvalue, 0, map->value_size);
491 memset(kvalue, 0, map->value_size);
494 mutex_unlock(&st_map->lock);
498 static int bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
500 enum bpf_struct_ops_state prev_state;
501 struct bpf_struct_ops_map *st_map;
503 st_map = (struct bpf_struct_ops_map *)map;
504 prev_state = cmpxchg(&st_map->kvalue.state,
505 BPF_STRUCT_OPS_STATE_INUSE,
506 BPF_STRUCT_OPS_STATE_TOBEFREE);
507 switch (prev_state) {
508 case BPF_STRUCT_OPS_STATE_INUSE:
509 st_map->st_ops->unreg(&st_map->kvalue.data);
510 if (refcount_dec_and_test(&st_map->kvalue.refcnt))
513 case BPF_STRUCT_OPS_STATE_TOBEFREE:
515 case BPF_STRUCT_OPS_STATE_INIT:
519 /* Should never happen. Treat it as not found. */
524 static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
530 value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
534 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
536 btf_type_seq_show(btf_vmlinux, map->btf_vmlinux_value_type_id,
544 static void bpf_struct_ops_map_free(struct bpf_map *map)
546 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
549 bpf_struct_ops_map_put_progs(st_map);
550 bpf_map_area_free(st_map->progs);
551 bpf_jit_free_exec(st_map->image);
552 bpf_map_area_free(st_map->uvalue);
553 bpf_map_area_free(st_map);
556 static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
558 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
559 attr->map_flags || !attr->btf_vmlinux_value_type_id)
564 static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
566 const struct bpf_struct_ops *st_ops;
568 struct bpf_struct_ops_map *st_map;
569 const struct btf_type *t, *vt;
573 return ERR_PTR(-EPERM);
575 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
577 return ERR_PTR(-ENOTSUPP);
579 vt = st_ops->value_type;
580 if (attr->value_size != vt->size)
581 return ERR_PTR(-EINVAL);
585 st_map_size = sizeof(*st_map) +
587 * struct bpf_struct_ops_tcp_congestions_ops
589 (vt->size - sizeof(struct bpf_struct_ops_value));
591 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
593 return ERR_PTR(-ENOMEM);
595 st_map->st_ops = st_ops;
598 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
600 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_prog *),
602 st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
603 if (!st_map->uvalue || !st_map->progs || !st_map->image) {
604 bpf_struct_ops_map_free(map);
605 return ERR_PTR(-ENOMEM);
608 mutex_init(&st_map->lock);
609 set_vm_flush_reset_perms(st_map->image);
610 bpf_map_init_from_attr(map, attr);
615 static int bpf_struct_ops_map_btf_id;
616 const struct bpf_map_ops bpf_struct_ops_map_ops = {
617 .map_alloc_check = bpf_struct_ops_map_alloc_check,
618 .map_alloc = bpf_struct_ops_map_alloc,
619 .map_free = bpf_struct_ops_map_free,
620 .map_get_next_key = bpf_struct_ops_map_get_next_key,
621 .map_lookup_elem = bpf_struct_ops_map_lookup_elem,
622 .map_delete_elem = bpf_struct_ops_map_delete_elem,
623 .map_update_elem = bpf_struct_ops_map_update_elem,
624 .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
625 .map_btf_name = "bpf_struct_ops_map",
626 .map_btf_id = &bpf_struct_ops_map_btf_id,
629 /* "const void *" because some subsystem is
630 * passing a const (e.g. const struct tcp_congestion_ops *)
632 bool bpf_struct_ops_get(const void *kdata)
634 struct bpf_struct_ops_value *kvalue;
636 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
638 return refcount_inc_not_zero(&kvalue->refcnt);
641 static void bpf_struct_ops_put_rcu(struct rcu_head *head)
643 struct bpf_struct_ops_map *st_map;
645 st_map = container_of(head, struct bpf_struct_ops_map, rcu);
646 bpf_map_put(&st_map->map);
649 void bpf_struct_ops_put(const void *kdata)
651 struct bpf_struct_ops_value *kvalue;
653 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
654 if (refcount_dec_and_test(&kvalue->refcnt)) {
655 struct bpf_struct_ops_map *st_map;
657 st_map = container_of(kvalue, struct bpf_struct_ops_map,
659 /* The struct_ops's function may switch to another struct_ops.
661 * For example, bpf_tcp_cc_x->init() may switch to
662 * another tcp_cc_y by calling
663 * setsockopt(TCP_CONGESTION, "tcp_cc_y").
664 * During the switch, bpf_struct_ops_put(tcp_cc_x) is called
665 * and its map->refcnt may reach 0 which then free its
666 * trampoline image while tcp_cc_x is still running.
668 * Thus, a rcu grace period is needed here.
670 call_rcu(&st_map->rcu, bpf_struct_ops_put_rcu);