1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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 #include <linux/kasan.h>
27 #include "../../lib/kstrtox.h"
29 /* If kernel subsystem is allowing eBPF programs to call this function,
30 * inside its own verifier_ops->get_func_proto() callback it should return
31 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
33 * Different map implementations will rely on rcu in map methods
34 * lookup/update/delete, therefore eBPF programs must run under rcu lock
35 * if program is allowed to access maps, so check rcu_read_lock_held() or
36 * rcu_read_lock_trace_held() in all three functions.
38 BPF_CALL_2(bpf_map_lookup_elem
, struct bpf_map
*, map
, void *, key
)
40 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
41 !rcu_read_lock_bh_held());
42 return (unsigned long) map
->ops
->map_lookup_elem(map
, key
);
45 const struct bpf_func_proto bpf_map_lookup_elem_proto
= {
46 .func
= bpf_map_lookup_elem
,
49 .ret_type
= RET_PTR_TO_MAP_VALUE_OR_NULL
,
50 .arg1_type
= ARG_CONST_MAP_PTR
,
51 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
54 BPF_CALL_4(bpf_map_update_elem
, struct bpf_map
*, map
, void *, key
,
55 void *, value
, u64
, flags
)
57 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
58 !rcu_read_lock_bh_held());
59 return map
->ops
->map_update_elem(map
, key
, value
, flags
);
62 const struct bpf_func_proto bpf_map_update_elem_proto
= {
63 .func
= bpf_map_update_elem
,
66 .ret_type
= RET_INTEGER
,
67 .arg1_type
= ARG_CONST_MAP_PTR
,
68 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
69 .arg3_type
= ARG_PTR_TO_MAP_VALUE
,
70 .arg4_type
= ARG_ANYTHING
,
73 BPF_CALL_2(bpf_map_delete_elem
, struct bpf_map
*, map
, void *, key
)
75 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
76 !rcu_read_lock_bh_held());
77 return map
->ops
->map_delete_elem(map
, key
);
80 const struct bpf_func_proto bpf_map_delete_elem_proto
= {
81 .func
= bpf_map_delete_elem
,
84 .ret_type
= RET_INTEGER
,
85 .arg1_type
= ARG_CONST_MAP_PTR
,
86 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
89 BPF_CALL_3(bpf_map_push_elem
, struct bpf_map
*, map
, void *, value
, u64
, flags
)
91 return map
->ops
->map_push_elem(map
, value
, flags
);
94 const struct bpf_func_proto bpf_map_push_elem_proto
= {
95 .func
= bpf_map_push_elem
,
98 .ret_type
= RET_INTEGER
,
99 .arg1_type
= ARG_CONST_MAP_PTR
,
100 .arg2_type
= ARG_PTR_TO_MAP_VALUE
,
101 .arg3_type
= ARG_ANYTHING
,
104 BPF_CALL_2(bpf_map_pop_elem
, struct bpf_map
*, map
, void *, value
)
106 return map
->ops
->map_pop_elem(map
, value
);
109 const struct bpf_func_proto bpf_map_pop_elem_proto
= {
110 .func
= bpf_map_pop_elem
,
112 .ret_type
= RET_INTEGER
,
113 .arg1_type
= ARG_CONST_MAP_PTR
,
114 .arg2_type
= ARG_PTR_TO_MAP_VALUE
| MEM_UNINIT
| MEM_WRITE
,
117 BPF_CALL_2(bpf_map_peek_elem
, struct bpf_map
*, map
, void *, value
)
119 return map
->ops
->map_peek_elem(map
, value
);
122 const struct bpf_func_proto bpf_map_peek_elem_proto
= {
123 .func
= bpf_map_peek_elem
,
125 .ret_type
= RET_INTEGER
,
126 .arg1_type
= ARG_CONST_MAP_PTR
,
127 .arg2_type
= ARG_PTR_TO_MAP_VALUE
| MEM_UNINIT
| MEM_WRITE
,
130 BPF_CALL_3(bpf_map_lookup_percpu_elem
, struct bpf_map
*, map
, void *, key
, u32
, cpu
)
132 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
133 return (unsigned long) map
->ops
->map_lookup_percpu_elem(map
, key
, cpu
);
136 const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto
= {
137 .func
= bpf_map_lookup_percpu_elem
,
140 .ret_type
= RET_PTR_TO_MAP_VALUE_OR_NULL
,
141 .arg1_type
= ARG_CONST_MAP_PTR
,
142 .arg2_type
= ARG_PTR_TO_MAP_KEY
,
143 .arg3_type
= ARG_ANYTHING
,
146 const struct bpf_func_proto bpf_get_prandom_u32_proto
= {
147 .func
= bpf_user_rnd_u32
,
149 .ret_type
= RET_INTEGER
,
152 BPF_CALL_0(bpf_get_smp_processor_id
)
154 return smp_processor_id();
157 const struct bpf_func_proto bpf_get_smp_processor_id_proto
= {
158 .func
= bpf_get_smp_processor_id
,
160 .ret_type
= RET_INTEGER
,
161 .allow_fastcall
= true,
164 BPF_CALL_0(bpf_get_numa_node_id
)
166 return numa_node_id();
169 const struct bpf_func_proto bpf_get_numa_node_id_proto
= {
170 .func
= bpf_get_numa_node_id
,
172 .ret_type
= RET_INTEGER
,
175 BPF_CALL_0(bpf_ktime_get_ns
)
177 /* NMI safe access to clock monotonic */
178 return ktime_get_mono_fast_ns();
181 const struct bpf_func_proto bpf_ktime_get_ns_proto
= {
182 .func
= bpf_ktime_get_ns
,
184 .ret_type
= RET_INTEGER
,
187 BPF_CALL_0(bpf_ktime_get_boot_ns
)
189 /* NMI safe access to clock boottime */
190 return ktime_get_boot_fast_ns();
193 const struct bpf_func_proto bpf_ktime_get_boot_ns_proto
= {
194 .func
= bpf_ktime_get_boot_ns
,
196 .ret_type
= RET_INTEGER
,
199 BPF_CALL_0(bpf_ktime_get_coarse_ns
)
201 return ktime_get_coarse_ns();
204 const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto
= {
205 .func
= bpf_ktime_get_coarse_ns
,
207 .ret_type
= RET_INTEGER
,
210 BPF_CALL_0(bpf_ktime_get_tai_ns
)
212 /* NMI safe access to clock tai */
213 return ktime_get_tai_fast_ns();
216 const struct bpf_func_proto bpf_ktime_get_tai_ns_proto
= {
217 .func
= bpf_ktime_get_tai_ns
,
219 .ret_type
= RET_INTEGER
,
222 BPF_CALL_0(bpf_get_current_pid_tgid
)
224 struct task_struct
*task
= current
;
229 return (u64
) task
->tgid
<< 32 | task
->pid
;
232 const struct bpf_func_proto bpf_get_current_pid_tgid_proto
= {
233 .func
= bpf_get_current_pid_tgid
,
235 .ret_type
= RET_INTEGER
,
238 BPF_CALL_0(bpf_get_current_uid_gid
)
240 struct task_struct
*task
= current
;
247 current_uid_gid(&uid
, &gid
);
248 return (u64
) from_kgid(&init_user_ns
, gid
) << 32 |
249 from_kuid(&init_user_ns
, uid
);
252 const struct bpf_func_proto bpf_get_current_uid_gid_proto
= {
253 .func
= bpf_get_current_uid_gid
,
255 .ret_type
= RET_INTEGER
,
258 BPF_CALL_2(bpf_get_current_comm
, char *, buf
, u32
, size
)
260 struct task_struct
*task
= current
;
265 /* Verifier guarantees that size > 0 */
266 strscpy_pad(buf
, task
->comm
, size
);
269 memset(buf
, 0, size
);
273 const struct bpf_func_proto bpf_get_current_comm_proto
= {
274 .func
= bpf_get_current_comm
,
276 .ret_type
= RET_INTEGER
,
277 .arg1_type
= ARG_PTR_TO_UNINIT_MEM
,
278 .arg2_type
= ARG_CONST_SIZE
,
281 #if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK)
283 static inline void __bpf_spin_lock(struct bpf_spin_lock
*lock
)
285 arch_spinlock_t
*l
= (void *)lock
;
288 arch_spinlock_t lock
;
289 } u
= { .lock
= __ARCH_SPIN_LOCK_UNLOCKED
};
291 compiletime_assert(u
.val
== 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0");
292 BUILD_BUG_ON(sizeof(*l
) != sizeof(__u32
));
293 BUILD_BUG_ON(sizeof(*lock
) != sizeof(__u32
));
298 static inline void __bpf_spin_unlock(struct bpf_spin_lock
*lock
)
300 arch_spinlock_t
*l
= (void *)lock
;
308 static inline void __bpf_spin_lock(struct bpf_spin_lock
*lock
)
310 atomic_t
*l
= (void *)lock
;
312 BUILD_BUG_ON(sizeof(*l
) != sizeof(*lock
));
314 atomic_cond_read_relaxed(l
, !VAL
);
315 } while (atomic_xchg(l
, 1));
318 static inline void __bpf_spin_unlock(struct bpf_spin_lock
*lock
)
320 atomic_t
*l
= (void *)lock
;
322 atomic_set_release(l
, 0);
327 static DEFINE_PER_CPU(unsigned long, irqsave_flags
);
329 static inline void __bpf_spin_lock_irqsave(struct bpf_spin_lock
*lock
)
333 local_irq_save(flags
);
334 __bpf_spin_lock(lock
);
335 __this_cpu_write(irqsave_flags
, flags
);
338 NOTRACE_BPF_CALL_1(bpf_spin_lock
, struct bpf_spin_lock
*, lock
)
340 __bpf_spin_lock_irqsave(lock
);
344 const struct bpf_func_proto bpf_spin_lock_proto
= {
345 .func
= bpf_spin_lock
,
347 .ret_type
= RET_VOID
,
348 .arg1_type
= ARG_PTR_TO_SPIN_LOCK
,
349 .arg1_btf_id
= BPF_PTR_POISON
,
352 static inline void __bpf_spin_unlock_irqrestore(struct bpf_spin_lock
*lock
)
356 flags
= __this_cpu_read(irqsave_flags
);
357 __bpf_spin_unlock(lock
);
358 local_irq_restore(flags
);
361 NOTRACE_BPF_CALL_1(bpf_spin_unlock
, struct bpf_spin_lock
*, lock
)
363 __bpf_spin_unlock_irqrestore(lock
);
367 const struct bpf_func_proto bpf_spin_unlock_proto
= {
368 .func
= bpf_spin_unlock
,
370 .ret_type
= RET_VOID
,
371 .arg1_type
= ARG_PTR_TO_SPIN_LOCK
,
372 .arg1_btf_id
= BPF_PTR_POISON
,
375 void copy_map_value_locked(struct bpf_map
*map
, void *dst
, void *src
,
378 struct bpf_spin_lock
*lock
;
381 lock
= src
+ map
->record
->spin_lock_off
;
383 lock
= dst
+ map
->record
->spin_lock_off
;
385 __bpf_spin_lock_irqsave(lock
);
386 copy_map_value(map
, dst
, src
);
387 __bpf_spin_unlock_irqrestore(lock
);
391 BPF_CALL_0(bpf_jiffies64
)
393 return get_jiffies_64();
396 const struct bpf_func_proto bpf_jiffies64_proto
= {
397 .func
= bpf_jiffies64
,
399 .ret_type
= RET_INTEGER
,
402 #ifdef CONFIG_CGROUPS
403 BPF_CALL_0(bpf_get_current_cgroup_id
)
409 cgrp
= task_dfl_cgroup(current
);
410 cgrp_id
= cgroup_id(cgrp
);
416 const struct bpf_func_proto bpf_get_current_cgroup_id_proto
= {
417 .func
= bpf_get_current_cgroup_id
,
419 .ret_type
= RET_INTEGER
,
422 BPF_CALL_1(bpf_get_current_ancestor_cgroup_id
, int, ancestor_level
)
425 struct cgroup
*ancestor
;
429 cgrp
= task_dfl_cgroup(current
);
430 ancestor
= cgroup_ancestor(cgrp
, ancestor_level
);
431 cgrp_id
= ancestor
? cgroup_id(ancestor
) : 0;
437 const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto
= {
438 .func
= bpf_get_current_ancestor_cgroup_id
,
440 .ret_type
= RET_INTEGER
,
441 .arg1_type
= ARG_ANYTHING
,
443 #endif /* CONFIG_CGROUPS */
445 #define BPF_STRTOX_BASE_MASK 0x1F
447 static int __bpf_strtoull(const char *buf
, size_t buf_len
, u64 flags
,
448 unsigned long long *res
, bool *is_negative
)
450 unsigned int base
= flags
& BPF_STRTOX_BASE_MASK
;
451 const char *cur_buf
= buf
;
452 size_t cur_len
= buf_len
;
453 unsigned int consumed
;
457 if (!buf
|| !buf_len
|| !res
|| !is_negative
)
460 if (base
!= 0 && base
!= 8 && base
!= 10 && base
!= 16)
463 if (flags
& ~BPF_STRTOX_BASE_MASK
)
466 while (cur_buf
< buf
+ buf_len
&& isspace(*cur_buf
))
469 *is_negative
= (cur_buf
< buf
+ buf_len
&& *cur_buf
== '-');
473 consumed
= cur_buf
- buf
;
478 cur_len
= min(cur_len
, sizeof(str
) - 1);
479 memcpy(str
, cur_buf
, cur_len
);
483 cur_buf
= _parse_integer_fixup_radix(cur_buf
, &base
);
484 val_len
= _parse_integer(cur_buf
, base
, res
);
486 if (val_len
& KSTRTOX_OVERFLOW
)
493 consumed
+= cur_buf
- str
;
498 static int __bpf_strtoll(const char *buf
, size_t buf_len
, u64 flags
,
501 unsigned long long _res
;
505 err
= __bpf_strtoull(buf
, buf_len
, flags
, &_res
, &is_negative
);
509 if ((long long)-_res
> 0)
513 if ((long long)_res
< 0)
520 BPF_CALL_4(bpf_strtol
, const char *, buf
, size_t, buf_len
, u64
, flags
,
527 err
= __bpf_strtoll(buf
, buf_len
, flags
, &_res
);
534 const struct bpf_func_proto bpf_strtol_proto
= {
537 .ret_type
= RET_INTEGER
,
538 .arg1_type
= ARG_PTR_TO_MEM
| MEM_RDONLY
,
539 .arg2_type
= ARG_CONST_SIZE
,
540 .arg3_type
= ARG_ANYTHING
,
541 .arg4_type
= ARG_PTR_TO_FIXED_SIZE_MEM
| MEM_UNINIT
| MEM_WRITE
| MEM_ALIGNED
,
542 .arg4_size
= sizeof(s64
),
545 BPF_CALL_4(bpf_strtoul
, const char *, buf
, size_t, buf_len
, u64
, flags
,
548 unsigned long long _res
;
553 err
= __bpf_strtoull(buf
, buf_len
, flags
, &_res
, &is_negative
);
562 const struct bpf_func_proto bpf_strtoul_proto
= {
565 .ret_type
= RET_INTEGER
,
566 .arg1_type
= ARG_PTR_TO_MEM
| MEM_RDONLY
,
567 .arg2_type
= ARG_CONST_SIZE
,
568 .arg3_type
= ARG_ANYTHING
,
569 .arg4_type
= ARG_PTR_TO_FIXED_SIZE_MEM
| MEM_UNINIT
| MEM_WRITE
| MEM_ALIGNED
,
570 .arg4_size
= sizeof(u64
),
573 BPF_CALL_3(bpf_strncmp
, const char *, s1
, u32
, s1_sz
, const char *, s2
)
575 return strncmp(s1
, s2
, s1_sz
);
578 static const struct bpf_func_proto bpf_strncmp_proto
= {
581 .ret_type
= RET_INTEGER
,
582 .arg1_type
= ARG_PTR_TO_MEM
| MEM_RDONLY
,
583 .arg2_type
= ARG_CONST_SIZE
,
584 .arg3_type
= ARG_PTR_TO_CONST_STR
,
587 BPF_CALL_4(bpf_get_ns_current_pid_tgid
, u64
, dev
, u64
, ino
,
588 struct bpf_pidns_info
*, nsdata
, u32
, size
)
590 struct task_struct
*task
= current
;
591 struct pid_namespace
*pidns
;
594 if (unlikely(size
!= sizeof(struct bpf_pidns_info
)))
597 if (unlikely((u64
)(dev_t
)dev
!= dev
))
603 pidns
= task_active_pid_ns(task
);
604 if (unlikely(!pidns
)) {
609 if (!ns_match(&pidns
->ns
, (dev_t
)dev
, ino
))
612 nsdata
->pid
= task_pid_nr_ns(task
, pidns
);
613 nsdata
->tgid
= task_tgid_nr_ns(task
, pidns
);
616 memset((void *)nsdata
, 0, (size_t) size
);
620 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto
= {
621 .func
= bpf_get_ns_current_pid_tgid
,
623 .ret_type
= RET_INTEGER
,
624 .arg1_type
= ARG_ANYTHING
,
625 .arg2_type
= ARG_ANYTHING
,
626 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
627 .arg4_type
= ARG_CONST_SIZE
,
630 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto
= {
631 .func
= bpf_get_raw_cpu_id
,
633 .ret_type
= RET_INTEGER
,
636 BPF_CALL_5(bpf_event_output_data
, void *, ctx
, struct bpf_map
*, map
,
637 u64
, flags
, void *, data
, u64
, size
)
639 if (unlikely(flags
& ~(BPF_F_INDEX_MASK
)))
642 return bpf_event_output(map
, flags
, data
, size
, NULL
, 0, NULL
);
645 const struct bpf_func_proto bpf_event_output_data_proto
= {
646 .func
= bpf_event_output_data
,
648 .ret_type
= RET_INTEGER
,
649 .arg1_type
= ARG_PTR_TO_CTX
,
650 .arg2_type
= ARG_CONST_MAP_PTR
,
651 .arg3_type
= ARG_ANYTHING
,
652 .arg4_type
= ARG_PTR_TO_MEM
| MEM_RDONLY
,
653 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
656 BPF_CALL_3(bpf_copy_from_user
, void *, dst
, u32
, size
,
657 const void __user
*, user_ptr
)
659 int ret
= copy_from_user(dst
, user_ptr
, size
);
662 memset(dst
, 0, size
);
669 const struct bpf_func_proto bpf_copy_from_user_proto
= {
670 .func
= bpf_copy_from_user
,
673 .ret_type
= RET_INTEGER
,
674 .arg1_type
= ARG_PTR_TO_UNINIT_MEM
,
675 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
676 .arg3_type
= ARG_ANYTHING
,
679 BPF_CALL_5(bpf_copy_from_user_task
, void *, dst
, u32
, size
,
680 const void __user
*, user_ptr
, struct task_struct
*, tsk
, u64
, flags
)
684 /* flags is not used yet */
691 ret
= access_process_vm(tsk
, (unsigned long)user_ptr
, dst
, size
, 0);
695 memset(dst
, 0, size
);
696 /* Return -EFAULT for partial read */
697 return ret
< 0 ? ret
: -EFAULT
;
700 const struct bpf_func_proto bpf_copy_from_user_task_proto
= {
701 .func
= bpf_copy_from_user_task
,
704 .ret_type
= RET_INTEGER
,
705 .arg1_type
= ARG_PTR_TO_UNINIT_MEM
,
706 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
707 .arg3_type
= ARG_ANYTHING
,
708 .arg4_type
= ARG_PTR_TO_BTF_ID
,
709 .arg4_btf_id
= &btf_tracing_ids
[BTF_TRACING_TYPE_TASK
],
710 .arg5_type
= ARG_ANYTHING
713 BPF_CALL_2(bpf_per_cpu_ptr
, const void *, ptr
, u32
, cpu
)
715 if (cpu
>= nr_cpu_ids
)
716 return (unsigned long)NULL
;
718 return (unsigned long)per_cpu_ptr((const void __percpu
*)(const uintptr_t)ptr
, cpu
);
721 const struct bpf_func_proto bpf_per_cpu_ptr_proto
= {
722 .func
= bpf_per_cpu_ptr
,
724 .ret_type
= RET_PTR_TO_MEM_OR_BTF_ID
| PTR_MAYBE_NULL
| MEM_RDONLY
,
725 .arg1_type
= ARG_PTR_TO_PERCPU_BTF_ID
,
726 .arg2_type
= ARG_ANYTHING
,
729 BPF_CALL_1(bpf_this_cpu_ptr
, const void *, percpu_ptr
)
731 return (unsigned long)this_cpu_ptr((const void __percpu
*)(const uintptr_t)percpu_ptr
);
734 const struct bpf_func_proto bpf_this_cpu_ptr_proto
= {
735 .func
= bpf_this_cpu_ptr
,
737 .ret_type
= RET_PTR_TO_MEM_OR_BTF_ID
| MEM_RDONLY
,
738 .arg1_type
= ARG_PTR_TO_PERCPU_BTF_ID
,
741 static int bpf_trace_copy_string(char *buf
, void *unsafe_ptr
, char fmt_ptype
,
744 void __user
*user_ptr
= (__force
void __user
*)unsafe_ptr
;
750 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
751 if ((unsigned long)unsafe_ptr
< TASK_SIZE
)
752 return strncpy_from_user_nofault(buf
, user_ptr
, bufsz
);
756 return strncpy_from_kernel_nofault(buf
, unsafe_ptr
, bufsz
);
758 return strncpy_from_user_nofault(buf
, user_ptr
, bufsz
);
764 /* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
765 * arguments representation.
767 #define MAX_BPRINTF_BIN_ARGS 512
769 /* Support executing three nested bprintf helper calls on a given CPU */
770 #define MAX_BPRINTF_NEST_LEVEL 3
771 struct bpf_bprintf_buffers
{
772 char bin_args
[MAX_BPRINTF_BIN_ARGS
];
773 char buf
[MAX_BPRINTF_BUF
];
776 static DEFINE_PER_CPU(struct bpf_bprintf_buffers
[MAX_BPRINTF_NEST_LEVEL
], bpf_bprintf_bufs
);
777 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level
);
779 static int try_get_buffers(struct bpf_bprintf_buffers
**bufs
)
784 nest_level
= this_cpu_inc_return(bpf_bprintf_nest_level
);
785 if (WARN_ON_ONCE(nest_level
> MAX_BPRINTF_NEST_LEVEL
)) {
786 this_cpu_dec(bpf_bprintf_nest_level
);
790 *bufs
= this_cpu_ptr(&bpf_bprintf_bufs
[nest_level
- 1]);
795 void bpf_bprintf_cleanup(struct bpf_bprintf_data
*data
)
797 if (!data
->bin_args
&& !data
->buf
)
799 if (WARN_ON_ONCE(this_cpu_read(bpf_bprintf_nest_level
) == 0))
801 this_cpu_dec(bpf_bprintf_nest_level
);
806 * bpf_bprintf_prepare - Generic pass on format strings for bprintf-like helpers
808 * Returns a negative value if fmt is an invalid format string or 0 otherwise.
810 * This can be used in two ways:
811 * - Format string verification only: when data->get_bin_args is false
812 * - Arguments preparation: in addition to the above verification, it writes in
813 * data->bin_args a binary representation of arguments usable by bstr_printf
814 * where pointers from BPF have been sanitized.
816 * In argument preparation mode, if 0 is returned, safe temporary buffers are
817 * allocated and bpf_bprintf_cleanup should be called to free them after use.
819 int bpf_bprintf_prepare(char *fmt
, u32 fmt_size
, const u64
*raw_args
,
820 u32 num_args
, struct bpf_bprintf_data
*data
)
822 bool get_buffers
= (data
->get_bin_args
&& num_args
) || data
->get_buf
;
823 char *unsafe_ptr
= NULL
, *tmp_buf
= NULL
, *tmp_buf_end
, *fmt_end
;
824 struct bpf_bprintf_buffers
*buffers
= NULL
;
825 size_t sizeof_cur_arg
, sizeof_cur_ip
;
826 int err
, i
, num_spec
= 0;
828 char fmt_ptype
, cur_ip
[16], ip_spec
[] = "%pXX";
830 fmt_end
= strnchr(fmt
, fmt_size
, 0);
833 fmt_size
= fmt_end
- fmt
;
835 if (get_buffers
&& try_get_buffers(&buffers
))
838 if (data
->get_bin_args
) {
840 tmp_buf
= buffers
->bin_args
;
841 tmp_buf_end
= tmp_buf
+ MAX_BPRINTF_BIN_ARGS
;
842 data
->bin_args
= (u32
*)tmp_buf
;
846 data
->buf
= buffers
->buf
;
848 for (i
= 0; i
< fmt_size
; i
++) {
849 if ((!isprint(fmt
[i
]) && !isspace(fmt
[i
])) || !isascii(fmt
[i
])) {
857 if (fmt
[i
+ 1] == '%') {
862 if (num_spec
>= num_args
) {
867 /* The string is zero-terminated so if fmt[i] != 0, we can
868 * always access fmt[i + 1], in the worst case it will be a 0
872 /* skip optional "[0 +-][num]" width formatting field */
873 while (fmt
[i
] == '0' || fmt
[i
] == '+' || fmt
[i
] == '-' ||
876 if (fmt
[i
] >= '1' && fmt
[i
] <= '9') {
878 while (fmt
[i
] >= '0' && fmt
[i
] <= '9')
883 sizeof_cur_arg
= sizeof(long);
885 if ((fmt
[i
+ 1] == 'k' || fmt
[i
+ 1] == 'u') &&
887 fmt_ptype
= fmt
[i
+ 1];
892 if (fmt
[i
+ 1] == 0 || isspace(fmt
[i
+ 1]) ||
893 ispunct(fmt
[i
+ 1]) || fmt
[i
+ 1] == 'K' ||
894 fmt
[i
+ 1] == 'x' || fmt
[i
+ 1] == 's' ||
896 /* just kernel pointers */
898 cur_arg
= raw_args
[num_spec
];
903 if (fmt
[i
+ 1] == 'B') {
905 err
= snprintf(tmp_buf
,
906 (tmp_buf_end
- tmp_buf
),
908 (void *)(long)raw_args
[num_spec
]);
909 tmp_buf
+= (err
+ 1);
917 /* only support "%pI4", "%pi4", "%pI6" and "%pi6". */
918 if ((fmt
[i
+ 1] != 'i' && fmt
[i
+ 1] != 'I') ||
919 (fmt
[i
+ 2] != '4' && fmt
[i
+ 2] != '6')) {
928 sizeof_cur_ip
= (fmt
[i
] == '4') ? 4 : 16;
929 if (tmp_buf_end
- tmp_buf
< sizeof_cur_ip
) {
934 unsafe_ptr
= (char *)(long)raw_args
[num_spec
];
935 err
= copy_from_kernel_nofault(cur_ip
, unsafe_ptr
,
938 memset(cur_ip
, 0, sizeof_cur_ip
);
940 /* hack: bstr_printf expects IP addresses to be
941 * pre-formatted as strings, ironically, the easiest way
942 * to do that is to call snprintf.
944 ip_spec
[2] = fmt
[i
- 1];
946 err
= snprintf(tmp_buf
, tmp_buf_end
- tmp_buf
,
953 } else if (fmt
[i
] == 's') {
956 if (fmt
[i
+ 1] != 0 &&
957 !isspace(fmt
[i
+ 1]) &&
958 !ispunct(fmt
[i
+ 1])) {
966 if (tmp_buf_end
== tmp_buf
) {
971 unsafe_ptr
= (char *)(long)raw_args
[num_spec
];
972 err
= bpf_trace_copy_string(tmp_buf
, unsafe_ptr
,
974 tmp_buf_end
- tmp_buf
);
984 } else if (fmt
[i
] == 'c') {
988 if (tmp_buf_end
== tmp_buf
) {
993 *tmp_buf
= raw_args
[num_spec
];
1000 sizeof_cur_arg
= sizeof(int);
1002 if (fmt
[i
] == 'l') {
1003 sizeof_cur_arg
= sizeof(long);
1006 if (fmt
[i
] == 'l') {
1007 sizeof_cur_arg
= sizeof(long long);
1011 if (fmt
[i
] != 'i' && fmt
[i
] != 'd' && fmt
[i
] != 'u' &&
1012 fmt
[i
] != 'x' && fmt
[i
] != 'X') {
1018 cur_arg
= raw_args
[num_spec
];
1021 tmp_buf
= PTR_ALIGN(tmp_buf
, sizeof(u32
));
1022 if (tmp_buf_end
- tmp_buf
< sizeof_cur_arg
) {
1027 if (sizeof_cur_arg
== 8) {
1028 *(u32
*)tmp_buf
= *(u32
*)&cur_arg
;
1029 *(u32
*)(tmp_buf
+ 4) = *((u32
*)&cur_arg
+ 1);
1031 *(u32
*)tmp_buf
= (u32
)(long)cur_arg
;
1033 tmp_buf
+= sizeof_cur_arg
;
1041 bpf_bprintf_cleanup(data
);
1045 BPF_CALL_5(bpf_snprintf
, char *, str
, u32
, str_size
, char *, fmt
,
1046 const void *, args
, u32
, data_len
)
1048 struct bpf_bprintf_data data
= {
1049 .get_bin_args
= true,
1053 if (data_len
% 8 || data_len
> MAX_BPRINTF_VARARGS
* 8 ||
1054 (data_len
&& !args
))
1056 num_args
= data_len
/ 8;
1058 /* ARG_PTR_TO_CONST_STR guarantees that fmt is zero-terminated so we
1059 * can safely give an unbounded size.
1061 err
= bpf_bprintf_prepare(fmt
, UINT_MAX
, args
, num_args
, &data
);
1065 err
= bstr_printf(str
, str_size
, fmt
, data
.bin_args
);
1067 bpf_bprintf_cleanup(&data
);
1072 const struct bpf_func_proto bpf_snprintf_proto
= {
1073 .func
= bpf_snprintf
,
1075 .ret_type
= RET_INTEGER
,
1076 .arg1_type
= ARG_PTR_TO_MEM_OR_NULL
,
1077 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
1078 .arg3_type
= ARG_PTR_TO_CONST_STR
,
1079 .arg4_type
= ARG_PTR_TO_MEM
| PTR_MAYBE_NULL
| MEM_RDONLY
,
1080 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
1083 struct bpf_async_cb
{
1084 struct bpf_map
*map
;
1085 struct bpf_prog
*prog
;
1086 void __rcu
*callback_fn
;
1089 struct rcu_head rcu
;
1090 struct work_struct delete_work
;
1095 /* BPF map elements can contain 'struct bpf_timer'.
1096 * Such map owns all of its BPF timers.
1097 * 'struct bpf_timer' is allocated as part of map element allocation
1098 * and it's zero initialized.
1099 * That space is used to keep 'struct bpf_async_kern'.
1100 * bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and
1101 * remembers 'struct bpf_map *' pointer it's part of.
1102 * bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn.
1103 * bpf_timer_start() arms the timer.
1104 * If user space reference to a map goes to zero at this point
1105 * ops->map_release_uref callback is responsible for cancelling the timers,
1106 * freeing their memory, and decrementing prog's refcnts.
1107 * bpf_timer_cancel() cancels the timer and decrements prog's refcnt.
1108 * Inner maps can contain bpf timers as well. ops->map_release_uref is
1109 * freeing the timers when inner map is replaced or deleted by user space.
1111 struct bpf_hrtimer
{
1112 struct bpf_async_cb cb
;
1113 struct hrtimer timer
;
1114 atomic_t cancelling
;
1118 struct bpf_async_cb cb
;
1119 struct work_struct work
;
1120 struct work_struct delete_work
;
1123 /* the actual struct hidden inside uapi struct bpf_timer and bpf_wq */
1124 struct bpf_async_kern
{
1126 struct bpf_async_cb
*cb
;
1127 struct bpf_hrtimer
*timer
;
1128 struct bpf_work
*work
;
1130 /* bpf_spin_lock is used here instead of spinlock_t to make
1131 * sure that it always fits into space reserved by struct bpf_timer
1132 * regardless of LOCKDEP and spinlock debug flags.
1134 struct bpf_spin_lock lock
;
1135 } __attribute__((aligned(8)));
1137 enum bpf_async_type
{
1138 BPF_ASYNC_TYPE_TIMER
= 0,
1142 static DEFINE_PER_CPU(struct bpf_hrtimer
*, hrtimer_running
);
1144 static enum hrtimer_restart
bpf_timer_cb(struct hrtimer
*hrtimer
)
1146 struct bpf_hrtimer
*t
= container_of(hrtimer
, struct bpf_hrtimer
, timer
);
1147 struct bpf_map
*map
= t
->cb
.map
;
1148 void *value
= t
->cb
.value
;
1149 bpf_callback_t callback_fn
;
1153 BTF_TYPE_EMIT(struct bpf_timer
);
1154 callback_fn
= rcu_dereference_check(t
->cb
.callback_fn
, rcu_read_lock_bh_held());
1158 /* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
1159 * cannot be preempted by another bpf_timer_cb() on the same cpu.
1160 * Remember the timer this callback is servicing to prevent
1161 * deadlock if callback_fn() calls bpf_timer_cancel() or
1162 * bpf_map_delete_elem() on the same timer.
1164 this_cpu_write(hrtimer_running
, t
);
1165 if (map
->map_type
== BPF_MAP_TYPE_ARRAY
) {
1166 struct bpf_array
*array
= container_of(map
, struct bpf_array
, map
);
1168 /* compute the key */
1169 idx
= ((char *)value
- array
->value
) / array
->elem_size
;
1171 } else { /* hash or lru */
1172 key
= value
- round_up(map
->key_size
, 8);
1175 callback_fn((u64
)(long)map
, (u64
)(long)key
, (u64
)(long)value
, 0, 0);
1176 /* The verifier checked that return value is zero. */
1178 this_cpu_write(hrtimer_running
, NULL
);
1180 return HRTIMER_NORESTART
;
1183 static void bpf_wq_work(struct work_struct
*work
)
1185 struct bpf_work
*w
= container_of(work
, struct bpf_work
, work
);
1186 struct bpf_async_cb
*cb
= &w
->cb
;
1187 struct bpf_map
*map
= cb
->map
;
1188 bpf_callback_t callback_fn
;
1189 void *value
= cb
->value
;
1193 BTF_TYPE_EMIT(struct bpf_wq
);
1195 callback_fn
= READ_ONCE(cb
->callback_fn
);
1199 if (map
->map_type
== BPF_MAP_TYPE_ARRAY
) {
1200 struct bpf_array
*array
= container_of(map
, struct bpf_array
, map
);
1202 /* compute the key */
1203 idx
= ((char *)value
- array
->value
) / array
->elem_size
;
1205 } else { /* hash or lru */
1206 key
= value
- round_up(map
->key_size
, 8);
1209 rcu_read_lock_trace();
1212 callback_fn((u64
)(long)map
, (u64
)(long)key
, (u64
)(long)value
, 0, 0);
1215 rcu_read_unlock_trace();
1218 static void bpf_wq_delete_work(struct work_struct
*work
)
1220 struct bpf_work
*w
= container_of(work
, struct bpf_work
, delete_work
);
1222 cancel_work_sync(&w
->work
);
1224 kfree_rcu(w
, cb
.rcu
);
1227 static void bpf_timer_delete_work(struct work_struct
*work
)
1229 struct bpf_hrtimer
*t
= container_of(work
, struct bpf_hrtimer
, cb
.delete_work
);
1231 /* Cancel the timer and wait for callback to complete if it was running.
1232 * If hrtimer_cancel() can be safely called it's safe to call
1233 * kfree_rcu(t) right after for both preallocated and non-preallocated
1234 * maps. The async->cb = NULL was already done and no code path can see
1235 * address 't' anymore. Timer if armed for existing bpf_hrtimer before
1236 * bpf_timer_cancel_and_free will have been cancelled.
1238 hrtimer_cancel(&t
->timer
);
1239 kfree_rcu(t
, cb
.rcu
);
1242 static int __bpf_async_init(struct bpf_async_kern
*async
, struct bpf_map
*map
, u64 flags
,
1243 enum bpf_async_type type
)
1245 struct bpf_async_cb
*cb
;
1246 struct bpf_hrtimer
*t
;
1256 case BPF_ASYNC_TYPE_TIMER
:
1257 size
= sizeof(struct bpf_hrtimer
);
1259 case BPF_ASYNC_TYPE_WQ
:
1260 size
= sizeof(struct bpf_work
);
1266 __bpf_spin_lock_irqsave(&async
->lock
);
1273 /* allocate hrtimer via map_kmalloc to use memcg accounting */
1274 cb
= bpf_map_kmalloc_node(map
, size
, GFP_ATOMIC
, map
->numa_node
);
1281 case BPF_ASYNC_TYPE_TIMER
:
1282 clockid
= flags
& (MAX_CLOCKS
- 1);
1283 t
= (struct bpf_hrtimer
*)cb
;
1285 atomic_set(&t
->cancelling
, 0);
1286 INIT_WORK(&t
->cb
.delete_work
, bpf_timer_delete_work
);
1287 hrtimer_init(&t
->timer
, clockid
, HRTIMER_MODE_REL_SOFT
);
1288 t
->timer
.function
= bpf_timer_cb
;
1289 cb
->value
= (void *)async
- map
->record
->timer_off
;
1291 case BPF_ASYNC_TYPE_WQ
:
1292 w
= (struct bpf_work
*)cb
;
1294 INIT_WORK(&w
->work
, bpf_wq_work
);
1295 INIT_WORK(&w
->delete_work
, bpf_wq_delete_work
);
1296 cb
->value
= (void *)async
- map
->record
->wq_off
;
1302 rcu_assign_pointer(cb
->callback_fn
, NULL
);
1304 WRITE_ONCE(async
->cb
, cb
);
1305 /* Guarantee the order between async->cb and map->usercnt. So
1306 * when there are concurrent uref release and bpf timer init, either
1307 * bpf_timer_cancel_and_free() called by uref release reads a no-NULL
1308 * timer or atomic64_read() below returns a zero usercnt.
1311 if (!atomic64_read(&map
->usercnt
)) {
1312 /* maps with timers must be either held by user space
1313 * or pinned in bpffs.
1315 WRITE_ONCE(async
->cb
, NULL
);
1320 __bpf_spin_unlock_irqrestore(&async
->lock
);
1324 BPF_CALL_3(bpf_timer_init
, struct bpf_async_kern
*, timer
, struct bpf_map
*, map
,
1327 clock_t clockid
= flags
& (MAX_CLOCKS
- 1);
1329 BUILD_BUG_ON(MAX_CLOCKS
!= 16);
1330 BUILD_BUG_ON(sizeof(struct bpf_async_kern
) > sizeof(struct bpf_timer
));
1331 BUILD_BUG_ON(__alignof__(struct bpf_async_kern
) != __alignof__(struct bpf_timer
));
1333 if (flags
>= MAX_CLOCKS
||
1334 /* similar to timerfd except _ALARM variants are not supported */
1335 (clockid
!= CLOCK_MONOTONIC
&&
1336 clockid
!= CLOCK_REALTIME
&&
1337 clockid
!= CLOCK_BOOTTIME
))
1340 return __bpf_async_init(timer
, map
, flags
, BPF_ASYNC_TYPE_TIMER
);
1343 static const struct bpf_func_proto bpf_timer_init_proto
= {
1344 .func
= bpf_timer_init
,
1346 .ret_type
= RET_INTEGER
,
1347 .arg1_type
= ARG_PTR_TO_TIMER
,
1348 .arg2_type
= ARG_CONST_MAP_PTR
,
1349 .arg3_type
= ARG_ANYTHING
,
1352 static int __bpf_async_set_callback(struct bpf_async_kern
*async
, void *callback_fn
,
1353 struct bpf_prog_aux
*aux
, unsigned int flags
,
1354 enum bpf_async_type type
)
1356 struct bpf_prog
*prev
, *prog
= aux
->prog
;
1357 struct bpf_async_cb
*cb
;
1362 __bpf_spin_lock_irqsave(&async
->lock
);
1368 if (!atomic64_read(&cb
->map
->usercnt
)) {
1369 /* maps with timers must be either held by user space
1370 * or pinned in bpffs. Otherwise timer might still be
1371 * running even when bpf prog is detached and user space
1372 * is gone, since map_release_uref won't ever be called.
1379 /* Bump prog refcnt once. Every bpf_timer_set_callback()
1380 * can pick different callback_fn-s within the same prog.
1382 prog
= bpf_prog_inc_not_zero(prog
);
1384 ret
= PTR_ERR(prog
);
1388 /* Drop prev prog refcnt when swapping with new prog */
1392 rcu_assign_pointer(cb
->callback_fn
, callback_fn
);
1394 __bpf_spin_unlock_irqrestore(&async
->lock
);
1398 BPF_CALL_3(bpf_timer_set_callback
, struct bpf_async_kern
*, timer
, void *, callback_fn
,
1399 struct bpf_prog_aux
*, aux
)
1401 return __bpf_async_set_callback(timer
, callback_fn
, aux
, 0, BPF_ASYNC_TYPE_TIMER
);
1404 static const struct bpf_func_proto bpf_timer_set_callback_proto
= {
1405 .func
= bpf_timer_set_callback
,
1407 .ret_type
= RET_INTEGER
,
1408 .arg1_type
= ARG_PTR_TO_TIMER
,
1409 .arg2_type
= ARG_PTR_TO_FUNC
,
1412 BPF_CALL_3(bpf_timer_start
, struct bpf_async_kern
*, timer
, u64
, nsecs
, u64
, flags
)
1414 struct bpf_hrtimer
*t
;
1416 enum hrtimer_mode mode
;
1420 if (flags
& ~(BPF_F_TIMER_ABS
| BPF_F_TIMER_CPU_PIN
))
1422 __bpf_spin_lock_irqsave(&timer
->lock
);
1424 if (!t
|| !t
->cb
.prog
) {
1429 if (flags
& BPF_F_TIMER_ABS
)
1430 mode
= HRTIMER_MODE_ABS_SOFT
;
1432 mode
= HRTIMER_MODE_REL_SOFT
;
1434 if (flags
& BPF_F_TIMER_CPU_PIN
)
1435 mode
|= HRTIMER_MODE_PINNED
;
1437 hrtimer_start(&t
->timer
, ns_to_ktime(nsecs
), mode
);
1439 __bpf_spin_unlock_irqrestore(&timer
->lock
);
1443 static const struct bpf_func_proto bpf_timer_start_proto
= {
1444 .func
= bpf_timer_start
,
1446 .ret_type
= RET_INTEGER
,
1447 .arg1_type
= ARG_PTR_TO_TIMER
,
1448 .arg2_type
= ARG_ANYTHING
,
1449 .arg3_type
= ARG_ANYTHING
,
1452 static void drop_prog_refcnt(struct bpf_async_cb
*async
)
1454 struct bpf_prog
*prog
= async
->prog
;
1459 rcu_assign_pointer(async
->callback_fn
, NULL
);
1463 BPF_CALL_1(bpf_timer_cancel
, struct bpf_async_kern
*, timer
)
1465 struct bpf_hrtimer
*t
, *cur_t
;
1472 __bpf_spin_lock_irqsave(&timer
->lock
);
1479 cur_t
= this_cpu_read(hrtimer_running
);
1481 /* If bpf callback_fn is trying to bpf_timer_cancel()
1482 * its own timer the hrtimer_cancel() will deadlock
1483 * since it waits for callback_fn to finish.
1489 /* Only account in-flight cancellations when invoked from a timer
1490 * callback, since we want to avoid waiting only if other _callbacks_
1491 * are waiting on us, to avoid introducing lockups. Non-callback paths
1492 * are ok, since nobody would synchronously wait for their completion.
1496 atomic_inc(&t
->cancelling
);
1497 /* Need full barrier after relaxed atomic_inc */
1498 smp_mb__after_atomic();
1500 if (atomic_read(&cur_t
->cancelling
)) {
1501 /* We're cancelling timer t, while some other timer callback is
1502 * attempting to cancel us. In such a case, it might be possible
1503 * that timer t belongs to the other callback, or some other
1504 * callback waiting upon it (creating transitive dependencies
1505 * upon us), and we will enter a deadlock if we continue
1506 * cancelling and waiting for it synchronously, since it might
1507 * do the same. Bail!
1513 drop_prog_refcnt(&t
->cb
);
1515 __bpf_spin_unlock_irqrestore(&timer
->lock
);
1516 /* Cancel the timer and wait for associated callback to finish
1517 * if it was running.
1519 ret
= ret
?: hrtimer_cancel(&t
->timer
);
1521 atomic_dec(&t
->cancelling
);
1526 static const struct bpf_func_proto bpf_timer_cancel_proto
= {
1527 .func
= bpf_timer_cancel
,
1529 .ret_type
= RET_INTEGER
,
1530 .arg1_type
= ARG_PTR_TO_TIMER
,
1533 static struct bpf_async_cb
*__bpf_async_cancel_and_free(struct bpf_async_kern
*async
)
1535 struct bpf_async_cb
*cb
;
1537 /* Performance optimization: read async->cb without lock first. */
1538 if (!READ_ONCE(async
->cb
))
1541 __bpf_spin_lock_irqsave(&async
->lock
);
1542 /* re-read it under lock */
1546 drop_prog_refcnt(cb
);
1547 /* The subsequent bpf_timer_start/cancel() helpers won't be able to use
1548 * this timer, since it won't be initialized.
1550 WRITE_ONCE(async
->cb
, NULL
);
1552 __bpf_spin_unlock_irqrestore(&async
->lock
);
1556 /* This function is called by map_delete/update_elem for individual element and
1557 * by ops->map_release_uref when the user space reference to a map reaches zero.
1559 void bpf_timer_cancel_and_free(void *val
)
1561 struct bpf_hrtimer
*t
;
1563 t
= (struct bpf_hrtimer
*)__bpf_async_cancel_and_free(val
);
1567 /* We check that bpf_map_delete/update_elem() was called from timer
1568 * callback_fn. In such case we don't call hrtimer_cancel() (since it
1569 * will deadlock) and don't call hrtimer_try_to_cancel() (since it will
1570 * just return -1). Though callback_fn is still running on this cpu it's
1571 * safe to do kfree(t) because bpf_timer_cb() read everything it needed
1572 * from 't'. The bpf subprog callback_fn won't be able to access 't',
1573 * since async->cb = NULL was already done. The timer will be
1574 * effectively cancelled because bpf_timer_cb() will return
1575 * HRTIMER_NORESTART.
1577 * However, it is possible the timer callback_fn calling us armed the
1578 * timer _before_ calling us, such that failing to cancel it here will
1579 * cause it to possibly use struct hrtimer after freeing bpf_hrtimer.
1580 * Therefore, we _need_ to cancel any outstanding timers before we do
1581 * kfree_rcu, even though no more timers can be armed.
1583 * Moreover, we need to schedule work even if timer does not belong to
1584 * the calling callback_fn, as on two different CPUs, we can end up in a
1585 * situation where both sides run in parallel, try to cancel one
1586 * another, and we end up waiting on both sides in hrtimer_cancel
1587 * without making forward progress, since timer1 depends on time2
1588 * callback to finish, and vice versa.
1590 * CPU 1 (timer1_cb) CPU 2 (timer2_cb)
1591 * bpf_timer_cancel_and_free(timer2) bpf_timer_cancel_and_free(timer1)
1593 * To avoid these issues, punt to workqueue context when we are in a
1596 if (this_cpu_read(hrtimer_running
))
1597 queue_work(system_unbound_wq
, &t
->cb
.delete_work
);
1599 bpf_timer_delete_work(&t
->cb
.delete_work
);
1602 /* This function is called by map_delete/update_elem for individual element and
1603 * by ops->map_release_uref when the user space reference to a map reaches zero.
1605 void bpf_wq_cancel_and_free(void *val
)
1607 struct bpf_work
*work
;
1609 BTF_TYPE_EMIT(struct bpf_wq
);
1611 work
= (struct bpf_work
*)__bpf_async_cancel_and_free(val
);
1614 /* Trigger cancel of the sleepable work, but *do not* wait for
1615 * it to finish if it was running as we might not be in a
1616 * sleepable context.
1617 * kfree will be called once the work has finished.
1619 schedule_work(&work
->delete_work
);
1622 BPF_CALL_2(bpf_kptr_xchg
, void *, dst
, void *, ptr
)
1624 unsigned long *kptr
= dst
;
1626 /* This helper may be inlined by verifier. */
1627 return xchg(kptr
, (unsigned long)ptr
);
1630 /* Unlike other PTR_TO_BTF_ID helpers the btf_id in bpf_kptr_xchg()
1631 * helper is determined dynamically by the verifier. Use BPF_PTR_POISON to
1632 * denote type that verifier will determine.
1634 static const struct bpf_func_proto bpf_kptr_xchg_proto
= {
1635 .func
= bpf_kptr_xchg
,
1637 .ret_type
= RET_PTR_TO_BTF_ID_OR_NULL
,
1638 .ret_btf_id
= BPF_PTR_POISON
,
1639 .arg1_type
= ARG_KPTR_XCHG_DEST
,
1640 .arg2_type
= ARG_PTR_TO_BTF_ID_OR_NULL
| OBJ_RELEASE
,
1641 .arg2_btf_id
= BPF_PTR_POISON
,
1644 /* Since the upper 8 bits of dynptr->size is reserved, the
1645 * maximum supported size is 2^24 - 1.
1647 #define DYNPTR_MAX_SIZE ((1UL << 24) - 1)
1648 #define DYNPTR_TYPE_SHIFT 28
1649 #define DYNPTR_SIZE_MASK 0xFFFFFF
1650 #define DYNPTR_RDONLY_BIT BIT(31)
1652 bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern
*ptr
)
1654 return ptr
->size
& DYNPTR_RDONLY_BIT
;
1657 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern
*ptr
)
1659 ptr
->size
|= DYNPTR_RDONLY_BIT
;
1662 static void bpf_dynptr_set_type(struct bpf_dynptr_kern
*ptr
, enum bpf_dynptr_type type
)
1664 ptr
->size
|= type
<< DYNPTR_TYPE_SHIFT
;
1667 static enum bpf_dynptr_type
bpf_dynptr_get_type(const struct bpf_dynptr_kern
*ptr
)
1669 return (ptr
->size
& ~(DYNPTR_RDONLY_BIT
)) >> DYNPTR_TYPE_SHIFT
;
1672 u32
__bpf_dynptr_size(const struct bpf_dynptr_kern
*ptr
)
1674 return ptr
->size
& DYNPTR_SIZE_MASK
;
1677 static void bpf_dynptr_set_size(struct bpf_dynptr_kern
*ptr
, u32 new_size
)
1679 u32 metadata
= ptr
->size
& ~DYNPTR_SIZE_MASK
;
1681 ptr
->size
= new_size
| metadata
;
1684 int bpf_dynptr_check_size(u32 size
)
1686 return size
> DYNPTR_MAX_SIZE
? -E2BIG
: 0;
1689 void bpf_dynptr_init(struct bpf_dynptr_kern
*ptr
, void *data
,
1690 enum bpf_dynptr_type type
, u32 offset
, u32 size
)
1693 ptr
->offset
= offset
;
1695 bpf_dynptr_set_type(ptr
, type
);
1698 void bpf_dynptr_set_null(struct bpf_dynptr_kern
*ptr
)
1700 memset(ptr
, 0, sizeof(*ptr
));
1703 static int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern
*ptr
, u32 offset
, u32 len
)
1705 u32 size
= __bpf_dynptr_size(ptr
);
1707 if (len
> size
|| offset
> size
- len
)
1713 BPF_CALL_4(bpf_dynptr_from_mem
, void *, data
, u32
, size
, u64
, flags
, struct bpf_dynptr_kern
*, ptr
)
1717 BTF_TYPE_EMIT(struct bpf_dynptr
);
1719 err
= bpf_dynptr_check_size(size
);
1723 /* flags is currently unsupported */
1729 bpf_dynptr_init(ptr
, data
, BPF_DYNPTR_TYPE_LOCAL
, 0, size
);
1734 bpf_dynptr_set_null(ptr
);
1738 static const struct bpf_func_proto bpf_dynptr_from_mem_proto
= {
1739 .func
= bpf_dynptr_from_mem
,
1741 .ret_type
= RET_INTEGER
,
1742 .arg1_type
= ARG_PTR_TO_UNINIT_MEM
,
1743 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
1744 .arg3_type
= ARG_ANYTHING
,
1745 .arg4_type
= ARG_PTR_TO_DYNPTR
| DYNPTR_TYPE_LOCAL
| MEM_UNINIT
| MEM_WRITE
,
1748 BPF_CALL_5(bpf_dynptr_read
, void *, dst
, u32
, len
, const struct bpf_dynptr_kern
*, src
,
1749 u32
, offset
, u64
, flags
)
1751 enum bpf_dynptr_type type
;
1754 if (!src
->data
|| flags
)
1757 err
= bpf_dynptr_check_off_len(src
, offset
, len
);
1761 type
= bpf_dynptr_get_type(src
);
1764 case BPF_DYNPTR_TYPE_LOCAL
:
1765 case BPF_DYNPTR_TYPE_RINGBUF
:
1766 /* Source and destination may possibly overlap, hence use memmove to
1767 * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr
1768 * pointing to overlapping PTR_TO_MAP_VALUE regions.
1770 memmove(dst
, src
->data
+ src
->offset
+ offset
, len
);
1772 case BPF_DYNPTR_TYPE_SKB
:
1773 return __bpf_skb_load_bytes(src
->data
, src
->offset
+ offset
, dst
, len
);
1774 case BPF_DYNPTR_TYPE_XDP
:
1775 return __bpf_xdp_load_bytes(src
->data
, src
->offset
+ offset
, dst
, len
);
1777 WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type
);
1782 static const struct bpf_func_proto bpf_dynptr_read_proto
= {
1783 .func
= bpf_dynptr_read
,
1785 .ret_type
= RET_INTEGER
,
1786 .arg1_type
= ARG_PTR_TO_UNINIT_MEM
,
1787 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
1788 .arg3_type
= ARG_PTR_TO_DYNPTR
| MEM_RDONLY
,
1789 .arg4_type
= ARG_ANYTHING
,
1790 .arg5_type
= ARG_ANYTHING
,
1793 BPF_CALL_5(bpf_dynptr_write
, const struct bpf_dynptr_kern
*, dst
, u32
, offset
, void *, src
,
1794 u32
, len
, u64
, flags
)
1796 enum bpf_dynptr_type type
;
1799 if (!dst
->data
|| __bpf_dynptr_is_rdonly(dst
))
1802 err
= bpf_dynptr_check_off_len(dst
, offset
, len
);
1806 type
= bpf_dynptr_get_type(dst
);
1809 case BPF_DYNPTR_TYPE_LOCAL
:
1810 case BPF_DYNPTR_TYPE_RINGBUF
:
1813 /* Source and destination may possibly overlap, hence use memmove to
1814 * copy the data. E.g. bpf_dynptr_from_mem may create two dynptr
1815 * pointing to overlapping PTR_TO_MAP_VALUE regions.
1817 memmove(dst
->data
+ dst
->offset
+ offset
, src
, len
);
1819 case BPF_DYNPTR_TYPE_SKB
:
1820 return __bpf_skb_store_bytes(dst
->data
, dst
->offset
+ offset
, src
, len
,
1822 case BPF_DYNPTR_TYPE_XDP
:
1825 return __bpf_xdp_store_bytes(dst
->data
, dst
->offset
+ offset
, src
, len
);
1827 WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type
);
1832 static const struct bpf_func_proto bpf_dynptr_write_proto
= {
1833 .func
= bpf_dynptr_write
,
1835 .ret_type
= RET_INTEGER
,
1836 .arg1_type
= ARG_PTR_TO_DYNPTR
| MEM_RDONLY
,
1837 .arg2_type
= ARG_ANYTHING
,
1838 .arg3_type
= ARG_PTR_TO_MEM
| MEM_RDONLY
,
1839 .arg4_type
= ARG_CONST_SIZE_OR_ZERO
,
1840 .arg5_type
= ARG_ANYTHING
,
1843 BPF_CALL_3(bpf_dynptr_data
, const struct bpf_dynptr_kern
*, ptr
, u32
, offset
, u32
, len
)
1845 enum bpf_dynptr_type type
;
1851 err
= bpf_dynptr_check_off_len(ptr
, offset
, len
);
1855 if (__bpf_dynptr_is_rdonly(ptr
))
1858 type
= bpf_dynptr_get_type(ptr
);
1861 case BPF_DYNPTR_TYPE_LOCAL
:
1862 case BPF_DYNPTR_TYPE_RINGBUF
:
1863 return (unsigned long)(ptr
->data
+ ptr
->offset
+ offset
);
1864 case BPF_DYNPTR_TYPE_SKB
:
1865 case BPF_DYNPTR_TYPE_XDP
:
1866 /* skb and xdp dynptrs should use bpf_dynptr_slice / bpf_dynptr_slice_rdwr */
1869 WARN_ONCE(true, "bpf_dynptr_data: unknown dynptr type %d\n", type
);
1874 static const struct bpf_func_proto bpf_dynptr_data_proto
= {
1875 .func
= bpf_dynptr_data
,
1877 .ret_type
= RET_PTR_TO_DYNPTR_MEM_OR_NULL
,
1878 .arg1_type
= ARG_PTR_TO_DYNPTR
| MEM_RDONLY
,
1879 .arg2_type
= ARG_ANYTHING
,
1880 .arg3_type
= ARG_CONST_ALLOC_SIZE_OR_ZERO
,
1883 const struct bpf_func_proto bpf_get_current_task_proto __weak
;
1884 const struct bpf_func_proto bpf_get_current_task_btf_proto __weak
;
1885 const struct bpf_func_proto bpf_probe_read_user_proto __weak
;
1886 const struct bpf_func_proto bpf_probe_read_user_str_proto __weak
;
1887 const struct bpf_func_proto bpf_probe_read_kernel_proto __weak
;
1888 const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak
;
1889 const struct bpf_func_proto bpf_task_pt_regs_proto __weak
;
1891 const struct bpf_func_proto
*
1892 bpf_base_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
1895 case BPF_FUNC_map_lookup_elem
:
1896 return &bpf_map_lookup_elem_proto
;
1897 case BPF_FUNC_map_update_elem
:
1898 return &bpf_map_update_elem_proto
;
1899 case BPF_FUNC_map_delete_elem
:
1900 return &bpf_map_delete_elem_proto
;
1901 case BPF_FUNC_map_push_elem
:
1902 return &bpf_map_push_elem_proto
;
1903 case BPF_FUNC_map_pop_elem
:
1904 return &bpf_map_pop_elem_proto
;
1905 case BPF_FUNC_map_peek_elem
:
1906 return &bpf_map_peek_elem_proto
;
1907 case BPF_FUNC_map_lookup_percpu_elem
:
1908 return &bpf_map_lookup_percpu_elem_proto
;
1909 case BPF_FUNC_get_prandom_u32
:
1910 return &bpf_get_prandom_u32_proto
;
1911 case BPF_FUNC_get_smp_processor_id
:
1912 return &bpf_get_raw_smp_processor_id_proto
;
1913 case BPF_FUNC_get_numa_node_id
:
1914 return &bpf_get_numa_node_id_proto
;
1915 case BPF_FUNC_tail_call
:
1916 return &bpf_tail_call_proto
;
1917 case BPF_FUNC_ktime_get_ns
:
1918 return &bpf_ktime_get_ns_proto
;
1919 case BPF_FUNC_ktime_get_boot_ns
:
1920 return &bpf_ktime_get_boot_ns_proto
;
1921 case BPF_FUNC_ktime_get_tai_ns
:
1922 return &bpf_ktime_get_tai_ns_proto
;
1923 case BPF_FUNC_ringbuf_output
:
1924 return &bpf_ringbuf_output_proto
;
1925 case BPF_FUNC_ringbuf_reserve
:
1926 return &bpf_ringbuf_reserve_proto
;
1927 case BPF_FUNC_ringbuf_submit
:
1928 return &bpf_ringbuf_submit_proto
;
1929 case BPF_FUNC_ringbuf_discard
:
1930 return &bpf_ringbuf_discard_proto
;
1931 case BPF_FUNC_ringbuf_query
:
1932 return &bpf_ringbuf_query_proto
;
1933 case BPF_FUNC_strncmp
:
1934 return &bpf_strncmp_proto
;
1935 case BPF_FUNC_strtol
:
1936 return &bpf_strtol_proto
;
1937 case BPF_FUNC_strtoul
:
1938 return &bpf_strtoul_proto
;
1939 case BPF_FUNC_get_current_pid_tgid
:
1940 return &bpf_get_current_pid_tgid_proto
;
1941 case BPF_FUNC_get_ns_current_pid_tgid
:
1942 return &bpf_get_ns_current_pid_tgid_proto
;
1947 if (!bpf_token_capable(prog
->aux
->token
, CAP_BPF
))
1951 case BPF_FUNC_spin_lock
:
1952 return &bpf_spin_lock_proto
;
1953 case BPF_FUNC_spin_unlock
:
1954 return &bpf_spin_unlock_proto
;
1955 case BPF_FUNC_jiffies64
:
1956 return &bpf_jiffies64_proto
;
1957 case BPF_FUNC_per_cpu_ptr
:
1958 return &bpf_per_cpu_ptr_proto
;
1959 case BPF_FUNC_this_cpu_ptr
:
1960 return &bpf_this_cpu_ptr_proto
;
1961 case BPF_FUNC_timer_init
:
1962 return &bpf_timer_init_proto
;
1963 case BPF_FUNC_timer_set_callback
:
1964 return &bpf_timer_set_callback_proto
;
1965 case BPF_FUNC_timer_start
:
1966 return &bpf_timer_start_proto
;
1967 case BPF_FUNC_timer_cancel
:
1968 return &bpf_timer_cancel_proto
;
1969 case BPF_FUNC_kptr_xchg
:
1970 return &bpf_kptr_xchg_proto
;
1971 case BPF_FUNC_for_each_map_elem
:
1972 return &bpf_for_each_map_elem_proto
;
1974 return &bpf_loop_proto
;
1975 case BPF_FUNC_user_ringbuf_drain
:
1976 return &bpf_user_ringbuf_drain_proto
;
1977 case BPF_FUNC_ringbuf_reserve_dynptr
:
1978 return &bpf_ringbuf_reserve_dynptr_proto
;
1979 case BPF_FUNC_ringbuf_submit_dynptr
:
1980 return &bpf_ringbuf_submit_dynptr_proto
;
1981 case BPF_FUNC_ringbuf_discard_dynptr
:
1982 return &bpf_ringbuf_discard_dynptr_proto
;
1983 case BPF_FUNC_dynptr_from_mem
:
1984 return &bpf_dynptr_from_mem_proto
;
1985 case BPF_FUNC_dynptr_read
:
1986 return &bpf_dynptr_read_proto
;
1987 case BPF_FUNC_dynptr_write
:
1988 return &bpf_dynptr_write_proto
;
1989 case BPF_FUNC_dynptr_data
:
1990 return &bpf_dynptr_data_proto
;
1991 #ifdef CONFIG_CGROUPS
1992 case BPF_FUNC_cgrp_storage_get
:
1993 return &bpf_cgrp_storage_get_proto
;
1994 case BPF_FUNC_cgrp_storage_delete
:
1995 return &bpf_cgrp_storage_delete_proto
;
1996 case BPF_FUNC_get_current_cgroup_id
:
1997 return &bpf_get_current_cgroup_id_proto
;
1998 case BPF_FUNC_get_current_ancestor_cgroup_id
:
1999 return &bpf_get_current_ancestor_cgroup_id_proto
;
2005 if (!bpf_token_capable(prog
->aux
->token
, CAP_PERFMON
))
2009 case BPF_FUNC_trace_printk
:
2010 return bpf_get_trace_printk_proto();
2011 case BPF_FUNC_get_current_task
:
2012 return &bpf_get_current_task_proto
;
2013 case BPF_FUNC_get_current_task_btf
:
2014 return &bpf_get_current_task_btf_proto
;
2015 case BPF_FUNC_probe_read_user
:
2016 return &bpf_probe_read_user_proto
;
2017 case BPF_FUNC_probe_read_kernel
:
2018 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL
) < 0 ?
2019 NULL
: &bpf_probe_read_kernel_proto
;
2020 case BPF_FUNC_probe_read_user_str
:
2021 return &bpf_probe_read_user_str_proto
;
2022 case BPF_FUNC_probe_read_kernel_str
:
2023 return security_locked_down(LOCKDOWN_BPF_READ_KERNEL
) < 0 ?
2024 NULL
: &bpf_probe_read_kernel_str_proto
;
2025 case BPF_FUNC_snprintf_btf
:
2026 return &bpf_snprintf_btf_proto
;
2027 case BPF_FUNC_snprintf
:
2028 return &bpf_snprintf_proto
;
2029 case BPF_FUNC_task_pt_regs
:
2030 return &bpf_task_pt_regs_proto
;
2031 case BPF_FUNC_trace_vprintk
:
2032 return bpf_get_trace_vprintk_proto();
2037 EXPORT_SYMBOL_GPL(bpf_base_func_proto
);
2039 void bpf_list_head_free(const struct btf_field
*field
, void *list_head
,
2040 struct bpf_spin_lock
*spin_lock
)
2042 struct list_head
*head
= list_head
, *orig_head
= list_head
;
2044 BUILD_BUG_ON(sizeof(struct list_head
) > sizeof(struct bpf_list_head
));
2045 BUILD_BUG_ON(__alignof__(struct list_head
) > __alignof__(struct bpf_list_head
));
2047 /* Do the actual list draining outside the lock to not hold the lock for
2048 * too long, and also prevent deadlocks if tracing programs end up
2049 * executing on entry/exit of functions called inside the critical
2050 * section, and end up doing map ops that call bpf_list_head_free for
2051 * the same map value again.
2053 __bpf_spin_lock_irqsave(spin_lock
);
2054 if (!head
->next
|| list_empty(head
))
2058 INIT_LIST_HEAD(orig_head
);
2059 __bpf_spin_unlock_irqrestore(spin_lock
);
2061 while (head
!= orig_head
) {
2064 obj
-= field
->graph_root
.node_offset
;
2066 /* The contained type can also have resources, including a
2067 * bpf_list_head which needs to be freed.
2070 __bpf_obj_drop_impl(obj
, field
->graph_root
.value_rec
, false);
2075 /* Like rbtree_postorder_for_each_entry_safe, but 'pos' and 'n' are
2076 * 'rb_node *', so field name of rb_node within containing struct is not
2079 * Since bpf_rb_tree's node type has a corresponding struct btf_field with
2080 * graph_root.node_offset, it's not necessary to know field name
2081 * or type of node struct
2083 #define bpf_rbtree_postorder_for_each_entry_safe(pos, n, root) \
2084 for (pos = rb_first_postorder(root); \
2085 pos && ({ n = rb_next_postorder(pos); 1; }); \
2088 void bpf_rb_root_free(const struct btf_field
*field
, void *rb_root
,
2089 struct bpf_spin_lock
*spin_lock
)
2091 struct rb_root_cached orig_root
, *root
= rb_root
;
2092 struct rb_node
*pos
, *n
;
2095 BUILD_BUG_ON(sizeof(struct rb_root_cached
) > sizeof(struct bpf_rb_root
));
2096 BUILD_BUG_ON(__alignof__(struct rb_root_cached
) > __alignof__(struct bpf_rb_root
));
2098 __bpf_spin_lock_irqsave(spin_lock
);
2100 *root
= RB_ROOT_CACHED
;
2101 __bpf_spin_unlock_irqrestore(spin_lock
);
2103 bpf_rbtree_postorder_for_each_entry_safe(pos
, n
, &orig_root
.rb_root
) {
2105 obj
-= field
->graph_root
.node_offset
;
2109 __bpf_obj_drop_impl(obj
, field
->graph_root
.value_rec
, false);
2114 __bpf_kfunc_start_defs();
2116 __bpf_kfunc
void *bpf_obj_new_impl(u64 local_type_id__k
, void *meta__ign
)
2118 struct btf_struct_meta
*meta
= meta__ign
;
2119 u64 size
= local_type_id__k
;
2122 p
= bpf_mem_alloc(&bpf_global_ma
, size
);
2126 bpf_obj_init(meta
->record
, p
);
2130 __bpf_kfunc
void *bpf_percpu_obj_new_impl(u64 local_type_id__k
, void *meta__ign
)
2132 u64 size
= local_type_id__k
;
2134 /* The verifier has ensured that meta__ign must be NULL */
2135 return bpf_mem_alloc(&bpf_global_percpu_ma
, size
);
2138 /* Must be called under migrate_disable(), as required by bpf_mem_free */
2139 void __bpf_obj_drop_impl(void *p
, const struct btf_record
*rec
, bool percpu
)
2141 struct bpf_mem_alloc
*ma
;
2143 if (rec
&& rec
->refcount_off
>= 0 &&
2144 !refcount_dec_and_test((refcount_t
*)(p
+ rec
->refcount_off
))) {
2145 /* Object is refcounted and refcount_dec didn't result in 0
2146 * refcount. Return without freeing the object
2152 bpf_obj_free_fields(rec
, p
);
2155 ma
= &bpf_global_percpu_ma
;
2157 ma
= &bpf_global_ma
;
2158 bpf_mem_free_rcu(ma
, p
);
2161 __bpf_kfunc
void bpf_obj_drop_impl(void *p__alloc
, void *meta__ign
)
2163 struct btf_struct_meta
*meta
= meta__ign
;
2166 __bpf_obj_drop_impl(p
, meta
? meta
->record
: NULL
, false);
2169 __bpf_kfunc
void bpf_percpu_obj_drop_impl(void *p__alloc
, void *meta__ign
)
2171 /* The verifier has ensured that meta__ign must be NULL */
2172 bpf_mem_free_rcu(&bpf_global_percpu_ma
, p__alloc
);
2175 __bpf_kfunc
void *bpf_refcount_acquire_impl(void *p__refcounted_kptr
, void *meta__ign
)
2177 struct btf_struct_meta
*meta
= meta__ign
;
2178 struct bpf_refcount
*ref
;
2180 /* Could just cast directly to refcount_t *, but need some code using
2181 * bpf_refcount type so that it is emitted in vmlinux BTF
2183 ref
= (struct bpf_refcount
*)(p__refcounted_kptr
+ meta
->record
->refcount_off
);
2184 if (!refcount_inc_not_zero((refcount_t
*)ref
))
2187 /* Verifier strips KF_RET_NULL if input is owned ref, see is_kfunc_ret_null
2190 return (void *)p__refcounted_kptr
;
2193 static int __bpf_list_add(struct bpf_list_node_kern
*node
,
2194 struct bpf_list_head
*head
,
2195 bool tail
, struct btf_record
*rec
, u64 off
)
2197 struct list_head
*n
= &node
->list_head
, *h
= (void *)head
;
2199 /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't
2200 * called on its fields, so init here
2202 if (unlikely(!h
->next
))
2205 /* node->owner != NULL implies !list_empty(n), no need to separately
2208 if (cmpxchg(&node
->owner
, NULL
, BPF_PTR_POISON
)) {
2209 /* Only called from BPF prog, no need to migrate_disable */
2210 __bpf_obj_drop_impl((void *)n
- off
, rec
, false);
2214 tail
? list_add_tail(n
, h
) : list_add(n
, h
);
2215 WRITE_ONCE(node
->owner
, head
);
2220 __bpf_kfunc
int bpf_list_push_front_impl(struct bpf_list_head
*head
,
2221 struct bpf_list_node
*node
,
2222 void *meta__ign
, u64 off
)
2224 struct bpf_list_node_kern
*n
= (void *)node
;
2225 struct btf_struct_meta
*meta
= meta__ign
;
2227 return __bpf_list_add(n
, head
, false, meta
? meta
->record
: NULL
, off
);
2230 __bpf_kfunc
int bpf_list_push_back_impl(struct bpf_list_head
*head
,
2231 struct bpf_list_node
*node
,
2232 void *meta__ign
, u64 off
)
2234 struct bpf_list_node_kern
*n
= (void *)node
;
2235 struct btf_struct_meta
*meta
= meta__ign
;
2237 return __bpf_list_add(n
, head
, true, meta
? meta
->record
: NULL
, off
);
2240 static struct bpf_list_node
*__bpf_list_del(struct bpf_list_head
*head
, bool tail
)
2242 struct list_head
*n
, *h
= (void *)head
;
2243 struct bpf_list_node_kern
*node
;
2245 /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't
2246 * called on its fields, so init here
2248 if (unlikely(!h
->next
))
2253 n
= tail
? h
->prev
: h
->next
;
2254 node
= container_of(n
, struct bpf_list_node_kern
, list_head
);
2255 if (WARN_ON_ONCE(READ_ONCE(node
->owner
) != head
))
2259 WRITE_ONCE(node
->owner
, NULL
);
2260 return (struct bpf_list_node
*)n
;
2263 __bpf_kfunc
struct bpf_list_node
*bpf_list_pop_front(struct bpf_list_head
*head
)
2265 return __bpf_list_del(head
, false);
2268 __bpf_kfunc
struct bpf_list_node
*bpf_list_pop_back(struct bpf_list_head
*head
)
2270 return __bpf_list_del(head
, true);
2273 __bpf_kfunc
struct bpf_rb_node
*bpf_rbtree_remove(struct bpf_rb_root
*root
,
2274 struct bpf_rb_node
*node
)
2276 struct bpf_rb_node_kern
*node_internal
= (struct bpf_rb_node_kern
*)node
;
2277 struct rb_root_cached
*r
= (struct rb_root_cached
*)root
;
2278 struct rb_node
*n
= &node_internal
->rb_node
;
2280 /* node_internal->owner != root implies either RB_EMPTY_NODE(n) or
2281 * n is owned by some other tree. No need to check RB_EMPTY_NODE(n)
2283 if (READ_ONCE(node_internal
->owner
) != root
)
2286 rb_erase_cached(n
, r
);
2288 WRITE_ONCE(node_internal
->owner
, NULL
);
2289 return (struct bpf_rb_node
*)n
;
2292 /* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF
2295 static int __bpf_rbtree_add(struct bpf_rb_root
*root
,
2296 struct bpf_rb_node_kern
*node
,
2297 void *less
, struct btf_record
*rec
, u64 off
)
2299 struct rb_node
**link
= &((struct rb_root_cached
*)root
)->rb_root
.rb_node
;
2300 struct rb_node
*parent
= NULL
, *n
= &node
->rb_node
;
2301 bpf_callback_t cb
= (bpf_callback_t
)less
;
2302 bool leftmost
= true;
2304 /* node->owner != NULL implies !RB_EMPTY_NODE(n), no need to separately
2307 if (cmpxchg(&node
->owner
, NULL
, BPF_PTR_POISON
)) {
2308 /* Only called from BPF prog, no need to migrate_disable */
2309 __bpf_obj_drop_impl((void *)n
- off
, rec
, false);
2315 if (cb((uintptr_t)node
, (uintptr_t)parent
, 0, 0, 0)) {
2316 link
= &parent
->rb_left
;
2318 link
= &parent
->rb_right
;
2323 rb_link_node(n
, parent
, link
);
2324 rb_insert_color_cached(n
, (struct rb_root_cached
*)root
, leftmost
);
2325 WRITE_ONCE(node
->owner
, root
);
2329 __bpf_kfunc
int bpf_rbtree_add_impl(struct bpf_rb_root
*root
, struct bpf_rb_node
*node
,
2330 bool (less
)(struct bpf_rb_node
*a
, const struct bpf_rb_node
*b
),
2331 void *meta__ign
, u64 off
)
2333 struct btf_struct_meta
*meta
= meta__ign
;
2334 struct bpf_rb_node_kern
*n
= (void *)node
;
2336 return __bpf_rbtree_add(root
, n
, (void *)less
, meta
? meta
->record
: NULL
, off
);
2339 __bpf_kfunc
struct bpf_rb_node
*bpf_rbtree_first(struct bpf_rb_root
*root
)
2341 struct rb_root_cached
*r
= (struct rb_root_cached
*)root
;
2343 return (struct bpf_rb_node
*)rb_first_cached(r
);
2347 * bpf_task_acquire - Acquire a reference to a task. A task acquired by this
2348 * kfunc which is not stored in a map as a kptr, must be released by calling
2349 * bpf_task_release().
2350 * @p: The task on which a reference is being acquired.
2352 __bpf_kfunc
struct task_struct
*bpf_task_acquire(struct task_struct
*p
)
2354 if (refcount_inc_not_zero(&p
->rcu_users
))
2360 * bpf_task_release - Release the reference acquired on a task.
2361 * @p: The task on which a reference is being released.
2363 __bpf_kfunc
void bpf_task_release(struct task_struct
*p
)
2365 put_task_struct_rcu_user(p
);
2368 __bpf_kfunc
void bpf_task_release_dtor(void *p
)
2370 put_task_struct_rcu_user(p
);
2372 CFI_NOSEAL(bpf_task_release_dtor
);
2374 #ifdef CONFIG_CGROUPS
2376 * bpf_cgroup_acquire - Acquire a reference to a cgroup. A cgroup acquired by
2377 * this kfunc which is not stored in a map as a kptr, must be released by
2378 * calling bpf_cgroup_release().
2379 * @cgrp: The cgroup on which a reference is being acquired.
2381 __bpf_kfunc
struct cgroup
*bpf_cgroup_acquire(struct cgroup
*cgrp
)
2383 return cgroup_tryget(cgrp
) ? cgrp
: NULL
;
2387 * bpf_cgroup_release - Release the reference acquired on a cgroup.
2388 * If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to
2389 * not be freed until the current grace period has ended, even if its refcount
2391 * @cgrp: The cgroup on which a reference is being released.
2393 __bpf_kfunc
void bpf_cgroup_release(struct cgroup
*cgrp
)
2398 __bpf_kfunc
void bpf_cgroup_release_dtor(void *cgrp
)
2402 CFI_NOSEAL(bpf_cgroup_release_dtor
);
2405 * bpf_cgroup_ancestor - Perform a lookup on an entry in a cgroup's ancestor
2406 * array. A cgroup returned by this kfunc which is not subsequently stored in a
2407 * map, must be released by calling bpf_cgroup_release().
2408 * @cgrp: The cgroup for which we're performing a lookup.
2409 * @level: The level of ancestor to look up.
2411 __bpf_kfunc
struct cgroup
*bpf_cgroup_ancestor(struct cgroup
*cgrp
, int level
)
2413 struct cgroup
*ancestor
;
2415 if (level
> cgrp
->level
|| level
< 0)
2418 /* cgrp's refcnt could be 0 here, but ancestors can still be accessed */
2419 ancestor
= cgrp
->ancestors
[level
];
2420 if (!cgroup_tryget(ancestor
))
2426 * bpf_cgroup_from_id - Find a cgroup from its ID. A cgroup returned by this
2427 * kfunc which is not subsequently stored in a map, must be released by calling
2428 * bpf_cgroup_release().
2431 __bpf_kfunc
struct cgroup
*bpf_cgroup_from_id(u64 cgid
)
2433 struct cgroup
*cgrp
;
2435 cgrp
= cgroup_get_from_id(cgid
);
2442 * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test
2443 * task's membership of cgroup ancestry.
2444 * @task: the task to be tested
2445 * @ancestor: possible ancestor of @task's cgroup
2447 * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
2448 * It follows all the same rules as cgroup_is_descendant, and only applies
2449 * to the default hierarchy.
2451 __bpf_kfunc
long bpf_task_under_cgroup(struct task_struct
*task
,
2452 struct cgroup
*ancestor
)
2457 ret
= task_under_cgroup_hierarchy(task
, ancestor
);
2462 BPF_CALL_2(bpf_current_task_under_cgroup
, struct bpf_map
*, map
, u32
, idx
)
2464 struct bpf_array
*array
= container_of(map
, struct bpf_array
, map
);
2465 struct cgroup
*cgrp
;
2467 if (unlikely(idx
>= array
->map
.max_entries
))
2470 cgrp
= READ_ONCE(array
->ptrs
[idx
]);
2471 if (unlikely(!cgrp
))
2474 return task_under_cgroup_hierarchy(current
, cgrp
);
2477 const struct bpf_func_proto bpf_current_task_under_cgroup_proto
= {
2478 .func
= bpf_current_task_under_cgroup
,
2480 .ret_type
= RET_INTEGER
,
2481 .arg1_type
= ARG_CONST_MAP_PTR
,
2482 .arg2_type
= ARG_ANYTHING
,
2486 * bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a
2487 * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its
2489 * @task: The target task
2490 * @hierarchy_id: The ID of a cgroup1 hierarchy
2492 * On success, the cgroup is returen. On failure, NULL is returned.
2494 __bpf_kfunc
struct cgroup
*
2495 bpf_task_get_cgroup1(struct task_struct
*task
, int hierarchy_id
)
2497 struct cgroup
*cgrp
= task_get_cgroup1(task
, hierarchy_id
);
2503 #endif /* CONFIG_CGROUPS */
2506 * bpf_task_from_pid - Find a struct task_struct from its pid by looking it up
2507 * in the root pid namespace idr. If a task is returned, it must either be
2508 * stored in a map, or released with bpf_task_release().
2509 * @pid: The pid of the task being looked up.
2511 __bpf_kfunc
struct task_struct
*bpf_task_from_pid(s32 pid
)
2513 struct task_struct
*p
;
2516 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
2518 p
= bpf_task_acquire(p
);
2525 * bpf_task_from_vpid - Find a struct task_struct from its vpid by looking it up
2526 * in the pid namespace of the current task. If a task is returned, it must
2527 * either be stored in a map, or released with bpf_task_release().
2528 * @vpid: The vpid of the task being looked up.
2530 __bpf_kfunc
struct task_struct
*bpf_task_from_vpid(s32 vpid
)
2532 struct task_struct
*p
;
2535 p
= find_task_by_vpid(vpid
);
2537 p
= bpf_task_acquire(p
);
2544 * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data.
2545 * @p: The dynptr whose data slice to retrieve
2546 * @offset: Offset into the dynptr
2547 * @buffer__opt: User-provided buffer to copy contents into. May be NULL
2548 * @buffer__szk: Size (in bytes) of the buffer if present. This is the
2549 * length of the requested slice. This must be a constant.
2551 * For non-skb and non-xdp type dynptrs, there is no difference between
2552 * bpf_dynptr_slice and bpf_dynptr_data.
2554 * If buffer__opt is NULL, the call will fail if buffer_opt was needed.
2556 * If the intention is to write to the data slice, please use
2557 * bpf_dynptr_slice_rdwr.
2559 * The user must check that the returned pointer is not null before using it.
2561 * Please note that in the case of skb and xdp dynptrs, bpf_dynptr_slice
2562 * does not change the underlying packet data pointers, so a call to
2563 * bpf_dynptr_slice will not invalidate any ctx->data/data_end pointers in
2566 * Return: NULL if the call failed (eg invalid dynptr), pointer to a read-only
2567 * data slice (can be either direct pointer to the data or a pointer to the user
2568 * provided buffer, with its contents containing the data, if unable to obtain
2571 __bpf_kfunc
void *bpf_dynptr_slice(const struct bpf_dynptr
*p
, u32 offset
,
2572 void *buffer__opt
, u32 buffer__szk
)
2574 const struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2575 enum bpf_dynptr_type type
;
2576 u32 len
= buffer__szk
;
2582 err
= bpf_dynptr_check_off_len(ptr
, offset
, len
);
2586 type
= bpf_dynptr_get_type(ptr
);
2589 case BPF_DYNPTR_TYPE_LOCAL
:
2590 case BPF_DYNPTR_TYPE_RINGBUF
:
2591 return ptr
->data
+ ptr
->offset
+ offset
;
2592 case BPF_DYNPTR_TYPE_SKB
:
2594 return skb_header_pointer(ptr
->data
, ptr
->offset
+ offset
, len
, buffer__opt
);
2596 return skb_pointer_if_linear(ptr
->data
, ptr
->offset
+ offset
, len
);
2597 case BPF_DYNPTR_TYPE_XDP
:
2599 void *xdp_ptr
= bpf_xdp_pointer(ptr
->data
, ptr
->offset
+ offset
, len
);
2600 if (!IS_ERR_OR_NULL(xdp_ptr
))
2605 bpf_xdp_copy_buf(ptr
->data
, ptr
->offset
+ offset
, buffer__opt
, len
, false);
2609 WARN_ONCE(true, "unknown dynptr type %d\n", type
);
2615 * bpf_dynptr_slice_rdwr() - Obtain a writable pointer to the dynptr data.
2616 * @p: The dynptr whose data slice to retrieve
2617 * @offset: Offset into the dynptr
2618 * @buffer__opt: User-provided buffer to copy contents into. May be NULL
2619 * @buffer__szk: Size (in bytes) of the buffer if present. This is the
2620 * length of the requested slice. This must be a constant.
2622 * For non-skb and non-xdp type dynptrs, there is no difference between
2623 * bpf_dynptr_slice and bpf_dynptr_data.
2625 * If buffer__opt is NULL, the call will fail if buffer_opt was needed.
2627 * The returned pointer is writable and may point to either directly the dynptr
2628 * data at the requested offset or to the buffer if unable to obtain a direct
2629 * data pointer to (example: the requested slice is to the paged area of an skb
2630 * packet). In the case where the returned pointer is to the buffer, the user
2631 * is responsible for persisting writes through calling bpf_dynptr_write(). This
2632 * usually looks something like this pattern:
2634 * struct eth_hdr *eth = bpf_dynptr_slice_rdwr(&dynptr, 0, buffer, sizeof(buffer));
2636 * return TC_ACT_SHOT;
2638 * // mutate eth header //
2640 * if (eth == buffer)
2641 * bpf_dynptr_write(&ptr, 0, buffer, sizeof(buffer), 0);
2643 * Please note that, as in the example above, the user must check that the
2644 * returned pointer is not null before using it.
2646 * Please also note that in the case of skb and xdp dynptrs, bpf_dynptr_slice_rdwr
2647 * does not change the underlying packet data pointers, so a call to
2648 * bpf_dynptr_slice_rdwr will not invalidate any ctx->data/data_end pointers in
2651 * Return: NULL if the call failed (eg invalid dynptr), pointer to a
2652 * data slice (can be either direct pointer to the data or a pointer to the user
2653 * provided buffer, with its contents containing the data, if unable to obtain
2656 __bpf_kfunc
void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr
*p
, u32 offset
,
2657 void *buffer__opt
, u32 buffer__szk
)
2659 const struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2661 if (!ptr
->data
|| __bpf_dynptr_is_rdonly(ptr
))
2664 /* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
2666 * For skb-type dynptrs, it is safe to write into the returned pointer
2667 * if the bpf program allows skb data writes. There are two possibilities
2668 * that may occur when calling bpf_dynptr_slice_rdwr:
2670 * 1) The requested slice is in the head of the skb. In this case, the
2671 * returned pointer is directly to skb data, and if the skb is cloned, the
2672 * verifier will have uncloned it (see bpf_unclone_prologue()) already.
2673 * The pointer can be directly written into.
2675 * 2) Some portion of the requested slice is in the paged buffer area.
2676 * In this case, the requested data will be copied out into the buffer
2677 * and the returned pointer will be a pointer to the buffer. The skb
2678 * will not be pulled. To persist the write, the user will need to call
2679 * bpf_dynptr_write(), which will pull the skb and commit the write.
2681 * Similarly for xdp programs, if the requested slice is not across xdp
2682 * fragments, then a direct pointer will be returned, otherwise the data
2683 * will be copied out into the buffer and the user will need to call
2684 * bpf_dynptr_write() to commit changes.
2686 return bpf_dynptr_slice(p
, offset
, buffer__opt
, buffer__szk
);
2689 __bpf_kfunc
int bpf_dynptr_adjust(const struct bpf_dynptr
*p
, u32 start
, u32 end
)
2691 struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2694 if (!ptr
->data
|| start
> end
)
2697 size
= __bpf_dynptr_size(ptr
);
2699 if (start
> size
|| end
> size
)
2702 ptr
->offset
+= start
;
2703 bpf_dynptr_set_size(ptr
, end
- start
);
2708 __bpf_kfunc
bool bpf_dynptr_is_null(const struct bpf_dynptr
*p
)
2710 struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2715 __bpf_kfunc
bool bpf_dynptr_is_rdonly(const struct bpf_dynptr
*p
)
2717 struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2722 return __bpf_dynptr_is_rdonly(ptr
);
2725 __bpf_kfunc __u32
bpf_dynptr_size(const struct bpf_dynptr
*p
)
2727 struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2732 return __bpf_dynptr_size(ptr
);
2735 __bpf_kfunc
int bpf_dynptr_clone(const struct bpf_dynptr
*p
,
2736 struct bpf_dynptr
*clone__uninit
)
2738 struct bpf_dynptr_kern
*clone
= (struct bpf_dynptr_kern
*)clone__uninit
;
2739 struct bpf_dynptr_kern
*ptr
= (struct bpf_dynptr_kern
*)p
;
2742 bpf_dynptr_set_null(clone
);
2751 __bpf_kfunc
void *bpf_cast_to_kern_ctx(void *obj
)
2756 __bpf_kfunc
void *bpf_rdonly_cast(const void *obj__ign
, u32 btf_id__k
)
2758 return (void *)obj__ign
;
2761 __bpf_kfunc
void bpf_rcu_read_lock(void)
2766 __bpf_kfunc
void bpf_rcu_read_unlock(void)
2771 struct bpf_throw_ctx
{
2772 struct bpf_prog_aux
*aux
;
2778 static bool bpf_stack_walker(void *cookie
, u64 ip
, u64 sp
, u64 bp
)
2780 struct bpf_throw_ctx
*ctx
= cookie
;
2781 struct bpf_prog
*prog
;
2783 if (!is_bpf_text_address(ip
))
2785 prog
= bpf_prog_ksym_find(ip
);
2787 if (bpf_is_subprog(prog
))
2789 ctx
->aux
= prog
->aux
;
2795 __bpf_kfunc
void bpf_throw(u64 cookie
)
2797 struct bpf_throw_ctx ctx
= {};
2799 arch_bpf_stack_walk(bpf_stack_walker
, &ctx
);
2800 WARN_ON_ONCE(!ctx
.aux
);
2802 WARN_ON_ONCE(!ctx
.aux
->exception_boundary
);
2803 WARN_ON_ONCE(!ctx
.bp
);
2804 WARN_ON_ONCE(!ctx
.cnt
);
2805 /* Prevent KASAN false positives for CONFIG_KASAN_STACK by unpoisoning
2806 * deeper stack depths than ctx.sp as we do not return from bpf_throw,
2807 * which skips compiler generated instrumentation to do the same.
2809 kasan_unpoison_task_stack_below((void *)(long)ctx
.sp
);
2810 ctx
.aux
->bpf_exception_cb(cookie
, ctx
.sp
, ctx
.bp
, 0, 0);
2811 WARN(1, "A call to BPF exception callback should never return\n");
2814 __bpf_kfunc
int bpf_wq_init(struct bpf_wq
*wq
, void *p__map
, unsigned int flags
)
2816 struct bpf_async_kern
*async
= (struct bpf_async_kern
*)wq
;
2817 struct bpf_map
*map
= p__map
;
2819 BUILD_BUG_ON(sizeof(struct bpf_async_kern
) > sizeof(struct bpf_wq
));
2820 BUILD_BUG_ON(__alignof__(struct bpf_async_kern
) != __alignof__(struct bpf_wq
));
2825 return __bpf_async_init(async
, map
, flags
, BPF_ASYNC_TYPE_WQ
);
2828 __bpf_kfunc
int bpf_wq_start(struct bpf_wq
*wq
, unsigned int flags
)
2830 struct bpf_async_kern
*async
= (struct bpf_async_kern
*)wq
;
2837 w
= READ_ONCE(async
->work
);
2838 if (!w
|| !READ_ONCE(w
->cb
.prog
))
2841 schedule_work(&w
->work
);
2845 __bpf_kfunc
int bpf_wq_set_callback_impl(struct bpf_wq
*wq
,
2846 int (callback_fn
)(void *map
, int *key
, void *value
),
2850 struct bpf_prog_aux
*aux
= (struct bpf_prog_aux
*)aux__ign
;
2851 struct bpf_async_kern
*async
= (struct bpf_async_kern
*)wq
;
2856 return __bpf_async_set_callback(async
, callback_fn
, aux
, flags
, BPF_ASYNC_TYPE_WQ
);
2859 __bpf_kfunc
void bpf_preempt_disable(void)
2864 __bpf_kfunc
void bpf_preempt_enable(void)
2869 struct bpf_iter_bits
{
2873 #define BITS_ITER_NR_WORDS_MAX 511
2875 struct bpf_iter_bits_kern
{
2884 /* On 64-bit hosts, unsigned long and u64 have the same size, so passing
2885 * a u64 pointer and an unsigned long pointer to find_next_bit() will
2886 * return the same result, as both point to the same 8-byte area.
2888 * For 32-bit little-endian hosts, using a u64 pointer or unsigned long
2889 * pointer also makes no difference. This is because the first iterated
2890 * unsigned long is composed of bits 0-31 of the u64 and the second unsigned
2891 * long is composed of bits 32-63 of the u64.
2893 * However, for 32-bit big-endian hosts, this is not the case. The first
2894 * iterated unsigned long will be bits 32-63 of the u64, so swap these two
2895 * ulong values within the u64.
2897 static void swap_ulong_in_u64(u64
*bits
, unsigned int nr
)
2899 #if (BITS_PER_LONG == 32) && defined(__BIG_ENDIAN)
2902 for (i
= 0; i
< nr
; i
++)
2903 bits
[i
] = (bits
[i
] >> 32) | ((u64
)(u32
)bits
[i
] << 32);
2908 * bpf_iter_bits_new() - Initialize a new bits iterator for a given memory area
2909 * @it: The new bpf_iter_bits to be created
2910 * @unsafe_ptr__ign: A pointer pointing to a memory area to be iterated over
2911 * @nr_words: The size of the specified memory area, measured in 8-byte units.
2912 * The maximum value of @nr_words is @BITS_ITER_NR_WORDS_MAX. This limit may be
2913 * further reduced by the BPF memory allocator implementation.
2915 * This function initializes a new bpf_iter_bits structure for iterating over
2916 * a memory area which is specified by the @unsafe_ptr__ign and @nr_words. It
2917 * copies the data of the memory area to the newly created bpf_iter_bits @it for
2918 * subsequent iteration operations.
2920 * On success, 0 is returned. On failure, ERR is returned.
2923 bpf_iter_bits_new(struct bpf_iter_bits
*it
, const u64
*unsafe_ptr__ign
, u32 nr_words
)
2925 struct bpf_iter_bits_kern
*kit
= (void *)it
;
2926 u32 nr_bytes
= nr_words
* sizeof(u64
);
2927 u32 nr_bits
= BYTES_TO_BITS(nr_bytes
);
2930 BUILD_BUG_ON(sizeof(struct bpf_iter_bits_kern
) != sizeof(struct bpf_iter_bits
));
2931 BUILD_BUG_ON(__alignof__(struct bpf_iter_bits_kern
) !=
2932 __alignof__(struct bpf_iter_bits
));
2938 if (!unsafe_ptr__ign
|| !nr_words
)
2940 if (nr_words
> BITS_ITER_NR_WORDS_MAX
)
2943 /* Optimization for u64 mask */
2944 if (nr_bits
== 64) {
2945 err
= bpf_probe_read_kernel_common(&kit
->bits_copy
, nr_bytes
, unsafe_ptr__ign
);
2949 swap_ulong_in_u64(&kit
->bits_copy
, nr_words
);
2951 kit
->nr_bits
= nr_bits
;
2955 if (bpf_mem_alloc_check_size(false, nr_bytes
))
2958 /* Fallback to memalloc */
2959 kit
->bits
= bpf_mem_alloc(&bpf_global_ma
, nr_bytes
);
2963 err
= bpf_probe_read_kernel_common(kit
->bits
, nr_bytes
, unsafe_ptr__ign
);
2965 bpf_mem_free(&bpf_global_ma
, kit
->bits
);
2969 swap_ulong_in_u64(kit
->bits
, nr_words
);
2971 kit
->nr_bits
= nr_bits
;
2976 * bpf_iter_bits_next() - Get the next bit in a bpf_iter_bits
2977 * @it: The bpf_iter_bits to be checked
2979 * This function returns a pointer to a number representing the value of the
2980 * next bit in the bits.
2982 * If there are no further bits available, it returns NULL.
2984 __bpf_kfunc
int *bpf_iter_bits_next(struct bpf_iter_bits
*it
)
2986 struct bpf_iter_bits_kern
*kit
= (void *)it
;
2987 int bit
= kit
->bit
, nr_bits
= kit
->nr_bits
;
2990 if (!nr_bits
|| bit
>= nr_bits
)
2993 bits
= nr_bits
== 64 ? &kit
->bits_copy
: kit
->bits
;
2994 bit
= find_next_bit(bits
, nr_bits
, bit
+ 1);
2995 if (bit
>= nr_bits
) {
3005 * bpf_iter_bits_destroy() - Destroy a bpf_iter_bits
3006 * @it: The bpf_iter_bits to be destroyed
3008 * Destroy the resource associated with the bpf_iter_bits.
3010 __bpf_kfunc
void bpf_iter_bits_destroy(struct bpf_iter_bits
*it
)
3012 struct bpf_iter_bits_kern
*kit
= (void *)it
;
3014 if (kit
->nr_bits
<= 64)
3016 bpf_mem_free(&bpf_global_ma
, kit
->bits
);
3020 * bpf_copy_from_user_str() - Copy a string from an unsafe user address
3021 * @dst: Destination address, in kernel space. This buffer must be
3022 * at least @dst__sz bytes long.
3023 * @dst__sz: Maximum number of bytes to copy, includes the trailing NUL.
3024 * @unsafe_ptr__ign: Source address, in user space.
3025 * @flags: The only supported flag is BPF_F_PAD_ZEROS
3027 * Copies a NUL-terminated string from userspace to BPF space. If user string is
3028 * too long this will still ensure zero termination in the dst buffer unless
3031 * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst to 0 on success and
3032 * memset all of @dst on failure.
3034 __bpf_kfunc
int bpf_copy_from_user_str(void *dst
, u32 dst__sz
, const void __user
*unsafe_ptr__ign
, u64 flags
)
3038 if (unlikely(flags
& ~BPF_F_PAD_ZEROS
))
3041 if (unlikely(!dst__sz
))
3044 ret
= strncpy_from_user(dst
, unsafe_ptr__ign
, dst__sz
- 1);
3046 if (flags
& BPF_F_PAD_ZEROS
)
3047 memset((char *)dst
, 0, dst__sz
);
3052 if (flags
& BPF_F_PAD_ZEROS
)
3053 memset((char *)dst
+ ret
, 0, dst__sz
- ret
);
3055 ((char *)dst
)[ret
] = '\0';
3060 __bpf_kfunc_end_defs();
3062 BTF_KFUNCS_START(generic_btf_ids
)
3063 #ifdef CONFIG_CRASH_DUMP
3064 BTF_ID_FLAGS(func
, crash_kexec
, KF_DESTRUCTIVE
)
3066 BTF_ID_FLAGS(func
, bpf_obj_new_impl
, KF_ACQUIRE
| KF_RET_NULL
)
3067 BTF_ID_FLAGS(func
, bpf_percpu_obj_new_impl
, KF_ACQUIRE
| KF_RET_NULL
)
3068 BTF_ID_FLAGS(func
, bpf_obj_drop_impl
, KF_RELEASE
)
3069 BTF_ID_FLAGS(func
, bpf_percpu_obj_drop_impl
, KF_RELEASE
)
3070 BTF_ID_FLAGS(func
, bpf_refcount_acquire_impl
, KF_ACQUIRE
| KF_RET_NULL
| KF_RCU
)
3071 BTF_ID_FLAGS(func
, bpf_list_push_front_impl
)
3072 BTF_ID_FLAGS(func
, bpf_list_push_back_impl
)
3073 BTF_ID_FLAGS(func
, bpf_list_pop_front
, KF_ACQUIRE
| KF_RET_NULL
)
3074 BTF_ID_FLAGS(func
, bpf_list_pop_back
, KF_ACQUIRE
| KF_RET_NULL
)
3075 BTF_ID_FLAGS(func
, bpf_task_acquire
, KF_ACQUIRE
| KF_RCU
| KF_RET_NULL
)
3076 BTF_ID_FLAGS(func
, bpf_task_release
, KF_RELEASE
)
3077 BTF_ID_FLAGS(func
, bpf_rbtree_remove
, KF_ACQUIRE
| KF_RET_NULL
)
3078 BTF_ID_FLAGS(func
, bpf_rbtree_add_impl
)
3079 BTF_ID_FLAGS(func
, bpf_rbtree_first
, KF_RET_NULL
)
3081 #ifdef CONFIG_CGROUPS
3082 BTF_ID_FLAGS(func
, bpf_cgroup_acquire
, KF_ACQUIRE
| KF_RCU
| KF_RET_NULL
)
3083 BTF_ID_FLAGS(func
, bpf_cgroup_release
, KF_RELEASE
)
3084 BTF_ID_FLAGS(func
, bpf_cgroup_ancestor
, KF_ACQUIRE
| KF_RCU
| KF_RET_NULL
)
3085 BTF_ID_FLAGS(func
, bpf_cgroup_from_id
, KF_ACQUIRE
| KF_RET_NULL
)
3086 BTF_ID_FLAGS(func
, bpf_task_under_cgroup
, KF_RCU
)
3087 BTF_ID_FLAGS(func
, bpf_task_get_cgroup1
, KF_ACQUIRE
| KF_RCU
| KF_RET_NULL
)
3089 BTF_ID_FLAGS(func
, bpf_task_from_pid
, KF_ACQUIRE
| KF_RET_NULL
)
3090 BTF_ID_FLAGS(func
, bpf_task_from_vpid
, KF_ACQUIRE
| KF_RET_NULL
)
3091 BTF_ID_FLAGS(func
, bpf_throw
)
3092 BTF_ID_FLAGS(func
, bpf_send_signal_task
, KF_TRUSTED_ARGS
)
3093 BTF_KFUNCS_END(generic_btf_ids
)
3095 static const struct btf_kfunc_id_set generic_kfunc_set
= {
3096 .owner
= THIS_MODULE
,
3097 .set
= &generic_btf_ids
,
3101 BTF_ID_LIST(generic_dtor_ids
)
3102 BTF_ID(struct, task_struct
)
3103 BTF_ID(func
, bpf_task_release_dtor
)
3104 #ifdef CONFIG_CGROUPS
3105 BTF_ID(struct, cgroup
)
3106 BTF_ID(func
, bpf_cgroup_release_dtor
)
3109 BTF_KFUNCS_START(common_btf_ids
)
3110 BTF_ID_FLAGS(func
, bpf_cast_to_kern_ctx
, KF_FASTCALL
)
3111 BTF_ID_FLAGS(func
, bpf_rdonly_cast
, KF_FASTCALL
)
3112 BTF_ID_FLAGS(func
, bpf_rcu_read_lock
)
3113 BTF_ID_FLAGS(func
, bpf_rcu_read_unlock
)
3114 BTF_ID_FLAGS(func
, bpf_dynptr_slice
, KF_RET_NULL
)
3115 BTF_ID_FLAGS(func
, bpf_dynptr_slice_rdwr
, KF_RET_NULL
)
3116 BTF_ID_FLAGS(func
, bpf_iter_num_new
, KF_ITER_NEW
)
3117 BTF_ID_FLAGS(func
, bpf_iter_num_next
, KF_ITER_NEXT
| KF_RET_NULL
)
3118 BTF_ID_FLAGS(func
, bpf_iter_num_destroy
, KF_ITER_DESTROY
)
3119 BTF_ID_FLAGS(func
, bpf_iter_task_vma_new
, KF_ITER_NEW
| KF_RCU
)
3120 BTF_ID_FLAGS(func
, bpf_iter_task_vma_next
, KF_ITER_NEXT
| KF_RET_NULL
)
3121 BTF_ID_FLAGS(func
, bpf_iter_task_vma_destroy
, KF_ITER_DESTROY
)
3122 #ifdef CONFIG_CGROUPS
3123 BTF_ID_FLAGS(func
, bpf_iter_css_task_new
, KF_ITER_NEW
| KF_TRUSTED_ARGS
)
3124 BTF_ID_FLAGS(func
, bpf_iter_css_task_next
, KF_ITER_NEXT
| KF_RET_NULL
)
3125 BTF_ID_FLAGS(func
, bpf_iter_css_task_destroy
, KF_ITER_DESTROY
)
3126 BTF_ID_FLAGS(func
, bpf_iter_css_new
, KF_ITER_NEW
| KF_TRUSTED_ARGS
| KF_RCU_PROTECTED
)
3127 BTF_ID_FLAGS(func
, bpf_iter_css_next
, KF_ITER_NEXT
| KF_RET_NULL
)
3128 BTF_ID_FLAGS(func
, bpf_iter_css_destroy
, KF_ITER_DESTROY
)
3130 BTF_ID_FLAGS(func
, bpf_iter_task_new
, KF_ITER_NEW
| KF_TRUSTED_ARGS
| KF_RCU_PROTECTED
)
3131 BTF_ID_FLAGS(func
, bpf_iter_task_next
, KF_ITER_NEXT
| KF_RET_NULL
)
3132 BTF_ID_FLAGS(func
, bpf_iter_task_destroy
, KF_ITER_DESTROY
)
3133 BTF_ID_FLAGS(func
, bpf_dynptr_adjust
)
3134 BTF_ID_FLAGS(func
, bpf_dynptr_is_null
)
3135 BTF_ID_FLAGS(func
, bpf_dynptr_is_rdonly
)
3136 BTF_ID_FLAGS(func
, bpf_dynptr_size
)
3137 BTF_ID_FLAGS(func
, bpf_dynptr_clone
)
3138 BTF_ID_FLAGS(func
, bpf_modify_return_test_tp
)
3139 BTF_ID_FLAGS(func
, bpf_wq_init
)
3140 BTF_ID_FLAGS(func
, bpf_wq_set_callback_impl
)
3141 BTF_ID_FLAGS(func
, bpf_wq_start
)
3142 BTF_ID_FLAGS(func
, bpf_preempt_disable
)
3143 BTF_ID_FLAGS(func
, bpf_preempt_enable
)
3144 BTF_ID_FLAGS(func
, bpf_iter_bits_new
, KF_ITER_NEW
)
3145 BTF_ID_FLAGS(func
, bpf_iter_bits_next
, KF_ITER_NEXT
| KF_RET_NULL
)
3146 BTF_ID_FLAGS(func
, bpf_iter_bits_destroy
, KF_ITER_DESTROY
)
3147 BTF_ID_FLAGS(func
, bpf_copy_from_user_str
, KF_SLEEPABLE
)
3148 BTF_ID_FLAGS(func
, bpf_get_kmem_cache
)
3149 BTF_ID_FLAGS(func
, bpf_iter_kmem_cache_new
, KF_ITER_NEW
| KF_SLEEPABLE
)
3150 BTF_ID_FLAGS(func
, bpf_iter_kmem_cache_next
, KF_ITER_NEXT
| KF_RET_NULL
| KF_SLEEPABLE
)
3151 BTF_ID_FLAGS(func
, bpf_iter_kmem_cache_destroy
, KF_ITER_DESTROY
| KF_SLEEPABLE
)
3152 BTF_KFUNCS_END(common_btf_ids
)
3154 static const struct btf_kfunc_id_set common_kfunc_set
= {
3155 .owner
= THIS_MODULE
,
3156 .set
= &common_btf_ids
,
3159 static int __init
kfunc_init(void)
3162 const struct btf_id_dtor_kfunc generic_dtors
[] = {
3164 .btf_id
= generic_dtor_ids
[0],
3165 .kfunc_btf_id
= generic_dtor_ids
[1]
3167 #ifdef CONFIG_CGROUPS
3169 .btf_id
= generic_dtor_ids
[2],
3170 .kfunc_btf_id
= generic_dtor_ids
[3]
3175 ret
= register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING
, &generic_kfunc_set
);
3176 ret
= ret
?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS
, &generic_kfunc_set
);
3177 ret
= ret
?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP
, &generic_kfunc_set
);
3178 ret
= ret
?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS
, &generic_kfunc_set
);
3179 ret
= ret
?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL
, &generic_kfunc_set
);
3180 ret
= ret
?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB
, &generic_kfunc_set
);
3181 ret
= ret
?: register_btf_id_dtor_kfuncs(generic_dtors
,
3182 ARRAY_SIZE(generic_dtors
),
3184 return ret
?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC
, &common_kfunc_set
);
3187 late_initcall(kfunc_init
);
3189 /* Get a pointer to dynptr data up to len bytes for read only access. If
3190 * the dynptr doesn't have continuous data up to len bytes, return NULL.
3192 const void *__bpf_dynptr_data(const struct bpf_dynptr_kern
*ptr
, u32 len
)
3194 const struct bpf_dynptr
*p
= (struct bpf_dynptr
*)ptr
;
3196 return bpf_dynptr_slice(p
, 0, NULL
, len
);
3199 /* Get a pointer to dynptr data up to len bytes for read write access. If
3200 * the dynptr doesn't have continuous data up to len bytes, or the dynptr
3201 * is read only, return NULL.
3203 void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern
*ptr
, u32 len
)
3205 if (__bpf_dynptr_is_rdonly(ptr
))
3207 return (void *)__bpf_dynptr_data(ptr
, len
);