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