Blame view

include/linux/btf.h 6.72 KB
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
1
2
3
4
5
6
7
  /* SPDX-License-Identifier: GPL-2.0 */
  /* Copyright (c) 2018 Facebook */
  
  #ifndef _LINUX_BTF_H
  #define _LINUX_BTF_H 1
  
  #include <linux/types.h>
382072916   Martin KaFai Lau   bpf: Prepare btf_...
8
  #include <uapi/linux/btf.h>
c4d0bfb45   Alan Maguire   bpf: Add bpf_snpr...
9
  #include <uapi/linux/bpf.h>
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
10

85d33df35   Martin KaFai Lau   bpf: Introduce BP...
11
  #define BTF_TYPE_EMIT(type) ((void)(type *)0)
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
12
  struct btf;
ffa0c1cf5   Yonghong Song   bpf: enable cgrou...
13
  struct btf_member;
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
14
  struct btf_type;
f56a653c1   Martin KaFai Lau   bpf: btf: Add BPF...
15
  union bpf_attr;
31d0bc816   Alan Maguire   bpf: Move to gene...
16
  struct btf_show;
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
17

60197cfb6   Martin KaFai Lau   bpf: btf: Add BPF...
18
  extern const struct file_operations btf_fops;
f56a653c1   Martin KaFai Lau   bpf: btf: Add BPF...
19
20
21
  void btf_put(struct btf *btf);
  int btf_new_fd(const union bpf_attr *attr);
  struct btf *btf_get_by_fd(int fd);
60197cfb6   Martin KaFai Lau   bpf: btf: Add BPF...
22
23
24
  int btf_get_info_by_fd(const struct btf *btf,
  		       const union bpf_attr *attr,
  		       union bpf_attr __user *uattr);
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  /* Figure out the size of a type_id.  If type_id is a modifier
   * (e.g. const), it will be resolved to find out the type with size.
   *
   * For example:
   * In describing "const void *",  type_id is "const" and "const"
   * refers to "void *".  The return type will be "void *".
   *
   * If type_id is a simple "int", then return type will be "int".
   *
   * @btf: struct btf object
   * @type_id: Find out the size of type_id. The type_id of the return
   *           type is set to *type_id.
   * @ret_size: It can be NULL.  If not NULL, the size of the return
   *            type is set to *ret_size.
   * Return: The btf_type (resolved to another type with size info if needed).
   *         NULL is returned if type_id itself does not have size info
   *         (e.g. void) or it cannot be resolved to another type that
   *         has size info.
   *         *type_id and *ret_size will not be changed in the
   *         NULL return case.
   */
  const struct btf_type *btf_type_id_size(const struct btf *btf,
  					u32 *type_id,
  					u32 *ret_size);
31d0bc816   Alan Maguire   bpf: Move to gene...
49
50
51
52
53
54
55
56
57
58
59
60
  
  /*
   * Options to control show behaviour.
   *	- BTF_SHOW_COMPACT: no formatting around type information
   *	- BTF_SHOW_NONAME: no struct/union member names/types
   *	- BTF_SHOW_PTR_RAW: show raw (unobfuscated) pointer values;
   *	  equivalent to %px.
   *	- BTF_SHOW_ZERO: show zero-valued struct/union members; they
   *	  are not displayed by default
   *	- BTF_SHOW_UNSAFE: skip use of bpf_probe_read() to safely read
   *	  data before displaying it.
   */
c4d0bfb45   Alan Maguire   bpf: Add bpf_snpr...
61
62
63
64
  #define BTF_SHOW_COMPACT	BTF_F_COMPACT
  #define BTF_SHOW_NONAME		BTF_F_NONAME
  #define BTF_SHOW_PTR_RAW	BTF_F_PTR_RAW
  #define BTF_SHOW_ZERO		BTF_F_ZERO
31d0bc816   Alan Maguire   bpf: Move to gene...
65
  #define BTF_SHOW_UNSAFE		(1ULL << 4)
b00b8daec   Martin KaFai Lau   bpf: btf: Add pre...
66
67
  void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
  		       struct seq_file *m);
eb411377a   Alan Maguire   bpf: Add bpf_seq_...
68
69
  int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
  			    struct seq_file *m, u64 flags);
31d0bc816   Alan Maguire   bpf: Move to gene...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  
  /*
   * Copy len bytes of string representation of obj of BTF type_id into buf.
   *
   * @btf: struct btf object
   * @type_id: type id of type obj points to
   * @obj: pointer to typed data
   * @buf: buffer to write to
   * @len: maximum length to write to buf
   * @flags: show options (see above)
   *
   * Return: length that would have been/was copied as per snprintf, or
   *	   negative error.
   */
  int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,
  			   char *buf, int len, u64 flags);
78958fca7   Martin KaFai Lau   bpf: btf: Introdu...
86
87
  int btf_get_fd_by_id(u32 id);
  u32 btf_id(const struct btf *btf);
ffa0c1cf5   Yonghong Song   bpf: enable cgrou...
88
89
90
  bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
  			   const struct btf_member *m,
  			   u32 expected_offset, u32 expected_size);
d83525ca6   Alexei Starovoitov   bpf: introduce bp...
91
  int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
2824ecb70   Daniel Borkmann   bpf: allow for ke...
92
  bool btf_type_is_void(const struct btf_type *t);
