bpf_core_read.h 19 KB

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