Merge branch 'main' into zstd-next
[linux-2.6-block.git] / include / linux / bpf.h
CommitLineData
25763b3c 1/* SPDX-License-Identifier: GPL-2.0-only */
99c55f7d 2/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
99c55f7d
AS
3 */
4#ifndef _LINUX_BPF_H
5#define _LINUX_BPF_H 1
6
7#include <uapi/linux/bpf.h>
d687f621 8#include <uapi/linux/filter.h>
74451e66 9
99c55f7d 10#include <linux/workqueue.h>
db20fd2b 11#include <linux/file.h>
b121d1e7 12#include <linux/percpu.h>
002245cc 13#include <linux/err.h>
74451e66 14#include <linux/rbtree_latch.h>
d6e1e46f 15#include <linux/numa.h>
fc970227 16#include <linux/mm_types.h>
ab3f0063 17#include <linux/wait.h>
fec56f58
AS
18#include <linux/refcount.h>
19#include <linux/mutex.h>
85d33df3 20#include <linux/module.h>
bfea9a85 21#include <linux/kallsyms.h>
2c78ee89 22#include <linux/capability.h>
48edc1f7
RG
23#include <linux/sched/mm.h>
24#include <linux/slab.h>
e21aa341 25#include <linux/percpu-refcount.h>
d687f621 26#include <linux/stddef.h>
af2ac3e1 27#include <linux/bpfptr.h>
14a324f6 28#include <linux/btf.h>
8c7dcb84 29#include <linux/rcupdate_trace.h>
99c55f7d 30
cae1927c 31struct bpf_verifier_env;
9e15db66 32struct bpf_verifier_log;
3b1efb19 33struct perf_event;
174a79ff 34struct bpf_prog;
da765a2f 35struct bpf_prog_aux;
99c55f7d 36struct bpf_map;
4f738adb 37struct sock;
a26ca7c9 38struct seq_file;
1b2b234b 39struct btf;
e8d2bec0 40struct btf_type;
3dec541b 41struct exception_table_entry;
ae24345d 42struct seq_operations;
f9c79272 43struct bpf_iter_aux_info;
f836a56e
KS
44struct bpf_local_storage;
45struct bpf_local_storage_map;
36e68442 46struct kobject;
48edc1f7 47struct mem_cgroup;
861de02e 48struct module;
69c087ba 49struct bpf_func_state;
00963a2e 50struct ftrace_ops;
d4ccaf58 51struct cgroup;
99c55f7d 52
1b9ed84e
QM
53extern struct idr btf_idr;
54extern spinlock_t btf_idr_lock;
36e68442 55extern struct kobject *btf_kobj;
1b9ed84e 56
102acbac 57typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
f9c79272
YS
58typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
59 struct bpf_iter_aux_info *aux);
14fc6bd6 60typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
af3f4134
SF
61typedef unsigned int (*bpf_func_t)(const void *,
62 const struct bpf_insn *);
14fc6bd6
YS
63struct bpf_iter_seq_info {
64 const struct seq_operations *seq_ops;
65 bpf_iter_init_seq_priv_t init_seq_private;
66 bpf_iter_fini_seq_priv_t fini_seq_private;
67 u32 seq_priv_size;
68};
69
5d903493 70/* map is generic key/value storage optionally accessible by eBPF programs */
99c55f7d
AS
71struct bpf_map_ops {
72 /* funcs callable from userspace (via syscall) */
1110f3a9 73 int (*map_alloc_check)(union bpf_attr *attr);
99c55f7d 74 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
61d1b6a4
DB
75 void (*map_release)(struct bpf_map *map, struct file *map_file);
76 void (*map_free)(struct bpf_map *map);
db20fd2b 77 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
ba6b8de4 78 void (*map_release_uref)(struct bpf_map *map);
c6110222 79 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
cb4d03ab
BV
80 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
81 union bpf_attr __user *uattr);
3e87f192
DS
82 int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
83 void *value, u64 flags);
05799638
YS
84 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
85 const union bpf_attr *attr,
86 union bpf_attr __user *uattr);
aa2e93b8
BV
87 int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,
88 union bpf_attr __user *uattr);
89 int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
90 union bpf_attr __user *uattr);
db20fd2b
AS
91
92 /* funcs callable from userspace and from eBPF programs */
93 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
3274f520 94 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
db20fd2b 95 int (*map_delete_elem)(struct bpf_map *map, void *key);
f1a2e44a
MV
96 int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
97 int (*map_pop_elem)(struct bpf_map *map, void *value);
98 int (*map_peek_elem)(struct bpf_map *map, void *value);
07343110 99 void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
2a36f0b9
WN
100
101 /* funcs called by prog_array and perf_event_array map */
d056a788
DB
102 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
103 int fd);
104 void (*map_fd_put_ptr)(void *ptr);
4a8f87e6 105 int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
14dc6f04 106 u32 (*map_fd_sys_lookup_elem)(void *ptr);
a26ca7c9
MKL
107 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
108 struct seq_file *m);
e8d2bec0 109 int (*map_check_btf)(const struct bpf_map *map,
1b2b234b 110 const struct btf *btf,
e8d2bec0
DB
111 const struct btf_type *key_type,
112 const struct btf_type *value_type);
d8eca5bb 113
da765a2f
DB
114 /* Prog poke tracking helpers. */
115 int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
116 void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
117 void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
118 struct bpf_prog *new);
119
d8eca5bb
DB
120 /* Direct value access helpers. */
121 int (*map_direct_value_addr)(const struct bpf_map *map,
122 u64 *imm, u32 off);
123 int (*map_direct_value_meta)(const struct bpf_map *map,
124 u64 imm, u32 *off);
fc970227 125 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
457f4436
AN
126 __poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
127 struct poll_table_struct *pts);
41c48f3a 128
f836a56e
KS
129 /* Functions called by bpf_local_storage maps */
130 int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
131 void *owner, u32 size);
132 void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
133 void *owner, u32 size);
134 struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
f4d05259 135
e6a4750f
BT
136 /* Misc helpers.*/
137 int (*map_redirect)(struct bpf_map *map, u32 ifindex, u64 flags);
138
f4d05259
MKL
139 /* map_meta_equal must be implemented for maps that can be
140 * used as an inner map. It is a runtime check to ensure
141 * an inner map can be inserted to an outer map.
142 *
143 * Some properties of the inner map has been used during the
144 * verification time. When inserting an inner map at the runtime,
145 * map_meta_equal has to ensure the inserting map has the same
146 * properties that the verifier has used earlier.
147 */
148 bool (*map_meta_equal)(const struct bpf_map *meta0,
149 const struct bpf_map *meta1);
150
69c087ba
YS
151
152 int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
153 struct bpf_func_state *caller,
154 struct bpf_func_state *callee);
102acbac
KC
155 int (*map_for_each_callback)(struct bpf_map *map,
156 bpf_callback_t callback_fn,
69c087ba
YS
157 void *callback_ctx, u64 flags);
158
c317ab71 159 /* BTF id of struct allocated by map_alloc */
41c48f3a 160 int *map_btf_id;
a5cbe05a
YS
161
162 /* bpf_iter info used to open a seq_file */
163 const struct bpf_iter_seq_info *iter_seq_info;
99c55f7d
AS
164};
165
61df10c7
KKD
166enum {
167 /* Support at most 8 pointers in a BPF map value */
168 BPF_MAP_VALUE_OFF_MAX = 8,
4d7d7f69
KKD
169 BPF_MAP_OFF_ARR_MAX = BPF_MAP_VALUE_OFF_MAX +
170 1 + /* for bpf_spin_lock */
171 1, /* for bpf_timer */
61df10c7
KKD
172};
173
c0a5a21c
KKD
174enum bpf_kptr_type {
175 BPF_KPTR_UNREF,
176 BPF_KPTR_REF,
177};
178
61df10c7
KKD
179struct bpf_map_value_off_desc {
180 u32 offset;
c0a5a21c 181 enum bpf_kptr_type type;
61df10c7
KKD
182 struct {
183 struct btf *btf;
14a324f6
KKD
184 struct module *module;
185 btf_dtor_kfunc_t dtor;
61df10c7
KKD
186 u32 btf_id;
187 } kptr;
188};
189
190struct bpf_map_value_off {
191 u32 nr_off;
192 struct bpf_map_value_off_desc off[];
193};
194
4d7d7f69
KKD
195struct bpf_map_off_arr {
196 u32 cnt;
197 u32 field_off[BPF_MAP_OFF_ARR_MAX];
198 u8 field_sz[BPF_MAP_OFF_ARR_MAX];
199};
200
99c55f7d 201struct bpf_map {
a26ca7c9 202 /* The first two cachelines with read-mostly members of which some
be95a845
DB
203 * are also accessed in fast-path (e.g. ops, max_entries).
204 */
205 const struct bpf_map_ops *ops ____cacheline_aligned;
206 struct bpf_map *inner_map_meta;
207#ifdef CONFIG_SECURITY
208 void *security;
209#endif
99c55f7d
AS
210 enum bpf_map_type map_type;
211 u32 key_size;
212 u32 value_size;
213 u32 max_entries;
9330986c 214 u64 map_extra; /* any per-map-type extra fields */
6c905981 215 u32 map_flags;
d83525ca 216 int spin_lock_off; /* >=0 valid offset, <0 error */
61df10c7 217 struct bpf_map_value_off *kptr_off_tab;
b00628b1 218 int timer_off; /* >=0 valid offset, <0 error */
f3f1c054 219 u32 id;
96eabe7a 220 int numa_node;
9b2cf328
MKL
221 u32 btf_key_type_id;
222 u32 btf_value_type_id;
8845b468 223 u32 btf_vmlinux_value_type_id;
a26ca7c9 224 struct btf *btf;
48edc1f7 225#ifdef CONFIG_MEMCG_KMEM
4201d9ab 226 struct obj_cgroup *objcg;
48edc1f7 227#endif
fc970227 228 char name[BPF_OBJ_NAME_LEN];
4d7d7f69 229 struct bpf_map_off_arr *off_arr;
a26ca7c9 230 /* The 3rd and 4th cacheline with misc members to avoid false sharing
be95a845
DB
231 * particularly with refcounting.
232 */
1e0bd5a0
AN
233 atomic64_t refcnt ____cacheline_aligned;
234 atomic64_t usercnt;
be95a845 235 struct work_struct work;
fc970227 236 struct mutex freeze_mutex;
353050be 237 atomic64_t writecnt;
f45d5b6c
THJ
238 /* 'Ownership' of program-containing map is claimed by the first program
239 * that is going to use this map or by the first program which FD is
240 * stored in the map to make sure that all callers and callees have the
241 * same prog type, JITed flag and xdp_has_frags flag.
242 */
243 struct {
244 spinlock_t lock;
245 enum bpf_prog_type type;
246 bool jited;
247 bool xdp_has_frags;
248 } owner;
4d7d7f69
KKD
249 bool bypass_spec_v1;
250 bool frozen; /* write-once; write-protected by freeze_mutex */
99c55f7d
AS
251};
252
d83525ca
AS
253static inline bool map_value_has_spin_lock(const struct bpf_map *map)
254{
255 return map->spin_lock_off >= 0;
256}
257
68134668 258static inline bool map_value_has_timer(const struct bpf_map *map)
d83525ca 259{
68134668 260 return map->timer_off >= 0;
d83525ca
AS
261}
262
61df10c7
KKD
263static inline bool map_value_has_kptrs(const struct bpf_map *map)
264{
265 return !IS_ERR_OR_NULL(map->kptr_off_tab);
266}
267
68134668
AS
268static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
269{
270 if (unlikely(map_value_has_spin_lock(map)))
5eaed6ee 271 memset(dst + map->spin_lock_off, 0, sizeof(struct bpf_spin_lock));
68134668 272 if (unlikely(map_value_has_timer(map)))
5eaed6ee 273 memset(dst + map->timer_off, 0, sizeof(struct bpf_timer));
4d7d7f69
KKD
274 if (unlikely(map_value_has_kptrs(map))) {
275 struct bpf_map_value_off *tab = map->kptr_off_tab;
276 int i;
277
278 for (i = 0; i < tab->nr_off; i++)
279 *(u64 *)(dst + tab->off[i].offset) = 0;
280 }
68134668
AS
281}
282
44832519
KKD
283/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
284 * forced to use 'long' read/writes to try to atomically copy long counters.
285 * Best-effort only. No barriers here, since it _will_ race with concurrent
286 * updates from BPF programs. Called from bpf syscall and mostly used with
287 * size 8 or 16 bytes, so ask compiler to inline it.
288 */
289static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
290{
291 const long *lsrc = src;
292 long *ldst = dst;
293
294 size /= sizeof(long);
295 while (size--)
296 *ldst++ = *lsrc++;
297}
298
299/* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */
300static inline void __copy_map_value(struct bpf_map *map, void *dst, void *src, bool long_memcpy)
d83525ca 301{
4d7d7f69
KKD
302 u32 curr_off = 0;
303 int i;
68134668 304
4d7d7f69 305 if (likely(!map->off_arr)) {
44832519
KKD
306 if (long_memcpy)
307 bpf_long_memcpy(dst, src, round_up(map->value_size, 8));
308 else
309 memcpy(dst, src, map->value_size);
4d7d7f69 310 return;
68134668 311 }
d83525ca 312
4d7d7f69
KKD
313 for (i = 0; i < map->off_arr->cnt; i++) {
314 u32 next_off = map->off_arr->field_off[i];
315
316 memcpy(dst + curr_off, src + curr_off, next_off - curr_off);
317 curr_off += map->off_arr->field_sz[i];
d83525ca 318 }
4d7d7f69 319 memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off);
d83525ca 320}
44832519
KKD
321
322static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
323{
324 __copy_map_value(map, dst, src, false);
325}
326
327static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src)
328{
329 __copy_map_value(map, dst, src, true);
330}
331
cc487558
KKD
332static inline void zero_map_value(struct bpf_map *map, void *dst)
333{
334 u32 curr_off = 0;
335 int i;
336
337 if (likely(!map->off_arr)) {
338 memset(dst, 0, map->value_size);
339 return;
340 }
341
342 for (i = 0; i < map->off_arr->cnt; i++) {
343 u32 next_off = map->off_arr->field_off[i];
344
345 memset(dst + curr_off, 0, next_off - curr_off);
346 curr_off += map->off_arr->field_sz[i];
347 }
348 memset(dst + curr_off, 0, map->value_size - curr_off);
349}
350
96049f3a
AS
351void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
352 bool lock_src);
b00628b1 353void bpf_timer_cancel_and_free(void *timer);
8e7ae251 354int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
d83525ca 355
602144c2 356struct bpf_offload_dev;
a3884572
JK
357struct bpf_offloaded_map;
358
359struct bpf_map_dev_ops {
360 int (*map_get_next_key)(struct bpf_offloaded_map *map,
361 void *key, void *next_key);
362 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
363 void *key, void *value);
364 int (*map_update_elem)(struct bpf_offloaded_map *map,
365 void *key, void *value, u64 flags);
366 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
367};
368
369struct bpf_offloaded_map {
370 struct bpf_map map;
371 struct net_device *netdev;
372 const struct bpf_map_dev_ops *dev_ops;
373 void *dev_priv;
374 struct list_head offloads;
375};
376
377static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
378{
379 return container_of(map, struct bpf_offloaded_map, map);
380}
381
0cd3cbed
JK
382static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
383{
384 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
385}
386
a26ca7c9
MKL
387static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
388{
85d33df3
MKL
389 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
390 map->ops->map_seq_show_elem;
a26ca7c9
MKL
391}
392
e8d2bec0 393int map_check_no_btf(const struct bpf_map *map,
1b2b234b 394 const struct btf *btf,
e8d2bec0
DB
395 const struct btf_type *key_type,
396 const struct btf_type *value_type);
397
f4d05259
MKL
398bool bpf_map_meta_equal(const struct bpf_map *meta0,
399 const struct bpf_map *meta1);
400
a3884572
JK
401extern const struct bpf_map_ops bpf_map_offload_ops;
402
d639b9d1
HL
403/* bpf_type_flag contains a set of flags that are applicable to the values of
404 * arg_type, ret_type and reg_type. For example, a pointer value may be null,
405 * or a memory is read-only. We classify types into two categories: base types
406 * and extended types. Extended types are base types combined with a type flag.
407 *
408 * Currently there are no more than 32 base types in arg_type, ret_type and
409 * reg_types.
410 */
411#define BPF_BASE_TYPE_BITS 8
412
413enum bpf_type_flag {
414 /* PTR may be NULL. */
415 PTR_MAYBE_NULL = BIT(0 + BPF_BASE_TYPE_BITS),
416
216e3cd2
HL
417 /* MEM is read-only. When applied on bpf_arg, it indicates the arg is
418 * compatible with both mutable and immutable memory.
419 */
20b2aff4
HL
420 MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS),
421
a672b2e3
DB
422 /* MEM was "allocated" from a different helper, and cannot be mixed
423 * with regular non-MEM_ALLOC'ed MEM types.
424 */
425 MEM_ALLOC = BIT(2 + BPF_BASE_TYPE_BITS),
426
c6f1bfe8
YS
427 /* MEM is in user address space. */
428 MEM_USER = BIT(3 + BPF_BASE_TYPE_BITS),
429
5844101a
HL
430 /* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
431 * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
432 * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
433 * or bpf_this_cpu_ptr(), which will return the pointer corresponding
434 * to the specified cpu.
435 */
436 MEM_PERCPU = BIT(4 + BPF_BASE_TYPE_BITS),
437
8f14852e
KKD
438 /* Indicates that the argument will be released. */
439 OBJ_RELEASE = BIT(5 + BPF_BASE_TYPE_BITS),
440
6efe152d
KKD
441 /* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
442 * unreferenced and referenced kptr loaded from map value using a load
443 * instruction, so that they can only be dereferenced but not escape the
444 * BPF program into the kernel (i.e. cannot be passed as arguments to
445 * kfunc or bpf helpers).
446 */
447 PTR_UNTRUSTED = BIT(6 + BPF_BASE_TYPE_BITS),
448
16d1e00c
JK
449 MEM_UNINIT = BIT(7 + BPF_BASE_TYPE_BITS),
450
97e03f52
JK
451 /* DYNPTR points to memory local to the bpf program. */
452 DYNPTR_TYPE_LOCAL = BIT(8 + BPF_BASE_TYPE_BITS),
453
20571567 454 /* DYNPTR points to a kernel-produced ringbuf record. */
bc34dee6
JK
455 DYNPTR_TYPE_RINGBUF = BIT(9 + BPF_BASE_TYPE_BITS),
456
508362ac
MM
457 /* Size is known at compile time. */
458 MEM_FIXED_SIZE = BIT(10 + BPF_BASE_TYPE_BITS),
459
16d1e00c
JK
460 __BPF_TYPE_FLAG_MAX,
461 __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1,
d639b9d1
HL
462};
463
bc34dee6 464#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
97e03f52 465
d639b9d1
HL
466/* Max number of base types. */
467#define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
468
469/* Max number of all types. */
470#define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
471
17a52670
AS
472/* function argument constraints */
473enum bpf_arg_type {
80f1d68c 474 ARG_DONTCARE = 0, /* unused argument in helper function */
17a52670
AS
475
476 /* the following constraints used to prototype
477 * bpf_map_lookup/update/delete_elem() functions
478 */
479 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
480 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
481 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
482
16d1e00c
JK
483 /* Used to prototype bpf_memcmp() and other functions that access data
484 * on eBPF program stack
17a52670 485 */
39f19ebb 486 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
435faee1 487
39f19ebb
AS
488 ARG_CONST_SIZE, /* number of bytes accessed from memory */
489 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
80f1d68c 490
608cd71a 491 ARG_PTR_TO_CTX, /* pointer to context */
80f1d68c 492 ARG_ANYTHING, /* any (initialized) argument is ok */
d83525ca 493 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
46f8bc92 494 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
57c3bb72
AI
495 ARG_PTR_TO_INT, /* pointer to int */
496 ARG_PTR_TO_LONG, /* pointer to long */
6ac99e8f 497 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
a7658e1a 498 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
457f4436 499 ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */
457f4436 500 ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
1df8f55a 501 ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
eaa6bcb7 502 ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
69c087ba 503 ARG_PTR_TO_FUNC, /* pointer to a bpf program function */
48946bd6 504 ARG_PTR_TO_STACK, /* pointer to stack */
fff13c4b 505 ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
b00628b1 506 ARG_PTR_TO_TIMER, /* pointer to bpf_timer */
c0a5a21c 507 ARG_PTR_TO_KPTR, /* pointer to referenced kptr */
97e03f52 508 ARG_PTR_TO_DYNPTR, /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
f79e7ea5 509 __BPF_ARG_TYPE_MAX,
d639b9d1 510
48946bd6
HL
511 /* Extended arg_types. */
512 ARG_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
513 ARG_PTR_TO_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
514 ARG_PTR_TO_CTX_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
515 ARG_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
516 ARG_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_ALLOC_MEM,
517 ARG_PTR_TO_STACK_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
c0a5a21c 518 ARG_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
16d1e00c
JK
519 /* pointer to memory does not need to be initialized, helper function must fill
520 * all bytes or clear them in error case.
521 */
522 ARG_PTR_TO_UNINIT_MEM = MEM_UNINIT | ARG_PTR_TO_MEM,
508362ac
MM
523 /* Pointer to valid memory of size known at compile time. */
524 ARG_PTR_TO_FIXED_SIZE_MEM = MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
48946bd6 525
d639b9d1
HL
526 /* This must be the last entry. Its purpose is to ensure the enum is
527 * wide enough to hold the higher bits reserved for bpf_type_flag.
528 */
529 __BPF_ARG_TYPE_LIMIT = BPF_TYPE_LIMIT,
17a52670 530};
d639b9d1 531static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
17a52670
AS
532
533/* type of values returned from helper functions */
534enum bpf_return_type {
535 RET_INTEGER, /* function returns integer */
536 RET_VOID, /* function doesn't return anything */
3e6a4b3e 537 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
3c480732
HL
538 RET_PTR_TO_SOCKET, /* returns a pointer to a socket */
539 RET_PTR_TO_TCP_SOCK, /* returns a pointer to a tcp_sock */
540 RET_PTR_TO_SOCK_COMMON, /* returns a pointer to a sock_common */
541 RET_PTR_TO_ALLOC_MEM, /* returns a pointer to dynamically allocated memory */
63d9b80d 542 RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
3ca1032a 543 RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
d639b9d1
HL
544 __BPF_RET_TYPE_MAX,
545
3c480732
HL
546 /* Extended ret_types. */
547 RET_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
548 RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
549 RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
550 RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
a672b2e3 551 RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM,
34d4ef57 552 RET_PTR_TO_DYNPTR_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM,
3c480732
HL
553 RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
554
d639b9d1
HL
555 /* This must be the last entry. Its purpose is to ensure the enum is
556 * wide enough to hold the higher bits reserved for bpf_type_flag.
557 */
558 __BPF_RET_TYPE_LIMIT = BPF_TYPE_LIMIT,
17a52670 559};
d639b9d1 560static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
17a52670 561
09756af4
AS
562/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
563 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
564 * instructions after verifying
565 */
566struct bpf_func_proto {
567 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
568 bool gpl_only;
36bbef52 569 bool pkt_access;
17a52670 570 enum bpf_return_type ret_type;
a7658e1a
AS
571 union {
572 struct {
573 enum bpf_arg_type arg1_type;
574 enum bpf_arg_type arg2_type;
575 enum bpf_arg_type arg3_type;
576 enum bpf_arg_type arg4_type;
577 enum bpf_arg_type arg5_type;
578 };
579 enum bpf_arg_type arg_type[5];
580 };
9436ef6e
LB
581 union {
582 struct {
583 u32 *arg1_btf_id;
584 u32 *arg2_btf_id;
585 u32 *arg3_btf_id;
586 u32 *arg4_btf_id;
587 u32 *arg5_btf_id;
588 };
589 u32 *arg_btf_id[5];
508362ac
MM
590 struct {
591 size_t arg1_size;
592 size_t arg2_size;
593 size_t arg3_size;
594 size_t arg4_size;
595 size_t arg5_size;
596 };
597 size_t arg_size[5];
9436ef6e 598 };
af7ec138 599 int *ret_btf_id; /* return value btf_id */
eae2e83e 600 bool (*allowed)(const struct bpf_prog *prog);
17a52670
AS
601};
602
603/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
604 * the first argument to eBPF programs.
605 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
606 */
607struct bpf_context;
608
609enum bpf_access_type {
610 BPF_READ = 1,
611 BPF_WRITE = 2
09756af4
AS
612};
613
19de99f7 614/* types of values stored in eBPF registers */
f1174f77
EC
615/* Pointer types represent:
616 * pointer
617 * pointer + imm
618 * pointer + (u16) var
619 * pointer + (u16) var + imm
620 * if (range > 0) then [ptr, ptr + range - off) is safe to access
621 * if (id > 0) means that some 'var' was added
622 * if (off > 0) means that 'imm' was added
623 */
19de99f7
AS
624enum bpf_reg_type {
625 NOT_INIT = 0, /* nothing was written into register */
f1174f77 626 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
19de99f7
AS
627 PTR_TO_CTX, /* reg points to bpf_context */
628 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
629 PTR_TO_MAP_VALUE, /* reg points to map element value */
c25b2ae1 630 PTR_TO_MAP_KEY, /* reg points to a map element key */
f1174f77 631 PTR_TO_STACK, /* reg == frame_pointer + offset */
de8f3a83 632 PTR_TO_PACKET_META, /* skb->data - meta_len */
f1174f77 633 PTR_TO_PACKET, /* reg points to skb->data */
19de99f7 634 PTR_TO_PACKET_END, /* skb->data + headlen */
d58e468b 635 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
c64b7983 636 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
46f8bc92 637 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
655a51e5 638 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
9df1c28b 639 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
fada7fdc 640 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
ba5f4cfe
JF
641 /* PTR_TO_BTF_ID points to a kernel struct that does not need
642 * to be null checked by the BPF program. This does not imply the
643 * pointer is _not_ null and in practice this can easily be a null
644 * pointer when reading pointer chains. The assumption is program
645 * context will handle null pointer dereference typically via fault
646 * handling. The verifier must keep this in mind and can make no
647 * assumptions about null or non-null when doing branch analysis.
648 * Further, when passed into helpers the helpers can not, without
649 * additional context, assume the value is non-null.
650 */
651 PTR_TO_BTF_ID,
652 /* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
653 * been checked for null. Used primarily to inform the verifier
654 * an explicit null check is required for this struct.
655 */
457f4436 656 PTR_TO_MEM, /* reg points to valid memory region */
20b2aff4 657 PTR_TO_BUF, /* reg points to a read/write buffer */
69c087ba 658 PTR_TO_FUNC, /* reg points to a bpf program function */
20571567 659 PTR_TO_DYNPTR, /* reg points to a dynptr */
e6ac2450 660 __BPF_REG_TYPE_MAX,
d639b9d1 661
c25b2ae1
HL
662 /* Extended reg_types. */
663 PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
664 PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCKET,
665 PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
666 PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
667 PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | PTR_TO_BTF_ID,
c25b2ae1 668
d639b9d1
HL
669 /* This must be the last entry. Its purpose is to ensure the enum is
670 * wide enough to hold the higher bits reserved for bpf_type_flag.
671 */
672 __BPF_REG_TYPE_LIMIT = BPF_TYPE_LIMIT,
19de99f7 673};
d639b9d1 674static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
19de99f7 675
23994631
YS
676/* The information passed from prog-specific *_is_valid_access
677 * back to the verifier.
678 */
679struct bpf_insn_access_aux {
680 enum bpf_reg_type reg_type;
9e15db66
AS
681 union {
682 int ctx_field_size;
22dc4a0f
AN
683 struct {
684 struct btf *btf;
685 u32 btf_id;
686 };
9e15db66
AS
687 };
688 struct bpf_verifier_log *log; /* for verbose logs */
23994631
YS
689};
690
f96da094
DB
691static inline void
692bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
693{
694 aux->ctx_field_size = size;
695}
696
3990ed4c
MKL
697static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
698{
699 return insn->code == (BPF_LD | BPF_IMM | BPF_DW) &&
700 insn->src_reg == BPF_PSEUDO_FUNC;
701}
702
7de16e3a
JK
703struct bpf_prog_ops {
704 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
705 union bpf_attr __user *uattr);
706};
707
09756af4
AS
708struct bpf_verifier_ops {
709 /* return eBPF function prototype for verification */
5e43f899
AI
710 const struct bpf_func_proto *
711 (*get_func_proto)(enum bpf_func_id func_id,
712 const struct bpf_prog *prog);
17a52670
AS
713
714 /* return true if 'size' wide access at offset 'off' within bpf_context
715 * with 'type' (read or write) is allowed
716 */
19de99f7 717 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
5e43f899 718 const struct bpf_prog *prog,
23994631 719 struct bpf_insn_access_aux *info);
36bbef52
DB
720 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
721 const struct bpf_prog *prog);
e0cea7ce
DB
722 int (*gen_ld_abs)(const struct bpf_insn *orig,
723 struct bpf_insn *insn_buf);
6b8cc1d1
DB
724 u32 (*convert_ctx_access)(enum bpf_access_type type,
725 const struct bpf_insn *src,
726 struct bpf_insn *dst,
f96da094 727 struct bpf_prog *prog, u32 *target_size);
27ae7997 728 int (*btf_struct_access)(struct bpf_verifier_log *log,
22dc4a0f 729 const struct btf *btf,
27ae7997
MKL
730 const struct btf_type *t, int off, int size,
731 enum bpf_access_type atype,
c6f1bfe8 732 u32 *next_btf_id, enum bpf_type_flag *flag);
09756af4
AS
733};
734
cae1927c 735struct bpf_prog_offload_ops {
08ca90af 736 /* verifier basic callbacks */
cae1927c
JK
737 int (*insn_hook)(struct bpf_verifier_env *env,
738 int insn_idx, int prev_insn_idx);
c941ce9c 739 int (*finalize)(struct bpf_verifier_env *env);
08ca90af
JK
740 /* verifier optimization callbacks (called after .finalize) */
741 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
742 struct bpf_insn *insn);
743 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
744 /* program management callbacks */
16a8cb5c
QM
745 int (*prepare)(struct bpf_prog *prog);
746 int (*translate)(struct bpf_prog *prog);
eb911947 747 void (*destroy)(struct bpf_prog *prog);
cae1927c
JK
748};
749
0a9c1991 750struct bpf_prog_offload {
ab3f0063
JK
751 struct bpf_prog *prog;
752 struct net_device *netdev;
341b3e7b 753 struct bpf_offload_dev *offdev;
ab3f0063
JK
754 void *dev_priv;
755 struct list_head offloads;
756 bool dev_state;
08ca90af 757 bool opt_failed;
fcfb126d
JW
758 void *jited_image;
759 u32 jited_len;
ab3f0063
JK
760};
761
8bad74f9
RG
762enum bpf_cgroup_storage_type {
763 BPF_CGROUP_STORAGE_SHARED,
b741f163 764 BPF_CGROUP_STORAGE_PERCPU,
8bad74f9
RG
765 __BPF_CGROUP_STORAGE_MAX
766};
767
768#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
769
f1b9509c
AS
770/* The longest tracepoint has 12 args.
771 * See include/trace/bpf_probe.h
772 */
773#define MAX_BPF_FUNC_ARGS 12
774
523a4cf4
DB
775/* The maximum number of arguments passed through registers
776 * a single function may have.
777 */
778#define MAX_BPF_FUNC_REG_ARGS 5
779
720e6a43
YS
780/* The argument is a structure. */
781#define BTF_FMODEL_STRUCT_ARG BIT(0)
782
fec56f58
AS
783struct btf_func_model {
784 u8 ret_size;
785 u8 nr_args;
786 u8 arg_size[MAX_BPF_FUNC_ARGS];
720e6a43 787 u8 arg_flags[MAX_BPF_FUNC_ARGS];
fec56f58
AS
788};
789
790/* Restore arguments before returning from trampoline to let original function
791 * continue executing. This flag is used for fentry progs when there are no
792 * fexit progs.
793 */
794#define BPF_TRAMP_F_RESTORE_REGS BIT(0)
795/* Call original function after fentry progs, but before fexit progs.
796 * Makes sense for fentry/fexit, normal calls and indirect calls.
797 */
798#define BPF_TRAMP_F_CALL_ORIG BIT(1)
799/* Skip current frame and return to parent. Makes sense for fentry/fexit
800 * programs only. Should not be used with normal calls and indirect calls.
801 */
802#define BPF_TRAMP_F_SKIP_FRAME BIT(2)
7e6f3cd8
JO
803/* Store IP address of the caller on the trampoline stack,
804 * so it's available for trampoline's programs.
805 */
806#define BPF_TRAMP_F_IP_ARG BIT(3)
356ed649
HT
807/* Return the return value of fentry prog. Only used by bpf_struct_ops. */
808#define BPF_TRAMP_F_RET_FENTRY_RET BIT(4)
7e6f3cd8 809
316cba62
JO
810/* Get original function from stack instead of from provided direct address.
811 * Makes sense for trampolines with fexit or fmod_ret programs.
812 */
813#define BPF_TRAMP_F_ORIG_STACK BIT(5)
814
00963a2e
SL
815/* This trampoline is on a function with another ftrace_ops with IPMODIFY,
816 * e.g., a live patch. This flag is set and cleared by ftrace call backs,
817 */
818#define BPF_TRAMP_F_SHARE_IPMODIFY BIT(6)
819
88fd9e53 820/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
b23316aa 821 * bytes on x86.
88fd9e53 822 */
f7e0beaf 823#define BPF_MAX_TRAMP_LINKS 38
88fd9e53 824
f7e0beaf
KFL
825struct bpf_tramp_links {
826 struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS];
827 int nr_links;
88fd9e53
KS
828};
829
e384c7b7
KFL
830struct bpf_tramp_run_ctx;
831
fec56f58
AS
832/* Different use cases for BPF trampoline:
833 * 1. replace nop at the function entry (kprobe equivalent)
834 * flags = BPF_TRAMP_F_RESTORE_REGS
835 * fentry = a set of programs to run before returning from trampoline
836 *
837 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
838 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
839 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
840 * fentry = a set of program to run before calling original function
841 * fexit = a set of program to run after original function
842 *
843 * 3. replace direct call instruction anywhere in the function body
844 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
845 * With flags = 0
846 * fentry = a set of programs to run before returning from trampoline
847 * With flags = BPF_TRAMP_F_CALL_ORIG
848 * orig_call = original callback addr or direct function addr
849 * fentry = a set of program to run before calling original function
850 * fexit = a set of program to run after original function
851 */
e21aa341
AS
852struct bpf_tramp_image;
853int arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
85d33df3 854 const struct btf_func_model *m, u32 flags,
f7e0beaf 855 struct bpf_tramp_links *tlinks,
fec56f58
AS
856 void *orig_call);
857/* these two functions are called from generated trampoline */
e384c7b7
KFL
858u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
859void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx);
860u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx);
861void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
862 struct bpf_tramp_run_ctx *run_ctx);
69fd337a
SF
863u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
864 struct bpf_tramp_run_ctx *run_ctx);
865void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
866 struct bpf_tramp_run_ctx *run_ctx);
64696c40
MKL
867u64 notrace __bpf_prog_enter_struct_ops(struct bpf_prog *prog,
868 struct bpf_tramp_run_ctx *run_ctx);
869void notrace __bpf_prog_exit_struct_ops(struct bpf_prog *prog, u64 start,
870 struct bpf_tramp_run_ctx *run_ctx);
e21aa341
AS
871void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
872void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
fec56f58 873
535911c8
JO
874struct bpf_ksym {
875 unsigned long start;
876 unsigned long end;
bfea9a85 877 char name[KSYM_NAME_LEN];
ecb60d1c 878 struct list_head lnode;
ca4424c9 879 struct latch_tree_node tnode;
cbd76f8d 880 bool prog;
535911c8
JO
881};
882
fec56f58
AS
883enum bpf_tramp_prog_type {
884 BPF_TRAMP_FENTRY,
885 BPF_TRAMP_FEXIT,
ae240823 886 BPF_TRAMP_MODIFY_RETURN,
be8704ff
AS
887 BPF_TRAMP_MAX,
888 BPF_TRAMP_REPLACE, /* more than MAX */
fec56f58
AS
889};
890
e21aa341
AS
891struct bpf_tramp_image {
892 void *image;
893 struct bpf_ksym ksym;
894 struct percpu_ref pcref;
895 void *ip_after_call;
896 void *ip_epilogue;
897 union {
898 struct rcu_head rcu;
899 struct work_struct work;
900 };
901};
902
fec56f58
AS
903struct bpf_trampoline {
904 /* hlist for trampoline_table */
905 struct hlist_node hlist;
00963a2e 906 struct ftrace_ops *fops;
fec56f58
AS
907 /* serializes access to fields of this trampoline */
908 struct mutex mutex;
909 refcount_t refcnt;
00963a2e 910 u32 flags;
fec56f58
AS
911 u64 key;
912 struct {
913 struct btf_func_model model;
914 void *addr;
b91e014f 915 bool ftrace_managed;
fec56f58 916 } func;
be8704ff
AS
917 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
918 * program by replacing one of its functions. func.addr is the address
919 * of the function it replaced.
920 */
921 struct bpf_prog *extension_prog;
fec56f58
AS
922 /* list of BPF programs using this trampoline */
923 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
924 /* Number of attached programs. A counter per kind. */
925 int progs_cnt[BPF_TRAMP_MAX];
926 /* Executable image of trampoline */
e21aa341 927 struct bpf_tramp_image *cur_image;
fec56f58 928 u64 selector;
861de02e 929 struct module *mod;
fec56f58 930};
75ccbef6 931
f7b12b6f
THJ
932struct bpf_attach_target_info {
933 struct btf_func_model fmodel;
934 long tgt_addr;
935 const char *tgt_name;
936 const struct btf_type *tgt_type;
937};
938
116eb788 939#define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
75ccbef6
BT
940
941struct bpf_dispatcher_prog {
942 struct bpf_prog *prog;
943 refcount_t users;
944};
945
946struct bpf_dispatcher {
947 /* dispatcher mutex */
948 struct mutex mutex;
949 void *func;
950 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
951 int num_progs;
952 void *image;
19c02415 953 void *rw_image;
75ccbef6 954 u32 image_off;
517b75e4 955 struct bpf_ksym ksym;
75ccbef6
BT
956};
957
9f5b4009 958static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
7e6897f9
BT
959 const void *ctx,
960 const struct bpf_insn *insnsi,
af3f4134 961 bpf_func_t bpf_func)
7e6897f9
BT
962{
963 return bpf_func(ctx, insnsi);
964}
f7e0beaf 965
fec56f58 966#ifdef CONFIG_BPF_JIT
f7e0beaf
KFL
967int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
968int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
f7b12b6f
THJ
969struct bpf_trampoline *bpf_trampoline_get(u64 key,
970 struct bpf_attach_target_info *tgt_info);
fec56f58 971void bpf_trampoline_put(struct bpf_trampoline *tr);
19c02415 972int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
517b75e4
JO
973#define BPF_DISPATCHER_INIT(_name) { \
974 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
975 .func = &_name##_func, \
976 .progs = {}, \
977 .num_progs = 0, \
978 .image = NULL, \
979 .image_off = 0, \
980 .ksym = { \
981 .name = #_name, \
982 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
983 }, \
75ccbef6
BT
984}
985
ceea991a
JO
986#ifdef CONFIG_X86_64
987#define BPF_DISPATCHER_ATTRIBUTES __attribute__((patchable_function_entry(5)))
988#else
989#define BPF_DISPATCHER_ATTRIBUTES
990#endif
991
75ccbef6 992#define DEFINE_BPF_DISPATCHER(name) \
ceea991a 993 notrace BPF_DISPATCHER_ATTRIBUTES \
9f5b4009 994 noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \
75ccbef6
BT
995 const void *ctx, \
996 const struct bpf_insn *insnsi, \
af3f4134 997 bpf_func_t bpf_func) \
75ccbef6
BT
998 { \
999 return bpf_func(ctx, insnsi); \
1000 } \
6a64037d
BT
1001 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
1002 struct bpf_dispatcher bpf_dispatcher_##name = \
1003 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
75ccbef6 1004#define DECLARE_BPF_DISPATCHER(name) \
6a64037d 1005 unsigned int bpf_dispatcher_##name##_func( \
75ccbef6
BT
1006 const void *ctx, \
1007 const struct bpf_insn *insnsi, \
af3f4134 1008 bpf_func_t bpf_func); \
6a64037d
BT
1009 extern struct bpf_dispatcher bpf_dispatcher_##name;
1010#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1011#define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
75ccbef6
BT
1012void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1013 struct bpf_prog *to);
dba122fb 1014/* Called only from JIT-enabled code, so there's no need for stubs. */
a108f7dc
JO
1015void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
1016void bpf_image_ksym_del(struct bpf_ksym *ksym);
dba122fb
JO
1017void bpf_ksym_add(struct bpf_ksym *ksym);
1018void bpf_ksym_del(struct bpf_ksym *ksym);
3486bedd
SL
1019int bpf_jit_charge_modmem(u32 size);
1020void bpf_jit_uncharge_modmem(u32 size);
f92c1e18 1021bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
fec56f58 1022#else
f7e0beaf 1023static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
3aac1ead 1024 struct bpf_trampoline *tr)
fec56f58
AS
1025{
1026 return -ENOTSUPP;
1027}
f7e0beaf 1028static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
3aac1ead 1029 struct bpf_trampoline *tr)
fec56f58
AS
1030{
1031 return -ENOTSUPP;
1032}
f7b12b6f
THJ
1033static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1034 struct bpf_attach_target_info *tgt_info)
1035{
1036 return ERR_PTR(-EOPNOTSUPP);
1037}
fec56f58 1038static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
75ccbef6
BT
1039#define DEFINE_BPF_DISPATCHER(name)
1040#define DECLARE_BPF_DISPATCHER(name)
6a64037d 1041#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
75ccbef6
BT
1042#define BPF_DISPATCHER_PTR(name) NULL
1043static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1044 struct bpf_prog *from,
1045 struct bpf_prog *to) {}
e9b4e606
JO
1046static inline bool is_bpf_image_address(unsigned long address)
1047{
1048 return false;
1049}
f92c1e18
JO
1050static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1051{
1052 return false;
1053}
fec56f58
AS
1054#endif
1055
8c1b6e69 1056struct bpf_func_info_aux {
51c39bb1 1057 u16 linkage;
8c1b6e69
AS
1058 bool unreliable;
1059};
1060
a66886fe
DB
1061enum bpf_jit_poke_reason {
1062 BPF_POKE_REASON_TAIL_CALL,
1063};
1064
1065/* Descriptor of pokes pointing /into/ the JITed image. */
1066struct bpf_jit_poke_descriptor {
cf71b174 1067 void *tailcall_target;
ebf7d1f5
MF
1068 void *tailcall_bypass;
1069 void *bypass_addr;
f263a814 1070 void *aux;
a66886fe
DB
1071 union {
1072 struct {
1073 struct bpf_map *map;
1074 u32 key;
1075 } tail_call;
1076 };
cf71b174 1077 bool tailcall_target_stable;
a66886fe
DB
1078 u8 adj_off;
1079 u16 reason;
a748c697 1080 u32 insn_idx;
a66886fe
DB
1081};
1082
3c32cc1b
YS
1083/* reg_type info for ctx arguments */
1084struct bpf_ctx_arg_aux {
1085 u32 offset;
1086 enum bpf_reg_type reg_type;
951cf368 1087 u32 btf_id;
3c32cc1b
YS
1088};
1089
541c3bad
AN
1090struct btf_mod_pair {
1091 struct btf *btf;
1092 struct module *module;
1093};
1094
e6ac2450
MKL
1095struct bpf_kfunc_desc_tab;
1096
09756af4 1097struct bpf_prog_aux {
85192dbf 1098 atomic64_t refcnt;
24701ece 1099 u32 used_map_cnt;
541c3bad 1100 u32 used_btf_cnt;
32bbe007 1101 u32 max_ctx_offset;
e647815a 1102 u32 max_pkt_offset;
9df1c28b 1103 u32 max_tp_access;
8726679a 1104 u32 stack_depth;
dc4bb0e2 1105 u32 id;
ba64e7d8
YS
1106 u32 func_cnt; /* used by non-func prog as the number of func progs */
1107 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
ccfe29eb 1108 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
3c32cc1b 1109 u32 ctx_arg_info_size;
afbf21dc
YS
1110 u32 max_rdonly_access;
1111 u32 max_rdwr_access;
22dc4a0f 1112 struct btf *attach_btf;
3c32cc1b 1113 const struct bpf_ctx_arg_aux *ctx_arg_info;
3aac1ead
THJ
1114 struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1115 struct bpf_prog *dst_prog;
1116 struct bpf_trampoline *dst_trampoline;
4a1e7c0c
THJ
1117 enum bpf_prog_type saved_dst_prog_type;
1118 enum bpf_attach_type saved_dst_attach_type;
a4b1d3c1 1119 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
9a18eedb 1120 bool offload_requested;
38207291 1121 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
8c1b6e69 1122 bool func_proto_unreliable;
1e6c62a8 1123 bool sleepable;
ebf7d1f5 1124 bool tail_call_reachable;
c2f2cdbe 1125 bool xdp_has_frags;
38207291
MKL
1126 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1127 const struct btf_type *attach_func_proto;
1128 /* function name for valid attach_btf_id */
1129 const char *attach_func_name;
1c2a088a
AS
1130 struct bpf_prog **func;
1131 void *jit_data; /* JIT specific data. arch dependent */
a66886fe 1132 struct bpf_jit_poke_descriptor *poke_tab;
e6ac2450 1133 struct bpf_kfunc_desc_tab *kfunc_tab;
2357672c 1134 struct bpf_kfunc_btf_tab *kfunc_btf_tab;
a66886fe 1135 u32 size_poke_tab;
535911c8 1136 struct bpf_ksym ksym;
7de16e3a 1137 const struct bpf_prog_ops *ops;
09756af4 1138 struct bpf_map **used_maps;
984fe94f 1139 struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
541c3bad 1140 struct btf_mod_pair *used_btfs;
09756af4 1141 struct bpf_prog *prog;
aaac3ba9 1142 struct user_struct *user;
cb4d2b3f 1143 u64 load_time; /* ns since boottime */
aba64c7d 1144 u32 verified_insns;
69fd337a 1145 int cgroup_atype; /* enum cgroup_bpf_attach_type */
8bad74f9 1146 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
067cae47 1147 char name[BPF_OBJ_NAME_LEN];
afdb09c7
CF
1148#ifdef CONFIG_SECURITY
1149 void *security;
1150#endif
0a9c1991 1151 struct bpf_prog_offload *offload;
838e9690 1152 struct btf *btf;
ba64e7d8 1153 struct bpf_func_info *func_info;
8c1b6e69 1154 struct bpf_func_info_aux *func_info_aux;
c454a46b
MKL
1155 /* bpf_line_info loaded from userspace. linfo->insn_off
1156 * has the xlated insn offset.
1157 * Both the main and sub prog share the same linfo.
1158 * The subprog can access its first linfo by
1159 * using the linfo_idx.
1160 */
1161 struct bpf_line_info *linfo;
1162 /* jited_linfo is the jited addr of the linfo. It has a
1163 * one to one mapping to linfo:
1164 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1165 * Both the main and sub prog share the same jited_linfo.
1166 * The subprog can access its first jited_linfo by
1167 * using the linfo_idx.
1168 */
1169 void **jited_linfo;
ba64e7d8 1170 u32 func_info_cnt;
c454a46b
MKL
1171 u32 nr_linfo;
1172 /* subprog can use linfo_idx to access its first linfo and
1173 * jited_linfo.
1174 * main prog always has linfo_idx == 0
1175 */
1176 u32 linfo_idx;
3dec541b
AS
1177 u32 num_exentries;
1178 struct exception_table_entry *extable;
abf2e7d6
AS
1179 union {
1180 struct work_struct work;
1181 struct rcu_head rcu;
1182 };
09756af4
AS
1183};
1184
d687f621
DK
1185struct bpf_prog {
1186 u16 pages; /* Number of allocated pages */
1187 u16 jited:1, /* Is our filter JIT'ed? */
1188 jit_requested:1,/* archs need to JIT the prog */
1189 gpl_compatible:1, /* Is filter GPL compatible? */
1190 cb_access:1, /* Is control block accessed? */
1191 dst_needed:1, /* Do we need dst entry? */
1192 blinding_requested:1, /* needs constant blinding */
1193 blinded:1, /* Was blinded */
1194 is_func:1, /* program is a bpf function */
1195 kprobe_override:1, /* Do we override a kprobe? */
1196 has_callchain_buf:1, /* callchain buffer allocated? */
1197 enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1198 call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1199 call_get_func_ip:1, /* Do we call get_func_ip() */
1200 tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
1201 enum bpf_prog_type type; /* Type of BPF program */
1202 enum bpf_attach_type expected_attach_type; /* For some prog types */
1203 u32 len; /* Number of filter blocks */
1204 u32 jited_len; /* Size of jited insns in bytes */
1205 u8 tag[BPF_TAG_SIZE];
1206 struct bpf_prog_stats __percpu *stats;
1207 int __percpu *active;
1208 unsigned int (*bpf_func)(const void *ctx,
1209 const struct bpf_insn *insn);
1210 struct bpf_prog_aux *aux; /* Auxiliary fields */
1211 struct sock_fprog_kern *orig_prog; /* Original BPF program */
1212 /* Instructions for interpreter */
1213 union {
1214 DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1215 DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1216 };
1217};
1218
2beee5f5 1219struct bpf_array_aux {
da765a2f
DB
1220 /* Programs with direct jumps into programs part of this array. */
1221 struct list_head poke_progs;
1222 struct bpf_map *map;
1223 struct mutex poke_mutex;
1224 struct work_struct work;
2beee5f5
DB
1225};
1226
6cc7d1e8
AN
1227struct bpf_link {
1228 atomic64_t refcnt;
1229 u32 id;
1230 enum bpf_link_type type;
1231 const struct bpf_link_ops *ops;
1232 struct bpf_prog *prog;
1233 struct work_struct work;
1234};
1235
1236struct bpf_link_ops {
1237 void (*release)(struct bpf_link *link);
1238 void (*dealloc)(struct bpf_link *link);
73b11c2a 1239 int (*detach)(struct bpf_link *link);
6cc7d1e8
AN
1240 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1241 struct bpf_prog *old_prog);
1242 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1243 int (*fill_link_info)(const struct bpf_link *link,
1244 struct bpf_link_info *info);
1245};
1246
f7e0beaf
KFL
1247struct bpf_tramp_link {
1248 struct bpf_link link;
1249 struct hlist_node tramp_hlist;
2fcc8241 1250 u64 cookie;
f7e0beaf
KFL
1251};
1252
69fd337a
SF
1253struct bpf_shim_tramp_link {
1254 struct bpf_tramp_link link;
1255 struct bpf_trampoline *trampoline;
1256};
1257
f7e0beaf
KFL
1258struct bpf_tracing_link {
1259 struct bpf_tramp_link link;
1260 enum bpf_attach_type attach_type;
1261 struct bpf_trampoline *trampoline;
1262 struct bpf_prog *tgt_prog;
1263};
1264
6cc7d1e8
AN
1265struct bpf_link_primer {
1266 struct bpf_link *link;
1267 struct file *file;
1268 int fd;
1269 u32 id;
1270};
1271
85d33df3 1272struct bpf_struct_ops_value;
27ae7997
MKL
1273struct btf_member;
1274
1275#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1276struct bpf_struct_ops {
1277 const struct bpf_verifier_ops *verifier_ops;
1278 int (*init)(struct btf *btf);
1279 int (*check_member)(const struct btf_type *t,
1280 const struct btf_member *member);
85d33df3
MKL
1281 int (*init_member)(const struct btf_type *t,
1282 const struct btf_member *member,
1283 void *kdata, const void *udata);
1284 int (*reg)(void *kdata);
1285 void (*unreg)(void *kdata);
27ae7997 1286 const struct btf_type *type;
85d33df3 1287 const struct btf_type *value_type;
27ae7997
MKL
1288 const char *name;
1289 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1290 u32 type_id;
85d33df3 1291 u32 value_id;
27ae7997
MKL
1292};
1293
1294#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
85d33df3 1295#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
27ae7997 1296const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
d3e42bb0 1297void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
85d33df3
MKL
1298bool bpf_struct_ops_get(const void *kdata);
1299void bpf_struct_ops_put(const void *kdata);
1300int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1301 void *value);
f7e0beaf
KFL
1302int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1303 struct bpf_tramp_link *link,
31a645ae
HT
1304 const struct btf_func_model *model,
1305 void *image, void *image_end);
85d33df3
MKL
1306static inline bool bpf_try_module_get(const void *data, struct module *owner)
1307{
1308 if (owner == BPF_MODULE_OWNER)
1309 return bpf_struct_ops_get(data);
1310 else
1311 return try_module_get(owner);
1312}
1313static inline void bpf_module_put(const void *data, struct module *owner)
1314{
1315 if (owner == BPF_MODULE_OWNER)
1316 bpf_struct_ops_put(data);
1317 else
1318 module_put(owner);
1319}
c196906d
HT
1320
1321#ifdef CONFIG_NET
1322/* Define it here to avoid the use of forward declaration */
1323struct bpf_dummy_ops_state {
1324 int val;
1325};
1326
1327struct bpf_dummy_ops {
1328 int (*test_1)(struct bpf_dummy_ops_state *cb);
1329 int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1330 char a3, unsigned long a4);
1331};
1332
1333int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1334 union bpf_attr __user *uattr);
1335#endif
27ae7997
MKL
1336#else
1337static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1338{
1339 return NULL;
1340}
d3e42bb0
MKL
1341static inline void bpf_struct_ops_init(struct btf *btf,
1342 struct bpf_verifier_log *log)
1343{
1344}
85d33df3
MKL
1345static inline bool bpf_try_module_get(const void *data, struct module *owner)
1346{
1347 return try_module_get(owner);
1348}
1349static inline void bpf_module_put(const void *data, struct module *owner)
1350{
1351 module_put(owner);
1352}
1353static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1354 void *key,
1355 void *value)
1356{
1357 return -EINVAL;
1358}
9cb61fda
SF
1359#endif
1360
1361#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1362int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1363 int cgroup_atype);
1364void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1365#else
69fd337a
SF
1366static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1367 int cgroup_atype)
1368{
1369 return -EOPNOTSUPP;
1370}
1371static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1372{
1373}
27ae7997
MKL
1374#endif
1375
04fd61ab
AS
1376struct bpf_array {
1377 struct bpf_map map;
1378 u32 elem_size;
b2157399 1379 u32 index_mask;
2beee5f5 1380 struct bpf_array_aux *aux;
04fd61ab
AS
1381 union {
1382 char value[0] __aligned(8);
2a36f0b9 1383 void *ptrs[0] __aligned(8);
a10423b8 1384 void __percpu *pptrs[0] __aligned(8);
04fd61ab
AS
1385 };
1386};
3b1efb19 1387
c04c0d2b 1388#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
ebf7f6f0 1389#define MAX_TAIL_CALL_CNT 33
04fd61ab 1390
1ade2371
EZ
1391/* Maximum number of loops for bpf_loop */
1392#define BPF_MAX_LOOPS BIT(23)
1393
591fe988
DB
1394#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
1395 BPF_F_RDONLY_PROG | \
1396 BPF_F_WRONLY | \
1397 BPF_F_WRONLY_PROG)
1398
1399#define BPF_MAP_CAN_READ BIT(0)
1400#define BPF_MAP_CAN_WRITE BIT(1)
1401
20571567
DV
1402/* Maximum number of user-producer ring buffer samples that can be drained in
1403 * a call to bpf_user_ringbuf_drain().
1404 */
1405#define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
1406
591fe988
DB
1407static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1408{
1409 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1410
1411 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1412 * not possible.
1413 */
1414 if (access_flags & BPF_F_RDONLY_PROG)
1415 return BPF_MAP_CAN_READ;
1416 else if (access_flags & BPF_F_WRONLY_PROG)
1417 return BPF_MAP_CAN_WRITE;
1418 else
1419 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1420}
1421
1422static inline bool bpf_map_flags_access_ok(u32 access_flags)
1423{
1424 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1425 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1426}
1427
3b1efb19
DB
1428struct bpf_event_entry {
1429 struct perf_event *event;
1430 struct file *perf_file;
1431 struct file *map_file;
1432 struct rcu_head rcu;
1433};
1434
f45d5b6c
THJ
1435static inline bool map_type_contains_progs(struct bpf_map *map)
1436{
1437 return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
1438 map->map_type == BPF_MAP_TYPE_DEVMAP ||
1439 map->map_type == BPF_MAP_TYPE_CPUMAP;
1440}
1441
1442bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
f1f7714e 1443int bpf_prog_calc_tag(struct bpf_prog *fp);
bd570ff9 1444
0756ea3e 1445const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
10aceb62 1446const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
555c8a86
DB
1447
1448typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
aa7145c1 1449 unsigned long off, unsigned long len);
c64b7983
JS
1450typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1451 const struct bpf_insn *src,
1452 struct bpf_insn *dst,
1453 struct bpf_prog *prog,
1454 u32 *target_size);
555c8a86
DB
1455
1456u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1457 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
04fd61ab 1458
324bda9e
AS
1459/* an array of programs to be executed under rcu_lock.
1460 *
1461 * Typical usage:
055eb955 1462 * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
324bda9e
AS
1463 *
1464 * the structure returned by bpf_prog_array_alloc() should be populated
1465 * with program pointers and the last pointer must be NULL.
1466 * The user has to keep refcnt on the program and make sure the program
1467 * is removed from the array before bpf_prog_put().
1468 * The 'struct bpf_prog_array *' should only be replaced with xchg()
1469 * since other cpus are walking the array of pointers in parallel.
1470 */
394e40a2
RG
1471struct bpf_prog_array_item {
1472 struct bpf_prog *prog;
82e6b1ee
AN
1473 union {
1474 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1475 u64 bpf_cookie;
1476 };
394e40a2
RG
1477};
1478
324bda9e
AS
1479struct bpf_prog_array {
1480 struct rcu_head rcu;
d7f10df8 1481 struct bpf_prog_array_item items[];
324bda9e
AS
1482};
1483
46531a30
PB
1484struct bpf_empty_prog_array {
1485 struct bpf_prog_array hdr;
1486 struct bpf_prog *null_prog;
1487};
1488
1489/* to avoid allocating empty bpf_prog_array for cgroups that
1490 * don't have bpf program attached use one global 'bpf_empty_prog_array'
1491 * It will not be modified the caller of bpf_prog_array_alloc()
1492 * (since caller requested prog_cnt == 0)
1493 * that pointer should be 'freed' by bpf_prog_array_free()
1494 */
1495extern struct bpf_empty_prog_array bpf_empty_prog_array;
1496
d29ab6e1 1497struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
54e9c9d4 1498void bpf_prog_array_free(struct bpf_prog_array *progs);
8c7dcb84
DK
1499/* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
1500void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
54e9c9d4 1501int bpf_prog_array_length(struct bpf_prog_array *progs);
0d01da6a 1502bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
54e9c9d4 1503int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
468e2f64 1504 __u32 __user *prog_ids, u32 cnt);
324bda9e 1505
54e9c9d4 1506void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
e87c6bc3 1507 struct bpf_prog *old_prog);
ce3aa9cc
JS
1508int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1509int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1510 struct bpf_prog *prog);
54e9c9d4 1511int bpf_prog_array_copy_info(struct bpf_prog_array *array,
3a38bb98
YS
1512 u32 *prog_ids, u32 request_cnt,
1513 u32 *prog_cnt);
54e9c9d4 1514int bpf_prog_array_copy(struct bpf_prog_array *old_array,
e87c6bc3
YS
1515 struct bpf_prog *exclude_prog,
1516 struct bpf_prog *include_prog,
82e6b1ee 1517 u64 bpf_cookie,
e87c6bc3
YS
1518 struct bpf_prog_array **new_array);
1519
c7603cfa
AN
1520struct bpf_run_ctx {};
1521
1522struct bpf_cg_run_ctx {
1523 struct bpf_run_ctx run_ctx;
7d08c2c9 1524 const struct bpf_prog_array_item *prog_item;
c4dcfdd4 1525 int retval;
c7603cfa
AN
1526};
1527
82e6b1ee
AN
1528struct bpf_trace_run_ctx {
1529 struct bpf_run_ctx run_ctx;
1530 u64 bpf_cookie;
1531};
1532
e384c7b7
KFL
1533struct bpf_tramp_run_ctx {
1534 struct bpf_run_ctx run_ctx;
1535 u64 bpf_cookie;
1536 struct bpf_run_ctx *saved_run_ctx;
1537};
1538
7d08c2c9
AN
1539static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1540{
1541 struct bpf_run_ctx *old_ctx = NULL;
1542
1543#ifdef CONFIG_BPF_SYSCALL
1544 old_ctx = current->bpf_ctx;
1545 current->bpf_ctx = new_ctx;
1546#endif
1547 return old_ctx;
1548}
1549
1550static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1551{
1552#ifdef CONFIG_BPF_SYSCALL
1553 current->bpf_ctx = old_ctx;
1554#endif
1555}
1556
77241217
SF
1557/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1558#define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)
1559/* BPF program asks to set CN on the packet. */
1560#define BPF_RET_SET_CN (1 << 0)
1561
7d08c2c9
AN
1562typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
1563
7d08c2c9 1564static __always_inline u32
055eb955 1565bpf_prog_run_array(const struct bpf_prog_array *array,
7d08c2c9
AN
1566 const void *ctx, bpf_prog_run_fn run_prog)
1567{
1568 const struct bpf_prog_array_item *item;
1569 const struct bpf_prog *prog;
82e6b1ee
AN
1570 struct bpf_run_ctx *old_run_ctx;
1571 struct bpf_trace_run_ctx run_ctx;
7d08c2c9
AN
1572 u32 ret = 1;
1573
055eb955
SF
1574 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
1575
7d08c2c9 1576 if (unlikely(!array))
055eb955
SF
1577 return ret;
1578
1579 migrate_disable();
82e6b1ee 1580 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
7d08c2c9
AN
1581 item = &array->items[0];
1582 while ((prog = READ_ONCE(item->prog))) {
82e6b1ee 1583 run_ctx.bpf_cookie = item->bpf_cookie;
7d08c2c9
AN
1584 ret &= run_prog(prog, ctx);
1585 item++;
1586 }
82e6b1ee 1587 bpf_reset_run_ctx(old_run_ctx);
7d08c2c9
AN
1588 migrate_enable();
1589 return ret;
1590}
324bda9e 1591
8c7dcb84
DK
1592/* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
1593 *
1594 * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
1595 * overall. As a result, we must use the bpf_prog_array_free_sleepable
1596 * in order to use the tasks_trace rcu grace period.
1597 *
1598 * When a non-sleepable program is inside the array, we take the rcu read
1599 * section and disable preemption for that program alone, so it can access
1600 * rcu-protected dynamically sized maps.
1601 */
1602static __always_inline u32
1603bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,
1604 const void *ctx, bpf_prog_run_fn run_prog)
1605{
1606 const struct bpf_prog_array_item *item;
1607 const struct bpf_prog *prog;
1608 const struct bpf_prog_array *array;
1609 struct bpf_run_ctx *old_run_ctx;
1610 struct bpf_trace_run_ctx run_ctx;
1611 u32 ret = 1;
1612
1613 might_fault();
1614
1615 rcu_read_lock_trace();
1616 migrate_disable();
1617
1618 array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
1619 if (unlikely(!array))
1620 goto out;
1621 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1622 item = &array->items[0];
1623 while ((prog = READ_ONCE(item->prog))) {
1624 if (!prog->aux->sleepable)
1625 rcu_read_lock();
1626
1627 run_ctx.bpf_cookie = item->bpf_cookie;
1628 ret &= run_prog(prog, ctx);
1629 item++;
1630
1631 if (!prog->aux->sleepable)
1632 rcu_read_unlock();
1633 }
1634 bpf_reset_run_ctx(old_run_ctx);
1635out:
1636 migrate_enable();
1637 rcu_read_unlock_trace();
1638 return ret;
1639}
1640
89aa0758 1641#ifdef CONFIG_BPF_SYSCALL
b121d1e7 1642DECLARE_PER_CPU(int, bpf_prog_active);
d46edd67 1643extern struct mutex bpf_stats_enabled_mutex;
b121d1e7 1644
c518cfa0
TG
1645/*
1646 * Block execution of BPF programs attached to instrumentation (perf,
1647 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1648 * these events can happen inside a region which holds a map bucket lock
1649 * and can deadlock on it.
c518cfa0
TG
1650 */
1651static inline void bpf_disable_instrumentation(void)
1652{
1653 migrate_disable();
79364031 1654 this_cpu_inc(bpf_prog_active);
c518cfa0
TG
1655}
1656
1657static inline void bpf_enable_instrumentation(void)
1658{
79364031 1659 this_cpu_dec(bpf_prog_active);
c518cfa0
TG
1660 migrate_enable();
1661}
1662
f66e448c
CF
1663extern const struct file_operations bpf_map_fops;
1664extern const struct file_operations bpf_prog_fops;
367ec3e4 1665extern const struct file_operations bpf_iter_fops;
f66e448c 1666
91cc1a99 1667#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
7de16e3a
JK
1668 extern const struct bpf_prog_ops _name ## _prog_ops; \
1669 extern const struct bpf_verifier_ops _name ## _verifier_ops;
40077e0c
JB
1670#define BPF_MAP_TYPE(_id, _ops) \
1671 extern const struct bpf_map_ops _ops;
f2e10bff 1672#define BPF_LINK_TYPE(_id, _name)
be9370a7
JB
1673#include <linux/bpf_types.h>
1674#undef BPF_PROG_TYPE
40077e0c 1675#undef BPF_MAP_TYPE
f2e10bff 1676#undef BPF_LINK_TYPE
0fc174de 1677
ab3f0063 1678extern const struct bpf_prog_ops bpf_offload_prog_ops;
4f9218aa
JK
1679extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1680extern const struct bpf_verifier_ops xdp_analyzer_ops;
1681
0fc174de 1682struct bpf_prog *bpf_prog_get(u32 ufd);
248f346f 1683struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
288b3de5 1684 bool attach_drv);
85192dbf 1685void bpf_prog_add(struct bpf_prog *prog, int i);
c540594f 1686void bpf_prog_sub(struct bpf_prog *prog, int i);
85192dbf 1687void bpf_prog_inc(struct bpf_prog *prog);
a6f6df69 1688struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
61e021f3
DB
1689void bpf_prog_put(struct bpf_prog *prog);
1690
ad8ad79f 1691void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
a3884572 1692void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
ad8ad79f 1693
61df10c7
KKD
1694struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset);
1695void bpf_map_free_kptr_off_tab(struct bpf_map *map);
1696struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map);
1697bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
14a324f6 1698void bpf_map_free_kptrs(struct bpf_map *map, void *map_value);
61df10c7 1699
1ed4d924 1700struct bpf_map *bpf_map_get(u32 ufd);
c9da161c 1701struct bpf_map *bpf_map_get_with_uref(u32 ufd);
c2101297 1702struct bpf_map *__bpf_map_get(struct fd f);
1e0bd5a0
AN
1703void bpf_map_inc(struct bpf_map *map);
1704void bpf_map_inc_with_uref(struct bpf_map *map);
1705struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
c9da161c 1706void bpf_map_put_with_uref(struct bpf_map *map);
61e021f3 1707void bpf_map_put(struct bpf_map *map);
196e8ca7
DB
1708void *bpf_map_area_alloc(u64 size, int numa_node);
1709void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
d407bd25 1710void bpf_map_area_free(void *base);
353050be 1711bool bpf_map_write_active(const struct bpf_map *map);
bd475643 1712void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
cb4d03ab
BV
1713int generic_map_lookup_batch(struct bpf_map *map,
1714 const union bpf_attr *attr,
aa2e93b8
BV
1715 union bpf_attr __user *uattr);
1716int generic_map_update_batch(struct bpf_map *map,
1717 const union bpf_attr *attr,
1718 union bpf_attr __user *uattr);
1719int generic_map_delete_batch(struct bpf_map *map,
1720 const union bpf_attr *attr,
cb4d03ab 1721 union bpf_attr __user *uattr);
6086d29d 1722struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
a228a64f 1723struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
61e021f3 1724
48edc1f7
RG
1725#ifdef CONFIG_MEMCG_KMEM
1726void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1727 int node);
1728void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1729void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1730 size_t align, gfp_t flags);
1731#else
1732static inline void *
1733bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1734 int node)
1735{
1736 return kmalloc_node(size, flags, node);
1737}
1738
1739static inline void *
1740bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1741{
1742 return kzalloc(size, flags);
1743}
1744
1745static inline void __percpu *
1746bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1747 gfp_t flags)
1748{
1749 return __alloc_percpu_gfp(size, align, flags);
1750}
1751#endif
1752
1be7f75d
AS
1753extern int sysctl_unprivileged_bpf_disabled;
1754
2c78ee89
AS
1755static inline bool bpf_allow_ptr_leaks(void)
1756{
1757 return perfmon_capable();
1758}
1759
01f810ac
AM
1760static inline bool bpf_allow_uninit_stack(void)
1761{
1762 return perfmon_capable();
1763}
1764
41c48f3a
AI
1765static inline bool bpf_allow_ptr_to_map_access(void)
1766{
1767 return perfmon_capable();
1768}
1769
2c78ee89
AS
1770static inline bool bpf_bypass_spec_v1(void)
1771{
1772 return perfmon_capable();
1773}
1774
1775static inline bool bpf_bypass_spec_v4(void)
1776{
1777 return perfmon_capable();
1778}
1779
6e71b04a 1780int bpf_map_new_fd(struct bpf_map *map, int flags);
b2197755
DB
1781int bpf_prog_new_fd(struct bpf_prog *prog);
1782
f2e10bff 1783void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
a3b80e10
AN
1784 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1785int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1786int bpf_link_settle(struct bpf_link_primer *primer);
1787void bpf_link_cleanup(struct bpf_link_primer *primer);
70ed506c
AN
1788void bpf_link_inc(struct bpf_link *link);
1789void bpf_link_put(struct bpf_link *link);
1790int bpf_link_new_fd(struct bpf_link *link);
babf3164 1791struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
70ed506c 1792struct bpf_link *bpf_link_get_from_fd(u32 ufd);
9f883612 1793struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
70ed506c 1794
b2197755 1795int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
6e71b04a 1796int bpf_obj_get_user(const char __user *pathname, int flags);
b2197755 1797
21aef70e 1798#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
e5158d98 1799#define DEFINE_BPF_ITER_FUNC(target, args...) \
21aef70e
YS
1800 extern int bpf_iter_ ## target(args); \
1801 int __init bpf_iter_ ## target(args) { return 0; }
15d83c4d 1802
f0d74c4d
KFL
1803/*
1804 * The task type of iterators.
1805 *
1806 * For BPF task iterators, they can be parameterized with various
1807 * parameters to visit only some of tasks.
1808 *
1809 * BPF_TASK_ITER_ALL (default)
1810 * Iterate over resources of every task.
1811 *
1812 * BPF_TASK_ITER_TID
1813 * Iterate over resources of a task/tid.
1814 *
1815 * BPF_TASK_ITER_TGID
1816 * Iterate over resources of every task of a process / task group.
1817 */
1818enum bpf_iter_task_type {
1819 BPF_TASK_ITER_ALL = 0,
1820 BPF_TASK_ITER_TID,
1821 BPF_TASK_ITER_TGID,
1822};
1823
f9c79272 1824struct bpf_iter_aux_info {
d4ccaf58 1825 /* for map_elem iter */
a5cbe05a 1826 struct bpf_map *map;
d4ccaf58
HL
1827
1828 /* for cgroup iter */
1829 struct {
1830 struct cgroup *start; /* starting cgroup */
1831 enum bpf_cgroup_iter_order order;
1832 } cgroup;
f0d74c4d
KFL
1833 struct {
1834 enum bpf_iter_task_type type;
1835 u32 pid;
1836 } task;
f9c79272
YS
1837};
1838
5e7b3020
YS
1839typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1840 union bpf_iter_link_info *linfo,
1841 struct bpf_iter_aux_info *aux);
1842typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
6b0a249a
YS
1843typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1844 struct seq_file *seq);
1845typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1846 struct bpf_link_info *info);
3cee6fb8
MKL
1847typedef const struct bpf_func_proto *
1848(*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1849 const struct bpf_prog *prog);
a5cbe05a 1850
cf83b2d2
YS
1851enum bpf_iter_feature {
1852 BPF_ITER_RESCHED = BIT(0),
1853};
1854
3c32cc1b 1855#define BPF_ITER_CTX_ARG_MAX 2
ae24345d
YS
1856struct bpf_iter_reg {
1857 const char *target;
5e7b3020
YS
1858 bpf_iter_attach_target_t attach_target;
1859 bpf_iter_detach_target_t detach_target;
6b0a249a
YS
1860 bpf_iter_show_fdinfo_t show_fdinfo;
1861 bpf_iter_fill_link_info_t fill_link_info;
3cee6fb8 1862 bpf_iter_get_func_proto_t get_func_proto;
3c32cc1b 1863 u32 ctx_arg_info_size;
cf83b2d2 1864 u32 feature;
3c32cc1b 1865 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
14fc6bd6 1866 const struct bpf_iter_seq_info *seq_info;
ae24345d
YS
1867};
1868
e5158d98
YS
1869struct bpf_iter_meta {
1870 __bpf_md_ptr(struct seq_file *, seq);
1871 u64 session_id;
1872 u64 seq_num;
1873};
1874
a5cbe05a
YS
1875struct bpf_iter__bpf_map_elem {
1876 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1877 __bpf_md_ptr(struct bpf_map *, map);
1878 __bpf_md_ptr(void *, key);
1879 __bpf_md_ptr(void *, value);
1880};
1881
15172a46 1882int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
ab2ee4fc 1883void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
15d83c4d 1884bool bpf_iter_prog_supported(struct bpf_prog *prog);
3cee6fb8
MKL
1885const struct bpf_func_proto *
1886bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
af2ac3e1 1887int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
ac51d99b 1888int bpf_iter_new_fd(struct bpf_link *link);
367ec3e4 1889bool bpf_link_is_iter(struct bpf_link *link);
e5158d98
YS
1890struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1891int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
b76f2226
YS
1892void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1893 struct seq_file *seq);
1894int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1895 struct bpf_link_info *info);
ae24345d 1896
314ee05e
YS
1897int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1898 struct bpf_func_state *caller,
1899 struct bpf_func_state *callee);
1900
15a07b33
AS
1901int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1902int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1903int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1904 u64 flags);
1905int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1906 u64 flags);
d056a788 1907
557c0c6e 1908int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
15a07b33 1909
d056a788
DB
1910int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1911 void *key, void *value, u64 map_flags);
14dc6f04 1912int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
bcc6b1b7
MKL
1913int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1914 void *key, void *value, u64 map_flags);
14dc6f04 1915int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
d056a788 1916
6e71b04a 1917int bpf_get_file_flag(int flags);
af2ac3e1 1918int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
dcab51f1 1919 size_t actual_size);
6e71b04a 1920
61e021f3 1921/* verify correctness of eBPF program */
af2ac3e1 1922int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
a643bff7
AN
1923
1924#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1ea47e01 1925void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
a643bff7 1926#endif
46f55cff 1927
76654e67
AM
1928struct btf *bpf_get_btf_vmlinux(void);
1929
46f55cff 1930/* Map specifics */
d53ad5d8 1931struct xdp_frame;
6d5fc195 1932struct sk_buff;
e6a4750f
BT
1933struct bpf_dtab_netdev;
1934struct bpf_cpu_map_entry;
67f29e07 1935
1d233886 1936void __dev_flush(void);
d53ad5d8 1937int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1d233886 1938 struct net_device *dev_rx);
d53ad5d8 1939int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
38edddb8 1940 struct net_device *dev_rx);
d53ad5d8 1941int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
e624d4ed 1942 struct bpf_map *map, bool exclude_ingress);
6d5fc195
TM
1943int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1944 struct bpf_prog *xdp_prog);
e624d4ed
HL
1945int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1946 struct bpf_prog *xdp_prog, struct bpf_map *map,
1947 bool exclude_ingress);
46f55cff 1948
cdfafe98 1949void __cpu_map_flush(void);
d53ad5d8 1950int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
9c270af3 1951 struct net_device *dev_rx);
11941f8a
KKD
1952int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1953 struct sk_buff *skb);
9c270af3 1954
96eabe7a
MKL
1955/* Return map's numa specified by userspace */
1956static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1957{
1958 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1959 attr->numa_node : NUMA_NO_NODE;
1960}
1961
040ee692 1962struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
5dc4c4b7 1963int array_map_alloc_check(union bpf_attr *attr);
040ee692 1964
c695865c
SF
1965int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1966 union bpf_attr __user *uattr);
1967int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1968 union bpf_attr __user *uattr);
da00d2f1
KS
1969int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1970 const union bpf_attr *kattr,
1971 union bpf_attr __user *uattr);
c695865c
SF
1972int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1973 const union bpf_attr *kattr,
1974 union bpf_attr __user *uattr);
1b4d60ec
SL
1975int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1976 const union bpf_attr *kattr,
1977 union bpf_attr __user *uattr);
7c32e8f8
LB
1978int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1979 const union bpf_attr *kattr,
1980 union bpf_attr __user *uattr);
9e15db66
AS
1981bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1982 const struct bpf_prog *prog,
1983 struct bpf_insn_access_aux *info);
35346ab6
HT
1984
1985static inline bool bpf_tracing_ctx_access(int off, int size,
1986 enum bpf_access_type type)
1987{
1988 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1989 return false;
1990 if (type != BPF_READ)
1991 return false;
1992 if (off % size != 0)
1993 return false;
1994 return true;
1995}
1996
1997static inline bool bpf_tracing_btf_ctx_access(int off, int size,
1998 enum bpf_access_type type,
1999 const struct bpf_prog *prog,
2000 struct bpf_insn_access_aux *info)
2001{
2002 if (!bpf_tracing_ctx_access(off, size, type))
2003 return false;
2004 return btf_ctx_access(off, size, type, prog, info);
2005}
2006
22dc4a0f 2007int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
9e15db66
AS
2008 const struct btf_type *t, int off, int size,
2009 enum bpf_access_type atype,
c6f1bfe8 2010 u32 *next_btf_id, enum bpf_type_flag *flag);
faaf4a79 2011bool btf_struct_ids_match(struct bpf_verifier_log *log,
22dc4a0f 2012 const struct btf *btf, u32 id, int off,
2ab3b380
KKD
2013 const struct btf *need_btf, u32 need_type_id,
2014 bool strict);
9e15db66 2015
fec56f58
AS
2016int btf_distill_func_proto(struct bpf_verifier_log *log,
2017 struct btf *btf,
2018 const struct btf_type *func_proto,
2019 const char *func_name,
2020 struct btf_func_model *m);
2021
eb1f7f71
BT
2022struct bpf_kfunc_arg_meta {
2023 u64 r0_size;
2024 bool r0_rdonly;
2025 int ref_obj_id;
2026 u32 flags;
2027};
2028
51c39bb1 2029struct bpf_reg_state;
34747c41
MKL
2030int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
2031 struct bpf_reg_state *regs);
95f2f26f
BT
2032int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
2033 struct bpf_reg_state *regs);
e6ac2450
MKL
2034int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
2035 const struct btf *btf, u32 func_id,
a4703e31 2036 struct bpf_reg_state *regs,
eb1f7f71 2037 struct bpf_kfunc_arg_meta *meta);
51c39bb1
AS
2038int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
2039 struct bpf_reg_state *reg);
efc68158 2040int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
be8704ff 2041 struct btf *btf, const struct btf_type *t);
8c1b6e69 2042
7e6897f9 2043struct bpf_prog *bpf_prog_by_id(u32 id);
005142b8 2044struct bpf_link *bpf_link_by_id(u32 id);
7e6897f9 2045
6890896b 2046const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
a10787e6 2047void bpf_task_storage_free(struct task_struct *task);
e6ac2450
MKL
2048bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
2049const struct btf_func_model *
2050bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2051 const struct bpf_insn *insn);
fbd94c7a
AS
2052struct bpf_core_ctx {
2053 struct bpf_verifier_log *log;
2054 const struct btf *btf;
2055};
2056
2057int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
2058 int relo_idx, void *insn);
2059
44a3918c
JP
2060static inline bool unprivileged_ebpf_enabled(void)
2061{
2062 return !sysctl_unprivileged_bpf_disabled;
2063}
2064
24426654
MKL
2065/* Not all bpf prog type has the bpf_ctx.
2066 * For the bpf prog type that has initialized the bpf_ctx,
2067 * this function can be used to decide if a kernel function
2068 * is called by a bpf program.
2069 */
2070static inline bool has_current_bpf_ctx(void)
2071{
2072 return !!current->bpf_ctx;
2073}
05b24ff9
JO
2074
2075void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
9c270af3 2076#else /* !CONFIG_BPF_SYSCALL */
0fc174de
DB
2077static inline struct bpf_prog *bpf_prog_get(u32 ufd)
2078{
2079 return ERR_PTR(-EOPNOTSUPP);
2080}
2081
248f346f
JK
2082static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
2083 enum bpf_prog_type type,
288b3de5 2084 bool attach_drv)
248f346f
JK
2085{
2086 return ERR_PTR(-EOPNOTSUPP);
2087}
2088
85192dbf 2089static inline void bpf_prog_add(struct bpf_prog *prog, int i)
cc2e0b3f 2090{
cc2e0b3f 2091}
113214be 2092
c540594f
DB
2093static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
2094{
2095}
2096
0fc174de
DB
2097static inline void bpf_prog_put(struct bpf_prog *prog)
2098{
2099}
6d67942d 2100
85192dbf 2101static inline void bpf_prog_inc(struct bpf_prog *prog)
aa6a5f3c 2102{
aa6a5f3c 2103}
5ccb071e 2104
a6f6df69
JF
2105static inline struct bpf_prog *__must_check
2106bpf_prog_inc_not_zero(struct bpf_prog *prog)
2107{
2108 return ERR_PTR(-EOPNOTSUPP);
2109}
2110
6cc7d1e8
AN
2111static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2112 const struct bpf_link_ops *ops,
2113 struct bpf_prog *prog)
2114{
2115}
2116
2117static inline int bpf_link_prime(struct bpf_link *link,
2118 struct bpf_link_primer *primer)
2119{
2120 return -EOPNOTSUPP;
2121}
2122
2123static inline int bpf_link_settle(struct bpf_link_primer *primer)
2124{
2125 return -EOPNOTSUPP;
2126}
2127
2128static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2129{
2130}
2131
2132static inline void bpf_link_inc(struct bpf_link *link)
2133{
2134}
2135
2136static inline void bpf_link_put(struct bpf_link *link)
2137{
2138}
2139
6e71b04a 2140static inline int bpf_obj_get_user(const char __user *pathname, int flags)
98589a09
SL
2141{
2142 return -EOPNOTSUPP;
2143}
2144
1d233886 2145static inline void __dev_flush(void)
46f55cff
JF
2146{
2147}
9c270af3 2148
d53ad5d8 2149struct xdp_frame;
67f29e07 2150struct bpf_dtab_netdev;
e6a4750f 2151struct bpf_cpu_map_entry;
67f29e07 2152
1d233886 2153static inline
d53ad5d8 2154int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1d233886
THJ
2155 struct net_device *dev_rx)
2156{
2157 return 0;
2158}
2159
67f29e07 2160static inline
d53ad5d8 2161int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
38edddb8 2162 struct net_device *dev_rx)
67f29e07
JDB
2163{
2164 return 0;
2165}
2166
e624d4ed 2167static inline
d53ad5d8 2168int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
e624d4ed
HL
2169 struct bpf_map *map, bool exclude_ingress)
2170{
2171 return 0;
2172}
2173
6d5fc195
TM
2174struct sk_buff;
2175
2176static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2177 struct sk_buff *skb,
2178 struct bpf_prog *xdp_prog)
2179{
2180 return 0;
2181}
2182
e624d4ed
HL
2183static inline
2184int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2185 struct bpf_prog *xdp_prog, struct bpf_map *map,
2186 bool exclude_ingress)
2187{
2188 return 0;
2189}
2190
cdfafe98 2191static inline void __cpu_map_flush(void)
9c270af3
JDB
2192{
2193}
2194
9c270af3 2195static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
d53ad5d8 2196 struct xdp_frame *xdpf,
9c270af3
JDB
2197 struct net_device *dev_rx)
2198{
2199 return 0;
2200}
040ee692 2201
11941f8a
KKD
2202static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2203 struct sk_buff *skb)
2204{
2205 return -EOPNOTSUPP;
2206}
2207
040ee692
AV
2208static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2209 enum bpf_prog_type type)
2210{
2211 return ERR_PTR(-EOPNOTSUPP);
2212}
c695865c
SF
2213
2214static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2215 const union bpf_attr *kattr,
2216 union bpf_attr __user *uattr)
2217{
2218 return -ENOTSUPP;
2219}
2220
2221static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2222 const union bpf_attr *kattr,
2223 union bpf_attr __user *uattr)
2224{
2225 return -ENOTSUPP;
2226}
2227
da00d2f1
KS
2228static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2229 const union bpf_attr *kattr,
2230 union bpf_attr __user *uattr)
2231{
2232 return -ENOTSUPP;
2233}
2234
c695865c
SF
2235static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2236 const union bpf_attr *kattr,
2237 union bpf_attr __user *uattr)
2238{
2239 return -ENOTSUPP;
2240}
6332be04 2241
7c32e8f8
LB
2242static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2243 const union bpf_attr *kattr,
2244 union bpf_attr __user *uattr)
2245{
2246 return -ENOTSUPP;
2247}
2248
6332be04
DB
2249static inline void bpf_map_put(struct bpf_map *map)
2250{
2251}
7e6897f9
BT
2252
2253static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2254{
2255 return ERR_PTR(-ENOTSUPP);
2256}
6890896b 2257
d4f7bdb2
DX
2258static inline int btf_struct_access(struct bpf_verifier_log *log,
2259 const struct btf *btf,
2260 const struct btf_type *t, int off, int size,
2261 enum bpf_access_type atype,
2262 u32 *next_btf_id, enum bpf_type_flag *flag)
2263{
2264 return -EACCES;
2265}
2266
6890896b
SF
2267static inline const struct bpf_func_proto *
2268bpf_base_func_proto(enum bpf_func_id func_id)
2269{
2270 return NULL;
2271}
a10787e6
SL
2272
2273static inline void bpf_task_storage_free(struct task_struct *task)
2274{
2275}
e6ac2450
MKL
2276
2277static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2278{
2279 return false;
2280}
2281
2282static inline const struct btf_func_model *
2283bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2284 const struct bpf_insn *insn)
2285{
2286 return NULL;
2287}
44a3918c
JP
2288
2289static inline bool unprivileged_ebpf_enabled(void)
2290{
2291 return false;
2292}
2293
24426654
MKL
2294static inline bool has_current_bpf_ctx(void)
2295{
2296 return false;
2297}
05b24ff9
JO
2298
2299static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2300{
2301}
61e021f3 2302#endif /* CONFIG_BPF_SYSCALL */
09756af4 2303
541c3bad
AN
2304void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
2305 struct btf_mod_pair *used_btfs, u32 len);
2306
479321e9
JK
2307static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2308 enum bpf_prog_type type)
2309{
2310 return bpf_prog_get_type_dev(ufd, type, false);
2311}
2312
936f8946
AN
2313void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2314 struct bpf_map **used_maps, u32 len);
2315
040ee692
AV
2316bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
2317
ab3f0063
JK
2318int bpf_prog_offload_compile(struct bpf_prog *prog);
2319void bpf_prog_offload_destroy(struct bpf_prog *prog);
675fc275
JK
2320int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
2321 struct bpf_prog *prog);
ab3f0063 2322
52775b33
JK
2323int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2324
a3884572
JK
2325int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
2326int bpf_map_offload_update_elem(struct bpf_map *map,
2327 void *key, void *value, u64 flags);
2328int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
2329int bpf_map_offload_get_next_key(struct bpf_map *map,
2330 void *key, void *next_key);
2331
09728266 2332bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
a3884572 2333
1385d755 2334struct bpf_offload_dev *
dd27c2e3 2335bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
602144c2 2336void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
dd27c2e3 2337void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
602144c2
JK
2338int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
2339 struct net_device *netdev);
2340void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
2341 struct net_device *netdev);
fd4f227d 2342bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
9fd7c555 2343
2147c438
JP
2344void unpriv_ebpf_notify(int new_state);
2345
ab3f0063
JK
2346#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
2347int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
2348
0d830032 2349static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
ab3f0063 2350{
9a18eedb 2351 return aux->offload_requested;
ab3f0063 2352}
a3884572
JK
2353
2354static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2355{
2356 return unlikely(map->ops == &bpf_map_offload_ops);
2357}
2358
2359struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
2360void bpf_map_offload_map_free(struct bpf_map *map);
79a7f8bd
AS
2361int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2362 const union bpf_attr *kattr,
2363 union bpf_attr __user *uattr);
17edea21
CW
2364
2365int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
2366int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
2367int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
748cd572
DZ
2368int sock_map_bpf_prog_query(const union bpf_attr *attr,
2369 union bpf_attr __user *uattr);
2370
17edea21 2371void sock_map_unhash(struct sock *sk);
d8616ee2 2372void sock_map_destroy(struct sock *sk);
17edea21 2373void sock_map_close(struct sock *sk, long timeout);
ab3f0063
JK
2374#else
2375static inline int bpf_prog_offload_init(struct bpf_prog *prog,
2376 union bpf_attr *attr)
2377{
2378 return -EOPNOTSUPP;
2379}
2380
2381static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
2382{
2383 return false;
2384}
a3884572
JK
2385
2386static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2387{
2388 return false;
2389}
2390
2391static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
2392{
2393 return ERR_PTR(-EOPNOTSUPP);
2394}
2395
2396static inline void bpf_map_offload_map_free(struct bpf_map *map)
2397{
2398}
79a7f8bd
AS
2399
2400static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2401 const union bpf_attr *kattr,
2402 union bpf_attr __user *uattr)
2403{
2404 return -ENOTSUPP;
2405}
fdb5c453 2406
88759609 2407#ifdef CONFIG_BPF_SYSCALL
604326b4
DB
2408static inline int sock_map_get_from_fd(const union bpf_attr *attr,
2409 struct bpf_prog *prog)
fdb5c453
SY
2410{
2411 return -EINVAL;
2412}
bb0de313
LB
2413
2414static inline int sock_map_prog_detach(const union bpf_attr *attr,
2415 enum bpf_prog_type ptype)
2416{
2417 return -EOPNOTSUPP;
2418}
13b79d3f
LB
2419
2420static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2421 u64 flags)
2422{
2423 return -EOPNOTSUPP;
2424}
748cd572
DZ
2425
2426static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
2427 union bpf_attr __user *uattr)
2428{
2429 return -EINVAL;
2430}
17edea21
CW
2431#endif /* CONFIG_BPF_SYSCALL */
2432#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
5dc4c4b7 2433
17edea21
CW
2434#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2435void bpf_sk_reuseport_detach(struct sock *sk);
2436int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2437 void *value);
2438int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2439 void *value, u64 map_flags);
2440#else
2441static inline void bpf_sk_reuseport_detach(struct sock *sk)
2442{
2443}
5dc4c4b7 2444
17edea21 2445#ifdef CONFIG_BPF_SYSCALL
5dc4c4b7
MKL
2446static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2447 void *key, void *value)
2448{
2449 return -EOPNOTSUPP;
2450}
2451
2452static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2453 void *key, void *value,
2454 u64 map_flags)
2455{
2456 return -EOPNOTSUPP;
2457}
2458#endif /* CONFIG_BPF_SYSCALL */
2459#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2460
d0003ec0 2461/* verifier prototypes for helper functions called from eBPF programs */
a2c83fff
DB
2462extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2463extern const struct bpf_func_proto bpf_map_update_elem_proto;
2464extern const struct bpf_func_proto bpf_map_delete_elem_proto;
f1a2e44a
MV
2465extern const struct bpf_func_proto bpf_map_push_elem_proto;
2466extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2467extern const struct bpf_func_proto bpf_map_peek_elem_proto;
07343110 2468extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
d0003ec0 2469
03e69b50 2470extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
c04167ce 2471extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2d0e30c3 2472extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
04fd61ab 2473extern const struct bpf_func_proto bpf_tail_call_proto;
17ca8cbf 2474extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
71d19214 2475extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
c8996c98 2476extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
ffeedafb
AS
2477extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2478extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2479extern const struct bpf_func_proto bpf_get_current_comm_proto;
d5a3b1f6 2480extern const struct bpf_func_proto bpf_get_stackid_proto;
c195651e 2481extern const struct bpf_func_proto bpf_get_stack_proto;
fa28dcb8 2482extern const struct bpf_func_proto bpf_get_task_stack_proto;
7b04d6d6
SL
2483extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2484extern const struct bpf_func_proto bpf_get_stack_proto_pe;
174a79ff 2485extern const struct bpf_func_proto bpf_sock_map_update_proto;
81110384 2486extern const struct bpf_func_proto bpf_sock_hash_update_proto;
bf6fa2c8 2487extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
0f09abd1 2488extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
bed89185 2489extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
604326b4
DB
2490extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2491extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2492extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2493extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
d83525ca
AS
2494extern const struct bpf_func_proto bpf_spin_lock_proto;
2495extern const struct bpf_func_proto bpf_spin_unlock_proto;
cd339431 2496extern const struct bpf_func_proto bpf_get_local_storage_proto;
d7a4cb9b
AI
2497extern const struct bpf_func_proto bpf_strtol_proto;
2498extern const struct bpf_func_proto bpf_strtoul_proto;
0d01da6a 2499extern const struct bpf_func_proto bpf_tcp_sock_proto;
5576b991 2500extern const struct bpf_func_proto bpf_jiffies64_proto;
b4490c5c 2501extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
0456ea17 2502extern const struct bpf_func_proto bpf_event_output_data_proto;
457f4436
AN
2503extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2504extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2505extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2506extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2507extern const struct bpf_func_proto bpf_ringbuf_query_proto;
bc34dee6
JK
2508extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
2509extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
2510extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
af7ec138 2511extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
478cfbdf
YS
2512extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2513extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2514extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
0d4fad3e 2515extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
9eeb3aa3 2516extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
3bc253c2 2517extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
07be4c4a 2518extern const struct bpf_func_proto bpf_copy_from_user_proto;
c4d0bfb4 2519extern const struct bpf_func_proto bpf_snprintf_btf_proto;
7b15523a 2520extern const struct bpf_func_proto bpf_snprintf_proto;
eaa6bcb7 2521extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
63d9b80d 2522extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
d0551261 2523extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
b60da495 2524extern const struct bpf_func_proto bpf_sock_from_file_proto;
c5dbb89f 2525extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
a10787e6
SL
2526extern const struct bpf_func_proto bpf_task_storage_get_proto;
2527extern const struct bpf_func_proto bpf_task_storage_delete_proto;
69c087ba 2528extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
3d78417b 2529extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
3cee6fb8
MKL
2530extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2531extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
9113d7e4
SF
2532extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
2533extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
7c7e3d31 2534extern const struct bpf_func_proto bpf_find_vma_proto;
e6f2dd0f 2535extern const struct bpf_func_proto bpf_loop_proto;
376040e4 2536extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
69fd337a
SF
2537extern const struct bpf_func_proto bpf_set_retval_proto;
2538extern const struct bpf_func_proto bpf_get_retval_proto;
20571567 2539extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
cd339431 2540
958a3f2d
JO
2541const struct bpf_func_proto *tracing_prog_func_proto(
2542 enum bpf_func_id func_id, const struct bpf_prog *prog);
2543
3ad00405
DB
2544/* Shared helpers among cBPF and eBPF. */
2545void bpf_user_rnd_init_once(void);
2546u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
6890896b 2547u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3ad00405 2548
c64b7983 2549#if defined(CONFIG_NET)
46f8bc92
MKL
2550bool bpf_sock_common_is_valid_access(int off, int size,
2551 enum bpf_access_type type,
2552 struct bpf_insn_access_aux *info);
c64b7983
JS
2553bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2554 struct bpf_insn_access_aux *info);
2555u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2556 const struct bpf_insn *si,
2557 struct bpf_insn *insn_buf,
2558 struct bpf_prog *prog,
2559 u32 *target_size);
2560#else
46f8bc92
MKL
2561static inline bool bpf_sock_common_is_valid_access(int off, int size,
2562 enum bpf_access_type type,
2563 struct bpf_insn_access_aux *info)
2564{
2565 return false;
2566}
c64b7983
JS
2567static inline bool bpf_sock_is_valid_access(int off, int size,
2568 enum bpf_access_type type,
2569 struct bpf_insn_access_aux *info)
2570{
2571 return false;
2572}
2573static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2574 const struct bpf_insn *si,
2575 struct bpf_insn *insn_buf,
2576 struct bpf_prog *prog,
2577 u32 *target_size)
2578{
2579 return 0;
2580}
2581#endif
2582
655a51e5 2583#ifdef CONFIG_INET
91cc1a99
AS
2584struct sk_reuseport_kern {
2585 struct sk_buff *skb;
2586 struct sock *sk;
2587 struct sock *selected_sk;
d5e4ddae 2588 struct sock *migrating_sk;
91cc1a99
AS
2589 void *data_end;
2590 u32 hash;
2591 u32 reuseport_id;
2592 bool bind_inany;
2593};
655a51e5
MKL
2594bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2595 struct bpf_insn_access_aux *info);
2596
2597u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2598 const struct bpf_insn *si,
2599 struct bpf_insn *insn_buf,
2600 struct bpf_prog *prog,
2601 u32 *target_size);
7f94208c
Y
2602
2603bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2604 struct bpf_insn_access_aux *info);
2605
2606u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2607 const struct bpf_insn *si,
2608 struct bpf_insn *insn_buf,
2609 struct bpf_prog *prog,
2610 u32 *target_size);
655a51e5
MKL
2611#else
2612static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2613 enum bpf_access_type type,
2614 struct bpf_insn_access_aux *info)
2615{
2616 return false;
2617}
2618
2619static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2620 const struct bpf_insn *si,
2621 struct bpf_insn *insn_buf,
2622 struct bpf_prog *prog,
2623 u32 *target_size)
2624{
2625 return 0;
2626}
7f94208c
Y
2627static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2628 enum bpf_access_type type,
2629 struct bpf_insn_access_aux *info)
2630{
2631 return false;
2632}
2633
2634static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2635 const struct bpf_insn *si,
2636 struct bpf_insn *insn_buf,
2637 struct bpf_prog *prog,
2638 u32 *target_size)
2639{
2640 return 0;
2641}
655a51e5
MKL
2642#endif /* CONFIG_INET */
2643
5964b200 2644enum bpf_text_poke_type {
b553a6ec
DB
2645 BPF_MOD_CALL,
2646 BPF_MOD_JUMP,
5964b200 2647};
4b3da77b 2648
5964b200
AS
2649int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2650 void *addr1, void *addr2);
2651
ebc1415d 2652void *bpf_arch_text_copy(void *dst, void *src, size_t len);
fe736565 2653int bpf_arch_text_invalidate(void *dst, size_t len);
ebc1415d 2654
eae2e83e 2655struct btf_id_set;
2af30f11 2656bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
eae2e83e 2657
335ff499
DM
2658#define MAX_BPRINTF_VARARGS 12
2659
48cac3f4
FR
2660int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2661 u32 **bin_buf, u32 num_args);
2662void bpf_bprintf_cleanup(void);
d9c9e4db 2663
97e03f52
JK
2664/* the implementation of the opaque uapi struct bpf_dynptr */
2665struct bpf_dynptr_kern {
2666 void *data;
2667 /* Size represents the number of usable bytes of dynptr data.
2668 * If for example the offset is at 4 for a local dynptr whose data is
2669 * of type u64, the number of usable bytes is 4.
2670 *
2671 * The upper 8 bits are reserved. It is as follows:
2672 * Bits 0 - 23 = size
2673 * Bits 24 - 30 = dynptr type
2674 * Bit 31 = whether dynptr is read-only
2675 */
2676 u32 size;
2677 u32 offset;
2678} __aligned(8);
2679
2680enum bpf_dynptr_type {
2681 BPF_DYNPTR_TYPE_INVALID,
2682 /* Points to memory that is local to the bpf program */
2683 BPF_DYNPTR_TYPE_LOCAL,
20571567 2684 /* Underlying data is a kernel-produced ringbuf record */
bc34dee6 2685 BPF_DYNPTR_TYPE_RINGBUF,
97e03f52
JK
2686};
2687
bc34dee6
JK
2688void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2689 enum bpf_dynptr_type type, u32 offset, u32 size);
2690void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2691int bpf_dynptr_check_size(u32 size);
51df4865 2692u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr);
bc34dee6 2693
c0e19f2c
SF
2694#ifdef CONFIG_BPF_LSM
2695void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
2696void bpf_cgroup_atype_put(int cgroup_atype);
2697#else
2698static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
2699static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
2700#endif /* CONFIG_BPF_LSM */
2701
f3cf4134
RS
2702struct key;
2703
2704#ifdef CONFIG_KEYS
2705struct bpf_key {
2706 struct key *key;
2707 bool has_ref;
2708};
2709#endif /* CONFIG_KEYS */
99c55f7d 2710#endif /* _LINUX_BPF_H */