27ae7997a   Martin KaFai Lau   bpf: Introduce BP...
93
94
95
96
97
98
99
  s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
  const struct btf_type *btf_type_skip_modifiers(const struct btf *btf,
  					       u32 id, u32 *res_id);
  const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
  					    u32 id, u32 *res_id);
  const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
  						 u32 id, u32 *res_id);
85d33df35   Martin KaFai Lau   bpf: Introduce BP...
100
101
  const struct btf_type *
  btf_resolve_size(const struct btf *btf, const struct btf_type *type,
6298399bf   Jiri Olsa   bpf: Move btf_res...
102
  		 u32 *type_size);
27ae7997a   Martin KaFai Lau   bpf: Introduce BP...
103
104
105
106
107
  
  #define for_each_member(i, struct_type, member)			\
  	for (i = 0, member = btf_type_member(struct_type);	\
  	     i < btf_type_vlen(struct_type);			\
  	     i++, member++)
f6161a8f3   Yonghong Song   bpf: fix a compil...
108

eaa6bcb71   Hao Luo   bpf: Introduce bp...
109
110
111
112
  #define for_each_vsi(i, datasec_type, member)			\
  	for (i = 0, member = btf_type_var_secinfo(datasec_type);	\
  	     i < btf_type_vlen(datasec_type);			\
  	     i++, member++)
382072916   Martin KaFai Lau   bpf: Prepare btf_...
113
114
115
116
117
118
119
120
121
  static inline bool btf_type_is_ptr(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_PTR;
  }
  
  static inline bool btf_type_is_int(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_INT;
  }
a9b59159d   John Fastabend   bpf: Do not allow...
122
123
124
125
  static inline bool btf_type_is_small_int(const struct btf_type *t)
  {
  	return btf_type_is_int(t) && t->size <= sizeof(u64);
  }
382072916   Martin KaFai Lau   bpf: Prepare btf_...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  static inline bool btf_type_is_enum(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_ENUM;
  }
  
  static inline bool btf_type_is_typedef(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
  }
  
  static inline bool btf_type_is_func(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;
  }
  
  static inline bool btf_type_is_func_proto(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC_PROTO;
  }
4976b718c   Hao Luo   bpf: Introduce ps...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  static inline bool btf_type_is_var(const struct btf_type *t)
  {
  	return BTF_INFO_KIND(t->info) == BTF_KIND_VAR;
  }
  
  /* union is only a special case of struct:
   * all its offsetof(member) == 0
   */
  static inline bool btf_type_is_struct(const struct btf_type *t)
  {
  	u8 kind = BTF_INFO_KIND(t->info);
  
  	return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
  }
27ae7997a   Martin KaFai Lau   bpf: Introduce BP...
159
160
161
162
  static inline u16 btf_type_vlen(const struct btf_type *t)
  {
  	return BTF_INFO_VLEN(t->info);
  }
be8704ff0   Alexei Starovoitov   bpf: Introduce dy...
163
164
165
166
  static inline u16 btf_func_linkage(const struct btf_type *t)
  {
  	return BTF_INFO_VLEN(t->info);
  }
27ae7997a   Martin KaFai Lau   bpf: Introduce BP...
167
168
169
170
  static inline bool btf_type_kflag(const struct btf_type *t)
  {
  	return BTF_INFO_KFLAG(t->info);
  }
85d33df35   Martin KaFai Lau   bpf: Introduce BP...
171
172
173
174
175
176
  static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
  					const struct btf_member *member)
  {
  	return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
  					   : member->offset;
  }
27ae7997a   Martin KaFai Lau   bpf: Introduce BP...
177
178
179
180
181
182
183
184
185
186
187
  static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
  					   const struct btf_member *member)
  {
  	return btf_type_kflag(struct_type) ? BTF_MEMBER_BITFIELD_SIZE(member->offset)
  					   : 0;
  }
  
  static inline const struct btf_member *btf_type_member(const struct btf_type *t)
  {
  	return (const struct btf_member *)(t + 1);
  }
eaa6bcb71   Hao Luo   bpf: Introduce bp...
188
189
190
191
192
  static inline const struct btf_var_secinfo *btf_type_var_secinfo(
  		const struct btf_type *t)
  {
  	return (const struct btf_var_secinfo *)(t + 1);
  }
f6161a8f3   Yonghong Song   bpf: fix a compil...
193
  #ifdef CONFIG_BPF_SYSCALL
838e96904   Yonghong Song   bpf: Introduce bp...
194
195
  const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
  const char *btf_name_by_offset(const struct btf *btf, u32 offset);
8580ac940   Alexei Starovoitov   bpf: Process in-k...
196
  struct btf *btf_parse_vmlinux(void);
5b92a28aa   Alexei Starovoitov   bpf: Support atta...
197
  struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog);
f6161a8f3   Yonghong Song   bpf: fix a compil...
198
199
200
201
202
203
204
205
206
207
208
209
  #else
  static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
  						    u32 type_id)
  {
  	return NULL;
  }
  static inline const char *btf_name_by_offset(const struct btf *btf,
  					     u32 offset)
  {
  	return NULL;
  }
  #endif
eb3f595da   Martin KaFai Lau   bpf: btf: Validat...
210
211
  
  #endif