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