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