Steven Rostedt (VMware) | 179a0cc | 2018-08-16 11:20:54 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016 Facebook |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/bpf.h> |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 9 | #include <linux/bpf_perf_event.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 10 | #include <linux/filter.h> |
| 11 | #include <linux/uaccess.h> |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 12 | #include <linux/ctype.h> |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 13 | #include <linux/kprobes.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 14 | #include <linux/syscalls.h> |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 15 | #include <linux/error-injection.h> |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 16 | |
| 17 | #include "trace_probe.h" |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 18 | #include "trace.h" |
| 19 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 20 | #ifdef CONFIG_MODULES |
| 21 | struct bpf_trace_module { |
| 22 | struct module *module; |
| 23 | struct list_head list; |
| 24 | }; |
| 25 | |
| 26 | static LIST_HEAD(bpf_trace_modules); |
| 27 | static DEFINE_MUTEX(bpf_module_mutex); |
| 28 | |
| 29 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) |
| 30 | { |
| 31 | struct bpf_raw_event_map *btp, *ret = NULL; |
| 32 | struct bpf_trace_module *btm; |
| 33 | unsigned int i; |
| 34 | |
| 35 | mutex_lock(&bpf_module_mutex); |
| 36 | list_for_each_entry(btm, &bpf_trace_modules, list) { |
| 37 | for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { |
| 38 | btp = &btm->module->bpf_raw_events[i]; |
| 39 | if (!strcmp(btp->tp->name, name)) { |
| 40 | if (try_module_get(btm->module)) |
| 41 | ret = btp; |
| 42 | goto out; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | out: |
| 47 | mutex_unlock(&bpf_module_mutex); |
| 48 | return ret; |
| 49 | } |
| 50 | #else |
| 51 | static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) |
| 52 | { |
| 53 | return NULL; |
| 54 | } |
| 55 | #endif /* CONFIG_MODULES */ |
| 56 | |
Gianluca Borello | 035226b | 2017-10-26 01:47:42 +0000 | [diff] [blame] | 57 | u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 58 | u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); |
Gianluca Borello | 035226b | 2017-10-26 01:47:42 +0000 | [diff] [blame] | 59 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 60 | /** |
| 61 | * trace_call_bpf - invoke BPF program |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 62 | * @call: tracepoint event |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 63 | * @ctx: opaque context pointer |
| 64 | * |
| 65 | * kprobe handlers execute BPF programs via this helper. |
| 66 | * Can be used from static tracepoints in the future. |
| 67 | * |
| 68 | * Return: BPF programs always return an integer which is interpreted by |
| 69 | * kprobe handler as: |
| 70 | * 0 - return from kprobe (event is filtered out) |
| 71 | * 1 - store kprobe event into ring buffer |
| 72 | * Other values are reserved and currently alias to 1 |
| 73 | */ |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 74 | unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 75 | { |
| 76 | unsigned int ret; |
| 77 | |
| 78 | if (in_nmi()) /* not supported yet */ |
| 79 | return 1; |
| 80 | |
| 81 | preempt_disable(); |
| 82 | |
| 83 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { |
| 84 | /* |
| 85 | * since some bpf program is already running on this cpu, |
| 86 | * don't call into another bpf program (same or different) |
| 87 | * and don't send kprobe event into ring-buffer, |
| 88 | * so return zero here |
| 89 | */ |
| 90 | ret = 0; |
| 91 | goto out; |
| 92 | } |
| 93 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock |
| 96 | * to all call sites, we did a bpf_prog_array_valid() there to check |
| 97 | * whether call->prog_array is empty or not, which is |
| 98 | * a heurisitc to speed up execution. |
| 99 | * |
| 100 | * If bpf_prog_array_valid() fetched prog_array was |
| 101 | * non-NULL, we go into trace_call_bpf() and do the actual |
| 102 | * proper rcu_dereference() under RCU lock. |
| 103 | * If it turns out that prog_array is NULL then, we bail out. |
| 104 | * For the opposite, if the bpf_prog_array_valid() fetched pointer |
| 105 | * was NULL, you'll skip the prog_array with the risk of missing |
| 106 | * out of events when it was updated in between this and the |
| 107 | * rcu_dereference() which is accepted risk. |
| 108 | */ |
| 109 | ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 110 | |
| 111 | out: |
| 112 | __this_cpu_dec(bpf_prog_active); |
| 113 | preempt_enable(); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(trace_call_bpf); |
| 118 | |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 119 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
| 120 | BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) |
| 121 | { |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 122 | regs_set_return_value(regs, rc); |
Masami Hiramatsu | 540adea | 2018-01-13 02:55:03 +0900 | [diff] [blame] | 123 | override_function_with_return(regs); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static const struct bpf_func_proto bpf_override_return_proto = { |
| 128 | .func = bpf_override_return, |
| 129 | .gpl_only = true, |
| 130 | .ret_type = RET_INTEGER, |
| 131 | .arg1_type = ARG_PTR_TO_CTX, |
| 132 | .arg2_type = ARG_ANYTHING, |
| 133 | }; |
| 134 | #endif |
| 135 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 136 | BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 137 | { |
Gianluca Borello | eb33f2c | 2017-11-22 18:32:54 +0000 | [diff] [blame] | 138 | int ret; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 139 | |
Daniel Borkmann | 074f528e | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 140 | ret = probe_kernel_read(dst, unsafe_ptr, size); |
| 141 | if (unlikely(ret < 0)) |
| 142 | memset(dst, 0, size); |
| 143 | |
| 144 | return ret; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static const struct bpf_func_proto bpf_probe_read_proto = { |
| 148 | .func = bpf_probe_read, |
| 149 | .gpl_only = true, |
| 150 | .ret_type = RET_INTEGER, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 151 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
Yonghong Song | 9c019e2 | 2017-11-12 14:49:10 -0800 | [diff] [blame] | 152 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 153 | .arg3_type = ARG_ANYTHING, |
| 154 | }; |
| 155 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 156 | BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, |
| 157 | u32, size) |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 158 | { |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Ensure we're in user context which is safe for the helper to |
| 161 | * run. This helper has no business in a kthread. |
| 162 | * |
| 163 | * access_ok() should prevent writing to non-user memory, but in |
| 164 | * some situations (nommu, temporary switch, etc) access_ok() does |
| 165 | * not provide enough validation, hence the check on KERNEL_DS. |
| 166 | */ |
| 167 | |
| 168 | if (unlikely(in_interrupt() || |
| 169 | current->flags & (PF_KTHREAD | PF_EXITING))) |
| 170 | return -EPERM; |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 171 | if (unlikely(uaccess_kernel())) |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 172 | return -EPERM; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 173 | if (!access_ok(unsafe_ptr, size)) |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 174 | return -EPERM; |
| 175 | |
| 176 | return probe_kernel_write(unsafe_ptr, src, size); |
| 177 | } |
| 178 | |
| 179 | static const struct bpf_func_proto bpf_probe_write_user_proto = { |
| 180 | .func = bpf_probe_write_user, |
| 181 | .gpl_only = true, |
| 182 | .ret_type = RET_INTEGER, |
| 183 | .arg1_type = ARG_ANYTHING, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 184 | .arg2_type = ARG_PTR_TO_MEM, |
| 185 | .arg3_type = ARG_CONST_SIZE, |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | static const struct bpf_func_proto *bpf_get_probe_write_proto(void) |
| 189 | { |
| 190 | pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", |
| 191 | current->comm, task_pid_nr(current)); |
| 192 | |
| 193 | return &bpf_probe_write_user_proto; |
| 194 | } |
| 195 | |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 196 | /* |
John Fastabend | 7bda4b4 | 2017-07-02 02:13:29 +0200 | [diff] [blame] | 197 | * Only limited trace_printk() conversion specifiers allowed: |
| 198 | * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 199 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 200 | BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, |
| 201 | u64, arg2, u64, arg3) |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 202 | { |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 203 | bool str_seen = false; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 204 | int mod[3] = {}; |
| 205 | int fmt_cnt = 0; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 206 | u64 unsafe_addr; |
| 207 | char buf[64]; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 208 | int i; |
| 209 | |
| 210 | /* |
| 211 | * bpf_check()->check_func_arg()->check_stack_boundary() |
| 212 | * guarantees that fmt points to bpf program stack, |
| 213 | * fmt_size bytes of it were initialized and fmt_size > 0 |
| 214 | */ |
| 215 | if (fmt[--fmt_size] != 0) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | /* check format string for allowed specifiers */ |
| 219 | for (i = 0; i < fmt_size; i++) { |
| 220 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | if (fmt[i] != '%') |
| 224 | continue; |
| 225 | |
| 226 | if (fmt_cnt >= 3) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ |
| 230 | i++; |
| 231 | if (fmt[i] == 'l') { |
| 232 | mod[fmt_cnt]++; |
| 233 | i++; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 234 | } else if (fmt[i] == 'p' || fmt[i] == 's') { |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 235 | mod[fmt_cnt]++; |
Martynas Pumputis | 1efb6ee | 2018-11-23 17:43:26 +0100 | [diff] [blame] | 236 | /* disallow any further format extensions */ |
| 237 | if (fmt[i + 1] != 0 && |
| 238 | !isspace(fmt[i + 1]) && |
| 239 | !ispunct(fmt[i + 1])) |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 240 | return -EINVAL; |
| 241 | fmt_cnt++; |
Martynas Pumputis | 1efb6ee | 2018-11-23 17:43:26 +0100 | [diff] [blame] | 242 | if (fmt[i] == 's') { |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 243 | if (str_seen) |
| 244 | /* allow only one '%s' per fmt string */ |
| 245 | return -EINVAL; |
| 246 | str_seen = true; |
| 247 | |
| 248 | switch (fmt_cnt) { |
| 249 | case 1: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 250 | unsafe_addr = arg1; |
| 251 | arg1 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 252 | break; |
| 253 | case 2: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 254 | unsafe_addr = arg2; |
| 255 | arg2 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 256 | break; |
| 257 | case 3: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 258 | unsafe_addr = arg3; |
| 259 | arg3 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | buf[0] = 0; |
| 263 | strncpy_from_unsafe(buf, |
| 264 | (void *) (long) unsafe_addr, |
| 265 | sizeof(buf)); |
| 266 | } |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 267 | continue; |
| 268 | } |
| 269 | |
| 270 | if (fmt[i] == 'l') { |
| 271 | mod[fmt_cnt]++; |
| 272 | i++; |
| 273 | } |
| 274 | |
John Fastabend | 7bda4b4 | 2017-07-02 02:13:29 +0200 | [diff] [blame] | 275 | if (fmt[i] != 'i' && fmt[i] != 'd' && |
| 276 | fmt[i] != 'u' && fmt[i] != 'x') |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | fmt_cnt++; |
| 279 | } |
| 280 | |
Daniel Borkmann | 88a5c69 | 2017-08-16 01:45:33 +0200 | [diff] [blame] | 281 | /* Horrid workaround for getting va_list handling working with different |
| 282 | * argument type combinations generically for 32 and 64 bit archs. |
| 283 | */ |
| 284 | #define __BPF_TP_EMIT() __BPF_ARG3_TP() |
| 285 | #define __BPF_TP(...) \ |
Yonghong Song | eefa864a | 2018-01-17 09:19:32 -0800 | [diff] [blame] | 286 | __trace_printk(0 /* Fake ip */, \ |
Daniel Borkmann | 88a5c69 | 2017-08-16 01:45:33 +0200 | [diff] [blame] | 287 | fmt, ##__VA_ARGS__) |
| 288 | |
| 289 | #define __BPF_ARG1_TP(...) \ |
| 290 | ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ |
| 291 | ? __BPF_TP(arg1, ##__VA_ARGS__) \ |
| 292 | : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ |
| 293 | ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ |
| 294 | : __BPF_TP((u32)arg1, ##__VA_ARGS__))) |
| 295 | |
| 296 | #define __BPF_ARG2_TP(...) \ |
| 297 | ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ |
| 298 | ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ |
| 299 | : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ |
| 300 | ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ |
| 301 | : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) |
| 302 | |
| 303 | #define __BPF_ARG3_TP(...) \ |
| 304 | ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ |
| 305 | ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ |
| 306 | : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ |
| 307 | ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ |
| 308 | : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) |
| 309 | |
| 310 | return __BPF_TP_EMIT(); |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static const struct bpf_func_proto bpf_trace_printk_proto = { |
| 314 | .func = bpf_trace_printk, |
| 315 | .gpl_only = true, |
| 316 | .ret_type = RET_INTEGER, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 317 | .arg1_type = ARG_PTR_TO_MEM, |
| 318 | .arg2_type = ARG_CONST_SIZE, |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 321 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void) |
| 322 | { |
| 323 | /* |
| 324 | * this program might be calling bpf_trace_printk, |
| 325 | * so allocate per-cpu printk buffers |
| 326 | */ |
| 327 | trace_printk_init_buffers(); |
| 328 | |
| 329 | return &bpf_trace_printk_proto; |
| 330 | } |
| 331 | |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 332 | static __always_inline int |
| 333 | get_map_perf_counter(struct bpf_map *map, u64 flags, |
| 334 | u64 *value, u64 *enabled, u64 *running) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 335 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 336 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 337 | unsigned int cpu = smp_processor_id(); |
| 338 | u64 index = flags & BPF_F_INDEX_MASK; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 339 | struct bpf_event_entry *ee; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 340 | |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 341 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 342 | return -EINVAL; |
| 343 | if (index == BPF_F_CURRENT_CPU) |
| 344 | index = cpu; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 345 | if (unlikely(index >= array->map.max_entries)) |
| 346 | return -E2BIG; |
| 347 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 348 | ee = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 349 | if (!ee) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 350 | return -ENOENT; |
| 351 | |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 352 | return perf_event_read_local(ee->event, value, enabled, running); |
| 353 | } |
| 354 | |
| 355 | BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) |
| 356 | { |
| 357 | u64 value = 0; |
| 358 | int err; |
| 359 | |
| 360 | err = get_map_perf_counter(map, flags, &value, NULL, NULL); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 361 | /* |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 362 | * this api is ugly since we miss [-22..-2] range of valid |
| 363 | * counter values, but that's uapi |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 364 | */ |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 365 | if (err) |
| 366 | return err; |
| 367 | return value; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 370 | static const struct bpf_func_proto bpf_perf_event_read_proto = { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 371 | .func = bpf_perf_event_read, |
Alexei Starovoitov | 1075ef5 | 2015-10-23 14:58:19 -0700 | [diff] [blame] | 372 | .gpl_only = true, |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 373 | .ret_type = RET_INTEGER, |
| 374 | .arg1_type = ARG_CONST_MAP_PTR, |
| 375 | .arg2_type = ARG_ANYTHING, |
| 376 | }; |
| 377 | |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 378 | BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, |
| 379 | struct bpf_perf_event_value *, buf, u32, size) |
| 380 | { |
| 381 | int err = -EINVAL; |
| 382 | |
| 383 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) |
| 384 | goto clear; |
| 385 | err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, |
| 386 | &buf->running); |
| 387 | if (unlikely(err)) |
| 388 | goto clear; |
| 389 | return 0; |
| 390 | clear: |
| 391 | memset(buf, 0, size); |
| 392 | return err; |
| 393 | } |
| 394 | |
| 395 | static const struct bpf_func_proto bpf_perf_event_read_value_proto = { |
| 396 | .func = bpf_perf_event_read_value, |
| 397 | .gpl_only = true, |
| 398 | .ret_type = RET_INTEGER, |
| 399 | .arg1_type = ARG_CONST_MAP_PTR, |
| 400 | .arg2_type = ARG_ANYTHING, |
| 401 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, |
| 402 | .arg4_type = ARG_CONST_SIZE, |
| 403 | }; |
| 404 | |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 405 | static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd); |
Daniel Borkmann | 20b9d7a | 2017-06-11 00:50:40 +0200 | [diff] [blame] | 406 | |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 407 | static __always_inline u64 |
| 408 | __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 409 | u64 flags, struct perf_sample_data *sd) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 410 | { |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 411 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 412 | unsigned int cpu = smp_processor_id(); |
Daniel Borkmann | 1e33759 | 2016-04-18 21:01:23 +0200 | [diff] [blame] | 413 | u64 index = flags & BPF_F_INDEX_MASK; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 414 | struct bpf_event_entry *ee; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 415 | struct perf_event *event; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 416 | |
Daniel Borkmann | 1e33759 | 2016-04-18 21:01:23 +0200 | [diff] [blame] | 417 | if (index == BPF_F_CURRENT_CPU) |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 418 | index = cpu; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 419 | if (unlikely(index >= array->map.max_entries)) |
| 420 | return -E2BIG; |
| 421 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 422 | ee = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 423 | if (!ee) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 424 | return -ENOENT; |
| 425 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 426 | event = ee->event; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 427 | if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || |
| 428 | event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) |
| 429 | return -EINVAL; |
| 430 | |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 431 | if (unlikely(event->oncpu != cpu)) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 432 | return -EOPNOTSUPP; |
| 433 | |
Arnaldo Carvalho de Melo | 5620196 | 2019-01-11 13:20:20 -0300 | [diff] [blame] | 434 | return perf_event_output(event, sd, regs); |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 437 | BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, |
| 438 | u64, flags, void *, data, u64, size) |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 439 | { |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 440 | struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd); |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 441 | struct perf_raw_record raw = { |
| 442 | .frag = { |
| 443 | .size = size, |
| 444 | .data = data, |
| 445 | }, |
| 446 | }; |
| 447 | |
| 448 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 449 | return -EINVAL; |
| 450 | |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 451 | perf_sample_data_init(sd, 0, 0); |
| 452 | sd->raw = &raw; |
| 453 | |
| 454 | return __bpf_perf_event_output(regs, map, flags, sd); |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 457 | static const struct bpf_func_proto bpf_perf_event_output_proto = { |
| 458 | .func = bpf_perf_event_output, |
Alexei Starovoitov | 1075ef5 | 2015-10-23 14:58:19 -0700 | [diff] [blame] | 459 | .gpl_only = true, |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 460 | .ret_type = RET_INTEGER, |
| 461 | .arg1_type = ARG_PTR_TO_CTX, |
| 462 | .arg2_type = ARG_CONST_MAP_PTR, |
| 463 | .arg3_type = ARG_ANYTHING, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 464 | .arg4_type = ARG_PTR_TO_MEM, |
Gianluca Borello | a60dd35 | 2017-11-22 18:32:56 +0000 | [diff] [blame] | 465 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 466 | }; |
| 467 | |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 468 | static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs); |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 469 | static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 470 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 471 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 472 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 473 | { |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 474 | struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 475 | struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs); |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 476 | struct perf_raw_frag frag = { |
| 477 | .copy = ctx_copy, |
| 478 | .size = ctx_size, |
| 479 | .data = ctx, |
| 480 | }; |
| 481 | struct perf_raw_record raw = { |
| 482 | .frag = { |
Andrew Morton | 183fc15 | 2016-07-18 15:50:58 -0700 | [diff] [blame] | 483 | { |
| 484 | .next = ctx_size ? &frag : NULL, |
| 485 | }, |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 486 | .size = meta_size, |
| 487 | .data = meta, |
| 488 | }, |
| 489 | }; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 490 | |
| 491 | perf_fetch_caller_regs(regs); |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 492 | perf_sample_data_init(sd, 0, 0); |
| 493 | sd->raw = &raw; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 494 | |
Daniel Borkmann | 283ca52 | 2017-12-12 02:25:30 +0100 | [diff] [blame] | 495 | return __bpf_perf_event_output(regs, map, flags, sd); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 498 | BPF_CALL_0(bpf_get_current_task) |
Alexei Starovoitov | 606274c | 2016-07-06 22:38:36 -0700 | [diff] [blame] | 499 | { |
| 500 | return (long) current; |
| 501 | } |
| 502 | |
| 503 | static const struct bpf_func_proto bpf_get_current_task_proto = { |
| 504 | .func = bpf_get_current_task, |
| 505 | .gpl_only = true, |
| 506 | .ret_type = RET_INTEGER, |
| 507 | }; |
| 508 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 509 | BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 510 | { |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 511 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 512 | struct cgroup *cgrp; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 513 | |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 514 | if (unlikely(idx >= array->map.max_entries)) |
| 515 | return -E2BIG; |
| 516 | |
| 517 | cgrp = READ_ONCE(array->ptrs[idx]); |
| 518 | if (unlikely(!cgrp)) |
| 519 | return -EAGAIN; |
| 520 | |
| 521 | return task_under_cgroup_hierarchy(current, cgrp); |
| 522 | } |
| 523 | |
| 524 | static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { |
| 525 | .func = bpf_current_task_under_cgroup, |
| 526 | .gpl_only = false, |
| 527 | .ret_type = RET_INTEGER, |
| 528 | .arg1_type = ARG_CONST_MAP_PTR, |
| 529 | .arg2_type = ARG_ANYTHING, |
| 530 | }; |
| 531 | |
Gianluca Borello | a5e8c07 | 2017-01-18 17:55:49 +0000 | [diff] [blame] | 532 | BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, |
| 533 | const void *, unsafe_ptr) |
| 534 | { |
| 535 | int ret; |
| 536 | |
| 537 | /* |
| 538 | * The strncpy_from_unsafe() call will likely not fill the entire |
| 539 | * buffer, but that's okay in this circumstance as we're probing |
| 540 | * arbitrary memory anyway similar to bpf_probe_read() and might |
| 541 | * as well probe the stack. Thus, memory is explicitly cleared |
| 542 | * only in error case, so that improper users ignoring return |
| 543 | * code altogether don't copy garbage; otherwise length of string |
| 544 | * is returned that can be used for bpf_perf_event_output() et al. |
| 545 | */ |
| 546 | ret = strncpy_from_unsafe(dst, unsafe_ptr, size); |
| 547 | if (unlikely(ret < 0)) |
| 548 | memset(dst, 0, size); |
| 549 | |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | static const struct bpf_func_proto bpf_probe_read_str_proto = { |
| 554 | .func = bpf_probe_read_str, |
| 555 | .gpl_only = true, |
| 556 | .ret_type = RET_INTEGER, |
| 557 | .arg1_type = ARG_PTR_TO_UNINIT_MEM, |
Gianluca Borello | 5c4e120 | 2017-11-22 18:32:55 +0000 | [diff] [blame] | 558 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
Gianluca Borello | a5e8c07 | 2017-01-18 17:55:49 +0000 | [diff] [blame] | 559 | .arg3_type = ARG_ANYTHING, |
| 560 | }; |
| 561 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 562 | static const struct bpf_func_proto * |
| 563 | tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 564 | { |
| 565 | switch (func_id) { |
| 566 | case BPF_FUNC_map_lookup_elem: |
| 567 | return &bpf_map_lookup_elem_proto; |
| 568 | case BPF_FUNC_map_update_elem: |
| 569 | return &bpf_map_update_elem_proto; |
| 570 | case BPF_FUNC_map_delete_elem: |
| 571 | return &bpf_map_delete_elem_proto; |
Alban Crequy | 02a8c81 | 2019-04-14 18:58:46 +0200 | [diff] [blame^] | 572 | case BPF_FUNC_map_push_elem: |
| 573 | return &bpf_map_push_elem_proto; |
| 574 | case BPF_FUNC_map_pop_elem: |
| 575 | return &bpf_map_pop_elem_proto; |
| 576 | case BPF_FUNC_map_peek_elem: |
| 577 | return &bpf_map_peek_elem_proto; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 578 | case BPF_FUNC_probe_read: |
| 579 | return &bpf_probe_read_proto; |
Alexei Starovoitov | d9847d3 | 2015-03-25 12:49:21 -0700 | [diff] [blame] | 580 | case BPF_FUNC_ktime_get_ns: |
| 581 | return &bpf_ktime_get_ns_proto; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 582 | case BPF_FUNC_tail_call: |
| 583 | return &bpf_tail_call_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 584 | case BPF_FUNC_get_current_pid_tgid: |
| 585 | return &bpf_get_current_pid_tgid_proto; |
Alexei Starovoitov | 606274c | 2016-07-06 22:38:36 -0700 | [diff] [blame] | 586 | case BPF_FUNC_get_current_task: |
| 587 | return &bpf_get_current_task_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 588 | case BPF_FUNC_get_current_uid_gid: |
| 589 | return &bpf_get_current_uid_gid_proto; |
| 590 | case BPF_FUNC_get_current_comm: |
| 591 | return &bpf_get_current_comm_proto; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 592 | case BPF_FUNC_trace_printk: |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 593 | return bpf_get_trace_printk_proto(); |
Alexei Starovoitov | ab1973d | 2015-06-12 19:39:14 -0700 | [diff] [blame] | 594 | case BPF_FUNC_get_smp_processor_id: |
| 595 | return &bpf_get_smp_processor_id_proto; |
Daniel Borkmann | 2d0e30c | 2016-10-21 12:46:33 +0200 | [diff] [blame] | 596 | case BPF_FUNC_get_numa_node_id: |
| 597 | return &bpf_get_numa_node_id_proto; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 598 | case BPF_FUNC_perf_event_read: |
| 599 | return &bpf_perf_event_read_proto; |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 600 | case BPF_FUNC_probe_write_user: |
| 601 | return bpf_get_probe_write_proto(); |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 602 | case BPF_FUNC_current_task_under_cgroup: |
| 603 | return &bpf_current_task_under_cgroup_proto; |
Alexei Starovoitov | 8937bd8 | 2016-08-11 18:17:18 -0700 | [diff] [blame] | 604 | case BPF_FUNC_get_prandom_u32: |
| 605 | return &bpf_get_prandom_u32_proto; |
Gianluca Borello | a5e8c07 | 2017-01-18 17:55:49 +0000 | [diff] [blame] | 606 | case BPF_FUNC_probe_read_str: |
| 607 | return &bpf_probe_read_str_proto; |
Yonghong Song | 34ea38c | 2018-06-04 08:53:41 -0700 | [diff] [blame] | 608 | #ifdef CONFIG_CGROUPS |
Yonghong Song | bf6fa2c8 | 2018-06-03 15:59:41 -0700 | [diff] [blame] | 609 | case BPF_FUNC_get_current_cgroup_id: |
| 610 | return &bpf_get_current_cgroup_id_proto; |
Yonghong Song | 34ea38c | 2018-06-04 08:53:41 -0700 | [diff] [blame] | 611 | #endif |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 612 | default: |
| 613 | return NULL; |
| 614 | } |
| 615 | } |
| 616 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 617 | static const struct bpf_func_proto * |
| 618 | kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 619 | { |
| 620 | switch (func_id) { |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 621 | case BPF_FUNC_perf_event_output: |
| 622 | return &bpf_perf_event_output_proto; |
Alexei Starovoitov | d5a3b1f | 2016-02-17 19:58:58 -0800 | [diff] [blame] | 623 | case BPF_FUNC_get_stackid: |
| 624 | return &bpf_get_stackid_proto; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 625 | case BPF_FUNC_get_stack: |
| 626 | return &bpf_get_stack_proto; |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 627 | case BPF_FUNC_perf_event_read_value: |
| 628 | return &bpf_perf_event_read_value_proto; |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 629 | #ifdef CONFIG_BPF_KPROBE_OVERRIDE |
| 630 | case BPF_FUNC_override_return: |
| 631 | return &bpf_override_return_proto; |
| 632 | #endif |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 633 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 634 | return tracing_func_proto(func_id, prog); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /* bpf+kprobe programs can access fields of 'struct pt_regs' */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 639 | static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 640 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 641 | struct bpf_insn_access_aux *info) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 642 | { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 643 | if (off < 0 || off >= sizeof(struct pt_regs)) |
| 644 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 645 | if (type != BPF_READ) |
| 646 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 647 | if (off % size != 0) |
| 648 | return false; |
Daniel Borkmann | 2d071c6 | 2017-01-15 01:34:25 +0100 | [diff] [blame] | 649 | /* |
| 650 | * Assertion for 32 bit to make sure last 8 byte access |
| 651 | * (BPF_DW) to the last 4 byte member is disallowed. |
| 652 | */ |
| 653 | if (off + size > sizeof(struct pt_regs)) |
| 654 | return false; |
| 655 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 659 | const struct bpf_verifier_ops kprobe_verifier_ops = { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 660 | .get_func_proto = kprobe_prog_func_proto, |
| 661 | .is_valid_access = kprobe_prog_is_valid_access, |
| 662 | }; |
| 663 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 664 | const struct bpf_prog_ops kprobe_prog_ops = { |
| 665 | }; |
| 666 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 667 | BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, |
| 668 | u64, flags, void *, data, u64, size) |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 669 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 670 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 671 | |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 672 | /* |
| 673 | * r1 points to perf tracepoint buffer where first 8 bytes are hidden |
| 674 | * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 675 | * from there and call the same bpf_perf_event_output() helper inline. |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 676 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 677 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { |
| 681 | .func = bpf_perf_event_output_tp, |
| 682 | .gpl_only = true, |
| 683 | .ret_type = RET_INTEGER, |
| 684 | .arg1_type = ARG_PTR_TO_CTX, |
| 685 | .arg2_type = ARG_CONST_MAP_PTR, |
| 686 | .arg3_type = ARG_ANYTHING, |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 687 | .arg4_type = ARG_PTR_TO_MEM, |
Gianluca Borello | a60dd35 | 2017-11-22 18:32:56 +0000 | [diff] [blame] | 688 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 691 | BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, |
| 692 | u64, flags) |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 693 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 694 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 695 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 696 | /* |
| 697 | * Same comment as in bpf_perf_event_output_tp(), only that this time |
| 698 | * the other helper's function body cannot be inlined due to being |
| 699 | * external, thus we need to call raw helper function. |
| 700 | */ |
| 701 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 702 | flags, 0, 0); |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static const struct bpf_func_proto bpf_get_stackid_proto_tp = { |
| 706 | .func = bpf_get_stackid_tp, |
| 707 | .gpl_only = true, |
| 708 | .ret_type = RET_INTEGER, |
| 709 | .arg1_type = ARG_PTR_TO_CTX, |
| 710 | .arg2_type = ARG_CONST_MAP_PTR, |
| 711 | .arg3_type = ARG_ANYTHING, |
| 712 | }; |
| 713 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 714 | BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, |
| 715 | u64, flags) |
| 716 | { |
| 717 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 718 | |
| 719 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
| 720 | (unsigned long) size, flags, 0); |
| 721 | } |
| 722 | |
| 723 | static const struct bpf_func_proto bpf_get_stack_proto_tp = { |
| 724 | .func = bpf_get_stack_tp, |
| 725 | .gpl_only = true, |
| 726 | .ret_type = RET_INTEGER, |
| 727 | .arg1_type = ARG_PTR_TO_CTX, |
| 728 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 729 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 730 | .arg4_type = ARG_ANYTHING, |
| 731 | }; |
| 732 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 733 | static const struct bpf_func_proto * |
| 734 | tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 735 | { |
| 736 | switch (func_id) { |
| 737 | case BPF_FUNC_perf_event_output: |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 738 | return &bpf_perf_event_output_proto_tp; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 739 | case BPF_FUNC_get_stackid: |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 740 | return &bpf_get_stackid_proto_tp; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 741 | case BPF_FUNC_get_stack: |
| 742 | return &bpf_get_stack_proto_tp; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 743 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 744 | return tracing_func_proto(func_id, prog); |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 748 | static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 749 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 750 | struct bpf_insn_access_aux *info) |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 751 | { |
| 752 | if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) |
| 753 | return false; |
| 754 | if (type != BPF_READ) |
| 755 | return false; |
| 756 | if (off % size != 0) |
| 757 | return false; |
Daniel Borkmann | 2d071c6 | 2017-01-15 01:34:25 +0100 | [diff] [blame] | 758 | |
| 759 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 760 | return true; |
| 761 | } |
| 762 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 763 | const struct bpf_verifier_ops tracepoint_verifier_ops = { |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 764 | .get_func_proto = tp_prog_func_proto, |
| 765 | .is_valid_access = tp_prog_is_valid_access, |
| 766 | }; |
| 767 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 768 | const struct bpf_prog_ops tracepoint_prog_ops = { |
| 769 | }; |
| 770 | |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 771 | BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, |
| 772 | struct bpf_perf_event_value *, buf, u32, size) |
| 773 | { |
| 774 | int err = -EINVAL; |
| 775 | |
| 776 | if (unlikely(size != sizeof(struct bpf_perf_event_value))) |
| 777 | goto clear; |
| 778 | err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, |
| 779 | &buf->running); |
| 780 | if (unlikely(err)) |
| 781 | goto clear; |
| 782 | return 0; |
| 783 | clear: |
| 784 | memset(buf, 0, size); |
| 785 | return err; |
| 786 | } |
| 787 | |
| 788 | static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { |
| 789 | .func = bpf_perf_prog_read_value, |
| 790 | .gpl_only = true, |
| 791 | .ret_type = RET_INTEGER, |
| 792 | .arg1_type = ARG_PTR_TO_CTX, |
| 793 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
| 794 | .arg3_type = ARG_CONST_SIZE, |
| 795 | }; |
| 796 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 797 | static const struct bpf_func_proto * |
| 798 | pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 799 | { |
| 800 | switch (func_id) { |
| 801 | case BPF_FUNC_perf_event_output: |
| 802 | return &bpf_perf_event_output_proto_tp; |
| 803 | case BPF_FUNC_get_stackid: |
| 804 | return &bpf_get_stackid_proto_tp; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 805 | case BPF_FUNC_get_stack: |
| 806 | return &bpf_get_stack_proto_tp; |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 807 | case BPF_FUNC_perf_prog_read_value: |
| 808 | return &bpf_perf_prog_read_value_proto; |
| 809 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 810 | return tracing_func_proto(func_id, prog); |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 814 | /* |
| 815 | * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp |
| 816 | * to avoid potential recursive reuse issue when/if tracepoints are added |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 817 | * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 818 | */ |
| 819 | static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs); |
| 820 | BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 821 | struct bpf_map *, map, u64, flags, void *, data, u64, size) |
| 822 | { |
| 823 | struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 824 | |
| 825 | perf_fetch_caller_regs(regs); |
| 826 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
| 827 | } |
| 828 | |
| 829 | static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { |
| 830 | .func = bpf_perf_event_output_raw_tp, |
| 831 | .gpl_only = true, |
| 832 | .ret_type = RET_INTEGER, |
| 833 | .arg1_type = ARG_PTR_TO_CTX, |
| 834 | .arg2_type = ARG_CONST_MAP_PTR, |
| 835 | .arg3_type = ARG_ANYTHING, |
| 836 | .arg4_type = ARG_PTR_TO_MEM, |
| 837 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
| 838 | }; |
| 839 | |
| 840 | BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 841 | struct bpf_map *, map, u64, flags) |
| 842 | { |
| 843 | struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 844 | |
| 845 | perf_fetch_caller_regs(regs); |
| 846 | /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ |
| 847 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 848 | flags, 0, 0); |
| 849 | } |
| 850 | |
| 851 | static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { |
| 852 | .func = bpf_get_stackid_raw_tp, |
| 853 | .gpl_only = true, |
| 854 | .ret_type = RET_INTEGER, |
| 855 | .arg1_type = ARG_PTR_TO_CTX, |
| 856 | .arg2_type = ARG_CONST_MAP_PTR, |
| 857 | .arg3_type = ARG_ANYTHING, |
| 858 | }; |
| 859 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 860 | BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, |
| 861 | void *, buf, u32, size, u64, flags) |
| 862 | { |
| 863 | struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); |
| 864 | |
| 865 | perf_fetch_caller_regs(regs); |
| 866 | return bpf_get_stack((unsigned long) regs, (unsigned long) buf, |
| 867 | (unsigned long) size, flags, 0); |
| 868 | } |
| 869 | |
| 870 | static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { |
| 871 | .func = bpf_get_stack_raw_tp, |
| 872 | .gpl_only = true, |
| 873 | .ret_type = RET_INTEGER, |
| 874 | .arg1_type = ARG_PTR_TO_CTX, |
| 875 | .arg2_type = ARG_PTR_TO_MEM, |
| 876 | .arg3_type = ARG_CONST_SIZE_OR_ZERO, |
| 877 | .arg4_type = ARG_ANYTHING, |
| 878 | }; |
| 879 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 880 | static const struct bpf_func_proto * |
| 881 | raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 882 | { |
| 883 | switch (func_id) { |
| 884 | case BPF_FUNC_perf_event_output: |
| 885 | return &bpf_perf_event_output_proto_raw_tp; |
| 886 | case BPF_FUNC_get_stackid: |
| 887 | return &bpf_get_stackid_proto_raw_tp; |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 888 | case BPF_FUNC_get_stack: |
| 889 | return &bpf_get_stack_proto_raw_tp; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 890 | default: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 891 | return tracing_func_proto(func_id, prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | static bool raw_tp_prog_is_valid_access(int off, int size, |
| 896 | enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 897 | const struct bpf_prog *prog, |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 898 | struct bpf_insn_access_aux *info) |
| 899 | { |
| 900 | /* largest tracepoint in the kernel has 12 args */ |
| 901 | if (off < 0 || off >= sizeof(__u64) * 12) |
| 902 | return false; |
| 903 | if (type != BPF_READ) |
| 904 | return false; |
| 905 | if (off % size != 0) |
| 906 | return false; |
| 907 | return true; |
| 908 | } |
| 909 | |
| 910 | const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { |
| 911 | .get_func_proto = raw_tp_prog_func_proto, |
| 912 | .is_valid_access = raw_tp_prog_is_valid_access, |
| 913 | }; |
| 914 | |
| 915 | const struct bpf_prog_ops raw_tracepoint_prog_ops = { |
| 916 | }; |
| 917 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 918 | static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 919 | const struct bpf_prog *prog, |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 920 | struct bpf_insn_access_aux *info) |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 921 | { |
Teng Qin | 95da0cd | 2018-03-06 10:55:01 -0800 | [diff] [blame] | 922 | const int size_u64 = sizeof(u64); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 923 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 924 | if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) |
| 925 | return false; |
| 926 | if (type != BPF_READ) |
| 927 | return false; |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 928 | if (off % size != 0) { |
| 929 | if (sizeof(unsigned long) != 4) |
| 930 | return false; |
| 931 | if (size != 8) |
| 932 | return false; |
| 933 | if (off % size != 4) |
| 934 | return false; |
| 935 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 936 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 937 | switch (off) { |
| 938 | case bpf_ctx_range(struct bpf_perf_event_data, sample_period): |
Teng Qin | 95da0cd | 2018-03-06 10:55:01 -0800 | [diff] [blame] | 939 | bpf_ctx_record_field_size(info, size_u64); |
| 940 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) |
| 941 | return false; |
| 942 | break; |
| 943 | case bpf_ctx_range(struct bpf_perf_event_data, addr): |
| 944 | bpf_ctx_record_field_size(info, size_u64); |
| 945 | if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 946 | return false; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 947 | break; |
| 948 | default: |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 949 | if (size != sizeof(long)) |
| 950 | return false; |
| 951 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 952 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 953 | return true; |
| 954 | } |
| 955 | |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 956 | static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, |
| 957 | const struct bpf_insn *si, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 958 | struct bpf_insn *insn_buf, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 959 | struct bpf_prog *prog, u32 *target_size) |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 960 | { |
| 961 | struct bpf_insn *insn = insn_buf; |
| 962 | |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 963 | switch (si->off) { |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 964 | case offsetof(struct bpf_perf_event_data, sample_period): |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 965 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 966 | data), si->dst_reg, si->src_reg, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 967 | offsetof(struct bpf_perf_event_data_kern, data)); |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 968 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 969 | bpf_target_off(struct perf_sample_data, period, 8, |
| 970 | target_size)); |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 971 | break; |
Teng Qin | 95da0cd | 2018-03-06 10:55:01 -0800 | [diff] [blame] | 972 | case offsetof(struct bpf_perf_event_data, addr): |
| 973 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 974 | data), si->dst_reg, si->src_reg, |
| 975 | offsetof(struct bpf_perf_event_data_kern, data)); |
| 976 | *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, |
| 977 | bpf_target_off(struct perf_sample_data, addr, 8, |
| 978 | target_size)); |
| 979 | break; |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 980 | default: |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 981 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 982 | regs), si->dst_reg, si->src_reg, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 983 | offsetof(struct bpf_perf_event_data_kern, regs)); |
Daniel Borkmann | 6b8cc1d | 2017-01-12 11:51:32 +0100 | [diff] [blame] | 984 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, |
| 985 | si->off); |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 986 | break; |
| 987 | } |
| 988 | |
| 989 | return insn - insn_buf; |
| 990 | } |
| 991 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 992 | const struct bpf_verifier_ops perf_event_verifier_ops = { |
Yonghong Song | f005afe | 2018-03-20 11:19:17 -0700 | [diff] [blame] | 993 | .get_func_proto = pe_prog_func_proto, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 994 | .is_valid_access = pe_prog_is_valid_access, |
| 995 | .convert_ctx_access = pe_prog_convert_ctx_access, |
| 996 | }; |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 997 | |
| 998 | const struct bpf_prog_ops perf_event_prog_ops = { |
| 999 | }; |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1000 | |
| 1001 | static DEFINE_MUTEX(bpf_event_mutex); |
| 1002 | |
Yonghong Song | c8c088b | 2017-11-30 13:47:54 -0800 | [diff] [blame] | 1003 | #define BPF_TRACE_MAX_PROGS 64 |
| 1004 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1005 | int perf_event_attach_bpf_prog(struct perf_event *event, |
| 1006 | struct bpf_prog *prog) |
| 1007 | { |
| 1008 | struct bpf_prog_array __rcu *old_array; |
| 1009 | struct bpf_prog_array *new_array; |
| 1010 | int ret = -EEXIST; |
| 1011 | |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1012 | /* |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 1013 | * Kprobe override only works if they are on the function entry, |
| 1014 | * and only if they are on the opt-in list. |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1015 | */ |
| 1016 | if (prog->kprobe_override && |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 1017 | (!trace_kprobe_on_func_entry(event->tp_event) || |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 1018 | !trace_kprobe_error_injectable(event->tp_event))) |
| 1019 | return -EINVAL; |
| 1020 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1021 | mutex_lock(&bpf_event_mutex); |
| 1022 | |
| 1023 | if (event->prog) |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1024 | goto unlock; |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1025 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1026 | old_array = event->tp_event->prog_array; |
Yonghong Song | c8c088b | 2017-11-30 13:47:54 -0800 | [diff] [blame] | 1027 | if (old_array && |
| 1028 | bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { |
| 1029 | ret = -E2BIG; |
| 1030 | goto unlock; |
| 1031 | } |
| 1032 | |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1033 | ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); |
| 1034 | if (ret < 0) |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1035 | goto unlock; |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1036 | |
| 1037 | /* set the new array to event->tp_event and set event->prog */ |
| 1038 | event->prog = prog; |
| 1039 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 1040 | bpf_prog_array_free(old_array); |
| 1041 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1042 | unlock: |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1043 | mutex_unlock(&bpf_event_mutex); |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | void perf_event_detach_bpf_prog(struct perf_event *event) |
| 1048 | { |
| 1049 | struct bpf_prog_array __rcu *old_array; |
| 1050 | struct bpf_prog_array *new_array; |
| 1051 | int ret; |
| 1052 | |
| 1053 | mutex_lock(&bpf_event_mutex); |
| 1054 | |
| 1055 | if (!event->prog) |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1056 | goto unlock; |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1057 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1058 | old_array = event->tp_event->prog_array; |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1059 | ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); |
Sean Young | 170a7e3 | 2018-05-27 12:24:08 +0100 | [diff] [blame] | 1060 | if (ret == -ENOENT) |
| 1061 | goto unlock; |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1062 | if (ret < 0) { |
| 1063 | bpf_prog_array_delete_safe(old_array, event->prog); |
| 1064 | } else { |
| 1065 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 1066 | bpf_prog_array_free(old_array); |
| 1067 | } |
| 1068 | |
| 1069 | bpf_prog_put(event->prog); |
| 1070 | event->prog = NULL; |
| 1071 | |
Yonghong Song | 07c41a2 | 2017-10-30 13:50:22 -0700 | [diff] [blame] | 1072 | unlock: |
Yonghong Song | e87c6bc | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 1073 | mutex_unlock(&bpf_event_mutex); |
| 1074 | } |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1075 | |
Yonghong Song | f4e2298 | 2017-12-13 10:35:37 -0800 | [diff] [blame] | 1076 | int perf_event_query_prog_array(struct perf_event *event, void __user *info) |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1077 | { |
| 1078 | struct perf_event_query_bpf __user *uquery = info; |
| 1079 | struct perf_event_query_bpf query = {}; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1080 | u32 *ids, prog_cnt, ids_len; |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1081 | int ret; |
| 1082 | |
| 1083 | if (!capable(CAP_SYS_ADMIN)) |
| 1084 | return -EPERM; |
| 1085 | if (event->attr.type != PERF_TYPE_TRACEPOINT) |
| 1086 | return -EINVAL; |
| 1087 | if (copy_from_user(&query, uquery, sizeof(query))) |
| 1088 | return -EFAULT; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1089 | |
| 1090 | ids_len = query.ids_len; |
| 1091 | if (ids_len > BPF_TRACE_MAX_PROGS) |
Daniel Borkmann | 9c481b9 | 2018-02-14 15:31:00 +0100 | [diff] [blame] | 1092 | return -E2BIG; |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1093 | ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); |
| 1094 | if (!ids) |
| 1095 | return -ENOMEM; |
| 1096 | /* |
| 1097 | * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which |
| 1098 | * is required when user only wants to check for uquery->prog_cnt. |
| 1099 | * There is no need to check for it since the case is handled |
| 1100 | * gracefully in bpf_prog_array_copy_info. |
| 1101 | */ |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1102 | |
| 1103 | mutex_lock(&bpf_event_mutex); |
| 1104 | ret = bpf_prog_array_copy_info(event->tp_event->prog_array, |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1105 | ids, |
| 1106 | ids_len, |
| 1107 | &prog_cnt); |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1108 | mutex_unlock(&bpf_event_mutex); |
| 1109 | |
Yonghong Song | 3a38bb9 | 2018-04-10 09:37:32 -0700 | [diff] [blame] | 1110 | if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || |
| 1111 | copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) |
| 1112 | ret = -EFAULT; |
| 1113 | |
| 1114 | kfree(ids); |
Yonghong Song | f371b30 | 2017-12-11 11:39:02 -0800 | [diff] [blame] | 1115 | return ret; |
| 1116 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1117 | |
| 1118 | extern struct bpf_raw_event_map __start__bpf_raw_tp[]; |
| 1119 | extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; |
| 1120 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1121 | struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1122 | { |
| 1123 | struct bpf_raw_event_map *btp = __start__bpf_raw_tp; |
| 1124 | |
| 1125 | for (; btp < __stop__bpf_raw_tp; btp++) { |
| 1126 | if (!strcmp(btp->tp->name, name)) |
| 1127 | return btp; |
| 1128 | } |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1129 | |
| 1130 | return bpf_get_raw_tracepoint_module(name); |
| 1131 | } |
| 1132 | |
| 1133 | void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) |
| 1134 | { |
| 1135 | struct module *mod = __module_address((unsigned long)btp); |
| 1136 | |
| 1137 | if (mod) |
| 1138 | module_put(mod); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static __always_inline |
| 1142 | void __bpf_trace_run(struct bpf_prog *prog, u64 *args) |
| 1143 | { |
| 1144 | rcu_read_lock(); |
| 1145 | preempt_disable(); |
| 1146 | (void) BPF_PROG_RUN(prog, args); |
| 1147 | preempt_enable(); |
| 1148 | rcu_read_unlock(); |
| 1149 | } |
| 1150 | |
| 1151 | #define UNPACK(...) __VA_ARGS__ |
| 1152 | #define REPEAT_1(FN, DL, X, ...) FN(X) |
| 1153 | #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) |
| 1154 | #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) |
| 1155 | #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) |
| 1156 | #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) |
| 1157 | #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) |
| 1158 | #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) |
| 1159 | #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) |
| 1160 | #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) |
| 1161 | #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) |
| 1162 | #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) |
| 1163 | #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) |
| 1164 | #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) |
| 1165 | |
| 1166 | #define SARG(X) u64 arg##X |
| 1167 | #define COPY(X) args[X] = arg##X |
| 1168 | |
| 1169 | #define __DL_COM (,) |
| 1170 | #define __DL_SEM (;) |
| 1171 | |
| 1172 | #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 |
| 1173 | |
| 1174 | #define BPF_TRACE_DEFN_x(x) \ |
| 1175 | void bpf_trace_run##x(struct bpf_prog *prog, \ |
| 1176 | REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ |
| 1177 | { \ |
| 1178 | u64 args[x]; \ |
| 1179 | REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ |
| 1180 | __bpf_trace_run(prog, args); \ |
| 1181 | } \ |
| 1182 | EXPORT_SYMBOL_GPL(bpf_trace_run##x) |
| 1183 | BPF_TRACE_DEFN_x(1); |
| 1184 | BPF_TRACE_DEFN_x(2); |
| 1185 | BPF_TRACE_DEFN_x(3); |
| 1186 | BPF_TRACE_DEFN_x(4); |
| 1187 | BPF_TRACE_DEFN_x(5); |
| 1188 | BPF_TRACE_DEFN_x(6); |
| 1189 | BPF_TRACE_DEFN_x(7); |
| 1190 | BPF_TRACE_DEFN_x(8); |
| 1191 | BPF_TRACE_DEFN_x(9); |
| 1192 | BPF_TRACE_DEFN_x(10); |
| 1193 | BPF_TRACE_DEFN_x(11); |
| 1194 | BPF_TRACE_DEFN_x(12); |
| 1195 | |
| 1196 | static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 1197 | { |
| 1198 | struct tracepoint *tp = btp->tp; |
| 1199 | |
| 1200 | /* |
| 1201 | * check that program doesn't access arguments beyond what's |
| 1202 | * available in this tracepoint |
| 1203 | */ |
| 1204 | if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) |
| 1205 | return -EINVAL; |
| 1206 | |
| 1207 | return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog); |
| 1208 | } |
| 1209 | |
| 1210 | int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 1211 | { |
Alexei Starovoitov | e16ec34 | 2019-01-30 18:12:44 -0800 | [diff] [blame] | 1212 | return __bpf_probe_register(btp, prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) |
| 1216 | { |
Alexei Starovoitov | e16ec34 | 2019-01-30 18:12:44 -0800 | [diff] [blame] | 1217 | return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1218 | } |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 1219 | |
| 1220 | int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, |
| 1221 | u32 *fd_type, const char **buf, |
| 1222 | u64 *probe_offset, u64 *probe_addr) |
| 1223 | { |
| 1224 | bool is_tracepoint, is_syscall_tp; |
| 1225 | struct bpf_prog *prog; |
| 1226 | int flags, err = 0; |
| 1227 | |
| 1228 | prog = event->prog; |
| 1229 | if (!prog) |
| 1230 | return -ENOENT; |
| 1231 | |
| 1232 | /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ |
| 1233 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) |
| 1234 | return -EOPNOTSUPP; |
| 1235 | |
| 1236 | *prog_id = prog->aux->id; |
| 1237 | flags = event->tp_event->flags; |
| 1238 | is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; |
| 1239 | is_syscall_tp = is_syscall_trace_event(event->tp_event); |
| 1240 | |
| 1241 | if (is_tracepoint || is_syscall_tp) { |
| 1242 | *buf = is_tracepoint ? event->tp_event->tp->name |
| 1243 | : event->tp_event->name; |
| 1244 | *fd_type = BPF_FD_TYPE_TRACEPOINT; |
| 1245 | *probe_offset = 0x0; |
| 1246 | *probe_addr = 0x0; |
| 1247 | } else { |
| 1248 | /* kprobe/uprobe */ |
| 1249 | err = -EOPNOTSUPP; |
| 1250 | #ifdef CONFIG_KPROBE_EVENTS |
| 1251 | if (flags & TRACE_EVENT_FL_KPROBE) |
| 1252 | err = bpf_get_kprobe_info(event, fd_type, buf, |
| 1253 | probe_offset, probe_addr, |
| 1254 | event->attr.type == PERF_TYPE_TRACEPOINT); |
| 1255 | #endif |
| 1256 | #ifdef CONFIG_UPROBE_EVENTS |
| 1257 | if (flags & TRACE_EVENT_FL_UPROBE) |
| 1258 | err = bpf_get_uprobe_info(event, fd_type, buf, |
| 1259 | probe_offset, |
| 1260 | event->attr.type == PERF_TYPE_TRACEPOINT); |
| 1261 | #endif |
| 1262 | } |
| 1263 | |
| 1264 | return err; |
| 1265 | } |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1266 | |
| 1267 | #ifdef CONFIG_MODULES |
| 1268 | int bpf_event_notify(struct notifier_block *nb, unsigned long op, void *module) |
| 1269 | { |
| 1270 | struct bpf_trace_module *btm, *tmp; |
| 1271 | struct module *mod = module; |
| 1272 | |
| 1273 | if (mod->num_bpf_raw_events == 0 || |
| 1274 | (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) |
| 1275 | return 0; |
| 1276 | |
| 1277 | mutex_lock(&bpf_module_mutex); |
| 1278 | |
| 1279 | switch (op) { |
| 1280 | case MODULE_STATE_COMING: |
| 1281 | btm = kzalloc(sizeof(*btm), GFP_KERNEL); |
| 1282 | if (btm) { |
| 1283 | btm->module = module; |
| 1284 | list_add(&btm->list, &bpf_trace_modules); |
| 1285 | } |
| 1286 | break; |
| 1287 | case MODULE_STATE_GOING: |
| 1288 | list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { |
| 1289 | if (btm->module == module) { |
| 1290 | list_del(&btm->list); |
| 1291 | kfree(btm); |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | mutex_unlock(&bpf_module_mutex); |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static struct notifier_block bpf_module_nb = { |
| 1304 | .notifier_call = bpf_event_notify, |
| 1305 | }; |
| 1306 | |
| 1307 | int __init bpf_event_init(void) |
| 1308 | { |
| 1309 | register_module_notifier(&bpf_module_nb); |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | fs_initcall(bpf_event_init); |
| 1314 | #endif /* CONFIG_MODULES */ |