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