bpf: Use bpf_mem_free_rcu when bpf_obj_dropping refcounted nodes
[platform/kernel/linux-rpi.git] / kernel / bpf / helpers.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  */
4 #include <linux/bpf.h>
5 #include <linux/btf.h>
6 #include <linux/bpf-cgroup.h>
7 #include <linux/cgroup.h>
8 #include <linux/rcupdate.h>
9 #include <linux/random.h>
10 #include <linux/smp.h>
11 #include <linux/topology.h>
12 #include <linux/ktime.h>
13 #include <linux/sched.h>
14 #include <linux/uidgid.h>
15 #include <linux/filter.h>
16 #include <linux/ctype.h>
17 #include <linux/jiffies.h>
18 #include <linux/pid_namespace.h>
19 #include <linux/poison.h>
20 #include <linux/proc_ns.h>
21 #include <linux/sched/task.h>
22 #include <linux/security.h>
23 #include <linux/btf_ids.h>
24 #include <linux/bpf_mem_alloc.h>
25
26 #include "../../lib/kstrtox.h"
27
28 /* If kernel subsystem is allowing eBPF programs to call this function,
29  * inside its own verifier_ops->get_func_proto() callback it should return
30  * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
31  *
32  * Different map implementations will rely on rcu in map methods
33  * lookup/update/delete, therefore eBPF programs must run under rcu lock
34  * if program is allowed to access maps, so check rcu_read_lock_held in
35  * all three functions.
36  */
37 BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
38 {
39         WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
40         return (unsigned long) map->ops->map_lookup_elem(map, key);
41 }
42
43 const struct bpf_func_proto bpf_map_lookup_elem_proto = {
44         .func           = bpf_map_lookup_elem,
45         .gpl_only       = false,
46         .pkt_access     = true,
47         .ret_type       = RET_PTR_TO_MAP_VALUE_OR_NULL,
48         .arg1_type      = ARG_CONST_MAP_PTR,
49         .arg2_type      = ARG_PTR_TO_MAP_KEY,
50 };
51
52 BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
53            void *, value, u64, flags)
54 {
55         WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
56         return map->ops->map_update_elem(map, key, value, flags);
57 }
58
59 const struct bpf_func_proto bpf_map_update_elem_proto = {
60         .func           = bpf_map_update_elem,
61         .gpl_only       = false,
62         .pkt_access     = true,
63         .ret_type       = RET_INTEGER,
64         .arg1_type      = ARG_CONST_MAP_PTR,
65         .arg2_type      = ARG_PTR_TO_MAP_KEY,
66         .arg3_type      = ARG_PTR_TO_MAP_VALUE,
67         .arg4_type      = ARG_ANYTHING,
68 };
69
70 BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
71 {
72         WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
73         return map->ops->map_delete_elem(map, key);
74 }
75
76 const struct bpf_func_proto bpf_map_delete_elem_proto = {
77         .func           = bpf_map_delete_elem,
78         .gpl_only       = false,
79         .pkt_access     = true,
80         .ret_type       = RET_INTEGER,
81         .arg1_type      = ARG_CONST_MAP_PTR,
82         .arg2_type      = ARG_PTR_TO_MAP_KEY,
83 };
84
85 BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags)
86 {
87         return map->ops->map_push_elem(map, value, flags);
88 }
89
90 const struct bpf_func_proto bpf_map_push_elem_proto = {
91         .func           = bpf_map_push_elem,
92         .gpl_only       = false,
93         .pkt_access     = true,
94         .ret_type       = RET_INTEGER,
95         .arg1_type      = ARG_CONST_MAP_PTR,
96         .arg2_type      = ARG_PTR_TO_MAP_VALUE,
97         .arg3_type      = ARG_ANYTHING,
98 };
99
100 BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value)
101 {
102         return map->ops->map_pop_elem(map, value);
103 }
104
105 const struct bpf_func_proto bpf_map_pop_elem_proto = {
106         .func           = bpf_map_pop_elem,
107         .gpl_only       = false,
108         .ret_type       = RET_INTEGER,
109         .arg1_type      = ARG_CONST_MAP_PTR,
110         .arg2_type      = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT,
111 };
112
113 BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value)
114 {
115         return map->ops->map_peek_elem(map, value);
116 }
117
118 const struct bpf_func_proto bpf_map_peek_elem_proto = {
119         .func           = bpf_map_peek_elem,
120         .gpl_only       = false,
121         .ret_type       = RET_INTEGER,
122         .arg1_type      = ARG_CONST_MAP_PTR,
123         .arg2_type      = ARG_PTR_TO_MAP_VALUE | MEM_UNINIT,
124 };
125
126 BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu)
127 {
128         WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
129         return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu);
130 }
131
132 const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto = {
133         .func           = bpf_map_lookup_percpu_elem,
134         .gpl_only       = false,
135         .pkt_access     = true,
136         .ret_type       = RET_PTR_TO_MAP_VALUE_OR_NULL,
137         .arg1_type      = ARG_CONST_MAP_PTR,
138         .arg2_type      = ARG_PTR_TO_MAP_KEY,
139         .arg3_type      = ARG_ANYTHING,
140 };
141
142 const struct bpf_func_proto bpf_get_prandom_u32_proto = {
143         .func           = bpf_user_rnd_u32,
144         .gpl_only       = false,
145         .ret_type       = RET_INTEGER,
146 };
147
148 BPF_CALL_0(bpf_get_smp_processor_id)
149 {
150         return smp_processor_id();
151 }
152
153 const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
154         .func           = bpf_get_smp_processor_id,
155         .gpl_only       = false,
156         .ret_type       = RET_INTEGER,
157 };
158
159 BPF_CALL_0(bpf_get_numa_node_id)
160 {
161         return numa_node_id();
162 }
163
164 const struct bpf_func_proto bpf_get_numa_node_id_proto = {
165         .func           = bpf_get_numa_node_id,
166         .gpl_only       = false,
167         .ret_type       = RET_INTEGER,
168 };
169
170 BPF_CALL_0(bpf_ktime_get_ns)
171 {
172         /* NMI safe access to clock monotonic */
173         return ktime_get_mono_fast_ns();
174 }
175
176 const struct bpf_func_proto bpf_ktime_get_ns_proto = {
177         .func           = bpf_ktime_get_ns,
178         .gpl_only       = false,
179         .ret_type       = RET_INTEGER,
180 };
181
182 BPF_CALL_0(bpf_ktime_get_boot_ns)
183 {
184         /* NMI safe access to clock boottime */
185         return ktime_get_boot_fast_ns();
186 }
187
188 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto = {
189         .func           = bpf_ktime_get_boot_ns,
190         .gpl_only       = false,
191         .ret_type       = RET_INTEGER,
192 };
193
194 BPF_CALL_0(bpf_ktime_get_coarse_ns)
195 {
196         return ktime_get_coarse_ns();
197 }
198
199 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto = {
200         .func           = bpf_ktime_get_coarse_ns,
201         .gpl_only       = false,
202         .ret_type       = RET_INTEGER,
203 };
204
205 BPF_CALL_0(bpf_ktime_get_tai_ns)
206 {
207         /* NMI safe access to clock tai */
208         return ktime_get_tai_fast_ns();
209 }
210
211 const struct bpf_func_proto bpf_ktime_get_tai_ns_proto = {
212         .func           = bpf_ktime_get_tai_ns,
213         .gpl_only       = false,
214         .ret_type       = RET_INTEGER,
215 };
216
217 BPF_CALL_0(bpf_get_current_pid_tgid)
218 {
219         struct task_struct *task = current;
220
221         if (unlikely(!task))
222                 return -EINVAL;
223
224         return (u64) task->tgid << 32 | task->pid;
225 }
226
227 const struct bpf_func_proto bpf_get_current_pid_tgid_proto = {
228         .func           = bpf_get_current_pid_tgid,
229         .gpl_only       = false,
230         .ret_type       = RET_INTEGER,
231 };
232
233 BPF_CALL_0(bpf_get_current_uid_gid)
234 {
235         struct task_struct *task = current;
236         kuid_t uid;
237         kgid_t gid;
238
239         if (unlikely(!task))
240                 return -EINVAL;
241
242         current_uid_gid(&uid, &gid);
243         return (u64) from_kgid(&init_user_ns, gid) << 32 |
244                      from_kuid(&init_user_ns, uid);
245 }
246
247 const struct bpf_func_proto bpf_get_current_uid_gid_proto = {
248         .func           = bpf_get_current_uid_gid,
249         .gpl_only       = false,
250         .ret_type       = RET_INTEGER,
251 };
252
253 BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
254 {
255         struct task_struct *task = current;
256
257         if (unlikely(!task))
258                 goto err_clear;
259
260         /* Verifier guarantees that size > 0 */
261         strscpy_pad(buf, task->comm, size);
262         return 0;
263 err_clear:
264         memset(buf, 0, size);
265         return -EINVAL;
266 }
267
268 const struct bpf_func_proto bpf_get_current_comm_proto = {
269         .func           = bpf_get_current_comm,
270         .gpl_only       = false,
271         .ret_type       = RET_INTEGER,
272         .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
273         .arg2_type      = ARG_CONST_SIZE,
274 };
275
276 #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
277
278 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
279 {
280         arch_spinlock_t *l = (void *)lock;
281         union {
282                 __u32 val;
283                 arch_spinlock_t lock;
284         } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED };
285
286         compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
287         BUILD_BUG_ON(sizeof(*l) != sizeof(__u32));
288         BUILD_BUG_ON(sizeof(*lock) != sizeof(__u32));
289         arch_spin_lock(l);
290 }
291
292 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
293 {
294         arch_spinlock_t *l = (void *)lock;
295
296         arch_spin_unlock(l);
297 }
298
299 #else
300
301 static inline void __bpf_spin_lock(struct bpf_spin_lock *lock)
302 {
303         atomic_t *l = (void *)lock;
304
305         BUILD_BUG_ON(sizeof(*l) != sizeof(*lock));
306         do {
307                 atomic_cond_read_relaxed(l, !VAL);
308         } while (atomic_xchg(l, 1));
309 }
310
311 static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock)
312 {
313         atomic_t *l = (void *)lock;
314
315         atomic_set_release(l, 0);
316 }
317
318 #endif
319
320 static DEFINE_PER_CPU(unsigned long, irqsave_flags);
321
322 static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock *lock)
323 {
324         unsigned long flags;
325
326         local_irq_save(flags);
327         __bpf_spin_lock(lock);
328         __this_cpu_write(irqsave_flags, flags);
329 }
330
331 notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock)
332 {
333         __bpf_spin_lock_irqsave(lock);
334         return 0;
335 }
336
337 const struct bpf_func_proto bpf_spin_lock_proto = {
338         .func           = bpf_spin_lock,
339         .gpl_only       = false,
340         .ret_type       = RET_VOID,
341         .arg1_type      = ARG_PTR_TO_SPIN_LOCK,
342         .arg1_btf_id    = BPF_PTR_POISON,
343 };
344
345 static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock *lock)
346 {
347         unsigned long flags;
348
349         flags = __this_cpu_read(irqsave_flags);
350         __bpf_spin_unlock(lock);
351         local_irq_restore(flags);
352 }
353
354 notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock)
355 {
356         __bpf_spin_unlock_irqrestore(lock);
357         return 0;
358 }
359
360 const struct bpf_func_proto bpf_spin_unlock_proto = {
361         .func           = bpf_spin_unlock,
362         .gpl_only       = false,
363         .ret_type       = RET_VOID,
364         .arg1_type      = ARG_PTR_TO_SPIN_LOCK,
365         .arg1_btf_id    = BPF_PTR_POISON,
366 };
367
368 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
369                            bool lock_src)
370 {
371         struct bpf_spin_lock *lock;
372
373         if (lock_src)
374                 lock = src + map->record->spin_lock_off;
375         else
376                 lock = dst + map->record->spin_lock_off;
377         preempt_disable();
378         __bpf_spin_lock_irqsave(lock);
379         copy_map_value(map, dst, src);
380         __bpf_spin_unlock_irqrestore(lock);
381         preempt_enable();
382 }
383
384 BPF_CALL_0(bpf_jiffies64)
385 {
386         return get_jiffies_64();
387 }
388
389 const struct bpf_func_proto bpf_jiffies64_proto = {
390         .func           = bpf_jiffies64,
391         .gpl_only       = false,
392         .ret_type       = RET_INTEGER,
393 };
394
395 #ifdef CONFIG_CGROUPS
396 BPF_CALL_0(bpf_get_current_cgroup_id)
397 {
398         struct cgroup *cgrp;
399         u64 cgrp_id;
400
401         rcu_read_lock();
402         cgrp = task_dfl_cgroup(current);
403         cgrp_id = cgroup_id(cgrp);
404         rcu_read_unlock();
405
406         return cgrp_id;
407 }
408
409 const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
410         .func           = bpf_get_current_cgroup_id,
411         .gpl_only       = false,
412         .ret_type       = RET_INTEGER,
413 };
414
415 BPF_CALL_1(bpf_get_current_ancestor_cgroup_id, int, ancestor_level)
416 {
417         struct cgroup *cgrp;
418         struct cgroup *ancestor;
419         u64 cgrp_id;
420
421         rcu_read_lock();
422         cgrp = task_dfl_cgroup(current);
423         ancestor = cgroup_ancestor(cgrp, ancestor_level);
424         cgrp_id = ancestor ? cgroup_id(ancestor) : 0;
425         rcu_read_unlock();
426
427         return cgrp_id;
428 }
429
430 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = {
431         .func           = bpf_get_current_ancestor_cgroup_id,
432         .gpl_only       = false,
433         .ret_type       = RET_INTEGER,
434         .arg1_type      = ARG_ANYTHING,
435 };
436 #endif /* CONFIG_CGROUPS */
437
438 #define BPF_STRTOX_BASE_MASK 0x1F
439
440 static int __bpf_strtoull(const char *buf, size_t buf_len, u64 flags,
441                           unsigned long long *res, bool *is_negative)
442 {
443         unsigned int base = flags & BPF_STRTOX_BASE_MASK;
444         const char *cur_buf = buf;
445         size_t cur_len = buf_len;
446         unsigned int consumed;
447         size_t val_len;
448         char str[64];
449
450         if (!buf || !buf_len || !res || !is_negative)
451                 return -EINVAL;
452
453         if (base != 0 && base != 8 && base != 10 && base != 16)
454                 return -EINVAL;
455
456         if (flags & ~BPF_STRTOX_BASE_MASK)
457                 return -EINVAL;
458
459         while (cur_buf < buf + buf_len && isspace(*cur_buf))
460                 ++cur_buf;
461
462         *is_negative = (cur_buf < buf + buf_len && *cur_buf == '-');
463         if (*is_negative)
464                 ++cur_buf;
465
466         consumed = cur_buf - buf;
467         cur_len -= consumed;
468         if (!cur_len)
469                 return -EINVAL;
470
471         cur_len = min(cur_len, sizeof(str) - 1);
472         memcpy(str, cur_buf, cur_len);
473         str[cur_len] = '\0';
474         cur_buf = str;
475
476         cur_buf = _parse_integer_fixup_radix(cur_buf, &base);
477         val_len = _parse_integer(cur_buf, base, res);
478
479         if (val_len & KSTRTOX_OVERFLOW)
480                 return -ERANGE;
481
482         if (val_len == 0)
483                 return -EINVAL;
484
485         cur_buf += val_len;
486         consumed += cur_buf - str;
487
488         return consumed;
489 }
490
491 static int __bpf_strtoll(const char *buf, size_t buf_len, u64 flags,
492                          long long *res)
493 {
494         unsigned long long _res;
495         bool is_negative;
496         int err;
497
498         err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
499         if (err < 0)
500                 return err;
501         if (is_negative) {
502                 if ((long long)-_res > 0)
503                         return -ERANGE;
504                 *res = -_res;
505         } else {
506                 if ((long long)_res < 0)
507                         return -ERANGE;
508                 *res = _res;
509         }
510         return err;
511 }
512
513 BPF_CALL_4(bpf_strtol, const char *, buf, size_t, buf_len, u64, flags,
514            long *, res)
515 {
516         long long _res;
517         int err;
518
519         err = __bpf_strtoll(buf, buf_len, flags, &_res);
520         if (err < 0)
521                 return err;
522         if (_res != (long)_res)
523                 return -ERANGE;
524         *res = _res;
525         return err;
526 }
527
528 const struct bpf_func_proto bpf_strtol_proto = {
529         .func           = bpf_strtol,
530         .gpl_only       = false,
531         .ret_type       = RET_INTEGER,
532         .arg1_type      = ARG_PTR_TO_MEM | MEM_RDONLY,
533         .arg2_type      = ARG_CONST_SIZE,
534         .arg3_type      = ARG_ANYTHING,
535         .arg4_type      = ARG_PTR_TO_LONG,
536 };
537
538 BPF_CALL_4(bpf_strtoul, const char *, buf, size_t, buf_len, u64, flags,
539            unsigned long *, res)
540 {
541         unsigned long long _res;
542         bool is_negative;
543         int err;
544
545         err = __bpf_strtoull(buf, buf_len, flags, &_res, &is_negative);
546         if (err < 0)
547                 return err;
548         if (is_negative)
549                 return -EINVAL;
550         if (_res != (unsigned long)_res)
551                 return -ERANGE;
552         *res = _res;
553         return err;
554 }
555
556 const struct bpf_func_proto bpf_strtoul_proto = {
557         .func           = bpf_strtoul,
558         .gpl_only       = false,
559         .ret_type       = RET_INTEGER,
560         .arg1_type      = ARG_PTR_TO_MEM | MEM_RDONLY,
561         .arg2_type      = ARG_CONST_SIZE,
562         .arg3_type      = ARG_ANYTHING,
563         .arg4_type      = ARG_PTR_TO_LONG,
564 };
565
566 BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
567 {
568         return strncmp(s1, s2, s1_sz);
569 }
570
571 static const struct bpf_func_proto bpf_strncmp_proto = {
572         .func           = bpf_strncmp,
573         .gpl_only       = false,
574         .ret_type       = RET_INTEGER,
575         .arg1_type      = ARG_PTR_TO_MEM | MEM_RDONLY,
576         .arg2_type      = ARG_CONST_SIZE,
577         .arg3_type      = ARG_PTR_TO_CONST_STR,
578 };
579
580 BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
581            struct bpf_pidns_info *, nsdata, u32, size)
582 {
583         struct task_struct *task = current;
584         struct pid_namespace *pidns;
585         int err = -EINVAL;
586
587         if (unlikely(size != sizeof(struct bpf_pidns_info)))
588                 goto clear;
589
590         if (unlikely((u64)(dev_t)dev != dev))
591                 goto clear;
592
593         if (unlikely(!task))
594                 goto clear;
595
596         pidns = task_active_pid_ns(task);
597         if (unlikely(!pidns)) {
598                 err = -ENOENT;
599                 goto clear;
600         }
601
602         if (!ns_match(&pidns->ns, (dev_t)dev, ino))
603                 goto clear;
604
605         nsdata->pid = task_pid_nr_ns(task, pidns);
606         nsdata->tgid = task_tgid_nr_ns(task, pidns);
607         return 0;
608 clear:
609         memset((void *)nsdata, 0, (size_t) size);
610         return err;
611 }
612
613 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = {
614         .func           = bpf_get_ns_current_pid_tgid,
615         .gpl_only       = false,
616         .ret_type       = RET_INTEGER,
617         .arg1_type      = ARG_ANYTHING,
618         .arg2_type      = ARG_ANYTHING,
619         .arg3_type      = ARG_PTR_TO_UNINIT_MEM,
620         .arg4_type      = ARG_CONST_SIZE,
621 };
622
623 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
624         .func           = bpf_get_raw_cpu_id,
625         .gpl_only       = false,
626         .ret_type       = RET_INTEGER,
627 };
628
629 BPF_CALL_5(bpf_event_output_data, void *, ctx, struct bpf_map *, map,
630            u64, flags, void *, data, u64, size)
631 {
632         if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
633                 return -EINVAL;
634
635         return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
636 }
637
638 const struct bpf_func_proto bpf_event_output_data_proto =  {
639         .func           = bpf_event_output_data,
640         .gpl_only       = true,
641         .ret_type       = RET_INTEGER,
642         .arg1_type      = ARG_PTR_TO_CTX,
643         .arg2_type      = ARG_CONST_MAP_PTR,
644         .arg3_type      = ARG_ANYTHING,
645         .arg4_type      = ARG_PTR_TO_MEM | MEM_RDONLY,
646         .arg5_type      = ARG_CONST_SIZE_OR_ZERO,
647 };
648
649 BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
650            const void __user *, user_ptr)
651 {
652         int ret = copy_from_user(dst, user_ptr, size);
653
654         if (unlikely(ret)) {
655                 memset(dst, 0, size);
656                 ret = -EFAULT;
657         }
658
659         return ret;
660 }
661
662 const struct bpf_func_proto bpf_copy_from_user_proto = {
663         .func           = bpf_copy_from_user,
664         .gpl_only       = false,
665         .might_sleep    = true,
666         .ret_type       = RET_INTEGER,
667         .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
668         .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
669         .arg3_type      = ARG_ANYTHING,
670 };
671
672 BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
673            const void __user *, user_ptr, struct task_struct *, tsk, u64, flags)
674 {
675         int ret;
676
677         /* flags is not used yet */
678         if (unlikely(flags))
679                 return -EINVAL;
680
681         if (unlikely(!size))
682                 return 0;
683
684         ret = access_process_vm(tsk, (unsigned long)user_ptr, dst, size, 0);
685         if (ret == size)
686                 return 0;
687
688         memset(dst, 0, size);
689         /* Return -EFAULT for partial read */
690         return ret < 0 ? ret : -EFAULT;
691 }
692
693 const struct bpf_func_proto bpf_copy_from_user_task_proto = {
694         .func           = bpf_copy_from_user_task,
695         .gpl_only       = true,
696         .might_sleep    = true,
697         .ret_type       = RET_INTEGER,
698         .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
699         .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
700         .arg3_type      = ARG_ANYTHING,
701         .arg4_type      = ARG_PTR_TO_BTF_ID,
702         .arg4_btf_id    = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
703         .arg5_type      = ARG_ANYTHING
704 };
705
706 BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu)
707 {
708         if (cpu >= nr_cpu_ids)
709                 return (unsigned long)NULL;
710
711         return (unsigned long)per_cpu_ptr((const void __percpu *)ptr, cpu);
712 }
713
714 const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
715         .func           = bpf_per_cpu_ptr,
716         .gpl_only       = false,
717         .ret_type       = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY,
718         .arg1_type      = ARG_PTR_TO_PERCPU_BTF_ID,
719         .arg2_type      = ARG_ANYTHING,
720 };
721
722 BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr)
723 {
724         return (unsigned long)this_cpu_ptr((const void __percpu *)percpu_ptr);
725 }
726
727 const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
728         .func           = bpf_this_cpu_ptr,
729         .gpl_only       = false,
730         .ret_type       = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY,
731         .arg1_type      = ARG_PTR_TO_PERCPU_BTF_ID,
732 };
733
734 static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
735                 size_t bufsz)
736 {
737         void __user *user_ptr = (__force void __user *)unsafe_ptr;
738
739         buf[0] = 0;
740
741         switch (fmt_ptype) {
742         case 's':
743 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
744                 if ((unsigned long)unsafe_ptr < TASK_SIZE)
745                         return strncpy_from_user_nofault(buf, user_ptr, bufsz);
746                 fallthrough;
747 #endif
748         case 'k':
749                 return strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz);
750         case 'u':
751                 return strncpy_from_user_nofault(buf, user_ptr, bufsz);
752         }
753
754         return -EINVAL;
755 }
756
757 /* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
758  * arguments representation.
759  */
760 #define MAX_BPRINTF_BIN_ARGS    512
761
762 /* Support executing three nested bprintf helper calls on a given CPU */
763 #define MAX_BPRINTF_NEST_LEVEL  3
764 struct bpf_bprintf_buffers {
765         char bin_args[MAX_BPRINTF_BIN_ARGS];
766         char buf[MAX_BPRINTF_BUF];
767 };
768
769 static DEFINE_PER_CPU(struct bpf_bprintf_buffers[MAX_BPRINTF_NEST_LEVEL], bpf_bprintf_bufs);
770 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
771
772 static int try_get_buffers(struct bpf_bprintf_buffers **bufs)
773 {
774         int nest_level;
775
776         preempt_disable();
777         nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
778         if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
779                 this_cpu_dec(bpf_bprintf_nest_level);
780                 preempt_enable();
781                 return -EBUSY;
782         }
783         *bufs = this_cpu_ptr(&bpf_bprintf_bufs[nest_level - 1]);
784
785         return 0;
786 }
787
788 void bpf_bprintf_cleanup(struct bpf_bprintf_data *data)
789 {
790         if (!data->bin_args && !data->buf)
791                 return;
792         if (WARN_ON_ONCE(this_cpu_read(bpf_bprintf_nest_level) == 0))
793                 return;
794         this_cpu_dec(bpf_bprintf_nest_level);
795         preempt_enable();
796 }
797
798 /*
799  * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers
800  *
801  * Returns a negative value if fmt is an invalid format string or 0 otherwise.
802  *
803  * This can be used in two ways:
804  * - Format string verification only: when data->get_bin_args is false
805  * - Arguments preparation: in addition to the above verification, it writes in
806  *   data->bin_args a binary representation of arguments usable by bstr_printf
807  *   where pointers from BPF have been sanitized.
808  *
809  * In argument preparation mode, if 0 is returned, safe temporary buffers are
810  * allocated and bpf_bprintf_cleanup should be called to free them after use.
811  */
812 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
813                         u32 num_args, struct bpf_bprintf_data *data)
814 {
815         bool get_buffers = (data->get_bin_args && num_args) || data->get_buf;
816         char *unsafe_ptr = NULL, *tmp_buf = NULL, *tmp_buf_end, *fmt_end;
817         struct bpf_bprintf_buffers *buffers = NULL;
818         size_t sizeof_cur_arg, sizeof_cur_ip;
819         int err, i, num_spec = 0;
820         u64 cur_arg;
821         char fmt_ptype, cur_ip[16], ip_spec[] = "%pXX";
822
823         fmt_end = strnchr(fmt, fmt_size, 0);
824         if (!fmt_end)
825                 return -EINVAL;
826         fmt_size = fmt_end - fmt;
827
828         if (get_buffers && try_get_buffers(&buffers))
829                 return -EBUSY;
830
831         if (data->get_bin_args) {
832                 if (num_args)
833                         tmp_buf = buffers->bin_args;
834                 tmp_buf_end = tmp_buf + MAX_BPRINTF_BIN_ARGS;
835                 data->bin_args = (u32 *)tmp_buf;
836         }
837
838         if (data->get_buf)
839                 data->buf = buffers->buf;
840
841         for (i = 0; i < fmt_size; i++) {
842                 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
843                         err = -EINVAL;
844                         goto out;
845                 }
846
847                 if (fmt[i] != '%')
848                         continue;
849
850                 if (fmt[i + 1] == '%') {
851                         i++;
852                         continue;
853                 }
854
855                 if (num_spec >= num_args) {
856                         err = -EINVAL;
857                         goto out;
858                 }
859
860                 /* The string is zero-terminated so if fmt[i] != 0, we can
861                  * always access fmt[i + 1], in the worst case it will be a 0
862                  */
863                 i++;
864
865                 /* skip optional "[0 +-][num]" width formatting field */
866                 while (fmt[i] == '0' || fmt[i] == '+'  || fmt[i] == '-' ||
867                        fmt[i] == ' ')
868                         i++;
869                 if (fmt[i] >= '1' && fmt[i] <= '9') {
870                         i++;
871                         while (fmt[i] >= '0' && fmt[i] <= '9')
872                                 i++;
873                 }
874
875                 if (fmt[i] == 'p') {
876                         sizeof_cur_arg = sizeof(long);
877
878                         if ((fmt[i + 1] == 'k' || fmt[i + 1] == 'u') &&
879                             fmt[i + 2] == 's') {
880                                 fmt_ptype = fmt[i + 1];
881                                 i += 2;
882                                 goto fmt_str;
883                         }
884
885                         if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
886                             ispunct(fmt[i + 1]) || fmt[i + 1] == 'K' ||
887                             fmt[i + 1] == 'x' || fmt[i + 1] == 's' ||
888                             fmt[i + 1] == 'S') {
889                                 /* just kernel pointers */
890                                 if (tmp_buf)
891                                         cur_arg = raw_args[num_spec];
892                                 i++;
893                                 goto nocopy_fmt;
894                         }
895
896                         if (fmt[i + 1] == 'B') {
897                                 if (tmp_buf)  {
898                                         err = snprintf(tmp_buf,
899                                                        (tmp_buf_end - tmp_buf),
900                                                        "%pB",
901                                                        (void *)(long)raw_args[num_spec]);
902                                         tmp_buf += (err + 1);
903                                 }
904
905                                 i++;
906                                 num_spec++;
907                                 continue;
908                         }
909
910                         /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
911                         if ((fmt[i + 1] != 'i' && fmt[i + 1] != 'I') ||
912                             (fmt[i + 2] != '4' && fmt[i + 2] != '6')) {
913                                 err = -EINVAL;
914                                 goto out;
915                         }
916
917                         i += 2;
918                         if (!tmp_buf)
919                                 goto nocopy_fmt;
920
921                         sizeof_cur_ip = (fmt[i] == '4') ? 4 : 16;
922                         if (tmp_buf_end - tmp_buf < sizeof_cur_ip) {
923                                 err = -ENOSPC;
924                                 goto out;
925                         }
926
927                         unsafe_ptr = (char *)(long)raw_args[num_spec];
928                         err = copy_from_kernel_nofault(cur_ip, unsafe_ptr,
929                                                        sizeof_cur_ip);
930                         if (err < 0)
931                                 memset(cur_ip, 0, sizeof_cur_ip);
932
933                         /* hack: bstr_printf expects IP addresses to be
934                          * pre-formatted as strings, ironically, the easiest way
935                          * to do that is to call snprintf.
936                          */
937                         ip_spec[2] = fmt[i - 1];
938                         ip_spec[3] = fmt[i];
939                         err = snprintf(tmp_buf, tmp_buf_end - tmp_buf,
940                                        ip_spec, &cur_ip);
941
942                         tmp_buf += err + 1;
943                         num_spec++;
944
945                         continue;
946                 } else if (fmt[i] == 's') {
947                         fmt_ptype = fmt[i];
948 fmt_str:
949                         if (fmt[i + 1] != 0 &&
950                             !isspace(fmt[i + 1]) &&
951                             !ispunct(fmt[i + 1])) {
952                                 err = -EINVAL;
953                                 goto out;
954                         }
955
956                         if (!tmp_buf)
957                                 goto nocopy_fmt;
958
959                         if (tmp_buf_end == tmp_buf) {
960                                 err = -ENOSPC;
961                                 goto out;
962                         }
963
964                         unsafe_ptr = (char *)(long)raw_args[num_spec];
965                         err = bpf_trace_copy_string(tmp_buf, unsafe_ptr,
966                                                     fmt_ptype,
967                                                     tmp_buf_end - tmp_buf);
968                         if (err < 0) {
969                                 tmp_buf[0] = '\0';
970                                 err = 1;
971                         }
972
973                         tmp_buf += err;
974                         num_spec++;
975
976                         continue;
977                 } else if (fmt[i] == 'c') {
978                         if (!tmp_buf)
979                                 goto nocopy_fmt;
980
981                         if (tmp_buf_end == tmp_buf) {
982                                 err = -ENOSPC;
983                                 goto out;
984                         }
985
986                         *tmp_buf = raw_args[num_spec];
987                         tmp_buf++;
988                         num_spec++;
989
990                         continue;
991                 }
992
993                 sizeof_cur_arg = sizeof(int);
994
995                 if (fmt[i] == 'l') {
996                         sizeof_cur_arg = sizeof(long);
997                         i++;
998                 }
999                 if (fmt[i] == 'l') {
1000                         sizeof_cur_arg = sizeof(long long);
1001                         i++;
1002                 }
1003
1004                 if (fmt[i] != 'i' && fmt[i] != 'd' && fmt[i] != 'u' &&
1005                     fmt[i] != 'x' && fmt[i] != 'X') {
1006                         err = -EINVAL;
1007                         goto out;
1008                 }
1009
1010                 if (tmp_buf)
1011                         cur_arg = raw_args[num_spec];
1012 nocopy_fmt:
1013                 if (tmp_buf) {
1014                         tmp_buf = PTR_ALIGN(tmp_buf, sizeof(u32));
1015                         if (tmp_buf_end - tmp_buf < sizeof_cur_arg) {
1016                                 err = -ENOSPC;
1017                                 goto out;
1018                         }
1019
1020                         if (sizeof_cur_arg == 8) {
1021                                 *(u32 *)tmp_buf = *(u32 *)&cur_arg;
1022                                 *(u32 *)(tmp_buf + 4) = *((u32 *)&cur_arg + 1);
1023                         } else {
1024                                 *(u32 *)tmp_buf = (u32)(long)cur_arg;
1025                         }
1026                         tmp_buf += sizeof_cur_arg;
1027                 }
1028                 num_spec++;
1029         }
1030
1031         err = 0;
1032 out:
1033         if (err)
1034                 bpf_bprintf_cleanup(data);
1035         return err;
1036 }
1037
1038 BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt,
1039            const void *, args, u32, data_len)
1040 {
1041         struct bpf_bprintf_data data = {
1042                 .get_bin_args   = true,
1043         };
1044         int err, num_args;
1045
1046         if (data_len % 8 || data_len > MAX_BPRINTF_VARARGS * 8 ||
1047             (data_len && !args))
1048                 return -EINVAL;
1049         num_args = data_len / 8;
1050
1051         /* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we
1052          * can safely give an unbounded size.
1053          */
1054         err = bpf_bprintf_prepare(fmt, UINT_MAX, args, num_args, &data);
1055         if (err < 0)
1056                 return err;
1057
1058         err = bstr_printf(str, str_size, fmt, data.bin_args);
1059
1060         bpf_bprintf_cleanup(&data);
1061
1062         return err + 1;
1063 }
1064
1065 const struct bpf_func_proto bpf_snprintf_proto = {
1066         .func           = bpf_snprintf,
1067         .gpl_only       = true,
1068         .ret_type       = RET_INTEGER,
1069         .arg1_type      = ARG_PTR_TO_MEM_OR_NULL,
1070         .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
1071         .arg3_type      = ARG_PTR_TO_CONST_STR,
1072         .arg4_type      = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
1073         .arg5_type      = ARG_CONST_SIZE_OR_ZERO,
1074 };
1075
1076 /* BPF map elements can contain 'struct bpf_timer'.
1077  * Such map owns all of its BPF timers.
1078  * 'struct bpf_timer' is allocated as part of map element allocation
1079  * and it's zero initialized.
1080  * That space is used to keep 'struct bpf_timer_kern'.
1081  * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and
1082  * remembers 'struct bpf_map *' pointer it's part of.
1083  * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn.
1084  * bpf_timer_start() arms the timer.
1085  * If user space reference to a map goes to zero at this point
1086  * ops->map_release_uref callback is responsible for cancelling the timers,
1087  * freeing their memory, and decrementing prog's refcnts.
1088  * bpf_timer_cancel() cancels the timer and decrements prog's refcnt.
1089  * Inner maps can contain bpf timers as well. ops->map_release_uref is
1090  * freeing the timers when inner map is replaced or deleted by user space.
1091  */
1092 struct bpf_hrtimer {
1093         struct hrtimer timer;
1094         struct bpf_map *map;
1095         struct bpf_prog *prog;
1096         void __rcu *callback_fn;
1097         void *value;
1098 };
1099
1100 /* the actual struct hidden inside uapi struct bpf_timer */
1101 struct bpf_timer_kern {
1102         struct bpf_hrtimer *timer;
1103         /* bpf_spin_lock is used here instead of spinlock_t to make
1104          * sure that it always fits into space reserved by struct bpf_timer
1105          * regardless of LOCKDEP and spinlock debug flags.
1106          */
1107         struct bpf_spin_lock lock;
1108 } __attribute__((aligned(8)));
1109
1110 static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
1111
1112 static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
1113 {
1114         struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
1115         struct bpf_map *map = t->map;
1116         void *value = t->value;
1117         bpf_callback_t callback_fn;
1118         void *key;
1119         u32 idx;
1120
1121         BTF_TYPE_EMIT(struct bpf_timer);
1122         callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held());
1123         if (!callback_fn)
1124                 goto out;
1125
1126         /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
1127          * cannot be preempted by another bpf_timer_cb() on the same cpu.
1128          * Remember the timer this callback is servicing to prevent
1129          * deadlock if callback_fn() calls bpf_timer_cancel() or
1130          * bpf_map_delete_elem() on the same timer.
1131          */
1132         this_cpu_write(hrtimer_running, t);
1133         if (map->map_type == BPF_MAP_TYPE_ARRAY) {
1134                 struct bpf_array *array = container_of(map, struct bpf_array, map);
1135
1136                 /* compute the key */
1137                 idx = ((char *)value - array->value) / array->elem_size;
1138                 key = &idx;
1139         } else { /* hash or lru */
1140                 key = value - round_up(map->key_size, 8);
1141         }
1142
1143         callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
1144         /* The verifier checked that return value is zero. */
1145
1146         this_cpu_write(hrtimer_running, NULL);
1147 out:
1148         return HRTIMER_NORESTART;
1149 }
1150
1151 BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map *, map,
1152            u64, flags)
1153 {
1154         clockid_t clockid = flags & (MAX_CLOCKS - 1);
1155         struct bpf_hrtimer *t;
1156         int ret = 0;
1157
1158         BUILD_BUG_ON(MAX_CLOCKS != 16);
1159         BUILD_BUG_ON(sizeof(struct bpf_timer_kern) > sizeof(struct bpf_timer));
1160         BUILD_BUG_ON(__alignof__(struct bpf_timer_kern) != __alignof__(struct bpf_timer));
1161
1162         if (in_nmi())
1163                 return -EOPNOTSUPP;
1164
1165         if (flags >= MAX_CLOCKS ||
1166             /* similar to timerfd except _ALARM variants are not supported */
1167             (clockid != CLOCK_MONOTONIC &&
1168              clockid != CLOCK_REALTIME &&
1169              clockid != CLOCK_BOOTTIME))
1170                 return -EINVAL;
1171         __bpf_spin_lock_irqsave(&timer->lock);
1172         t = timer->timer;
1173         if (t) {
1174                 ret = -EBUSY;
1175                 goto out;
1176         }
1177         if (!atomic64_read(&map->usercnt)) {
1178                 /* maps with timers must be either held by user space
1179                  * or pinned in bpffs.
1180                  */
1181                 ret = -EPERM;
1182                 goto out;
1183         }
1184         /* allocate hrtimer via map_kmalloc to use memcg accounting */
1185         t = bpf_map_kmalloc_node(map, sizeof(*t), GFP_ATOMIC, map->numa_node);
1186         if (!t) {
1187                 ret = -ENOMEM;
1188                 goto out;
1189         }
1190         t->value = (void *)timer - map->record->timer_off;
1191         t->map = map;
1192         t->prog = NULL;
1193         rcu_assign_pointer(t->callback_fn, NULL);
1194         hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT);
1195         t->timer.function = bpf_timer_cb;
1196         timer->timer = t;
1197 out:
1198         __bpf_spin_unlock_irqrestore(&timer->lock);
1199         return ret;
1200 }
1201
1202 static const struct bpf_func_proto bpf_timer_init_proto = {
1203         .func           = bpf_timer_init,
1204         .gpl_only       = true,
1205         .ret_type       = RET_INTEGER,
1206         .arg1_type      = ARG_PTR_TO_TIMER,
1207         .arg2_type      = ARG_CONST_MAP_PTR,
1208         .arg3_type      = ARG_ANYTHING,
1209 };
1210
1211 BPF_CALL_3(bpf_timer_set_callback, struct bpf_timer_kern *, timer, void *, callback_fn,
1212            struct bpf_prog_aux *, aux)
1213 {
1214         struct bpf_prog *prev, *prog = aux->prog;
1215         struct bpf_hrtimer *t;
1216         int ret = 0;
1217
1218         if (in_nmi())
1219                 return -EOPNOTSUPP;
1220         __bpf_spin_lock_irqsave(&timer->lock);
1221         t = timer->timer;
1222         if (!t) {
1223                 ret = -EINVAL;
1224                 goto out;
1225         }
1226         if (!atomic64_read(&t->map->usercnt)) {
1227                 /* maps with timers must be either held by user space
1228                  * or pinned in bpffs. Otherwise timer might still be
1229                  * running even when bpf prog is detached and user space
1230                  * is gone, since map_release_uref won't ever be called.
1231                  */
1232                 ret = -EPERM;
1233                 goto out;
1234         }
1235         prev = t->prog;
1236         if (prev != prog) {
1237                 /* Bump prog refcnt once. Every bpf_timer_set_callback()
1238                  * can pick different callback_fn-s within the same prog.
1239                  */
1240                 prog = bpf_prog_inc_not_zero(prog);
1241                 if (IS_ERR(prog)) {
1242                         ret = PTR_ERR(prog);
1243                         goto out;
1244                 }
1245                 if (prev)
1246                         /* Drop prev prog refcnt when swapping with new prog */
1247                         bpf_prog_put(prev);
1248                 t->prog = prog;
1249         }
1250         rcu_assign_pointer(t->callback_fn, callback_fn);
1251 out:
1252         __bpf_spin_unlock_irqrestore(&timer->lock);
1253         return ret;
1254 }
1255
1256 static const struct bpf_func_proto bpf_timer_set_callback_proto = {
1257         .func           = bpf_timer_set_callback,
1258         .gpl_only       = true,
1259         .ret_type       = RET_INTEGER,
1260         .arg1_type      = ARG_PTR_TO_TIMER,
1261         .arg2_type      = ARG_PTR_TO_FUNC,
1262 };
1263
1264 BPF_CALL_3(bpf_timer_start, struct bpf_timer_kern *, timer, u64, nsecs, u64, flags)
1265 {
1266         struct bpf_hrtimer *t;
1267         int ret = 0;
1268         enum hrtimer_mode mode;
1269
1270         if (in_nmi())
1271                 return -EOPNOTSUPP;
1272         if (flags > BPF_F_TIMER_ABS)
1273                 return -EINVAL;
1274         __bpf_spin_lock_irqsave(&timer->lock);
1275         t = timer->timer;
1276         if (!t || !t->prog) {
1277                 ret = -EINVAL;
1278                 goto out;
1279         }
1280
1281         if (flags & BPF_F_TIMER_ABS)
1282                 mode = HRTIMER_MODE_ABS_SOFT;
1283         else
1284                 mode = HRTIMER_MODE_REL_SOFT;
1285
1286         hrtimer_start(&t->timer, ns_to_ktime(nsecs), mode);
1287 out:
1288         __bpf_spin_unlock_irqrestore(&timer->lock);
1289         return ret;
1290 }
1291
1292 static const struct bpf_func_proto bpf_timer_start_proto = {
1293         .func           = bpf_timer_start,
1294         .gpl_only       = true,
1295         .ret_type       = RET_INTEGER,
1296         .arg1_type      = ARG_PTR_TO_TIMER,
1297         .arg2_type      = ARG_ANYTHING,
1298         .arg3_type      = ARG_ANYTHING,
1299 };
1300
1301 static void drop_prog_refcnt(struct bpf_hrtimer *t)
1302 {
1303         struct bpf_prog *prog = t->prog;
1304
1305         if (prog) {
1306                 bpf_prog_put(prog);
1307                 t->prog = NULL;
1308                 rcu_assign_pointer(t->callback_fn, NULL);
1309         }
1310 }
1311
1312 BPF_CALL_1(bpf_timer_cancel, struct bpf_timer_kern *, timer)
1313 {
1314         struct bpf_hrtimer *t;
1315         int ret = 0;
1316
1317         if (in_nmi())
1318                 return -EOPNOTSUPP;
1319         __bpf_spin_lock_irqsave(&timer->lock);
1320         t = timer->timer;
1321         if (!t) {
1322                 ret = -EINVAL;
1323                 goto out;
1324         }
1325         if (this_cpu_read(hrtimer_running) == t) {
1326                 /* If bpf callback_fn is trying to bpf_timer_cancel()
1327                  * its own timer the hrtimer_cancel() will deadlock
1328                  * since it waits for callback_fn to finish
1329                  */
1330                 ret = -EDEADLK;
1331                 goto out;
1332         }
1333         drop_prog_refcnt(t);
1334 out:
1335         __bpf_spin_unlock_irqrestore(&timer->lock);
1336         /* Cancel the timer and wait for associated callback to finish
1337          * if it was running.
1338          */
1339         ret = ret ?: hrtimer_cancel(&t->timer);
1340         return ret;
1341 }
1342
1343 static const struct bpf_func_proto bpf_timer_cancel_proto = {
1344         .func           = bpf_timer_cancel,
1345         .gpl_only       = true,
1346         .ret_type       = RET_INTEGER,
1347         .arg1_type      = ARG_PTR_TO_TIMER,
1348 };
1349
1350 /* This function is called by map_delete/update_elem for individual element and
1351  * by ops->map_release_uref when the user space reference to a map reaches zero.
1352  */
1353 void bpf_timer_cancel_and_free(void *val)
1354 {
1355         struct bpf_timer_kern *timer = val;
1356         struct bpf_hrtimer *t;
1357
1358         /* Performance optimization: read timer->timer without lock first. */
1359         if (!READ_ONCE(timer->timer))
1360                 return;
1361
1362         __bpf_spin_lock_irqsave(&timer->lock);
1363         /* re-read it under lock */
1364         t = timer->timer;
1365         if (!t)
1366                 goto out;
1367         drop_prog_refcnt(t);
1368         /* The subsequent bpf_timer_start/cancel() helpers won't be able to use
1369          * this timer, since it won't be initialized.
1370          */
1371         timer->timer = NULL;
1372 out:
1373         __bpf_spin_unlock_irqrestore(&timer->lock);
1374         if (!t)
1375                 return;
1376         /* Cancel the timer and wait for callback to complete if it was running.
1377          * If hrtimer_cancel() can be safely called it's safe to call kfree(t)
1378          * right after for both preallocated and non-preallocated maps.
1379          * The timer->timer = NULL was already done and no code path can
1380          * see address 't' anymore.
1381          *
1382          * Check that bpf_map_delete/update_elem() wasn't called from timer
1383          * callback_fn. In such case don't call hrtimer_cancel() (since it will
1384          * deadlock) and don't call hrtimer_try_to_cancel() (since it will just
1385          * return -1). Though callback_fn is still running on this cpu it's
1386          * safe to do kfree(t) because bpf_timer_cb() read everything it needed
1387          * from 't'. The bpf subprog callback_fn won't be able to access 't',
1388          * since timer->timer = NULL was already done. The timer will be
1389          * effectively cancelled because bpf_timer_cb() will return
1390          * HRTIMER_NORESTART.
1391          */
1392         if (this_cpu_read(hrtimer_running) != t)
1393                 hrtimer_cancel(&t->timer);
1394         kfree(t);
1395 }
1396
1397 BPF_CALL_2(bpf_kptr_xchg, void *, map_value, void *, ptr)
1398 {
1399         unsigned long *kptr = map_value;
1400
1401         return xchg(kptr, (unsigned long)ptr);
1402 }
1403
1404 /* Unlike other PTR_TO_BTF_ID helpers the btf_id in bpf_kptr_xchg()
1405  * helper is determined dynamically by the verifier. Use BPF_PTR_POISON to
1406  * denote type that verifier will determine.
1407  */
1408 static const struct bpf_func_proto bpf_kptr_xchg_proto = {
1409         .func         = bpf_kptr_xchg,
1410         .gpl_only     = false,
1411         .ret_type     = RET_PTR_TO_BTF_ID_OR_NULL,
1412         .ret_btf_id   = BPF_PTR_POISON,
1413         .arg1_type    = ARG_PTR_TO_KPTR,
1414         .arg2_type    = ARG_PTR_TO_BTF_ID_OR_NULL | OBJ_RELEASE,
1415         .arg2_btf_id  = BPF_PTR_POISON,
1416 };
1417
1418 /* Since the upper 8 bits of dynptr->size is reserved, the
1419  * maximum supported size is 2^24 - 1.
1420  */
1421 #define DYNPTR_MAX_SIZE ((1UL << 24) - 1)
1422 #define DYNPTR_TYPE_SHIFT       28
1423 #define DYNPTR_SIZE_MASK        0xFFFFFF
1424 #define DYNPTR_RDONLY_BIT       BIT(31)
1425
1426 static bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
1427 {
1428         return ptr->size & DYNPTR_RDONLY_BIT;
1429 }
1430
1431 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
1432 {
1433         ptr->size |= DYNPTR_RDONLY_BIT;
1434 }
1435
1436 static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_type type)
1437 {
1438         ptr->size |= type << DYNPTR_TYPE_SHIFT;
1439 }
1440
1441 static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *ptr)
1442 {
1443         return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT;
1444 }
1445
1446 u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
1447 {
1448         return ptr->size & DYNPTR_SIZE_MASK;
1449 }
1450
1451 static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
1452 {
1453         u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
1454
1455         ptr->size = new_size | metadata;
1456 }
1457
1458 int bpf_dynptr_check_size(u32 size)
1459 {
1460         return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
1461 }
1462
1463 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
1464                      enum bpf_dynptr_type type, u32 offset, u32 size)
1465 {
1466         ptr->data = data;
1467         ptr->offset = offset;
1468         ptr->size = size;
1469         bpf_dynptr_set_type(ptr, type);
1470 }
1471
1472 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
1473 {
1474         memset(ptr, 0, sizeof(*ptr));
1475 }
1476
1477 static int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
1478 {
1479         u32 size = __bpf_dynptr_size(ptr);
1480
1481         if (len > size || offset > size - len)
1482                 return -E2BIG;
1483
1484         return 0;
1485 }
1486
1487 BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u32, size, u64, flags, struct bpf_dynptr_kern *, ptr)
1488 {
1489         int err;
1490
1491         BTF_TYPE_EMIT(struct bpf_dynptr);
1492
1493         err = bpf_dynptr_check_size(size);
1494         if (err)
1495                 goto error;
1496
1497         /* flags is currently unsupported */
1498         if (flags) {
1499                 err = -EINVAL;
1500                 goto error;
1501         }
1502
1503         bpf_dynptr_init(ptr, data, BPF_DYNPTR_TYPE_LOCAL, 0, size);
1504
1505         return 0;
1506
1507 error:
1508         bpf_dynptr_set_null(ptr);
1509         return err;
1510 }
1511
1512 static const struct bpf_func_proto bpf_dynptr_from_mem_proto = {
1513         .func           = bpf_dynptr_from_mem,
1514         .gpl_only       = false,
1515         .ret_type       = RET_INTEGER,
1516         .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
1517         .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
1518         .arg3_type      = ARG_ANYTHING,
1519         .arg4_type      = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT,
1520 };
1521
1522 BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, const struct bpf_dynptr_kern *, src,
1523            u32, offset, u64, flags)
1524 {
1525         enum bpf_dynptr_type type;
1526         int err;
1527
1528         if (!src->data || flags)
1529                 return -EINVAL;
1530
1531         err = bpf_dynptr_check_off_len(src, offset, len);
1532         if (err)
1533                 return err;
1534
1535         type = bpf_dynptr_get_type(src);
1536
1537         switch (type) {
1538         case BPF_DYNPTR_TYPE_LOCAL:
1539         case BPF_DYNPTR_TYPE_RINGBUF:
1540                 /* Source and destination may possibly overlap, hence use memmove to
1541                  * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr
1542                  * pointing to overlapping PTR_TO_MAP_VALUE regions.
1543                  */
1544                 memmove(dst, src->data + src->offset + offset, len);
1545                 return 0;
1546         case BPF_DYNPTR_TYPE_SKB:
1547                 return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
1548         case BPF_DYNPTR_TYPE_XDP:
1549                 return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len);
1550         default:
1551                 WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
1552                 return -EFAULT;
1553         }
1554 }
1555
1556 static const struct bpf_func_proto bpf_dynptr_read_proto = {
1557         .func           = bpf_dynptr_read,
1558         .gpl_only       = false,
1559         .ret_type       = RET_INTEGER,
1560         .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
1561         .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
1562         .arg3_type      = ARG_PTR_TO_DYNPTR | MEM_RDONLY,
1563         .arg4_type      = ARG_ANYTHING,
1564         .arg5_type      = ARG_ANYTHING,
1565 };
1566
1567 BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, void *, src,
1568            u32, len, u64, flags)
1569 {
1570         enum bpf_dynptr_type type;
1571         int err;
1572
1573         if (!dst->data || __bpf_dynptr_is_rdonly(dst))
1574                 return -EINVAL;
1575
1576         err = bpf_dynptr_check_off_len(dst, offset, len);
1577         if (err)
1578                 return err;
1579
1580         type = bpf_dynptr_get_type(dst);
1581
1582         switch (type) {
1583         case BPF_DYNPTR_TYPE_LOCAL:
1584         case BPF_DYNPTR_TYPE_RINGBUF:
1585                 if (flags)
1586                         return -EINVAL;
1587                 /* Source and destination may possibly overlap, hence use memmove to
1588                  * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr
1589                  * pointing to overlapping PTR_TO_MAP_VALUE regions.
1590                  */
1591                 memmove(dst->data + dst->offset + offset, src, len);
1592                 return 0;
1593         case BPF_DYNPTR_TYPE_SKB:
1594                 return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,
1595                                              flags);
1596         case BPF_DYNPTR_TYPE_XDP:
1597                 if (flags)
1598                         return -EINVAL;
1599                 return __bpf_xdp_store_bytes(dst->data, dst->offset + offset, src, len);
1600         default:
1601                 WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
1602                 return -EFAULT;
1603         }
1604 }
1605
1606 static const struct bpf_func_proto bpf_dynptr_write_proto = {
1607         .func           = bpf_dynptr_write,
1608         .gpl_only       = false,
1609         .ret_type       = RET_INTEGER,
1610         .arg1_type      = ARG_PTR_TO_DYNPTR | MEM_RDONLY,
1611         .arg2_type      = ARG_ANYTHING,
1612         .arg3_type      = ARG_PTR_TO_MEM | MEM_RDONLY,
1613         .arg4_type      = ARG_CONST_SIZE_OR_ZERO,
1614         .arg5_type      = ARG_ANYTHING,
1615 };
1616
1617 BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u32, len)
1618 {
1619         enum bpf_dynptr_type type;
1620         int err;
1621
1622         if (!ptr->data)
1623                 return 0;
1624
1625         err = bpf_dynptr_check_off_len(ptr, offset, len);
1626         if (err)
1627                 return 0;
1628
1629         if (__bpf_dynptr_is_rdonly(ptr))
1630                 return 0;
1631
1632         type = bpf_dynptr_get_type(ptr);
1633
1634         switch (type) {
1635         case BPF_DYNPTR_TYPE_LOCAL:
1636         case BPF_DYNPTR_TYPE_RINGBUF:
1637                 return (unsigned long)(ptr->data + ptr->offset + offset);
1638         case BPF_DYNPTR_TYPE_SKB:
1639         case BPF_DYNPTR_TYPE_XDP:
1640                 /* skb and xdp dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */
1641                 return 0;
1642         default:
1643                 WARN_ONCE(true, "bpf_dynptr_data: unknown dynptr type %d\n", type);
1644                 return 0;
1645         }
1646 }
1647
1648 static const struct bpf_func_proto bpf_dynptr_data_proto = {
1649         .func           = bpf_dynptr_data,
1650         .gpl_only       = false,
1651         .ret_type       = RET_PTR_TO_DYNPTR_MEM_OR_NULL,
1652         .arg1_type      = ARG_PTR_TO_DYNPTR | MEM_RDONLY,
1653         .arg2_type      = ARG_ANYTHING,
1654         .arg3_type      = ARG_CONST_ALLOC_SIZE_OR_ZERO,
1655 };
1656
1657 const struct bpf_func_proto bpf_get_current_task_proto __weak;
1658 const struct bpf_func_proto bpf_get_current_task_btf_proto __weak;
1659 const struct bpf_func_proto bpf_probe_read_user_proto __weak;
1660 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak;
1661 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak;
1662 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
1663 const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1664
1665 const struct bpf_func_proto *
1666 bpf_base_func_proto(enum bpf_func_id func_id)
1667 {
1668         switch (func_id) {
1669         case BPF_FUNC_map_lookup_elem:
1670                 return &bpf_map_lookup_elem_proto;
1671         case BPF_FUNC_map_update_elem:
1672                 return &bpf_map_update_elem_proto;
1673         case BPF_FUNC_map_delete_elem:
1674                 return &bpf_map_delete_elem_proto;
1675         case BPF_FUNC_map_push_elem:
1676                 return &bpf_map_push_elem_proto;
1677         case BPF_FUNC_map_pop_elem:
1678                 return &bpf_map_pop_elem_proto;
1679         case BPF_FUNC_map_peek_elem:
1680                 return &bpf_map_peek_elem_proto;
1681         case BPF_FUNC_map_lookup_percpu_elem:
1682                 return &bpf_map_lookup_percpu_elem_proto;
1683         case BPF_FUNC_get_prandom_u32:
1684                 return &bpf_get_prandom_u32_proto;
1685         case BPF_FUNC_get_smp_processor_id:
1686                 return &bpf_get_raw_smp_processor_id_proto;
1687         case BPF_FUNC_get_numa_node_id:
1688                 return &bpf_get_numa_node_id_proto;
1689         case BPF_FUNC_tail_call:
1690                 return &bpf_tail_call_proto;
1691         case BPF_FUNC_ktime_get_ns:
1692                 return &bpf_ktime_get_ns_proto;
1693         case BPF_FUNC_ktime_get_boot_ns:
1694                 return &bpf_ktime_get_boot_ns_proto;
1695         case BPF_FUNC_ktime_get_tai_ns:
1696                 return &bpf_ktime_get_tai_ns_proto;
1697         case BPF_FUNC_ringbuf_output:
1698                 return &bpf_ringbuf_output_proto;
1699         case BPF_FUNC_ringbuf_reserve:
1700                 return &bpf_ringbuf_reserve_proto;
1701         case BPF_FUNC_ringbuf_submit:
1702                 return &bpf_ringbuf_submit_proto;
1703         case BPF_FUNC_ringbuf_discard:
1704                 return &bpf_ringbuf_discard_proto;
1705         case BPF_FUNC_ringbuf_query:
1706                 return &bpf_ringbuf_query_proto;
1707         case BPF_FUNC_strncmp:
1708                 return &bpf_strncmp_proto;
1709         case BPF_FUNC_strtol:
1710                 return &bpf_strtol_proto;
1711         case BPF_FUNC_strtoul:
1712                 return &bpf_strtoul_proto;
1713         default:
1714                 break;
1715         }
1716
1717         if (!bpf_capable())
1718                 return NULL;
1719
1720         switch (func_id) {
1721         case BPF_FUNC_spin_lock:
1722                 return &bpf_spin_lock_proto;
1723         case BPF_FUNC_spin_unlock:
1724                 return &bpf_spin_unlock_proto;
1725         case BPF_FUNC_jiffies64:
1726                 return &bpf_jiffies64_proto;
1727         case BPF_FUNC_per_cpu_ptr:
1728                 return &bpf_per_cpu_ptr_proto;
1729         case BPF_FUNC_this_cpu_ptr:
1730                 return &bpf_this_cpu_ptr_proto;
1731         case BPF_FUNC_timer_init:
1732                 return &bpf_timer_init_proto;
1733         case BPF_FUNC_timer_set_callback:
1734                 return &bpf_timer_set_callback_proto;
1735         case BPF_FUNC_timer_start:
1736                 return &bpf_timer_start_proto;
1737         case BPF_FUNC_timer_cancel:
1738                 return &bpf_timer_cancel_proto;
1739         case BPF_FUNC_kptr_xchg:
1740                 return &bpf_kptr_xchg_proto;
1741         case BPF_FUNC_for_each_map_elem:
1742                 return &bpf_for_each_map_elem_proto;
1743         case BPF_FUNC_loop:
1744                 return &bpf_loop_proto;
1745         case BPF_FUNC_user_ringbuf_drain:
1746                 return &bpf_user_ringbuf_drain_proto;
1747         case BPF_FUNC_ringbuf_reserve_dynptr:
1748                 return &bpf_ringbuf_reserve_dynptr_proto;
1749         case BPF_FUNC_ringbuf_submit_dynptr:
1750                 return &bpf_ringbuf_submit_dynptr_proto;
1751         case BPF_FUNC_ringbuf_discard_dynptr:
1752                 return &bpf_ringbuf_discard_dynptr_proto;
1753         case BPF_FUNC_dynptr_from_mem:
1754                 return &bpf_dynptr_from_mem_proto;
1755         case BPF_FUNC_dynptr_read:
1756                 return &bpf_dynptr_read_proto;
1757         case BPF_FUNC_dynptr_write:
1758                 return &bpf_dynptr_write_proto;
1759         case BPF_FUNC_dynptr_data:
1760                 return &bpf_dynptr_data_proto;
1761 #ifdef CONFIG_CGROUPS
1762         case BPF_FUNC_cgrp_storage_get:
1763                 return &bpf_cgrp_storage_get_proto;
1764         case BPF_FUNC_cgrp_storage_delete:
1765                 return &bpf_cgrp_storage_delete_proto;
1766         case BPF_FUNC_get_current_cgroup_id:
1767                 return &bpf_get_current_cgroup_id_proto;
1768         case BPF_FUNC_get_current_ancestor_cgroup_id:
1769                 return &bpf_get_current_ancestor_cgroup_id_proto;
1770 #endif
1771         default:
1772                 break;
1773         }
1774
1775         if (!perfmon_capable())
1776                 return NULL;
1777
1778         switch (func_id) {
1779         case BPF_FUNC_trace_printk:
1780                 return bpf_get_trace_printk_proto();
1781         case BPF_FUNC_get_current_task:
1782                 return &bpf_get_current_task_proto;
1783         case BPF_FUNC_get_current_task_btf:
1784                 return &bpf_get_current_task_btf_proto;
1785         case BPF_FUNC_probe_read_user:
1786                 return &bpf_probe_read_user_proto;
1787         case BPF_FUNC_probe_read_kernel:
1788                 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1789                        NULL : &bpf_probe_read_kernel_proto;
1790         case BPF_FUNC_probe_read_user_str:
1791                 return &bpf_probe_read_user_str_proto;
1792         case BPF_FUNC_probe_read_kernel_str:
1793                 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1794                        NULL : &bpf_probe_read_kernel_str_proto;
1795         case BPF_FUNC_snprintf_btf:
1796                 return &bpf_snprintf_btf_proto;
1797         case BPF_FUNC_snprintf:
1798                 return &bpf_snprintf_proto;
1799         case BPF_FUNC_task_pt_regs:
1800                 return &bpf_task_pt_regs_proto;
1801         case BPF_FUNC_trace_vprintk:
1802                 return bpf_get_trace_vprintk_proto();
1803         default:
1804                 return NULL;
1805         }
1806 }
1807
1808 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec);
1809
1810 void bpf_list_head_free(const struct btf_field *field, void *list_head,
1811                         struct bpf_spin_lock *spin_lock)
1812 {
1813         struct list_head *head = list_head, *orig_head = list_head;
1814
1815         BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head));
1816         BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head));
1817
1818         /* Do the actual list draining outside the lock to not hold the lock for
1819          * too long, and also prevent deadlocks if tracing programs end up
1820          * executing on entry/exit of functions called inside the critical
1821          * section, and end up doing map ops that call bpf_list_head_free for
1822          * the same map value again.
1823          */
1824         __bpf_spin_lock_irqsave(spin_lock);
1825         if (!head->next || list_empty(head))
1826                 goto unlock;
1827         head = head->next;
1828 unlock:
1829         INIT_LIST_HEAD(orig_head);
1830         __bpf_spin_unlock_irqrestore(spin_lock);
1831
1832         while (head != orig_head) {
1833                 void *obj = head;
1834
1835                 obj -= field->graph_root.node_offset;
1836                 head = head->next;
1837                 /* The contained type can also have resources, including a
1838                  * bpf_list_head which needs to be freed.
1839                  */
1840                 migrate_disable();
1841                 __bpf_obj_drop_impl(obj, field->graph_root.value_rec);
1842                 migrate_enable();
1843         }
1844 }
1845
1846 /* Like rbtree_postorder_for_each_entry_safe, but 'pos' and 'n' are
1847  * 'rb_node *', so field name of rb_node within containing struct is not
1848  * needed.
1849  *
1850  * Since bpf_rb_tree's node type has a corresponding struct btf_field with
1851  * graph_root.node_offset, it's not necessary to know field name
1852  * or type of node struct
1853  */
1854 #define bpf_rbtree_postorder_for_each_entry_safe(pos, n, root) \
1855         for (pos = rb_first_postorder(root); \
1856             pos && ({ n = rb_next_postorder(pos); 1; }); \
1857             pos = n)
1858
1859 void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
1860                       struct bpf_spin_lock *spin_lock)
1861 {
1862         struct rb_root_cached orig_root, *root = rb_root;
1863         struct rb_node *pos, *n;
1864         void *obj;
1865
1866         BUILD_BUG_ON(sizeof(struct rb_root_cached) > sizeof(struct bpf_rb_root));
1867         BUILD_BUG_ON(__alignof__(struct rb_root_cached) > __alignof__(struct bpf_rb_root));
1868
1869         __bpf_spin_lock_irqsave(spin_lock);
1870         orig_root = *root;
1871         *root = RB_ROOT_CACHED;
1872         __bpf_spin_unlock_irqrestore(spin_lock);
1873
1874         bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) {
1875                 obj = pos;
1876                 obj -= field->graph_root.node_offset;
1877
1878
1879                 migrate_disable();
1880                 __bpf_obj_drop_impl(obj, field->graph_root.value_rec);
1881                 migrate_enable();
1882         }
1883 }
1884
1885 __diag_push();
1886 __diag_ignore_all("-Wmissing-prototypes",
1887                   "Global functions as their definitions will be in vmlinux BTF");
1888
1889 __bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign)
1890 {
1891         struct btf_struct_meta *meta = meta__ign;
1892         u64 size = local_type_id__k;
1893         void *p;
1894
1895         p = bpf_mem_alloc(&bpf_global_ma, size);
1896         if (!p)
1897                 return NULL;
1898         if (meta)
1899                 bpf_obj_init(meta->record, p);
1900         return p;
1901 }
1902
1903 /* Must be called under migrate_disable(), as required by bpf_mem_free */
1904 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec)
1905 {
1906         if (rec && rec->refcount_off >= 0 &&
1907             !refcount_dec_and_test((refcount_t *)(p + rec->refcount_off))) {
1908                 /* Object is refcounted and refcount_dec didn't result in 0
1909                  * refcount. Return without freeing the object
1910                  */
1911                 return;
1912         }
1913
1914         if (rec)
1915                 bpf_obj_free_fields(rec, p);
1916
1917         if (rec && rec->refcount_off >= 0)
1918                 bpf_mem_free_rcu(&bpf_global_ma, p);
1919         else
1920                 bpf_mem_free(&bpf_global_ma, p);
1921 }
1922
1923 __bpf_kfunc void bpf_obj_drop_impl(void *p__alloc, void *meta__ign)
1924 {
1925         struct btf_struct_meta *meta = meta__ign;
1926         void *p = p__alloc;
1927
1928         __bpf_obj_drop_impl(p, meta ? meta->record : NULL);
1929 }
1930
1931 __bpf_kfunc void *bpf_refcount_acquire_impl(void *p__refcounted_kptr, void *meta__ign)
1932 {
1933         struct btf_struct_meta *meta = meta__ign;
1934         struct bpf_refcount *ref;
1935
1936         /* Could just cast directly to refcount_t *, but need some code using
1937          * bpf_refcount type so that it is emitted in vmlinux BTF
1938          */
1939         ref = (struct bpf_refcount *)(p__refcounted_kptr + meta->record->refcount_off);
1940         if (!refcount_inc_not_zero((refcount_t *)ref))
1941                 return NULL;
1942
1943         /* Verifier strips KF_RET_NULL if input is owned ref, see is_kfunc_ret_null
1944          * in verifier.c
1945          */
1946         return (void *)p__refcounted_kptr;
1947 }
1948
1949 static int __bpf_list_add(struct bpf_list_node_kern *node,
1950                           struct bpf_list_head *head,
1951                           bool tail, struct btf_record *rec, u64 off)
1952 {
1953         struct list_head *n = &node->list_head, *h = (void *)head;
1954
1955         /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't
1956          * called on its fields, so init here
1957          */
1958         if (unlikely(!h->next))
1959                 INIT_LIST_HEAD(h);
1960
1961         /* node->owner != NULL implies !list_empty(n), no need to separately
1962          * check the latter
1963          */
1964         if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) {
1965                 /* Only called from BPF prog, no need to migrate_disable */
1966                 __bpf_obj_drop_impl((void *)n - off, rec);
1967                 return -EINVAL;
1968         }
1969
1970         tail ? list_add_tail(n, h) : list_add(n, h);
1971         WRITE_ONCE(node->owner, head);
1972
1973         return 0;
1974 }
1975
1976 __bpf_kfunc int bpf_list_push_front_impl(struct bpf_list_head *head,
1977                                          struct bpf_list_node *node,
1978                                          void *meta__ign, u64 off)
1979 {
1980         struct bpf_list_node_kern *n = (void *)node;
1981         struct btf_struct_meta *meta = meta__ign;
1982
1983         return __bpf_list_add(n, head, false, meta ? meta->record : NULL, off);
1984 }
1985
1986 __bpf_kfunc int bpf_list_push_back_impl(struct bpf_list_head *head,
1987                                         struct bpf_list_node *node,
1988                                         void *meta__ign, u64 off)
1989 {
1990         struct bpf_list_node_kern *n = (void *)node;
1991         struct btf_struct_meta *meta = meta__ign;
1992
1993         return __bpf_list_add(n, head, true, meta ? meta->record : NULL, off);
1994 }
1995
1996 static struct bpf_list_node *__bpf_list_del(struct bpf_list_head *head, bool tail)
1997 {
1998         struct list_head *n, *h = (void *)head;
1999         struct bpf_list_node_kern *node;
2000
2001         /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't
2002          * called on its fields, so init here
2003          */
2004         if (unlikely(!h->next))
2005                 INIT_LIST_HEAD(h);
2006         if (list_empty(h))
2007                 return NULL;
2008
2009         n = tail ? h->prev : h->next;
2010         node = container_of(n, struct bpf_list_node_kern, list_head);
2011         if (WARN_ON_ONCE(READ_ONCE(node->owner) != head))
2012                 return NULL;
2013
2014         list_del_init(n);
2015         WRITE_ONCE(node->owner, NULL);
2016         return (struct bpf_list_node *)n;
2017 }
2018
2019 __bpf_kfunc struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head)
2020 {
2021         return __bpf_list_del(head, false);
2022 }
2023
2024 __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head)
2025 {
2026         return __bpf_list_del(head, true);
2027 }
2028
2029 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,
2030                                                   struct bpf_rb_node *node)
2031 {
2032         struct bpf_rb_node_kern *node_internal = (struct bpf_rb_node_kern *)node;
2033         struct rb_root_cached *r = (struct rb_root_cached *)root;
2034         struct rb_node *n = &node_internal->rb_node;
2035
2036         /* node_internal->owner != root implies either RB_EMPTY_NODE(n) or
2037          * n is owned by some other tree. No need to check RB_EMPTY_NODE(n)
2038          */
2039         if (READ_ONCE(node_internal->owner) != root)
2040                 return NULL;
2041
2042         rb_erase_cached(n, r);
2043         RB_CLEAR_NODE(n);
2044         WRITE_ONCE(node_internal->owner, NULL);
2045         return (struct bpf_rb_node *)n;
2046 }
2047
2048 /* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF
2049  * program
2050  */
2051 static int __bpf_rbtree_add(struct bpf_rb_root *root,
2052                             struct bpf_rb_node_kern *node,
2053                             void *less, struct btf_record *rec, u64 off)
2054 {
2055         struct rb_node **link = &((struct rb_root_cached *)root)->rb_root.rb_node;
2056         struct rb_node *parent = NULL, *n = &node->rb_node;
2057         bpf_callback_t cb = (bpf_callback_t)less;
2058         bool leftmost = true;
2059
2060         /* node->owner != NULL implies !RB_EMPTY_NODE(n), no need to separately
2061          * check the latter
2062          */
2063         if (cmpxchg(&node->owner, NULL, BPF_PTR_POISON)) {
2064                 /* Only called from BPF prog, no need to migrate_disable */
2065                 __bpf_obj_drop_impl((void *)n - off, rec);
2066                 return -EINVAL;
2067         }
2068
2069         while (*link) {
2070                 parent = *link;
2071                 if (cb((uintptr_t)node, (uintptr_t)parent, 0, 0, 0)) {
2072                         link = &parent->rb_left;
2073                 } else {
2074                         link = &parent->rb_right;
2075                         leftmost = false;
2076                 }
2077         }
2078
2079         rb_link_node(n, parent, link);
2080         rb_insert_color_cached(n, (struct rb_root_cached *)root, leftmost);
2081         WRITE_ONCE(node->owner, root);
2082         return 0;
2083 }
2084
2085 __bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node,
2086                                     bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b),
2087                                     void *meta__ign, u64 off)
2088 {
2089         struct btf_struct_meta *meta = meta__ign;
2090         struct bpf_rb_node_kern *n = (void *)node;
2091
2092         return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL, off);
2093 }
2094
2095 __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root)
2096 {
2097         struct rb_root_cached *r = (struct rb_root_cached *)root;
2098
2099         return (struct bpf_rb_node *)rb_first_cached(r);
2100 }
2101
2102 /**
2103  * bpf_task_acquire - Acquire a reference to a task. A task acquired by this
2104  * kfunc which is not stored in a map as a kptr, must be released by calling
2105  * bpf_task_release().
2106  * @p: The task on which a reference is being acquired.
2107  */
2108 __bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p)
2109 {
2110         if (refcount_inc_not_zero(&p->rcu_users))
2111                 return p;
2112         return NULL;
2113 }
2114
2115 /**
2116  * bpf_task_release - Release the reference acquired on a task.
2117  * @p: The task on which a reference is being released.
2118  */
2119 __bpf_kfunc void bpf_task_release(struct task_struct *p)
2120 {
2121         put_task_struct_rcu_user(p);
2122 }
2123
2124 #ifdef CONFIG_CGROUPS
2125 /**
2126  * bpf_cgroup_acquire - Acquire a reference to a cgroup. A cgroup acquired by
2127  * this kfunc which is not stored in a map as a kptr, must be released by
2128  * calling bpf_cgroup_release().
2129  * @cgrp: The cgroup on which a reference is being acquired.
2130  */
2131 __bpf_kfunc struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp)
2132 {
2133         return cgroup_tryget(cgrp) ? cgrp : NULL;
2134 }
2135
2136 /**
2137  * bpf_cgroup_release - Release the reference acquired on a cgroup.
2138  * If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to
2139  * not be freed until the current grace period has ended, even if its refcount
2140  * drops to 0.
2141  * @cgrp: The cgroup on which a reference is being released.
2142  */
2143 __bpf_kfunc void bpf_cgroup_release(struct cgroup *cgrp)
2144 {
2145         cgroup_put(cgrp);
2146 }
2147
2148 /**
2149  * bpf_cgroup_ancestor - Perform a lookup on an entry in a cgroup's ancestor
2150  * array. A cgroup returned by this kfunc which is not subsequently stored in a
2151  * map, must be released by calling bpf_cgroup_release().
2152  * @cgrp: The cgroup for which we're performing a lookup.
2153  * @level: The level of ancestor to look up.
2154  */
2155 __bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)
2156 {
2157         struct cgroup *ancestor;
2158
2159         if (level > cgrp->level || level < 0)
2160                 return NULL;
2161
2162         /* cgrp's refcnt could be 0 here, but ancestors can still be accessed */
2163         ancestor = cgrp->ancestors[level];
2164         if (!cgroup_tryget(ancestor))
2165                 return NULL;
2166         return ancestor;
2167 }
2168
2169 /**
2170  * bpf_cgroup_from_id - Find a cgroup from its ID. A cgroup returned by this
2171  * kfunc which is not subsequently stored in a map, must be released by calling
2172  * bpf_cgroup_release().
2173  * @cgid: cgroup id.
2174  */
2175 __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
2176 {
2177         struct cgroup *cgrp;
2178
2179         cgrp = cgroup_get_from_id(cgid);
2180         if (IS_ERR(cgrp))
2181                 return NULL;
2182         return cgrp;
2183 }
2184
2185 /**
2186  * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test
2187  * task's membership of cgroup ancestry.
2188  * @task: the task to be tested
2189  * @ancestor: possible ancestor of @task's cgroup
2190  *
2191  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
2192  * It follows all the same rules as cgroup_is_descendant, and only applies
2193  * to the default hierarchy.
2194  */
2195 __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
2196                                        struct cgroup *ancestor)
2197 {
2198         return task_under_cgroup_hierarchy(task, ancestor);
2199 }
2200 #endif /* CONFIG_CGROUPS */
2201
2202 /**
2203  * bpf_task_from_pid - Find a struct task_struct from its pid by looking it up
2204  * in the root pid namespace idr. If a task is returned, it must either be
2205  * stored in a map, or released with bpf_task_release().
2206  * @pid: The pid of the task being looked up.
2207  */
2208 __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
2209 {
2210         struct task_struct *p;
2211
2212         rcu_read_lock();
2213         p = find_task_by_pid_ns(pid, &init_pid_ns);
2214         if (p)
2215                 p = bpf_task_acquire(p);
2216         rcu_read_unlock();
2217
2218         return p;
2219 }
2220
2221 /**
2222  * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data.
2223  * @ptr: The dynptr whose data slice to retrieve
2224  * @offset: Offset into the dynptr
2225  * @buffer__opt: User-provided buffer to copy contents into.  May be NULL
2226  * @buffer__szk: Size (in bytes) of the buffer if present. This is the
2227  *               length of the requested slice. This must be a constant.
2228  *
2229  * For non-skb and non-xdp type dynptrs, there is no difference between
2230  * bpf_dynptr_slice and bpf_dynptr_data.
2231  *
2232  *  If buffer__opt is NULL, the call will fail if buffer_opt was needed.
2233  *
2234  * If the intention is to write to the data slice, please use
2235  * bpf_dynptr_slice_rdwr.
2236  *
2237  * The user must check that the returned pointer is not null before using it.
2238  *
2239  * Please note that in the case of skb and xdp dynptrs, bpf_dynptr_slice
2240  * does not change the underlying packet data pointers, so a call to
2241  * bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in
2242  * the bpf program.
2243  *
2244  * Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only
2245  * data slice (can be either direct pointer to the data or a pointer to the user
2246  * provided buffer, with its contents containing the data, if unable to obtain
2247  * direct pointer)
2248  */
2249 __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,
2250                                    void *buffer__opt, u32 buffer__szk)
2251 {
2252         enum bpf_dynptr_type type;
2253         u32 len = buffer__szk;
2254         int err;
2255
2256         if (!ptr->data)
2257                 return NULL;
2258
2259         err = bpf_dynptr_check_off_len(ptr, offset, len);
2260         if (err)
2261                 return NULL;
2262
2263         type = bpf_dynptr_get_type(ptr);
2264
2265         switch (type) {
2266         case BPF_DYNPTR_TYPE_LOCAL:
2267         case BPF_DYNPTR_TYPE_RINGBUF:
2268                 return ptr->data + ptr->offset + offset;
2269         case BPF_DYNPTR_TYPE_SKB:
2270                 if (buffer__opt)
2271                         return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer__opt);
2272                 else
2273                         return skb_pointer_if_linear(ptr->data, ptr->offset + offset, len);
2274         case BPF_DYNPTR_TYPE_XDP:
2275         {
2276                 void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
2277                 if (!IS_ERR_OR_NULL(xdp_ptr))
2278                         return xdp_ptr;
2279
2280                 if (!buffer__opt)
2281                         return NULL;
2282                 bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer__opt, len, false);
2283                 return buffer__opt;
2284         }
2285         default:
2286                 WARN_ONCE(true, "unknown dynptr type %d\n", type);
2287                 return NULL;
2288         }
2289 }
2290
2291 /**
2292  * bpf_dynptr_slice_rdwr() - Obtain a writable pointer to the dynptr data.
2293  * @ptr: The dynptr whose data slice to retrieve
2294  * @offset: Offset into the dynptr
2295  * @buffer__opt: User-provided buffer to copy contents into. May be NULL
2296  * @buffer__szk: Size (in bytes) of the buffer if present. This is the
2297  *               length of the requested slice. This must be a constant.
2298  *
2299  * For non-skb and non-xdp type dynptrs, there is no difference between
2300  * bpf_dynptr_slice and bpf_dynptr_data.
2301  *
2302  * If buffer__opt is NULL, the call will fail if buffer_opt was needed.
2303  *
2304  * The returned pointer is writable and may point to either directly the dynptr
2305  * data at the requested offset or to the buffer if unable to obtain a direct
2306  * data pointer to (example: the requested slice is to the paged area of an skb
2307  * packet). In the case where the returned pointer is to the buffer, the user
2308  * is responsible for persisting writes through calling bpf_dynptr_write(). This
2309  * usually looks something like this pattern:
2310  *
2311  * struct eth_hdr *eth = bpf_dynptr_slice_rdwr(&dynptr, 0, buffer, sizeof(buffer));
2312  * if (!eth)
2313  *      return TC_ACT_SHOT;
2314  *
2315  * // mutate eth header //
2316  *
2317  * if (eth == buffer)
2318  *      bpf_dynptr_write(&ptr, 0, buffer, sizeof(buffer), 0);
2319  *
2320  * Please note that, as in the example above, the user must check that the
2321  * returned pointer is not null before using it.
2322  *
2323  * Please also note that in the case of skb and xdp dynptrs, bpf_dynptr_slice_rdwr
2324  * does not change the underlying packet data pointers, so a call to
2325  * bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in
2326  * the bpf program.
2327  *
2328  * Return: NULL if the call failed (eg invalid dynptr), pointer to a
2329  * data slice (can be either direct pointer to the data or a pointer to the user
2330  * provided buffer, with its contents containing the data, if unable to obtain
2331  * direct pointer)
2332  */
2333 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
2334                                         void *buffer__opt, u32 buffer__szk)
2335 {
2336         if (!ptr->data || __bpf_dynptr_is_rdonly(ptr))
2337                 return NULL;
2338
2339         /* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
2340          *
2341          * For skb-type dynptrs, it is safe to write into the returned pointer
2342          * if the bpf program allows skb data writes. There are two possiblities
2343          * that may occur when calling bpf_dynptr_slice_rdwr:
2344          *
2345          * 1) The requested slice is in the head of the skb. In this case, the
2346          * returned pointer is directly to skb data, and if the skb is cloned, the
2347          * verifier will have uncloned it (see bpf_unclone_prologue()) already.
2348          * The pointer can be directly written into.
2349          *
2350          * 2) Some portion of the requested slice is in the paged buffer area.
2351          * In this case, the requested data will be copied out into the buffer
2352          * and the returned pointer will be a pointer to the buffer. The skb
2353          * will not be pulled. To persist the write, the user will need to call
2354          * bpf_dynptr_write(), which will pull the skb and commit the write.
2355          *
2356          * Similarly for xdp programs, if the requested slice is not across xdp
2357          * fragments, then a direct pointer will be returned, otherwise the data
2358          * will be copied out into the buffer and the user will need to call
2359          * bpf_dynptr_write() to commit changes.
2360          */
2361         return bpf_dynptr_slice(ptr, offset, buffer__opt, buffer__szk);
2362 }
2363
2364 __bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)
2365 {
2366         u32 size;
2367
2368         if (!ptr->data || start > end)
2369                 return -EINVAL;
2370
2371         size = __bpf_dynptr_size(ptr);
2372
2373         if (start > size || end > size)
2374                 return -ERANGE;
2375
2376         ptr->offset += start;
2377         bpf_dynptr_set_size(ptr, end - start);
2378
2379         return 0;
2380 }
2381
2382 __bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
2383 {
2384         return !ptr->data;
2385 }
2386
2387 __bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
2388 {
2389         if (!ptr->data)
2390                 return false;
2391
2392         return __bpf_dynptr_is_rdonly(ptr);
2393 }
2394
2395 __bpf_kfunc __u32 bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
2396 {
2397         if (!ptr->data)
2398                 return -EINVAL;
2399
2400         return __bpf_dynptr_size(ptr);
2401 }
2402
2403 __bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
2404                                  struct bpf_dynptr_kern *clone__uninit)
2405 {
2406         if (!ptr->data) {
2407                 bpf_dynptr_set_null(clone__uninit);
2408                 return -EINVAL;
2409         }
2410
2411         *clone__uninit = *ptr;
2412
2413         return 0;
2414 }
2415
2416 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
2417 {
2418         return obj;
2419 }
2420
2421 __bpf_kfunc void *bpf_rdonly_cast(void *obj__ign, u32 btf_id__k)
2422 {
2423         return obj__ign;
2424 }
2425
2426 __bpf_kfunc void bpf_rcu_read_lock(void)
2427 {
2428         rcu_read_lock();
2429 }
2430
2431 __bpf_kfunc void bpf_rcu_read_unlock(void)
2432 {
2433         rcu_read_unlock();
2434 }
2435
2436 __diag_pop();
2437
2438 BTF_SET8_START(generic_btf_ids)
2439 #ifdef CONFIG_KEXEC_CORE
2440 BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)
2441 #endif
2442 BTF_ID_FLAGS(func, bpf_obj_new_impl, KF_ACQUIRE | KF_RET_NULL)
2443 BTF_ID_FLAGS(func, bpf_obj_drop_impl, KF_RELEASE)
2444 BTF_ID_FLAGS(func, bpf_refcount_acquire_impl, KF_ACQUIRE | KF_RET_NULL)
2445 BTF_ID_FLAGS(func, bpf_list_push_front_impl)
2446 BTF_ID_FLAGS(func, bpf_list_push_back_impl)
2447 BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL)
2448 BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL)
2449 BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
2450 BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE)
2451 BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL)
2452 BTF_ID_FLAGS(func, bpf_rbtree_add_impl)
2453 BTF_ID_FLAGS(func, bpf_rbtree_first, KF_RET_NULL)
2454
2455 #ifdef CONFIG_CGROUPS
2456 BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
2457 BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
2458 BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
2459 BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
2460 BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
2461 #endif
2462 BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
2463 BTF_SET8_END(generic_btf_ids)
2464
2465 static const struct btf_kfunc_id_set generic_kfunc_set = {
2466         .owner = THIS_MODULE,
2467         .set   = &generic_btf_ids,
2468 };
2469
2470
2471 BTF_ID_LIST(generic_dtor_ids)
2472 BTF_ID(struct, task_struct)
2473 BTF_ID(func, bpf_task_release)
2474 #ifdef CONFIG_CGROUPS
2475 BTF_ID(struct, cgroup)
2476 BTF_ID(func, bpf_cgroup_release)
2477 #endif
2478
2479 BTF_SET8_START(common_btf_ids)
2480 BTF_ID_FLAGS(func, bpf_cast_to_kern_ctx)
2481 BTF_ID_FLAGS(func, bpf_rdonly_cast)
2482 BTF_ID_FLAGS(func, bpf_rcu_read_lock)
2483 BTF_ID_FLAGS(func, bpf_rcu_read_unlock)
2484 BTF_ID_FLAGS(func, bpf_dynptr_slice, KF_RET_NULL)
2485 BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
2486 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
2487 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
2488 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
2489 BTF_ID_FLAGS(func, bpf_dynptr_adjust)
2490 BTF_ID_FLAGS(func, bpf_dynptr_is_null)
2491 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
2492 BTF_ID_FLAGS(func, bpf_dynptr_size)
2493 BTF_ID_FLAGS(func, bpf_dynptr_clone)
2494 BTF_SET8_END(common_btf_ids)
2495
2496 static const struct btf_kfunc_id_set common_kfunc_set = {
2497         .owner = THIS_MODULE,
2498         .set   = &common_btf_ids,
2499 };
2500
2501 static int __init kfunc_init(void)
2502 {
2503         int ret;
2504         const struct btf_id_dtor_kfunc generic_dtors[] = {
2505                 {
2506                         .btf_id       = generic_dtor_ids[0],
2507                         .kfunc_btf_id = generic_dtor_ids[1]
2508                 },
2509 #ifdef CONFIG_CGROUPS
2510                 {
2511                         .btf_id       = generic_dtor_ids[2],
2512                         .kfunc_btf_id = generic_dtor_ids[3]
2513                 },
2514 #endif
2515         };
2516
2517         ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &generic_kfunc_set);
2518         ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &generic_kfunc_set);
2519         ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);
2520         ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
2521                                                   ARRAY_SIZE(generic_dtors),
2522                                                   THIS_MODULE);
2523         return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &common_kfunc_set);
2524 }
2525
2526 late_initcall(kfunc_init);