blob: c5dd60cc07518fb1f26bd8427f6cc581523e7346 [file] [log] [blame]
Alexei Starovoitov25415172015-03-25 12:49:20 -07001/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
Alexei Starovoitov0515e592016-09-01 18:37:22 -07002 * Copyright (c) 2016 Facebook
Alexei Starovoitov25415172015-03-25 12:49:20 -07003 *
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 Starovoitov0515e592016-09-01 18:37:22 -070012#include <linux/bpf_perf_event.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070013#include <linux/filter.h>
14#include <linux/uaccess.h>
Alexei Starovoitov9c959c82015-03-25 12:49:22 -070015#include <linux/ctype.h>
Josef Bacik9802d862017-12-11 11:36:48 -050016#include <linux/kprobes.h>
17#include <asm/kprobes.h>
18
19#include "trace_probe.h"
Alexei Starovoitov25415172015-03-25 12:49:20 -070020#include "trace.h"
21
Gianluca Borello035226b2017-10-26 01:47:42 +000022u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
23
Alexei Starovoitov25415172015-03-25 12:49:20 -070024/**
25 * trace_call_bpf - invoke BPF program
Yonghong Songe87c6bc382017-10-23 23:53:08 -070026 * @call: tracepoint event
Alexei Starovoitov25415172015-03-25 12:49:20 -070027 * @ctx: opaque context pointer
28 *
29 * kprobe handlers execute BPF programs via this helper.
30 * Can be used from static tracepoints in the future.
31 *
32 * Return: BPF programs always return an integer which is interpreted by
33 * kprobe handler as:
34 * 0 - return from kprobe (event is filtered out)
35 * 1 - store kprobe event into ring buffer
36 * Other values are reserved and currently alias to 1
37 */
Yonghong Songe87c6bc382017-10-23 23:53:08 -070038unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
Alexei Starovoitov25415172015-03-25 12:49:20 -070039{
40 unsigned int ret;
41
42 if (in_nmi()) /* not supported yet */
43 return 1;
44
45 preempt_disable();
46
47 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
48 /*
49 * since some bpf program is already running on this cpu,
50 * don't call into another bpf program (same or different)
51 * and don't send kprobe event into ring-buffer,
52 * so return zero here
53 */
54 ret = 0;
55 goto out;
56 }
57
Yonghong Songe87c6bc382017-10-23 23:53:08 -070058 /*
59 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
60 * to all call sites, we did a bpf_prog_array_valid() there to check
61 * whether call->prog_array is empty or not, which is
62 * a heurisitc to speed up execution.
63 *
64 * If bpf_prog_array_valid() fetched prog_array was
65 * non-NULL, we go into trace_call_bpf() and do the actual
66 * proper rcu_dereference() under RCU lock.
67 * If it turns out that prog_array is NULL then, we bail out.
68 * For the opposite, if the bpf_prog_array_valid() fetched pointer
69 * was NULL, you'll skip the prog_array with the risk of missing
70 * out of events when it was updated in between this and the
71 * rcu_dereference() which is accepted risk.
72 */
73 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
Alexei Starovoitov25415172015-03-25 12:49:20 -070074
75 out:
76 __this_cpu_dec(bpf_prog_active);
77 preempt_enable();
78
79 return ret;
80}
81EXPORT_SYMBOL_GPL(trace_call_bpf);
82
Josef Bacik9802d862017-12-11 11:36:48 -050083#ifdef CONFIG_BPF_KPROBE_OVERRIDE
84BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
85{
86 __this_cpu_write(bpf_kprobe_override, 1);
87 regs_set_return_value(regs, rc);
88 arch_ftrace_kprobe_override_function(regs);
89 return 0;
90}
91
92static const struct bpf_func_proto bpf_override_return_proto = {
93 .func = bpf_override_return,
94 .gpl_only = true,
95 .ret_type = RET_INTEGER,
96 .arg1_type = ARG_PTR_TO_CTX,
97 .arg2_type = ARG_ANYTHING,
98};
99#endif
100
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200101BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700102{
Gianluca Borelloeb33f2c2017-11-22 18:32:54 +0000103 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700104
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200105 ret = probe_kernel_read(dst, unsafe_ptr, size);
106 if (unlikely(ret < 0))
107 memset(dst, 0, size);
108
109 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700110}
111
112static const struct bpf_func_proto bpf_probe_read_proto = {
113 .func = bpf_probe_read,
114 .gpl_only = true,
115 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800116 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Yonghong Song9c019e22017-11-12 14:49:10 -0800117 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700118 .arg3_type = ARG_ANYTHING,
119};
120
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200121BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
122 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700123{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700124 /*
125 * Ensure we're in user context which is safe for the helper to
126 * run. This helper has no business in a kthread.
127 *
128 * access_ok() should prevent writing to non-user memory, but in
129 * some situations (nommu, temporary switch, etc) access_ok() does
130 * not provide enough validation, hence the check on KERNEL_DS.
131 */
132
133 if (unlikely(in_interrupt() ||
134 current->flags & (PF_KTHREAD | PF_EXITING)))
135 return -EPERM;
Al Virodb68ce12017-03-20 21:08:07 -0400136 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700137 return -EPERM;
138 if (!access_ok(VERIFY_WRITE, unsafe_ptr, size))
139 return -EPERM;
140
141 return probe_kernel_write(unsafe_ptr, src, size);
142}
143
144static const struct bpf_func_proto bpf_probe_write_user_proto = {
145 .func = bpf_probe_write_user,
146 .gpl_only = true,
147 .ret_type = RET_INTEGER,
148 .arg1_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800149 .arg2_type = ARG_PTR_TO_MEM,
150 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700151};
152
153static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
154{
155 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
156 current->comm, task_pid_nr(current));
157
158 return &bpf_probe_write_user_proto;
159}
160
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700161/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200162 * Only limited trace_printk() conversion specifiers allowed:
163 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700164 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200165BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
166 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700167{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700168 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700169 int mod[3] = {};
170 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700171 u64 unsafe_addr;
172 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700173 int i;
174
175 /*
176 * bpf_check()->check_func_arg()->check_stack_boundary()
177 * guarantees that fmt points to bpf program stack,
178 * fmt_size bytes of it were initialized and fmt_size > 0
179 */
180 if (fmt[--fmt_size] != 0)
181 return -EINVAL;
182
183 /* check format string for allowed specifiers */
184 for (i = 0; i < fmt_size; i++) {
185 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
186 return -EINVAL;
187
188 if (fmt[i] != '%')
189 continue;
190
191 if (fmt_cnt >= 3)
192 return -EINVAL;
193
194 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
195 i++;
196 if (fmt[i] == 'l') {
197 mod[fmt_cnt]++;
198 i++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700199 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700200 mod[fmt_cnt]++;
201 i++;
202 if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
203 return -EINVAL;
204 fmt_cnt++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700205 if (fmt[i - 1] == '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:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200213 unsafe_addr = arg1;
214 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700215 break;
216 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200217 unsafe_addr = arg2;
218 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700219 break;
220 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200221 unsafe_addr = arg3;
222 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700223 break;
224 }
225 buf[0] = 0;
226 strncpy_from_unsafe(buf,
227 (void *) (long) unsafe_addr,
228 sizeof(buf));
229 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700230 continue;
231 }
232
233 if (fmt[i] == 'l') {
234 mod[fmt_cnt]++;
235 i++;
236 }
237
John Fastabend7bda4b42017-07-02 02:13:29 +0200238 if (fmt[i] != 'i' && fmt[i] != 'd' &&
239 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700240 return -EINVAL;
241 fmt_cnt++;
242 }
243
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200244/* 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(1 /* Fake ip will not be printed. */, \
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();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700274}
275
276static const struct bpf_func_proto bpf_trace_printk_proto = {
277 .func = bpf_trace_printk,
278 .gpl_only = true,
279 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800280 .arg1_type = ARG_PTR_TO_MEM,
281 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700282};
283
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700284const 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
Yonghong Song908432c2017-10-05 09:19:20 -0700295static __always_inline int
296get_map_perf_counter(struct bpf_map *map, u64 flags,
297 u64 *value, u64 *enabled, u64 *running)
Kaixu Xia35578d72015-08-06 07:02:35 +0000298{
Kaixu Xia35578d72015-08-06 07:02:35 +0000299 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200300 unsigned int cpu = smp_processor_id();
301 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200302 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000303
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200304 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
305 return -EINVAL;
306 if (index == BPF_F_CURRENT_CPU)
307 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000308 if (unlikely(index >= array->map.max_entries))
309 return -E2BIG;
310
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200311 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200312 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000313 return -ENOENT;
314
Yonghong Song908432c2017-10-05 09:19:20 -0700315 return perf_event_read_local(ee->event, value, enabled, running);
316}
317
318BPF_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);
Kaixu Xia35578d72015-08-06 07:02:35 +0000324 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700325 * this api is ugly since we miss [-22..-2] range of valid
326 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000327 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700328 if (err)
329 return err;
330 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000331}
332
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700333static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000334 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700335 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000336 .ret_type = RET_INTEGER,
337 .arg1_type = ARG_CONST_MAP_PTR,
338 .arg2_type = ARG_ANYTHING,
339};
340
Yonghong Song908432c2017-10-05 09:19:20 -0700341BPF_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;
353clear:
354 memset(buf, 0, size);
355 return err;
356}
357
358static 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
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200368static DEFINE_PER_CPU(struct perf_sample_data, bpf_sd);
369
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200370static __always_inline u64
371__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
372 u64 flags, struct perf_raw_record *raw)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700373{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700374 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200375 struct perf_sample_data *sd = this_cpu_ptr(&bpf_sd);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200376 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200377 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200378 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700379 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700380
Daniel Borkmann1e337592016-04-18 21:01:23 +0200381 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200382 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700383 if (unlikely(index >= array->map.max_entries))
384 return -E2BIG;
385
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200386 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200387 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700388 return -ENOENT;
389
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200390 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700391 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
392 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
393 return -EINVAL;
394
Daniel Borkmannd7931332016-06-28 12:18:24 +0200395 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700396 return -EOPNOTSUPP;
397
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200398 perf_sample_data_init(sd, 0, 0);
399 sd->raw = raw;
400 perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700401 return 0;
402}
403
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200404BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
405 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200406{
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200407 struct perf_raw_record raw = {
408 .frag = {
409 .size = size,
410 .data = data,
411 },
412 };
413
414 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
415 return -EINVAL;
416
417 return __bpf_perf_event_output(regs, map, flags, &raw);
418}
419
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700420static const struct bpf_func_proto bpf_perf_event_output_proto = {
421 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700422 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700423 .ret_type = RET_INTEGER,
424 .arg1_type = ARG_PTR_TO_CTX,
425 .arg2_type = ARG_CONST_MAP_PTR,
426 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800427 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000428 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700429};
430
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200431static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
432
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200433u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
434 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200435{
436 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200437 struct perf_raw_frag frag = {
438 .copy = ctx_copy,
439 .size = ctx_size,
440 .data = ctx,
441 };
442 struct perf_raw_record raw = {
443 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700444 {
445 .next = ctx_size ? &frag : NULL,
446 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200447 .size = meta_size,
448 .data = meta,
449 },
450 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200451
452 perf_fetch_caller_regs(regs);
453
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200454 return __bpf_perf_event_output(regs, map, flags, &raw);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200455}
456
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200457BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700458{
459 return (long) current;
460}
461
462static const struct bpf_func_proto bpf_get_current_task_proto = {
463 .func = bpf_get_current_task,
464 .gpl_only = true,
465 .ret_type = RET_INTEGER,
466};
467
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200468BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700469{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700470 struct bpf_array *array = container_of(map, struct bpf_array, map);
471 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700472
473 if (unlikely(in_interrupt()))
474 return -EINVAL;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700475 if (unlikely(idx >= array->map.max_entries))
476 return -E2BIG;
477
478 cgrp = READ_ONCE(array->ptrs[idx]);
479 if (unlikely(!cgrp))
480 return -EAGAIN;
481
482 return task_under_cgroup_hierarchy(current, cgrp);
483}
484
485static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
486 .func = bpf_current_task_under_cgroup,
487 .gpl_only = false,
488 .ret_type = RET_INTEGER,
489 .arg1_type = ARG_CONST_MAP_PTR,
490 .arg2_type = ARG_ANYTHING,
491};
492
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000493BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
494 const void *, unsafe_ptr)
495{
496 int ret;
497
498 /*
499 * The strncpy_from_unsafe() call will likely not fill the entire
500 * buffer, but that's okay in this circumstance as we're probing
501 * arbitrary memory anyway similar to bpf_probe_read() and might
502 * as well probe the stack. Thus, memory is explicitly cleared
503 * only in error case, so that improper users ignoring return
504 * code altogether don't copy garbage; otherwise length of string
505 * is returned that can be used for bpf_perf_event_output() et al.
506 */
507 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
508 if (unlikely(ret < 0))
509 memset(dst, 0, size);
510
511 return ret;
512}
513
514static const struct bpf_func_proto bpf_probe_read_str_proto = {
515 .func = bpf_probe_read_str,
516 .gpl_only = true,
517 .ret_type = RET_INTEGER,
518 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Gianluca Borello5c4e1202017-11-22 18:32:55 +0000519 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000520 .arg3_type = ARG_ANYTHING,
521};
522
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700523static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700524{
525 switch (func_id) {
526 case BPF_FUNC_map_lookup_elem:
527 return &bpf_map_lookup_elem_proto;
528 case BPF_FUNC_map_update_elem:
529 return &bpf_map_update_elem_proto;
530 case BPF_FUNC_map_delete_elem:
531 return &bpf_map_delete_elem_proto;
532 case BPF_FUNC_probe_read:
533 return &bpf_probe_read_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700534 case BPF_FUNC_ktime_get_ns:
535 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700536 case BPF_FUNC_tail_call:
537 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700538 case BPF_FUNC_get_current_pid_tgid:
539 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700540 case BPF_FUNC_get_current_task:
541 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700542 case BPF_FUNC_get_current_uid_gid:
543 return &bpf_get_current_uid_gid_proto;
544 case BPF_FUNC_get_current_comm:
545 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700546 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700547 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700548 case BPF_FUNC_get_smp_processor_id:
549 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200550 case BPF_FUNC_get_numa_node_id:
551 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000552 case BPF_FUNC_perf_event_read:
553 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700554 case BPF_FUNC_probe_write_user:
555 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700556 case BPF_FUNC_current_task_under_cgroup:
557 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700558 case BPF_FUNC_get_prandom_u32:
559 return &bpf_get_prandom_u32_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000560 case BPF_FUNC_probe_read_str:
561 return &bpf_probe_read_str_proto;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700562 default:
563 return NULL;
564 }
565}
566
567static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
568{
569 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700570 case BPF_FUNC_perf_event_output:
571 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800572 case BPF_FUNC_get_stackid:
573 return &bpf_get_stackid_proto;
Yonghong Song908432c2017-10-05 09:19:20 -0700574 case BPF_FUNC_perf_event_read_value:
575 return &bpf_perf_event_read_value_proto;
Josef Bacik9802d862017-12-11 11:36:48 -0500576#ifdef CONFIG_BPF_KPROBE_OVERRIDE
577 case BPF_FUNC_override_return:
578 return &bpf_override_return_proto;
579#endif
Alexei Starovoitov25415172015-03-25 12:49:20 -0700580 default:
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700581 return tracing_func_proto(func_id);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700582 }
583}
584
585/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700586static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Yonghong Song23994632017-06-22 15:07:39 -0700587 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700588{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700589 if (off < 0 || off >= sizeof(struct pt_regs))
590 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700591 if (type != BPF_READ)
592 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700593 if (off % size != 0)
594 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100595 /*
596 * Assertion for 32 bit to make sure last 8 byte access
597 * (BPF_DW) to the last 4 byte member is disallowed.
598 */
599 if (off + size > sizeof(struct pt_regs))
600 return false;
601
Alexei Starovoitov25415172015-03-25 12:49:20 -0700602 return true;
603}
604
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700605const struct bpf_verifier_ops kprobe_verifier_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700606 .get_func_proto = kprobe_prog_func_proto,
607 .is_valid_access = kprobe_prog_is_valid_access,
608};
609
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700610const struct bpf_prog_ops kprobe_prog_ops = {
611};
612
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200613BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
614 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700615{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200616 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
617
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700618 /*
619 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
620 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200621 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700622 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200623 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700624}
625
626static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
627 .func = bpf_perf_event_output_tp,
628 .gpl_only = true,
629 .ret_type = RET_INTEGER,
630 .arg1_type = ARG_PTR_TO_CTX,
631 .arg2_type = ARG_CONST_MAP_PTR,
632 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800633 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000634 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700635};
636
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200637BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
638 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700639{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200640 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700641
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200642 /*
643 * Same comment as in bpf_perf_event_output_tp(), only that this time
644 * the other helper's function body cannot be inlined due to being
645 * external, thus we need to call raw helper function.
646 */
647 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
648 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700649}
650
651static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
652 .func = bpf_get_stackid_tp,
653 .gpl_only = true,
654 .ret_type = RET_INTEGER,
655 .arg1_type = ARG_PTR_TO_CTX,
656 .arg2_type = ARG_CONST_MAP_PTR,
657 .arg3_type = ARG_ANYTHING,
658};
659
Yonghong Song4bebdc72017-10-05 09:19:22 -0700660BPF_CALL_3(bpf_perf_prog_read_value_tp, struct bpf_perf_event_data_kern *, ctx,
661 struct bpf_perf_event_value *, buf, u32, size)
662{
663 int err = -EINVAL;
664
665 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
666 goto clear;
667 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
668 &buf->running);
669 if (unlikely(err))
670 goto clear;
671 return 0;
672clear:
673 memset(buf, 0, size);
674 return err;
675}
676
677static const struct bpf_func_proto bpf_perf_prog_read_value_proto_tp = {
678 .func = bpf_perf_prog_read_value_tp,
679 .gpl_only = true,
680 .ret_type = RET_INTEGER,
681 .arg1_type = ARG_PTR_TO_CTX,
682 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
683 .arg3_type = ARG_CONST_SIZE,
684};
685
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700686static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
687{
688 switch (func_id) {
689 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700690 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700691 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700692 return &bpf_get_stackid_proto_tp;
Yonghong Song4bebdc72017-10-05 09:19:22 -0700693 case BPF_FUNC_perf_prog_read_value:
694 return &bpf_perf_prog_read_value_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700695 default:
696 return tracing_func_proto(func_id);
697 }
698}
699
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700700static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Yonghong Song23994632017-06-22 15:07:39 -0700701 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700702{
703 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
704 return false;
705 if (type != BPF_READ)
706 return false;
707 if (off % size != 0)
708 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100709
710 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700711 return true;
712}
713
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700714const struct bpf_verifier_ops tracepoint_verifier_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700715 .get_func_proto = tp_prog_func_proto,
716 .is_valid_access = tp_prog_is_valid_access,
717};
718
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700719const struct bpf_prog_ops tracepoint_prog_ops = {
720};
721
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700722static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Yonghong Song23994632017-06-22 15:07:39 -0700723 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700724{
Daniel Borkmannf96da092017-07-02 02:13:27 +0200725 const int size_sp = FIELD_SIZEOF(struct bpf_perf_event_data,
726 sample_period);
Yonghong Song31fd8582017-06-13 15:52:13 -0700727
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700728 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
729 return false;
730 if (type != BPF_READ)
731 return false;
732 if (off % size != 0)
733 return false;
Yonghong Song31fd8582017-06-13 15:52:13 -0700734
Daniel Borkmannf96da092017-07-02 02:13:27 +0200735 switch (off) {
736 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
737 bpf_ctx_record_field_size(info, size_sp);
738 if (!bpf_ctx_narrow_access_ok(off, size, size_sp))
Yonghong Song23994632017-06-22 15:07:39 -0700739 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +0200740 break;
741 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700742 if (size != sizeof(long))
743 return false;
744 }
Daniel Borkmannf96da092017-07-02 02:13:27 +0200745
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700746 return true;
747}
748
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100749static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
750 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700751 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200752 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700753{
754 struct bpf_insn *insn = insn_buf;
755
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100756 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700757 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +0200758 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100759 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700760 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100761 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200762 bpf_target_off(struct perf_sample_data, period, 8,
763 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700764 break;
765 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200766 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100767 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700768 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100769 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
770 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700771 break;
772 }
773
774 return insn - insn_buf;
775}
776
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700777const struct bpf_verifier_ops perf_event_verifier_ops = {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700778 .get_func_proto = tp_prog_func_proto,
779 .is_valid_access = pe_prog_is_valid_access,
780 .convert_ctx_access = pe_prog_convert_ctx_access,
781};
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700782
783const struct bpf_prog_ops perf_event_prog_ops = {
784};
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700785
786static DEFINE_MUTEX(bpf_event_mutex);
787
Yonghong Songc8c088b2017-11-30 13:47:54 -0800788#define BPF_TRACE_MAX_PROGS 64
789
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700790int perf_event_attach_bpf_prog(struct perf_event *event,
791 struct bpf_prog *prog)
792{
793 struct bpf_prog_array __rcu *old_array;
794 struct bpf_prog_array *new_array;
795 int ret = -EEXIST;
796
Josef Bacik9802d862017-12-11 11:36:48 -0500797 /*
798 * Kprobe override only works for ftrace based kprobes, and only if they
799 * are on the opt-in list.
800 */
801 if (prog->kprobe_override &&
802 (!trace_kprobe_ftrace(event->tp_event) ||
803 !trace_kprobe_error_injectable(event->tp_event)))
804 return -EINVAL;
805
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700806 mutex_lock(&bpf_event_mutex);
807
808 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -0700809 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700810
Yonghong Song07c41a22017-10-30 13:50:22 -0700811 old_array = event->tp_event->prog_array;
Yonghong Songc8c088b2017-11-30 13:47:54 -0800812 if (old_array &&
813 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
814 ret = -E2BIG;
815 goto unlock;
816 }
817
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700818 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
819 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -0700820 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700821
822 /* set the new array to event->tp_event and set event->prog */
823 event->prog = prog;
824 rcu_assign_pointer(event->tp_event->prog_array, new_array);
825 bpf_prog_array_free(old_array);
826
Yonghong Song07c41a22017-10-30 13:50:22 -0700827unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700828 mutex_unlock(&bpf_event_mutex);
829 return ret;
830}
831
832void perf_event_detach_bpf_prog(struct perf_event *event)
833{
834 struct bpf_prog_array __rcu *old_array;
835 struct bpf_prog_array *new_array;
836 int ret;
837
838 mutex_lock(&bpf_event_mutex);
839
840 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -0700841 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700842
Yonghong Song07c41a22017-10-30 13:50:22 -0700843 old_array = event->tp_event->prog_array;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700844 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
845 if (ret < 0) {
846 bpf_prog_array_delete_safe(old_array, event->prog);
847 } else {
848 rcu_assign_pointer(event->tp_event->prog_array, new_array);
849 bpf_prog_array_free(old_array);
850 }
851
852 bpf_prog_put(event->prog);
853 event->prog = NULL;
854
Yonghong Song07c41a22017-10-30 13:50:22 -0700855unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700856 mutex_unlock(&bpf_event_mutex);
857}
Yonghong Songf371b302017-12-11 11:39:02 -0800858
Yonghong Songf4e22982017-12-13 10:35:37 -0800859int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -0800860{
861 struct perf_event_query_bpf __user *uquery = info;
862 struct perf_event_query_bpf query = {};
863 int ret;
864
865 if (!capable(CAP_SYS_ADMIN))
866 return -EPERM;
867 if (event->attr.type != PERF_TYPE_TRACEPOINT)
868 return -EINVAL;
869 if (copy_from_user(&query, uquery, sizeof(query)))
870 return -EFAULT;
871
872 mutex_lock(&bpf_event_mutex);
873 ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
874 uquery->ids,
875 query.ids_len,
876 &uquery->prog_cnt);
877 mutex_unlock(&bpf_event_mutex);
878
879 return ret;
880}