treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / bpf / bpf_trace_helpers.h
blobc6f1354d93fb112cc987d8e9171e7406f8efa334
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_TRACE_HELPERS_H
3 #define __BPF_TRACE_HELPERS_H
5 #include <bpf/bpf_helpers.h>
7 #define ___bpf_concat(a, b) a ## b
8 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
9 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
10 #define ___bpf_narg(...) \
11 ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
12 #define ___bpf_empty(...) \
13 ___bpf_nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0)
15 #define ___bpf_ctx_cast0() ctx
16 #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0]
17 #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1]
18 #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2]
19 #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3]
20 #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4]
21 #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5]
22 #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6]
23 #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7]
24 #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8]
25 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
26 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
27 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
28 #define ___bpf_ctx_cast(args...) \
29 ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
32 * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
33 * similar kinds of BPF programs, that accept input arguments as a single
34 * pointer to untyped u64 array, where each u64 can actually be a typed
35 * pointer or integer of different size. Instead of requring user to write
36 * manual casts and work with array elements by index, BPF_PROG macro
37 * allows user to declare a list of named and typed input arguments in the
38 * same syntax as for normal C function. All the casting is hidden and
39 * performed transparently, while user code can just assume working with
40 * function arguments of specified type and name.
42 * Original raw context argument is preserved as well as 'ctx' argument.
43 * This is useful when using BPF helpers that expect original context
44 * as one of the parameters (e.g., for bpf_perf_event_output()).
46 #define BPF_PROG(name, args...) \
47 name(unsigned long long *ctx); \
48 static __always_inline typeof(name(0)) \
49 ____##name(unsigned long long *ctx, ##args); \
50 typeof(name(0)) name(unsigned long long *ctx) \
51 { \
52 _Pragma("GCC diagnostic push") \
53 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
54 return ____##name(___bpf_ctx_cast(args)); \
55 _Pragma("GCC diagnostic pop") \
56 } \
57 static __always_inline typeof(name(0)) \
58 ____##name(unsigned long long *ctx, ##args)
60 struct pt_regs;
62 #define ___bpf_kprobe_args0() ctx
63 #define ___bpf_kprobe_args1(x) \
64 ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx)
65 #define ___bpf_kprobe_args2(x, args...) \
66 ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx)
67 #define ___bpf_kprobe_args3(x, args...) \
68 ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx)
69 #define ___bpf_kprobe_args4(x, args...) \
70 ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx)
71 #define ___bpf_kprobe_args5(x, args...) \
72 ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx)
73 #define ___bpf_kprobe_args(args...) \
74 ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args)
77 * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for
78 * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific
79 * low-level way of getting kprobe input arguments from struct pt_regs, and
80 * provides a familiar typed and named function arguments syntax and
81 * semantics of accessing kprobe input paremeters.
83 * Original struct pt_regs* context is preserved as 'ctx' argument. This might
84 * be necessary when using BPF helpers like bpf_perf_event_output().
86 #define BPF_KPROBE(name, args...) \
87 name(struct pt_regs *ctx); \
88 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args);\
89 typeof(name(0)) name(struct pt_regs *ctx) \
90 { \
91 _Pragma("GCC diagnostic push") \
92 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
93 return ____##name(___bpf_kprobe_args(args)); \
94 _Pragma("GCC diagnostic pop") \
95 } \
96 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
98 #define ___bpf_kretprobe_args0() ctx
99 #define ___bpf_kretprobe_argsN(x, args...) \
100 ___bpf_kprobe_args(args), (void *)PT_REGS_RET(ctx)
101 #define ___bpf_kretprobe_args(args...) \
102 ___bpf_apply(___bpf_kretprobe_args, ___bpf_empty(args))(args)
105 * BPF_KRETPROBE is similar to BPF_KPROBE, except, in addition to listing all
106 * input kprobe arguments, one last extra argument has to be specified, which
107 * captures kprobe return value.
109 #define BPF_KRETPROBE(name, args...) \
110 name(struct pt_regs *ctx); \
111 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args);\
112 typeof(name(0)) name(struct pt_regs *ctx) \
114 _Pragma("GCC diagnostic push") \
115 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
116 return ____##name(___bpf_kretprobe_args(args)); \
117 _Pragma("GCC diagnostic pop") \
119 static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args)
120 #endif