blob: 9864a35c8bb576e30655bccf62e5174cb1591072 [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]++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100199 /* disallow any further format extensions */
200 if (fmt[i + 1] != 0 &&
201 !isspace(fmt[i + 1]) &&
202 !ispunct(fmt[i + 1]))
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700203 return -EINVAL;
204 fmt_cnt++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100205 if (fmt[i] == 's') {
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700206 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(...) \
Yonghong Songeefa864a2018-01-17 09:19:32 -0800249 __trace_printk(0 /* Fake ip */, \
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200250 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 Borkmann283ca522017-12-12 02:25:30 +0100368static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200369
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200370static __always_inline u64
371__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
Daniel Borkmann283ca522017-12-12 02:25:30 +0100372 u64 flags, struct perf_sample_data *sd)
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 Borkmannd7931332016-06-28 12:18:24 +0200375 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200376 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200377 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700378 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700379
Daniel Borkmann1e337592016-04-18 21:01:23 +0200380 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200381 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700382 if (unlikely(index >= array->map.max_entries))
383 return -E2BIG;
384
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200385 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200386 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700387 return -ENOENT;
388
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200389 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700390 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
391 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
392 return -EINVAL;
393
Daniel Borkmannd7931332016-06-28 12:18:24 +0200394 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700395 return -EOPNOTSUPP;
396
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200397 perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700398 return 0;
399}
400
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200401BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
402 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200403{
Daniel Borkmann283ca522017-12-12 02:25:30 +0100404 struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200405 struct perf_raw_record raw = {
406 .frag = {
407 .size = size,
408 .data = data,
409 },
410 };
411
412 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
413 return -EINVAL;
414
Daniel Borkmann283ca522017-12-12 02:25:30 +0100415 perf_sample_data_init(sd, 0, 0);
416 sd->raw = &raw;
417
418 return __bpf_perf_event_output(regs, map, flags, sd);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200419}
420
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700421static const struct bpf_func_proto bpf_perf_event_output_proto = {
422 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700423 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700424 .ret_type = RET_INTEGER,
425 .arg1_type = ARG_PTR_TO_CTX,
426 .arg2_type = ARG_CONST_MAP_PTR,
427 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800428 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000429 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700430};
431
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200432static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100433static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200434
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200435u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
436 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200437{
Daniel Borkmann283ca522017-12-12 02:25:30 +0100438 struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200439 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200440 struct perf_raw_frag frag = {
441 .copy = ctx_copy,
442 .size = ctx_size,
443 .data = ctx,
444 };
445 struct perf_raw_record raw = {
446 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700447 {
448 .next = ctx_size ? &frag : NULL,
449 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200450 .size = meta_size,
451 .data = meta,
452 },
453 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200454
455 perf_fetch_caller_regs(regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100456 perf_sample_data_init(sd, 0, 0);
457 sd->raw = &raw;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200458
Daniel Borkmann283ca522017-12-12 02:25:30 +0100459 return __bpf_perf_event_output(regs, map, flags, sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200460}
461
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200462BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700463{
464 return (long) current;
465}
466
467static const struct bpf_func_proto bpf_get_current_task_proto = {
468 .func = bpf_get_current_task,
469 .gpl_only = true,
470 .ret_type = RET_INTEGER,
471};
472
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200473BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700474{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700475 struct bpf_array *array = container_of(map, struct bpf_array, map);
476 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700477
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700478 if (unlikely(idx >= array->map.max_entries))
479 return -E2BIG;
480
481 cgrp = READ_ONCE(array->ptrs[idx]);
482 if (unlikely(!cgrp))
483 return -EAGAIN;
484
485 return task_under_cgroup_hierarchy(current, cgrp);
486}
487
488static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
489 .func = bpf_current_task_under_cgroup,
490 .gpl_only = false,
491 .ret_type = RET_INTEGER,
492 .arg1_type = ARG_CONST_MAP_PTR,
493 .arg2_type = ARG_ANYTHING,
494};
495
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000496BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
497 const void *, unsafe_ptr)
498{
499 int ret;
500
501 /*
502 * The strncpy_from_unsafe() call will likely not fill the entire
503 * buffer, but that's okay in this circumstance as we're probing
504 * arbitrary memory anyway similar to bpf_probe_read() and might
505 * as well probe the stack. Thus, memory is explicitly cleared
506 * only in error case, so that improper users ignoring return
507 * code altogether don't copy garbage; otherwise length of string
508 * is returned that can be used for bpf_perf_event_output() et al.
509 */
510 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
511 if (unlikely(ret < 0))
512 memset(dst, 0, size);
513
514 return ret;
515}
516
517static const struct bpf_func_proto bpf_probe_read_str_proto = {
518 .func = bpf_probe_read_str,
519 .gpl_only = true,
520 .ret_type = RET_INTEGER,
521 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Gianluca Borello5c4e1202017-11-22 18:32:55 +0000522 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000523 .arg3_type = ARG_ANYTHING,
524};
525
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700526static const struct bpf_func_proto *
527tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700528{
529 switch (func_id) {
530 case BPF_FUNC_map_lookup_elem:
531 return &bpf_map_lookup_elem_proto;
532 case BPF_FUNC_map_update_elem:
533 return &bpf_map_update_elem_proto;
534 case BPF_FUNC_map_delete_elem:
535 return &bpf_map_delete_elem_proto;
536 case BPF_FUNC_probe_read:
537 return &bpf_probe_read_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700538 case BPF_FUNC_ktime_get_ns:
539 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700540 case BPF_FUNC_tail_call:
541 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700542 case BPF_FUNC_get_current_pid_tgid:
543 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700544 case BPF_FUNC_get_current_task:
545 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700546 case BPF_FUNC_get_current_uid_gid:
547 return &bpf_get_current_uid_gid_proto;
548 case BPF_FUNC_get_current_comm:
549 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700550 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700551 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700552 case BPF_FUNC_get_smp_processor_id:
553 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200554 case BPF_FUNC_get_numa_node_id:
555 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000556 case BPF_FUNC_perf_event_read:
557 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700558 case BPF_FUNC_probe_write_user:
559 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700560 case BPF_FUNC_current_task_under_cgroup:
561 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700562 case BPF_FUNC_get_prandom_u32:
563 return &bpf_get_prandom_u32_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000564 case BPF_FUNC_probe_read_str:
565 return &bpf_probe_read_str_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700566#ifdef CONFIG_CGROUPS
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700567 case BPF_FUNC_get_current_cgroup_id:
568 return &bpf_get_current_cgroup_id_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700569#endif
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700570 default:
571 return NULL;
572 }
573}
574
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700575static const struct bpf_func_proto *
576kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700577{
578 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700579 case BPF_FUNC_perf_event_output:
580 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800581 case BPF_FUNC_get_stackid:
582 return &bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -0700583 case BPF_FUNC_get_stack:
584 return &bpf_get_stack_proto;
Yonghong Song908432c2017-10-05 09:19:20 -0700585 case BPF_FUNC_perf_event_read_value:
586 return &bpf_perf_event_read_value_proto;
Josef Bacik9802d862017-12-11 11:36:48 -0500587#ifdef CONFIG_BPF_KPROBE_OVERRIDE
588 case BPF_FUNC_override_return:
589 return &bpf_override_return_proto;
590#endif
Alexei Starovoitov25415172015-03-25 12:49:20 -0700591 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700592 return tracing_func_proto(func_id, prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700593 }
594}
595
596/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700597static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700598 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700599 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700600{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700601 if (off < 0 || off >= sizeof(struct pt_regs))
602 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700603 if (type != BPF_READ)
604 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700605 if (off % size != 0)
606 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100607 /*
608 * Assertion for 32 bit to make sure last 8 byte access
609 * (BPF_DW) to the last 4 byte member is disallowed.
610 */
611 if (off + size > sizeof(struct pt_regs))
612 return false;
613
Alexei Starovoitov25415172015-03-25 12:49:20 -0700614 return true;
615}
616
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700617const struct bpf_verifier_ops kprobe_verifier_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700618 .get_func_proto = kprobe_prog_func_proto,
619 .is_valid_access = kprobe_prog_is_valid_access,
620};
621
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700622const struct bpf_prog_ops kprobe_prog_ops = {
623};
624
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200625BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
626 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700627{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200628 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
629
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700630 /*
631 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
632 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200633 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700634 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200635 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700636}
637
638static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
639 .func = bpf_perf_event_output_tp,
640 .gpl_only = true,
641 .ret_type = RET_INTEGER,
642 .arg1_type = ARG_PTR_TO_CTX,
643 .arg2_type = ARG_CONST_MAP_PTR,
644 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800645 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000646 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700647};
648
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200649BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
650 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700651{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200652 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700653
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200654 /*
655 * Same comment as in bpf_perf_event_output_tp(), only that this time
656 * the other helper's function body cannot be inlined due to being
657 * external, thus we need to call raw helper function.
658 */
659 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
660 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700661}
662
663static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
664 .func = bpf_get_stackid_tp,
665 .gpl_only = true,
666 .ret_type = RET_INTEGER,
667 .arg1_type = ARG_PTR_TO_CTX,
668 .arg2_type = ARG_CONST_MAP_PTR,
669 .arg3_type = ARG_ANYTHING,
670};
671
Yonghong Songc195651e2018-04-28 22:28:08 -0700672BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
673 u64, flags)
674{
675 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
676
677 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
678 (unsigned long) size, flags, 0);
679}
680
681static const struct bpf_func_proto bpf_get_stack_proto_tp = {
682 .func = bpf_get_stack_tp,
683 .gpl_only = true,
684 .ret_type = RET_INTEGER,
685 .arg1_type = ARG_PTR_TO_CTX,
686 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
687 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
688 .arg4_type = ARG_ANYTHING,
689};
690
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700691static const struct bpf_func_proto *
692tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700693{
694 switch (func_id) {
695 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700696 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700697 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700698 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700699 case BPF_FUNC_get_stack:
700 return &bpf_get_stack_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700701 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700702 return tracing_func_proto(func_id, prog);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700703 }
704}
705
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700706static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700707 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700708 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700709{
710 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
711 return false;
712 if (type != BPF_READ)
713 return false;
714 if (off % size != 0)
715 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100716
717 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700718 return true;
719}
720
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700721const struct bpf_verifier_ops tracepoint_verifier_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700722 .get_func_proto = tp_prog_func_proto,
723 .is_valid_access = tp_prog_is_valid_access,
724};
725
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700726const struct bpf_prog_ops tracepoint_prog_ops = {
727};
728
Yonghong Songf005afe2018-03-20 11:19:17 -0700729BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
730 struct bpf_perf_event_value *, buf, u32, size)
731{
732 int err = -EINVAL;
733
734 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
735 goto clear;
736 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
737 &buf->running);
738 if (unlikely(err))
739 goto clear;
740 return 0;
741clear:
742 memset(buf, 0, size);
743 return err;
744}
745
746static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
747 .func = bpf_perf_prog_read_value,
748 .gpl_only = true,
749 .ret_type = RET_INTEGER,
750 .arg1_type = ARG_PTR_TO_CTX,
751 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
752 .arg3_type = ARG_CONST_SIZE,
753};
754
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700755static const struct bpf_func_proto *
756pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Yonghong Songf005afe2018-03-20 11:19:17 -0700757{
758 switch (func_id) {
759 case BPF_FUNC_perf_event_output:
760 return &bpf_perf_event_output_proto_tp;
761 case BPF_FUNC_get_stackid:
762 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700763 case BPF_FUNC_get_stack:
764 return &bpf_get_stack_proto_tp;
Yonghong Songf005afe2018-03-20 11:19:17 -0700765 case BPF_FUNC_perf_prog_read_value:
766 return &bpf_perf_prog_read_value_proto;
767 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700768 return tracing_func_proto(func_id, prog);
Yonghong Songf005afe2018-03-20 11:19:17 -0700769 }
770}
771
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700772/*
773 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
774 * to avoid potential recursive reuse issue when/if tracepoints are added
Yonghong Songc195651e2018-04-28 22:28:08 -0700775 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700776 */
777static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
778BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
779 struct bpf_map *, map, u64, flags, void *, data, u64, size)
780{
781 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
782
783 perf_fetch_caller_regs(regs);
784 return ____bpf_perf_event_output(regs, map, flags, data, size);
785}
786
787static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
788 .func = bpf_perf_event_output_raw_tp,
789 .gpl_only = true,
790 .ret_type = RET_INTEGER,
791 .arg1_type = ARG_PTR_TO_CTX,
792 .arg2_type = ARG_CONST_MAP_PTR,
793 .arg3_type = ARG_ANYTHING,
794 .arg4_type = ARG_PTR_TO_MEM,
795 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
796};
797
798BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
799 struct bpf_map *, map, u64, flags)
800{
801 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
802
803 perf_fetch_caller_regs(regs);
804 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
805 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
806 flags, 0, 0);
807}
808
809static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
810 .func = bpf_get_stackid_raw_tp,
811 .gpl_only = true,
812 .ret_type = RET_INTEGER,
813 .arg1_type = ARG_PTR_TO_CTX,
814 .arg2_type = ARG_CONST_MAP_PTR,
815 .arg3_type = ARG_ANYTHING,
816};
817
Yonghong Songc195651e2018-04-28 22:28:08 -0700818BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
819 void *, buf, u32, size, u64, flags)
820{
821 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
822
823 perf_fetch_caller_regs(regs);
824 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
825 (unsigned long) size, flags, 0);
826}
827
828static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
829 .func = bpf_get_stack_raw_tp,
830 .gpl_only = true,
831 .ret_type = RET_INTEGER,
832 .arg1_type = ARG_PTR_TO_CTX,
833 .arg2_type = ARG_PTR_TO_MEM,
834 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
835 .arg4_type = ARG_ANYTHING,
836};
837
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700838static const struct bpf_func_proto *
839raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700840{
841 switch (func_id) {
842 case BPF_FUNC_perf_event_output:
843 return &bpf_perf_event_output_proto_raw_tp;
844 case BPF_FUNC_get_stackid:
845 return &bpf_get_stackid_proto_raw_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700846 case BPF_FUNC_get_stack:
847 return &bpf_get_stack_proto_raw_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700848 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700849 return tracing_func_proto(func_id, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700850 }
851}
852
853static bool raw_tp_prog_is_valid_access(int off, int size,
854 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700855 const struct bpf_prog *prog,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700856 struct bpf_insn_access_aux *info)
857{
858 /* largest tracepoint in the kernel has 12 args */
859 if (off < 0 || off >= sizeof(__u64) * 12)
860 return false;
861 if (type != BPF_READ)
862 return false;
863 if (off % size != 0)
864 return false;
865 return true;
866}
867
868const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
869 .get_func_proto = raw_tp_prog_func_proto,
870 .is_valid_access = raw_tp_prog_is_valid_access,
871};
872
873const struct bpf_prog_ops raw_tracepoint_prog_ops = {
874};
875
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700876static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700877 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700878 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700879{
Teng Qin95da0cd2018-03-06 10:55:01 -0800880 const int size_u64 = sizeof(u64);
Yonghong Song31fd8582017-06-13 15:52:13 -0700881
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700882 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
883 return false;
884 if (type != BPF_READ)
885 return false;
Daniel Borkmannbc231052018-06-02 23:06:39 +0200886 if (off % size != 0) {
887 if (sizeof(unsigned long) != 4)
888 return false;
889 if (size != 8)
890 return false;
891 if (off % size != 4)
892 return false;
893 }
Yonghong Song31fd8582017-06-13 15:52:13 -0700894
Daniel Borkmannf96da092017-07-02 02:13:27 +0200895 switch (off) {
896 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
Teng Qin95da0cd2018-03-06 10:55:01 -0800897 bpf_ctx_record_field_size(info, size_u64);
898 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
899 return false;
900 break;
901 case bpf_ctx_range(struct bpf_perf_event_data, addr):
902 bpf_ctx_record_field_size(info, size_u64);
903 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
Yonghong Song23994632017-06-22 15:07:39 -0700904 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +0200905 break;
906 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700907 if (size != sizeof(long))
908 return false;
909 }
Daniel Borkmannf96da092017-07-02 02:13:27 +0200910
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700911 return true;
912}
913
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100914static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
915 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700916 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200917 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700918{
919 struct bpf_insn *insn = insn_buf;
920
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100921 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700922 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +0200923 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100924 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700925 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100926 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200927 bpf_target_off(struct perf_sample_data, period, 8,
928 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700929 break;
Teng Qin95da0cd2018-03-06 10:55:01 -0800930 case offsetof(struct bpf_perf_event_data, addr):
931 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
932 data), si->dst_reg, si->src_reg,
933 offsetof(struct bpf_perf_event_data_kern, data));
934 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
935 bpf_target_off(struct perf_sample_data, addr, 8,
936 target_size));
937 break;
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700938 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200939 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100940 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700941 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100942 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
943 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700944 break;
945 }
946
947 return insn - insn_buf;
948}
949
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700950const struct bpf_verifier_ops perf_event_verifier_ops = {
Yonghong Songf005afe2018-03-20 11:19:17 -0700951 .get_func_proto = pe_prog_func_proto,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700952 .is_valid_access = pe_prog_is_valid_access,
953 .convert_ctx_access = pe_prog_convert_ctx_access,
954};
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700955
956const struct bpf_prog_ops perf_event_prog_ops = {
957};
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700958
959static DEFINE_MUTEX(bpf_event_mutex);
960
Yonghong Songc8c088b2017-11-30 13:47:54 -0800961#define BPF_TRACE_MAX_PROGS 64
962
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700963int perf_event_attach_bpf_prog(struct perf_event *event,
964 struct bpf_prog *prog)
965{
966 struct bpf_prog_array __rcu *old_array;
967 struct bpf_prog_array *new_array;
968 int ret = -EEXIST;
969
Josef Bacik9802d862017-12-11 11:36:48 -0500970 /*
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900971 * Kprobe override only works if they are on the function entry,
972 * and only if they are on the opt-in list.
Josef Bacik9802d862017-12-11 11:36:48 -0500973 */
974 if (prog->kprobe_override &&
Masami Hiramatsub4da3342018-01-13 02:54:04 +0900975 (!trace_kprobe_on_func_entry(event->tp_event) ||
Josef Bacik9802d862017-12-11 11:36:48 -0500976 !trace_kprobe_error_injectable(event->tp_event)))
977 return -EINVAL;
978
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700979 mutex_lock(&bpf_event_mutex);
980
981 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -0700982 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700983
Yonghong Song07c41a22017-10-30 13:50:22 -0700984 old_array = event->tp_event->prog_array;
Yonghong Songc8c088b2017-11-30 13:47:54 -0800985 if (old_array &&
986 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
987 ret = -E2BIG;
988 goto unlock;
989 }
990
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700991 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
992 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -0700993 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -0700994
995 /* set the new array to event->tp_event and set event->prog */
996 event->prog = prog;
997 rcu_assign_pointer(event->tp_event->prog_array, new_array);
998 bpf_prog_array_free(old_array);
999
Yonghong Song07c41a22017-10-30 13:50:22 -07001000unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001001 mutex_unlock(&bpf_event_mutex);
1002 return ret;
1003}
1004
1005void perf_event_detach_bpf_prog(struct perf_event *event)
1006{
1007 struct bpf_prog_array __rcu *old_array;
1008 struct bpf_prog_array *new_array;
1009 int ret;
1010
1011 mutex_lock(&bpf_event_mutex);
1012
1013 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001014 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001015
Yonghong Song07c41a22017-10-30 13:50:22 -07001016 old_array = event->tp_event->prog_array;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001017 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
Sean Young170a7e32018-05-27 12:24:08 +01001018 if (ret == -ENOENT)
1019 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001020 if (ret < 0) {
1021 bpf_prog_array_delete_safe(old_array, event->prog);
1022 } else {
1023 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1024 bpf_prog_array_free(old_array);
1025 }
1026
1027 bpf_prog_put(event->prog);
1028 event->prog = NULL;
1029
Yonghong Song07c41a22017-10-30 13:50:22 -07001030unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001031 mutex_unlock(&bpf_event_mutex);
1032}
Yonghong Songf371b302017-12-11 11:39:02 -08001033
Yonghong Songf4e22982017-12-13 10:35:37 -08001034int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -08001035{
1036 struct perf_event_query_bpf __user *uquery = info;
1037 struct perf_event_query_bpf query = {};
Yonghong Song3a38bb92018-04-10 09:37:32 -07001038 u32 *ids, prog_cnt, ids_len;
Yonghong Songf371b302017-12-11 11:39:02 -08001039 int ret;
1040
1041 if (!capable(CAP_SYS_ADMIN))
1042 return -EPERM;
1043 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1044 return -EINVAL;
1045 if (copy_from_user(&query, uquery, sizeof(query)))
1046 return -EFAULT;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001047
1048 ids_len = query.ids_len;
1049 if (ids_len > BPF_TRACE_MAX_PROGS)
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001050 return -E2BIG;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001051 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1052 if (!ids)
1053 return -ENOMEM;
1054 /*
1055 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1056 * is required when user only wants to check for uquery->prog_cnt.
1057 * There is no need to check for it since the case is handled
1058 * gracefully in bpf_prog_array_copy_info.
1059 */
Yonghong Songf371b302017-12-11 11:39:02 -08001060
1061 mutex_lock(&bpf_event_mutex);
1062 ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001063 ids,
1064 ids_len,
1065 &prog_cnt);
Yonghong Songf371b302017-12-11 11:39:02 -08001066 mutex_unlock(&bpf_event_mutex);
1067
Yonghong Song3a38bb92018-04-10 09:37:32 -07001068 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1069 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1070 ret = -EFAULT;
1071
1072 kfree(ids);
Yonghong Songf371b302017-12-11 11:39:02 -08001073 return ret;
1074}
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001075
1076extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1077extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1078
1079struct bpf_raw_event_map *bpf_find_raw_tracepoint(const char *name)
1080{
1081 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1082
1083 for (; btp < __stop__bpf_raw_tp; btp++) {
1084 if (!strcmp(btp->tp->name, name))
1085 return btp;
1086 }
1087 return NULL;
1088}
1089
1090static __always_inline
1091void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1092{
1093 rcu_read_lock();
1094 preempt_disable();
1095 (void) BPF_PROG_RUN(prog, args);
1096 preempt_enable();
1097 rcu_read_unlock();
1098}
1099
1100#define UNPACK(...) __VA_ARGS__
1101#define REPEAT_1(FN, DL, X, ...) FN(X)
1102#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1103#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1104#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1105#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1106#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1107#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1108#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1109#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1110#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1111#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1112#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1113#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1114
1115#define SARG(X) u64 arg##X
1116#define COPY(X) args[X] = arg##X
1117
1118#define __DL_COM (,)
1119#define __DL_SEM (;)
1120
1121#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1122
1123#define BPF_TRACE_DEFN_x(x) \
1124 void bpf_trace_run##x(struct bpf_prog *prog, \
1125 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1126 { \
1127 u64 args[x]; \
1128 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1129 __bpf_trace_run(prog, args); \
1130 } \
1131 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1132BPF_TRACE_DEFN_x(1);
1133BPF_TRACE_DEFN_x(2);
1134BPF_TRACE_DEFN_x(3);
1135BPF_TRACE_DEFN_x(4);
1136BPF_TRACE_DEFN_x(5);
1137BPF_TRACE_DEFN_x(6);
1138BPF_TRACE_DEFN_x(7);
1139BPF_TRACE_DEFN_x(8);
1140BPF_TRACE_DEFN_x(9);
1141BPF_TRACE_DEFN_x(10);
1142BPF_TRACE_DEFN_x(11);
1143BPF_TRACE_DEFN_x(12);
1144
1145static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1146{
1147 struct tracepoint *tp = btp->tp;
1148
1149 /*
1150 * check that program doesn't access arguments beyond what's
1151 * available in this tracepoint
1152 */
1153 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1154 return -EINVAL;
1155
1156 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1157}
1158
1159int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1160{
1161 int err;
1162
1163 mutex_lock(&bpf_event_mutex);
1164 err = __bpf_probe_register(btp, prog);
1165 mutex_unlock(&bpf_event_mutex);
1166 return err;
1167}
1168
1169int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1170{
1171 int err;
1172
1173 mutex_lock(&bpf_event_mutex);
1174 err = tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
1175 mutex_unlock(&bpf_event_mutex);
1176 return err;
1177}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001178
1179int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1180 u32 *fd_type, const char **buf,
1181 u64 *probe_offset, u64 *probe_addr)
1182{
1183 bool is_tracepoint, is_syscall_tp;
1184 struct bpf_prog *prog;
1185 int flags, err = 0;
1186
1187 prog = event->prog;
1188 if (!prog)
1189 return -ENOENT;
1190
1191 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1192 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1193 return -EOPNOTSUPP;
1194
1195 *prog_id = prog->aux->id;
1196 flags = event->tp_event->flags;
1197 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1198 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1199
1200 if (is_tracepoint || is_syscall_tp) {
1201 *buf = is_tracepoint ? event->tp_event->tp->name
1202 : event->tp_event->name;
1203 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1204 *probe_offset = 0x0;
1205 *probe_addr = 0x0;
1206 } else {
1207 /* kprobe/uprobe */
1208 err = -EOPNOTSUPP;
1209#ifdef CONFIG_KPROBE_EVENTS
1210 if (flags & TRACE_EVENT_FL_KPROBE)
1211 err = bpf_get_kprobe_info(event, fd_type, buf,
1212 probe_offset, probe_addr,
1213 event->attr.type == PERF_TYPE_TRACEPOINT);
1214#endif
1215#ifdef CONFIG_UPROBE_EVENTS
1216 if (flags & TRACE_EVENT_FL_UPROBE)
1217 err = bpf_get_uprobe_info(event, fd_type, buf,
1218 probe_offset,
1219 event->attr.type == PERF_TYPE_TRACEPOINT);
1220#endif
1221 }
1222
1223 return err;
1224}