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