| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* Copyright (c) 2018 Facebook */ |
| 3 | |
| 4 | #ifndef _LINUX_BTF_H |
| 5 | #define _LINUX_BTF_H 1 |
| 6 | |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/bpfptr.h> |
| 9 | #include <linux/bsearch.h> |
| 10 | #include <linux/btf_ids.h> |
| 11 | #include <uapi/linux/btf.h> |
| 12 | #include <uapi/linux/bpf.h> |
| 13 | |
| 14 | #define BTF_TYPE_EMIT(type) ((void)(type *)0) |
| 15 | #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val) |
| 16 | |
| 17 | /* These need to be macros, as the expressions are used in assembler input */ |
| 18 | #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */ |
| 19 | #define KF_RELEASE (1 << 1) /* kfunc is a release function */ |
| 20 | #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */ |
| 21 | /* Trusted arguments are those which are guaranteed to be valid when passed to |
| 22 | * the kfunc. It is used to enforce that pointers obtained from either acquire |
| 23 | * kfuncs, or from the main kernel on a tracepoint or struct_ops callback |
| 24 | * invocation, remain unmodified when being passed to helpers taking trusted |
| 25 | * args. |
| 26 | * |
| 27 | * Consider, for example, the following new task tracepoint: |
| 28 | * |
| 29 | * SEC("tp_btf/task_newtask") |
| 30 | * int BPF_PROG(new_task_tp, struct task_struct *task, u64 clone_flags) |
| 31 | * { |
| 32 | * ... |
| 33 | * } |
| 34 | * |
| 35 | * And the following kfunc: |
| 36 | * |
| 37 | * BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) |
| 38 | * |
| 39 | * All invocations to the kfunc must pass the unmodified, unwalked task: |
| 40 | * |
| 41 | * bpf_task_acquire(task); // Allowed |
| 42 | * bpf_task_acquire(task->last_wakee); // Rejected, walked task |
| 43 | * |
| 44 | * Programs may also pass referenced tasks directly to the kfunc: |
| 45 | * |
| 46 | * struct task_struct *acquired; |
| 47 | * |
| 48 | * acquired = bpf_task_acquire(task); // Allowed, same as above |
| 49 | * bpf_task_acquire(acquired); // Allowed |
| 50 | * bpf_task_acquire(task); // Allowed |
| 51 | * bpf_task_acquire(acquired->last_wakee); // Rejected, walked task |
| 52 | * |
| 53 | * Programs may _not_, however, pass a task from an arbitrary fentry/fexit, or |
| 54 | * kprobe/kretprobe to the kfunc, as BPF cannot guarantee that all of these |
| 55 | * pointers are guaranteed to be safe. For example, the following BPF program |
| 56 | * would be rejected: |
| 57 | * |
| 58 | * SEC("kretprobe/free_task") |
| 59 | * int BPF_PROG(free_task_probe, struct task_struct *tsk) |
| 60 | * { |
| 61 | * struct task_struct *acquired; |
| 62 | * |
| 63 | * acquired = bpf_task_acquire(acquired); // Rejected, not a trusted pointer |
| 64 | * bpf_task_release(acquired); |
| 65 | * |
| 66 | * return 0; |
| 67 | * } |
| 68 | */ |
| 69 | #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */ |
| 70 | #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */ |
| 71 | #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */ |
| 72 | #define KF_RCU (1 << 7) /* kfunc takes either rcu or trusted pointer arguments */ |
| 73 | /* only one of KF_ITER_{NEW,NEXT,DESTROY} could be specified per kfunc */ |
| 74 | #define KF_ITER_NEW (1 << 8) /* kfunc implements BPF iter constructor */ |
| 75 | #define KF_ITER_NEXT (1 << 9) /* kfunc implements BPF iter next method */ |
| 76 | #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */ |
| 77 | #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */ |
| 78 | #define KF_FASTCALL (1 << 12) /* kfunc supports bpf_fastcall protocol */ |
| 79 | #define KF_ARENA_RET (1 << 13) /* kfunc returns an arena pointer */ |
| 80 | #define KF_ARENA_ARG1 (1 << 14) /* kfunc takes an arena pointer as its first argument */ |
| 81 | #define KF_ARENA_ARG2 (1 << 15) /* kfunc takes an arena pointer as its second argument */ |
| 82 | |
| 83 | /* |
| 84 | * Tag marking a kernel function as a kfunc. This is meant to minimize the |
| 85 | * amount of copy-paste that kfunc authors have to include for correctness so |
| 86 | * as to avoid issues such as the compiler inlining or eliding either a static |
| 87 | * kfunc, or a global kfunc in an LTO build. |
| 88 | */ |
| 89 | #define __bpf_kfunc __used __retain noinline |
| 90 | |
| 91 | #define __bpf_kfunc_start_defs() \ |
| 92 | __diag_push(); \ |
| 93 | __diag_ignore_all("-Wmissing-declarations", \ |
| 94 | "Global kfuncs as their definitions will be in BTF");\ |
| 95 | __diag_ignore_all("-Wmissing-prototypes", \ |
| 96 | "Global kfuncs as their definitions will be in BTF") |
| 97 | |
| 98 | #define __bpf_kfunc_end_defs() __diag_pop() |
| 99 | #define __bpf_hook_start() __bpf_kfunc_start_defs() |
| 100 | #define __bpf_hook_end() __bpf_kfunc_end_defs() |
| 101 | |
| 102 | /* |
| 103 | * Return the name of the passed struct, if exists, or halt the build if for |
| 104 | * example the structure gets renamed. In this way, developers have to revisit |
| 105 | * the code using that structure name, and update it accordingly. |
| 106 | */ |
| 107 | #define stringify_struct(x) \ |
| 108 | ({ BUILD_BUG_ON(sizeof(struct x) < 0); \ |
| 109 | __stringify(x); }) |
| 110 | |
| 111 | struct btf; |
| 112 | struct btf_member; |
| 113 | struct btf_type; |
| 114 | union bpf_attr; |
| 115 | struct btf_show; |
| 116 | struct btf_id_set; |
| 117 | struct bpf_prog; |
| 118 | |
| 119 | typedef int (*btf_kfunc_filter_t)(const struct bpf_prog *prog, u32 kfunc_id); |
| 120 | |
| 121 | struct btf_kfunc_id_set { |
| 122 | struct module *owner; |
| 123 | struct btf_id_set8 *set; |
| 124 | btf_kfunc_filter_t filter; |
| 125 | }; |
| 126 | |
| 127 | struct btf_id_dtor_kfunc { |
| 128 | u32 btf_id; |
| 129 | u32 kfunc_btf_id; |
| 130 | }; |
| 131 | |
| 132 | struct btf_struct_meta { |
| 133 | u32 btf_id; |
| 134 | struct btf_record *record; |
| 135 | }; |
| 136 | |
| 137 | struct btf_struct_metas { |
| 138 | u32 cnt; |
| 139 | struct btf_struct_meta types[]; |
| 140 | }; |
| 141 | |
| 142 | extern const struct file_operations btf_fops; |
| 143 | |
| 144 | const char *btf_get_name(const struct btf *btf); |
| 145 | void btf_get(struct btf *btf); |
| 146 | void btf_put(struct btf *btf); |
| 147 | const struct btf_header *(const struct btf *btf); |
| 148 | int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); |
| 149 | struct btf *btf_get_by_fd(int fd); |
| 150 | int btf_get_info_by_fd(const struct btf *btf, |
| 151 | const union bpf_attr *attr, |
| 152 | union bpf_attr __user *uattr); |
| 153 | /* Figure out the size of a type_id. If type_id is a modifier |
| 154 | * (e.g. const), it will be resolved to find out the type with size. |
| 155 | * |
| 156 | * For example: |
| 157 | * In describing "const void *", type_id is "const" and "const" |
| 158 | * refers to "void *". The return type will be "void *". |
| 159 | * |
| 160 | * If type_id is a simple "int", then return type will be "int". |
| 161 | * |
| 162 | * @btf: struct btf object |
| 163 | * @type_id: Find out the size of type_id. The type_id of the return |
| 164 | * type is set to *type_id. |
| 165 | * @ret_size: It can be NULL. If not NULL, the size of the return |
| 166 | * type is set to *ret_size. |
| 167 | * Return: The btf_type (resolved to another type with size info if needed). |
| 168 | * NULL is returned if type_id itself does not have size info |
| 169 | * (e.g. void) or it cannot be resolved to another type that |
| 170 | * has size info. |
| 171 | * *type_id and *ret_size will not be changed in the |
| 172 | * NULL return case. |
| 173 | */ |
| 174 | const struct btf_type *btf_type_id_size(const struct btf *btf, |
| 175 | u32 *type_id, |
| 176 | u32 *ret_size); |
| 177 | |
| 178 | /* |
| 179 | * Options to control show behaviour. |
| 180 | * - BTF_SHOW_COMPACT: no formatting around type information |
| 181 | * - BTF_SHOW_NONAME: no struct/union member names/types |
| 182 | * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values; |
| 183 | * equivalent to %px. |
| 184 | * - BTF_SHOW_ZERO: show zero-valued struct/union members; they |
| 185 | * are not displayed by default |
| 186 | * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read |
| 187 | * data before displaying it. |
| 188 | */ |
| 189 | #define BTF_SHOW_COMPACT BTF_F_COMPACT |
| 190 | #define BTF_SHOW_NONAME BTF_F_NONAME |
| 191 | #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW |
| 192 | #define BTF_SHOW_ZERO BTF_F_ZERO |
| 193 | #define BTF_SHOW_UNSAFE (1ULL << 4) |
| 194 | |
| 195 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, |
| 196 | struct seq_file *m); |
| 197 | int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj, |
| 198 | struct seq_file *m, u64 flags); |
| 199 | |
| 200 | /* |
| 201 | * Copy len bytes of string representation of obj of BTF type_id into buf. |
| 202 | * |
| 203 | * @btf: struct btf object |
| 204 | * @type_id: type id of type obj points to |
| 205 | * @obj: pointer to typed data |
| 206 | * @buf: buffer to write to |
| 207 | * @len: maximum length to write to buf |
| 208 | * @flags: show options (see above) |
| 209 | * |
| 210 | * Return: length that would have been/was copied as per snprintf, or |
| 211 | * negative error. |
| 212 | */ |
| 213 | int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, |
| 214 | char *buf, int len, u64 flags); |
| 215 | |
| 216 | int btf_get_fd_by_id(u32 id); |
| 217 | u32 btf_obj_id(const struct btf *btf); |
| 218 | bool btf_is_kernel(const struct btf *btf); |
| 219 | bool btf_is_module(const struct btf *btf); |
| 220 | bool btf_is_vmlinux(const struct btf *btf); |
| 221 | struct module *btf_try_get_module(const struct btf *btf); |
| 222 | u32 btf_nr_types(const struct btf *btf); |
| 223 | struct btf *btf_base_btf(const struct btf *btf); |
| 224 | bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, |
| 225 | const struct btf_member *m, |
| 226 | u32 expected_offset, u32 expected_size); |
| 227 | struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type *t, |
| 228 | u32 field_mask, u32 value_size); |
| 229 | int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec); |
| 230 | bool btf_type_is_void(const struct btf_type *t); |
| 231 | s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); |
| 232 | s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); |
| 233 | const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, |
| 234 | u32 id, u32 *res_id); |
| 235 | const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, |
| 236 | u32 id, u32 *res_id); |
| 237 | const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf, |
| 238 | u32 id, u32 *res_id); |
| 239 | const struct btf_type * |
| 240 | btf_resolve_size(const struct btf *btf, const struct btf_type *type, |
| 241 | u32 *type_size); |
| 242 | const char *btf_type_str(const struct btf_type *t); |
| 243 | |
| 244 | #define for_each_member(i, struct_type, member) \ |
| 245 | for (i = 0, member = btf_type_member(struct_type); \ |
| 246 | i < btf_type_vlen(struct_type); \ |
| 247 | i++, member++) |
| 248 | |
| 249 | #define for_each_vsi(i, datasec_type, member) \ |
| 250 | for (i = 0, member = btf_type_var_secinfo(datasec_type); \ |
| 251 | i < btf_type_vlen(datasec_type); \ |
| 252 | i++, member++) |
| 253 | |
| 254 | static inline bool btf_type_is_ptr(const struct btf_type *t) |
| 255 | { |
| 256 | return BTF_INFO_KIND(t->info) == BTF_KIND_PTR; |
| 257 | } |
| 258 | |
| 259 | static inline bool btf_type_is_int(const struct btf_type *t) |
| 260 | { |
| 261 | return BTF_INFO_KIND(t->info) == BTF_KIND_INT; |
| 262 | } |
| 263 | |
| 264 | static inline bool btf_type_is_small_int(const struct btf_type *t) |
| 265 | { |
| 266 | return btf_type_is_int(t) && t->size <= sizeof(u64); |
| 267 | } |
| 268 | |
| 269 | static inline u8 btf_int_encoding(const struct btf_type *t) |
| 270 | { |
| 271 | return BTF_INT_ENCODING(*(u32 *)(t + 1)); |
| 272 | } |
| 273 | |
| 274 | static inline bool btf_type_is_signed_int(const struct btf_type *t) |
| 275 | { |
| 276 | return btf_type_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED); |
| 277 | } |
| 278 | |
| 279 | static inline bool btf_type_is_enum(const struct btf_type *t) |
| 280 | { |
| 281 | return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM; |
| 282 | } |
| 283 | |
| 284 | static inline bool btf_is_any_enum(const struct btf_type *t) |
| 285 | { |
| 286 | return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM || |
| 287 | BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64; |
| 288 | } |
| 289 | |
| 290 | static inline bool btf_kind_core_compat(const struct btf_type *t1, |
| 291 | const struct btf_type *t2) |
| 292 | { |
| 293 | return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) || |
| 294 | (btf_is_any_enum(t: t1) && btf_is_any_enum(t: t2)); |
| 295 | } |
| 296 | |
| 297 | static inline bool str_is_empty(const char *s) |
| 298 | { |
| 299 | return !s || !s[0]; |
| 300 | } |
| 301 | |
| 302 | static inline u16 btf_kind(const struct btf_type *t) |
| 303 | { |
| 304 | return BTF_INFO_KIND(t->info); |
| 305 | } |
| 306 | |
| 307 | static inline bool btf_is_enum(const struct btf_type *t) |
| 308 | { |
| 309 | return btf_kind(t) == BTF_KIND_ENUM; |
| 310 | } |
| 311 | |
| 312 | static inline bool btf_is_enum64(const struct btf_type *t) |
| 313 | { |
| 314 | return btf_kind(t) == BTF_KIND_ENUM64; |
| 315 | } |
| 316 | |
| 317 | static inline u64 btf_enum64_value(const struct btf_enum64 *e) |
| 318 | { |
| 319 | return ((u64)e->val_hi32 << 32) | e->val_lo32; |
| 320 | } |
| 321 | |
| 322 | static inline bool btf_is_composite(const struct btf_type *t) |
| 323 | { |
| 324 | u16 kind = btf_kind(t); |
| 325 | |
| 326 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; |
| 327 | } |
| 328 | |
| 329 | static inline bool btf_is_array(const struct btf_type *t) |
| 330 | { |
| 331 | return btf_kind(t) == BTF_KIND_ARRAY; |
| 332 | } |
| 333 | |
| 334 | static inline bool btf_is_int(const struct btf_type *t) |
| 335 | { |
| 336 | return btf_kind(t) == BTF_KIND_INT; |
| 337 | } |
| 338 | |
| 339 | static inline bool btf_is_ptr(const struct btf_type *t) |
| 340 | { |
| 341 | return btf_kind(t) == BTF_KIND_PTR; |
| 342 | } |
| 343 | |
| 344 | static inline u8 btf_int_offset(const struct btf_type *t) |
| 345 | { |
| 346 | return BTF_INT_OFFSET(*(u32 *)(t + 1)); |
| 347 | } |
| 348 | |
| 349 | static inline __u8 btf_int_bits(const struct btf_type *t) |
| 350 | { |
| 351 | return BTF_INT_BITS(*(__u32 *)(t + 1)); |
| 352 | } |
| 353 | |
| 354 | static inline bool btf_type_is_scalar(const struct btf_type *t) |
| 355 | { |
| 356 | return btf_type_is_int(t) || btf_type_is_enum(t); |
| 357 | } |
| 358 | |
| 359 | static inline bool btf_type_is_fwd(const struct btf_type *t) |
| 360 | { |
| 361 | return BTF_INFO_KIND(t->info) == BTF_KIND_FWD; |
| 362 | } |
| 363 | |
| 364 | static inline bool btf_type_is_typedef(const struct btf_type *t) |
| 365 | { |
| 366 | return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF; |
| 367 | } |
| 368 | |
| 369 | static inline bool btf_type_is_volatile(const struct btf_type *t) |
| 370 | { |
| 371 | return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE; |
| 372 | } |
| 373 | |
| 374 | static inline bool btf_type_is_func(const struct btf_type *t) |
| 375 | { |
| 376 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC; |
| 377 | } |
| 378 | |
| 379 | static inline bool btf_type_is_func_proto(const struct btf_type *t) |
| 380 | { |
| 381 | return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO; |
| 382 | } |
| 383 | |
| 384 | static inline bool btf_type_is_var(const struct btf_type *t) |
| 385 | { |
| 386 | return BTF_INFO_KIND(t->info) == BTF_KIND_VAR; |
| 387 | } |
| 388 | |
| 389 | static inline bool btf_type_is_type_tag(const struct btf_type *t) |
| 390 | { |
| 391 | return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG; |
| 392 | } |
| 393 | |
| 394 | /* union is only a special case of struct: |
| 395 | * all its offsetof(member) == 0 |
| 396 | */ |
| 397 | static inline bool btf_type_is_struct(const struct btf_type *t) |
| 398 | { |
| 399 | u8 kind = BTF_INFO_KIND(t->info); |
| 400 | |
| 401 | return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; |
| 402 | } |
| 403 | |
| 404 | static inline bool __btf_type_is_struct(const struct btf_type *t) |
| 405 | { |
| 406 | return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT; |
| 407 | } |
| 408 | |
| 409 | static inline bool btf_type_is_array(const struct btf_type *t) |
| 410 | { |
| 411 | return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY; |
| 412 | } |
| 413 | |
| 414 | static inline u16 btf_type_vlen(const struct btf_type *t) |
| 415 | { |
| 416 | return BTF_INFO_VLEN(t->info); |
| 417 | } |
| 418 | |
| 419 | static inline u16 btf_vlen(const struct btf_type *t) |
| 420 | { |
| 421 | return btf_type_vlen(t); |
| 422 | } |
| 423 | |
| 424 | static inline u16 btf_func_linkage(const struct btf_type *t) |
| 425 | { |
| 426 | return BTF_INFO_VLEN(t->info); |
| 427 | } |
| 428 | |
| 429 | static inline bool btf_type_kflag(const struct btf_type *t) |
| 430 | { |
| 431 | return BTF_INFO_KFLAG(t->info); |
| 432 | } |
| 433 | |
| 434 | static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type, |
| 435 | const struct btf_member *member) |
| 436 | { |
| 437 | return btf_type_kflag(t: struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset) |
| 438 | : member->offset; |
| 439 | } |
| 440 | |
| 441 | static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type, |
| 442 | const struct btf_member *member) |
| 443 | { |
| 444 | return btf_type_kflag(t: struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset) |
| 445 | : 0; |
| 446 | } |
| 447 | |
| 448 | static inline struct btf_member *btf_members(const struct btf_type *t) |
| 449 | { |
| 450 | return (struct btf_member *)(t + 1); |
| 451 | } |
| 452 | |
| 453 | static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx) |
| 454 | { |
| 455 | const struct btf_member *m = btf_members(t) + member_idx; |
| 456 | |
| 457 | return __btf_member_bit_offset(struct_type: t, member: m); |
| 458 | } |
| 459 | |
| 460 | static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx) |
| 461 | { |
| 462 | const struct btf_member *m = btf_members(t) + member_idx; |
| 463 | |
| 464 | return __btf_member_bitfield_size(struct_type: t, member: m); |
| 465 | } |
| 466 | |
| 467 | static inline const struct btf_member *btf_type_member(const struct btf_type *t) |
| 468 | { |
| 469 | return (const struct btf_member *)(t + 1); |
| 470 | } |
| 471 | |
| 472 | static inline struct btf_array *btf_array(const struct btf_type *t) |
| 473 | { |
| 474 | return (struct btf_array *)(t + 1); |
| 475 | } |
| 476 | |
| 477 | static inline struct btf_enum *btf_enum(const struct btf_type *t) |
| 478 | { |
| 479 | return (struct btf_enum *)(t + 1); |
| 480 | } |
| 481 | |
| 482 | static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) |
| 483 | { |
| 484 | return (struct btf_enum64 *)(t + 1); |
| 485 | } |
| 486 | |
| 487 | static inline const struct btf_var_secinfo *btf_type_var_secinfo( |
| 488 | const struct btf_type *t) |
| 489 | { |
| 490 | return (const struct btf_var_secinfo *)(t + 1); |
| 491 | } |
| 492 | |
| 493 | static inline struct btf_param *btf_params(const struct btf_type *t) |
| 494 | { |
| 495 | return (struct btf_param *)(t + 1); |
| 496 | } |
| 497 | |
| 498 | static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t) |
| 499 | { |
| 500 | return (struct btf_decl_tag *)(t + 1); |
| 501 | } |
| 502 | |
| 503 | static inline int btf_id_cmp_func(const void *a, const void *b) |
| 504 | { |
| 505 | const int *pa = a, *pb = b; |
| 506 | |
| 507 | return *pa - *pb; |
| 508 | } |
| 509 | |
| 510 | static inline bool btf_id_set_contains(const struct btf_id_set *set, u32 id) |
| 511 | { |
| 512 | return bsearch(key: &id, base: set->ids, num: set->cnt, size: sizeof(u32), cmp: btf_id_cmp_func) != NULL; |
| 513 | } |
| 514 | |
| 515 | static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id) |
| 516 | { |
| 517 | return bsearch(key: &id, base: set->pairs, num: set->cnt, size: sizeof(set->pairs[0]), cmp: btf_id_cmp_func); |
| 518 | } |
| 519 | |
| 520 | bool btf_param_match_suffix(const struct btf *btf, |
| 521 | const struct btf_param *arg, |
| 522 | const char *suffix); |
| 523 | int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto, |
| 524 | u32 arg_no); |
| 525 | u32 btf_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto, int off); |
| 526 | |
| 527 | struct bpf_verifier_log; |
| 528 | |
| 529 | #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL) |
| 530 | struct bpf_struct_ops; |
| 531 | int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops); |
| 532 | const struct bpf_struct_ops_desc *bpf_struct_ops_find_value(struct btf *btf, u32 value_id); |
| 533 | const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id); |
| 534 | #else |
| 535 | static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id) |
| 536 | { |
| 537 | return NULL; |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | enum btf_field_iter_kind { |
| 542 | BTF_FIELD_ITER_IDS, |
| 543 | BTF_FIELD_ITER_STRS, |
| 544 | }; |
| 545 | |
| 546 | struct btf_field_desc { |
| 547 | /* once-per-type offsets */ |
| 548 | int t_off_cnt, t_offs[2]; |
| 549 | /* member struct size, or zero, if no members */ |
| 550 | int m_sz; |
| 551 | /* repeated per-member offsets */ |
| 552 | int m_off_cnt, m_offs[1]; |
| 553 | }; |
| 554 | |
| 555 | struct btf_field_iter { |
| 556 | struct btf_field_desc desc; |
| 557 | void *p; |
| 558 | int m_idx; |
| 559 | int off_idx; |
| 560 | int vlen; |
| 561 | }; |
| 562 | |
| 563 | #ifdef CONFIG_BPF_SYSCALL |
| 564 | const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); |
| 565 | void btf_set_base_btf(struct btf *btf, const struct btf *base_btf); |
| 566 | int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **map_ids); |
| 567 | int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t, |
| 568 | enum btf_field_iter_kind iter_kind); |
| 569 | __u32 *btf_field_iter_next(struct btf_field_iter *it); |
| 570 | |
| 571 | const char *btf_name_by_offset(const struct btf *btf, u32 offset); |
| 572 | const char *btf_str_by_offset(const struct btf *btf, u32 offset); |
| 573 | struct btf *btf_parse_vmlinux(void); |
| 574 | struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog); |
| 575 | u32 *btf_kfunc_id_set_contains(const struct btf *btf, u32 kfunc_btf_id, |
| 576 | const struct bpf_prog *prog); |
| 577 | u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id, |
| 578 | const struct bpf_prog *prog); |
| 579 | int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, |
| 580 | const struct btf_kfunc_id_set *s); |
| 581 | int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset); |
| 582 | s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id); |
| 583 | int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt, |
| 584 | struct module *owner); |
| 585 | struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id); |
| 586 | bool btf_is_projection_of(const char *pname, const char *tname); |
| 587 | bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, |
| 588 | const struct btf_type *t, enum bpf_prog_type prog_type, |
| 589 | int arg); |
| 590 | int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type); |
| 591 | bool btf_types_are_same(const struct btf *btf1, u32 id1, |
| 592 | const struct btf *btf2, u32 id2); |
| 593 | int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx); |
| 594 | |
| 595 | static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t) |
| 596 | { |
| 597 | if (!btf_type_is_ptr(t)) |
| 598 | return false; |
| 599 | |
| 600 | t = btf_type_skip_modifiers(btf, id: t->type, NULL); |
| 601 | |
| 602 | return btf_type_is_struct(t); |
| 603 | } |
| 604 | #else |
| 605 | static inline const struct btf_type *btf_type_by_id(const struct btf *btf, |
| 606 | u32 type_id) |
| 607 | { |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | static inline void btf_set_base_btf(struct btf *btf, const struct btf *base_btf) |
| 612 | { |
| 613 | } |
| 614 | |
| 615 | static inline int btf_relocate(void *log, struct btf *btf, const struct btf *base_btf, |
| 616 | __u32 **map_ids) |
| 617 | { |
| 618 | return -EOPNOTSUPP; |
| 619 | } |
| 620 | |
| 621 | static inline int btf_field_iter_init(struct btf_field_iter *it, struct btf_type *t, |
| 622 | enum btf_field_iter_kind iter_kind) |
| 623 | { |
| 624 | return -EOPNOTSUPP; |
| 625 | } |
| 626 | |
| 627 | static inline __u32 *btf_field_iter_next(struct btf_field_iter *it) |
| 628 | { |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | static inline const char *btf_name_by_offset(const struct btf *btf, |
| 633 | u32 offset) |
| 634 | { |
| 635 | return NULL; |
| 636 | } |
| 637 | static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf, |
| 638 | u32 kfunc_btf_id, |
| 639 | struct bpf_prog *prog) |
| 640 | |
| 641 | { |
| 642 | return NULL; |
| 643 | } |
| 644 | static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, |
| 645 | const struct btf_kfunc_id_set *s) |
| 646 | { |
| 647 | return 0; |
| 648 | } |
| 649 | static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id) |
| 650 | { |
| 651 | return -ENOENT; |
| 652 | } |
| 653 | static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, |
| 654 | u32 add_cnt, struct module *owner) |
| 655 | { |
| 656 | return 0; |
| 657 | } |
| 658 | static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id) |
| 659 | { |
| 660 | return NULL; |
| 661 | } |
| 662 | static inline bool |
| 663 | btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf, |
| 664 | const struct btf_type *t, enum bpf_prog_type prog_type, |
| 665 | int arg) |
| 666 | { |
| 667 | return false; |
| 668 | } |
| 669 | static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log, |
| 670 | enum bpf_prog_type prog_type) { |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | static inline bool btf_types_are_same(const struct btf *btf1, u32 id1, |
| 674 | const struct btf *btf2, u32 id2) |
| 675 | { |
| 676 | return false; |
| 677 | } |
| 678 | static inline int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx) |
| 679 | { |
| 680 | return -EOPNOTSUPP; |
| 681 | } |
| 682 | #endif |
| 683 | #endif |
| 684 | |