Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2019 Facebook */
3
4#include <linux/bpf.h>
5#include <linux/bpf_verifier.h>
6#include <linux/btf.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>
13#include <linux/btf_ids.h>
14
15enum bpf_struct_ops_state {
16 BPF_STRUCT_OPS_STATE_INIT,
17 BPF_STRUCT_OPS_STATE_INUSE,
18 BPF_STRUCT_OPS_STATE_TOBEFREE,
19};
20
21#define BPF_STRUCT_OPS_COMMON_VALUE \
22 refcount_t refcnt; \
23 enum bpf_struct_ops_state state
24
25struct bpf_struct_ops_value {
26 BPF_STRUCT_OPS_COMMON_VALUE;
27 char data[] ____cacheline_aligned_in_smp;
28};
29
30struct bpf_struct_ops_map {
31 struct bpf_map map;
32 struct rcu_head rcu;
33 const struct bpf_struct_ops *st_ops;
34 /* protect map_update */
35 struct mutex lock;
36 /* link has all the bpf_links that is populated
37 * to the func ptr of the kernel's struct
38 * (in kvalue.data).
39 */
40 struct bpf_link **links;
41 /* image is a page that has all the trampolines
42 * that stores the func args before calling the bpf_prog.
43 * A PAGE_SIZE "image" is enough to store all trampoline for
44 * "links[]".
45 */
46 void *image;
47 /* uvalue->data stores the kernel struct
48 * (e.g. tcp_congestion_ops) that is more useful
49 * to userspace than the kvalue. For example,
50 * the bpf_prog's id is stored instead of the kernel
51 * address of a func ptr.
52 */
53 struct bpf_struct_ops_value *uvalue;
54 /* kvalue.data stores the actual kernel's struct
55 * (e.g. tcp_congestion_ops) that will be
56 * registered to the kernel subsystem.
57 */
58 struct bpf_struct_ops_value kvalue;
59};
60
61#define VALUE_PREFIX "bpf_struct_ops_"
62#define VALUE_PREFIX_LEN (sizeof(VALUE_PREFIX) - 1)
63
64/* bpf_struct_ops_##_name (e.g. bpf_struct_ops_tcp_congestion_ops) is
65 * the map's value exposed to the userspace and its btf-type-id is
66 * stored at the map->btf_vmlinux_value_type_id.
67 *
68 */
69#define BPF_STRUCT_OPS_TYPE(_name) \
70extern struct bpf_struct_ops bpf_##_name; \
71 \
72struct bpf_struct_ops_##_name { \
73 BPF_STRUCT_OPS_COMMON_VALUE; \
74 struct _name data ____cacheline_aligned_in_smp; \
75};
76#include "bpf_struct_ops_types.h"
77#undef BPF_STRUCT_OPS_TYPE
78
79enum {
80#define BPF_STRUCT_OPS_TYPE(_name) BPF_STRUCT_OPS_TYPE_##_name,
81#include "bpf_struct_ops_types.h"
82#undef BPF_STRUCT_OPS_TYPE
83 __NR_BPF_STRUCT_OPS_TYPE,
84};
85
86static struct bpf_struct_ops * const bpf_struct_ops[] = {
87#define BPF_STRUCT_OPS_TYPE(_name) \
88 [BPF_STRUCT_OPS_TYPE_##_name] = &bpf_##_name,
89#include "bpf_struct_ops_types.h"
90#undef BPF_STRUCT_OPS_TYPE
91};
92
93const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {
94};
95
96const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
97#ifdef CONFIG_NET
98 .test_run = bpf_struct_ops_test_run,
99#endif
100};
101
102static const struct btf_type *module_type;
103
104void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
105{
106 s32 type_id, value_id, module_id;
107 const struct btf_member *member;
108 struct bpf_struct_ops *st_ops;
109 const struct btf_type *t;
110 char value_name[128];
111 const char *mname;
112 u32 i, j;
113
114 /* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */
115#define BPF_STRUCT_OPS_TYPE(_name) BTF_TYPE_EMIT(struct bpf_struct_ops_##_name);
116#include "bpf_struct_ops_types.h"
117#undef BPF_STRUCT_OPS_TYPE
118
119 module_id = btf_find_by_name_kind(btf, "module", BTF_KIND_STRUCT);
120 if (module_id < 0) {
121 pr_warn("Cannot find struct module in btf_vmlinux\n");
122 return;
123 }
124 module_type = btf_type_by_id(btf, module_id);
125
126 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
127 st_ops = bpf_struct_ops[i];
128
129 if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
130 sizeof(value_name)) {
131 pr_warn("struct_ops name %s is too long\n",
132 st_ops->name);
133 continue;
134 }
135 sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
136
137 value_id = btf_find_by_name_kind(btf, value_name,
138 BTF_KIND_STRUCT);
139 if (value_id < 0) {
140 pr_warn("Cannot find struct %s in btf_vmlinux\n",
141 value_name);
142 continue;
143 }
144
145 type_id = btf_find_by_name_kind(btf, st_ops->name,
146 BTF_KIND_STRUCT);
147 if (type_id < 0) {
148 pr_warn("Cannot find struct %s in btf_vmlinux\n",
149 st_ops->name);
150 continue;
151 }
152 t = btf_type_by_id(btf, type_id);
153 if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
154 pr_warn("Cannot support #%u members in struct %s\n",
155 btf_type_vlen(t), st_ops->name);
156 continue;
157 }
158
159 for_each_member(j, t, member) {
160 const struct btf_type *func_proto;
161
162 mname = btf_name_by_offset(btf, member->name_off);
163 if (!*mname) {
164 pr_warn("anon member in struct %s is not supported\n",
165 st_ops->name);
166 break;
167 }
168
169 if (__btf_member_bitfield_size(t, member)) {
170 pr_warn("bit field member %s in struct %s is not supported\n",
171 mname, st_ops->name);
172 break;
173 }
174
175 func_proto = btf_type_resolve_func_ptr(btf,
176 member->type,
177 NULL);
178 if (func_proto &&
179 btf_distill_func_proto(log, btf,
180 func_proto, mname,
181 &st_ops->func_models[j])) {
182 pr_warn("Error in parsing func ptr %s in struct %s\n",
183 mname, st_ops->name);
184 break;
185 }
186 }
187
188 if (j == btf_type_vlen(t)) {
189 if (st_ops->init(btf)) {
190 pr_warn("Error in init bpf_struct_ops %s\n",
191 st_ops->name);
192 } else {
193 st_ops->type_id = type_id;
194 st_ops->type = t;
195 st_ops->value_id = value_id;
196 st_ops->value_type = btf_type_by_id(btf,
197 value_id);
198 }
199 }
200 }
201}
202
203extern struct btf *btf_vmlinux;
204
205static const struct bpf_struct_ops *
206bpf_struct_ops_find_value(u32 value_id)
207{
208 unsigned int i;
209
210 if (!value_id || !btf_vmlinux)
211 return NULL;
212
213 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
214 if (bpf_struct_ops[i]->value_id == value_id)
215 return bpf_struct_ops[i];
216 }
217
218 return NULL;
219}
220
221const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
222{
223 unsigned int i;
224
225 if (!type_id || !btf_vmlinux)
226 return NULL;
227
228 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
229 if (bpf_struct_ops[i]->type_id == type_id)
230 return bpf_struct_ops[i];
231 }
232
233 return NULL;
234}
235
236static int bpf_struct_ops_map_get_next_key(struct bpf_map *map, void *key,
237 void *next_key)
238{
239 if (key && *(u32 *)key == 0)
240 return -ENOENT;
241
242 *(u32 *)next_key = 0;
243 return 0;
244}
245
246int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
247 void *value)
248{
249 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
250 struct bpf_struct_ops_value *uvalue, *kvalue;
251 enum bpf_struct_ops_state state;
252
253 if (unlikely(*(u32 *)key != 0))
254 return -ENOENT;
255
256 kvalue = &st_map->kvalue;
257 /* Pair with smp_store_release() during map_update */
258 state = smp_load_acquire(&kvalue->state);
259 if (state == BPF_STRUCT_OPS_STATE_INIT) {
260 memset(value, 0, map->value_size);
261 return 0;
262 }
263
264 /* No lock is needed. state and refcnt do not need
265 * to be updated together under atomic context.
266 */
267 uvalue = value;
268 memcpy(uvalue, st_map->uvalue, map->value_size);
269 uvalue->state = state;
270 refcount_set(&uvalue->refcnt, refcount_read(&kvalue->refcnt));
271
272 return 0;
273}
274
275static void *bpf_struct_ops_map_lookup_elem(struct bpf_map *map, void *key)
276{
277 return ERR_PTR(-EINVAL);
278}
279
280static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
281{
282 const struct btf_type *t = st_map->st_ops->type;
283 u32 i;
284
285 for (i = 0; i < btf_type_vlen(t); i++) {
286 if (st_map->links[i]) {
287 bpf_link_put(st_map->links[i]);
288 st_map->links[i] = NULL;
289 }
290 }
291}
292
293static int check_zero_holes(const struct btf_type *t, void *data)
294{
295 const struct btf_member *member;
296 u32 i, moff, msize, prev_mend = 0;
297 const struct btf_type *mtype;
298
299 for_each_member(i, t, member) {
300 moff = __btf_member_bit_offset(t, member) / 8;
301 if (moff > prev_mend &&
302 memchr_inv(data + prev_mend, 0, moff - prev_mend))
303 return -EINVAL;
304
305 mtype = btf_type_by_id(btf_vmlinux, member->type);
306 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
307 if (IS_ERR(mtype))
308 return PTR_ERR(mtype);
309 prev_mend = moff + msize;
310 }
311
312 if (t->size > prev_mend &&
313 memchr_inv(data + prev_mend, 0, t->size - prev_mend))
314 return -EINVAL;
315
316 return 0;
317}
318
319static void bpf_struct_ops_link_release(struct bpf_link *link)
320{
321}
322
323static void bpf_struct_ops_link_dealloc(struct bpf_link *link)
324{
325 struct bpf_tramp_link *tlink = container_of(link, struct bpf_tramp_link, link);
326
327 kfree(tlink);
328}
329
330const struct bpf_link_ops bpf_struct_ops_link_lops = {
331 .release = bpf_struct_ops_link_release,
332 .dealloc = bpf_struct_ops_link_dealloc,
333};
334
335int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
336 struct bpf_tramp_link *link,
337 const struct btf_func_model *model,
338 void *image, void *image_end)
339{
340 u32 flags;
341
342 tlinks[BPF_TRAMP_FENTRY].links[0] = link;
343 tlinks[BPF_TRAMP_FENTRY].nr_links = 1;
344 /* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
345 * and it must be used alone.
346 */
347 flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
348 return arch_prepare_bpf_trampoline(NULL, image, image_end,
349 model, flags, tlinks, NULL);
350}
351
352static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
353 void *value, u64 flags)
354{
355 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
356 const struct bpf_struct_ops *st_ops = st_map->st_ops;
357 struct bpf_struct_ops_value *uvalue, *kvalue;
358 const struct btf_member *member;
359 const struct btf_type *t = st_ops->type;
360 struct bpf_tramp_links *tlinks = NULL;
361 void *udata, *kdata;
362 int prog_fd, err = 0;
363 void *image, *image_end;
364 u32 i;
365
366 if (flags)
367 return -EINVAL;
368
369 if (*(u32 *)key != 0)
370 return -E2BIG;
371
372 err = check_zero_holes(st_ops->value_type, value);
373 if (err)
374 return err;
375
376 uvalue = value;
377 err = check_zero_holes(t, uvalue->data);
378 if (err)
379 return err;
380
381 if (uvalue->state || refcount_read(&uvalue->refcnt))
382 return -EINVAL;
383
384 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
385 if (!tlinks)
386 return -ENOMEM;
387
388 uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
389 kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
390
391 mutex_lock(&st_map->lock);
392
393 if (kvalue->state != BPF_STRUCT_OPS_STATE_INIT) {
394 err = -EBUSY;
395 goto unlock;
396 }
397
398 memcpy(uvalue, value, map->value_size);
399
400 udata = &uvalue->data;
401 kdata = &kvalue->data;
402 image = st_map->image;
403 image_end = st_map->image + PAGE_SIZE;
404
405 for_each_member(i, t, member) {
406 const struct btf_type *mtype, *ptype;
407 struct bpf_prog *prog;
408 struct bpf_tramp_link *link;
409 u32 moff;
410
411 moff = __btf_member_bit_offset(t, member) / 8;
412 ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
413 if (ptype == module_type) {
414 if (*(void **)(udata + moff))
415 goto reset_unlock;
416 *(void **)(kdata + moff) = BPF_MODULE_OWNER;
417 continue;
418 }
419
420 err = st_ops->init_member(t, member, kdata, udata);
421 if (err < 0)
422 goto reset_unlock;
423
424 /* The ->init_member() has handled this member */
425 if (err > 0)
426 continue;
427
428 /* If st_ops->init_member does not handle it,
429 * we will only handle func ptrs and zero-ed members
430 * here. Reject everything else.
431 */
432
433 /* All non func ptr member must be 0 */
434 if (!ptype || !btf_type_is_func_proto(ptype)) {
435 u32 msize;
436
437 mtype = btf_type_by_id(btf_vmlinux, member->type);
438 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
439 if (IS_ERR(mtype)) {
440 err = PTR_ERR(mtype);
441 goto reset_unlock;
442 }
443
444 if (memchr_inv(udata + moff, 0, msize)) {
445 err = -EINVAL;
446 goto reset_unlock;
447 }
448
449 continue;
450 }
451
452 prog_fd = (int)(*(unsigned long *)(udata + moff));
453 /* Similar check as the attr->attach_prog_fd */
454 if (!prog_fd)
455 continue;
456
457 prog = bpf_prog_get(prog_fd);
458 if (IS_ERR(prog)) {
459 err = PTR_ERR(prog);
460 goto reset_unlock;
461 }
462
463 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
464 prog->aux->attach_btf_id != st_ops->type_id ||
465 prog->expected_attach_type != i) {
466 bpf_prog_put(prog);
467 err = -EINVAL;
468 goto reset_unlock;
469 }
470
471 link = kzalloc(sizeof(*link), GFP_USER);
472 if (!link) {
473 bpf_prog_put(prog);
474 err = -ENOMEM;
475 goto reset_unlock;
476 }
477 bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS,
478 &bpf_struct_ops_link_lops, prog);
479 st_map->links[i] = &link->link;
480
481 err = bpf_struct_ops_prepare_trampoline(tlinks, link,
482 &st_ops->func_models[i],
483 image, image_end);
484 if (err < 0)
485 goto reset_unlock;
486
487 *(void **)(kdata + moff) = image;
488 image += err;
489
490 /* put prog_id to udata */
491 *(unsigned long *)(udata + moff) = prog->aux->id;
492 }
493
494 refcount_set(&kvalue->refcnt, 1);
495 bpf_map_inc(map);
496
497 set_memory_rox((long)st_map->image, 1);
498 err = st_ops->reg(kdata);
499 if (likely(!err)) {
500 /* Pair with smp_load_acquire() during lookup_elem().
501 * It ensures the above udata updates (e.g. prog->aux->id)
502 * can be seen once BPF_STRUCT_OPS_STATE_INUSE is set.
503 */
504 smp_store_release(&kvalue->state, BPF_STRUCT_OPS_STATE_INUSE);
505 goto unlock;
506 }
507
508 /* Error during st_ops->reg(). Can happen if this struct_ops needs to be
509 * verified as a whole, after all init_member() calls. Can also happen if
510 * there was a race in registering the struct_ops (under the same name) to
511 * a sub-system through different struct_ops's maps.
512 */
513 set_memory_nx((long)st_map->image, 1);
514 set_memory_rw((long)st_map->image, 1);
515 bpf_map_put(map);
516
517reset_unlock:
518 bpf_struct_ops_map_put_progs(st_map);
519 memset(uvalue, 0, map->value_size);
520 memset(kvalue, 0, map->value_size);
521unlock:
522 kfree(tlinks);
523 mutex_unlock(&st_map->lock);
524 return err;
525}
526
527static int bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
528{
529 enum bpf_struct_ops_state prev_state;
530 struct bpf_struct_ops_map *st_map;
531
532 st_map = (struct bpf_struct_ops_map *)map;
533 prev_state = cmpxchg(&st_map->kvalue.state,
534 BPF_STRUCT_OPS_STATE_INUSE,
535 BPF_STRUCT_OPS_STATE_TOBEFREE);
536 switch (prev_state) {
537 case BPF_STRUCT_OPS_STATE_INUSE:
538 st_map->st_ops->unreg(&st_map->kvalue.data);
539 if (refcount_dec_and_test(&st_map->kvalue.refcnt))
540 bpf_map_put(map);
541 return 0;
542 case BPF_STRUCT_OPS_STATE_TOBEFREE:
543 return -EINPROGRESS;
544 case BPF_STRUCT_OPS_STATE_INIT:
545 return -ENOENT;
546 default:
547 WARN_ON_ONCE(1);
548 /* Should never happen. Treat it as not found. */
549 return -ENOENT;
550 }
551}
552
553static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
554 struct seq_file *m)
555{
556 void *value;
557 int err;
558
559 value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
560 if (!value)
561 return;
562
563 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
564 if (!err) {
565 btf_type_seq_show(btf_vmlinux, map->btf_vmlinux_value_type_id,
566 value, m);
567 seq_puts(m, "\n");
568 }
569
570 kfree(value);
571}
572
573static void bpf_struct_ops_map_free(struct bpf_map *map)
574{
575 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
576
577 if (st_map->links)
578 bpf_struct_ops_map_put_progs(st_map);
579 bpf_map_area_free(st_map->links);
580 bpf_jit_free_exec(st_map->image);
581 bpf_map_area_free(st_map->uvalue);
582 bpf_map_area_free(st_map);
583}
584
585static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
586{
587 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
588 attr->map_flags || !attr->btf_vmlinux_value_type_id)
589 return -EINVAL;
590 return 0;
591}
592
593static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
594{
595 const struct bpf_struct_ops *st_ops;
596 size_t st_map_size;
597 struct bpf_struct_ops_map *st_map;
598 const struct btf_type *t, *vt;
599 struct bpf_map *map;
600
601 if (!bpf_capable())
602 return ERR_PTR(-EPERM);
603
604 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
605 if (!st_ops)
606 return ERR_PTR(-ENOTSUPP);
607
608 vt = st_ops->value_type;
609 if (attr->value_size != vt->size)
610 return ERR_PTR(-EINVAL);
611
612 t = st_ops->type;
613
614 st_map_size = sizeof(*st_map) +
615 /* kvalue stores the
616 * struct bpf_struct_ops_tcp_congestions_ops
617 */
618 (vt->size - sizeof(struct bpf_struct_ops_value));
619
620 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
621 if (!st_map)
622 return ERR_PTR(-ENOMEM);
623
624 st_map->st_ops = st_ops;
625 map = &st_map->map;
626
627 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
628 st_map->links =
629 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_links *),
630 NUMA_NO_NODE);
631 st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
632 if (!st_map->uvalue || !st_map->links || !st_map->image) {
633 bpf_struct_ops_map_free(map);
634 return ERR_PTR(-ENOMEM);
635 }
636
637 mutex_init(&st_map->lock);
638 set_vm_flush_reset_perms(st_map->image);
639 bpf_map_init_from_attr(map, attr);
640
641 return map;
642}
643
644BTF_ID_LIST_SINGLE(bpf_struct_ops_map_btf_ids, struct, bpf_struct_ops_map)
645const struct bpf_map_ops bpf_struct_ops_map_ops = {
646 .map_alloc_check = bpf_struct_ops_map_alloc_check,
647 .map_alloc = bpf_struct_ops_map_alloc,
648 .map_free = bpf_struct_ops_map_free,
649 .map_get_next_key = bpf_struct_ops_map_get_next_key,
650 .map_lookup_elem = bpf_struct_ops_map_lookup_elem,
651 .map_delete_elem = bpf_struct_ops_map_delete_elem,
652 .map_update_elem = bpf_struct_ops_map_update_elem,
653 .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
654 .map_btf_id = &bpf_struct_ops_map_btf_ids[0],
655};
656
657/* "const void *" because some subsystem is
658 * passing a const (e.g. const struct tcp_congestion_ops *)
659 */
660bool bpf_struct_ops_get(const void *kdata)
661{
662 struct bpf_struct_ops_value *kvalue;
663
664 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
665
666 return refcount_inc_not_zero(&kvalue->refcnt);
667}
668
669static void bpf_struct_ops_put_rcu(struct rcu_head *head)
670{
671 struct bpf_struct_ops_map *st_map;
672
673 st_map = container_of(head, struct bpf_struct_ops_map, rcu);
674 bpf_map_put(&st_map->map);
675}
676
677void bpf_struct_ops_put(const void *kdata)
678{
679 struct bpf_struct_ops_value *kvalue;
680
681 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
682 if (refcount_dec_and_test(&kvalue->refcnt)) {
683 struct bpf_struct_ops_map *st_map;
684
685 st_map = container_of(kvalue, struct bpf_struct_ops_map,
686 kvalue);
687 /* The struct_ops's function may switch to another struct_ops.
688 *
689 * For example, bpf_tcp_cc_x->init() may switch to
690 * another tcp_cc_y by calling
691 * setsockopt(TCP_CONGESTION, "tcp_cc_y").
692 * During the switch, bpf_struct_ops_put(tcp_cc_x) is called
693 * and its map->refcnt may reach 0 which then free its
694 * trampoline image while tcp_cc_x is still running.
695 *
696 * Thus, a rcu grace period is needed here.
697 */
698 call_rcu(&st_map->rcu, bpf_struct_ops_put_rcu);
699 }
700}
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2019 Facebook */
3
4#include <linux/bpf.h>
5#include <linux/bpf_verifier.h>
6#include <linux/btf.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>
13
14enum bpf_struct_ops_state {
15 BPF_STRUCT_OPS_STATE_INIT,
16 BPF_STRUCT_OPS_STATE_INUSE,
17 BPF_STRUCT_OPS_STATE_TOBEFREE,
18};
19
20#define BPF_STRUCT_OPS_COMMON_VALUE \
21 refcount_t refcnt; \
22 enum bpf_struct_ops_state state
23
24struct bpf_struct_ops_value {
25 BPF_STRUCT_OPS_COMMON_VALUE;
26 char data[] ____cacheline_aligned_in_smp;
27};
28
29struct bpf_struct_ops_map {
30 struct bpf_map map;
31 const struct bpf_struct_ops *st_ops;
32 /* protect map_update */
33 struct mutex lock;
34 /* progs has all the bpf_prog that is populated
35 * to the func ptr of the kernel's struct
36 * (in kvalue.data).
37 */
38 struct bpf_prog **progs;
39 /* image is a page that has all the trampolines
40 * that stores the func args before calling the bpf_prog.
41 * A PAGE_SIZE "image" is enough to store all trampoline for
42 * "progs[]".
43 */
44 void *image;
45 /* uvalue->data stores the kernel struct
46 * (e.g. tcp_congestion_ops) that is more useful
47 * to userspace than the kvalue. For example,
48 * the bpf_prog's id is stored instead of the kernel
49 * address of a func ptr.
50 */
51 struct bpf_struct_ops_value *uvalue;
52 /* kvalue.data stores the actual kernel's struct
53 * (e.g. tcp_congestion_ops) that will be
54 * registered to the kernel subsystem.
55 */
56 struct bpf_struct_ops_value kvalue;
57};
58
59#define VALUE_PREFIX "bpf_struct_ops_"
60#define VALUE_PREFIX_LEN (sizeof(VALUE_PREFIX) - 1)
61
62/* bpf_struct_ops_##_name (e.g. bpf_struct_ops_tcp_congestion_ops) is
63 * the map's value exposed to the userspace and its btf-type-id is
64 * stored at the map->btf_vmlinux_value_type_id.
65 *
66 */
67#define BPF_STRUCT_OPS_TYPE(_name) \
68extern struct bpf_struct_ops bpf_##_name; \
69 \
70struct bpf_struct_ops_##_name { \
71 BPF_STRUCT_OPS_COMMON_VALUE; \
72 struct _name data ____cacheline_aligned_in_smp; \
73};
74#include "bpf_struct_ops_types.h"
75#undef BPF_STRUCT_OPS_TYPE
76
77enum {
78#define BPF_STRUCT_OPS_TYPE(_name) BPF_STRUCT_OPS_TYPE_##_name,
79#include "bpf_struct_ops_types.h"
80#undef BPF_STRUCT_OPS_TYPE
81 __NR_BPF_STRUCT_OPS_TYPE,
82};
83
84static struct bpf_struct_ops * const bpf_struct_ops[] = {
85#define BPF_STRUCT_OPS_TYPE(_name) \
86 [BPF_STRUCT_OPS_TYPE_##_name] = &bpf_##_name,
87#include "bpf_struct_ops_types.h"
88#undef BPF_STRUCT_OPS_TYPE
89};
90
91const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {
92};
93
94const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
95};
96
97static const struct btf_type *module_type;
98
99void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
100{
101 s32 type_id, value_id, module_id;
102 const struct btf_member *member;
103 struct bpf_struct_ops *st_ops;
104 const struct btf_type *t;
105 char value_name[128];
106 const char *mname;
107 u32 i, j;
108
109 /* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */
110#define BPF_STRUCT_OPS_TYPE(_name) BTF_TYPE_EMIT(struct bpf_struct_ops_##_name);
111#include "bpf_struct_ops_types.h"
112#undef BPF_STRUCT_OPS_TYPE
113
114 module_id = btf_find_by_name_kind(btf, "module", BTF_KIND_STRUCT);
115 if (module_id < 0) {
116 pr_warn("Cannot find struct module in btf_vmlinux\n");
117 return;
118 }
119 module_type = btf_type_by_id(btf, module_id);
120
121 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
122 st_ops = bpf_struct_ops[i];
123
124 if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
125 sizeof(value_name)) {
126 pr_warn("struct_ops name %s is too long\n",
127 st_ops->name);
128 continue;
129 }
130 sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
131
132 value_id = btf_find_by_name_kind(btf, value_name,
133 BTF_KIND_STRUCT);
134 if (value_id < 0) {
135 pr_warn("Cannot find struct %s in btf_vmlinux\n",
136 value_name);
137 continue;
138 }
139
140 type_id = btf_find_by_name_kind(btf, st_ops->name,
141 BTF_KIND_STRUCT);
142 if (type_id < 0) {
143 pr_warn("Cannot find struct %s in btf_vmlinux\n",
144 st_ops->name);
145 continue;
146 }
147 t = btf_type_by_id(btf, type_id);
148 if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
149 pr_warn("Cannot support #%u members in struct %s\n",
150 btf_type_vlen(t), st_ops->name);
151 continue;
152 }
153
154 for_each_member(j, t, member) {
155 const struct btf_type *func_proto;
156
157 mname = btf_name_by_offset(btf, member->name_off);
158 if (!*mname) {
159 pr_warn("anon member in struct %s is not supported\n",
160 st_ops->name);
161 break;
162 }
163
164 if (btf_member_bitfield_size(t, member)) {
165 pr_warn("bit field member %s in struct %s is not supported\n",
166 mname, st_ops->name);
167 break;
168 }
169
170 func_proto = btf_type_resolve_func_ptr(btf,
171 member->type,
172 NULL);
173 if (func_proto &&
174 btf_distill_func_proto(log, btf,
175 func_proto, mname,
176 &st_ops->func_models[j])) {
177 pr_warn("Error in parsing func ptr %s in struct %s\n",
178 mname, st_ops->name);
179 break;
180 }
181 }
182
183 if (j == btf_type_vlen(t)) {
184 if (st_ops->init(btf)) {
185 pr_warn("Error in init bpf_struct_ops %s\n",
186 st_ops->name);
187 } else {
188 st_ops->type_id = type_id;
189 st_ops->type = t;
190 st_ops->value_id = value_id;
191 st_ops->value_type = btf_type_by_id(btf,
192 value_id);
193 }
194 }
195 }
196}
197
198extern struct btf *btf_vmlinux;
199
200static const struct bpf_struct_ops *
201bpf_struct_ops_find_value(u32 value_id)
202{
203 unsigned int i;
204
205 if (!value_id || !btf_vmlinux)
206 return NULL;
207
208 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
209 if (bpf_struct_ops[i]->value_id == value_id)
210 return bpf_struct_ops[i];
211 }
212
213 return NULL;
214}
215
216const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
217{
218 unsigned int i;
219
220 if (!type_id || !btf_vmlinux)
221 return NULL;
222
223 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
224 if (bpf_struct_ops[i]->type_id == type_id)
225 return bpf_struct_ops[i];
226 }
227
228 return NULL;
229}
230
231static int bpf_struct_ops_map_get_next_key(struct bpf_map *map, void *key,
232 void *next_key)
233{
234 if (key && *(u32 *)key == 0)
235 return -ENOENT;
236
237 *(u32 *)next_key = 0;
238 return 0;
239}
240
241int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
242 void *value)
243{
244 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
245 struct bpf_struct_ops_value *uvalue, *kvalue;
246 enum bpf_struct_ops_state state;
247
248 if (unlikely(*(u32 *)key != 0))
249 return -ENOENT;
250
251 kvalue = &st_map->kvalue;
252 /* Pair with smp_store_release() during map_update */
253 state = smp_load_acquire(&kvalue->state);
254 if (state == BPF_STRUCT_OPS_STATE_INIT) {
255 memset(value, 0, map->value_size);
256 return 0;
257 }
258
259 /* No lock is needed. state and refcnt do not need
260 * to be updated together under atomic context.
261 */
262 uvalue = (struct bpf_struct_ops_value *)value;
263 memcpy(uvalue, st_map->uvalue, map->value_size);
264 uvalue->state = state;
265 refcount_set(&uvalue->refcnt, refcount_read(&kvalue->refcnt));
266
267 return 0;
268}
269
270static void *bpf_struct_ops_map_lookup_elem(struct bpf_map *map, void *key)
271{
272 return ERR_PTR(-EINVAL);
273}
274
275static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
276{
277 const struct btf_type *t = st_map->st_ops->type;
278 u32 i;
279
280 for (i = 0; i < btf_type_vlen(t); i++) {
281 if (st_map->progs[i]) {
282 bpf_prog_put(st_map->progs[i]);
283 st_map->progs[i] = NULL;
284 }
285 }
286}
287
288static int check_zero_holes(const struct btf_type *t, void *data)
289{
290 const struct btf_member *member;
291 u32 i, moff, msize, prev_mend = 0;
292 const struct btf_type *mtype;
293
294 for_each_member(i, t, member) {
295 moff = btf_member_bit_offset(t, member) / 8;
296 if (moff > prev_mend &&
297 memchr_inv(data + prev_mend, 0, moff - prev_mend))
298 return -EINVAL;
299
300 mtype = btf_type_by_id(btf_vmlinux, member->type);
301 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
302 if (IS_ERR(mtype))
303 return PTR_ERR(mtype);
304 prev_mend = moff + msize;
305 }
306
307 if (t->size > prev_mend &&
308 memchr_inv(data + prev_mend, 0, t->size - prev_mend))
309 return -EINVAL;
310
311 return 0;
312}
313
314static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
315 void *value, u64 flags)
316{
317 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
318 const struct bpf_struct_ops *st_ops = st_map->st_ops;
319 struct bpf_struct_ops_value *uvalue, *kvalue;
320 const struct btf_member *member;
321 const struct btf_type *t = st_ops->type;
322 struct bpf_tramp_progs *tprogs = NULL;
323 void *udata, *kdata;
324 int prog_fd, err = 0;
325 void *image;
326 u32 i;
327
328 if (flags)
329 return -EINVAL;
330
331 if (*(u32 *)key != 0)
332 return -E2BIG;
333
334 err = check_zero_holes(st_ops->value_type, value);
335 if (err)
336 return err;
337
338 uvalue = (struct bpf_struct_ops_value *)value;
339 err = check_zero_holes(t, uvalue->data);
340 if (err)
341 return err;
342
343 if (uvalue->state || refcount_read(&uvalue->refcnt))
344 return -EINVAL;
345
346 tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
347 if (!tprogs)
348 return -ENOMEM;
349
350 uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
351 kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
352
353 mutex_lock(&st_map->lock);
354
355 if (kvalue->state != BPF_STRUCT_OPS_STATE_INIT) {
356 err = -EBUSY;
357 goto unlock;
358 }
359
360 memcpy(uvalue, value, map->value_size);
361
362 udata = &uvalue->data;
363 kdata = &kvalue->data;
364 image = st_map->image;
365
366 for_each_member(i, t, member) {
367 const struct btf_type *mtype, *ptype;
368 struct bpf_prog *prog;
369 u32 moff;
370 u32 flags;
371
372 moff = btf_member_bit_offset(t, member) / 8;
373 ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
374 if (ptype == module_type) {
375 if (*(void **)(udata + moff))
376 goto reset_unlock;
377 *(void **)(kdata + moff) = BPF_MODULE_OWNER;
378 continue;
379 }
380
381 err = st_ops->init_member(t, member, kdata, udata);
382 if (err < 0)
383 goto reset_unlock;
384
385 /* The ->init_member() has handled this member */
386 if (err > 0)
387 continue;
388
389 /* If st_ops->init_member does not handle it,
390 * we will only handle func ptrs and zero-ed members
391 * here. Reject everything else.
392 */
393
394 /* All non func ptr member must be 0 */
395 if (!ptype || !btf_type_is_func_proto(ptype)) {
396 u32 msize;
397
398 mtype = btf_type_by_id(btf_vmlinux, member->type);
399 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
400 if (IS_ERR(mtype)) {
401 err = PTR_ERR(mtype);
402 goto reset_unlock;
403 }
404
405 if (memchr_inv(udata + moff, 0, msize)) {
406 err = -EINVAL;
407 goto reset_unlock;
408 }
409
410 continue;
411 }
412
413 prog_fd = (int)(*(unsigned long *)(udata + moff));
414 /* Similar check as the attr->attach_prog_fd */
415 if (!prog_fd)
416 continue;
417
418 prog = bpf_prog_get(prog_fd);
419 if (IS_ERR(prog)) {
420 err = PTR_ERR(prog);
421 goto reset_unlock;
422 }
423 st_map->progs[i] = prog;
424
425 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
426 prog->aux->attach_btf_id != st_ops->type_id ||
427 prog->expected_attach_type != i) {
428 err = -EINVAL;
429 goto reset_unlock;
430 }
431
432 tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
433 tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
434 flags = st_ops->func_models[i].ret_size > 0 ?
435 BPF_TRAMP_F_RET_FENTRY_RET : 0;
436 err = arch_prepare_bpf_trampoline(NULL, image,
437 st_map->image + PAGE_SIZE,
438 &st_ops->func_models[i],
439 flags, tprogs, NULL);
440 if (err < 0)
441 goto reset_unlock;
442
443 *(void **)(kdata + moff) = image;
444 image += err;
445
446 /* put prog_id to udata */
447 *(unsigned long *)(udata + moff) = prog->aux->id;
448 }
449
450 refcount_set(&kvalue->refcnt, 1);
451 bpf_map_inc(map);
452
453 set_memory_ro((long)st_map->image, 1);
454 set_memory_x((long)st_map->image, 1);
455 err = st_ops->reg(kdata);
456 if (likely(!err)) {
457 /* Pair with smp_load_acquire() during lookup_elem().
458 * It ensures the above udata updates (e.g. prog->aux->id)
459 * can be seen once BPF_STRUCT_OPS_STATE_INUSE is set.
460 */
461 smp_store_release(&kvalue->state, BPF_STRUCT_OPS_STATE_INUSE);
462 goto unlock;
463 }
464
465 /* Error during st_ops->reg(). It is very unlikely since
466 * the above init_member() should have caught it earlier
467 * before reg(). The only possibility is if there was a race
468 * in registering the struct_ops (under the same name) to
469 * a sub-system through different struct_ops's maps.
470 */
471 set_memory_nx((long)st_map->image, 1);
472 set_memory_rw((long)st_map->image, 1);
473 bpf_map_put(map);
474
475reset_unlock:
476 bpf_struct_ops_map_put_progs(st_map);
477 memset(uvalue, 0, map->value_size);
478 memset(kvalue, 0, map->value_size);
479unlock:
480 kfree(tprogs);
481 mutex_unlock(&st_map->lock);
482 return err;
483}
484
485static int bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
486{
487 enum bpf_struct_ops_state prev_state;
488 struct bpf_struct_ops_map *st_map;
489
490 st_map = (struct bpf_struct_ops_map *)map;
491 prev_state = cmpxchg(&st_map->kvalue.state,
492 BPF_STRUCT_OPS_STATE_INUSE,
493 BPF_STRUCT_OPS_STATE_TOBEFREE);
494 switch (prev_state) {
495 case BPF_STRUCT_OPS_STATE_INUSE:
496 st_map->st_ops->unreg(&st_map->kvalue.data);
497 if (refcount_dec_and_test(&st_map->kvalue.refcnt))
498 bpf_map_put(map);
499 return 0;
500 case BPF_STRUCT_OPS_STATE_TOBEFREE:
501 return -EINPROGRESS;
502 case BPF_STRUCT_OPS_STATE_INIT:
503 return -ENOENT;
504 default:
505 WARN_ON_ONCE(1);
506 /* Should never happen. Treat it as not found. */
507 return -ENOENT;
508 }
509}
510
511static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
512 struct seq_file *m)
513{
514 void *value;
515 int err;
516
517 value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
518 if (!value)
519 return;
520
521 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
522 if (!err) {
523 btf_type_seq_show(btf_vmlinux, map->btf_vmlinux_value_type_id,
524 value, m);
525 seq_puts(m, "\n");
526 }
527
528 kfree(value);
529}
530
531static void bpf_struct_ops_map_free(struct bpf_map *map)
532{
533 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
534
535 if (st_map->progs)
536 bpf_struct_ops_map_put_progs(st_map);
537 bpf_map_area_free(st_map->progs);
538 bpf_jit_free_exec(st_map->image);
539 bpf_map_area_free(st_map->uvalue);
540 bpf_map_area_free(st_map);
541}
542
543static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
544{
545 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
546 attr->map_flags || !attr->btf_vmlinux_value_type_id)
547 return -EINVAL;
548 return 0;
549}
550
551static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
552{
553 const struct bpf_struct_ops *st_ops;
554 size_t st_map_size;
555 struct bpf_struct_ops_map *st_map;
556 const struct btf_type *t, *vt;
557 struct bpf_map *map;
558
559 if (!bpf_capable())
560 return ERR_PTR(-EPERM);
561
562 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
563 if (!st_ops)
564 return ERR_PTR(-ENOTSUPP);
565
566 vt = st_ops->value_type;
567 if (attr->value_size != vt->size)
568 return ERR_PTR(-EINVAL);
569
570 t = st_ops->type;
571
572 st_map_size = sizeof(*st_map) +
573 /* kvalue stores the
574 * struct bpf_struct_ops_tcp_congestions_ops
575 */
576 (vt->size - sizeof(struct bpf_struct_ops_value));
577
578 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
579 if (!st_map)
580 return ERR_PTR(-ENOMEM);
581
582 st_map->st_ops = st_ops;
583 map = &st_map->map;
584
585 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
586 st_map->progs =
587 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_prog *),
588 NUMA_NO_NODE);
589 st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
590 if (!st_map->uvalue || !st_map->progs || !st_map->image) {
591 bpf_struct_ops_map_free(map);
592 return ERR_PTR(-ENOMEM);
593 }
594
595 mutex_init(&st_map->lock);
596 set_vm_flush_reset_perms(st_map->image);
597 bpf_map_init_from_attr(map, attr);
598
599 return map;
600}
601
602static int bpf_struct_ops_map_btf_id;
603const struct bpf_map_ops bpf_struct_ops_map_ops = {
604 .map_alloc_check = bpf_struct_ops_map_alloc_check,
605 .map_alloc = bpf_struct_ops_map_alloc,
606 .map_free = bpf_struct_ops_map_free,
607 .map_get_next_key = bpf_struct_ops_map_get_next_key,
608 .map_lookup_elem = bpf_struct_ops_map_lookup_elem,
609 .map_delete_elem = bpf_struct_ops_map_delete_elem,
610 .map_update_elem = bpf_struct_ops_map_update_elem,
611 .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
612 .map_btf_name = "bpf_struct_ops_map",
613 .map_btf_id = &bpf_struct_ops_map_btf_id,
614};
615
616/* "const void *" because some subsystem is
617 * passing a const (e.g. const struct tcp_congestion_ops *)
618 */
619bool bpf_struct_ops_get(const void *kdata)
620{
621 struct bpf_struct_ops_value *kvalue;
622
623 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
624
625 return refcount_inc_not_zero(&kvalue->refcnt);
626}
627
628void bpf_struct_ops_put(const void *kdata)
629{
630 struct bpf_struct_ops_value *kvalue;
631
632 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
633 if (refcount_dec_and_test(&kvalue->refcnt)) {
634 struct bpf_struct_ops_map *st_map;
635
636 st_map = container_of(kvalue, struct bpf_struct_ops_map,
637 kvalue);
638 bpf_map_put(&st_map->map);
639 }
640}