bpf: Refactor some inode/task/sk storage functions for reuse
[linux-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 856 void *orig_call);
271de525
MKL
857u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
858 struct bpf_tramp_run_ctx *run_ctx);
859void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,
860 struct bpf_tramp_run_ctx *run_ctx);
e21aa341
AS
861void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
862void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
271de525
MKL
863typedef u64 (*bpf_trampoline_enter_t)(struct bpf_prog *prog,
864 struct bpf_tramp_run_ctx *run_ctx);
865typedef void (*bpf_trampoline_exit_t)(struct bpf_prog *prog, u64 start,
866 struct bpf_tramp_run_ctx *run_ctx);
867bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog);
868bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog);
fec56f58 869
535911c8
JO
870struct bpf_ksym {
871 unsigned long start;
872 unsigned long end;
bfea9a85 873 char name[KSYM_NAME_LEN];
ecb60d1c 874 struct list_head lnode;
ca4424c9 875 struct latch_tree_node tnode;
cbd76f8d 876 bool prog;
535911c8
JO
877};
878
fec56f58
AS
879enum bpf_tramp_prog_type {
880 BPF_TRAMP_FENTRY,
881 BPF_TRAMP_FEXIT,
ae240823 882 BPF_TRAMP_MODIFY_RETURN,
be8704ff
AS
883 BPF_TRAMP_MAX,
884 BPF_TRAMP_REPLACE, /* more than MAX */
fec56f58
AS
885};
886
e21aa341
AS
887struct bpf_tramp_image {
888 void *image;
889 struct bpf_ksym ksym;
890 struct percpu_ref pcref;
891 void *ip_after_call;
892 void *ip_epilogue;
893 union {
894 struct rcu_head rcu;
895 struct work_struct work;
896 };
897};
898
fec56f58
AS
899struct bpf_trampoline {
900 /* hlist for trampoline_table */
901 struct hlist_node hlist;
00963a2e 902 struct ftrace_ops *fops;
fec56f58
AS
903 /* serializes access to fields of this trampoline */
904 struct mutex mutex;
905 refcount_t refcnt;
00963a2e 906 u32 flags;
fec56f58
AS
907 u64 key;
908 struct {
909 struct btf_func_model model;
910 void *addr;
b91e014f 911 bool ftrace_managed;
fec56f58 912 } func;
be8704ff
AS
913 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
914 * program by replacing one of its functions. func.addr is the address
915 * of the function it replaced.
916 */
917 struct bpf_prog *extension_prog;
fec56f58
AS
918 /* list of BPF programs using this trampoline */
919 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
920 /* Number of attached programs. A counter per kind. */
921 int progs_cnt[BPF_TRAMP_MAX];
922 /* Executable image of trampoline */
e21aa341 923 struct bpf_tramp_image *cur_image;
fec56f58 924 u64 selector;
861de02e 925 struct module *mod;
fec56f58 926};
75ccbef6 927
f7b12b6f
THJ
928struct bpf_attach_target_info {
929 struct btf_func_model fmodel;
930 long tgt_addr;
931 const char *tgt_name;
932 const struct btf_type *tgt_type;
933};
934
116eb788 935#define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
75ccbef6
BT
936
937struct bpf_dispatcher_prog {
938 struct bpf_prog *prog;
939 refcount_t users;
940};
941
942struct bpf_dispatcher {
943 /* dispatcher mutex */
944 struct mutex mutex;
945 void *func;
946 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
947 int num_progs;
948 void *image;
19c02415 949 void *rw_image;
75ccbef6 950 u32 image_off;
517b75e4 951 struct bpf_ksym ksym;
75ccbef6
BT
952};
953
9f5b4009 954static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
7e6897f9
BT
955 const void *ctx,
956 const struct bpf_insn *insnsi,
af3f4134 957 bpf_func_t bpf_func)
7e6897f9
BT
958{
959 return bpf_func(ctx, insnsi);
960}
f7e0beaf 961
fec56f58 962#ifdef CONFIG_BPF_JIT
f7e0beaf
KFL
963int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
964int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
f7b12b6f
THJ
965struct bpf_trampoline *bpf_trampoline_get(u64 key,
966 struct bpf_attach_target_info *tgt_info);
fec56f58 967void bpf_trampoline_put(struct bpf_trampoline *tr);
19c02415 968int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
517b75e4
JO
969#define BPF_DISPATCHER_INIT(_name) { \
970 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
971 .func = &_name##_func, \
972 .progs = {}, \
973 .num_progs = 0, \
974 .image = NULL, \
975 .image_off = 0, \
976 .ksym = { \
977 .name = #_name, \
978 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
979 }, \
75ccbef6
BT
980}
981
ceea991a
JO
982#ifdef CONFIG_X86_64
983#define BPF_DISPATCHER_ATTRIBUTES __attribute__((patchable_function_entry(5)))
984#else
985#define BPF_DISPATCHER_ATTRIBUTES
986#endif
987
75ccbef6 988#define DEFINE_BPF_DISPATCHER(name) \
ceea991a 989 notrace BPF_DISPATCHER_ATTRIBUTES \
9f5b4009 990 noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \
75ccbef6
BT
991 const void *ctx, \
992 const struct bpf_insn *insnsi, \
af3f4134 993 bpf_func_t bpf_func) \
75ccbef6
BT
994 { \
995 return bpf_func(ctx, insnsi); \
996 } \
6a64037d
BT
997 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
998 struct bpf_dispatcher bpf_dispatcher_##name = \
999 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
75ccbef6 1000#define DECLARE_BPF_DISPATCHER(name) \
6a64037d 1001 unsigned int bpf_dispatcher_##name##_func( \
75ccbef6
BT
1002 const void *ctx, \
1003 const struct bpf_insn *insnsi, \
af3f4134 1004 bpf_func_t bpf_func); \
6a64037d
BT
1005 extern struct bpf_dispatcher bpf_dispatcher_##name;
1006#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1007#define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
75ccbef6
BT
1008void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1009 struct bpf_prog *to);
dba122fb 1010/* Called only from JIT-enabled code, so there's no need for stubs. */
a108f7dc
JO
1011void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
1012void bpf_image_ksym_del(struct bpf_ksym *ksym);
dba122fb
JO
1013void bpf_ksym_add(struct bpf_ksym *ksym);
1014void bpf_ksym_del(struct bpf_ksym *ksym);
3486bedd
SL
1015int bpf_jit_charge_modmem(u32 size);
1016void bpf_jit_uncharge_modmem(u32 size);
f92c1e18 1017bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
fec56f58 1018#else
f7e0beaf 1019static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
3aac1ead 1020 struct bpf_trampoline *tr)
fec56f58
AS
1021{
1022 return -ENOTSUPP;
1023}
f7e0beaf 1024static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
3aac1ead 1025 struct bpf_trampoline *tr)
fec56f58
AS
1026{
1027 return -ENOTSUPP;
1028}
f7b12b6f
THJ
1029static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1030 struct bpf_attach_target_info *tgt_info)
1031{
1032 return ERR_PTR(-EOPNOTSUPP);
1033}
fec56f58 1034static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
75ccbef6
BT
1035#define DEFINE_BPF_DISPATCHER(name)
1036#define DECLARE_BPF_DISPATCHER(name)
6a64037d 1037#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
75ccbef6
BT
1038#define BPF_DISPATCHER_PTR(name) NULL
1039static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1040 struct bpf_prog *from,
1041 struct bpf_prog *to) {}
e9b4e606
JO
1042static inline bool is_bpf_image_address(unsigned long address)
1043{
1044 return false;
1045}
f92c1e18
JO
1046static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1047{
1048 return false;
1049}
fec56f58
AS
1050#endif
1051
8c1b6e69 1052struct bpf_func_info_aux {
51c39bb1 1053 u16 linkage;
8c1b6e69
AS
1054 bool unreliable;
1055};
1056
a66886fe
DB
1057enum bpf_jit_poke_reason {
1058 BPF_POKE_REASON_TAIL_CALL,
1059};
1060
1061/* Descriptor of pokes pointing /into/ the JITed image. */
1062struct bpf_jit_poke_descriptor {
cf71b174 1063 void *tailcall_target;
ebf7d1f5
MF
1064 void *tailcall_bypass;
1065 void *bypass_addr;
f263a814 1066 void *aux;
a66886fe
DB
1067 union {
1068 struct {
1069 struct bpf_map *map;
1070 u32 key;
1071 } tail_call;
1072 };
cf71b174 1073 bool tailcall_target_stable;
a66886fe
DB
1074 u8 adj_off;
1075 u16 reason;
a748c697 1076 u32 insn_idx;
a66886fe
DB
1077};
1078
3c32cc1b
YS
1079/* reg_type info for ctx arguments */
1080struct bpf_ctx_arg_aux {
1081 u32 offset;
1082 enum bpf_reg_type reg_type;
951cf368 1083 u32 btf_id;
3c32cc1b
YS
1084};
1085
541c3bad
AN
1086struct btf_mod_pair {
1087 struct btf *btf;
1088 struct module *module;
1089};
1090
e6ac2450
MKL
1091struct bpf_kfunc_desc_tab;
1092
09756af4 1093struct bpf_prog_aux {
85192dbf 1094 atomic64_t refcnt;
24701ece 1095 u32 used_map_cnt;
541c3bad 1096 u32 used_btf_cnt;
32bbe007 1097 u32 max_ctx_offset;
e647815a 1098 u32 max_pkt_offset;
9df1c28b 1099 u32 max_tp_access;
8726679a 1100 u32 stack_depth;
dc4bb0e2 1101 u32 id;
ba64e7d8
YS
1102 u32 func_cnt; /* used by non-func prog as the number of func progs */
1103 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
ccfe29eb 1104 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
3c32cc1b 1105 u32 ctx_arg_info_size;
afbf21dc
YS
1106 u32 max_rdonly_access;
1107 u32 max_rdwr_access;
22dc4a0f 1108 struct btf *attach_btf;
3c32cc1b 1109 const struct bpf_ctx_arg_aux *ctx_arg_info;
3aac1ead
THJ
1110 struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1111 struct bpf_prog *dst_prog;
1112 struct bpf_trampoline *dst_trampoline;
4a1e7c0c
THJ
1113 enum bpf_prog_type saved_dst_prog_type;
1114 enum bpf_attach_type saved_dst_attach_type;
a4b1d3c1 1115 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
9a18eedb 1116 bool offload_requested;
38207291 1117 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
8c1b6e69 1118 bool func_proto_unreliable;
1e6c62a8 1119 bool sleepable;
ebf7d1f5 1120 bool tail_call_reachable;
c2f2cdbe 1121 bool xdp_has_frags;
38207291
MKL
1122 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1123 const struct btf_type *attach_func_proto;
1124 /* function name for valid attach_btf_id */
1125 const char *attach_func_name;
1c2a088a
AS
1126 struct bpf_prog **func;
1127 void *jit_data; /* JIT specific data. arch dependent */
a66886fe 1128 struct bpf_jit_poke_descriptor *poke_tab;
e6ac2450 1129 struct bpf_kfunc_desc_tab *kfunc_tab;
2357672c 1130 struct bpf_kfunc_btf_tab *kfunc_btf_tab;
a66886fe 1131 u32 size_poke_tab;
535911c8 1132 struct bpf_ksym ksym;
7de16e3a 1133 const struct bpf_prog_ops *ops;
09756af4 1134 struct bpf_map **used_maps;
984fe94f 1135 struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
541c3bad 1136 struct btf_mod_pair *used_btfs;
09756af4 1137 struct bpf_prog *prog;
aaac3ba9 1138 struct user_struct *user;
cb4d2b3f 1139 u64 load_time; /* ns since boottime */
aba64c7d 1140 u32 verified_insns;
69fd337a 1141 int cgroup_atype; /* enum cgroup_bpf_attach_type */
8bad74f9 1142 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
067cae47 1143 char name[BPF_OBJ_NAME_LEN];
afdb09c7
CF
1144#ifdef CONFIG_SECURITY
1145 void *security;
1146#endif
0a9c1991 1147 struct bpf_prog_offload *offload;
838e9690 1148 struct btf *btf;
ba64e7d8 1149 struct bpf_func_info *func_info;
8c1b6e69 1150 struct bpf_func_info_aux *func_info_aux;
c454a46b
MKL
1151 /* bpf_line_info loaded from userspace. linfo->insn_off
1152 * has the xlated insn offset.
1153 * Both the main and sub prog share the same linfo.
1154 * The subprog can access its first linfo by
1155 * using the linfo_idx.
1156 */
1157 struct bpf_line_info *linfo;
1158 /* jited_linfo is the jited addr of the linfo. It has a
1159 * one to one mapping to linfo:
1160 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1161 * Both the main and sub prog share the same jited_linfo.
1162 * The subprog can access its first jited_linfo by
1163 * using the linfo_idx.
1164 */
1165 void **jited_linfo;
ba64e7d8 1166 u32 func_info_cnt;
c454a46b
MKL
1167 u32 nr_linfo;
1168 /* subprog can use linfo_idx to access its first linfo and
1169 * jited_linfo.
1170 * main prog always has linfo_idx == 0
1171 */
1172 u32 linfo_idx;
3dec541b
AS
1173 u32 num_exentries;
1174 struct exception_table_entry *extable;
abf2e7d6
AS
1175 union {
1176 struct work_struct work;
1177 struct rcu_head rcu;
1178 };
09756af4
AS
1179};
1180
d687f621
DK
1181struct bpf_prog {
1182 u16 pages; /* Number of allocated pages */
1183 u16 jited:1, /* Is our filter JIT'ed? */
1184 jit_requested:1,/* archs need to JIT the prog */
1185 gpl_compatible:1, /* Is filter GPL compatible? */
1186 cb_access:1, /* Is control block accessed? */
1187 dst_needed:1, /* Do we need dst entry? */
1188 blinding_requested:1, /* needs constant blinding */
1189 blinded:1, /* Was blinded */
1190 is_func:1, /* program is a bpf function */
1191 kprobe_override:1, /* Do we override a kprobe? */
1192 has_callchain_buf:1, /* callchain buffer allocated? */
1193 enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1194 call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1195 call_get_func_ip:1, /* Do we call get_func_ip() */
1196 tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
1197 enum bpf_prog_type type; /* Type of BPF program */
1198 enum bpf_attach_type expected_attach_type; /* For some prog types */
1199 u32 len; /* Number of filter blocks */
1200 u32 jited_len; /* Size of jited insns in bytes */
1201 u8 tag[BPF_TAG_SIZE];
1202 struct bpf_prog_stats __percpu *stats;
1203 int __percpu *active;
1204 unsigned int (*bpf_func)(const void *ctx,
1205 const struct bpf_insn *insn);
1206 struct bpf_prog_aux *aux; /* Auxiliary fields */
1207 struct sock_fprog_kern *orig_prog; /* Original BPF program */
1208 /* Instructions for interpreter */
1209 union {
1210 DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1211 DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1212 };
1213};
1214
2beee5f5 1215struct bpf_array_aux {
da765a2f
DB
1216 /* Programs with direct jumps into programs part of this array. */
1217 struct list_head poke_progs;
1218 struct bpf_map *map;
1219 struct mutex poke_mutex;
1220 struct work_struct work;
2beee5f5
DB
1221};
1222
6cc7d1e8
AN
1223struct bpf_link {
1224 atomic64_t refcnt;
1225 u32 id;
1226 enum bpf_link_type type;
1227 const struct bpf_link_ops *ops;
1228 struct bpf_prog *prog;
1229 struct work_struct work;
1230};
1231
1232struct bpf_link_ops {
1233 void (*release)(struct bpf_link *link);
1234 void (*dealloc)(struct bpf_link *link);
73b11c2a 1235 int (*detach)(struct bpf_link *link);
6cc7d1e8
AN
1236 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1237 struct bpf_prog *old_prog);
1238 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1239 int (*fill_link_info)(const struct bpf_link *link,
1240 struct bpf_link_info *info);
1241};
1242
f7e0beaf
KFL
1243struct bpf_tramp_link {
1244 struct bpf_link link;
1245 struct hlist_node tramp_hlist;
2fcc8241 1246 u64 cookie;
f7e0beaf
KFL
1247};
1248
69fd337a
SF
1249struct bpf_shim_tramp_link {
1250 struct bpf_tramp_link link;
1251 struct bpf_trampoline *trampoline;
1252};
1253
f7e0beaf
KFL
1254struct bpf_tracing_link {
1255 struct bpf_tramp_link link;
1256 enum bpf_attach_type attach_type;
1257 struct bpf_trampoline *trampoline;
1258 struct bpf_prog *tgt_prog;
1259};
1260
6cc7d1e8
AN
1261struct bpf_link_primer {
1262 struct bpf_link *link;
1263 struct file *file;
1264 int fd;
1265 u32 id;
1266};
1267
85d33df3 1268struct bpf_struct_ops_value;
27ae7997
MKL
1269struct btf_member;
1270
1271#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1272struct bpf_struct_ops {
1273 const struct bpf_verifier_ops *verifier_ops;
1274 int (*init)(struct btf *btf);
1275 int (*check_member)(const struct btf_type *t,
1276 const struct btf_member *member);
85d33df3
MKL
1277 int (*init_member)(const struct btf_type *t,
1278 const struct btf_member *member,
1279 void *kdata, const void *udata);
1280 int (*reg)(void *kdata);
1281 void (*unreg)(void *kdata);
27ae7997 1282 const struct btf_type *type;
85d33df3 1283 const struct btf_type *value_type;
27ae7997
MKL
1284 const char *name;
1285 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1286 u32 type_id;
85d33df3 1287 u32 value_id;
27ae7997
MKL
1288};
1289
1290#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
85d33df3 1291#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
27ae7997 1292const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
d3e42bb0 1293void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
85d33df3
MKL
1294bool bpf_struct_ops_get(const void *kdata);
1295void bpf_struct_ops_put(const void *kdata);
1296int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1297 void *value);
f7e0beaf
KFL
1298int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1299 struct bpf_tramp_link *link,
31a645ae
HT
1300 const struct btf_func_model *model,
1301 void *image, void *image_end);
85d33df3
MKL
1302static inline bool bpf_try_module_get(const void *data, struct module *owner)
1303{
1304 if (owner == BPF_MODULE_OWNER)
1305 return bpf_struct_ops_get(data);
1306 else
1307 return try_module_get(owner);
1308}
1309static inline void bpf_module_put(const void *data, struct module *owner)
1310{
1311 if (owner == BPF_MODULE_OWNER)
1312 bpf_struct_ops_put(data);
1313 else
1314 module_put(owner);
1315}
c196906d
HT
1316
1317#ifdef CONFIG_NET
1318/* Define it here to avoid the use of forward declaration */
1319struct bpf_dummy_ops_state {
1320 int val;
1321};
1322
1323struct bpf_dummy_ops {
1324 int (*test_1)(struct bpf_dummy_ops_state *cb);
1325 int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1326 char a3, unsigned long a4);
1327};
1328
1329int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1330 union bpf_attr __user *uattr);
1331#endif
27ae7997
MKL
1332#else
1333static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
1334{
1335 return NULL;
1336}
d3e42bb0
MKL
1337static inline void bpf_struct_ops_init(struct btf *btf,
1338 struct bpf_verifier_log *log)
1339{
1340}
85d33df3
MKL
1341static inline bool bpf_try_module_get(const void *data, struct module *owner)
1342{
1343 return try_module_get(owner);
1344}
1345static inline void bpf_module_put(const void *data, struct module *owner)
1346{
1347 module_put(owner);
1348}
1349static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1350 void *key,
1351 void *value)
1352{
1353 return -EINVAL;
1354}
9cb61fda
SF
1355#endif
1356
1357#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1358int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1359 int cgroup_atype);
1360void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1361#else
69fd337a
SF
1362static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1363 int cgroup_atype)
1364{
1365 return -EOPNOTSUPP;
1366}
1367static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1368{
1369}
27ae7997
MKL
1370#endif
1371
04fd61ab
AS
1372struct bpf_array {
1373 struct bpf_map map;
1374 u32 elem_size;
b2157399 1375 u32 index_mask;
2beee5f5 1376 struct bpf_array_aux *aux;
04fd61ab
AS
1377 union {
1378 char value[0] __aligned(8);
2a36f0b9 1379 void *ptrs[0] __aligned(8);
a10423b8 1380 void __percpu *pptrs[0] __aligned(8);
04fd61ab
AS
1381 };
1382};
3b1efb19 1383
c04c0d2b 1384#define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
ebf7f6f0 1385#define MAX_TAIL_CALL_CNT 33
04fd61ab 1386
1ade2371
EZ
1387/* Maximum number of loops for bpf_loop */
1388#define BPF_MAX_LOOPS BIT(23)
1389
591fe988
DB
1390#define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
1391 BPF_F_RDONLY_PROG | \
1392 BPF_F_WRONLY | \
1393 BPF_F_WRONLY_PROG)
1394
1395#define BPF_MAP_CAN_READ BIT(0)
1396#define BPF_MAP_CAN_WRITE BIT(1)
1397
20571567
DV
1398/* Maximum number of user-producer ring buffer samples that can be drained in
1399 * a call to bpf_user_ringbuf_drain().
1400 */
1401#define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
1402
591fe988
DB
1403static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1404{
1405 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1406
1407 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1408 * not possible.
1409 */
1410 if (access_flags & BPF_F_RDONLY_PROG)
1411 return BPF_MAP_CAN_READ;
1412 else if (access_flags & BPF_F_WRONLY_PROG)
1413 return BPF_MAP_CAN_WRITE;
1414 else
1415 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1416}
1417
1418static inline bool bpf_map_flags_access_ok(u32 access_flags)
1419{
1420 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1421 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1422}
1423
3b1efb19
DB
1424struct bpf_event_entry {
1425 struct perf_event *event;
1426 struct file *perf_file;
1427 struct file *map_file;
1428 struct rcu_head rcu;
1429};
1430
f45d5b6c
THJ
1431static inline bool map_type_contains_progs(struct bpf_map *map)
1432{
1433 return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
1434 map->map_type == BPF_MAP_TYPE_DEVMAP ||
1435 map->map_type == BPF_MAP_TYPE_CPUMAP;
1436}
1437
1438bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
f1f7714e 1439int bpf_prog_calc_tag(struct bpf_prog *fp);
bd570ff9 1440
0756ea3e 1441const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
10aceb62 1442const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
555c8a86
DB
1443
1444typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
aa7145c1 1445 unsigned long off, unsigned long len);
c64b7983
JS
1446typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
1447 const struct bpf_insn *src,
1448 struct bpf_insn *dst,
1449 struct bpf_prog *prog,
1450 u32 *target_size);
555c8a86
DB
1451
1452u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
1453 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
04fd61ab 1454
324bda9e
AS
1455/* an array of programs to be executed under rcu_lock.
1456 *
1457 * Typical usage:
055eb955 1458 * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
324bda9e
AS
1459 *
1460 * the structure returned by bpf_prog_array_alloc() should be populated
1461 * with program pointers and the last pointer must be NULL.
1462 * The user has to keep refcnt on the program and make sure the program
1463 * is removed from the array before bpf_prog_put().
1464 * The 'struct bpf_prog_array *' should only be replaced with xchg()
1465 * since other cpus are walking the array of pointers in parallel.
1466 */
394e40a2
RG
1467struct bpf_prog_array_item {
1468 struct bpf_prog *prog;
82e6b1ee
AN
1469 union {
1470 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1471 u64 bpf_cookie;
1472 };
394e40a2
RG
1473};
1474
324bda9e
AS
1475struct bpf_prog_array {
1476 struct rcu_head rcu;
d7f10df8 1477 struct bpf_prog_array_item items[];
324bda9e
AS
1478};
1479
46531a30
PB
1480struct bpf_empty_prog_array {
1481 struct bpf_prog_array hdr;
1482 struct bpf_prog *null_prog;
1483};
1484
1485/* to avoid allocating empty bpf_prog_array for cgroups that
1486 * don't have bpf program attached use one global 'bpf_empty_prog_array'
1487 * It will not be modified the caller of bpf_prog_array_alloc()
1488 * (since caller requested prog_cnt == 0)
1489 * that pointer should be 'freed' by bpf_prog_array_free()
1490 */
1491extern struct bpf_empty_prog_array bpf_empty_prog_array;
1492
d29ab6e1 1493struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
54e9c9d4 1494void bpf_prog_array_free(struct bpf_prog_array *progs);
8c7dcb84
DK
1495/* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
1496void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
54e9c9d4 1497int bpf_prog_array_length(struct bpf_prog_array *progs);
0d01da6a 1498bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
54e9c9d4 1499int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
468e2f64 1500 __u32 __user *prog_ids, u32 cnt);
324bda9e 1501
54e9c9d4 1502void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
e87c6bc3 1503 struct bpf_prog *old_prog);
ce3aa9cc
JS
1504int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
1505int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1506 struct bpf_prog *prog);
54e9c9d4 1507int bpf_prog_array_copy_info(struct bpf_prog_array *array,
3a38bb98
YS
1508 u32 *prog_ids, u32 request_cnt,
1509 u32 *prog_cnt);
54e9c9d4 1510int bpf_prog_array_copy(struct bpf_prog_array *old_array,
e87c6bc3
YS
1511 struct bpf_prog *exclude_prog,
1512 struct bpf_prog *include_prog,
82e6b1ee 1513 u64 bpf_cookie,
e87c6bc3
YS
1514 struct bpf_prog_array **new_array);
1515
c7603cfa
AN
1516struct bpf_run_ctx {};
1517
1518struct bpf_cg_run_ctx {
1519 struct bpf_run_ctx run_ctx;
7d08c2c9 1520 const struct bpf_prog_array_item *prog_item;
c4dcfdd4 1521 int retval;
c7603cfa
AN
1522};
1523
82e6b1ee
AN
1524struct bpf_trace_run_ctx {
1525 struct bpf_run_ctx run_ctx;
1526 u64 bpf_cookie;
1527};
1528
e384c7b7
KFL
1529struct bpf_tramp_run_ctx {
1530 struct bpf_run_ctx run_ctx;
1531 u64 bpf_cookie;
1532 struct bpf_run_ctx *saved_run_ctx;
1533};
1534
7d08c2c9
AN
1535static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
1536{
1537 struct bpf_run_ctx *old_ctx = NULL;
1538
1539#ifdef CONFIG_BPF_SYSCALL
1540 old_ctx = current->bpf_ctx;
1541 current->bpf_ctx = new_ctx;
1542#endif
1543 return old_ctx;
1544}
1545
1546static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
1547{
1548#ifdef CONFIG_BPF_SYSCALL
1549 current->bpf_ctx = old_ctx;
1550#endif
1551}
1552
77241217
SF
1553/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
1554#define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)
1555/* BPF program asks to set CN on the packet. */
1556#define BPF_RET_SET_CN (1 << 0)
1557
7d08c2c9
AN
1558typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
1559
7d08c2c9 1560static __always_inline u32
055eb955 1561bpf_prog_run_array(const struct bpf_prog_array *array,
7d08c2c9
AN
1562 const void *ctx, bpf_prog_run_fn run_prog)
1563{
1564 const struct bpf_prog_array_item *item;
1565 const struct bpf_prog *prog;
82e6b1ee
AN
1566 struct bpf_run_ctx *old_run_ctx;
1567 struct bpf_trace_run_ctx run_ctx;
7d08c2c9
AN
1568 u32 ret = 1;
1569
055eb955
SF
1570 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
1571
7d08c2c9 1572 if (unlikely(!array))
055eb955
SF
1573 return ret;
1574
1575 migrate_disable();
82e6b1ee 1576 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
7d08c2c9
AN
1577 item = &array->items[0];
1578 while ((prog = READ_ONCE(item->prog))) {
82e6b1ee 1579 run_ctx.bpf_cookie = item->bpf_cookie;
7d08c2c9
AN
1580 ret &= run_prog(prog, ctx);
1581 item++;
1582 }
82e6b1ee 1583 bpf_reset_run_ctx(old_run_ctx);
7d08c2c9
AN
1584 migrate_enable();
1585 return ret;
1586}
324bda9e 1587
8c7dcb84
DK
1588/* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
1589 *
1590 * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
1591 * overall. As a result, we must use the bpf_prog_array_free_sleepable
1592 * in order to use the tasks_trace rcu grace period.
1593 *
1594 * When a non-sleepable program is inside the array, we take the rcu read
1595 * section and disable preemption for that program alone, so it can access
1596 * rcu-protected dynamically sized maps.
1597 */
1598static __always_inline u32
1599bpf_prog_run_array_sleepable(const struct bpf_prog_array __rcu *array_rcu,
1600 const void *ctx, bpf_prog_run_fn run_prog)
1601{
1602 const struct bpf_prog_array_item *item;
1603 const struct bpf_prog *prog;
1604 const struct bpf_prog_array *array;
1605 struct bpf_run_ctx *old_run_ctx;
1606 struct bpf_trace_run_ctx run_ctx;
1607 u32 ret = 1;
1608
1609 might_fault();
1610
1611 rcu_read_lock_trace();
1612 migrate_disable();
1613
1614 array = rcu_dereference_check(array_rcu, rcu_read_lock_trace_held());
1615 if (unlikely(!array))
1616 goto out;
1617 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
1618 item = &array->items[0];
1619 while ((prog = READ_ONCE(item->prog))) {
1620 if (!prog->aux->sleepable)
1621 rcu_read_lock();
1622
1623 run_ctx.bpf_cookie = item->bpf_cookie;
1624 ret &= run_prog(prog, ctx);
1625 item++;
1626
1627 if (!prog->aux->sleepable)
1628 rcu_read_unlock();
1629 }
1630 bpf_reset_run_ctx(old_run_ctx);
1631out:
1632 migrate_enable();
1633 rcu_read_unlock_trace();
1634 return ret;
1635}
1636
89aa0758 1637#ifdef CONFIG_BPF_SYSCALL
b121d1e7 1638DECLARE_PER_CPU(int, bpf_prog_active);
d46edd67 1639extern struct mutex bpf_stats_enabled_mutex;
b121d1e7 1640
c518cfa0
TG
1641/*
1642 * Block execution of BPF programs attached to instrumentation (perf,
1643 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
1644 * these events can happen inside a region which holds a map bucket lock
1645 * and can deadlock on it.
c518cfa0
TG
1646 */
1647static inline void bpf_disable_instrumentation(void)
1648{
1649 migrate_disable();
79364031 1650 this_cpu_inc(bpf_prog_active);
c518cfa0
TG
1651}
1652
1653static inline void bpf_enable_instrumentation(void)
1654{
79364031 1655 this_cpu_dec(bpf_prog_active);
c518cfa0
TG
1656 migrate_enable();
1657}
1658
f66e448c
CF
1659extern const struct file_operations bpf_map_fops;
1660extern const struct file_operations bpf_prog_fops;
367ec3e4 1661extern const struct file_operations bpf_iter_fops;
f66e448c 1662
91cc1a99 1663#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
7de16e3a
JK
1664 extern const struct bpf_prog_ops _name ## _prog_ops; \
1665 extern const struct bpf_verifier_ops _name ## _verifier_ops;
40077e0c
JB
1666#define BPF_MAP_TYPE(_id, _ops) \
1667 extern const struct bpf_map_ops _ops;
f2e10bff 1668#define BPF_LINK_TYPE(_id, _name)
be9370a7
JB
1669#include <linux/bpf_types.h>
1670#undef BPF_PROG_TYPE
40077e0c 1671#undef BPF_MAP_TYPE
f2e10bff 1672#undef BPF_LINK_TYPE
0fc174de 1673
ab3f0063 1674extern const struct bpf_prog_ops bpf_offload_prog_ops;
4f9218aa
JK
1675extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
1676extern const struct bpf_verifier_ops xdp_analyzer_ops;
1677
0fc174de 1678struct bpf_prog *bpf_prog_get(u32 ufd);
248f346f 1679struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
288b3de5 1680 bool attach_drv);
85192dbf 1681void bpf_prog_add(struct bpf_prog *prog, int i);
c540594f 1682void bpf_prog_sub(struct bpf_prog *prog, int i);
85192dbf 1683void bpf_prog_inc(struct bpf_prog *prog);
a6f6df69 1684struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
61e021f3
DB
1685void bpf_prog_put(struct bpf_prog *prog);
1686
ad8ad79f 1687void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
a3884572 1688void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
ad8ad79f 1689
61df10c7
KKD
1690struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset);
1691void bpf_map_free_kptr_off_tab(struct bpf_map *map);
1692struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map);
1693bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b);
14a324f6 1694void bpf_map_free_kptrs(struct bpf_map *map, void *map_value);
61df10c7 1695
1ed4d924 1696struct bpf_map *bpf_map_get(u32 ufd);
c9da161c 1697struct bpf_map *bpf_map_get_with_uref(u32 ufd);
c2101297 1698struct bpf_map *__bpf_map_get(struct fd f);
1e0bd5a0
AN
1699void bpf_map_inc(struct bpf_map *map);
1700void bpf_map_inc_with_uref(struct bpf_map *map);
1701struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
c9da161c 1702void bpf_map_put_with_uref(struct bpf_map *map);
61e021f3 1703void bpf_map_put(struct bpf_map *map);
196e8ca7
DB
1704void *bpf_map_area_alloc(u64 size, int numa_node);
1705void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
d407bd25 1706void bpf_map_area_free(void *base);
353050be 1707bool bpf_map_write_active(const struct bpf_map *map);
bd475643 1708void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
cb4d03ab
BV
1709int generic_map_lookup_batch(struct bpf_map *map,
1710 const union bpf_attr *attr,
aa2e93b8
BV
1711 union bpf_attr __user *uattr);
1712int generic_map_update_batch(struct bpf_map *map,
1713 const union bpf_attr *attr,
1714 union bpf_attr __user *uattr);
1715int generic_map_delete_batch(struct bpf_map *map,
1716 const union bpf_attr *attr,
cb4d03ab 1717 union bpf_attr __user *uattr);
6086d29d 1718struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
a228a64f 1719struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
61e021f3 1720
48edc1f7
RG
1721#ifdef CONFIG_MEMCG_KMEM
1722void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1723 int node);
1724void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1725void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1726 size_t align, gfp_t flags);
1727#else
1728static inline void *
1729bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1730 int node)
1731{
1732 return kmalloc_node(size, flags, node);
1733}
1734
1735static inline void *
1736bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1737{
1738 return kzalloc(size, flags);
1739}
1740
1741static inline void __percpu *
1742bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1743 gfp_t flags)
1744{
1745 return __alloc_percpu_gfp(size, align, flags);
1746}
1747#endif
1748
1be7f75d
AS
1749extern int sysctl_unprivileged_bpf_disabled;
1750
2c78ee89
AS
1751static inline bool bpf_allow_ptr_leaks(void)
1752{
1753 return perfmon_capable();
1754}
1755
01f810ac
AM
1756static inline bool bpf_allow_uninit_stack(void)
1757{
1758 return perfmon_capable();
1759}
1760
41c48f3a
AI
1761static inline bool bpf_allow_ptr_to_map_access(void)
1762{
1763 return perfmon_capable();
1764}
1765
2c78ee89
AS
1766static inline bool bpf_bypass_spec_v1(void)
1767{
1768 return perfmon_capable();
1769}
1770
1771static inline bool bpf_bypass_spec_v4(void)
1772{
1773 return perfmon_capable();
1774}
1775
6e71b04a 1776int bpf_map_new_fd(struct bpf_map *map, int flags);
b2197755
DB
1777int bpf_prog_new_fd(struct bpf_prog *prog);
1778
f2e10bff 1779void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
a3b80e10
AN
1780 const struct bpf_link_ops *ops, struct bpf_prog *prog);
1781int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
1782int bpf_link_settle(struct bpf_link_primer *primer);
1783void bpf_link_cleanup(struct bpf_link_primer *primer);
70ed506c
AN
1784void bpf_link_inc(struct bpf_link *link);
1785void bpf_link_put(struct bpf_link *link);
1786int bpf_link_new_fd(struct bpf_link *link);
babf3164 1787struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd);
70ed506c 1788struct bpf_link *bpf_link_get_from_fd(u32 ufd);
9f883612 1789struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
70ed506c 1790
b2197755 1791int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
6e71b04a 1792int bpf_obj_get_user(const char __user *pathname, int flags);
b2197755 1793
21aef70e 1794#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
e5158d98 1795#define DEFINE_BPF_ITER_FUNC(target, args...) \
21aef70e
YS
1796 extern int bpf_iter_ ## target(args); \
1797 int __init bpf_iter_ ## target(args) { return 0; }
15d83c4d 1798
f0d74c4d
KFL
1799/*
1800 * The task type of iterators.
1801 *
1802 * For BPF task iterators, they can be parameterized with various
1803 * parameters to visit only some of tasks.
1804 *
1805 * BPF_TASK_ITER_ALL (default)
1806 * Iterate over resources of every task.
1807 *
1808 * BPF_TASK_ITER_TID
1809 * Iterate over resources of a task/tid.
1810 *
1811 * BPF_TASK_ITER_TGID
1812 * Iterate over resources of every task of a process / task group.
1813 */
1814enum bpf_iter_task_type {
1815 BPF_TASK_ITER_ALL = 0,
1816 BPF_TASK_ITER_TID,
1817 BPF_TASK_ITER_TGID,
1818};
1819
f9c79272 1820struct bpf_iter_aux_info {
d4ccaf58 1821 /* for map_elem iter */
a5cbe05a 1822 struct bpf_map *map;
d4ccaf58
HL
1823
1824 /* for cgroup iter */
1825 struct {
1826 struct cgroup *start; /* starting cgroup */
1827 enum bpf_cgroup_iter_order order;
1828 } cgroup;
f0d74c4d
KFL
1829 struct {
1830 enum bpf_iter_task_type type;
1831 u32 pid;
1832 } task;
f9c79272
YS
1833};
1834
5e7b3020
YS
1835typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1836 union bpf_iter_link_info *linfo,
1837 struct bpf_iter_aux_info *aux);
1838typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
6b0a249a
YS
1839typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
1840 struct seq_file *seq);
1841typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
1842 struct bpf_link_info *info);
3cee6fb8
MKL
1843typedef const struct bpf_func_proto *
1844(*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
1845 const struct bpf_prog *prog);
a5cbe05a 1846
cf83b2d2
YS
1847enum bpf_iter_feature {
1848 BPF_ITER_RESCHED = BIT(0),
1849};
1850
3c32cc1b 1851#define BPF_ITER_CTX_ARG_MAX 2
ae24345d
YS
1852struct bpf_iter_reg {
1853 const char *target;
5e7b3020
YS
1854 bpf_iter_attach_target_t attach_target;
1855 bpf_iter_detach_target_t detach_target;
6b0a249a
YS
1856 bpf_iter_show_fdinfo_t show_fdinfo;
1857 bpf_iter_fill_link_info_t fill_link_info;
3cee6fb8 1858 bpf_iter_get_func_proto_t get_func_proto;
3c32cc1b 1859 u32 ctx_arg_info_size;
cf83b2d2 1860 u32 feature;
3c32cc1b 1861 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
14fc6bd6 1862 const struct bpf_iter_seq_info *seq_info;
ae24345d
YS
1863};
1864
e5158d98
YS
1865struct bpf_iter_meta {
1866 __bpf_md_ptr(struct seq_file *, seq);
1867 u64 session_id;
1868 u64 seq_num;
1869};
1870
a5cbe05a
YS
1871struct bpf_iter__bpf_map_elem {
1872 __bpf_md_ptr(struct bpf_iter_meta *, meta);
1873 __bpf_md_ptr(struct bpf_map *, map);
1874 __bpf_md_ptr(void *, key);
1875 __bpf_md_ptr(void *, value);
1876};
1877
15172a46 1878int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
ab2ee4fc 1879void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
15d83c4d 1880bool bpf_iter_prog_supported(struct bpf_prog *prog);
3cee6fb8
MKL
1881const struct bpf_func_proto *
1882bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
af2ac3e1 1883int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
ac51d99b 1884int bpf_iter_new_fd(struct bpf_link *link);
367ec3e4 1885bool bpf_link_is_iter(struct bpf_link *link);
e5158d98
YS
1886struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
1887int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
b76f2226
YS
1888void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
1889 struct seq_file *seq);
1890int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
1891 struct bpf_link_info *info);
ae24345d 1892
314ee05e
YS
1893int map_set_for_each_callback_args(struct bpf_verifier_env *env,
1894 struct bpf_func_state *caller,
1895 struct bpf_func_state *callee);
1896
15a07b33
AS
1897int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
1898int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
1899int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1900 u64 flags);
1901int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
1902 u64 flags);
d056a788 1903
557c0c6e 1904int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
15a07b33 1905
d056a788
DB
1906int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
1907 void *key, void *value, u64 map_flags);
14dc6f04 1908int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
bcc6b1b7
MKL
1909int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1910 void *key, void *value, u64 map_flags);
14dc6f04 1911int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
d056a788 1912
6e71b04a 1913int bpf_get_file_flag(int flags);
af2ac3e1 1914int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
dcab51f1 1915 size_t actual_size);
6e71b04a 1916
61e021f3 1917/* verify correctness of eBPF program */
af2ac3e1 1918int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr);
a643bff7
AN
1919
1920#ifndef CONFIG_BPF_JIT_ALWAYS_ON
1ea47e01 1921void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
a643bff7 1922#endif
46f55cff 1923
76654e67
AM
1924struct btf *bpf_get_btf_vmlinux(void);
1925
46f55cff 1926/* Map specifics */
d53ad5d8 1927struct xdp_frame;
6d5fc195 1928struct sk_buff;
e6a4750f
BT
1929struct bpf_dtab_netdev;
1930struct bpf_cpu_map_entry;
67f29e07 1931
1d233886 1932void __dev_flush(void);
d53ad5d8 1933int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1d233886 1934 struct net_device *dev_rx);
d53ad5d8 1935int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
38edddb8 1936 struct net_device *dev_rx);
d53ad5d8 1937int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
e624d4ed 1938 struct bpf_map *map, bool exclude_ingress);
6d5fc195
TM
1939int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
1940 struct bpf_prog *xdp_prog);
e624d4ed
HL
1941int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
1942 struct bpf_prog *xdp_prog, struct bpf_map *map,
1943 bool exclude_ingress);
46f55cff 1944
cdfafe98 1945void __cpu_map_flush(void);
d53ad5d8 1946int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
9c270af3 1947 struct net_device *dev_rx);
11941f8a
KKD
1948int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
1949 struct sk_buff *skb);
9c270af3 1950
96eabe7a
MKL
1951/* Return map's numa specified by userspace */
1952static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
1953{
1954 return (attr->map_flags & BPF_F_NUMA_NODE) ?
1955 attr->numa_node : NUMA_NO_NODE;
1956}
1957
040ee692 1958struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
5dc4c4b7 1959int array_map_alloc_check(union bpf_attr *attr);
040ee692 1960
c695865c
SF
1961int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1962 union bpf_attr __user *uattr);
1963int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1964 union bpf_attr __user *uattr);
da00d2f1
KS
1965int bpf_prog_test_run_tracing(struct bpf_prog *prog,
1966 const union bpf_attr *kattr,
1967 union bpf_attr __user *uattr);
c695865c
SF
1968int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1969 const union bpf_attr *kattr,
1970 union bpf_attr __user *uattr);
1b4d60ec
SL
1971int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
1972 const union bpf_attr *kattr,
1973 union bpf_attr __user *uattr);
7c32e8f8
LB
1974int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
1975 const union bpf_attr *kattr,
1976 union bpf_attr __user *uattr);
9e15db66
AS
1977bool btf_ctx_access(int off, int size, enum bpf_access_type type,
1978 const struct bpf_prog *prog,
1979 struct bpf_insn_access_aux *info);
35346ab6
HT
1980
1981static inline bool bpf_tracing_ctx_access(int off, int size,
1982 enum bpf_access_type type)
1983{
1984 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
1985 return false;
1986 if (type != BPF_READ)
1987 return false;
1988 if (off % size != 0)
1989 return false;
1990 return true;
1991}
1992
1993static inline bool bpf_tracing_btf_ctx_access(int off, int size,
1994 enum bpf_access_type type,
1995 const struct bpf_prog *prog,
1996 struct bpf_insn_access_aux *info)
1997{
1998 if (!bpf_tracing_ctx_access(off, size, type))
1999 return false;
2000 return btf_ctx_access(off, size, type, prog, info);
2001}
2002
22dc4a0f 2003int btf_struct_access(struct bpf_verifier_log *log, const struct btf *btf,
9e15db66
AS
2004 const struct btf_type *t, int off, int size,
2005 enum bpf_access_type atype,
c6f1bfe8 2006 u32 *next_btf_id, enum bpf_type_flag *flag);
faaf4a79 2007bool btf_struct_ids_match(struct bpf_verifier_log *log,
22dc4a0f 2008 const struct btf *btf, u32 id, int off,
2ab3b380
KKD
2009 const struct btf *need_btf, u32 need_type_id,
2010 bool strict);
9e15db66 2011
fec56f58
AS
2012int btf_distill_func_proto(struct bpf_verifier_log *log,
2013 struct btf *btf,
2014 const struct btf_type *func_proto,
2015 const char *func_name,
2016 struct btf_func_model *m);
2017
eb1f7f71
BT
2018struct bpf_kfunc_arg_meta {
2019 u64 r0_size;
2020 bool r0_rdonly;
2021 int ref_obj_id;
2022 u32 flags;
2023};
2024
51c39bb1 2025struct bpf_reg_state;
34747c41
MKL
2026int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
2027 struct bpf_reg_state *regs);
95f2f26f
BT
2028int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
2029 struct bpf_reg_state *regs);
e6ac2450
MKL
2030int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
2031 const struct btf *btf, u32 func_id,
a4703e31 2032 struct bpf_reg_state *regs,
eb1f7f71 2033 struct bpf_kfunc_arg_meta *meta);
51c39bb1
AS
2034int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
2035 struct bpf_reg_state *reg);
efc68158 2036int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
be8704ff 2037 struct btf *btf, const struct btf_type *t);
8c1b6e69 2038
7e6897f9 2039struct bpf_prog *bpf_prog_by_id(u32 id);
005142b8 2040struct bpf_link *bpf_link_by_id(u32 id);
7e6897f9 2041
6890896b 2042const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
a10787e6 2043void bpf_task_storage_free(struct task_struct *task);
e6ac2450
MKL
2044bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
2045const struct btf_func_model *
2046bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2047 const struct bpf_insn *insn);
fbd94c7a
AS
2048struct bpf_core_ctx {
2049 struct bpf_verifier_log *log;
2050 const struct btf *btf;
2051};
2052
2053int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
2054 int relo_idx, void *insn);
2055
44a3918c
JP
2056static inline bool unprivileged_ebpf_enabled(void)
2057{
2058 return !sysctl_unprivileged_bpf_disabled;
2059}
2060
24426654
MKL
2061/* Not all bpf prog type has the bpf_ctx.
2062 * For the bpf prog type that has initialized the bpf_ctx,
2063 * this function can be used to decide if a kernel function
2064 * is called by a bpf program.
2065 */
2066static inline bool has_current_bpf_ctx(void)
2067{
2068 return !!current->bpf_ctx;
2069}
05b24ff9
JO
2070
2071void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
9c270af3 2072#else /* !CONFIG_BPF_SYSCALL */
0fc174de
DB
2073static inline struct bpf_prog *bpf_prog_get(u32 ufd)
2074{
2075 return ERR_PTR(-EOPNOTSUPP);
2076}
2077
248f346f
JK
2078static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
2079 enum bpf_prog_type type,
288b3de5 2080 bool attach_drv)
248f346f
JK
2081{
2082 return ERR_PTR(-EOPNOTSUPP);
2083}
2084
85192dbf 2085static inline void bpf_prog_add(struct bpf_prog *prog, int i)
cc2e0b3f 2086{
cc2e0b3f 2087}
113214be 2088
c540594f
DB
2089static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
2090{
2091}
2092
0fc174de
DB
2093static inline void bpf_prog_put(struct bpf_prog *prog)
2094{
2095}
6d67942d 2096
85192dbf 2097static inline void bpf_prog_inc(struct bpf_prog *prog)
aa6a5f3c 2098{
aa6a5f3c 2099}
5ccb071e 2100
a6f6df69
JF
2101static inline struct bpf_prog *__must_check
2102bpf_prog_inc_not_zero(struct bpf_prog *prog)
2103{
2104 return ERR_PTR(-EOPNOTSUPP);
2105}
2106
6cc7d1e8
AN
2107static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2108 const struct bpf_link_ops *ops,
2109 struct bpf_prog *prog)
2110{
2111}
2112
2113static inline int bpf_link_prime(struct bpf_link *link,
2114 struct bpf_link_primer *primer)
2115{
2116 return -EOPNOTSUPP;
2117}
2118
2119static inline int bpf_link_settle(struct bpf_link_primer *primer)
2120{
2121 return -EOPNOTSUPP;
2122}
2123
2124static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2125{
2126}
2127
2128static inline void bpf_link_inc(struct bpf_link *link)
2129{
2130}
2131
2132static inline void bpf_link_put(struct bpf_link *link)
2133{
2134}
2135
6e71b04a 2136static inline int bpf_obj_get_user(const char __user *pathname, int flags)
98589a09
SL
2137{
2138 return -EOPNOTSUPP;
2139}
2140
1d233886 2141static inline void __dev_flush(void)
46f55cff
JF
2142{
2143}
9c270af3 2144
d53ad5d8 2145struct xdp_frame;
67f29e07 2146struct bpf_dtab_netdev;
e6a4750f 2147struct bpf_cpu_map_entry;
67f29e07 2148
1d233886 2149static inline
d53ad5d8 2150int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
1d233886
THJ
2151 struct net_device *dev_rx)
2152{
2153 return 0;
2154}
2155
67f29e07 2156static inline
d53ad5d8 2157int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
38edddb8 2158 struct net_device *dev_rx)
67f29e07
JDB
2159{
2160 return 0;
2161}
2162
e624d4ed 2163static inline
d53ad5d8 2164int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
e624d4ed
HL
2165 struct bpf_map *map, bool exclude_ingress)
2166{
2167 return 0;
2168}
2169
6d5fc195
TM
2170struct sk_buff;
2171
2172static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2173 struct sk_buff *skb,
2174 struct bpf_prog *xdp_prog)
2175{
2176 return 0;
2177}
2178
e624d4ed
HL
2179static inline
2180int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2181 struct bpf_prog *xdp_prog, struct bpf_map *map,
2182 bool exclude_ingress)
2183{
2184 return 0;
2185}
2186
cdfafe98 2187static inline void __cpu_map_flush(void)
9c270af3
JDB
2188{
2189}
2190
9c270af3 2191static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
d53ad5d8 2192 struct xdp_frame *xdpf,
9c270af3
JDB
2193 struct net_device *dev_rx)
2194{
2195 return 0;
2196}
040ee692 2197
11941f8a
KKD
2198static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2199 struct sk_buff *skb)
2200{
2201 return -EOPNOTSUPP;
2202}
2203
040ee692
AV
2204static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2205 enum bpf_prog_type type)
2206{
2207 return ERR_PTR(-EOPNOTSUPP);
2208}
c695865c
SF
2209
2210static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2211 const union bpf_attr *kattr,
2212 union bpf_attr __user *uattr)
2213{
2214 return -ENOTSUPP;
2215}
2216
2217static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2218 const union bpf_attr *kattr,
2219 union bpf_attr __user *uattr)
2220{
2221 return -ENOTSUPP;
2222}
2223
da00d2f1
KS
2224static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2225 const union bpf_attr *kattr,
2226 union bpf_attr __user *uattr)
2227{
2228 return -ENOTSUPP;
2229}
2230
c695865c
SF
2231static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2232 const union bpf_attr *kattr,
2233 union bpf_attr __user *uattr)
2234{
2235 return -ENOTSUPP;
2236}
6332be04 2237
7c32e8f8
LB
2238static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2239 const union bpf_attr *kattr,
2240 union bpf_attr __user *uattr)
2241{
2242 return -ENOTSUPP;
2243}
2244
6332be04
DB
2245static inline void bpf_map_put(struct bpf_map *map)
2246{
2247}
7e6897f9
BT
2248
2249static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2250{
2251 return ERR_PTR(-ENOTSUPP);
2252}
6890896b 2253
d4f7bdb2
DX
2254static inline int btf_struct_access(struct bpf_verifier_log *log,
2255 const struct btf *btf,
2256 const struct btf_type *t, int off, int size,
2257 enum bpf_access_type atype,
2258 u32 *next_btf_id, enum bpf_type_flag *flag)
2259{
2260 return -EACCES;
2261}
2262
6890896b
SF
2263static inline const struct bpf_func_proto *
2264bpf_base_func_proto(enum bpf_func_id func_id)
2265{
2266 return NULL;
2267}
a10787e6
SL
2268
2269static inline void bpf_task_storage_free(struct task_struct *task)
2270{
2271}
e6ac2450
MKL
2272
2273static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2274{
2275 return false;
2276}
2277
2278static inline const struct btf_func_model *
2279bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2280 const struct bpf_insn *insn)
2281{
2282 return NULL;
2283}
44a3918c
JP
2284
2285static inline bool unprivileged_ebpf_enabled(void)
2286{
2287 return false;
2288}
2289
24426654
MKL
2290static inline bool has_current_bpf_ctx(void)
2291{
2292 return false;
2293}
05b24ff9
JO
2294
2295static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2296{
2297}
61e021f3 2298#endif /* CONFIG_BPF_SYSCALL */
09756af4 2299
541c3bad
AN
2300void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
2301 struct btf_mod_pair *used_btfs, u32 len);
2302
479321e9
JK
2303static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2304 enum bpf_prog_type type)
2305{
2306 return bpf_prog_get_type_dev(ufd, type, false);
2307}
2308
936f8946
AN
2309void __bpf_free_used_maps(struct bpf_prog_aux *aux,
2310 struct bpf_map **used_maps, u32 len);
2311
040ee692
AV
2312bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
2313
ab3f0063
JK
2314int bpf_prog_offload_compile(struct bpf_prog *prog);
2315void bpf_prog_offload_destroy(struct bpf_prog *prog);
675fc275
JK
2316int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
2317 struct bpf_prog *prog);
ab3f0063 2318
52775b33
JK
2319int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
2320
a3884572
JK
2321int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
2322int bpf_map_offload_update_elem(struct bpf_map *map,
2323 void *key, void *value, u64 flags);
2324int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
2325int bpf_map_offload_get_next_key(struct bpf_map *map,
2326 void *key, void *next_key);
2327
09728266 2328bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
a3884572 2329
1385d755 2330struct bpf_offload_dev *
dd27c2e3 2331bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
602144c2 2332void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
dd27c2e3 2333void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
602144c2
JK
2334int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
2335 struct net_device *netdev);
2336void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
2337 struct net_device *netdev);
fd4f227d 2338bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
9fd7c555 2339
2147c438
JP
2340void unpriv_ebpf_notify(int new_state);
2341
ab3f0063
JK
2342#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
2343int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
2344
0d830032 2345static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
ab3f0063 2346{
9a18eedb 2347 return aux->offload_requested;
ab3f0063 2348}
a3884572
JK
2349
2350static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2351{
2352 return unlikely(map->ops == &bpf_map_offload_ops);
2353}
2354
2355struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
2356void bpf_map_offload_map_free(struct bpf_map *map);
79a7f8bd
AS
2357int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2358 const union bpf_attr *kattr,
2359 union bpf_attr __user *uattr);
17edea21
CW
2360
2361int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
2362int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
2363int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
748cd572
DZ
2364int sock_map_bpf_prog_query(const union bpf_attr *attr,
2365 union bpf_attr __user *uattr);
2366
17edea21 2367void sock_map_unhash(struct sock *sk);
d8616ee2 2368void sock_map_destroy(struct sock *sk);
17edea21 2369void sock_map_close(struct sock *sk, long timeout);
ab3f0063
JK
2370#else
2371static inline int bpf_prog_offload_init(struct bpf_prog *prog,
2372 union bpf_attr *attr)
2373{
2374 return -EOPNOTSUPP;
2375}
2376
2377static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
2378{
2379 return false;
2380}
a3884572
JK
2381
2382static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
2383{
2384 return false;
2385}
2386
2387static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
2388{
2389 return ERR_PTR(-EOPNOTSUPP);
2390}
2391
2392static inline void bpf_map_offload_map_free(struct bpf_map *map)
2393{
2394}
79a7f8bd
AS
2395
2396static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
2397 const union bpf_attr *kattr,
2398 union bpf_attr __user *uattr)
2399{
2400 return -ENOTSUPP;
2401}
fdb5c453 2402
88759609 2403#ifdef CONFIG_BPF_SYSCALL
604326b4
DB
2404static inline int sock_map_get_from_fd(const union bpf_attr *attr,
2405 struct bpf_prog *prog)
fdb5c453
SY
2406{
2407 return -EINVAL;
2408}
bb0de313
LB
2409
2410static inline int sock_map_prog_detach(const union bpf_attr *attr,
2411 enum bpf_prog_type ptype)
2412{
2413 return -EOPNOTSUPP;
2414}
13b79d3f
LB
2415
2416static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
2417 u64 flags)
2418{
2419 return -EOPNOTSUPP;
2420}
748cd572
DZ
2421
2422static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
2423 union bpf_attr __user *uattr)
2424{
2425 return -EINVAL;
2426}
17edea21
CW
2427#endif /* CONFIG_BPF_SYSCALL */
2428#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
5dc4c4b7 2429
17edea21
CW
2430#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
2431void bpf_sk_reuseport_detach(struct sock *sk);
2432int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
2433 void *value);
2434int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
2435 void *value, u64 map_flags);
2436#else
2437static inline void bpf_sk_reuseport_detach(struct sock *sk)
2438{
2439}
5dc4c4b7 2440
17edea21 2441#ifdef CONFIG_BPF_SYSCALL
5dc4c4b7
MKL
2442static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
2443 void *key, void *value)
2444{
2445 return -EOPNOTSUPP;
2446}
2447
2448static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
2449 void *key, void *value,
2450 u64 map_flags)
2451{
2452 return -EOPNOTSUPP;
2453}
2454#endif /* CONFIG_BPF_SYSCALL */
2455#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
2456
d0003ec0 2457/* verifier prototypes for helper functions called from eBPF programs */
a2c83fff
DB
2458extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
2459extern const struct bpf_func_proto bpf_map_update_elem_proto;
2460extern const struct bpf_func_proto bpf_map_delete_elem_proto;
f1a2e44a
MV
2461extern const struct bpf_func_proto bpf_map_push_elem_proto;
2462extern const struct bpf_func_proto bpf_map_pop_elem_proto;
2463extern const struct bpf_func_proto bpf_map_peek_elem_proto;
07343110 2464extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
d0003ec0 2465
03e69b50 2466extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
c04167ce 2467extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
2d0e30c3 2468extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
04fd61ab 2469extern const struct bpf_func_proto bpf_tail_call_proto;
17ca8cbf 2470extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
71d19214 2471extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
c8996c98 2472extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
ffeedafb
AS
2473extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
2474extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
2475extern const struct bpf_func_proto bpf_get_current_comm_proto;
d5a3b1f6 2476extern const struct bpf_func_proto bpf_get_stackid_proto;
c195651e 2477extern const struct bpf_func_proto bpf_get_stack_proto;
fa28dcb8 2478extern const struct bpf_func_proto bpf_get_task_stack_proto;
7b04d6d6
SL
2479extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
2480extern const struct bpf_func_proto bpf_get_stack_proto_pe;
174a79ff 2481extern const struct bpf_func_proto bpf_sock_map_update_proto;
81110384 2482extern const struct bpf_func_proto bpf_sock_hash_update_proto;
bf6fa2c8 2483extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
0f09abd1 2484extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
bed89185 2485extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
604326b4
DB
2486extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
2487extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
2488extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
2489extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
d83525ca
AS
2490extern const struct bpf_func_proto bpf_spin_lock_proto;
2491extern const struct bpf_func_proto bpf_spin_unlock_proto;
cd339431 2492extern const struct bpf_func_proto bpf_get_local_storage_proto;
d7a4cb9b
AI
2493extern const struct bpf_func_proto bpf_strtol_proto;
2494extern const struct bpf_func_proto bpf_strtoul_proto;
0d01da6a 2495extern const struct bpf_func_proto bpf_tcp_sock_proto;
5576b991 2496extern const struct bpf_func_proto bpf_jiffies64_proto;
b4490c5c 2497extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
0456ea17 2498extern const struct bpf_func_proto bpf_event_output_data_proto;
457f4436
AN
2499extern const struct bpf_func_proto bpf_ringbuf_output_proto;
2500extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
2501extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
2502extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
2503extern const struct bpf_func_proto bpf_ringbuf_query_proto;
bc34dee6
JK
2504extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
2505extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
2506extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
af7ec138 2507extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
478cfbdf
YS
2508extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
2509extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
2510extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
0d4fad3e 2511extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
9eeb3aa3 2512extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
3bc253c2 2513extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
07be4c4a 2514extern const struct bpf_func_proto bpf_copy_from_user_proto;
c4d0bfb4 2515extern const struct bpf_func_proto bpf_snprintf_btf_proto;
7b15523a 2516extern const struct bpf_func_proto bpf_snprintf_proto;
eaa6bcb7 2517extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
63d9b80d 2518extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
d0551261 2519extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
b60da495 2520extern const struct bpf_func_proto bpf_sock_from_file_proto;
c5dbb89f 2521extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
0593dd34 2522extern const struct bpf_func_proto bpf_task_storage_get_recur_proto;
4279adb0 2523extern const struct bpf_func_proto bpf_task_storage_get_proto;
0593dd34 2524extern const struct bpf_func_proto bpf_task_storage_delete_recur_proto;
8a7dac37 2525extern const struct bpf_func_proto bpf_task_storage_delete_proto;
69c087ba 2526extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
3d78417b 2527extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
3cee6fb8
MKL
2528extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
2529extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
9113d7e4
SF
2530extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
2531extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
7c7e3d31 2532extern const struct bpf_func_proto bpf_find_vma_proto;
e6f2dd0f 2533extern const struct bpf_func_proto bpf_loop_proto;
376040e4 2534extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
69fd337a
SF
2535extern const struct bpf_func_proto bpf_set_retval_proto;
2536extern const struct bpf_func_proto bpf_get_retval_proto;
20571567 2537extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
cd339431 2538
958a3f2d
JO
2539const struct bpf_func_proto *tracing_prog_func_proto(
2540 enum bpf_func_id func_id, const struct bpf_prog *prog);
2541
3ad00405
DB
2542/* Shared helpers among cBPF and eBPF. */
2543void bpf_user_rnd_init_once(void);
2544u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
6890896b 2545u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3ad00405 2546
c64b7983 2547#if defined(CONFIG_NET)
46f8bc92
MKL
2548bool bpf_sock_common_is_valid_access(int off, int size,
2549 enum bpf_access_type type,
2550 struct bpf_insn_access_aux *info);
c64b7983
JS
2551bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2552 struct bpf_insn_access_aux *info);
2553u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2554 const struct bpf_insn *si,
2555 struct bpf_insn *insn_buf,
2556 struct bpf_prog *prog,
2557 u32 *target_size);
2558#else
46f8bc92
MKL
2559static inline bool bpf_sock_common_is_valid_access(int off, int size,
2560 enum bpf_access_type type,
2561 struct bpf_insn_access_aux *info)
2562{
2563 return false;
2564}
c64b7983
JS
2565static inline bool bpf_sock_is_valid_access(int off, int size,
2566 enum bpf_access_type type,
2567 struct bpf_insn_access_aux *info)
2568{
2569 return false;
2570}
2571static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
2572 const struct bpf_insn *si,
2573 struct bpf_insn *insn_buf,
2574 struct bpf_prog *prog,
2575 u32 *target_size)
2576{
2577 return 0;
2578}
2579#endif
2580
655a51e5 2581#ifdef CONFIG_INET
91cc1a99
AS
2582struct sk_reuseport_kern {
2583 struct sk_buff *skb;
2584 struct sock *sk;
2585 struct sock *selected_sk;
d5e4ddae 2586 struct sock *migrating_sk;
91cc1a99
AS
2587 void *data_end;
2588 u32 hash;
2589 u32 reuseport_id;
2590 bool bind_inany;
2591};
655a51e5
MKL
2592bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2593 struct bpf_insn_access_aux *info);
2594
2595u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2596 const struct bpf_insn *si,
2597 struct bpf_insn *insn_buf,
2598 struct bpf_prog *prog,
2599 u32 *target_size);
7f94208c
Y
2600
2601bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
2602 struct bpf_insn_access_aux *info);
2603
2604u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2605 const struct bpf_insn *si,
2606 struct bpf_insn *insn_buf,
2607 struct bpf_prog *prog,
2608 u32 *target_size);
655a51e5
MKL
2609#else
2610static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
2611 enum bpf_access_type type,
2612 struct bpf_insn_access_aux *info)
2613{
2614 return false;
2615}
2616
2617static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
2618 const struct bpf_insn *si,
2619 struct bpf_insn *insn_buf,
2620 struct bpf_prog *prog,
2621 u32 *target_size)
2622{
2623 return 0;
2624}
7f94208c
Y
2625static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
2626 enum bpf_access_type type,
2627 struct bpf_insn_access_aux *info)
2628{
2629 return false;
2630}
2631
2632static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
2633 const struct bpf_insn *si,
2634 struct bpf_insn *insn_buf,
2635 struct bpf_prog *prog,
2636 u32 *target_size)
2637{
2638 return 0;
2639}
655a51e5
MKL
2640#endif /* CONFIG_INET */
2641
5964b200 2642enum bpf_text_poke_type {
b553a6ec
DB
2643 BPF_MOD_CALL,
2644 BPF_MOD_JUMP,
5964b200 2645};
4b3da77b 2646
5964b200
AS
2647int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
2648 void *addr1, void *addr2);
2649
ebc1415d 2650void *bpf_arch_text_copy(void *dst, void *src, size_t len);
fe736565 2651int bpf_arch_text_invalidate(void *dst, size_t len);
ebc1415d 2652
eae2e83e 2653struct btf_id_set;
2af30f11 2654bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
eae2e83e 2655
335ff499
DM
2656#define MAX_BPRINTF_VARARGS 12
2657
48cac3f4
FR
2658int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
2659 u32 **bin_buf, u32 num_args);
2660void bpf_bprintf_cleanup(void);
d9c9e4db 2661
97e03f52
JK
2662/* the implementation of the opaque uapi struct bpf_dynptr */
2663struct bpf_dynptr_kern {
2664 void *data;
2665 /* Size represents the number of usable bytes of dynptr data.
2666 * If for example the offset is at 4 for a local dynptr whose data is
2667 * of type u64, the number of usable bytes is 4.
2668 *
2669 * The upper 8 bits are reserved. It is as follows:
2670 * Bits 0 - 23 = size
2671 * Bits 24 - 30 = dynptr type
2672 * Bit 31 = whether dynptr is read-only
2673 */
2674 u32 size;
2675 u32 offset;
2676} __aligned(8);
2677
2678enum bpf_dynptr_type {
2679 BPF_DYNPTR_TYPE_INVALID,
2680 /* Points to memory that is local to the bpf program */
2681 BPF_DYNPTR_TYPE_LOCAL,
20571567 2682 /* Underlying data is a kernel-produced ringbuf record */
bc34dee6 2683 BPF_DYNPTR_TYPE_RINGBUF,
97e03f52
JK
2684};
2685
bc34dee6
JK
2686void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2687 enum bpf_dynptr_type type, u32 offset, u32 size);
2688void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2689int bpf_dynptr_check_size(u32 size);
51df4865 2690u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr);
bc34dee6 2691
c0e19f2c
SF
2692#ifdef CONFIG_BPF_LSM
2693void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
2694void bpf_cgroup_atype_put(int cgroup_atype);
2695#else
2696static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
2697static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
2698#endif /* CONFIG_BPF_LSM */
2699
f3cf4134
RS
2700struct key;
2701
2702#ifdef CONFIG_KEYS
2703struct bpf_key {
2704 struct key *key;
2705 bool has_ref;
2706};
2707#endif /* CONFIG_KEYS */
99c55f7d 2708#endif /* _LINUX_BPF_H */