btf.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018 Facebook */
  3. #ifndef _LINUX_BTF_H
  4. #define _LINUX_BTF_H 1
  5. #include <linux/types.h>
  6. #include <linux/bpfptr.h>
  7. #include <uapi/linux/btf.h>
  8. #include <uapi/linux/bpf.h>
  9. #define BTF_TYPE_EMIT(type) ((void)(type *)0)
  10. #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
  11. /* These need to be macros, as the expressions are used in assembler input */
  12. #define KF_ACQUIRE (1 << 0) /* kfunc is an acquire function */
  13. #define KF_RELEASE (1 << 1) /* kfunc is a release function */
  14. #define KF_RET_NULL (1 << 2) /* kfunc returns a pointer that may be NULL */
  15. #define KF_KPTR_GET (1 << 3) /* kfunc returns reference to a kptr */
  16. /* Trusted arguments are those which are meant to be referenced arguments with
  17. * unchanged offset. It is used to enforce that pointers obtained from acquire
  18. * kfuncs remain unmodified when being passed to helpers taking trusted args.
  19. *
  20. * Consider
  21. * struct foo {
  22. * int data;
  23. * struct foo *next;
  24. * };
  25. *
  26. * struct bar {
  27. * int data;
  28. * struct foo f;
  29. * };
  30. *
  31. * struct foo *f = alloc_foo(); // Acquire kfunc
  32. * struct bar *b = alloc_bar(); // Acquire kfunc
  33. *
  34. * If a kfunc set_foo_data() wants to operate only on the allocated object, it
  35. * will set the KF_TRUSTED_ARGS flag, which will prevent unsafe usage like:
  36. *
  37. * set_foo_data(f, 42); // Allowed
  38. * set_foo_data(f->next, 42); // Rejected, non-referenced pointer
  39. * set_foo_data(&f->next, 42);// Rejected, referenced, but wrong type
  40. * set_foo_data(&b->f, 42); // Rejected, referenced, but bad offset
  41. *
  42. * In the final case, usually for the purposes of type matching, it is deduced
  43. * by looking at the type of the member at the offset, but due to the
  44. * requirement of trusted argument, this deduction will be strict and not done
  45. * for this case.
  46. */
  47. #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
  48. #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */
  49. #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */
  50. /*
  51. * Return the name of the passed struct, if exists, or halt the build if for
  52. * example the structure gets renamed. In this way, developers have to revisit
  53. * the code using that structure name, and update it accordingly.
  54. */
  55. #define stringify_struct(x) \
  56. ({ BUILD_BUG_ON(sizeof(struct x) < 0); \
  57. __stringify(x); })
  58. struct btf;
  59. struct btf_member;
  60. struct btf_type;
  61. union bpf_attr;
  62. struct btf_show;
  63. struct btf_id_set;
  64. struct btf_kfunc_id_set {
  65. struct module *owner;
  66. struct btf_id_set8 *set;
  67. };
  68. struct btf_id_dtor_kfunc {
  69. u32 btf_id;
  70. u32 kfunc_btf_id;
  71. };
  72. typedef void (*btf_dtor_kfunc_t)(void *);
  73. extern const struct file_operations btf_fops;
  74. void btf_get(struct btf *btf);
  75. void btf_put(struct btf *btf);
  76. int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr);
  77. struct btf *btf_get_by_fd(int fd);
  78. int btf_get_info_by_fd(const struct btf *btf,
  79. const union bpf_attr *attr,
  80. union bpf_attr __user *uattr);
  81. /* Figure out the size of a type_id. If type_id is a modifier
  82. * (e.g. const), it will be resolved to find out the type with size.
  83. *
  84. * For example:
  85. * In describing "const void *", type_id is "const" and "const"
  86. * refers to "void *". The return type will be "void *".
  87. *
  88. * If type_id is a simple "int", then return type will be "int".
  89. *
  90. * @btf: struct btf object
  91. * @type_id: Find out the size of type_id. The type_id of the return
  92. * type is set to *type_id.
  93. * @ret_size: It can be NULL. If not NULL, the size of the return
  94. * type is set to *ret_size.
  95. * Return: The btf_type (resolved to another type with size info if needed).
  96. * NULL is returned if type_id itself does not have size info
  97. * (e.g. void) or it cannot be resolved to another type that
  98. * has size info.
  99. * *type_id and *ret_size will not be changed in the
  100. * NULL return case.
  101. */
  102. const struct btf_type *btf_type_id_size(const struct btf *btf,
  103. u32 *type_id,
  104. u32 *ret_size);
  105. /*
  106. * Options to control show behaviour.
  107. * - BTF_SHOW_COMPACT: no formatting around type information
  108. * - BTF_SHOW_NONAME: no struct/union member names/types
  109. * - BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
  110. * equivalent to %px.
  111. * - BTF_SHOW_ZERO: show zero-valued struct/union members; they
  112. * are not displayed by default
  113. * - BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
  114. * data before displaying it.
  115. */
  116. #define BTF_SHOW_COMPACT BTF_F_COMPACT
  117. #define BTF_SHOW_NONAME BTF_F_NONAME
  118. #define BTF_SHOW_PTR_RAW BTF_F_PTR_RAW
  119. #define BTF_SHOW_ZERO BTF_F_ZERO
  120. #define BTF_SHOW_UNSAFE (1ULL << 4)
  121. void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  122. struct seq_file *m);
  123. int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
  124. struct seq_file *m, u64 flags);
  125. /*
  126. * Copy len bytes of string representation of obj of BTF type_id into buf.
  127. *
  128. * @btf: struct btf object
  129. * @type_id: type id of type obj points to
  130. * @obj: pointer to typed data
  131. * @buf: buffer to write to
  132. * @len: maximum length to write to buf
  133. * @flags: show options (see above)
  134. *
  135. * Return: length that would have been/was copied as per snprintf, or
  136. * negative error.
  137. */
  138. int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
  139. char *buf, int len, u64 flags);
  140. int btf_get_fd_by_id(u32 id);
  141. u32 btf_obj_id(const struct btf *btf);
  142. bool btf_is_kernel(const struct btf *btf);
  143. bool btf_is_module(const struct btf *btf);
  144. struct module *btf_try_get_module(const struct btf *btf);
  145. u32 btf_nr_types(const struct btf *btf);
  146. bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
  147. const struct btf_member *m,
  148. u32 expected_offset, u32 expected_size);
  149. int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
  150. int btf_find_timer(const struct btf *btf, const struct btf_type *t);
  151. struct bpf_map_value_off *btf_parse_kptrs(const struct btf *btf,
  152. const struct btf_type *t);
  153. bool btf_type_is_void(const struct btf_type *t);
  154. s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
  155. const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
  156. u32 id, u32 *res_id);
  157. const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
  158. u32 id, u32 *res_id);
  159. const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
  160. u32 id, u32 *res_id);
  161. const struct btf_type *
  162. btf_resolve_size(const struct btf *btf, const struct btf_type *type,
  163. u32 *type_size);
  164. const char *btf_type_str(const struct btf_type *t);
  165. #define for_each_member(i, struct_type, member) \
  166. for (i = 0, member = btf_type_member(struct_type); \
  167. i < btf_type_vlen(struct_type); \
  168. i++, member++)
  169. #define for_each_vsi(i, datasec_type, member) \
  170. for (i = 0, member = btf_type_var_secinfo(datasec_type); \
  171. i < btf_type_vlen(datasec_type); \
  172. i++, member++)
  173. static inline bool btf_type_is_ptr(const struct btf_type *t)
  174. {
  175. return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
  176. }
  177. static inline bool btf_type_is_int(const struct btf_type *t)
  178. {
  179. return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
  180. }
  181. static inline bool btf_type_is_small_int(const struct btf_type *t)
  182. {
  183. return btf_type_is_int(t) && t->size <= sizeof(u64);
  184. }
  185. static inline bool btf_type_is_enum(const struct btf_type *t)
  186. {
  187. return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
  188. }
  189. static inline bool btf_is_any_enum(const struct btf_type *t)
  190. {
  191. return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM ||
  192. BTF_INFO_KIND(t->info) == BTF_KIND_ENUM64;
  193. }
  194. static inline bool btf_kind_core_compat(const struct btf_type *t1,
  195. const struct btf_type *t2)
  196. {
  197. return BTF_INFO_KIND(t1->info) == BTF_INFO_KIND(t2->info) ||
  198. (btf_is_any_enum(t1) && btf_is_any_enum(t2));
  199. }
  200. static inline bool str_is_empty(const char *s)
  201. {
  202. return !s || !s[0];
  203. }
  204. static inline u16 btf_kind(const struct btf_type *t)
  205. {
  206. return BTF_INFO_KIND(t->info);
  207. }
  208. static inline bool btf_is_enum(const struct btf_type *t)
  209. {
  210. return btf_kind(t) == BTF_KIND_ENUM;
  211. }
  212. static inline bool btf_is_enum64(const struct btf_type *t)
  213. {
  214. return btf_kind(t) == BTF_KIND_ENUM64;
  215. }
  216. static inline u64 btf_enum64_value(const struct btf_enum64 *e)
  217. {
  218. return ((u64)e->val_hi32 << 32) | e->val_lo32;
  219. }
  220. static inline bool btf_is_composite(const struct btf_type *t)
  221. {
  222. u16 kind = btf_kind(t);
  223. return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
  224. }
  225. static inline bool btf_is_array(const struct btf_type *t)
  226. {
  227. return btf_kind(t) == BTF_KIND_ARRAY;
  228. }
  229. static inline bool btf_is_int(const struct btf_type *t)
  230. {
  231. return btf_kind(t) == BTF_KIND_INT;
  232. }
  233. static inline bool btf_is_ptr(const struct btf_type *t)
  234. {
  235. return btf_kind(t) == BTF_KIND_PTR;
  236. }
  237. static inline u8 btf_int_offset(const struct btf_type *t)
  238. {
  239. return BTF_INT_OFFSET(*(u32 *)(t + 1));
  240. }
  241. static inline u8 btf_int_encoding(const struct btf_type *t)
  242. {
  243. return BTF_INT_ENCODING(*(u32 *)(t + 1));
  244. }
  245. static inline bool btf_type_is_scalar(const struct btf_type *t)
  246. {
  247. return btf_type_is_int(t) || btf_type_is_enum(t);
  248. }
  249. static inline bool btf_type_is_typedef(const struct btf_type *t)
  250. {
  251. return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
  252. }
  253. static inline bool btf_type_is_func(const struct btf_type *t)
  254. {
  255. return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
  256. }
  257. static inline bool btf_type_is_func_proto(const struct btf_type *t)
  258. {
  259. return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
  260. }
  261. static inline bool btf_type_is_var(const struct btf_type *t)
  262. {
  263. return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
  264. }
  265. static inline bool btf_type_is_type_tag(const struct btf_type *t)
  266. {
  267. return BTF_INFO_KIND(t->info) == BTF_KIND_TYPE_TAG;
  268. }
  269. /* union is only a special case of struct:
  270. * all its offsetof(member) == 0
  271. */
  272. static inline bool btf_type_is_struct(const struct btf_type *t)
  273. {
  274. u8 kind = BTF_INFO_KIND(t->info);
  275. return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
  276. }
  277. static inline u16 btf_type_vlen(const struct btf_type *t)
  278. {
  279. return BTF_INFO_VLEN(t->info);
  280. }
  281. static inline u16 btf_vlen(const struct btf_type *t)
  282. {
  283. return btf_type_vlen(t);
  284. }
  285. static inline u16 btf_func_linkage(const struct btf_type *t)
  286. {
  287. return BTF_INFO_VLEN(t->info);
  288. }
  289. static inline bool btf_type_kflag(const struct btf_type *t)
  290. {
  291. return BTF_INFO_KFLAG(t->info);
  292. }
  293. static inline u32 __btf_member_bit_offset(const struct btf_type *struct_type,
  294. const struct btf_member *member)
  295. {
  296. return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
  297. : member->offset;
  298. }
  299. static inline u32 __btf_member_bitfield_size(const struct btf_type *struct_type,
  300. const struct btf_member *member)
  301. {
  302. return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
  303. : 0;
  304. }
  305. static inline struct btf_member *btf_members(const struct btf_type *t)
  306. {
  307. return (struct btf_member *)(t + 1);
  308. }
  309. static inline u32 btf_member_bit_offset(const struct btf_type *t, u32 member_idx)
  310. {
  311. const struct btf_member *m = btf_members(t) + member_idx;
  312. return __btf_member_bit_offset(t, m);
  313. }
  314. static inline u32 btf_member_bitfield_size(const struct btf_type *t, u32 member_idx)
  315. {
  316. const struct btf_member *m = btf_members(t) + member_idx;
  317. return __btf_member_bitfield_size(t, m);
  318. }
  319. static inline const struct btf_member *btf_type_member(const struct btf_type *t)
  320. {
  321. return (const struct btf_member *)(t + 1);
  322. }
  323. static inline struct btf_array *btf_array(const struct btf_type *t)
  324. {
  325. return (struct btf_array *)(t + 1);
  326. }
  327. static inline struct btf_enum *btf_enum(const struct btf_type *t)
  328. {
  329. return (struct btf_enum *)(t + 1);
  330. }
  331. static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
  332. {
  333. return (struct btf_enum64 *)(t + 1);
  334. }
  335. static inline const struct btf_var_secinfo *btf_type_var_secinfo(
  336. const struct btf_type *t)
  337. {
  338. return (const struct btf_var_secinfo *)(t + 1);
  339. }
  340. static inline struct btf_param *btf_params(const struct btf_type *t)
  341. {
  342. return (struct btf_param *)(t + 1);
  343. }
  344. #ifdef CONFIG_BPF_SYSCALL
  345. struct bpf_prog;
  346. const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
  347. const char *btf_name_by_offset(const struct btf *btf, u32 offset);
  348. struct btf *btf_parse_vmlinux(void);
  349. struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
  350. u32 *btf_kfunc_id_set_contains(const struct btf *btf,
  351. enum bpf_prog_type prog_type,
  352. u32 kfunc_btf_id);
  353. int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
  354. const struct btf_kfunc_id_set *s);
  355. s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
  356. int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
  357. struct module *owner);
  358. #else
  359. static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
  360. u32 type_id)
  361. {
  362. return NULL;
  363. }
  364. static inline const char *btf_name_by_offset(const struct btf *btf,
  365. u32 offset)
  366. {
  367. return NULL;
  368. }
  369. static inline u32 *btf_kfunc_id_set_contains(const struct btf *btf,
  370. enum bpf_prog_type prog_type,
  371. u32 kfunc_btf_id)
  372. {
  373. return NULL;
  374. }
  375. static inline int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,
  376. const struct btf_kfunc_id_set *s)
  377. {
  378. return 0;
  379. }
  380. static inline s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id)
  381. {
  382. return -ENOENT;
  383. }
  384. static inline int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors,
  385. u32 add_cnt, struct module *owner)
  386. {
  387. return 0;
  388. }
  389. #endif
  390. static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)
  391. {
  392. if (!btf_type_is_ptr(t))
  393. return false;
  394. t = btf_type_skip_modifiers(btf, t->type, NULL);
  395. return btf_type_is_struct(t);
  396. }
  397. #endif