Linux 4.9.199
[linux/fpc-iii.git] / kernel / trace / bpf_trace.c
blob7cc06f267be5dde51f4138830fdab2100f8d8938
1 /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
2 * Copyright (c) 2016 Facebook
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>
12 #include <linux/bpf_perf_event.h>
13 #include <linux/filter.h>
14 #include <linux/uaccess.h>
15 #include <linux/ctype.h>
16 #include "trace.h"
18 /**
19 * trace_call_bpf - invoke BPF program
20 * @prog: BPF program
21 * @ctx: opaque context pointer
23 * kprobe handlers execute BPF programs via this helper.
24 * Can be used from static tracepoints in the future.
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
32 unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
34 unsigned int ret;
36 if (in_nmi()) /* not supported yet */
37 return 1;
39 preempt_disable();
41 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
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
48 ret = 0;
49 goto out;
52 rcu_read_lock();
53 ret = BPF_PROG_RUN(prog, ctx);
54 rcu_read_unlock();
56 out:
57 __this_cpu_dec(bpf_prog_active);
58 preempt_enable();
60 return ret;
62 EXPORT_SYMBOL_GPL(trace_call_bpf);
64 BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
66 int ret;
68 ret = probe_kernel_read(dst, unsafe_ptr, size);
69 if (unlikely(ret < 0))
70 memset(dst, 0, size);
72 return ret;
75 static const struct bpf_func_proto bpf_probe_read_proto = {
76 .func = bpf_probe_read,
77 .gpl_only = true,
78 .ret_type = RET_INTEGER,
79 .arg1_type = ARG_PTR_TO_RAW_STACK,
80 .arg2_type = ARG_CONST_STACK_SIZE,
81 .arg3_type = ARG_ANYTHING,
84 BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
85 u32, size)
88 * Ensure we're in user context which is safe for the helper to
89 * run. This helper has no business in a kthread.
91 * access_ok() should prevent writing to non-user memory, but in
92 * some situations (nommu, temporary switch, etc) access_ok() does
93 * not provide enough validation, hence the check on KERNEL_DS.
96 if (unlikely(in_interrupt() ||
97 current->flags & (PF_KTHREAD | PF_EXITING)))
98 return -EPERM;
99 if (unlikely(segment_eq(get_fs(), KERNEL_DS)))
100 return -EPERM;
101 if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
102 return -EPERM;
104 return probe_kernel_write(unsafe_ptr, src, size);
107 static const struct bpf_func_proto bpf_probe_write_user_proto = {
108 .func = bpf_probe_write_user,
109 .gpl_only = true,
110 .ret_type = RET_INTEGER,
111 .arg1_type = ARG_ANYTHING,
112 .arg2_type = ARG_PTR_TO_STACK,
113 .arg3_type = ARG_CONST_STACK_SIZE,
116 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
118 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
119 current->comm, task_pid_nr(current));
121 return &bpf_probe_write_user_proto;
125 * limited trace_printk()
126 * only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed
128 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
129 u64, arg2, u64, arg3)
131 bool str_seen = false;
132 int mod[3] = {};
133 int fmt_cnt = 0;
134 u64 unsafe_addr;
135 char buf[64];
136 int i;
139 * bpf_check()->check_func_arg()->check_stack_boundary()
140 * guarantees that fmt points to bpf program stack,
141 * fmt_size bytes of it were initialized and fmt_size > 0
143 if (fmt[--fmt_size] != 0)
144 return -EINVAL;
146 /* check format string for allowed specifiers */
147 for (i = 0; i < fmt_size; i++) {
148 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
149 return -EINVAL;
151 if (fmt[i] != '%')
152 continue;
154 if (fmt_cnt >= 3)
155 return -EINVAL;
157 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
158 i++;
159 if (fmt[i] == 'l') {
160 mod[fmt_cnt]++;
161 i++;
162 } else if (fmt[i] == 'p' || fmt[i] == 's') {
163 mod[fmt_cnt]++;
164 /* disallow any further format extensions */
165 if (fmt[i + 1] != 0 &&
166 !isspace(fmt[i + 1]) &&
167 !ispunct(fmt[i + 1]))
168 return -EINVAL;
169 fmt_cnt++;
170 if (fmt[i] == 's') {
171 if (str_seen)
172 /* allow only one '%s' per fmt string */
173 return -EINVAL;
174 str_seen = true;
176 switch (fmt_cnt) {
177 case 1:
178 unsafe_addr = arg1;
179 arg1 = (long) buf;
180 break;
181 case 2:
182 unsafe_addr = arg2;
183 arg2 = (long) buf;
184 break;
185 case 3:
186 unsafe_addr = arg3;
187 arg3 = (long) buf;
188 break;
190 buf[0] = 0;
191 strncpy_from_unsafe(buf,
192 (void *) (long) unsafe_addr,
193 sizeof(buf));
195 continue;
198 if (fmt[i] == 'l') {
199 mod[fmt_cnt]++;
200 i++;
203 if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
204 return -EINVAL;
205 fmt_cnt++;
208 /* Horrid workaround for getting va_list handling working with different
209 * argument type combinations generically for 32 and 64 bit archs.
211 #define __BPF_TP_EMIT() __BPF_ARG3_TP()
212 #define __BPF_TP(...) \
213 __trace_printk(1 /* Fake ip will not be printed. */, \
214 fmt, ##__VA_ARGS__)
216 #define __BPF_ARG1_TP(...) \
217 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
218 ? __BPF_TP(arg1, ##__VA_ARGS__) \
219 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
220 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
221 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
223 #define __BPF_ARG2_TP(...) \
224 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
225 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
226 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
227 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
228 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
230 #define __BPF_ARG3_TP(...) \
231 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
232 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
233 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
234 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
235 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
237 return __BPF_TP_EMIT();
240 static const struct bpf_func_proto bpf_trace_printk_proto = {
241 .func = bpf_trace_printk,
242 .gpl_only = true,
243 .ret_type = RET_INTEGER,
244 .arg1_type = ARG_PTR_TO_STACK,
245 .arg2_type = ARG_CONST_STACK_SIZE,
248 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
251 * this program might be calling bpf_trace_printk,
252 * so allocate per-cpu printk buffers
254 trace_printk_init_buffers();
256 return &bpf_trace_printk_proto;
259 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
261 struct bpf_array *array = container_of(map, struct bpf_array, map);
262 unsigned int cpu = smp_processor_id();
263 u64 index = flags & BPF_F_INDEX_MASK;
264 struct bpf_event_entry *ee;
265 struct perf_event *event;
267 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
268 return -EINVAL;
269 if (index == BPF_F_CURRENT_CPU)
270 index = cpu;
271 if (unlikely(index >= array->map.max_entries))
272 return -E2BIG;
274 ee = READ_ONCE(array->ptrs[index]);
275 if (!ee)
276 return -ENOENT;
278 event = ee->event;
279 if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
280 event->attr.type != PERF_TYPE_RAW))
281 return -EINVAL;
283 /* make sure event is local and doesn't have pmu::count */
284 if (unlikely(event->oncpu != cpu || event->pmu->count))
285 return -EINVAL;
288 * we don't know if the function is run successfully by the
289 * return value. It can be judged in other places, such as
290 * eBPF programs.
292 return perf_event_read_local(event);
295 static const struct bpf_func_proto bpf_perf_event_read_proto = {
296 .func = bpf_perf_event_read,
297 .gpl_only = true,
298 .ret_type = RET_INTEGER,
299 .arg1_type = ARG_CONST_MAP_PTR,
300 .arg2_type = ARG_ANYTHING,
303 static __always_inline u64
304 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
305 u64 flags, struct perf_raw_record *raw)
307 struct bpf_array *array = container_of(map, struct bpf_array, map);
308 unsigned int cpu = smp_processor_id();
309 u64 index = flags & BPF_F_INDEX_MASK;
310 struct perf_sample_data sample_data;
311 struct bpf_event_entry *ee;
312 struct perf_event *event;
314 if (index == BPF_F_CURRENT_CPU)
315 index = cpu;
316 if (unlikely(index >= array->map.max_entries))
317 return -E2BIG;
319 ee = READ_ONCE(array->ptrs[index]);
320 if (!ee)
321 return -ENOENT;
323 event = ee->event;
324 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
325 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
326 return -EINVAL;
328 if (unlikely(event->oncpu != cpu))
329 return -EOPNOTSUPP;
331 perf_sample_data_init(&sample_data, 0, 0);
332 sample_data.raw = raw;
333 perf_event_output(event, &sample_data, regs);
334 return 0;
337 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
338 u64, flags, void *, data, u64, size)
340 struct perf_raw_record raw = {
341 .frag = {
342 .size = size,
343 .data = data,
347 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
348 return -EINVAL;
350 return __bpf_perf_event_output(regs, map, flags, &raw);
353 static const struct bpf_func_proto bpf_perf_event_output_proto = {
354 .func = bpf_perf_event_output,
355 .gpl_only = true,
356 .ret_type = RET_INTEGER,
357 .arg1_type = ARG_PTR_TO_CTX,
358 .arg2_type = ARG_CONST_MAP_PTR,
359 .arg3_type = ARG_ANYTHING,
360 .arg4_type = ARG_PTR_TO_STACK,
361 .arg5_type = ARG_CONST_STACK_SIZE,
364 static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
366 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
367 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
369 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
370 struct perf_raw_frag frag = {
371 .copy = ctx_copy,
372 .size = ctx_size,
373 .data = ctx,
375 struct perf_raw_record raw = {
376 .frag = {
378 .next = ctx_size ? &frag : NULL,
380 .size = meta_size,
381 .data = meta,
385 perf_fetch_caller_regs(regs);
387 return __bpf_perf_event_output(regs, map, flags, &raw);
390 BPF_CALL_0(bpf_get_current_task)
392 return (long) current;
395 static const struct bpf_func_proto bpf_get_current_task_proto = {
396 .func = bpf_get_current_task,
397 .gpl_only = true,
398 .ret_type = RET_INTEGER,
401 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
403 struct bpf_array *array = container_of(map, struct bpf_array, map);
404 struct cgroup *cgrp;
406 if (unlikely(in_interrupt()))
407 return -EINVAL;
408 if (unlikely(idx >= array->map.max_entries))
409 return -E2BIG;
411 cgrp = READ_ONCE(array->ptrs[idx]);
412 if (unlikely(!cgrp))
413 return -EAGAIN;
415 return task_under_cgroup_hierarchy(current, cgrp);
418 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
419 .func = bpf_current_task_under_cgroup,
420 .gpl_only = false,
421 .ret_type = RET_INTEGER,
422 .arg1_type = ARG_CONST_MAP_PTR,
423 .arg2_type = ARG_ANYTHING,
426 static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
428 switch (func_id) {
429 case BPF_FUNC_map_lookup_elem:
430 return &bpf_map_lookup_elem_proto;
431 case BPF_FUNC_map_update_elem:
432 return &bpf_map_update_elem_proto;
433 case BPF_FUNC_map_delete_elem:
434 return &bpf_map_delete_elem_proto;
435 case BPF_FUNC_probe_read:
436 return &bpf_probe_read_proto;
437 case BPF_FUNC_ktime_get_ns:
438 return &bpf_ktime_get_ns_proto;
439 case BPF_FUNC_tail_call:
440 return &bpf_tail_call_proto;
441 case BPF_FUNC_get_current_pid_tgid:
442 return &bpf_get_current_pid_tgid_proto;
443 case BPF_FUNC_get_current_task:
444 return &bpf_get_current_task_proto;
445 case BPF_FUNC_get_current_uid_gid:
446 return &bpf_get_current_uid_gid_proto;
447 case BPF_FUNC_get_current_comm:
448 return &bpf_get_current_comm_proto;
449 case BPF_FUNC_trace_printk:
450 return bpf_get_trace_printk_proto();
451 case BPF_FUNC_get_smp_processor_id:
452 return &bpf_get_smp_processor_id_proto;
453 case BPF_FUNC_perf_event_read:
454 return &bpf_perf_event_read_proto;
455 case BPF_FUNC_probe_write_user:
456 return bpf_get_probe_write_proto();
457 case BPF_FUNC_current_task_under_cgroup:
458 return &bpf_current_task_under_cgroup_proto;
459 case BPF_FUNC_get_prandom_u32:
460 return &bpf_get_prandom_u32_proto;
461 default:
462 return NULL;
466 static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
468 switch (func_id) {
469 case BPF_FUNC_perf_event_output:
470 return &bpf_perf_event_output_proto;
471 case BPF_FUNC_get_stackid:
472 return &bpf_get_stackid_proto;
473 default:
474 return tracing_func_proto(func_id);
478 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
479 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
480 enum bpf_reg_type *reg_type)
482 if (off < 0 || off >= sizeof(struct pt_regs))
483 return false;
484 if (type != BPF_READ)
485 return false;
486 if (off % size != 0)
487 return false;
488 return true;
491 static const struct bpf_verifier_ops kprobe_prog_ops = {
492 .get_func_proto = kprobe_prog_func_proto,
493 .is_valid_access = kprobe_prog_is_valid_access,
496 static struct bpf_prog_type_list kprobe_tl = {
497 .ops = &kprobe_prog_ops,
498 .type = BPF_PROG_TYPE_KPROBE,
501 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
502 u64, flags, void *, data, u64, size)
504 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
507 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
508 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
509 * from there and call the same bpf_perf_event_output() helper inline.
511 return ____bpf_perf_event_output(regs, map, flags, data, size);
514 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
515 .func = bpf_perf_event_output_tp,
516 .gpl_only = true,
517 .ret_type = RET_INTEGER,
518 .arg1_type = ARG_PTR_TO_CTX,
519 .arg2_type = ARG_CONST_MAP_PTR,
520 .arg3_type = ARG_ANYTHING,
521 .arg4_type = ARG_PTR_TO_STACK,
522 .arg5_type = ARG_CONST_STACK_SIZE,
525 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
526 u64, flags)
528 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
531 * Same comment as in bpf_perf_event_output_tp(), only that this time
532 * the other helper's function body cannot be inlined due to being
533 * external, thus we need to call raw helper function.
535 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
536 flags, 0, 0);
539 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
540 .func = bpf_get_stackid_tp,
541 .gpl_only = true,
542 .ret_type = RET_INTEGER,
543 .arg1_type = ARG_PTR_TO_CTX,
544 .arg2_type = ARG_CONST_MAP_PTR,
545 .arg3_type = ARG_ANYTHING,
548 static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
550 switch (func_id) {
551 case BPF_FUNC_perf_event_output:
552 return &bpf_perf_event_output_proto_tp;
553 case BPF_FUNC_get_stackid:
554 return &bpf_get_stackid_proto_tp;
555 default:
556 return tracing_func_proto(func_id);
560 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
561 enum bpf_reg_type *reg_type)
563 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
564 return false;
565 if (type != BPF_READ)
566 return false;
567 if (off % size != 0)
568 return false;
569 return true;
572 static const struct bpf_verifier_ops tracepoint_prog_ops = {
573 .get_func_proto = tp_prog_func_proto,
574 .is_valid_access = tp_prog_is_valid_access,
577 static struct bpf_prog_type_list tracepoint_tl = {
578 .ops = &tracepoint_prog_ops,
579 .type = BPF_PROG_TYPE_TRACEPOINT,
582 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
583 enum bpf_reg_type *reg_type)
585 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
586 return false;
587 if (type != BPF_READ)
588 return false;
589 if (off % size != 0)
590 return false;
591 if (off == offsetof(struct bpf_perf_event_data, sample_period)) {
592 if (size != sizeof(u64))
593 return false;
594 } else {
595 if (size != sizeof(long))
596 return false;
598 return true;
601 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, int dst_reg,
602 int src_reg, int ctx_off,
603 struct bpf_insn *insn_buf,
604 struct bpf_prog *prog)
606 struct bpf_insn *insn = insn_buf;
608 switch (ctx_off) {
609 case offsetof(struct bpf_perf_event_data, sample_period):
610 BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64));
612 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
613 data), dst_reg, src_reg,
614 offsetof(struct bpf_perf_event_data_kern, data));
615 *insn++ = BPF_LDX_MEM(BPF_DW, dst_reg, dst_reg,
616 offsetof(struct perf_sample_data, period));
617 break;
618 default:
619 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
620 regs), dst_reg, src_reg,
621 offsetof(struct bpf_perf_event_data_kern, regs));
622 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), dst_reg, dst_reg, ctx_off);
623 break;
626 return insn - insn_buf;
629 static const struct bpf_verifier_ops perf_event_prog_ops = {
630 .get_func_proto = tp_prog_func_proto,
631 .is_valid_access = pe_prog_is_valid_access,
632 .convert_ctx_access = pe_prog_convert_ctx_access,
635 static struct bpf_prog_type_list perf_event_tl = {
636 .ops = &perf_event_prog_ops,
637 .type = BPF_PROG_TYPE_PERF_EVENT,
640 static int __init register_kprobe_prog_ops(void)
642 bpf_register_prog_type(&kprobe_tl);
643 bpf_register_prog_type(&tracepoint_tl);
644 bpf_register_prog_type(&perf_event_tl);
645 return 0;
647 late_initcall(register_kprobe_prog_ops);