blob: 44be101f25621fc02a64497833ac8662aeccd936 [file] [log] [blame]
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001// 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>
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080012#include <linux/mutex.h>
Martin KaFai Lau27ae79972020-01-08 16:35:03 -080013
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080014enum 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;
Gustavo A. R. Silvad7f10df2020-02-26 18:17:44 -060026 char data[] ____cacheline_aligned_in_smp;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080027};
28
29struct bpf_struct_ops_map {
30 struct bpf_map map;
Martin KaFai Laueb18b49e2021-08-24 10:30:07 -070031 struct rcu_head rcu;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080032 const struct bpf_struct_ops *st_ops;
33 /* protect map_update */
34 struct mutex lock;
35 /* progs has all the bpf_prog that is populated
36 * to the func ptr of the kernel's struct
37 * (in kvalue.data).
38 */
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
43 * "progs[]".
44 */
45 void *image;
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.
51 */
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.
56 */
57 struct bpf_struct_ops_value kvalue;
58};
59
60#define VALUE_PREFIX "bpf_struct_ops_"
61#define VALUE_PREFIX_LEN (sizeof(VALUE_PREFIX) - 1)
62
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.
66 *
67 */
Martin KaFai Lau27ae79972020-01-08 16:35:03 -080068#define BPF_STRUCT_OPS_TYPE(_name) \
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080069extern struct bpf_struct_ops bpf_##_name; \
70 \
71struct bpf_struct_ops_##_name { \
72 BPF_STRUCT_OPS_COMMON_VALUE; \
73 struct _name data ____cacheline_aligned_in_smp; \
74};
Martin KaFai Lau27ae79972020-01-08 16:35:03 -080075#include "bpf_struct_ops_types.h"
76#undef BPF_STRUCT_OPS_TYPE
77
78enum {
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,
83};
84
85static 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
90};
91
92const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {
93};
94
95const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
96};
97
Martin KaFai Lau85d33df2020-01-08 16:35:05 -080098static const struct btf_type *module_type;
99
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -0800100void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800101{
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800102 s32 type_id, value_id, module_id;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800103 const struct btf_member *member;
104 struct bpf_struct_ops *st_ops;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800105 const struct btf_type *t;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800106 char value_name[128];
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800107 const char *mname;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800108 u32 i, j;
109
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800110 /* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */
111#define BPF_STRUCT_OPS_TYPE(_name) BTF_TYPE_EMIT(struct bpf_struct_ops_##_name);
112#include "bpf_struct_ops_types.h"
113#undef BPF_STRUCT_OPS_TYPE
114
115 module_id = btf_find_by_name_kind(btf, "module", BTF_KIND_STRUCT);
116 if (module_id < 0) {
117 pr_warn("Cannot find struct module in btf_vmlinux\n");
118 return;
119 }
120 module_type = btf_type_by_id(btf, module_id);
121
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800122 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
123 st_ops = bpf_struct_ops[i];
124
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800125 if (strlen(st_ops->name) + VALUE_PREFIX_LEN >=
126 sizeof(value_name)) {
127 pr_warn("struct_ops name %s is too long\n",
128 st_ops->name);
129 continue;
130 }
131 sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
132
133 value_id = btf_find_by_name_kind(btf, value_name,
134 BTF_KIND_STRUCT);
135 if (value_id < 0) {
136 pr_warn("Cannot find struct %s in btf_vmlinux\n",
137 value_name);
138 continue;
139 }
140
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800141 type_id = btf_find_by_name_kind(btf, st_ops->name,
142 BTF_KIND_STRUCT);
143 if (type_id < 0) {
144 pr_warn("Cannot find struct %s in btf_vmlinux\n",
145 st_ops->name);
146 continue;
147 }
148 t = btf_type_by_id(btf, type_id);
149 if (btf_type_vlen(t) > BPF_STRUCT_OPS_MAX_NR_MEMBERS) {
150 pr_warn("Cannot support #%u members in struct %s\n",
151 btf_type_vlen(t), st_ops->name);
152 continue;
153 }
154
155 for_each_member(j, t, member) {
156 const struct btf_type *func_proto;
157
158 mname = btf_name_by_offset(btf, member->name_off);
159 if (!*mname) {
160 pr_warn("anon member in struct %s is not supported\n",
161 st_ops->name);
162 break;
163 }
164
165 if (btf_member_bitfield_size(t, member)) {
166 pr_warn("bit field member %s in struct %s is not supported\n",
167 mname, st_ops->name);
168 break;
169 }
170
171 func_proto = btf_type_resolve_func_ptr(btf,
172 member->type,
173 NULL);
174 if (func_proto &&
Martin KaFai Laud3e42bb2020-01-27 09:51:45 -0800175 btf_distill_func_proto(log, btf,
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800176 func_proto, mname,
177 &st_ops->func_models[j])) {
178 pr_warn("Error in parsing func ptr %s in struct %s\n",
179 mname, st_ops->name);
180 break;
181 }
182 }
183
184 if (j == btf_type_vlen(t)) {
185 if (st_ops->init(btf)) {
186 pr_warn("Error in init bpf_struct_ops %s\n",
187 st_ops->name);
188 } else {
189 st_ops->type_id = type_id;
190 st_ops->type = t;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800191 st_ops->value_id = value_id;
192 st_ops->value_type = btf_type_by_id(btf,
193 value_id);
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800194 }
195 }
196 }
197}
198
199extern struct btf *btf_vmlinux;
200
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800201static const struct bpf_struct_ops *
202bpf_struct_ops_find_value(u32 value_id)
203{
204 unsigned int i;
205
206 if (!value_id || !btf_vmlinux)
207 return NULL;
208
209 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
210 if (bpf_struct_ops[i]->value_id == value_id)
211 return bpf_struct_ops[i];
212 }
213
214 return NULL;
215}
216
Martin KaFai Lau27ae79972020-01-08 16:35:03 -0800217const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
218{
219 unsigned int i;
220
221 if (!type_id || !btf_vmlinux)
222 return NULL;
223
224 for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
225 if (bpf_struct_ops[i]->type_id == type_id)
226 return bpf_struct_ops[i];
227 }
228
229 return NULL;
230}
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800231
232static int bpf_struct_ops_map_get_next_key(struct bpf_map *map, void *key,
233 void *next_key)
234{
235 if (key && *(u32 *)key == 0)
236 return -ENOENT;
237
238 *(u32 *)next_key = 0;
239 return 0;
240}
241
242int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
243 void *value)
244{
245 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
246 struct bpf_struct_ops_value *uvalue, *kvalue;
247 enum bpf_struct_ops_state state;
248
249 if (unlikely(*(u32 *)key != 0))
250 return -ENOENT;
251
252 kvalue = &st_map->kvalue;
253 /* Pair with smp_store_release() during map_update */
254 state = smp_load_acquire(&kvalue->state);
255 if (state == BPF_STRUCT_OPS_STATE_INIT) {
256 memset(value, 0, map->value_size);
257 return 0;
258 }
259
260 /* No lock is needed. state and refcnt do not need
261 * to be updated together under atomic context.
262 */
263 uvalue = (struct bpf_struct_ops_value *)value;
264 memcpy(uvalue, st_map->uvalue, map->value_size);
265 uvalue->state = state;
266 refcount_set(&uvalue->refcnt, refcount_read(&kvalue->refcnt));
267
268 return 0;
269}
270
271static void *bpf_struct_ops_map_lookup_elem(struct bpf_map *map, void *key)
272{
273 return ERR_PTR(-EINVAL);
274}
275
276static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
277{
278 const struct btf_type *t = st_map->st_ops->type;
279 u32 i;
280
281 for (i = 0; i < btf_type_vlen(t); i++) {
282 if (st_map->progs[i]) {
283 bpf_prog_put(st_map->progs[i]);
284 st_map->progs[i] = NULL;
285 }
286 }
287}
288
289static int check_zero_holes(const struct btf_type *t, void *data)
290{
291 const struct btf_member *member;
292 u32 i, moff, msize, prev_mend = 0;
293 const struct btf_type *mtype;
294
295 for_each_member(i, t, member) {
296 moff = btf_member_bit_offset(t, member) / 8;
297 if (moff > prev_mend &&
298 memchr_inv(data + prev_mend, 0, moff - prev_mend))
299 return -EINVAL;
300
301 mtype = btf_type_by_id(btf_vmlinux, member->type);
Jiri Olsa62983992020-08-25 21:21:13 +0200302 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800303 if (IS_ERR(mtype))
304 return PTR_ERR(mtype);
305 prev_mend = moff + msize;
306 }
307
308 if (t->size > prev_mend &&
309 memchr_inv(data + prev_mend, 0, t->size - prev_mend))
310 return -EINVAL;
311
312 return 0;
313}
314
Hou Tao31a645a2021-10-25 14:40:22 +0800315int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_progs *tprogs,
316 struct bpf_prog *prog,
317 const struct btf_func_model *model,
318 void *image, void *image_end)
319{
320 u32 flags;
321
322 tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
323 tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
324 flags = model->ret_size > 0 ? BPF_TRAMP_F_RET_FENTRY_RET : 0;
325 return arch_prepare_bpf_trampoline(NULL, image, image_end,
326 model, flags, tprogs, NULL);
327}
328
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800329static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
330 void *value, u64 flags)
331{
332 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
333 const struct bpf_struct_ops *st_ops = st_map->st_ops;
334 struct bpf_struct_ops_value *uvalue, *kvalue;
335 const struct btf_member *member;
336 const struct btf_type *t = st_ops->type;
KP Singh88fd9e52020-03-04 20:18:47 +0100337 struct bpf_tramp_progs *tprogs = NULL;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800338 void *udata, *kdata;
339 int prog_fd, err = 0;
Hou Tao31a645a2021-10-25 14:40:22 +0800340 void *image, *image_end;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800341 u32 i;
342
343 if (flags)
344 return -EINVAL;
345
346 if (*(u32 *)key != 0)
347 return -E2BIG;
348
349 err = check_zero_holes(st_ops->value_type, value);
350 if (err)
351 return err;
352
353 uvalue = (struct bpf_struct_ops_value *)value;
354 err = check_zero_holes(t, uvalue->data);
355 if (err)
356 return err;
357
358 if (uvalue->state || refcount_read(&uvalue->refcnt))
359 return -EINVAL;
360
KP Singh88fd9e52020-03-04 20:18:47 +0100361 tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
362 if (!tprogs)
363 return -ENOMEM;
364
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800365 uvalue = (struct bpf_struct_ops_value *)st_map->uvalue;
366 kvalue = (struct bpf_struct_ops_value *)&st_map->kvalue;
367
368 mutex_lock(&st_map->lock);
369
370 if (kvalue->state != BPF_STRUCT_OPS_STATE_INIT) {
371 err = -EBUSY;
372 goto unlock;
373 }
374
375 memcpy(uvalue, value, map->value_size);
376
377 udata = &uvalue->data;
378 kdata = &kvalue->data;
379 image = st_map->image;
Hou Tao31a645a2021-10-25 14:40:22 +0800380 image_end = st_map->image + PAGE_SIZE;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800381
382 for_each_member(i, t, member) {
383 const struct btf_type *mtype, *ptype;
384 struct bpf_prog *prog;
385 u32 moff;
386
387 moff = btf_member_bit_offset(t, member) / 8;
388 ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
389 if (ptype == module_type) {
390 if (*(void **)(udata + moff))
391 goto reset_unlock;
392 *(void **)(kdata + moff) = BPF_MODULE_OWNER;
393 continue;
394 }
395
396 err = st_ops->init_member(t, member, kdata, udata);
397 if (err < 0)
398 goto reset_unlock;
399
400 /* The ->init_member() has handled this member */
401 if (err > 0)
402 continue;
403
404 /* If st_ops->init_member does not handle it,
405 * we will only handle func ptrs and zero-ed members
406 * here. Reject everything else.
407 */
408
409 /* All non func ptr member must be 0 */
410 if (!ptype || !btf_type_is_func_proto(ptype)) {
411 u32 msize;
412
413 mtype = btf_type_by_id(btf_vmlinux, member->type);
Jiri Olsa62983992020-08-25 21:21:13 +0200414 mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800415 if (IS_ERR(mtype)) {
416 err = PTR_ERR(mtype);
417 goto reset_unlock;
418 }
419
420 if (memchr_inv(udata + moff, 0, msize)) {
421 err = -EINVAL;
422 goto reset_unlock;
423 }
424
425 continue;
426 }
427
428 prog_fd = (int)(*(unsigned long *)(udata + moff));
429 /* Similar check as the attr->attach_prog_fd */
430 if (!prog_fd)
431 continue;
432
433 prog = bpf_prog_get(prog_fd);
434 if (IS_ERR(prog)) {
435 err = PTR_ERR(prog);
436 goto reset_unlock;
437 }
438 st_map->progs[i] = prog;
439
440 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
441 prog->aux->attach_btf_id != st_ops->type_id ||
442 prog->expected_attach_type != i) {
443 err = -EINVAL;
444 goto reset_unlock;
445 }
446
Hou Tao31a645a2021-10-25 14:40:22 +0800447 err = bpf_struct_ops_prepare_trampoline(tprogs, prog,
448 &st_ops->func_models[i],
449 image, image_end);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800450 if (err < 0)
451 goto reset_unlock;
452
453 *(void **)(kdata + moff) = image;
454 image += err;
455
456 /* put prog_id to udata */
457 *(unsigned long *)(udata + moff) = prog->aux->id;
458 }
459
460 refcount_set(&kvalue->refcnt, 1);
461 bpf_map_inc(map);
462
463 set_memory_ro((long)st_map->image, 1);
464 set_memory_x((long)st_map->image, 1);
465 err = st_ops->reg(kdata);
466 if (likely(!err)) {
467 /* Pair with smp_load_acquire() during lookup_elem().
468 * It ensures the above udata updates (e.g. prog->aux->id)
469 * can be seen once BPF_STRUCT_OPS_STATE_INUSE is set.
470 */
471 smp_store_release(&kvalue->state, BPF_STRUCT_OPS_STATE_INUSE);
472 goto unlock;
473 }
474
475 /* Error during st_ops->reg(). It is very unlikely since
476 * the above init_member() should have caught it earlier
477 * before reg(). The only possibility is if there was a race
478 * in registering the struct_ops (under the same name) to
479 * a sub-system through different struct_ops's maps.
480 */
481 set_memory_nx((long)st_map->image, 1);
482 set_memory_rw((long)st_map->image, 1);
483 bpf_map_put(map);
484
485reset_unlock:
486 bpf_struct_ops_map_put_progs(st_map);
487 memset(uvalue, 0, map->value_size);
488 memset(kvalue, 0, map->value_size);
489unlock:
KP Singh88fd9e52020-03-04 20:18:47 +0100490 kfree(tprogs);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800491 mutex_unlock(&st_map->lock);
492 return err;
493}
494
495static int bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
496{
497 enum bpf_struct_ops_state prev_state;
498 struct bpf_struct_ops_map *st_map;
499
500 st_map = (struct bpf_struct_ops_map *)map;
501 prev_state = cmpxchg(&st_map->kvalue.state,
502 BPF_STRUCT_OPS_STATE_INUSE,
503 BPF_STRUCT_OPS_STATE_TOBEFREE);
Martin KaFai Lau8e5290e2020-03-04 17:34:47 -0800504 switch (prev_state) {
505 case BPF_STRUCT_OPS_STATE_INUSE:
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800506 st_map->st_ops->unreg(&st_map->kvalue.data);
507 if (refcount_dec_and_test(&st_map->kvalue.refcnt))
508 bpf_map_put(map);
Martin KaFai Lau8e5290e2020-03-04 17:34:47 -0800509 return 0;
510 case BPF_STRUCT_OPS_STATE_TOBEFREE:
511 return -EINPROGRESS;
512 case BPF_STRUCT_OPS_STATE_INIT:
513 return -ENOENT;
514 default:
515 WARN_ON_ONCE(1);
516 /* Should never happen. Treat it as not found. */
517 return -ENOENT;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800518 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800519}
520
521static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
522 struct seq_file *m)
523{
524 void *value;
Martin KaFai Lau3b413042020-01-13 23:26:47 -0800525 int err;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800526
Martin KaFai Lau3b413042020-01-13 23:26:47 -0800527 value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800528 if (!value)
529 return;
530
Martin KaFai Lau3b413042020-01-13 23:26:47 -0800531 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
532 if (!err) {
533 btf_type_seq_show(btf_vmlinux, map->btf_vmlinux_value_type_id,
534 value, m);
535 seq_puts(m, "\n");
536 }
537
538 kfree(value);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800539}
540
541static void bpf_struct_ops_map_free(struct bpf_map *map)
542{
543 struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
544
545 if (st_map->progs)
546 bpf_struct_ops_map_put_progs(st_map);
547 bpf_map_area_free(st_map->progs);
548 bpf_jit_free_exec(st_map->image);
549 bpf_map_area_free(st_map->uvalue);
550 bpf_map_area_free(st_map);
551}
552
553static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
554{
555 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
556 attr->map_flags || !attr->btf_vmlinux_value_type_id)
557 return -EINVAL;
558 return 0;
559}
560
561static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
562{
563 const struct bpf_struct_ops *st_ops;
Roman Gushchinf0437332020-12-01 13:58:45 -0800564 size_t st_map_size;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800565 struct bpf_struct_ops_map *st_map;
566 const struct btf_type *t, *vt;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800567 struct bpf_map *map;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800568
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -0700569 if (!bpf_capable())
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800570 return ERR_PTR(-EPERM);
571
572 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
573 if (!st_ops)
574 return ERR_PTR(-ENOTSUPP);
575
576 vt = st_ops->value_type;
577 if (attr->value_size != vt->size)
578 return ERR_PTR(-EINVAL);
579
580 t = st_ops->type;
581
582 st_map_size = sizeof(*st_map) +
583 /* kvalue stores the
584 * struct bpf_struct_ops_tcp_congestions_ops
585 */
586 (vt->size - sizeof(struct bpf_struct_ops_value));
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800587
588 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
Roman Gushchinf0437332020-12-01 13:58:45 -0800589 if (!st_map)
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800590 return ERR_PTR(-ENOMEM);
Roman Gushchinf0437332020-12-01 13:58:45 -0800591
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800592 st_map->st_ops = st_ops;
593 map = &st_map->map;
594
595 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
596 st_map->progs =
597 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_prog *),
598 NUMA_NO_NODE);
599 st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
600 if (!st_map->uvalue || !st_map->progs || !st_map->image) {
601 bpf_struct_ops_map_free(map);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800602 return ERR_PTR(-ENOMEM);
603 }
604
605 mutex_init(&st_map->lock);
606 set_vm_flush_reset_perms(st_map->image);
607 bpf_map_init_from_attr(map, attr);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800608
609 return map;
610}
611
Andrey Ignatov2872e9a2020-06-19 14:11:44 -0700612static int bpf_struct_ops_map_btf_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800613const struct bpf_map_ops bpf_struct_ops_map_ops = {
614 .map_alloc_check = bpf_struct_ops_map_alloc_check,
615 .map_alloc = bpf_struct_ops_map_alloc,
616 .map_free = bpf_struct_ops_map_free,
617 .map_get_next_key = bpf_struct_ops_map_get_next_key,
618 .map_lookup_elem = bpf_struct_ops_map_lookup_elem,
619 .map_delete_elem = bpf_struct_ops_map_delete_elem,
620 .map_update_elem = bpf_struct_ops_map_update_elem,
621 .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
Andrey Ignatov2872e9a2020-06-19 14:11:44 -0700622 .map_btf_name = "bpf_struct_ops_map",
623 .map_btf_id = &bpf_struct_ops_map_btf_id,
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800624};
625
626/* "const void *" because some subsystem is
627 * passing a const (e.g. const struct tcp_congestion_ops *)
628 */
629bool bpf_struct_ops_get(const void *kdata)
630{
631 struct bpf_struct_ops_value *kvalue;
632
633 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
634
635 return refcount_inc_not_zero(&kvalue->refcnt);
636}
637
Martin KaFai Laueb18b49e2021-08-24 10:30:07 -0700638static void bpf_struct_ops_put_rcu(struct rcu_head *head)
639{
640 struct bpf_struct_ops_map *st_map;
641
642 st_map = container_of(head, struct bpf_struct_ops_map, rcu);
643 bpf_map_put(&st_map->map);
644}
645
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800646void bpf_struct_ops_put(const void *kdata)
647{
648 struct bpf_struct_ops_value *kvalue;
649
650 kvalue = container_of(kdata, struct bpf_struct_ops_value, data);
651 if (refcount_dec_and_test(&kvalue->refcnt)) {
652 struct bpf_struct_ops_map *st_map;
653
654 st_map = container_of(kvalue, struct bpf_struct_ops_map,
655 kvalue);
Martin KaFai Laueb18b49e2021-08-24 10:30:07 -0700656 /* The struct_ops's function may switch to another struct_ops.
657 *
658 * For example, bpf_tcp_cc_x->init() may switch to
659 * another tcp_cc_y by calling
660 * setsockopt(TCP_CONGESTION, "tcp_cc_y").
661 * During the switch, bpf_struct_ops_put(tcp_cc_x) is called
662 * and its map->refcnt may reach 0 which then free its
663 * trampoline image while tcp_cc_x is still running.
664 *
665 * Thus, a rcu grace period is needed here.
666 */
667 call_rcu(&st_map->rcu, bpf_struct_ops_put_rcu);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800668 }
669}