Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/bpf.h> |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 12 | #include <linux/bpf_perf_event.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 13 | #include <linux/filter.h> |
| 14 | #include <linux/uaccess.h> |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 15 | #include <linux/ctype.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 16 | #include "trace.h" |
| 17 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 18 | /** |
| 19 | * trace_call_bpf - invoke BPF program |
Yonghong Song | 265229d | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 20 | * @call: tracepoint event |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 21 | * @ctx: opaque context pointer |
| 22 | * |
| 23 | * kprobe handlers execute BPF programs via this helper. |
| 24 | * Can be used from static tracepoints in the future. |
| 25 | * |
| 26 | * Return: BPF programs always return an integer which is interpreted by |
| 27 | * kprobe handler as: |
| 28 | * 0 - return from kprobe (event is filtered out) |
| 29 | * 1 - store kprobe event into ring buffer |
| 30 | * Other values are reserved and currently alias to 1 |
| 31 | */ |
Yonghong Song | 265229d | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 32 | unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 33 | { |
| 34 | unsigned int ret; |
| 35 | |
| 36 | if (in_nmi()) /* not supported yet */ |
| 37 | return 1; |
| 38 | |
| 39 | preempt_disable(); |
| 40 | |
| 41 | if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { |
| 42 | /* |
| 43 | * since some bpf program is already running on this cpu, |
| 44 | * don't call into another bpf program (same or different) |
| 45 | * and don't send kprobe event into ring-buffer, |
| 46 | * so return zero here |
| 47 | */ |
| 48 | ret = 0; |
| 49 | goto out; |
| 50 | } |
| 51 | |
Yonghong Song | 265229d | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock |
| 54 | * to all call sites, we did a bpf_prog_array_valid() there to check |
| 55 | * whether call->prog_array is empty or not, which is |
| 56 | * a heurisitc to speed up execution. |
| 57 | * |
| 58 | * If bpf_prog_array_valid() fetched prog_array was |
| 59 | * non-NULL, we go into trace_call_bpf() and do the actual |
| 60 | * proper rcu_dereference() under RCU lock. |
| 61 | * If it turns out that prog_array is NULL then, we bail out. |
| 62 | * For the opposite, if the bpf_prog_array_valid() fetched pointer |
| 63 | * was NULL, you'll skip the prog_array with the risk of missing |
| 64 | * out of events when it was updated in between this and the |
| 65 | * rcu_dereference() which is accepted risk. |
| 66 | */ |
| 67 | 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] | 68 | |
| 69 | out: |
| 70 | __this_cpu_dec(bpf_prog_active); |
| 71 | preempt_enable(); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(trace_call_bpf); |
| 76 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 77 | 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] | 78 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 79 | int ret; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 80 | |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 81 | ret = probe_kernel_read(dst, unsafe_ptr, size); |
| 82 | if (unlikely(ret < 0)) |
| 83 | memset(dst, 0, size); |
| 84 | |
| 85 | return ret; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static const struct bpf_func_proto bpf_probe_read_proto = { |
| 89 | .func = bpf_probe_read, |
| 90 | .gpl_only = true, |
| 91 | .ret_type = RET_INTEGER, |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 92 | .arg1_type = ARG_PTR_TO_RAW_STACK, |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 93 | .arg2_type = ARG_CONST_STACK_SIZE, |
| 94 | .arg3_type = ARG_ANYTHING, |
| 95 | }; |
| 96 | |
Joel Fernandes | 0a8a3e5 | 2019-06-04 17:01:00 -0400 | [diff] [blame] | 97 | BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, const void *, unsafe_ptr) |
| 98 | { |
| 99 | int ret; |
| 100 | |
| 101 | /* |
| 102 | * The strncpy_from_unsafe() call will likely not fill the entire |
| 103 | * buffer, but that's okay in this circumstance as we're probing |
| 104 | * arbitrary memory anyway similar to bpf_probe_read() and might |
| 105 | * as well probe the stack. Thus, memory is explicitly cleared |
| 106 | * only in error case, so that improper users ignoring return |
| 107 | * code altogether don't copy garbage; otherwise length of string |
| 108 | * is returned that can be used for bpf_perf_event_output() et al. |
| 109 | */ |
| 110 | ret = strncpy_from_unsafe(dst, unsafe_ptr, size); |
| 111 | if (unlikely(ret < 0)) |
| 112 | memset(dst, 0, size); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static const struct bpf_func_proto bpf_probe_read_str_proto = { |
| 118 | .func = bpf_probe_read_str, |
| 119 | .gpl_only = true, |
| 120 | .ret_type = RET_INTEGER, |
| 121 | .arg1_type = ARG_PTR_TO_RAW_STACK, |
| 122 | .arg2_type = ARG_CONST_STACK_SIZE, |
| 123 | .arg3_type = ARG_ANYTHING, |
| 124 | }; |
| 125 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 126 | BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, |
| 127 | u32, size) |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 128 | { |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 129 | /* |
| 130 | * Ensure we're in user context which is safe for the helper to |
| 131 | * run. This helper has no business in a kthread. |
| 132 | * |
| 133 | * access_ok() should prevent writing to non-user memory, but in |
| 134 | * some situations (nommu, temporary switch, etc) access_ok() does |
| 135 | * not provide enough validation, hence the check on KERNEL_DS. |
| 136 | */ |
| 137 | |
| 138 | if (unlikely(in_interrupt() || |
| 139 | current->flags & (PF_KTHREAD | PF_EXITING))) |
| 140 | return -EPERM; |
| 141 | if (unlikely(segment_eq(get_fs(), KERNEL_DS))) |
| 142 | return -EPERM; |
| 143 | if (!access_ok(VERIFY_WRITE, unsafe_ptr, size)) |
| 144 | return -EPERM; |
| 145 | |
| 146 | return probe_kernel_write(unsafe_ptr, src, size); |
| 147 | } |
| 148 | |
| 149 | static const struct bpf_func_proto bpf_probe_write_user_proto = { |
| 150 | .func = bpf_probe_write_user, |
| 151 | .gpl_only = true, |
| 152 | .ret_type = RET_INTEGER, |
| 153 | .arg1_type = ARG_ANYTHING, |
| 154 | .arg2_type = ARG_PTR_TO_STACK, |
| 155 | .arg3_type = ARG_CONST_STACK_SIZE, |
| 156 | }; |
| 157 | |
| 158 | static const struct bpf_func_proto *bpf_get_probe_write_proto(void) |
| 159 | { |
| 160 | pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", |
| 161 | current->comm, task_pid_nr(current)); |
| 162 | |
| 163 | return &bpf_probe_write_user_proto; |
| 164 | } |
| 165 | |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 166 | /* |
| 167 | * limited trace_printk() |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 168 | * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 169 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 170 | BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, |
| 171 | u64, arg2, u64, arg3) |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 172 | { |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 173 | bool str_seen = false; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 174 | int mod[3] = {}; |
| 175 | int fmt_cnt = 0; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 176 | u64 unsafe_addr; |
| 177 | char buf[64]; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 178 | int i; |
| 179 | |
| 180 | /* |
| 181 | * bpf_check()->check_func_arg()->check_stack_boundary() |
| 182 | * guarantees that fmt points to bpf program stack, |
| 183 | * fmt_size bytes of it were initialized and fmt_size > 0 |
| 184 | */ |
| 185 | if (fmt[--fmt_size] != 0) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | /* check format string for allowed specifiers */ |
| 189 | for (i = 0; i < fmt_size; i++) { |
| 190 | if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | if (fmt[i] != '%') |
| 194 | continue; |
| 195 | |
| 196 | if (fmt_cnt >= 3) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ |
| 200 | i++; |
| 201 | if (fmt[i] == 'l') { |
| 202 | mod[fmt_cnt]++; |
| 203 | i++; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 204 | } else if (fmt[i] == 'p' || fmt[i] == 's') { |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 205 | mod[fmt_cnt]++; |
Martynas Pumputis | 98547af | 2018-11-23 17:43:26 +0100 | [diff] [blame] | 206 | /* disallow any further format extensions */ |
| 207 | if (fmt[i + 1] != 0 && |
| 208 | !isspace(fmt[i + 1]) && |
| 209 | !ispunct(fmt[i + 1])) |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 210 | return -EINVAL; |
| 211 | fmt_cnt++; |
Martynas Pumputis | 98547af | 2018-11-23 17:43:26 +0100 | [diff] [blame] | 212 | if (fmt[i] == 's') { |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 213 | if (str_seen) |
| 214 | /* allow only one '%s' per fmt string */ |
| 215 | return -EINVAL; |
| 216 | str_seen = true; |
| 217 | |
| 218 | switch (fmt_cnt) { |
| 219 | case 1: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 220 | unsafe_addr = arg1; |
| 221 | arg1 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 222 | break; |
| 223 | case 2: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 224 | unsafe_addr = arg2; |
| 225 | arg2 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 226 | break; |
| 227 | case 3: |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 228 | unsafe_addr = arg3; |
| 229 | arg3 = (long) buf; |
Alexei Starovoitov | 8d3b7dc | 2015-08-28 15:56:23 -0700 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | buf[0] = 0; |
| 233 | strncpy_from_unsafe(buf, |
| 234 | (void *) (long) unsafe_addr, |
| 235 | sizeof(buf)); |
| 236 | } |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 237 | continue; |
| 238 | } |
| 239 | |
| 240 | if (fmt[i] == 'l') { |
| 241 | mod[fmt_cnt]++; |
| 242 | i++; |
| 243 | } |
| 244 | |
| 245 | if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x') |
| 246 | return -EINVAL; |
| 247 | fmt_cnt++; |
| 248 | } |
| 249 | |
Daniel Borkmann | d6a6b6b | 2017-08-16 01:45:33 +0200 | [diff] [blame] | 250 | /* Horrid workaround for getting va_list handling working with different |
| 251 | * argument type combinations generically for 32 and 64 bit archs. |
| 252 | */ |
| 253 | #define __BPF_TP_EMIT() __BPF_ARG3_TP() |
| 254 | #define __BPF_TP(...) \ |
| 255 | __trace_printk(1 /* Fake ip will not be printed. */, \ |
| 256 | fmt, ##__VA_ARGS__) |
| 257 | |
| 258 | #define __BPF_ARG1_TP(...) \ |
| 259 | ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ |
| 260 | ? __BPF_TP(arg1, ##__VA_ARGS__) \ |
| 261 | : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ |
| 262 | ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ |
| 263 | : __BPF_TP((u32)arg1, ##__VA_ARGS__))) |
| 264 | |
| 265 | #define __BPF_ARG2_TP(...) \ |
| 266 | ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ |
| 267 | ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ |
| 268 | : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ |
| 269 | ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ |
| 270 | : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) |
| 271 | |
| 272 | #define __BPF_ARG3_TP(...) \ |
| 273 | ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ |
| 274 | ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ |
| 275 | : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ |
| 276 | ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ |
| 277 | : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) |
| 278 | |
| 279 | return __BPF_TP_EMIT(); |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static const struct bpf_func_proto bpf_trace_printk_proto = { |
| 283 | .func = bpf_trace_printk, |
| 284 | .gpl_only = true, |
| 285 | .ret_type = RET_INTEGER, |
| 286 | .arg1_type = ARG_PTR_TO_STACK, |
| 287 | .arg2_type = ARG_CONST_STACK_SIZE, |
| 288 | }; |
| 289 | |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 290 | const struct bpf_func_proto *bpf_get_trace_printk_proto(void) |
| 291 | { |
| 292 | /* |
| 293 | * this program might be calling bpf_trace_printk, |
| 294 | * so allocate per-cpu printk buffers |
| 295 | */ |
| 296 | trace_printk_init_buffers(); |
| 297 | |
| 298 | return &bpf_trace_printk_proto; |
| 299 | } |
| 300 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 301 | BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 302 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 303 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 304 | unsigned int cpu = smp_processor_id(); |
| 305 | u64 index = flags & BPF_F_INDEX_MASK; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 306 | struct bpf_event_entry *ee; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 307 | struct perf_event *event; |
| 308 | |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 309 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 310 | return -EINVAL; |
| 311 | if (index == BPF_F_CURRENT_CPU) |
| 312 | index = cpu; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 313 | if (unlikely(index >= array->map.max_entries)) |
| 314 | return -E2BIG; |
| 315 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 316 | ee = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 317 | if (!ee) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 318 | return -ENOENT; |
| 319 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 320 | event = ee->event; |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 321 | if (unlikely(event->attr.type != PERF_TYPE_HARDWARE && |
| 322 | event->attr.type != PERF_TYPE_RAW)) |
| 323 | return -EINVAL; |
| 324 | |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 325 | /* make sure event is local and doesn't have pmu::count */ |
Daniel Borkmann | 6816a7f | 2016-06-28 12:18:25 +0200 | [diff] [blame] | 326 | if (unlikely(event->oncpu != cpu || event->pmu->count)) |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 327 | return -EINVAL; |
| 328 | |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 329 | /* |
| 330 | * we don't know if the function is run successfully by the |
| 331 | * return value. It can be judged in other places, such as |
| 332 | * eBPF programs. |
| 333 | */ |
| 334 | return perf_event_read_local(event); |
| 335 | } |
| 336 | |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 337 | static const struct bpf_func_proto bpf_perf_event_read_proto = { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 338 | .func = bpf_perf_event_read, |
Alexei Starovoitov | 1075ef5 | 2015-10-23 14:58:19 -0700 | [diff] [blame] | 339 | .gpl_only = true, |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 340 | .ret_type = RET_INTEGER, |
| 341 | .arg1_type = ARG_CONST_MAP_PTR, |
| 342 | .arg2_type = ARG_ANYTHING, |
| 343 | }; |
| 344 | |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 345 | static __always_inline u64 |
| 346 | __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, |
| 347 | u64 flags, struct perf_raw_record *raw) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 348 | { |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 349 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 350 | unsigned int cpu = smp_processor_id(); |
Daniel Borkmann | 1e33759 | 2016-04-18 21:01:23 +0200 | [diff] [blame] | 351 | u64 index = flags & BPF_F_INDEX_MASK; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 352 | struct perf_sample_data sample_data; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 353 | struct bpf_event_entry *ee; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 354 | struct perf_event *event; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 355 | |
Daniel Borkmann | 1e33759 | 2016-04-18 21:01:23 +0200 | [diff] [blame] | 356 | if (index == BPF_F_CURRENT_CPU) |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 357 | index = cpu; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 358 | if (unlikely(index >= array->map.max_entries)) |
| 359 | return -E2BIG; |
| 360 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 361 | ee = READ_ONCE(array->ptrs[index]); |
Daniel Borkmann | 1ca1cc9 | 2016-06-28 12:18:23 +0200 | [diff] [blame] | 362 | if (!ee) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 363 | return -ENOENT; |
| 364 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 365 | event = ee->event; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 366 | if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || |
| 367 | event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) |
| 368 | return -EINVAL; |
| 369 | |
Daniel Borkmann | d793133 | 2016-06-28 12:18:24 +0200 | [diff] [blame] | 370 | if (unlikely(event->oncpu != cpu)) |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 371 | return -EOPNOTSUPP; |
| 372 | |
| 373 | perf_sample_data_init(&sample_data, 0, 0); |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 374 | sample_data.raw = raw; |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 375 | perf_event_output(event, &sample_data, regs); |
| 376 | return 0; |
| 377 | } |
| 378 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 379 | BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, |
| 380 | u64, flags, void *, data, u64, size) |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 381 | { |
Daniel Borkmann | 8e7a392 | 2016-07-14 18:08:04 +0200 | [diff] [blame] | 382 | struct perf_raw_record raw = { |
| 383 | .frag = { |
| 384 | .size = size, |
| 385 | .data = data, |
| 386 | }, |
| 387 | }; |
| 388 | |
| 389 | if (unlikely(flags & ~(BPF_F_INDEX_MASK))) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | return __bpf_perf_event_output(regs, map, flags, &raw); |
| 393 | } |
| 394 | |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 395 | static const struct bpf_func_proto bpf_perf_event_output_proto = { |
| 396 | .func = bpf_perf_event_output, |
Alexei Starovoitov | 1075ef5 | 2015-10-23 14:58:19 -0700 | [diff] [blame] | 397 | .gpl_only = true, |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 398 | .ret_type = RET_INTEGER, |
| 399 | .arg1_type = ARG_PTR_TO_CTX, |
| 400 | .arg2_type = ARG_CONST_MAP_PTR, |
| 401 | .arg3_type = ARG_ANYTHING, |
| 402 | .arg4_type = ARG_PTR_TO_STACK, |
| 403 | .arg5_type = ARG_CONST_STACK_SIZE, |
| 404 | }; |
| 405 | |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 406 | static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs); |
| 407 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 408 | u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, |
| 409 | void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 410 | { |
| 411 | struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs); |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 412 | struct perf_raw_frag frag = { |
| 413 | .copy = ctx_copy, |
| 414 | .size = ctx_size, |
| 415 | .data = ctx, |
| 416 | }; |
| 417 | struct perf_raw_record raw = { |
| 418 | .frag = { |
Andrew Morton | 183fc15 | 2016-07-18 15:50:58 -0700 | [diff] [blame] | 419 | { |
| 420 | .next = ctx_size ? &frag : NULL, |
| 421 | }, |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 422 | .size = meta_size, |
| 423 | .data = meta, |
| 424 | }, |
| 425 | }; |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 426 | |
| 427 | perf_fetch_caller_regs(regs); |
| 428 | |
Daniel Borkmann | 555c8a8 | 2016-07-14 18:08:05 +0200 | [diff] [blame] | 429 | return __bpf_perf_event_output(regs, map, flags, &raw); |
Daniel Borkmann | bd570ff | 2016-04-18 21:01:24 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 432 | BPF_CALL_0(bpf_get_current_task) |
Alexei Starovoitov | 606274c | 2016-07-06 22:38:36 -0700 | [diff] [blame] | 433 | { |
| 434 | return (long) current; |
| 435 | } |
| 436 | |
| 437 | static const struct bpf_func_proto bpf_get_current_task_proto = { |
| 438 | .func = bpf_get_current_task, |
| 439 | .gpl_only = true, |
| 440 | .ret_type = RET_INTEGER, |
| 441 | }; |
| 442 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 443 | 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] | 444 | { |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 445 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 446 | struct cgroup *cgrp; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 447 | |
| 448 | if (unlikely(in_interrupt())) |
| 449 | return -EINVAL; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 450 | if (unlikely(idx >= array->map.max_entries)) |
| 451 | return -E2BIG; |
| 452 | |
| 453 | cgrp = READ_ONCE(array->ptrs[idx]); |
| 454 | if (unlikely(!cgrp)) |
| 455 | return -EAGAIN; |
| 456 | |
| 457 | return task_under_cgroup_hierarchy(current, cgrp); |
| 458 | } |
| 459 | |
| 460 | static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { |
| 461 | .func = bpf_current_task_under_cgroup, |
| 462 | .gpl_only = false, |
| 463 | .ret_type = RET_INTEGER, |
| 464 | .arg1_type = ARG_CONST_MAP_PTR, |
| 465 | .arg2_type = ARG_ANYTHING, |
| 466 | }; |
| 467 | |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 468 | static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 469 | { |
| 470 | switch (func_id) { |
| 471 | case BPF_FUNC_map_lookup_elem: |
| 472 | return &bpf_map_lookup_elem_proto; |
| 473 | case BPF_FUNC_map_update_elem: |
| 474 | return &bpf_map_update_elem_proto; |
| 475 | case BPF_FUNC_map_delete_elem: |
| 476 | return &bpf_map_delete_elem_proto; |
| 477 | case BPF_FUNC_probe_read: |
| 478 | return &bpf_probe_read_proto; |
Joel Fernandes | 0a8a3e5 | 2019-06-04 17:01:00 -0400 | [diff] [blame] | 479 | case BPF_FUNC_probe_read_str: |
| 480 | return &bpf_probe_read_str_proto; |
Alexei Starovoitov | d9847d3 | 2015-03-25 12:49:21 -0700 | [diff] [blame] | 481 | case BPF_FUNC_ktime_get_ns: |
| 482 | return &bpf_ktime_get_ns_proto; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 483 | case BPF_FUNC_tail_call: |
| 484 | return &bpf_tail_call_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 485 | case BPF_FUNC_get_current_pid_tgid: |
| 486 | return &bpf_get_current_pid_tgid_proto; |
Alexei Starovoitov | 606274c | 2016-07-06 22:38:36 -0700 | [diff] [blame] | 487 | case BPF_FUNC_get_current_task: |
| 488 | return &bpf_get_current_task_proto; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 489 | case BPF_FUNC_get_current_uid_gid: |
| 490 | return &bpf_get_current_uid_gid_proto; |
| 491 | case BPF_FUNC_get_current_comm: |
| 492 | return &bpf_get_current_comm_proto; |
Alexei Starovoitov | 9c959c8 | 2015-03-25 12:49:22 -0700 | [diff] [blame] | 493 | case BPF_FUNC_trace_printk: |
Alexei Starovoitov | 0756ea3 | 2015-06-12 19:39:13 -0700 | [diff] [blame] | 494 | return bpf_get_trace_printk_proto(); |
Alexei Starovoitov | ab1973d | 2015-06-12 19:39:14 -0700 | [diff] [blame] | 495 | case BPF_FUNC_get_smp_processor_id: |
| 496 | return &bpf_get_smp_processor_id_proto; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 497 | case BPF_FUNC_perf_event_read: |
| 498 | return &bpf_perf_event_read_proto; |
Sargun Dhillon | 96ae522 | 2016-07-25 05:54:46 -0700 | [diff] [blame] | 499 | case BPF_FUNC_probe_write_user: |
| 500 | return bpf_get_probe_write_proto(); |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 501 | case BPF_FUNC_current_task_under_cgroup: |
| 502 | return &bpf_current_task_under_cgroup_proto; |
Alexei Starovoitov | 8937bd8 | 2016-08-11 18:17:18 -0700 | [diff] [blame] | 503 | case BPF_FUNC_get_prandom_u32: |
| 504 | return &bpf_get_prandom_u32_proto; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 505 | default: |
| 506 | return NULL; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id) |
| 511 | { |
| 512 | switch (func_id) { |
Alexei Starovoitov | a43eec3 | 2015-10-20 20:02:34 -0700 | [diff] [blame] | 513 | case BPF_FUNC_perf_event_output: |
| 514 | return &bpf_perf_event_output_proto; |
Alexei Starovoitov | d5a3b1f | 2016-02-17 19:58:58 -0800 | [diff] [blame] | 515 | case BPF_FUNC_get_stackid: |
| 516 | return &bpf_get_stackid_proto; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 517 | default: |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 518 | return tracing_func_proto(func_id); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | /* bpf+kprobe programs can access fields of 'struct pt_regs' */ |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 523 | static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
| 524 | enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 525 | { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 526 | if (off < 0 || off >= sizeof(struct pt_regs)) |
| 527 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 528 | if (type != BPF_READ) |
| 529 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 530 | if (off % size != 0) |
| 531 | return false; |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 532 | return true; |
| 533 | } |
| 534 | |
Julia Lawall | 27dff4e | 2015-12-11 18:35:59 +0100 | [diff] [blame] | 535 | static const struct bpf_verifier_ops kprobe_prog_ops = { |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 536 | .get_func_proto = kprobe_prog_func_proto, |
| 537 | .is_valid_access = kprobe_prog_is_valid_access, |
| 538 | }; |
| 539 | |
| 540 | static struct bpf_prog_type_list kprobe_tl = { |
| 541 | .ops = &kprobe_prog_ops, |
| 542 | .type = BPF_PROG_TYPE_KPROBE, |
| 543 | }; |
| 544 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 545 | BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, |
| 546 | u64, flags, void *, data, u64, size) |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 547 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 548 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
| 549 | |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 550 | /* |
| 551 | * r1 points to perf tracepoint buffer where first 8 bytes are hidden |
| 552 | * 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] | 553 | * from there and call the same bpf_perf_event_output() helper inline. |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 554 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 555 | return ____bpf_perf_event_output(regs, map, flags, data, size); |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { |
| 559 | .func = bpf_perf_event_output_tp, |
| 560 | .gpl_only = true, |
| 561 | .ret_type = RET_INTEGER, |
| 562 | .arg1_type = ARG_PTR_TO_CTX, |
| 563 | .arg2_type = ARG_CONST_MAP_PTR, |
| 564 | .arg3_type = ARG_ANYTHING, |
| 565 | .arg4_type = ARG_PTR_TO_STACK, |
| 566 | .arg5_type = ARG_CONST_STACK_SIZE, |
| 567 | }; |
| 568 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 569 | BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, |
| 570 | u64, flags) |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 571 | { |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 572 | struct pt_regs *regs = *(struct pt_regs **)tp_buff; |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 573 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 574 | /* |
| 575 | * Same comment as in bpf_perf_event_output_tp(), only that this time |
| 576 | * the other helper's function body cannot be inlined due to being |
| 577 | * external, thus we need to call raw helper function. |
| 578 | */ |
| 579 | return bpf_get_stackid((unsigned long) regs, (unsigned long) map, |
| 580 | flags, 0, 0); |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static const struct bpf_func_proto bpf_get_stackid_proto_tp = { |
| 584 | .func = bpf_get_stackid_tp, |
| 585 | .gpl_only = true, |
| 586 | .ret_type = RET_INTEGER, |
| 587 | .arg1_type = ARG_PTR_TO_CTX, |
| 588 | .arg2_type = ARG_CONST_MAP_PTR, |
| 589 | .arg3_type = ARG_ANYTHING, |
| 590 | }; |
| 591 | |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 592 | static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id) |
| 593 | { |
| 594 | switch (func_id) { |
| 595 | case BPF_FUNC_perf_event_output: |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 596 | return &bpf_perf_event_output_proto_tp; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 597 | case BPF_FUNC_get_stackid: |
Alexei Starovoitov | 9940d67 | 2016-04-06 18:43:27 -0700 | [diff] [blame] | 598 | return &bpf_get_stackid_proto_tp; |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 599 | default: |
| 600 | return tracing_func_proto(func_id); |
| 601 | } |
| 602 | } |
| 603 | |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 604 | static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
| 605 | enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 606 | { |
| 607 | if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) |
| 608 | return false; |
| 609 | if (type != BPF_READ) |
| 610 | return false; |
| 611 | if (off % size != 0) |
| 612 | return false; |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | static const struct bpf_verifier_ops tracepoint_prog_ops = { |
| 617 | .get_func_proto = tp_prog_func_proto, |
| 618 | .is_valid_access = tp_prog_is_valid_access, |
| 619 | }; |
| 620 | |
| 621 | static struct bpf_prog_type_list tracepoint_tl = { |
| 622 | .ops = &tracepoint_prog_ops, |
| 623 | .type = BPF_PROG_TYPE_TRACEPOINT, |
| 624 | }; |
| 625 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 626 | static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, |
| 627 | enum bpf_reg_type *reg_type) |
| 628 | { |
| 629 | if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) |
| 630 | return false; |
| 631 | if (type != BPF_READ) |
| 632 | return false; |
| 633 | if (off % size != 0) |
| 634 | return false; |
| 635 | if (off == offsetof(struct bpf_perf_event_data, sample_period)) { |
| 636 | if (size != sizeof(u64)) |
| 637 | return false; |
| 638 | } else { |
| 639 | if (size != sizeof(long)) |
| 640 | return false; |
| 641 | } |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, int dst_reg, |
| 646 | int src_reg, int ctx_off, |
| 647 | struct bpf_insn *insn_buf, |
| 648 | struct bpf_prog *prog) |
| 649 | { |
| 650 | struct bpf_insn *insn = insn_buf; |
| 651 | |
| 652 | switch (ctx_off) { |
| 653 | case offsetof(struct bpf_perf_event_data, sample_period): |
| 654 | BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64)); |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 655 | |
| 656 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 657 | data), dst_reg, src_reg, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 658 | offsetof(struct bpf_perf_event_data_kern, data)); |
| 659 | *insn++ = BPF_LDX_MEM(BPF_DW, dst_reg, dst_reg, |
| 660 | offsetof(struct perf_sample_data, period)); |
| 661 | break; |
| 662 | default: |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 663 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, |
| 664 | regs), dst_reg, src_reg, |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 665 | offsetof(struct bpf_perf_event_data_kern, regs)); |
Daniel Borkmann | f035a51 | 2016-09-09 02:45:29 +0200 | [diff] [blame] | 666 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), dst_reg, dst_reg, ctx_off); |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | |
| 670 | return insn - insn_buf; |
| 671 | } |
| 672 | |
| 673 | static const struct bpf_verifier_ops perf_event_prog_ops = { |
| 674 | .get_func_proto = tp_prog_func_proto, |
| 675 | .is_valid_access = pe_prog_is_valid_access, |
| 676 | .convert_ctx_access = pe_prog_convert_ctx_access, |
| 677 | }; |
| 678 | |
Yonghong Song | 265229d | 2017-10-23 23:53:08 -0700 | [diff] [blame] | 679 | static DEFINE_MUTEX(bpf_event_mutex); |
| 680 | |
| 681 | int perf_event_attach_bpf_prog(struct perf_event *event, |
| 682 | struct bpf_prog *prog) |
| 683 | { |
| 684 | struct bpf_prog_array __rcu *old_array; |
| 685 | struct bpf_prog_array *new_array; |
| 686 | int ret = -EEXIST; |
| 687 | |
| 688 | mutex_lock(&bpf_event_mutex); |
| 689 | |
| 690 | if (event->prog) |
| 691 | goto out; |
| 692 | |
| 693 | old_array = rcu_dereference_protected(event->tp_event->prog_array, |
| 694 | lockdep_is_held(&bpf_event_mutex)); |
| 695 | ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); |
| 696 | if (ret < 0) |
| 697 | goto out; |
| 698 | |
| 699 | /* set the new array to event->tp_event and set event->prog */ |
| 700 | event->prog = prog; |
| 701 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 702 | bpf_prog_array_free(old_array); |
| 703 | |
| 704 | out: |
| 705 | mutex_unlock(&bpf_event_mutex); |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | void perf_event_detach_bpf_prog(struct perf_event *event) |
| 710 | { |
| 711 | struct bpf_prog_array __rcu *old_array; |
| 712 | struct bpf_prog_array *new_array; |
| 713 | int ret; |
| 714 | |
| 715 | mutex_lock(&bpf_event_mutex); |
| 716 | |
| 717 | if (!event->prog) |
| 718 | goto out; |
| 719 | |
| 720 | old_array = rcu_dereference_protected(event->tp_event->prog_array, |
| 721 | lockdep_is_held(&bpf_event_mutex)); |
| 722 | |
| 723 | ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); |
| 724 | if (ret < 0) { |
| 725 | bpf_prog_array_delete_safe(old_array, event->prog); |
| 726 | } else { |
| 727 | rcu_assign_pointer(event->tp_event->prog_array, new_array); |
| 728 | bpf_prog_array_free(old_array); |
| 729 | } |
| 730 | |
| 731 | bpf_prog_put(event->prog); |
| 732 | event->prog = NULL; |
| 733 | |
| 734 | out: |
| 735 | mutex_unlock(&bpf_event_mutex); |
| 736 | } |
| 737 | |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 738 | static struct bpf_prog_type_list perf_event_tl = { |
| 739 | .ops = &perf_event_prog_ops, |
| 740 | .type = BPF_PROG_TYPE_PERF_EVENT, |
| 741 | }; |
| 742 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 743 | static int __init register_kprobe_prog_ops(void) |
| 744 | { |
| 745 | bpf_register_prog_type(&kprobe_tl); |
Alexei Starovoitov | 9fd82b61 | 2016-04-06 18:43:26 -0700 | [diff] [blame] | 746 | bpf_register_prog_type(&tracepoint_tl); |
Alexei Starovoitov | 0515e59 | 2016-09-01 18:37:22 -0700 | [diff] [blame] | 747 | bpf_register_prog_type(&perf_event_tl); |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | late_initcall(register_kprobe_prog_ops); |