xref: /linux/tools/lib/bpf/bpf_core_read.h (revision bf36793fa260cb68cc817f311f1f683788261796)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_CORE_READ_H__
3 #define __BPF_CORE_READ_H__
4 
5 #include "bpf_helpers.h"
6 
7 /*
8  * enum bpf_field_info_kind is passed as a second argument into
9  * __builtin_preserve_field_info() built-in to get a specific aspect of
10  * a field, captured as a first argument. __builtin_preserve_field_info(field,
11  * info_kind) returns __u32 integer and produces BTF field relocation, which
12  * is understood and processed by libbpf during BPF object loading. See
13  * selftests/bpf for examples.
14  */
15 enum bpf_field_info_kind {
16 	BPF_FIELD_BYTE_OFFSET = 0,	/* field byte offset */
17 	BPF_FIELD_BYTE_SIZE = 1,
18 	BPF_FIELD_EXISTS = 2,		/* field existence in target kernel */
19 	BPF_FIELD_SIGNED = 3,
20 	BPF_FIELD_LSHIFT_U64 = 4,
21 	BPF_FIELD_RSHIFT_U64 = 5,
22 };
23 
24 /* second argument to __builtin_btf_type_id() built-in */
25 enum bpf_type_id_kind {
26 	BPF_TYPE_ID_LOCAL = 0,		/* BTF type ID in local program */
27 	BPF_TYPE_ID_TARGET = 1,		/* BTF type ID in target kernel */
28 };
29 
30 /* second argument to __builtin_preserve_type_info() built-in */
31 enum bpf_type_info_kind {
32 	BPF_TYPE_EXISTS = 0,		/* type existence in target kernel */
33 	BPF_TYPE_SIZE = 1,		/* type size in target kernel */
34 	BPF_TYPE_MATCHES = 2,		/* type match in target kernel */
35 };
36 
37 /* second argument to __builtin_preserve_enum_value() built-in */
38 enum bpf_enum_value_kind {
39 	BPF_ENUMVAL_EXISTS = 0,		/* enum value existence in kernel */
40 	BPF_ENUMVAL_VALUE = 1,		/* enum value value relocation */
41 };
42 
43 #define __CORE_RELO(src, field, info)					      \
44 	__builtin_preserve_field_info((src)->field, BPF_FIELD_##info)
45 
46 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
47 #define __CORE_BITFIELD_PROBE_READ(dst, src, fld)			      \
48 	bpf_probe_read_kernel(						      \
49 			(void *)dst,					      \
50 			__CORE_RELO(src, fld, BYTE_SIZE),		      \
51 			(const void *)src + __CORE_RELO(src, fld, BYTE_OFFSET))
52 #else
53 /* semantics of LSHIFT_64 assumes loading values into low-ordered bytes, so
54  * for big-endian we need to adjust destination pointer accordingly, based on
55  * field byte size
56  */
57 #define __CORE_BITFIELD_PROBE_READ(dst, src, fld)			      \
58 	bpf_probe_read_kernel(						      \
59 			(void *)dst + (8 - __CORE_RELO(src, fld, BYTE_SIZE)), \
60 			__CORE_RELO(src, fld, BYTE_SIZE),		      \
61 			(const void *)src + __CORE_RELO(src, fld, BYTE_OFFSET))
62 #endif
63 
64 /*
65  * Extract bitfield, identified by s->field, and return its value as u64.
66  * All this is done in relocatable manner, so bitfield changes such as
67  * signedness, bit size, offset changes, this will be handled automatically.
68  * This version of macro is using bpf_probe_read_kernel() to read underlying
69  * integer storage. Macro functions as an expression and its return type is
70  * bpf_probe_read_kernel()'s return value: 0, on success, <0 on error.
71  */
72 #define BPF_CORE_READ_BITFIELD_PROBED(s, field) ({			      \
73 	unsigned long long val = 0;					      \
74 									      \
75 	__CORE_BITFIELD_PROBE_READ(&val, s, field);			      \
76 	val <<= __CORE_RELO(s, field, LSHIFT_U64);			      \
77 	if (__CORE_RELO(s, field, SIGNED))				      \
78 		val = ((long long)val) >> __CORE_RELO(s, field, RSHIFT_U64);  \
79 	else								      \
80 		val = val >> __CORE_RELO(s, field, RSHIFT_U64);		      \
81 	val;								      \
82 })
83 
84 /*
85  * Extract bitfield, identified by s->field, and return its value as u64.
86  * This version of macro is using direct memory reads and should be used from
87  * BPF program types that support such functionality (e.g., typed raw
88  * tracepoints).
89  */
90 #define BPF_CORE_READ_BITFIELD(s, field) ({				      \
91 	const void *p = (const void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \
92 	unsigned long long val;						      \
93 									      \
94 	/* This is a so-called barrier_var() operation that makes specified   \
95 	 * variable "a black box" for optimizing compiler.		      \
96 	 * It forces compiler to perform BYTE_OFFSET relocation on p and use  \
97 	 * its calculated value in the switch below, instead of applying      \
98 	 * the same relocation 4 times for each individual memory load.       \
99 	 */								      \
100 	asm volatile("" : "=r"(p) : "0"(p));				      \
101 									      \
102 	switch (__CORE_RELO(s, field, BYTE_SIZE)) {			      \
103 	case 1: val = *(const unsigned char *)p; break;			      \
104 	case 2: val = *(const unsigned short *)p; break;		      \
105 	case 4: val = *(const unsigned int *)p; break;			      \
106 	case 8: val = *(const unsigned long long *)p; break;		      \
107 	default: val = 0; break;					      \
108 	}								      \
109 	val <<= __CORE_RELO(s, field, LSHIFT_U64);			      \
110 	if (__CORE_RELO(s, field, SIGNED))				      \
111 		val = ((long long)val) >> __CORE_RELO(s, field, RSHIFT_U64);  \
112 	else								      \
113 		val = val >> __CORE_RELO(s, field, RSHIFT_U64);		      \
114 	val;								      \
115 })
116 
117 /*
118  * Write to a bitfield, identified by s->field.
119  * This is the inverse of BPF_CORE_WRITE_BITFIELD().
120  */
121 #define BPF_CORE_WRITE_BITFIELD(s, field, new_val) ({			\
122 	void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET);	\
123 	unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE);	\
124 	unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64);	\
125 	unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64);	\
126 	unsigned long long mask, val, nval = new_val;			\
127 	unsigned int rpad = rshift - lshift;				\
128 									\
129 	asm volatile("" : "+r"(p));					\
130 									\
131 	switch (byte_size) {						\
132 	case 1: val = *(unsigned char *)p; break;			\
133 	case 2: val = *(unsigned short *)p; break;			\
134 	case 4: val = *(unsigned int *)p; break;			\
135 	case 8: val = *(unsigned long long *)p; break;			\
136 	}								\
137 									\
138 	mask = (~0ULL << rshift) >> lshift;				\
139 	val = (val & ~mask) | ((nval << rpad) & mask);			\
140 									\
141 	switch (byte_size) {						\
142 	case 1: *(unsigned char *)p      = val; break;			\
143 	case 2: *(unsigned short *)p     = val; break;			\
144 	case 4: *(unsigned int *)p       = val; break;			\
145 	case 8: *(unsigned long long *)p = val; break;			\
146 	}								\
147 })
148 
149 /* Differentiator between compilers builtin implementations. This is a
150  * requirement due to the compiler parsing differences where GCC optimizes
151  * early in parsing those constructs of type pointers to the builtin specific
152  * type, resulting in not being possible to collect the required type
153  * information in the builtin expansion.
154  */
155 #ifdef __clang__
156 #define ___bpf_typeof(type) ((typeof(type) *) 0)
157 #else
158 #define ___bpf_typeof1(type, NR) ({					    \
159 	extern typeof(type) *___concat(bpf_type_tmp_, NR);		    \
160 	___concat(bpf_type_tmp_, NR);					    \
161 })
162 #define ___bpf_typeof(type) ___bpf_typeof1(type, __COUNTER__)
163 #endif
164 
165 #ifdef __clang__
166 #define ___bpf_field_ref1(field)	(field)
167 #define ___bpf_field_ref2(type, field)	(___bpf_typeof(type)->field)
168 #else
169 #define ___bpf_field_ref1(field)	(&(field))
170 #define ___bpf_field_ref2(type, field)	(&(___bpf_typeof(type)->field))
171 #endif
172 #define ___bpf_field_ref(args...)					    \
173 	___bpf_apply(___bpf_field_ref, ___bpf_narg(args))(args)
174 
175 /*
176  * Convenience macro to check that field actually exists in target kernel's.
177  * Returns:
178  *    1, if matching field is present in target kernel;
179  *    0, if no matching field found.
180  *
181  * Supports two forms:
182  *   - field reference through variable access:
183  *     bpf_core_field_exists(p->my_field);
184  *   - field reference through type and field names:
185  *     bpf_core_field_exists(struct my_type, my_field).
186  */
187 #define bpf_core_field_exists(field...)					    \
188 	__builtin_preserve_field_info(___bpf_field_ref(field), BPF_FIELD_EXISTS)
189 
190 /*
191  * Convenience macro to get the byte size of a field. Works for integers,
192  * struct/unions, pointers, arrays, and enums.
193  *
194  * Supports two forms:
195  *   - field reference through variable access:
196  *     bpf_core_field_size(p->my_field);
197  *   - field reference through type and field names:
198  *     bpf_core_field_size(struct my_type, my_field).
199  */
200 #define bpf_core_field_size(field...)					    \
201 	__builtin_preserve_field_info(___bpf_field_ref(field), BPF_FIELD_BYTE_SIZE)
202 
203 /*
204  * Convenience macro to get field's byte offset.
205  *
206  * Supports two forms:
207  *   - field reference through variable access:
208  *     bpf_core_field_offset(p->my_field);
209  *   - field reference through type and field names:
210  *     bpf_core_field_offset(struct my_type, my_field).
211  */
212 #define bpf_core_field_offset(field...)					    \
213 	__builtin_preserve_field_info(___bpf_field_ref(field), BPF_FIELD_BYTE_OFFSET)
214 
215 /*
216  * Convenience macro to get BTF type ID of a specified type, using a local BTF
217  * information. Return 32-bit unsigned integer with type ID from program's own
218  * BTF. Always succeeds.
219  */
220 #define bpf_core_type_id_local(type)					    \
221 	__builtin_btf_type_id(*___bpf_typeof(type), BPF_TYPE_ID_LOCAL)
222 
223 /*
224  * Convenience macro to get BTF type ID of a target kernel's type that matches
225  * specified local type.
226  * Returns:
227  *    - valid 32-bit unsigned type ID in kernel BTF;
228  *    - 0, if no matching type was found in a target kernel BTF.
229  */
230 #define bpf_core_type_id_kernel(type)					    \
231 	__builtin_btf_type_id(*___bpf_typeof(type), BPF_TYPE_ID_TARGET)
232 
233 /*
234  * Convenience macro to check that provided named type
235  * (struct/union/enum/typedef) exists in a target kernel.
236  * Returns:
237  *    1, if such type is present in target kernel's BTF;
238  *    0, if no matching type is found.
239  */
240 #define bpf_core_type_exists(type)					    \
241 	__builtin_preserve_type_info(*___bpf_typeof(type), BPF_TYPE_EXISTS)
242 
243 /*
244  * Convenience macro to check that provided named type
245  * (struct/union/enum/typedef) "matches" that in a target kernel.
246  * Returns:
247  *    1, if the type matches in the target kernel's BTF;
248  *    0, if the type does not match any in the target kernel
249  */
250 #define bpf_core_type_matches(type)					    \
251 	__builtin_preserve_type_info(*___bpf_typeof(type), BPF_TYPE_MATCHES)
252 
253 /*
254  * Convenience macro to get the byte size of a provided named type
255  * (struct/union/enum/typedef) in a target kernel.
256  * Returns:
257  *    >= 0 size (in bytes), if type is present in target kernel's BTF;
258  *    0, if no matching type is found.
259  */
260 #define bpf_core_type_size(type)					    \
261 	__builtin_preserve_type_info(*___bpf_typeof(type), BPF_TYPE_SIZE)
262 
263 /*
264  * Convenience macro to check that provided enumerator value is defined in
265  * a target kernel.
266  * Returns:
267  *    1, if specified enum type and its enumerator value are present in target
268  *    kernel's BTF;
269  *    0, if no matching enum and/or enum value within that enum is found.
270  */
271 #ifdef __clang__
272 #define bpf_core_enum_value_exists(enum_type, enum_value)		    \
273 	__builtin_preserve_enum_value(*(typeof(enum_type) *)enum_value, BPF_ENUMVAL_EXISTS)
274 #else
275 #define bpf_core_enum_value_exists(enum_type, enum_value)		    \
276 	__builtin_preserve_enum_value(___bpf_typeof(enum_type), enum_value, BPF_ENUMVAL_EXISTS)
277 #endif
278 
279 /*
280  * Convenience macro to get the integer value of an enumerator value in
281  * a target kernel.
282  * Returns:
283  *    64-bit value, if specified enum type and its enumerator value are
284  *    present in target kernel's BTF;
285  *    0, if no matching enum and/or enum value within that enum is found.
286  */
287 #ifdef __clang__
288 #define bpf_core_enum_value(enum_type, enum_value)			    \
289 	__builtin_preserve_enum_value(*(typeof(enum_type) *)enum_value, BPF_ENUMVAL_VALUE)
290 #else
291 #define bpf_core_enum_value(enum_type, enum_value)			    \
292 	__builtin_preserve_enum_value(___bpf_typeof(enum_type), enum_value, BPF_ENUMVAL_VALUE)
293 #endif
294 
295 /*
296  * bpf_core_read() abstracts away bpf_probe_read_kernel() call and captures
297  * offset relocation for source address using __builtin_preserve_access_index()
298  * built-in, provided by Clang.
299  *
300  * __builtin_preserve_access_index() takes as an argument an expression of
301  * taking an address of a field within struct/union. It makes compiler emit
302  * a relocation, which records BTF type ID describing root struct/union and an
303  * accessor string which describes exact embedded field that was used to take
304  * an address. See detailed description of this relocation format and
305  * semantics in comments to struct bpf_core_relo in include/uapi/linux/bpf.h.
306  *
307  * This relocation allows libbpf to adjust BPF instruction to use correct
308  * actual field offset, based on target kernel BTF type that matches original
309  * (local) BTF, used to record relocation.
310  */
311 #define bpf_core_read(dst, sz, src)					    \
312 	bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))
313 
314 /* NOTE: see comments for BPF_CORE_READ_USER() about the proper types use. */
315 #define bpf_core_read_user(dst, sz, src)				    \
316 	bpf_probe_read_user(dst, sz, (const void *)__builtin_preserve_access_index(src))
317 /*
318  * bpf_core_read_str() is a thin wrapper around bpf_probe_read_str()
319  * additionally emitting BPF CO-RE field relocation for specified source
320  * argument.
321  */
322 #define bpf_core_read_str(dst, sz, src)					    \
323 	bpf_probe_read_kernel_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
324 
325 /* NOTE: see comments for BPF_CORE_READ_USER() about the proper types use. */
326 #define bpf_core_read_user_str(dst, sz, src)				    \
327 	bpf_probe_read_user_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
328 
329 extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
330 
331 /*
332  * Cast provided pointer *ptr* into a pointer to a specified *type* in such
333  * a way that BPF verifier will become aware of associated kernel-side BTF
334  * type. This allows to access members of kernel types directly without the
335  * need to use BPF_CORE_READ() macros.
336  */
337 #define bpf_core_cast(ptr, type)					    \
338 	((typeof(type) *)bpf_rdonly_cast((ptr), bpf_core_type_id_kernel(type)))
339 
340 #define ___concat(a, b) a ## b
341 #define ___apply(fn, n) ___concat(fn, n)
342 #define ___nth(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11, N, ...) N
343 
344 /*
345  * return number of provided arguments; used for switch-based variadic macro
346  * definitions (see ___last, ___arrow, etc below)
347  */
348 #define ___narg(...) ___nth(_, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
349 /*
350  * return 0 if no arguments are passed, N - otherwise; used for
351  * recursively-defined macros to specify termination (0) case, and generic
352  * (N) case (e.g., ___read_ptrs, ___core_read)
353  */
354 #define ___empty(...) ___nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0)
355 
356 #define ___last1(x) x
357 #define ___last2(a, x) x
358 #define ___last3(a, b, x) x
359 #define ___last4(a, b, c, x) x
360 #define ___last5(a, b, c, d, x) x
361 #define ___last6(a, b, c, d, e, x) x
362 #define ___last7(a, b, c, d, e, f, x) x
363 #define ___last8(a, b, c, d, e, f, g, x) x
364 #define ___last9(a, b, c, d, e, f, g, h, x) x
365 #define ___last10(a, b, c, d, e, f, g, h, i, x) x
366 #define ___last(...) ___apply(___last, ___narg(__VA_ARGS__))(__VA_ARGS__)
367 
368 #define ___nolast2(a, _) a
369 #define ___nolast3(a, b, _) a, b
370 #define ___nolast4(a, b, c, _) a, b, c
371 #define ___nolast5(a, b, c, d, _) a, b, c, d
372 #define ___nolast6(a, b, c, d, e, _) a, b, c, d, e
373 #define ___nolast7(a, b, c, d, e, f, _) a, b, c, d, e, f
374 #define ___nolast8(a, b, c, d, e, f, g, _) a, b, c, d, e, f, g
375 #define ___nolast9(a, b, c, d, e, f, g, h, _) a, b, c, d, e, f, g, h
376 #define ___nolast10(a, b, c, d, e, f, g, h, i, _) a, b, c, d, e, f, g, h, i
377 #define ___nolast(...) ___apply(___nolast, ___narg(__VA_ARGS__))(__VA_ARGS__)
378 
379 #define ___arrow1(a) a
380 #define ___arrow2(a, b) a->b
381 #define ___arrow3(a, b, c) a->b->c
382 #define ___arrow4(a, b, c, d) a->b->c->d
383 #define ___arrow5(a, b, c, d, e) a->b->c->d->e
384 #define ___arrow6(a, b, c, d, e, f) a->b->c->d->e->f
385 #define ___arrow7(a, b, c, d, e, f, g) a->b->c->d->e->f->g
386 #define ___arrow8(a, b, c, d, e, f, g, h) a->b->c->d->e->f->g->h
387 #define ___arrow9(a, b, c, d, e, f, g, h, i) a->b->c->d->e->f->g->h->i
388 #define ___arrow10(a, b, c, d, e, f, g, h, i, j) a->b->c->d->e->f->g->h->i->j
389 #define ___arrow(...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__)
390 
391 #define ___type(...) typeof(___arrow(__VA_ARGS__))
392 
393 #define ___read(read_fn, dst, src_type, src, accessor)			    \
394 	read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)
395 
396 /* "recursively" read a sequence of inner pointers using local __t var */
397 #define ___rd_first(fn, src, a) ___read(fn, &__t, ___type(src), src, a);
398 #define ___rd_last(fn, ...)						    \
399 	___read(fn, &__t, ___type(___nolast(__VA_ARGS__)), __t, ___last(__VA_ARGS__));
400 #define ___rd_p1(fn, ...) const void *__t; ___rd_first(fn, __VA_ARGS__)
401 #define ___rd_p2(fn, ...) ___rd_p1(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
402 #define ___rd_p3(fn, ...) ___rd_p2(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
403 #define ___rd_p4(fn, ...) ___rd_p3(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
404 #define ___rd_p5(fn, ...) ___rd_p4(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
405 #define ___rd_p6(fn, ...) ___rd_p5(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
406 #define ___rd_p7(fn, ...) ___rd_p6(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
407 #define ___rd_p8(fn, ...) ___rd_p7(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
408 #define ___rd_p9(fn, ...) ___rd_p8(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__)
409 #define ___read_ptrs(fn, src, ...)					    \
410 	___apply(___rd_p, ___narg(__VA_ARGS__))(fn, src, __VA_ARGS__)
411 
412 #define ___core_read0(fn, fn_ptr, dst, src, a)				    \
413 	___read(fn, dst, ___type(src), src, a);
414 #define ___core_readN(fn, fn_ptr, dst, src, ...)			    \
415 	___read_ptrs(fn_ptr, src, ___nolast(__VA_ARGS__))		    \
416 	___read(fn, dst, ___type(src, ___nolast(__VA_ARGS__)), __t,	    \
417 		___last(__VA_ARGS__));
418 #define ___core_read(fn, fn_ptr, dst, src, a, ...)			    \
419 	___apply(___core_read, ___empty(__VA_ARGS__))(fn, fn_ptr, dst,	    \
420 						      src, a, ##__VA_ARGS__)
421 
422 /*
423  * BPF_CORE_READ_INTO() is a more performance-conscious variant of
424  * BPF_CORE_READ(), in which final field is read into user-provided storage.
425  * See BPF_CORE_READ() below for more details on general usage.
426  */
427 #define BPF_CORE_READ_INTO(dst, src, a, ...) ({				    \
428 	___core_read(bpf_core_read, bpf_core_read,			    \
429 		     dst, (src), a, ##__VA_ARGS__)			    \
430 })
431 
432 /*
433  * Variant of BPF_CORE_READ_INTO() for reading from user-space memory.
434  *
435  * NOTE: see comments for BPF_CORE_READ_USER() about the proper types use.
436  */
437 #define BPF_CORE_READ_USER_INTO(dst, src, a, ...) ({			    \
438 	___core_read(bpf_core_read_user, bpf_core_read_user,		    \
439 		     dst, (src), a, ##__VA_ARGS__)			    \
440 })
441 
442 /* Non-CO-RE variant of BPF_CORE_READ_INTO() */
443 #define BPF_PROBE_READ_INTO(dst, src, a, ...) ({			    \
444 	___core_read(bpf_probe_read_kernel, bpf_probe_read_kernel,	    \
445 		     dst, (src), a, ##__VA_ARGS__)			    \
446 })
447 
448 /* Non-CO-RE variant of BPF_CORE_READ_USER_INTO().
449  *
450  * As no CO-RE relocations are emitted, source types can be arbitrary and are
451  * not restricted to kernel types only.
452  */
453 #define BPF_PROBE_READ_USER_INTO(dst, src, a, ...) ({			    \
454 	___core_read(bpf_probe_read_user, bpf_probe_read_user,		    \
455 		     dst, (src), a, ##__VA_ARGS__)			    \
456 })
457 
458 /*
459  * BPF_CORE_READ_STR_INTO() does same "pointer chasing" as
460  * BPF_CORE_READ() for intermediate pointers, but then executes (and returns
461  * corresponding error code) bpf_core_read_str() for final string read.
462  */
463 #define BPF_CORE_READ_STR_INTO(dst, src, a, ...) ({			    \
464 	___core_read(bpf_core_read_str, bpf_core_read,			    \
465 		     dst, (src), a, ##__VA_ARGS__)			    \
466 })
467 
468 /*
469  * Variant of BPF_CORE_READ_STR_INTO() for reading from user-space memory.
470  *
471  * NOTE: see comments for BPF_CORE_READ_USER() about the proper types use.
472  */
473 #define BPF_CORE_READ_USER_STR_INTO(dst, src, a, ...) ({		    \
474 	___core_read(bpf_core_read_user_str, bpf_core_read_user,	    \
475 		     dst, (src), a, ##__VA_ARGS__)			    \
476 })
477 
478 /* Non-CO-RE variant of BPF_CORE_READ_STR_INTO() */
479 #define BPF_PROBE_READ_STR_INTO(dst, src, a, ...) ({			    \
480 	___core_read(bpf_probe_read_kernel_str, bpf_probe_read_kernel,	    \
481 		     dst, (src), a, ##__VA_ARGS__)			    \
482 })
483 
484 /*
485  * Non-CO-RE variant of BPF_CORE_READ_USER_STR_INTO().
486  *
487  * As no CO-RE relocations are emitted, source types can be arbitrary and are
488  * not restricted to kernel types only.
489  */
490 #define BPF_PROBE_READ_USER_STR_INTO(dst, src, a, ...) ({		    \
491 	___core_read(bpf_probe_read_user_str, bpf_probe_read_user,	    \
492 		     dst, (src), a, ##__VA_ARGS__)			    \
493 })
494 
495 /*
496  * BPF_CORE_READ() is used to simplify BPF CO-RE relocatable read, especially
497  * when there are few pointer chasing steps.
498  * E.g., what in non-BPF world (or in BPF w/ BCC) would be something like:
499  *	int x = s->a.b.c->d.e->f->g;
500  * can be succinctly achieved using BPF_CORE_READ as:
501  *	int x = BPF_CORE_READ(s, a.b.c, d.e, f, g);
502  *
503  * BPF_CORE_READ will decompose above statement into 4 bpf_core_read (BPF
504  * CO-RE relocatable bpf_probe_read_kernel() wrapper) calls, logically
505  * equivalent to:
506  * 1. const void *__t = s->a.b.c;
507  * 2. __t = __t->d.e;
508  * 3. __t = __t->f;
509  * 4. return __t->g;
510  *
511  * Equivalence is logical, because there is a heavy type casting/preservation
512  * involved, as well as all the reads are happening through
513  * bpf_probe_read_kernel() calls using __builtin_preserve_access_index() to
514  * emit CO-RE relocations.
515  *
516  * N.B. Only up to 9 "field accessors" are supported, which should be more
517  * than enough for any practical purpose.
518  */
519 #define BPF_CORE_READ(src, a, ...) ({					    \
520 	___type((src), a, ##__VA_ARGS__) __r;				    \
521 	BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
522 	__r;								    \
523 })
524 
525 /*
526  * Variant of BPF_CORE_READ() for reading from user-space memory.
527  *
528  * NOTE: all the source types involved are still *kernel types* and need to
529  * exist in kernel (or kernel module) BTF, otherwise CO-RE relocation will
530  * fail. Custom user types are not relocatable with CO-RE.
531  * The typical situation in which BPF_CORE_READ_USER() might be used is to
532  * read kernel UAPI types from the user-space memory passed in as a syscall
533  * input argument.
534  */
535 #define BPF_CORE_READ_USER(src, a, ...) ({				    \
536 	___type((src), a, ##__VA_ARGS__) __r;				    \
537 	BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
538 	__r;								    \
539 })
540 
541 /* Non-CO-RE variant of BPF_CORE_READ() */
542 #define BPF_PROBE_READ(src, a, ...) ({					    \
543 	___type((src), a, ##__VA_ARGS__) __r;				    \
544 	BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
545 	__r;								    \
546 })
547 
548 /*
549  * Non-CO-RE variant of BPF_CORE_READ_USER().
550  *
551  * As no CO-RE relocations are emitted, source types can be arbitrary and are
552  * not restricted to kernel types only.
553  */
554 #define BPF_PROBE_READ_USER(src, a, ...) ({				    \
555 	___type((src), a, ##__VA_ARGS__) __r;				    \
556 	BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);	    \
557 	__r;								    \
558 })
559 
560 #endif
561 
562