blob: 94b0e37d90ef7852886a62da2a03a3a25e798db0 [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
Nadav Amitc7b6f292019-04-25 17:11:43 -070017#include <asm/tlb.h>
18
Josef Bacik9802d862017-12-11 11:36:48 -050019#include "trace_probe.h"
Alexei Starovoitov25415172015-03-25 12:49:20 -070020#include "trace.h"
21
Matt Mullinsa38d1102018-12-12 16:42:37 -080022#ifdef CONFIG_MODULES
23struct bpf_trace_module {
24 struct module *module;
25 struct list_head list;
26};
27
28static LIST_HEAD(bpf_trace_modules);
29static DEFINE_MUTEX(bpf_module_mutex);
30
31static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
32{
33 struct bpf_raw_event_map *btp, *ret = NULL;
34 struct bpf_trace_module *btm;
35 unsigned int i;
36
37 mutex_lock(&bpf_module_mutex);
38 list_for_each_entry(btm, &bpf_trace_modules, list) {
39 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
40 btp = &btm->module->bpf_raw_events[i];
41 if (!strcmp(btp->tp->name, name)) {
42 if (try_module_get(btm->module))
43 ret = btp;
44 goto out;
45 }
46 }
47 }
48out:
49 mutex_unlock(&bpf_module_mutex);
50 return ret;
51}
52#else
53static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
54{
55 return NULL;
56}
57#endif /* CONFIG_MODULES */
58
Gianluca Borello035226b2017-10-26 01:47:42 +000059u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Yonghong Songc195651e2018-04-28 22:28:08 -070060u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
Gianluca Borello035226b2017-10-26 01:47:42 +000061
Alexei Starovoitov25415172015-03-25 12:49:20 -070062/**
63 * trace_call_bpf - invoke BPF program
Yonghong Songe87c6bc382017-10-23 23:53:08 -070064 * @call: tracepoint event
Alexei Starovoitov25415172015-03-25 12:49:20 -070065 * @ctx: opaque context pointer
66 *
67 * kprobe handlers execute BPF programs via this helper.
68 * Can be used from static tracepoints in the future.
69 *
70 * Return: BPF programs always return an integer which is interpreted by
71 * kprobe handler as:
72 * 0 - return from kprobe (event is filtered out)
73 * 1 - store kprobe event into ring buffer
74 * Other values are reserved and currently alias to 1
75 */
Yonghong Songe87c6bc382017-10-23 23:53:08 -070076unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
Alexei Starovoitov25415172015-03-25 12:49:20 -070077{
78 unsigned int ret;
79
80 if (in_nmi()) /* not supported yet */
81 return 1;
82
83 preempt_disable();
84
85 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
86 /*
87 * since some bpf program is already running on this cpu,
88 * don't call into another bpf program (same or different)
89 * and don't send kprobe event into ring-buffer,
90 * so return zero here
91 */
92 ret = 0;
93 goto out;
94 }
95
Yonghong Songe87c6bc382017-10-23 23:53:08 -070096 /*
97 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
98 * to all call sites, we did a bpf_prog_array_valid() there to check
99 * whether call->prog_array is empty or not, which is
100 * a heurisitc to speed up execution.
101 *
102 * If bpf_prog_array_valid() fetched prog_array was
103 * non-NULL, we go into trace_call_bpf() and do the actual
104 * proper rcu_dereference() under RCU lock.
105 * If it turns out that prog_array is NULL then, we bail out.
106 * For the opposite, if the bpf_prog_array_valid() fetched pointer
107 * was NULL, you'll skip the prog_array with the risk of missing
108 * out of events when it was updated in between this and the
109 * rcu_dereference() which is accepted risk.
110 */
111 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700112
113 out:
114 __this_cpu_dec(bpf_prog_active);
115 preempt_enable();
116
117 return ret;
118}
119EXPORT_SYMBOL_GPL(trace_call_bpf);
120
Josef Bacik9802d862017-12-11 11:36:48 -0500121#ifdef CONFIG_BPF_KPROBE_OVERRIDE
122BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
123{
Josef Bacik9802d862017-12-11 11:36:48 -0500124 regs_set_return_value(regs, rc);
Masami Hiramatsu540adea2018-01-13 02:55:03 +0900125 override_function_with_return(regs);
Josef Bacik9802d862017-12-11 11:36:48 -0500126 return 0;
127}
128
129static const struct bpf_func_proto bpf_override_return_proto = {
130 .func = bpf_override_return,
131 .gpl_only = true,
132 .ret_type = RET_INTEGER,
133 .arg1_type = ARG_PTR_TO_CTX,
134 .arg2_type = ARG_ANYTHING,
135};
136#endif
137
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200138BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700139{
Gianluca Borelloeb33f2c2017-11-22 18:32:54 +0000140 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700141
Daniel Borkmann074f528e2016-04-13 00:10:52 +0200142 ret = probe_kernel_read(dst, unsafe_ptr, size);
143 if (unlikely(ret < 0))
144 memset(dst, 0, size);
145
146 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700147}
148
149static const struct bpf_func_proto bpf_probe_read_proto = {
150 .func = bpf_probe_read,
151 .gpl_only = true,
152 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800153 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Yonghong Song9c019e22017-11-12 14:49:10 -0800154 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov25415172015-03-25 12:49:20 -0700155 .arg3_type = ARG_ANYTHING,
156};
157
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200158BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
159 u32, size)
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700160{
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700161 /*
162 * Ensure we're in user context which is safe for the helper to
163 * run. This helper has no business in a kthread.
164 *
165 * access_ok() should prevent writing to non-user memory, but in
166 * some situations (nommu, temporary switch, etc) access_ok() does
167 * not provide enough validation, hence the check on KERNEL_DS.
Nadav Amitc7b6f292019-04-25 17:11:43 -0700168 *
169 * nmi_uaccess_okay() ensures the probe is not run in an interim
170 * state, when the task or mm are switched. This is specifically
171 * required to prevent the use of temporary mm.
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700172 */
173
174 if (unlikely(in_interrupt() ||
175 current->flags & (PF_KTHREAD | PF_EXITING)))
176 return -EPERM;
Al Virodb68ce12017-03-20 21:08:07 -0400177 if (unlikely(uaccess_kernel()))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700178 return -EPERM;
Nadav Amitc7b6f292019-04-25 17:11:43 -0700179 if (unlikely(!nmi_uaccess_okay()))
180 return -EPERM;
Linus Torvalds96d4f262019-01-03 18:57:57 -0800181 if (!access_ok(unsafe_ptr, size))
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700182 return -EPERM;
183
184 return probe_kernel_write(unsafe_ptr, src, size);
185}
186
187static const struct bpf_func_proto bpf_probe_write_user_proto = {
188 .func = bpf_probe_write_user,
189 .gpl_only = true,
190 .ret_type = RET_INTEGER,
191 .arg1_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800192 .arg2_type = ARG_PTR_TO_MEM,
193 .arg3_type = ARG_CONST_SIZE,
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700194};
195
196static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
197{
198 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
199 current->comm, task_pid_nr(current));
200
201 return &bpf_probe_write_user_proto;
202}
203
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700204/*
John Fastabend7bda4b42017-07-02 02:13:29 +0200205 * Only limited trace_printk() conversion specifiers allowed:
206 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700207 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200208BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
209 u64, arg2, u64, arg3)
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700210{
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700211 bool str_seen = false;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700212 int mod[3] = {};
213 int fmt_cnt = 0;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700214 u64 unsafe_addr;
215 char buf[64];
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700216 int i;
217
218 /*
219 * bpf_check()->check_func_arg()->check_stack_boundary()
220 * guarantees that fmt points to bpf program stack,
221 * fmt_size bytes of it were initialized and fmt_size > 0
222 */
223 if (fmt[--fmt_size] != 0)
224 return -EINVAL;
225
226 /* check format string for allowed specifiers */
227 for (i = 0; i < fmt_size; i++) {
228 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
229 return -EINVAL;
230
231 if (fmt[i] != '%')
232 continue;
233
234 if (fmt_cnt >= 3)
235 return -EINVAL;
236
237 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
238 i++;
239 if (fmt[i] == 'l') {
240 mod[fmt_cnt]++;
241 i++;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700242 } else if (fmt[i] == 'p' || fmt[i] == 's') {
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700243 mod[fmt_cnt]++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100244 /* disallow any further format extensions */
245 if (fmt[i + 1] != 0 &&
246 !isspace(fmt[i + 1]) &&
247 !ispunct(fmt[i + 1]))
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700248 return -EINVAL;
249 fmt_cnt++;
Martynas Pumputis1efb6ee2018-11-23 17:43:26 +0100250 if (fmt[i] == 's') {
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700251 if (str_seen)
252 /* allow only one '%s' per fmt string */
253 return -EINVAL;
254 str_seen = true;
255
256 switch (fmt_cnt) {
257 case 1:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200258 unsafe_addr = arg1;
259 arg1 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700260 break;
261 case 2:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200262 unsafe_addr = arg2;
263 arg2 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700264 break;
265 case 3:
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200266 unsafe_addr = arg3;
267 arg3 = (long) buf;
Alexei Starovoitov8d3b7dc2015-08-28 15:56:23 -0700268 break;
269 }
270 buf[0] = 0;
271 strncpy_from_unsafe(buf,
272 (void *) (long) unsafe_addr,
273 sizeof(buf));
274 }
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700275 continue;
276 }
277
278 if (fmt[i] == 'l') {
279 mod[fmt_cnt]++;
280 i++;
281 }
282
John Fastabend7bda4b42017-07-02 02:13:29 +0200283 if (fmt[i] != 'i' && fmt[i] != 'd' &&
284 fmt[i] != 'u' && fmt[i] != 'x')
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700285 return -EINVAL;
286 fmt_cnt++;
287 }
288
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200289/* Horrid workaround for getting va_list handling working with different
290 * argument type combinations generically for 32 and 64 bit archs.
291 */
292#define __BPF_TP_EMIT() __BPF_ARG3_TP()
293#define __BPF_TP(...) \
Yonghong Songeefa864a2018-01-17 09:19:32 -0800294 __trace_printk(0 /* Fake ip */, \
Daniel Borkmann88a5c692017-08-16 01:45:33 +0200295 fmt, ##__VA_ARGS__)
296
297#define __BPF_ARG1_TP(...) \
298 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \
299 ? __BPF_TP(arg1, ##__VA_ARGS__) \
300 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \
301 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \
302 : __BPF_TP((u32)arg1, ##__VA_ARGS__)))
303
304#define __BPF_ARG2_TP(...) \
305 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \
306 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \
307 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \
308 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \
309 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__)))
310
311#define __BPF_ARG3_TP(...) \
312 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \
313 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \
314 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \
315 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \
316 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__)))
317
318 return __BPF_TP_EMIT();
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700319}
320
321static const struct bpf_func_proto bpf_trace_printk_proto = {
322 .func = bpf_trace_printk,
323 .gpl_only = true,
324 .ret_type = RET_INTEGER,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800325 .arg1_type = ARG_PTR_TO_MEM,
326 .arg2_type = ARG_CONST_SIZE,
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700327};
328
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700329const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
330{
331 /*
332 * this program might be calling bpf_trace_printk,
333 * so allocate per-cpu printk buffers
334 */
335 trace_printk_init_buffers();
336
337 return &bpf_trace_printk_proto;
338}
339
Yonghong Song908432c2017-10-05 09:19:20 -0700340static __always_inline int
341get_map_perf_counter(struct bpf_map *map, u64 flags,
342 u64 *value, u64 *enabled, u64 *running)
Kaixu Xia35578d72015-08-06 07:02:35 +0000343{
Kaixu Xia35578d72015-08-06 07:02:35 +0000344 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200345 unsigned int cpu = smp_processor_id();
346 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200347 struct bpf_event_entry *ee;
Kaixu Xia35578d72015-08-06 07:02:35 +0000348
Daniel Borkmann6816a7f2016-06-28 12:18:25 +0200349 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
350 return -EINVAL;
351 if (index == BPF_F_CURRENT_CPU)
352 index = cpu;
Kaixu Xia35578d72015-08-06 07:02:35 +0000353 if (unlikely(index >= array->map.max_entries))
354 return -E2BIG;
355
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200356 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200357 if (!ee)
Kaixu Xia35578d72015-08-06 07:02:35 +0000358 return -ENOENT;
359
Yonghong Song908432c2017-10-05 09:19:20 -0700360 return perf_event_read_local(ee->event, value, enabled, running);
361}
362
363BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
364{
365 u64 value = 0;
366 int err;
367
368 err = get_map_perf_counter(map, flags, &value, NULL, NULL);
Kaixu Xia35578d72015-08-06 07:02:35 +0000369 /*
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700370 * this api is ugly since we miss [-22..-2] range of valid
371 * counter values, but that's uapi
Kaixu Xia35578d72015-08-06 07:02:35 +0000372 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700373 if (err)
374 return err;
375 return value;
Kaixu Xia35578d72015-08-06 07:02:35 +0000376}
377
Alexei Starovoitov62544ce2015-10-22 17:10:14 -0700378static const struct bpf_func_proto bpf_perf_event_read_proto = {
Kaixu Xia35578d72015-08-06 07:02:35 +0000379 .func = bpf_perf_event_read,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700380 .gpl_only = true,
Kaixu Xia35578d72015-08-06 07:02:35 +0000381 .ret_type = RET_INTEGER,
382 .arg1_type = ARG_CONST_MAP_PTR,
383 .arg2_type = ARG_ANYTHING,
384};
385
Yonghong Song908432c2017-10-05 09:19:20 -0700386BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
387 struct bpf_perf_event_value *, buf, u32, size)
388{
389 int err = -EINVAL;
390
391 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
392 goto clear;
393 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
394 &buf->running);
395 if (unlikely(err))
396 goto clear;
397 return 0;
398clear:
399 memset(buf, 0, size);
400 return err;
401}
402
403static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
404 .func = bpf_perf_event_read_value,
405 .gpl_only = true,
406 .ret_type = RET_INTEGER,
407 .arg1_type = ARG_CONST_MAP_PTR,
408 .arg2_type = ARG_ANYTHING,
409 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
410 .arg4_type = ARG_CONST_SIZE,
411};
412
Daniel Borkmann283ca522017-12-12 02:25:30 +0100413static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd);
Daniel Borkmann20b9d7a2017-06-11 00:50:40 +0200414
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200415static __always_inline u64
416__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
Daniel Borkmann283ca522017-12-12 02:25:30 +0100417 u64 flags, struct perf_sample_data *sd)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700418{
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700419 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmannd7931332016-06-28 12:18:24 +0200420 unsigned int cpu = smp_processor_id();
Daniel Borkmann1e337592016-04-18 21:01:23 +0200421 u64 index = flags & BPF_F_INDEX_MASK;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200422 struct bpf_event_entry *ee;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700423 struct perf_event *event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700424
Daniel Borkmann1e337592016-04-18 21:01:23 +0200425 if (index == BPF_F_CURRENT_CPU)
Daniel Borkmannd7931332016-06-28 12:18:24 +0200426 index = cpu;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700427 if (unlikely(index >= array->map.max_entries))
428 return -E2BIG;
429
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200430 ee = READ_ONCE(array->ptrs[index]);
Daniel Borkmann1ca1cc92016-06-28 12:18:23 +0200431 if (!ee)
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700432 return -ENOENT;
433
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200434 event = ee->event;
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700435 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
436 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
437 return -EINVAL;
438
Daniel Borkmannd7931332016-06-28 12:18:24 +0200439 if (unlikely(event->oncpu != cpu))
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700440 return -EOPNOTSUPP;
441
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -0300442 return perf_event_output(event, sd, regs);
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700443}
444
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200445BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
446 u64, flags, void *, data, u64, size)
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200447{
Daniel Borkmann283ca522017-12-12 02:25:30 +0100448 struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200449 struct perf_raw_record raw = {
450 .frag = {
451 .size = size,
452 .data = data,
453 },
454 };
455
456 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
457 return -EINVAL;
458
Daniel Borkmann283ca522017-12-12 02:25:30 +0100459 perf_sample_data_init(sd, 0, 0);
460 sd->raw = &raw;
461
462 return __bpf_perf_event_output(regs, map, flags, sd);
Daniel Borkmann8e7a3922016-07-14 18:08:04 +0200463}
464
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700465static const struct bpf_func_proto bpf_perf_event_output_proto = {
466 .func = bpf_perf_event_output,
Alexei Starovoitov1075ef52015-10-23 14:58:19 -0700467 .gpl_only = true,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700468 .ret_type = RET_INTEGER,
469 .arg1_type = ARG_PTR_TO_CTX,
470 .arg2_type = ARG_CONST_MAP_PTR,
471 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800472 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000473 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700474};
475
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200476static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100477static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200478
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200479u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
480 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200481{
Daniel Borkmann283ca522017-12-12 02:25:30 +0100482 struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200483 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200484 struct perf_raw_frag frag = {
485 .copy = ctx_copy,
486 .size = ctx_size,
487 .data = ctx,
488 };
489 struct perf_raw_record raw = {
490 .frag = {
Andrew Morton183fc152016-07-18 15:50:58 -0700491 {
492 .next = ctx_size ? &frag : NULL,
493 },
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200494 .size = meta_size,
495 .data = meta,
496 },
497 };
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200498
499 perf_fetch_caller_regs(regs);
Daniel Borkmann283ca522017-12-12 02:25:30 +0100500 perf_sample_data_init(sd, 0, 0);
501 sd->raw = &raw;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200502
Daniel Borkmann283ca522017-12-12 02:25:30 +0100503 return __bpf_perf_event_output(regs, map, flags, sd);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200504}
505
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200506BPF_CALL_0(bpf_get_current_task)
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700507{
508 return (long) current;
509}
510
511static const struct bpf_func_proto bpf_get_current_task_proto = {
512 .func = bpf_get_current_task,
513 .gpl_only = true,
514 .ret_type = RET_INTEGER,
515};
516
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200517BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700518{
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700519 struct bpf_array *array = container_of(map, struct bpf_array, map);
520 struct cgroup *cgrp;
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700521
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700522 if (unlikely(idx >= array->map.max_entries))
523 return -E2BIG;
524
525 cgrp = READ_ONCE(array->ptrs[idx]);
526 if (unlikely(!cgrp))
527 return -EAGAIN;
528
529 return task_under_cgroup_hierarchy(current, cgrp);
530}
531
532static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
533 .func = bpf_current_task_under_cgroup,
534 .gpl_only = false,
535 .ret_type = RET_INTEGER,
536 .arg1_type = ARG_CONST_MAP_PTR,
537 .arg2_type = ARG_ANYTHING,
538};
539
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000540BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
541 const void *, unsafe_ptr)
542{
543 int ret;
544
545 /*
546 * The strncpy_from_unsafe() call will likely not fill the entire
547 * buffer, but that's okay in this circumstance as we're probing
548 * arbitrary memory anyway similar to bpf_probe_read() and might
549 * as well probe the stack. Thus, memory is explicitly cleared
550 * only in error case, so that improper users ignoring return
551 * code altogether don't copy garbage; otherwise length of string
552 * is returned that can be used for bpf_perf_event_output() et al.
553 */
554 ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
555 if (unlikely(ret < 0))
556 memset(dst, 0, size);
557
558 return ret;
559}
560
561static const struct bpf_func_proto bpf_probe_read_str_proto = {
562 .func = bpf_probe_read_str,
563 .gpl_only = true,
564 .ret_type = RET_INTEGER,
565 .arg1_type = ARG_PTR_TO_UNINIT_MEM,
Gianluca Borello5c4e1202017-11-22 18:32:55 +0000566 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000567 .arg3_type = ARG_ANYTHING,
568};
569
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700570static const struct bpf_func_proto *
571tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700572{
573 switch (func_id) {
574 case BPF_FUNC_map_lookup_elem:
575 return &bpf_map_lookup_elem_proto;
576 case BPF_FUNC_map_update_elem:
577 return &bpf_map_update_elem_proto;
578 case BPF_FUNC_map_delete_elem:
579 return &bpf_map_delete_elem_proto;
580 case BPF_FUNC_probe_read:
581 return &bpf_probe_read_proto;
Alexei Starovoitovd9847d32015-03-25 12:49:21 -0700582 case BPF_FUNC_ktime_get_ns:
583 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700584 case BPF_FUNC_tail_call:
585 return &bpf_tail_call_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700586 case BPF_FUNC_get_current_pid_tgid:
587 return &bpf_get_current_pid_tgid_proto;
Alexei Starovoitov606274c2016-07-06 22:38:36 -0700588 case BPF_FUNC_get_current_task:
589 return &bpf_get_current_task_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700590 case BPF_FUNC_get_current_uid_gid:
591 return &bpf_get_current_uid_gid_proto;
592 case BPF_FUNC_get_current_comm:
593 return &bpf_get_current_comm_proto;
Alexei Starovoitov9c959c82015-03-25 12:49:22 -0700594 case BPF_FUNC_trace_printk:
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700595 return bpf_get_trace_printk_proto();
Alexei Starovoitovab1973d2015-06-12 19:39:14 -0700596 case BPF_FUNC_get_smp_processor_id:
597 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200598 case BPF_FUNC_get_numa_node_id:
599 return &bpf_get_numa_node_id_proto;
Kaixu Xia35578d72015-08-06 07:02:35 +0000600 case BPF_FUNC_perf_event_read:
601 return &bpf_perf_event_read_proto;
Sargun Dhillon96ae5222016-07-25 05:54:46 -0700602 case BPF_FUNC_probe_write_user:
603 return bpf_get_probe_write_proto();
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700604 case BPF_FUNC_current_task_under_cgroup:
605 return &bpf_current_task_under_cgroup_proto;
Alexei Starovoitov8937bd82016-08-11 18:17:18 -0700606 case BPF_FUNC_get_prandom_u32:
607 return &bpf_get_prandom_u32_proto;
Gianluca Borelloa5e8c072017-01-18 17:55:49 +0000608 case BPF_FUNC_probe_read_str:
609 return &bpf_probe_read_str_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700610#ifdef CONFIG_CGROUPS
Yonghong Songbf6fa2c82018-06-03 15:59:41 -0700611 case BPF_FUNC_get_current_cgroup_id:
612 return &bpf_get_current_cgroup_id_proto;
Yonghong Song34ea38c2018-06-04 08:53:41 -0700613#endif
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700614 default:
615 return NULL;
616 }
617}
618
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700619static const struct bpf_func_proto *
620kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700621{
622 switch (func_id) {
Alexei Starovoitova43eec32015-10-20 20:02:34 -0700623 case BPF_FUNC_perf_event_output:
624 return &bpf_perf_event_output_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800625 case BPF_FUNC_get_stackid:
626 return &bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -0700627 case BPF_FUNC_get_stack:
628 return &bpf_get_stack_proto;
Yonghong Song908432c2017-10-05 09:19:20 -0700629 case BPF_FUNC_perf_event_read_value:
630 return &bpf_perf_event_read_value_proto;
Josef Bacik9802d862017-12-11 11:36:48 -0500631#ifdef CONFIG_BPF_KPROBE_OVERRIDE
632 case BPF_FUNC_override_return:
633 return &bpf_override_return_proto;
634#endif
Alexei Starovoitov25415172015-03-25 12:49:20 -0700635 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700636 return tracing_func_proto(func_id, prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -0700637 }
638}
639
640/* bpf+kprobe programs can access fields of 'struct pt_regs' */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700641static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700642 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700643 struct bpf_insn_access_aux *info)
Alexei Starovoitov25415172015-03-25 12:49:20 -0700644{
Alexei Starovoitov25415172015-03-25 12:49:20 -0700645 if (off < 0 || off >= sizeof(struct pt_regs))
646 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700647 if (type != BPF_READ)
648 return false;
Alexei Starovoitov25415172015-03-25 12:49:20 -0700649 if (off % size != 0)
650 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100651 /*
652 * Assertion for 32 bit to make sure last 8 byte access
653 * (BPF_DW) to the last 4 byte member is disallowed.
654 */
655 if (off + size > sizeof(struct pt_regs))
656 return false;
657
Alexei Starovoitov25415172015-03-25 12:49:20 -0700658 return true;
659}
660
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700661const struct bpf_verifier_ops kprobe_verifier_ops = {
Alexei Starovoitov25415172015-03-25 12:49:20 -0700662 .get_func_proto = kprobe_prog_func_proto,
663 .is_valid_access = kprobe_prog_is_valid_access,
664};
665
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700666const struct bpf_prog_ops kprobe_prog_ops = {
667};
668
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200669BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
670 u64, flags, void *, data, u64, size)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700671{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200672 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
673
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700674 /*
675 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
676 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200677 * from there and call the same bpf_perf_event_output() helper inline.
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700678 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200679 return ____bpf_perf_event_output(regs, map, flags, data, size);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700680}
681
682static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
683 .func = bpf_perf_event_output_tp,
684 .gpl_only = true,
685 .ret_type = RET_INTEGER,
686 .arg1_type = ARG_PTR_TO_CTX,
687 .arg2_type = ARG_CONST_MAP_PTR,
688 .arg3_type = ARG_ANYTHING,
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800689 .arg4_type = ARG_PTR_TO_MEM,
Gianluca Borelloa60dd352017-11-22 18:32:56 +0000690 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700691};
692
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200693BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
694 u64, flags)
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700695{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200696 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700697
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200698 /*
699 * Same comment as in bpf_perf_event_output_tp(), only that this time
700 * the other helper's function body cannot be inlined due to being
701 * external, thus we need to call raw helper function.
702 */
703 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
704 flags, 0, 0);
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700705}
706
707static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
708 .func = bpf_get_stackid_tp,
709 .gpl_only = true,
710 .ret_type = RET_INTEGER,
711 .arg1_type = ARG_PTR_TO_CTX,
712 .arg2_type = ARG_CONST_MAP_PTR,
713 .arg3_type = ARG_ANYTHING,
714};
715
Yonghong Songc195651e2018-04-28 22:28:08 -0700716BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
717 u64, flags)
718{
719 struct pt_regs *regs = *(struct pt_regs **)tp_buff;
720
721 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
722 (unsigned long) size, flags, 0);
723}
724
725static const struct bpf_func_proto bpf_get_stack_proto_tp = {
726 .func = bpf_get_stack_tp,
727 .gpl_only = true,
728 .ret_type = RET_INTEGER,
729 .arg1_type = ARG_PTR_TO_CTX,
730 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
731 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
732 .arg4_type = ARG_ANYTHING,
733};
734
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700735static const struct bpf_func_proto *
736tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700737{
738 switch (func_id) {
739 case BPF_FUNC_perf_event_output:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700740 return &bpf_perf_event_output_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700741 case BPF_FUNC_get_stackid:
Alexei Starovoitov9940d672016-04-06 18:43:27 -0700742 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700743 case BPF_FUNC_get_stack:
744 return &bpf_get_stack_proto_tp;
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700745 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700746 return tracing_func_proto(func_id, prog);
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700747 }
748}
749
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700750static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700751 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700752 struct bpf_insn_access_aux *info)
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700753{
754 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
755 return false;
756 if (type != BPF_READ)
757 return false;
758 if (off % size != 0)
759 return false;
Daniel Borkmann2d071c62017-01-15 01:34:25 +0100760
761 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700762 return true;
763}
764
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700765const struct bpf_verifier_ops tracepoint_verifier_ops = {
Alexei Starovoitov9fd82b612016-04-06 18:43:26 -0700766 .get_func_proto = tp_prog_func_proto,
767 .is_valid_access = tp_prog_is_valid_access,
768};
769
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700770const struct bpf_prog_ops tracepoint_prog_ops = {
771};
772
Yonghong Songf005afe2018-03-20 11:19:17 -0700773BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
774 struct bpf_perf_event_value *, buf, u32, size)
775{
776 int err = -EINVAL;
777
778 if (unlikely(size != sizeof(struct bpf_perf_event_value)))
779 goto clear;
780 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
781 &buf->running);
782 if (unlikely(err))
783 goto clear;
784 return 0;
785clear:
786 memset(buf, 0, size);
787 return err;
788}
789
790static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
791 .func = bpf_perf_prog_read_value,
792 .gpl_only = true,
793 .ret_type = RET_INTEGER,
794 .arg1_type = ARG_PTR_TO_CTX,
795 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
796 .arg3_type = ARG_CONST_SIZE,
797};
798
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700799static const struct bpf_func_proto *
800pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Yonghong Songf005afe2018-03-20 11:19:17 -0700801{
802 switch (func_id) {
803 case BPF_FUNC_perf_event_output:
804 return &bpf_perf_event_output_proto_tp;
805 case BPF_FUNC_get_stackid:
806 return &bpf_get_stackid_proto_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700807 case BPF_FUNC_get_stack:
808 return &bpf_get_stack_proto_tp;
Yonghong Songf005afe2018-03-20 11:19:17 -0700809 case BPF_FUNC_perf_prog_read_value:
810 return &bpf_perf_prog_read_value_proto;
811 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700812 return tracing_func_proto(func_id, prog);
Yonghong Songf005afe2018-03-20 11:19:17 -0700813 }
814}
815
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700816/*
817 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
818 * to avoid potential recursive reuse issue when/if tracepoints are added
Yonghong Songc195651e2018-04-28 22:28:08 -0700819 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700820 */
821static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs);
822BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
823 struct bpf_map *, map, u64, flags, void *, data, u64, size)
824{
825 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
826
827 perf_fetch_caller_regs(regs);
828 return ____bpf_perf_event_output(regs, map, flags, data, size);
829}
830
831static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
832 .func = bpf_perf_event_output_raw_tp,
833 .gpl_only = true,
834 .ret_type = RET_INTEGER,
835 .arg1_type = ARG_PTR_TO_CTX,
836 .arg2_type = ARG_CONST_MAP_PTR,
837 .arg3_type = ARG_ANYTHING,
838 .arg4_type = ARG_PTR_TO_MEM,
839 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
840};
841
842BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
843 struct bpf_map *, map, u64, flags)
844{
845 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
846
847 perf_fetch_caller_regs(regs);
848 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
849 return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
850 flags, 0, 0);
851}
852
853static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
854 .func = bpf_get_stackid_raw_tp,
855 .gpl_only = true,
856 .ret_type = RET_INTEGER,
857 .arg1_type = ARG_PTR_TO_CTX,
858 .arg2_type = ARG_CONST_MAP_PTR,
859 .arg3_type = ARG_ANYTHING,
860};
861
Yonghong Songc195651e2018-04-28 22:28:08 -0700862BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
863 void *, buf, u32, size, u64, flags)
864{
865 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs);
866
867 perf_fetch_caller_regs(regs);
868 return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
869 (unsigned long) size, flags, 0);
870}
871
872static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
873 .func = bpf_get_stack_raw_tp,
874 .gpl_only = true,
875 .ret_type = RET_INTEGER,
876 .arg1_type = ARG_PTR_TO_CTX,
877 .arg2_type = ARG_PTR_TO_MEM,
878 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
879 .arg4_type = ARG_ANYTHING,
880};
881
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700882static const struct bpf_func_proto *
883raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700884{
885 switch (func_id) {
886 case BPF_FUNC_perf_event_output:
887 return &bpf_perf_event_output_proto_raw_tp;
888 case BPF_FUNC_get_stackid:
889 return &bpf_get_stackid_proto_raw_tp;
Yonghong Songc195651e2018-04-28 22:28:08 -0700890 case BPF_FUNC_get_stack:
891 return &bpf_get_stack_proto_raw_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700892 default:
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700893 return tracing_func_proto(func_id, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700894 }
895}
896
897static bool raw_tp_prog_is_valid_access(int off, int size,
898 enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700899 const struct bpf_prog *prog,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -0700900 struct bpf_insn_access_aux *info)
901{
902 /* largest tracepoint in the kernel has 12 args */
903 if (off < 0 || off >= sizeof(__u64) * 12)
904 return false;
905 if (type != BPF_READ)
906 return false;
907 if (off % size != 0)
908 return false;
909 return true;
910}
911
912const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
913 .get_func_proto = raw_tp_prog_func_proto,
914 .is_valid_access = raw_tp_prog_is_valid_access,
915};
916
917const struct bpf_prog_ops raw_tracepoint_prog_ops = {
918};
919
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700920static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700921 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700922 struct bpf_insn_access_aux *info)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700923{
Teng Qin95da0cd2018-03-06 10:55:01 -0800924 const int size_u64 = sizeof(u64);
Yonghong Song31fd8582017-06-13 15:52:13 -0700925
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700926 if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
927 return false;
928 if (type != BPF_READ)
929 return false;
Daniel Borkmannbc231052018-06-02 23:06:39 +0200930 if (off % size != 0) {
931 if (sizeof(unsigned long) != 4)
932 return false;
933 if (size != 8)
934 return false;
935 if (off % size != 4)
936 return false;
937 }
Yonghong Song31fd8582017-06-13 15:52:13 -0700938
Daniel Borkmannf96da092017-07-02 02:13:27 +0200939 switch (off) {
940 case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
Teng Qin95da0cd2018-03-06 10:55:01 -0800941 bpf_ctx_record_field_size(info, size_u64);
942 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
943 return false;
944 break;
945 case bpf_ctx_range(struct bpf_perf_event_data, addr):
946 bpf_ctx_record_field_size(info, size_u64);
947 if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
Yonghong Song23994632017-06-22 15:07:39 -0700948 return false;
Daniel Borkmannf96da092017-07-02 02:13:27 +0200949 break;
950 default:
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700951 if (size != sizeof(long))
952 return false;
953 }
Daniel Borkmannf96da092017-07-02 02:13:27 +0200954
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700955 return true;
956}
957
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100958static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
959 const struct bpf_insn *si,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700960 struct bpf_insn *insn_buf,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200961 struct bpf_prog *prog, u32 *target_size)
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700962{
963 struct bpf_insn *insn = insn_buf;
964
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100965 switch (si->off) {
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700966 case offsetof(struct bpf_perf_event_data, sample_period):
Daniel Borkmannf035a512016-09-09 02:45:29 +0200967 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100968 data), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700969 offsetof(struct bpf_perf_event_data_kern, data));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100970 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200971 bpf_target_off(struct perf_sample_data, period, 8,
972 target_size));
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700973 break;
Teng Qin95da0cd2018-03-06 10:55:01 -0800974 case offsetof(struct bpf_perf_event_data, addr):
975 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
976 data), si->dst_reg, si->src_reg,
977 offsetof(struct bpf_perf_event_data_kern, data));
978 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
979 bpf_target_off(struct perf_sample_data, addr, 8,
980 target_size));
981 break;
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700982 default:
Daniel Borkmannf035a512016-09-09 02:45:29 +0200983 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100984 regs), si->dst_reg, si->src_reg,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700985 offsetof(struct bpf_perf_event_data_kern, regs));
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100986 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
987 si->off);
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700988 break;
989 }
990
991 return insn - insn_buf;
992}
993
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700994const struct bpf_verifier_ops perf_event_verifier_ops = {
Yonghong Songf005afe2018-03-20 11:19:17 -0700995 .get_func_proto = pe_prog_func_proto,
Alexei Starovoitov0515e592016-09-01 18:37:22 -0700996 .is_valid_access = pe_prog_is_valid_access,
997 .convert_ctx_access = pe_prog_convert_ctx_access,
998};
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700999
1000const struct bpf_prog_ops perf_event_prog_ops = {
1001};
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001002
1003static DEFINE_MUTEX(bpf_event_mutex);
1004
Yonghong Songc8c088b2017-11-30 13:47:54 -08001005#define BPF_TRACE_MAX_PROGS 64
1006
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001007int perf_event_attach_bpf_prog(struct perf_event *event,
1008 struct bpf_prog *prog)
1009{
1010 struct bpf_prog_array __rcu *old_array;
1011 struct bpf_prog_array *new_array;
1012 int ret = -EEXIST;
1013
Josef Bacik9802d862017-12-11 11:36:48 -05001014 /*
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001015 * Kprobe override only works if they are on the function entry,
1016 * and only if they are on the opt-in list.
Josef Bacik9802d862017-12-11 11:36:48 -05001017 */
1018 if (prog->kprobe_override &&
Masami Hiramatsub4da3342018-01-13 02:54:04 +09001019 (!trace_kprobe_on_func_entry(event->tp_event) ||
Josef Bacik9802d862017-12-11 11:36:48 -05001020 !trace_kprobe_error_injectable(event->tp_event)))
1021 return -EINVAL;
1022
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001023 mutex_lock(&bpf_event_mutex);
1024
1025 if (event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001026 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001027
Yonghong Song07c41a22017-10-30 13:50:22 -07001028 old_array = event->tp_event->prog_array;
Yonghong Songc8c088b2017-11-30 13:47:54 -08001029 if (old_array &&
1030 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1031 ret = -E2BIG;
1032 goto unlock;
1033 }
1034
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001035 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
1036 if (ret < 0)
Yonghong Song07c41a22017-10-30 13:50:22 -07001037 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001038
1039 /* set the new array to event->tp_event and set event->prog */
1040 event->prog = prog;
1041 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1042 bpf_prog_array_free(old_array);
1043
Yonghong Song07c41a22017-10-30 13:50:22 -07001044unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001045 mutex_unlock(&bpf_event_mutex);
1046 return ret;
1047}
1048
1049void perf_event_detach_bpf_prog(struct perf_event *event)
1050{
1051 struct bpf_prog_array __rcu *old_array;
1052 struct bpf_prog_array *new_array;
1053 int ret;
1054
1055 mutex_lock(&bpf_event_mutex);
1056
1057 if (!event->prog)
Yonghong Song07c41a22017-10-30 13:50:22 -07001058 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001059
Yonghong Song07c41a22017-10-30 13:50:22 -07001060 old_array = event->tp_event->prog_array;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001061 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array);
Sean Young170a7e32018-05-27 12:24:08 +01001062 if (ret == -ENOENT)
1063 goto unlock;
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001064 if (ret < 0) {
1065 bpf_prog_array_delete_safe(old_array, event->prog);
1066 } else {
1067 rcu_assign_pointer(event->tp_event->prog_array, new_array);
1068 bpf_prog_array_free(old_array);
1069 }
1070
1071 bpf_prog_put(event->prog);
1072 event->prog = NULL;
1073
Yonghong Song07c41a22017-10-30 13:50:22 -07001074unlock:
Yonghong Songe87c6bc382017-10-23 23:53:08 -07001075 mutex_unlock(&bpf_event_mutex);
1076}
Yonghong Songf371b302017-12-11 11:39:02 -08001077
Yonghong Songf4e22982017-12-13 10:35:37 -08001078int perf_event_query_prog_array(struct perf_event *event, void __user *info)
Yonghong Songf371b302017-12-11 11:39:02 -08001079{
1080 struct perf_event_query_bpf __user *uquery = info;
1081 struct perf_event_query_bpf query = {};
Yonghong Song3a38bb92018-04-10 09:37:32 -07001082 u32 *ids, prog_cnt, ids_len;
Yonghong Songf371b302017-12-11 11:39:02 -08001083 int ret;
1084
1085 if (!capable(CAP_SYS_ADMIN))
1086 return -EPERM;
1087 if (event->attr.type != PERF_TYPE_TRACEPOINT)
1088 return -EINVAL;
1089 if (copy_from_user(&query, uquery, sizeof(query)))
1090 return -EFAULT;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001091
1092 ids_len = query.ids_len;
1093 if (ids_len > BPF_TRACE_MAX_PROGS)
Daniel Borkmann9c481b92018-02-14 15:31:00 +01001094 return -E2BIG;
Yonghong Song3a38bb92018-04-10 09:37:32 -07001095 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1096 if (!ids)
1097 return -ENOMEM;
1098 /*
1099 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1100 * is required when user only wants to check for uquery->prog_cnt.
1101 * There is no need to check for it since the case is handled
1102 * gracefully in bpf_prog_array_copy_info.
1103 */
Yonghong Songf371b302017-12-11 11:39:02 -08001104
1105 mutex_lock(&bpf_event_mutex);
1106 ret = bpf_prog_array_copy_info(event->tp_event->prog_array,
Yonghong Song3a38bb92018-04-10 09:37:32 -07001107 ids,
1108 ids_len,
1109 &prog_cnt);
Yonghong Songf371b302017-12-11 11:39:02 -08001110 mutex_unlock(&bpf_event_mutex);
1111
Yonghong Song3a38bb92018-04-10 09:37:32 -07001112 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1113 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1114 ret = -EFAULT;
1115
1116 kfree(ids);
Yonghong Songf371b302017-12-11 11:39:02 -08001117 return ret;
1118}
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001119
1120extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1121extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1122
Matt Mullinsa38d1102018-12-12 16:42:37 -08001123struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001124{
1125 struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
1126
1127 for (; btp < __stop__bpf_raw_tp; btp++) {
1128 if (!strcmp(btp->tp->name, name))
1129 return btp;
1130 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001131
1132 return bpf_get_raw_tracepoint_module(name);
1133}
1134
1135void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
1136{
1137 struct module *mod = __module_address((unsigned long)btp);
1138
1139 if (mod)
1140 module_put(mod);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001141}
1142
1143static __always_inline
1144void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
1145{
1146 rcu_read_lock();
1147 preempt_disable();
1148 (void) BPF_PROG_RUN(prog, args);
1149 preempt_enable();
1150 rcu_read_unlock();
1151}
1152
1153#define UNPACK(...) __VA_ARGS__
1154#define REPEAT_1(FN, DL, X, ...) FN(X)
1155#define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
1156#define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
1157#define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
1158#define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
1159#define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
1160#define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
1161#define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
1162#define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
1163#define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
1164#define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
1165#define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
1166#define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__)
1167
1168#define SARG(X) u64 arg##X
1169#define COPY(X) args[X] = arg##X
1170
1171#define __DL_COM (,)
1172#define __DL_SEM (;)
1173
1174#define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1175
1176#define BPF_TRACE_DEFN_x(x) \
1177 void bpf_trace_run##x(struct bpf_prog *prog, \
1178 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \
1179 { \
1180 u64 args[x]; \
1181 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \
1182 __bpf_trace_run(prog, args); \
1183 } \
1184 EXPORT_SYMBOL_GPL(bpf_trace_run##x)
1185BPF_TRACE_DEFN_x(1);
1186BPF_TRACE_DEFN_x(2);
1187BPF_TRACE_DEFN_x(3);
1188BPF_TRACE_DEFN_x(4);
1189BPF_TRACE_DEFN_x(5);
1190BPF_TRACE_DEFN_x(6);
1191BPF_TRACE_DEFN_x(7);
1192BPF_TRACE_DEFN_x(8);
1193BPF_TRACE_DEFN_x(9);
1194BPF_TRACE_DEFN_x(10);
1195BPF_TRACE_DEFN_x(11);
1196BPF_TRACE_DEFN_x(12);
1197
1198static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1199{
1200 struct tracepoint *tp = btp->tp;
1201
1202 /*
1203 * check that program doesn't access arguments beyond what's
1204 * available in this tracepoint
1205 */
1206 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
1207 return -EINVAL;
1208
1209 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog);
1210}
1211
1212int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1213{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08001214 return __bpf_probe_register(btp, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001215}
1216
1217int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
1218{
Alexei Starovoitove16ec342019-01-30 18:12:44 -08001219 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001220}
Yonghong Song41bdc4b2018-05-24 11:21:09 -07001221
1222int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
1223 u32 *fd_type, const char **buf,
1224 u64 *probe_offset, u64 *probe_addr)
1225{
1226 bool is_tracepoint, is_syscall_tp;
1227 struct bpf_prog *prog;
1228 int flags, err = 0;
1229
1230 prog = event->prog;
1231 if (!prog)
1232 return -ENOENT;
1233
1234 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
1235 if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
1236 return -EOPNOTSUPP;
1237
1238 *prog_id = prog->aux->id;
1239 flags = event->tp_event->flags;
1240 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
1241 is_syscall_tp = is_syscall_trace_event(event->tp_event);
1242
1243 if (is_tracepoint || is_syscall_tp) {
1244 *buf = is_tracepoint ? event->tp_event->tp->name
1245 : event->tp_event->name;
1246 *fd_type = BPF_FD_TYPE_TRACEPOINT;
1247 *probe_offset = 0x0;
1248 *probe_addr = 0x0;
1249 } else {
1250 /* kprobe/uprobe */
1251 err = -EOPNOTSUPP;
1252#ifdef CONFIG_KPROBE_EVENTS
1253 if (flags & TRACE_EVENT_FL_KPROBE)
1254 err = bpf_get_kprobe_info(event, fd_type, buf,
1255 probe_offset, probe_addr,
1256 event->attr.type == PERF_TYPE_TRACEPOINT);
1257#endif
1258#ifdef CONFIG_UPROBE_EVENTS
1259 if (flags & TRACE_EVENT_FL_UPROBE)
1260 err = bpf_get_uprobe_info(event, fd_type, buf,
1261 probe_offset,
1262 event->attr.type == PERF_TYPE_TRACEPOINT);
1263#endif
1264 }
1265
1266 return err;
1267}
Matt Mullinsa38d1102018-12-12 16:42:37 -08001268
1269#ifdef CONFIG_MODULES
1270int bpf_event_notify(struct notifier_block *nb, unsigned long op, void *module)
1271{
1272 struct bpf_trace_module *btm, *tmp;
1273 struct module *mod = module;
1274
1275 if (mod->num_bpf_raw_events == 0 ||
1276 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
1277 return 0;
1278
1279 mutex_lock(&bpf_module_mutex);
1280
1281 switch (op) {
1282 case MODULE_STATE_COMING:
1283 btm = kzalloc(sizeof(*btm), GFP_KERNEL);
1284 if (btm) {
1285 btm->module = module;
1286 list_add(&btm->list, &bpf_trace_modules);
1287 }
1288 break;
1289 case MODULE_STATE_GOING:
1290 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
1291 if (btm->module == module) {
1292 list_del(&btm->list);
1293 kfree(btm);
1294 break;
1295 }
1296 }
1297 break;
1298 }
1299
1300 mutex_unlock(&bpf_module_mutex);
1301
1302 return 0;
1303}
1304
1305static struct notifier_block bpf_module_nb = {
1306 .notifier_call = bpf_event_notify,
1307};
1308
1309int __init bpf_event_init(void)
1310{
1311 register_module_notifier(&bpf_module_nb);
1312 return 0;
1313}
1314
1315fs_initcall(bpf_event_init);
1316#endif /* CONFIG_MODULES */