blob: 08fcfe440c6374e336b02c1dde33a478fc58615a [file] [log] [blame]
Steven Rostedt (VMware)179a0cc2018-08-16 11:20:54 -04001// SPDX-License-Identifier: GPL-2.0
Alexei Starovoitov25415172015-03-25 12:49:20 -07002/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
Alexei Starovoitov0515e592016-09-01 18:37:22 -07003 * Copyright (c) 2016 Facebook
Alexei Starovoitov25415172015-03-25 12:49:20 -07004 */
5#include <linux/kernel.h>
6#include <linux/types.h>
7#include <linux/slab.h>
8#include <linux/bpf.h>
Alexei Starovoitov0515e592016-09-01 18:37:22 -07009#include <linux/bpf_perf_event.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070010#include <linux/filter.h>
11#include <linux/uaccess.h>
Alexei Starovoitov9c959c82015-03-25 12:49:22 -070012#include <linux/ctype.h>
Josef Bacik9802d862017-12-11 11:36:48 -050013#include <linux/kprobes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070014#include <linux/syscalls.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090015#include <linux/error-injection.h>
Josef Bacik9802d862017-12-11 11:36:48 -050016
17#include "trace_probe.h"
Alexei Starovoitov25415172015-03-25 12:49:20 -070018#include "trace.h"
19
Gianluca Borello035226b2017-10-26 01:47:42 +000020u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Yonghong Songc195651e2018-04-28 22:28:08 -070021u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Gianluca Borello035226b2017-10-26 01:47:42 +000022
Alexei Starovoitov25415172015-03-25 12:49:20 -070023/**
24 * trace_call_bpf - invoke BPF program
Yonghong Songe87c6bc382017-10-23 23:53:08 -070025 * @call: tracepoint event
Alexei Starovoitov25415172015-03-25 12:49:20 -070026 * @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 */
Yonghong Songe87c6bc382017-10-23 23:53:08 -070037unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
Alexei Starovoitov25415172015-03-25 12:49:20 -070038{
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
Yonghong Songe87c6bc382017-10-23 23:53:08 -070057 /*
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);
Alexei Starovoitov25415172015-03-25 12:49:20 -070073
74 out:
75 __this_cpu_dec(bpf_prog_active);
76 preempt_enable();
77
78 return ret;
79}
80EXPORT_SYMBOL_GPL(trace_call_bpf);
81
Josef Bacik9802d862017-12-11 11:36:48 -050082#ifdef CONFIG_BPF_KPROBE_OVERRIDE
83BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
84{
Josef Bacik9802d862017-12-11 11:36:48 -050085 regs_set_return_value(regs, rc);
Masami Hiramatsu540adea2018-01-13 02:55:03 +090086 override_function_with_return(regs);
Josef Bacik9802d862017-12-11 11:36:48 -050087 return 0;
88}
89
90static 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
Daniel Borkmannf3694e02016-09-09 02:45:31 +020099BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700100{
Gianluca Borelloeb33f2c2017-11-22 18:32:54 +0000101 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700102
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200103 ret = probe_kernel_read(dst, unsafe_ptr, size);
104 if (unlikely(ret < 0))
105 memset(dst, 0, size);
106
107 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700108}
109
110static const struct bpf_func_proto bpf_probe_read_proto = {
111 .func = bpf_probe_read,
112 .gpl_only = true,
113 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800114 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Yonghong Song9c019e22017-11-12 14:49:10 -0800115 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700116 .arg3_type = ARG_ANYTHING,
117};
118
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200119BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
120 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700121{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700122 /*
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;
Al Virodb68ce12017-03-20 21:08:07 -0400134 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700135 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
142static 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,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800147 .arg2_type = ARG_PTR_TO_MEM,
148 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700149};
150
151static 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
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700159/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200160 * Only limited trace_printk() conversion specifiers allowed:
161 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700162 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200163BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
164 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700165{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700166 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700167 int mod[3] = {};
168 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700169 u64 unsafe_addr;
170 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700171 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++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700197 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700198 mod[fmt_cnt]++;
199 i++;
200 if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
201 return -EINVAL;
202 fmt_cnt++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700203 if (fmt[i - 1] == 's') {
204 if (str_seen)
205 /* allow only one '%s' per fmt string */
206 return -EINVAL;
207 str_seen = true;
208
209 switch (fmt_cnt) {
210 case 1:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200211 unsafe_addr = arg1;
212 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700213 break;
214 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200215 unsafe_addr = arg2;
216 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700217 break;
218 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200219 unsafe_addr = arg3;
220 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700221 break;
222 }
223 buf[0] = 0;
224 strncpy_from_unsafe(buf,
225 (void *) (long) unsafe_addr,
226 sizeof(buf));
227 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700228 continue;
229 }
230
231 if (fmt[i] == 'l') {
232 mod[fmt_cnt]++;
233 i++;
234 }
235
John Fastabend7bda4b42017-07-02 02:13:29 +0200236 if (fmt[i] != 'i' && fmt[i] != 'd' &&
237 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700238 return -EINVAL;
239 fmt_cnt++;
240 }
241
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200242/* Horrid workaround for getting va_list handling working with different
243 * argument type combinations generically for 32 and 64 bit archs.
244 */
245#define __BPF_TP_EMIT() __BPF_ARG3_TP()
246#define __BPF_TP(...) \
Yonghong Songeefa864a2018-01-17 09:19:32 -0800247 __trace_printk(0 /* Fake ip */, \
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200248 fmt, ##__VA_ARGS__)
249
250#define __BPF_ARG1_TP(...) \
251 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
252 ? __BPF_TP(arg1, ##__VA_ARGS__) \
253 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
254 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
255 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
256
257#define __BPF_ARG2_TP(...) \
258 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
259 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
260 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
261 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
262 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
263
264#define __BPF_ARG3_TP(...) \
265 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
266 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
267 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
268 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
269 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
270
271 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700272}
273
274static const struct bpf_func_proto bpf_trace_printk_proto = {
275 .func = bpf_trace_printk,
276 .gpl_only = true,
277 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800278 .arg1_type = ARG_PTR_TO_MEM,
279 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700280};
281
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700282const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
283{
284 /*
285 * this program might be calling bpf_trace_printk,
286 * so allocate per-cpu printk buffers
287 */
288 trace_printk_init_buffers();
289
290 return &bpf_trace_printk_proto;
291}
292
Yonghong Song908432c2017-10-05 09:19:20 -0700293static __always_inline int
294get_map_perf_counter(struct bpf_map *map, u64 flags,
295 u64 *value, u64 *enabled, u64 *running)
Kaixu Xia35578d72015-08-06 07:02:35 +0000296{
Kaixu Xia35578d72015-08-06 07:02:35 +0000297 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200298 unsigned int cpu = smp_processor_id();
299 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200300 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000301
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200302 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
303 return -EINVAL;
304 if (index == BPF_F_CURRENT_CPU)
305 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000306 if (unlikely(index >= array->map.max_entries))
307 return -E2BIG;
308
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200309 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200310 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000311 return -ENOENT;
312
Yonghong Song908432c2017-10-05 09:19:20 -0700313 return perf_event_read_local(ee->event, value, enabled, running);
314}
315
316BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
317{
318 u64 value = 0;
319 int err;
320
321 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
Kaixu Xia35578d72015-08-06 07:02:35 +0000322 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700323 * this api is ugly since we miss [-22..-2] range of valid
324 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000325 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700326 if (err)
327 return err;
328 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000329}
330
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700331static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000332 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700333 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000334 .ret_type = RET_INTEGER,
335 .arg1_type = ARG_CONST_MAP_PTR,
336 .arg2_type = ARG_ANYTHING,
337};
338
Yonghong Song908432c2017-10-05 09:19:20 -0700339BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
340 struct bpf_perf_event_value *, buf, u32, size)
341{
342 int err = -EINVAL;
343
344 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
345 goto clear;
346 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
347 &buf->running);
348 if (unlikely(err))
349 goto clear;
350 return 0;
351clear:
352 memset(buf, 0, size);
353 return err;
354}
355
356static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
357 .func = bpf_perf_event_read_value,
358 .gpl_only = true,
359 .ret_type = RET_INTEGER,
360 .arg1_type = ARG_CONST_MAP_PTR,
361 .arg2_type = ARG_ANYTHING,
362 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
363 .arg4_type = ARG_CONST_SIZE,
364};
365
Daniel Borkmann283ca522017-12-12 02:25:30 +0100366static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200367
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200368static __always_inline u64
369__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
Daniel Borkmann283ca522017-12-12 02:25:30 +0100370 u64 flags, struct perf_sample_data *sd)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700371{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700372 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200373 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200374 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200375 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700376 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700377
Daniel Borkmann1e337592016-04-18 21:01:23 +0200378 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200379 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700380 if (unlikely(index >= array->map.max_entries))
381 return -E2BIG;
382
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200383 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200384 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700385 return -ENOENT;
386
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200387 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700388 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
389 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
390 return -EINVAL;
391
Daniel Borkmannd7931332016-06-28 12:18:24 +0200392 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700393 return -EOPNOTSUPP;
394
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200395 perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700396 return 0;
397}
398
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200399BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
400 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200401{
Daniel Borkmann283ca522017-12-12 02:25:30 +0100402 struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200403 struct perf_raw_record raw = {
404 .frag = {
405 .size = size,
406 .data = data,
407 },
408 };
409
410 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
411 return -EINVAL;
412
Daniel Borkmann283ca522017-12-12 02:25:30 +0100413 perf_sample_data_init(sd, 0, 0);
414 sd->raw = &raw;
415
416 return __bpf_perf_event_output(regs, map, flags, sd);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200417}
418
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700419static const struct bpf_func_proto bpf_perf_event_output_proto = {
420 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700421 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700422 .ret_type = RET_INTEGER,
423 .arg1_type = ARG_PTR_TO_CTX,
424 .arg2_type = ARG_CONST_MAP_PTR,
425 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800426 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000427 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700428};
429
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200430static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100431static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200432
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{
Daniel Borkmann283ca522017-12-12 02:25:30 +0100436 struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200437 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200438 struct perf_raw_frag frag = {
439 .copy = ctx_copy,
440 .size = ctx_size,
441 .data = ctx,
442 };
443 struct perf_raw_record raw = {
444 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700445 {
446 .next = ctx_size ? &frag : NULL,
447 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200448 .size = meta_size,
449 .data = meta,
450 },
451 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200452
453 perf_fetch_caller_regs(regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100454 perf_sample_data_init(sd, 0, 0);
455 sd->raw = &raw;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200456
Daniel Borkmann283ca522017-12-12 02:25:30 +0100457 return __bpf_perf_event_output(regs, map, flags, sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200458}
459
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200460BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700461{
462 return (long) current;
463}
464
465static const struct bpf_func_proto bpf_get_current_task_proto = {
466 .func = bpf_get_current_task,
467 .gpl_only = true,
468 .ret_type = RET_INTEGER,
469};
470
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200471BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700472{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700473 struct bpf_array *array = container_of(map, struct bpf_array, map);
474 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700475
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700476 if (unlikely(idx >= array->map.max_entries))
477 return -E2BIG;
478
479 cgrp = READ_ONCE(array->ptrs[idx]);
480 if (unlikely(!cgrp))
481 return -EAGAIN;
482
483 return task_under_cgroup_hierarchy(current, cgrp);
484}
485
486static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
487 .func = bpf_current_task_under_cgroup,
488 .gpl_only = false,
489 .ret_type = RET_INTEGER,
490 .arg1_type = ARG_CONST_MAP_PTR,
491 .arg2_type = ARG_ANYTHING,
492};
493
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000494BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
495 const void *, unsafe_ptr)
496{
497 int ret;
498
499 /*
500 * The strncpy_from_unsafe() call will likely not fill the entire
501 * buffer, but that's okay in this circumstance as we're probing
502 * arbitrary memory anyway similar to bpf_probe_read() and might
503 * as well probe the stack. Thus, memory is explicitly cleared
504 * only in error case, so that improper users ignoring return
505 * code altogether don't copy garbage; otherwise length of string
506 * is returned that can be used for bpf_perf_event_output() et al.
507 */
508 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
509 if (unlikely(ret < 0))
510 memset(dst, 0, size);
511
512 return ret;
513}
514
515static const struct bpf_func_proto bpf_probe_read_str_proto = {
516 .func = bpf_probe_read_str,
517 .gpl_only = true,
518 .ret_type = RET_INTEGER,
519 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Gianluca Borello5c4e1202017-11-22 18:32:55 +0000520 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000521 .arg3_type = ARG_ANYTHING,
522};
523
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700524static const struct bpf_func_proto *
525tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700526{
527 switch (func_id) {
528 case BPF_FUNC_map_lookup_elem:
529 return &bpf_map_lookup_elem_proto;
530 case BPF_FUNC_map_update_elem:
531 return &bpf_map_update_elem_proto;
532 case BPF_FUNC_map_delete_elem:
533 return &bpf_map_delete_elem_proto;
534 case BPF_FUNC_probe_read:
535 return &bpf_probe_read_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700536 case BPF_FUNC_ktime_get_ns:
537 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700538 case BPF_FUNC_tail_call:
539 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700540 case BPF_FUNC_get_current_pid_tgid:
541 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700542 case BPF_FUNC_get_current_task:
543 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700544 case BPF_FUNC_get_current_uid_gid:
545 return &bpf_get_current_uid_gid_proto;
546 case BPF_FUNC_get_current_comm:
547 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700548 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700549 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700550 case BPF_FUNC_get_smp_processor_id:
551 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200552 case BPF_FUNC_get_numa_node_id:
553 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000554 case BPF_FUNC_perf_event_read:
555 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700556 case BPF_FUNC_probe_write_user:
557 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700558 case BPF_FUNC_current_task_under_cgroup:
559 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700560 case BPF_FUNC_get_prandom_u32:
561 return &bpf_get_prandom_u32_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000562 case BPF_FUNC_probe_read_str:
563 return &bpf_probe_read_str_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700564#ifdef CONFIG_CGROUPS
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700565 case BPF_FUNC_get_current_cgroup_id:
566 return &bpf_get_current_cgroup_id_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700567#endif
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700568 default:
569 return NULL;
570 }
571}
572
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700573static const struct bpf_func_proto *
574kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700575{
576 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700577 case BPF_FUNC_perf_event_output:
578 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800579 case BPF_FUNC_get_stackid:
580 return &bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -0700581 case BPF_FUNC_get_stack:
582 return &bpf_get_stack_proto;
Yonghong Song908432c2017-10-05 09:19:20 -0700583 case BPF_FUNC_perf_event_read_value:
584 return &bpf_perf_event_read_value_proto;
Josef Bacik9802d862017-12-11 11:36:48 -0500585#ifdef CONFIG_BPF_KPROBE_OVERRIDE
586 case BPF_FUNC_override_return:
587 return &bpf_override_return_proto;
588#endif
Alexei Starovoitov25415172015-03-25 12:49:20 -0700589 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700590 return tracing_func_proto(func_id, prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700591 }
592}
593
594/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700595static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700596 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700597 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700598{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700599 if (off < 0 || off >= sizeof(struct pt_regs))
600 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700601 if (type != BPF_READ)
602 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700603 if (off % size != 0)
604 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100605 /*
606 * Assertion for 32 bit to make sure last 8 byte access
607 * (BPF_DW) to the last 4 byte member is disallowed.
608 */
609 if (off + size > sizeof(struct pt_regs))
610 return false;
611
Alexei Starovoitov25415172015-03-25 12:49:20 -0700612 return true;
613}
614
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700615const struct bpf_verifier_ops kprobe_verifier_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700616 .get_func_proto = kprobe_prog_func_proto,
617 .is_valid_access = kprobe_prog_is_valid_access,
618};
619
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700620const struct bpf_prog_ops kprobe_prog_ops = {
621};
622
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200623BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
624 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700625{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200626 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
627
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700628 /*
629 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
630 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200631 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700632 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200633 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700634}
635
636static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
637 .func = bpf_perf_event_output_tp,
638 .gpl_only = true,
639 .ret_type = RET_INTEGER,
640 .arg1_type = ARG_PTR_TO_CTX,
641 .arg2_type = ARG_CONST_MAP_PTR,
642 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800643 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000644 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700645};
646
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200647BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
648 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700649{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200650 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700651
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200652 /*
653 * Same comment as in bpf_perf_event_output_tp(), only that this time
654 * the other helper's function body cannot be inlined due to being
655 * external, thus we need to call raw helper function.
656 */
657 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
658 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700659}
660
661static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
662 .func = bpf_get_stackid_tp,
663 .gpl_only = true,
664 .ret_type = RET_INTEGER,
665 .arg1_type = ARG_PTR_TO_CTX,
666 .arg2_type = ARG_CONST_MAP_PTR,
667 .arg3_type = ARG_ANYTHING,
668};
669
Yonghong Songc195651e2018-04-28 22:28:08 -0700670BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
671 u64, flags)
672{
673 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
674
675 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
676 (unsigned long) size, flags, 0);
677}
678
679static const struct bpf_func_proto bpf_get_stack_proto_tp = {
680 .func = bpf_get_stack_tp,
681 .gpl_only = true,
682 .ret_type = RET_INTEGER,
683 .arg1_type = ARG_PTR_TO_CTX,
684 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
685 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
686 .arg4_type = ARG_ANYTHING,
687};
688
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700689static const struct bpf_func_proto *
690tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700691{
692 switch (func_id) {
693 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700694 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700695 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700696 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700697 case BPF_FUNC_get_stack:
698 return &bpf_get_stack_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700699 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700700 return tracing_func_proto(func_id, prog);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700701 }
702}
703
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700704static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700705 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700706 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700707{
708 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
709 return false;
710 if (type != BPF_READ)
711 return false;
712 if (off % size != 0)
713 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100714
715 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700716 return true;
717}
718
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700719const struct bpf_verifier_ops tracepoint_verifier_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700720 .get_func_proto = tp_prog_func_proto,
721 .is_valid_access = tp_prog_is_valid_access,
722};
723
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700724const struct bpf_prog_ops tracepoint_prog_ops = {
725};
726
Yonghong Songf005afe2018-03-20 11:19:17 -0700727BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
728 struct bpf_perf_event_value *, buf, u32, size)
729{
730 int err = -EINVAL;
731
732 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
733 goto clear;
734 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
735 &buf->running);
736 if (unlikely(err))
737 goto clear;
738 return 0;
739clear:
740 memset(buf, 0, size);
741 return err;
742}
743
744static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
745 .func = bpf_perf_prog_read_value,
746 .gpl_only = true,
747 .ret_type = RET_INTEGER,
748 .arg1_type = ARG_PTR_TO_CTX,
749 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
750 .arg3_type = ARG_CONST_SIZE,
751};
752
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700753static const struct bpf_func_proto *
754pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Yonghong Songf005afe2018-03-20 11:19:17 -0700755{
756 switch (func_id) {
757 case BPF_FUNC_perf_event_output:
758 return &bpf_perf_event_output_proto_tp;
759 case BPF_FUNC_get_stackid:
760 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700761 case BPF_FUNC_get_stack:
762 return &bpf_get_stack_proto_tp;
Yonghong Songf005afe2018-03-20 11:19:17 -0700763 case BPF_FUNC_perf_prog_read_value:
764 return &bpf_perf_prog_read_value_proto;
765 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700766 return tracing_func_proto(func_id, prog);
Yonghong Songf005afe2018-03-20 11:19:17 -0700767 }
768}
769
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700770/*
771 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
772 * to avoid potential recursive reuse issue when/if tracepoints are added
Yonghong Songc195651e2018-04-28 22:28:08 -0700773 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700774 */
775static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
776BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
777 struct bpf_map *, map, u64, flags, void *, data, u64, size)
778{
779 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
780
781 perf_fetch_caller_regs(regs);
782 return ____bpf_perf_event_output(regs, map, flags, data, size);
783}
784
785static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
786 .func = bpf_perf_event_output_raw_tp,
787 .gpl_only = true,
788 .ret_type = RET_INTEGER,
789 .arg1_type = ARG_PTR_TO_CTX,
790 .arg2_type = ARG_CONST_MAP_PTR,
791 .arg3_type = ARG_ANYTHING,
792 .arg4_type = ARG_PTR_TO_MEM,
793 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
794};
795
796BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
797 struct bpf_map *, map, u64, flags)
798{
799 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
800
801 perf_fetch_caller_regs(regs);
802 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
803 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
804 flags, 0, 0);
805}
806
807static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
808 .func = bpf_get_stackid_raw_tp,
809 .gpl_only = true,
810 .ret_type = RET_INTEGER,
811 .arg1_type = ARG_PTR_TO_CTX,
812 .arg2_type = ARG_CONST_MAP_PTR,
813 .arg3_type = ARG_ANYTHING,
814};
815
Yonghong Songc195651e2018-04-28 22:28:08 -0700816BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
817 void *, buf, u32, size, u64, flags)
818{
819 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
820
821 perf_fetch_caller_regs(regs);
822 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
823 (unsigned long) size, flags, 0);
824}
825
826static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
827 .func = bpf_get_stack_raw_tp,
828 .gpl_only = true,
829 .ret_type = RET_INTEGER,
830 .arg1_type = ARG_PTR_TO_CTX,
831 .arg2_type = ARG_PTR_TO_MEM,
832 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
833 .arg4_type = ARG_ANYTHING,
834};
835
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700836static const struct bpf_func_proto *
837raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700838{
839 switch (func_id) {
840 case BPF_FUNC_perf_event_output:
841 return &bpf_perf_event_output_proto_raw_tp;
842 case BPF_FUNC_get_stackid:
843 return &bpf_get_stackid_proto_raw_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700844 case BPF_FUNC_get_stack:
845 return &bpf_get_stack_proto_raw_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700846 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700847 return tracing_func_proto(func_id, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700848 }
849}
850
851static bool raw_tp_prog_is_valid_access(int off, int size,
852 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700853 const struct bpf_prog *prog,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700854 struct bpf_insn_access_aux *info)
855{
856 /* largest tracepoint in the kernel has 12 args */
857 if (off < 0 || off >= sizeof(__u64) * 12)
858 return false;
859 if (type != BPF_READ)
860 return false;
861 if (off % size != 0)
862 return false;
863 return true;
864}
865
866const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
867 .get_func_proto = raw_tp_prog_func_proto,
868 .is_valid_access = raw_tp_prog_is_valid_access,
869};
870
871const struct bpf_prog_ops raw_tracepoint_prog_ops = {
872};
873
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700874static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700875 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700876 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700877{
Teng Qin95da0cd2018-03-06 10:55:01 -0800878 const int size_u64 = sizeof(u64);
Yonghong Song31fd8582017-06-13 15:52:13 -0700879
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700880 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
881 return false;
882 if (type != BPF_READ)
883 return false;
Daniel Borkmannbc231052018-06-02 23:06:39 +0200884 if (off % size != 0) {
885 if (sizeof(unsigned long) != 4)
886 return false;
887 if (size != 8)
888 return false;
889 if (off % size != 4)
890 return false;
891 }
Yonghong Song31fd8582017-06-13 15:52:13 -0700892
Daniel Borkmannf96da092017-07-02 02:13:27 +0200893 switch (off) {
894 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
Teng Qin95da0cd2018-03-06 10:55:01 -0800895 bpf_ctx_record_field_size(info, size_u64);
896 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
897 return false;
898 break;
899 case bpf_ctx_range(struct bpf_perf_event_data, addr):
900 bpf_ctx_record_field_size(info, size_u64);
901 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
Yonghong Song23994632017-06-22 15:07:39 -0700902 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +0200903 break;
904 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700905 if (size != sizeof(long))
906 return false;
907 }
Daniel Borkmannf96da092017-07-02 02:13:27 +0200908
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700909 return true;
910}
911
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100912static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
913 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700914 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200915 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700916{
917 struct bpf_insn *insn = insn_buf;
918
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100919 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700920 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +0200921 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100922 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700923 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100924 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200925 bpf_target_off(struct perf_sample_data, period, 8,
926 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700927 break;
Teng Qin95da0cd2018-03-06 10:55:01 -0800928 case offsetof(struct bpf_perf_event_data, addr):
929 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
930 data), si->dst_reg, si->src_reg,
931 offsetof(struct bpf_perf_event_data_kern, data));
932 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
933 bpf_target_off(struct perf_sample_data, addr, 8,
934 target_size));
935 break;
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700936 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200937 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100938 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700939 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100940 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
941 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700942 break;
943 }
944
945 return insn - insn_buf;
946}
947
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700948const struct bpf_verifier_ops perf_event_verifier_ops = {
Yonghong Songf005afe2018-03-20 11:19:17 -0700949 .get_func_proto = pe_prog_func_proto,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700950 .is_valid_access = pe_prog_is_valid_access,
951 .convert_ctx_access = pe_prog_convert_ctx_access,
952};
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700953
954const struct bpf_prog_ops perf_event_prog_ops = {
955};
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700956
957static DEFINE_MUTEX(bpf_event_mutex);
958
Yonghong Songc8c088b2017-11-30 13:47:54 -0800959#define BPF_TRACE_MAX_PROGS 64
960
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700961int perf_event_attach_bpf_prog(struct perf_event *event,
962 struct bpf_prog *prog)
963{
964 struct bpf_prog_array __rcu *old_array;
965 struct bpf_prog_array *new_array;
966 int ret = -EEXIST;
967
Josef Bacik9802d862017-12-11 11:36:48 -0500968 /*
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900969 * Kprobe override only works if they are on the function entry,
970 * and only if they are on the opt-in list.
Josef Bacik9802d862017-12-11 11:36:48 -0500971 */
972 if (prog->kprobe_override &&
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900973 (!trace_kprobe_on_func_entry(event->tp_event) ||
Josef Bacik9802d862017-12-11 11:36:48 -0500974 !trace_kprobe_error_injectable(event->tp_event)))
975 return -EINVAL;
976
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700977 mutex_lock(&bpf_event_mutex);
978
979 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -0700980 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700981
Yonghong Song07c41a22017-10-30 13:50:22 -0700982 old_array = event->tp_event->prog_array;
Yonghong Songc8c088b2017-11-30 13:47:54 -0800983 if (old_array &&
984 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
985 ret = -E2BIG;
986 goto unlock;
987 }
988
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700989 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
990 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -0700991 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700992
993 /* set the new array to event->tp_event and set event->prog */
994 event->prog = prog;
995 rcu_assign_pointer(event->tp_event->prog_array, new_array);
996 bpf_prog_array_free(old_array);
997
Yonghong Song07c41a22017-10-30 13:50:22 -0700998unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700999 mutex_unlock(&bpf_event_mutex);
1000 return ret;
1001}
1002
1003void perf_event_detach_bpf_prog(struct perf_event *event)
1004{
1005 struct bpf_prog_array __rcu *old_array;
1006 struct bpf_prog_array *new_array;
1007 int ret;
1008
1009 mutex_lock(&bpf_event_mutex);
1010
1011 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001012 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001013
Yonghong Song07c41a22017-10-30 13:50:22 -07001014 old_array = event->tp_event->prog_array;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001015 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
Sean Young170a7e32018-05-27 12:24:08 +01001016 if (ret == -ENOENT)
1017 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001018 if (ret < 0) {
1019 bpf_prog_array_delete_safe(old_array, event->prog);
1020 } else {
1021 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1022 bpf_prog_array_free(old_array);
1023 }
1024
1025 bpf_prog_put(event->prog);
1026 event->prog = NULL;
1027
Yonghong Song07c41a22017-10-30 13:50:22 -07001028unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001029 mutex_unlock(&bpf_event_mutex);
1030}
Yonghong Songf371b302017-12-11 11:39:02 -08001031
Yonghong Songf4e22982017-12-13 10:35:37 -08001032int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -08001033{
1034 struct perf_event_query_bpf __user *uquery = info;
1035 struct perf_event_query_bpf query = {};
Yonghong Song3a38bb92018-04-10 09:37:32 -07001036 u32 *ids, prog_cnt, ids_len;
Yonghong Songf371b302017-12-11 11:39:02 -08001037 int ret;
1038
1039 if (!capable(CAP_SYS_ADMIN))
1040 return -EPERM;
1041 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1042 return -EINVAL;
1043 if (copy_from_user(&query, uquery, sizeof(query)))
1044 return -EFAULT;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001045
1046 ids_len = query.ids_len;
1047 if (ids_len > BPF_TRACE_MAX_PROGS)
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001048 return -E2BIG;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001049 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1050 if (!ids)
1051 return -ENOMEM;
1052 /*
1053 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1054 * is required when user only wants to check for uquery->prog_cnt.
1055 * There is no need to check for it since the case is handled
1056 * gracefully in bpf_prog_array_copy_info.
1057 */
Yonghong Songf371b302017-12-11 11:39:02 -08001058
1059 mutex_lock(&bpf_event_mutex);
1060 ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001061 ids,
1062 ids_len,
1063 &prog_cnt);
Yonghong Songf371b302017-12-11 11:39:02 -08001064 mutex_unlock(&bpf_event_mutex);
1065
Yonghong Song3a38bb92018-04-10 09:37:32 -07001066 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1067 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1068 ret = -EFAULT;
1069
1070 kfree(ids);
Yonghong Songf371b302017-12-11 11:39:02 -08001071 return ret;
1072}
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001073
1074extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1075extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1076
1077struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
1078{
1079 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1080
1081 for (; btp < __stop__bpf_raw_tp; btp++) {
1082 if (!strcmp(btp->tp->name, name))
1083 return btp;
1084 }
1085 return NULL;
1086}
1087
1088static __always_inline
1089void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1090{
1091 rcu_read_lock();
1092 preempt_disable();
1093 (void) BPF_PROG_RUN(prog, args);
1094 preempt_enable();
1095 rcu_read_unlock();
1096}
1097
1098#define UNPACK(...) __VA_ARGS__
1099#define REPEAT_1(FN, DL, X, ...) FN(X)
1100#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1101#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1102#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1103#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1104#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1105#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1106#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1107#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1108#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1109#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1110#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1111#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1112
1113#define SARG(X) u64 arg##X
1114#define COPY(X) args[X] = arg##X
1115
1116#define __DL_COM (,)
1117#define __DL_SEM (;)
1118
1119#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1120
1121#define BPF_TRACE_DEFN_x(x) \
1122 void bpf_trace_run##x(struct bpf_prog *prog, \
1123 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1124 { \
1125 u64 args[x]; \
1126 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1127 __bpf_trace_run(prog, args); \
1128 } \
1129 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1130BPF_TRACE_DEFN_x(1);
1131BPF_TRACE_DEFN_x(2);
1132BPF_TRACE_DEFN_x(3);
1133BPF_TRACE_DEFN_x(4);
1134BPF_TRACE_DEFN_x(5);
1135BPF_TRACE_DEFN_x(6);
1136BPF_TRACE_DEFN_x(7);
1137BPF_TRACE_DEFN_x(8);
1138BPF_TRACE_DEFN_x(9);
1139BPF_TRACE_DEFN_x(10);
1140BPF_TRACE_DEFN_x(11);
1141BPF_TRACE_DEFN_x(12);
1142
1143static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1144{
1145 struct tracepoint *tp = btp->tp;
1146
1147 /*
1148 * check that program doesn't access arguments beyond what's
1149 * available in this tracepoint
1150 */
1151 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1152 return -EINVAL;
1153
1154 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1155}
1156
1157int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1158{
1159 int err;
1160
1161 mutex_lock(&bpf_event_mutex);
1162 err = __bpf_probe_register(btp, prog);
1163 mutex_unlock(&bpf_event_mutex);
1164 return err;
1165}
1166
1167int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1168{
1169 int err;
1170
1171 mutex_lock(&bpf_event_mutex);
1172 err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
1173 mutex_unlock(&bpf_event_mutex);
1174 return err;
1175}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001176
1177int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1178 u32 *fd_type, const char **buf,
1179 u64 *probe_offset, u64 *probe_addr)
1180{
1181 bool is_tracepoint, is_syscall_tp;
1182 struct bpf_prog *prog;
1183 int flags, err = 0;
1184
1185 prog = event->prog;
1186 if (!prog)
1187 return -ENOENT;
1188
1189 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1190 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1191 return -EOPNOTSUPP;
1192
1193 *prog_id = prog->aux->id;
1194 flags = event->tp_event->flags;
1195 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1196 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1197
1198 if (is_tracepoint || is_syscall_tp) {
1199 *buf = is_tracepoint ? event->tp_event->tp->name
1200 : event->tp_event->name;
1201 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1202 *probe_offset = 0x0;
1203 *probe_addr = 0x0;
1204 } else {
1205 /* kprobe/uprobe */
1206 err = -EOPNOTSUPP;
1207#ifdef CONFIG_KPROBE_EVENTS
1208 if (flags & TRACE_EVENT_FL_KPROBE)
1209 err = bpf_get_kprobe_info(event, fd_type, buf,
1210 probe_offset, probe_addr,
1211 event->attr.type == PERF_TYPE_TRACEPOINT);
1212#endif
1213#ifdef CONFIG_UPROBE_EVENTS
1214 if (flags & TRACE_EVENT_FL_UPROBE)
1215 err = bpf_get_uprobe_info(event, fd_type, buf,
1216 probe_offset,
1217 event->attr.type == PERF_TYPE_TRACEPOINT);
1218#endif
1219 }
1220
1221 return err;
1222}