xref: /linux/kernel/trace/bpf_trace.c (revision 00389c58ffe993782a8ba4bb5a34a102b1f6fe24)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
3  * Copyright (c) 2016 Facebook
4  */
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/slab.h>
8 #include <linux/bpf.h>
9 #include <linux/bpf_perf_event.h>
10 #include <linux/btf.h>
11 #include <linux/filter.h>
12 #include <linux/uaccess.h>
13 #include <linux/ctype.h>
14 #include <linux/kprobes.h>
15 #include <linux/spinlock.h>
16 #include <linux/syscalls.h>
17 #include <linux/error-injection.h>
18 #include <linux/btf_ids.h>
19 #include <linux/bpf_lsm.h>
20 #include <linux/fprobe.h>
21 #include <linux/bsearch.h>
22 #include <linux/sort.h>
23 
24 #include <net/bpf_sk_storage.h>
25 
26 #include <uapi/linux/bpf.h>
27 #include <uapi/linux/btf.h>
28 
29 #include <asm/tlb.h>
30 
31 #include "trace_probe.h"
32 #include "trace.h"
33 
34 #define CREATE_TRACE_POINTS
35 #include "bpf_trace.h"
36 
37 #define bpf_event_rcu_dereference(p)					\
38 	rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
39 
40 #ifdef CONFIG_MODULES
41 struct bpf_trace_module {
42 	struct module *module;
43 	struct list_head list;
44 };
45 
46 static LIST_HEAD(bpf_trace_modules);
47 static DEFINE_MUTEX(bpf_module_mutex);
48 
49 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
50 {
51 	struct bpf_raw_event_map *btp, *ret = NULL;
52 	struct bpf_trace_module *btm;
53 	unsigned int i;
54 
55 	mutex_lock(&bpf_module_mutex);
56 	list_for_each_entry(btm, &bpf_trace_modules, list) {
57 		for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
58 			btp = &btm->module->bpf_raw_events[i];
59 			if (!strcmp(btp->tp->name, name)) {
60 				if (try_module_get(btm->module))
61 					ret = btp;
62 				goto out;
63 			}
64 		}
65 	}
66 out:
67 	mutex_unlock(&bpf_module_mutex);
68 	return ret;
69 }
70 #else
71 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
72 {
73 	return NULL;
74 }
75 #endif /* CONFIG_MODULES */
76 
77 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
78 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
79 
80 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
81 				  u64 flags, const struct btf **btf,
82 				  s32 *btf_id);
83 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx, u64 ip);
84 
85 /**
86  * trace_call_bpf - invoke BPF program
87  * @call: tracepoint event
88  * @ctx: opaque context pointer
89  *
90  * kprobe handlers execute BPF programs via this helper.
91  * Can be used from static tracepoints in the future.
92  *
93  * Return: BPF programs always return an integer which is interpreted by
94  * kprobe handler as:
95  * 0 - return from kprobe (event is filtered out)
96  * 1 - store kprobe event into ring buffer
97  * Other values are reserved and currently alias to 1
98  */
99 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
100 {
101 	unsigned int ret;
102 
103 	cant_sleep();
104 
105 	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
106 		/*
107 		 * since some bpf program is already running on this cpu,
108 		 * don't call into another bpf program (same or different)
109 		 * and don't send kprobe event into ring-buffer,
110 		 * so return zero here
111 		 */
112 		ret = 0;
113 		goto out;
114 	}
115 
116 	/*
117 	 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
118 	 * to all call sites, we did a bpf_prog_array_valid() there to check
119 	 * whether call->prog_array is empty or not, which is
120 	 * a heuristic to speed up execution.
121 	 *
122 	 * If bpf_prog_array_valid() fetched prog_array was
123 	 * non-NULL, we go into trace_call_bpf() and do the actual
124 	 * proper rcu_dereference() under RCU lock.
125 	 * If it turns out that prog_array is NULL then, we bail out.
126 	 * For the opposite, if the bpf_prog_array_valid() fetched pointer
127 	 * was NULL, you'll skip the prog_array with the risk of missing
128 	 * out of events when it was updated in between this and the
129 	 * rcu_dereference() which is accepted risk.
130 	 */
131 	ret = BPF_PROG_RUN_ARRAY(call->prog_array, ctx, bpf_prog_run);
132 
133  out:
134 	__this_cpu_dec(bpf_prog_active);
135 
136 	return ret;
137 }
138 
139 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
140 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
141 {
142 	regs_set_return_value(regs, rc);
143 	override_function_with_return(regs);
144 	return 0;
145 }
146 
147 static const struct bpf_func_proto bpf_override_return_proto = {
148 	.func		= bpf_override_return,
149 	.gpl_only	= true,
150 	.ret_type	= RET_INTEGER,
151 	.arg1_type	= ARG_PTR_TO_CTX,
152 	.arg2_type	= ARG_ANYTHING,
153 };
154 #endif
155 
156 static __always_inline int
157 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
158 {
159 	int ret;
160 
161 	ret = copy_from_user_nofault(dst, unsafe_ptr, size);
162 	if (unlikely(ret < 0))
163 		memset(dst, 0, size);
164 	return ret;
165 }
166 
167 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
168 	   const void __user *, unsafe_ptr)
169 {
170 	return bpf_probe_read_user_common(dst, size, unsafe_ptr);
171 }
172 
173 const struct bpf_func_proto bpf_probe_read_user_proto = {
174 	.func		= bpf_probe_read_user,
175 	.gpl_only	= true,
176 	.ret_type	= RET_INTEGER,
177 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
178 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
179 	.arg3_type	= ARG_ANYTHING,
180 };
181 
182 static __always_inline int
183 bpf_probe_read_user_str_common(void *dst, u32 size,
184 			       const void __user *unsafe_ptr)
185 {
186 	int ret;
187 
188 	/*
189 	 * NB: We rely on strncpy_from_user() not copying junk past the NUL
190 	 * terminator into `dst`.
191 	 *
192 	 * strncpy_from_user() does long-sized strides in the fast path. If the
193 	 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
194 	 * then there could be junk after the NUL in `dst`. If user takes `dst`
195 	 * and keys a hash map with it, then semantically identical strings can
196 	 * occupy multiple entries in the map.
197 	 */
198 	ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
199 	if (unlikely(ret < 0))
200 		memset(dst, 0, size);
201 	return ret;
202 }
203 
204 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
205 	   const void __user *, unsafe_ptr)
206 {
207 	return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
208 }
209 
210 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
211 	.func		= bpf_probe_read_user_str,
212 	.gpl_only	= true,
213 	.ret_type	= RET_INTEGER,
214 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
215 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
216 	.arg3_type	= ARG_ANYTHING,
217 };
218 
219 static __always_inline int
220 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
221 {
222 	int ret;
223 
224 	ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
225 	if (unlikely(ret < 0))
226 		memset(dst, 0, size);
227 	return ret;
228 }
229 
230 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
231 	   const void *, unsafe_ptr)
232 {
233 	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
234 }
235 
236 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
237 	.func		= bpf_probe_read_kernel,
238 	.gpl_only	= true,
239 	.ret_type	= RET_INTEGER,
240 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
241 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
242 	.arg3_type	= ARG_ANYTHING,
243 };
244 
245 static __always_inline int
246 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
247 {
248 	int ret;
249 
250 	/*
251 	 * The strncpy_from_kernel_nofault() call will likely not fill the
252 	 * entire buffer, but that's okay in this circumstance as we're probing
253 	 * arbitrary memory anyway similar to bpf_probe_read_*() and might
254 	 * as well probe the stack. Thus, memory is explicitly cleared
255 	 * only in error case, so that improper users ignoring return
256 	 * code altogether don't copy garbage; otherwise length of string
257 	 * is returned that can be used for bpf_perf_event_output() et al.
258 	 */
259 	ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
260 	if (unlikely(ret < 0))
261 		memset(dst, 0, size);
262 	return ret;
263 }
264 
265 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
266 	   const void *, unsafe_ptr)
267 {
268 	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
269 }
270 
271 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
272 	.func		= bpf_probe_read_kernel_str,
273 	.gpl_only	= true,
274 	.ret_type	= RET_INTEGER,
275 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
276 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
277 	.arg3_type	= ARG_ANYTHING,
278 };
279 
280 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
281 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
282 	   const void *, unsafe_ptr)
283 {
284 	if ((unsigned long)unsafe_ptr < TASK_SIZE) {
285 		return bpf_probe_read_user_common(dst, size,
286 				(__force void __user *)unsafe_ptr);
287 	}
288 	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
289 }
290 
291 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
292 	.func		= bpf_probe_read_compat,
293 	.gpl_only	= true,
294 	.ret_type	= RET_INTEGER,
295 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
296 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
297 	.arg3_type	= ARG_ANYTHING,
298 };
299 
300 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
301 	   const void *, unsafe_ptr)
302 {
303 	if ((unsigned long)unsafe_ptr < TASK_SIZE) {
304 		return bpf_probe_read_user_str_common(dst, size,
305 				(__force void __user *)unsafe_ptr);
306 	}
307 	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
308 }
309 
310 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
311 	.func		= bpf_probe_read_compat_str,
312 	.gpl_only	= true,
313 	.ret_type	= RET_INTEGER,
314 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
315 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
316 	.arg3_type	= ARG_ANYTHING,
317 };
318 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
319 
320 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
321 	   u32, size)
322 {
323 	/*
324 	 * Ensure we're in user context which is safe for the helper to
325 	 * run. This helper has no business in a kthread.
326 	 *
327 	 * access_ok() should prevent writing to non-user memory, but in
328 	 * some situations (nommu, temporary switch, etc) access_ok() does
329 	 * not provide enough validation, hence the check on KERNEL_DS.
330 	 *
331 	 * nmi_uaccess_okay() ensures the probe is not run in an interim
332 	 * state, when the task or mm are switched. This is specifically
333 	 * required to prevent the use of temporary mm.
334 	 */
335 
336 	if (unlikely(in_interrupt() ||
337 		     current->flags & (PF_KTHREAD | PF_EXITING)))
338 		return -EPERM;
339 	if (unlikely(uaccess_kernel()))
340 		return -EPERM;
341 	if (unlikely(!nmi_uaccess_okay()))
342 		return -EPERM;
343 
344 	return copy_to_user_nofault(unsafe_ptr, src, size);
345 }
346 
347 static const struct bpf_func_proto bpf_probe_write_user_proto = {
348 	.func		= bpf_probe_write_user,
349 	.gpl_only	= true,
350 	.ret_type	= RET_INTEGER,
351 	.arg1_type	= ARG_ANYTHING,
352 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
353 	.arg3_type	= ARG_CONST_SIZE,
354 };
355 
356 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
357 {
358 	if (!capable(CAP_SYS_ADMIN))
359 		return NULL;
360 
361 	pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
362 			    current->comm, task_pid_nr(current));
363 
364 	return &bpf_probe_write_user_proto;
365 }
366 
367 static DEFINE_RAW_SPINLOCK(trace_printk_lock);
368 
369 #define MAX_TRACE_PRINTK_VARARGS	3
370 #define BPF_TRACE_PRINTK_SIZE		1024
371 
372 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
373 	   u64, arg2, u64, arg3)
374 {
375 	u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
376 	u32 *bin_args;
377 	static char buf[BPF_TRACE_PRINTK_SIZE];
378 	unsigned long flags;
379 	int ret;
380 
381 	ret = bpf_bprintf_prepare(fmt, fmt_size, args, &bin_args,
382 				  MAX_TRACE_PRINTK_VARARGS);
383 	if (ret < 0)
384 		return ret;
385 
386 	raw_spin_lock_irqsave(&trace_printk_lock, flags);
387 	ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
388 
389 	trace_bpf_trace_printk(buf);
390 	raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
391 
392 	bpf_bprintf_cleanup();
393 
394 	return ret;
395 }
396 
397 static const struct bpf_func_proto bpf_trace_printk_proto = {
398 	.func		= bpf_trace_printk,
399 	.gpl_only	= true,
400 	.ret_type	= RET_INTEGER,
401 	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
402 	.arg2_type	= ARG_CONST_SIZE,
403 };
404 
405 static void __set_printk_clr_event(void)
406 {
407 	/*
408 	 * This program might be calling bpf_trace_printk,
409 	 * so enable the associated bpf_trace/bpf_trace_printk event.
410 	 * Repeat this each time as it is possible a user has
411 	 * disabled bpf_trace_printk events.  By loading a program
412 	 * calling bpf_trace_printk() however the user has expressed
413 	 * the intent to see such events.
414 	 */
415 	if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
416 		pr_warn_ratelimited("could not enable bpf_trace_printk events");
417 }
418 
419 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
420 {
421 	__set_printk_clr_event();
422 	return &bpf_trace_printk_proto;
423 }
424 
425 BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, data,
426 	   u32, data_len)
427 {
428 	static char buf[BPF_TRACE_PRINTK_SIZE];
429 	unsigned long flags;
430 	int ret, num_args;
431 	u32 *bin_args;
432 
433 	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
434 	    (data_len && !data))
435 		return -EINVAL;
436 	num_args = data_len / 8;
437 
438 	ret = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
439 	if (ret < 0)
440 		return ret;
441 
442 	raw_spin_lock_irqsave(&trace_printk_lock, flags);
443 	ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
444 
445 	trace_bpf_trace_printk(buf);
446 	raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
447 
448 	bpf_bprintf_cleanup();
449 
450 	return ret;
451 }
452 
453 static const struct bpf_func_proto bpf_trace_vprintk_proto = {
454 	.func		= bpf_trace_vprintk,
455 	.gpl_only	= true,
456 	.ret_type	= RET_INTEGER,
457 	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
458 	.arg2_type	= ARG_CONST_SIZE,
459 	.arg3_type	= ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
460 	.arg4_type	= ARG_CONST_SIZE_OR_ZERO,
461 };
462 
463 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
464 {
465 	__set_printk_clr_event();
466 	return &bpf_trace_vprintk_proto;
467 }
468 
469 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
470 	   const void *, data, u32, data_len)
471 {
472 	int err, num_args;
473 	u32 *bin_args;
474 
475 	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
476 	    (data_len && !data))
477 		return -EINVAL;
478 	num_args = data_len / 8;
479 
480 	err = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
481 	if (err < 0)
482 		return err;
483 
484 	seq_bprintf(m, fmt, bin_args);
485 
486 	bpf_bprintf_cleanup();
487 
488 	return seq_has_overflowed(m) ? -EOVERFLOW : 0;
489 }
490 
491 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
492 
493 static const struct bpf_func_proto bpf_seq_printf_proto = {
494 	.func		= bpf_seq_printf,
495 	.gpl_only	= true,
496 	.ret_type	= RET_INTEGER,
497 	.arg1_type	= ARG_PTR_TO_BTF_ID,
498 	.arg1_btf_id	= &btf_seq_file_ids[0],
499 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
500 	.arg3_type	= ARG_CONST_SIZE,
501 	.arg4_type      = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
502 	.arg5_type      = ARG_CONST_SIZE_OR_ZERO,
503 };
504 
505 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
506 {
507 	return seq_write(m, data, len) ? -EOVERFLOW : 0;
508 }
509 
510 static const struct bpf_func_proto bpf_seq_write_proto = {
511 	.func		= bpf_seq_write,
512 	.gpl_only	= true,
513 	.ret_type	= RET_INTEGER,
514 	.arg1_type	= ARG_PTR_TO_BTF_ID,
515 	.arg1_btf_id	= &btf_seq_file_ids[0],
516 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
517 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
518 };
519 
520 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
521 	   u32, btf_ptr_size, u64, flags)
522 {
523 	const struct btf *btf;
524 	s32 btf_id;
525 	int ret;
526 
527 	ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
528 	if (ret)
529 		return ret;
530 
531 	return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
532 }
533 
534 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
535 	.func		= bpf_seq_printf_btf,
536 	.gpl_only	= true,
537 	.ret_type	= RET_INTEGER,
538 	.arg1_type	= ARG_PTR_TO_BTF_ID,
539 	.arg1_btf_id	= &btf_seq_file_ids[0],
540 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
541 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
542 	.arg4_type	= ARG_ANYTHING,
543 };
544 
545 static __always_inline int
546 get_map_perf_counter(struct bpf_map *map, u64 flags,
547 		     u64 *value, u64 *enabled, u64 *running)
548 {
549 	struct bpf_array *array = container_of(map, struct bpf_array, map);
550 	unsigned int cpu = smp_processor_id();
551 	u64 index = flags & BPF_F_INDEX_MASK;
552 	struct bpf_event_entry *ee;
553 
554 	if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
555 		return -EINVAL;
556 	if (index == BPF_F_CURRENT_CPU)
557 		index = cpu;
558 	if (unlikely(index >= array->map.max_entries))
559 		return -E2BIG;
560 
561 	ee = READ_ONCE(array->ptrs[index]);
562 	if (!ee)
563 		return -ENOENT;
564 
565 	return perf_event_read_local(ee->event, value, enabled, running);
566 }
567 
568 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
569 {
570 	u64 value = 0;
571 	int err;
572 
573 	err = get_map_perf_counter(map, flags, &value, NULL, NULL);
574 	/*
575 	 * this api is ugly since we miss [-22..-2] range of valid
576 	 * counter values, but that's uapi
577 	 */
578 	if (err)
579 		return err;
580 	return value;
581 }
582 
583 static const struct bpf_func_proto bpf_perf_event_read_proto = {
584 	.func		= bpf_perf_event_read,
585 	.gpl_only	= true,
586 	.ret_type	= RET_INTEGER,
587 	.arg1_type	= ARG_CONST_MAP_PTR,
588 	.arg2_type	= ARG_ANYTHING,
589 };
590 
591 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
592 	   struct bpf_perf_event_value *, buf, u32, size)
593 {
594 	int err = -EINVAL;
595 
596 	if (unlikely(size != sizeof(struct bpf_perf_event_value)))
597 		goto clear;
598 	err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
599 				   &buf->running);
600 	if (unlikely(err))
601 		goto clear;
602 	return 0;
603 clear:
604 	memset(buf, 0, size);
605 	return err;
606 }
607 
608 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
609 	.func		= bpf_perf_event_read_value,
610 	.gpl_only	= true,
611 	.ret_type	= RET_INTEGER,
612 	.arg1_type	= ARG_CONST_MAP_PTR,
613 	.arg2_type	= ARG_ANYTHING,
614 	.arg3_type	= ARG_PTR_TO_UNINIT_MEM,
615 	.arg4_type	= ARG_CONST_SIZE,
616 };
617 
618 static __always_inline u64
619 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
620 			u64 flags, struct perf_sample_data *sd)
621 {
622 	struct bpf_array *array = container_of(map, struct bpf_array, map);
623 	unsigned int cpu = smp_processor_id();
624 	u64 index = flags & BPF_F_INDEX_MASK;
625 	struct bpf_event_entry *ee;
626 	struct perf_event *event;
627 
628 	if (index == BPF_F_CURRENT_CPU)
629 		index = cpu;
630 	if (unlikely(index >= array->map.max_entries))
631 		return -E2BIG;
632 
633 	ee = READ_ONCE(array->ptrs[index]);
634 	if (!ee)
635 		return -ENOENT;
636 
637 	event = ee->event;
638 	if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
639 		     event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
640 		return -EINVAL;
641 
642 	if (unlikely(event->oncpu != cpu))
643 		return -EOPNOTSUPP;
644 
645 	return perf_event_output(event, sd, regs);
646 }
647 
648 /*
649  * Support executing tracepoints in normal, irq, and nmi context that each call
650  * bpf_perf_event_output
651  */
652 struct bpf_trace_sample_data {
653 	struct perf_sample_data sds[3];
654 };
655 
656 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
657 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
658 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
659 	   u64, flags, void *, data, u64, size)
660 {
661 	struct bpf_trace_sample_data *sds = this_cpu_ptr(&bpf_trace_sds);
662 	int nest_level = this_cpu_inc_return(bpf_trace_nest_level);
663 	struct perf_raw_record raw = {
664 		.frag = {
665 			.size = size,
666 			.data = data,
667 		},
668 	};
669 	struct perf_sample_data *sd;
670 	int err;
671 
672 	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
673 		err = -EBUSY;
674 		goto out;
675 	}
676 
677 	sd = &sds->sds[nest_level - 1];
678 
679 	if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
680 		err = -EINVAL;
681 		goto out;
682 	}
683 
684 	perf_sample_data_init(sd, 0, 0);
685 	sd->raw = &raw;
686 
687 	err = __bpf_perf_event_output(regs, map, flags, sd);
688 
689 out:
690 	this_cpu_dec(bpf_trace_nest_level);
691 	return err;
692 }
693 
694 static const struct bpf_func_proto bpf_perf_event_output_proto = {
695 	.func		= bpf_perf_event_output,
696 	.gpl_only	= true,
697 	.ret_type	= RET_INTEGER,
698 	.arg1_type	= ARG_PTR_TO_CTX,
699 	.arg2_type	= ARG_CONST_MAP_PTR,
700 	.arg3_type	= ARG_ANYTHING,
701 	.arg4_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
702 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
703 };
704 
705 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
706 struct bpf_nested_pt_regs {
707 	struct pt_regs regs[3];
708 };
709 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
710 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
711 
712 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
713 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
714 {
715 	int nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
716 	struct perf_raw_frag frag = {
717 		.copy		= ctx_copy,
718 		.size		= ctx_size,
719 		.data		= ctx,
720 	};
721 	struct perf_raw_record raw = {
722 		.frag = {
723 			{
724 				.next	= ctx_size ? &frag : NULL,
725 			},
726 			.size	= meta_size,
727 			.data	= meta,
728 		},
729 	};
730 	struct perf_sample_data *sd;
731 	struct pt_regs *regs;
732 	u64 ret;
733 
734 	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
735 		ret = -EBUSY;
736 		goto out;
737 	}
738 	sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
739 	regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
740 
741 	perf_fetch_caller_regs(regs);
742 	perf_sample_data_init(sd, 0, 0);
743 	sd->raw = &raw;
744 
745 	ret = __bpf_perf_event_output(regs, map, flags, sd);
746 out:
747 	this_cpu_dec(bpf_event_output_nest_level);
748 	return ret;
749 }
750 
751 BPF_CALL_0(bpf_get_current_task)
752 {
753 	return (long) current;
754 }
755 
756 const struct bpf_func_proto bpf_get_current_task_proto = {
757 	.func		= bpf_get_current_task,
758 	.gpl_only	= true,
759 	.ret_type	= RET_INTEGER,
760 };
761 
762 BPF_CALL_0(bpf_get_current_task_btf)
763 {
764 	return (unsigned long) current;
765 }
766 
767 const struct bpf_func_proto bpf_get_current_task_btf_proto = {
768 	.func		= bpf_get_current_task_btf,
769 	.gpl_only	= true,
770 	.ret_type	= RET_PTR_TO_BTF_ID,
771 	.ret_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
772 };
773 
774 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
775 {
776 	return (unsigned long) task_pt_regs(task);
777 }
778 
779 BTF_ID_LIST(bpf_task_pt_regs_ids)
780 BTF_ID(struct, pt_regs)
781 
782 const struct bpf_func_proto bpf_task_pt_regs_proto = {
783 	.func		= bpf_task_pt_regs,
784 	.gpl_only	= true,
785 	.arg1_type	= ARG_PTR_TO_BTF_ID,
786 	.arg1_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
787 	.ret_type	= RET_PTR_TO_BTF_ID,
788 	.ret_btf_id	= &bpf_task_pt_regs_ids[0],
789 };
790 
791 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
792 {
793 	struct bpf_array *array = container_of(map, struct bpf_array, map);
794 	struct cgroup *cgrp;
795 
796 	if (unlikely(idx >= array->map.max_entries))
797 		return -E2BIG;
798 
799 	cgrp = READ_ONCE(array->ptrs[idx]);
800 	if (unlikely(!cgrp))
801 		return -EAGAIN;
802 
803 	return task_under_cgroup_hierarchy(current, cgrp);
804 }
805 
806 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
807 	.func           = bpf_current_task_under_cgroup,
808 	.gpl_only       = false,
809 	.ret_type       = RET_INTEGER,
810 	.arg1_type      = ARG_CONST_MAP_PTR,
811 	.arg2_type      = ARG_ANYTHING,
812 };
813 
814 struct send_signal_irq_work {
815 	struct irq_work irq_work;
816 	struct task_struct *task;
817 	u32 sig;
818 	enum pid_type type;
819 };
820 
821 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
822 
823 static void do_bpf_send_signal(struct irq_work *entry)
824 {
825 	struct send_signal_irq_work *work;
826 
827 	work = container_of(entry, struct send_signal_irq_work, irq_work);
828 	group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, work->type);
829 }
830 
831 static int bpf_send_signal_common(u32 sig, enum pid_type type)
832 {
833 	struct send_signal_irq_work *work = NULL;
834 
835 	/* Similar to bpf_probe_write_user, task needs to be
836 	 * in a sound condition and kernel memory access be
837 	 * permitted in order to send signal to the current
838 	 * task.
839 	 */
840 	if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING)))
841 		return -EPERM;
842 	if (unlikely(uaccess_kernel()))
843 		return -EPERM;
844 	if (unlikely(!nmi_uaccess_okay()))
845 		return -EPERM;
846 
847 	if (irqs_disabled()) {
848 		/* Do an early check on signal validity. Otherwise,
849 		 * the error is lost in deferred irq_work.
850 		 */
851 		if (unlikely(!valid_signal(sig)))
852 			return -EINVAL;
853 
854 		work = this_cpu_ptr(&send_signal_work);
855 		if (irq_work_is_busy(&work->irq_work))
856 			return -EBUSY;
857 
858 		/* Add the current task, which is the target of sending signal,
859 		 * to the irq_work. The current task may change when queued
860 		 * irq works get executed.
861 		 */
862 		work->task = current;
863 		work->sig = sig;
864 		work->type = type;
865 		irq_work_queue(&work->irq_work);
866 		return 0;
867 	}
868 
869 	return group_send_sig_info(sig, SEND_SIG_PRIV, current, type);
870 }
871 
872 BPF_CALL_1(bpf_send_signal, u32, sig)
873 {
874 	return bpf_send_signal_common(sig, PIDTYPE_TGID);
875 }
876 
877 static const struct bpf_func_proto bpf_send_signal_proto = {
878 	.func		= bpf_send_signal,
879 	.gpl_only	= false,
880 	.ret_type	= RET_INTEGER,
881 	.arg1_type	= ARG_ANYTHING,
882 };
883 
884 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
885 {
886 	return bpf_send_signal_common(sig, PIDTYPE_PID);
887 }
888 
889 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
890 	.func		= bpf_send_signal_thread,
891 	.gpl_only	= false,
892 	.ret_type	= RET_INTEGER,
893 	.arg1_type	= ARG_ANYTHING,
894 };
895 
896 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
897 {
898 	long len;
899 	char *p;
900 
901 	if (!sz)
902 		return 0;
903 
904 	p = d_path(path, buf, sz);
905 	if (IS_ERR(p)) {
906 		len = PTR_ERR(p);
907 	} else {
908 		len = buf + sz - p;
909 		memmove(buf, p, len);
910 	}
911 
912 	return len;
913 }
914 
915 BTF_SET_START(btf_allowlist_d_path)
916 #ifdef CONFIG_SECURITY
917 BTF_ID(func, security_file_permission)
918 BTF_ID(func, security_inode_getattr)
919 BTF_ID(func, security_file_open)
920 #endif
921 #ifdef CONFIG_SECURITY_PATH
922 BTF_ID(func, security_path_truncate)
923 #endif
924 BTF_ID(func, vfs_truncate)
925 BTF_ID(func, vfs_fallocate)
926 BTF_ID(func, dentry_open)
927 BTF_ID(func, vfs_getattr)
928 BTF_ID(func, filp_close)
929 BTF_SET_END(btf_allowlist_d_path)
930 
931 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
932 {
933 	if (prog->type == BPF_PROG_TYPE_TRACING &&
934 	    prog->expected_attach_type == BPF_TRACE_ITER)
935 		return true;
936 
937 	if (prog->type == BPF_PROG_TYPE_LSM)
938 		return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
939 
940 	return btf_id_set_contains(&btf_allowlist_d_path,
941 				   prog->aux->attach_btf_id);
942 }
943 
944 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
945 
946 static const struct bpf_func_proto bpf_d_path_proto = {
947 	.func		= bpf_d_path,
948 	.gpl_only	= false,
949 	.ret_type	= RET_INTEGER,
950 	.arg1_type	= ARG_PTR_TO_BTF_ID,
951 	.arg1_btf_id	= &bpf_d_path_btf_ids[0],
952 	.arg2_type	= ARG_PTR_TO_MEM,
953 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
954 	.allowed	= bpf_d_path_allowed,
955 };
956 
957 #define BTF_F_ALL	(BTF_F_COMPACT  | BTF_F_NONAME | \
958 			 BTF_F_PTR_RAW | BTF_F_ZERO)
959 
960 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
961 				  u64 flags, const struct btf **btf,
962 				  s32 *btf_id)
963 {
964 	const struct btf_type *t;
965 
966 	if (unlikely(flags & ~(BTF_F_ALL)))
967 		return -EINVAL;
968 
969 	if (btf_ptr_size != sizeof(struct btf_ptr))
970 		return -EINVAL;
971 
972 	*btf = bpf_get_btf_vmlinux();
973 
974 	if (IS_ERR_OR_NULL(*btf))
975 		return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
976 
977 	if (ptr->type_id > 0)
978 		*btf_id = ptr->type_id;
979 	else
980 		return -EINVAL;
981 
982 	if (*btf_id > 0)
983 		t = btf_type_by_id(*btf, *btf_id);
984 	if (*btf_id <= 0 || !t)
985 		return -ENOENT;
986 
987 	return 0;
988 }
989 
990 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
991 	   u32, btf_ptr_size, u64, flags)
992 {
993 	const struct btf *btf;
994 	s32 btf_id;
995 	int ret;
996 
997 	ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
998 	if (ret)
999 		return ret;
1000 
1001 	return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1002 				      flags);
1003 }
1004 
1005 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1006 	.func		= bpf_snprintf_btf,
1007 	.gpl_only	= false,
1008 	.ret_type	= RET_INTEGER,
1009 	.arg1_type	= ARG_PTR_TO_MEM,
1010 	.arg2_type	= ARG_CONST_SIZE,
1011 	.arg3_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1012 	.arg4_type	= ARG_CONST_SIZE,
1013 	.arg5_type	= ARG_ANYTHING,
1014 };
1015 
1016 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1017 {
1018 	/* This helper call is inlined by verifier. */
1019 	return ((u64 *)ctx)[-2];
1020 }
1021 
1022 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1023 	.func		= bpf_get_func_ip_tracing,
1024 	.gpl_only	= true,
1025 	.ret_type	= RET_INTEGER,
1026 	.arg1_type	= ARG_PTR_TO_CTX,
1027 };
1028 
1029 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1030 {
1031 	struct kprobe *kp = kprobe_running();
1032 
1033 	return kp ? (uintptr_t)kp->addr : 0;
1034 }
1035 
1036 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1037 	.func		= bpf_get_func_ip_kprobe,
1038 	.gpl_only	= true,
1039 	.ret_type	= RET_INTEGER,
1040 	.arg1_type	= ARG_PTR_TO_CTX,
1041 };
1042 
1043 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1044 {
1045 	/* This helper call is inlined by verifier on x86. */
1046 	return instruction_pointer(regs);
1047 }
1048 
1049 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1050 	.func		= bpf_get_func_ip_kprobe_multi,
1051 	.gpl_only	= false,
1052 	.ret_type	= RET_INTEGER,
1053 	.arg1_type	= ARG_PTR_TO_CTX,
1054 };
1055 
1056 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1057 {
1058 	return bpf_kprobe_multi_cookie(current->bpf_ctx, instruction_pointer(regs));
1059 }
1060 
1061 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1062 	.func		= bpf_get_attach_cookie_kprobe_multi,
1063 	.gpl_only	= false,
1064 	.ret_type	= RET_INTEGER,
1065 	.arg1_type	= ARG_PTR_TO_CTX,
1066 };
1067 
1068 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1069 {
1070 	struct bpf_trace_run_ctx *run_ctx;
1071 
1072 	run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1073 	return run_ctx->bpf_cookie;
1074 }
1075 
1076 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1077 	.func		= bpf_get_attach_cookie_trace,
1078 	.gpl_only	= false,
1079 	.ret_type	= RET_INTEGER,
1080 	.arg1_type	= ARG_PTR_TO_CTX,
1081 };
1082 
1083 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1084 {
1085 	return ctx->event->bpf_cookie;
1086 }
1087 
1088 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1089 	.func		= bpf_get_attach_cookie_pe,
1090 	.gpl_only	= false,
1091 	.ret_type	= RET_INTEGER,
1092 	.arg1_type	= ARG_PTR_TO_CTX,
1093 };
1094 
1095 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1096 {
1097 #ifndef CONFIG_X86
1098 	return -ENOENT;
1099 #else
1100 	static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1101 	u32 entry_cnt = size / br_entry_size;
1102 
1103 	entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1104 
1105 	if (unlikely(flags))
1106 		return -EINVAL;
1107 
1108 	if (!entry_cnt)
1109 		return -ENOENT;
1110 
1111 	return entry_cnt * br_entry_size;
1112 #endif
1113 }
1114 
1115 static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1116 	.func		= bpf_get_branch_snapshot,
1117 	.gpl_only	= true,
1118 	.ret_type	= RET_INTEGER,
1119 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
1120 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
1121 };
1122 
1123 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1124 {
1125 	/* This helper call is inlined by verifier. */
1126 	u64 nr_args = ((u64 *)ctx)[-1];
1127 
1128 	if ((u64) n >= nr_args)
1129 		return -EINVAL;
1130 	*value = ((u64 *)ctx)[n];
1131 	return 0;
1132 }
1133 
1134 static const struct bpf_func_proto bpf_get_func_arg_proto = {
1135 	.func		= get_func_arg,
1136 	.ret_type	= RET_INTEGER,
1137 	.arg1_type	= ARG_PTR_TO_CTX,
1138 	.arg2_type	= ARG_ANYTHING,
1139 	.arg3_type	= ARG_PTR_TO_LONG,
1140 };
1141 
1142 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1143 {
1144 	/* This helper call is inlined by verifier. */
1145 	u64 nr_args = ((u64 *)ctx)[-1];
1146 
1147 	*value = ((u64 *)ctx)[nr_args];
1148 	return 0;
1149 }
1150 
1151 static const struct bpf_func_proto bpf_get_func_ret_proto = {
1152 	.func		= get_func_ret,
1153 	.ret_type	= RET_INTEGER,
1154 	.arg1_type	= ARG_PTR_TO_CTX,
1155 	.arg2_type	= ARG_PTR_TO_LONG,
1156 };
1157 
1158 BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1159 {
1160 	/* This helper call is inlined by verifier. */
1161 	return ((u64 *)ctx)[-1];
1162 }
1163 
1164 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1165 	.func		= get_func_arg_cnt,
1166 	.ret_type	= RET_INTEGER,
1167 	.arg1_type	= ARG_PTR_TO_CTX,
1168 };
1169 
1170 static const struct bpf_func_proto *
1171 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1172 {
1173 	switch (func_id) {
1174 	case BPF_FUNC_map_lookup_elem:
1175 		return &bpf_map_lookup_elem_proto;
1176 	case BPF_FUNC_map_update_elem:
1177 		return &bpf_map_update_elem_proto;
1178 	case BPF_FUNC_map_delete_elem:
1179 		return &bpf_map_delete_elem_proto;
1180 	case BPF_FUNC_map_push_elem:
1181 		return &bpf_map_push_elem_proto;
1182 	case BPF_FUNC_map_pop_elem:
1183 		return &bpf_map_pop_elem_proto;
1184 	case BPF_FUNC_map_peek_elem:
1185 		return &bpf_map_peek_elem_proto;
1186 	case BPF_FUNC_ktime_get_ns:
1187 		return &bpf_ktime_get_ns_proto;
1188 	case BPF_FUNC_ktime_get_boot_ns:
1189 		return &bpf_ktime_get_boot_ns_proto;
1190 	case BPF_FUNC_tail_call:
1191 		return &bpf_tail_call_proto;
1192 	case BPF_FUNC_get_current_pid_tgid:
1193 		return &bpf_get_current_pid_tgid_proto;
1194 	case BPF_FUNC_get_current_task:
1195 		return &bpf_get_current_task_proto;
1196 	case BPF_FUNC_get_current_task_btf:
1197 		return &bpf_get_current_task_btf_proto;
1198 	case BPF_FUNC_task_pt_regs:
1199 		return &bpf_task_pt_regs_proto;
1200 	case BPF_FUNC_get_current_uid_gid:
1201 		return &bpf_get_current_uid_gid_proto;
1202 	case BPF_FUNC_get_current_comm:
1203 		return &bpf_get_current_comm_proto;
1204 	case BPF_FUNC_trace_printk:
1205 		return bpf_get_trace_printk_proto();
1206 	case BPF_FUNC_get_smp_processor_id:
1207 		return &bpf_get_smp_processor_id_proto;
1208 	case BPF_FUNC_get_numa_node_id:
1209 		return &bpf_get_numa_node_id_proto;
1210 	case BPF_FUNC_perf_event_read:
1211 		return &bpf_perf_event_read_proto;
1212 	case BPF_FUNC_current_task_under_cgroup:
1213 		return &bpf_current_task_under_cgroup_proto;
1214 	case BPF_FUNC_get_prandom_u32:
1215 		return &bpf_get_prandom_u32_proto;
1216 	case BPF_FUNC_probe_write_user:
1217 		return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1218 		       NULL : bpf_get_probe_write_proto();
1219 	case BPF_FUNC_probe_read_user:
1220 		return &bpf_probe_read_user_proto;
1221 	case BPF_FUNC_probe_read_kernel:
1222 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1223 		       NULL : &bpf_probe_read_kernel_proto;
1224 	case BPF_FUNC_probe_read_user_str:
1225 		return &bpf_probe_read_user_str_proto;
1226 	case BPF_FUNC_probe_read_kernel_str:
1227 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1228 		       NULL : &bpf_probe_read_kernel_str_proto;
1229 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1230 	case BPF_FUNC_probe_read:
1231 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1232 		       NULL : &bpf_probe_read_compat_proto;
1233 	case BPF_FUNC_probe_read_str:
1234 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1235 		       NULL : &bpf_probe_read_compat_str_proto;
1236 #endif
1237 #ifdef CONFIG_CGROUPS
1238 	case BPF_FUNC_get_current_cgroup_id:
1239 		return &bpf_get_current_cgroup_id_proto;
1240 	case BPF_FUNC_get_current_ancestor_cgroup_id:
1241 		return &bpf_get_current_ancestor_cgroup_id_proto;
1242 #endif
1243 	case BPF_FUNC_send_signal:
1244 		return &bpf_send_signal_proto;
1245 	case BPF_FUNC_send_signal_thread:
1246 		return &bpf_send_signal_thread_proto;
1247 	case BPF_FUNC_perf_event_read_value:
1248 		return &bpf_perf_event_read_value_proto;
1249 	case BPF_FUNC_get_ns_current_pid_tgid:
1250 		return &bpf_get_ns_current_pid_tgid_proto;
1251 	case BPF_FUNC_ringbuf_output:
1252 		return &bpf_ringbuf_output_proto;
1253 	case BPF_FUNC_ringbuf_reserve:
1254 		return &bpf_ringbuf_reserve_proto;
1255 	case BPF_FUNC_ringbuf_submit:
1256 		return &bpf_ringbuf_submit_proto;
1257 	case BPF_FUNC_ringbuf_discard:
1258 		return &bpf_ringbuf_discard_proto;
1259 	case BPF_FUNC_ringbuf_query:
1260 		return &bpf_ringbuf_query_proto;
1261 	case BPF_FUNC_jiffies64:
1262 		return &bpf_jiffies64_proto;
1263 	case BPF_FUNC_get_task_stack:
1264 		return &bpf_get_task_stack_proto;
1265 	case BPF_FUNC_copy_from_user:
1266 		return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1267 	case BPF_FUNC_copy_from_user_task:
1268 		return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
1269 	case BPF_FUNC_snprintf_btf:
1270 		return &bpf_snprintf_btf_proto;
1271 	case BPF_FUNC_per_cpu_ptr:
1272 		return &bpf_per_cpu_ptr_proto;
1273 	case BPF_FUNC_this_cpu_ptr:
1274 		return &bpf_this_cpu_ptr_proto;
1275 	case BPF_FUNC_task_storage_get:
1276 		return &bpf_task_storage_get_proto;
1277 	case BPF_FUNC_task_storage_delete:
1278 		return &bpf_task_storage_delete_proto;
1279 	case BPF_FUNC_for_each_map_elem:
1280 		return &bpf_for_each_map_elem_proto;
1281 	case BPF_FUNC_snprintf:
1282 		return &bpf_snprintf_proto;
1283 	case BPF_FUNC_get_func_ip:
1284 		return &bpf_get_func_ip_proto_tracing;
1285 	case BPF_FUNC_get_branch_snapshot:
1286 		return &bpf_get_branch_snapshot_proto;
1287 	case BPF_FUNC_find_vma:
1288 		return &bpf_find_vma_proto;
1289 	case BPF_FUNC_trace_vprintk:
1290 		return bpf_get_trace_vprintk_proto();
1291 	default:
1292 		return bpf_base_func_proto(func_id);
1293 	}
1294 }
1295 
1296 static const struct bpf_func_proto *
1297 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1298 {
1299 	switch (func_id) {
1300 	case BPF_FUNC_perf_event_output:
1301 		return &bpf_perf_event_output_proto;
1302 	case BPF_FUNC_get_stackid:
1303 		return &bpf_get_stackid_proto;
1304 	case BPF_FUNC_get_stack:
1305 		return &bpf_get_stack_proto;
1306 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1307 	case BPF_FUNC_override_return:
1308 		return &bpf_override_return_proto;
1309 #endif
1310 	case BPF_FUNC_get_func_ip:
1311 		return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1312 			&bpf_get_func_ip_proto_kprobe_multi :
1313 			&bpf_get_func_ip_proto_kprobe;
1314 	case BPF_FUNC_get_attach_cookie:
1315 		return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ?
1316 			&bpf_get_attach_cookie_proto_kmulti :
1317 			&bpf_get_attach_cookie_proto_trace;
1318 	default:
1319 		return bpf_tracing_func_proto(func_id, prog);
1320 	}
1321 }
1322 
1323 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1324 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1325 					const struct bpf_prog *prog,
1326 					struct bpf_insn_access_aux *info)
1327 {
1328 	if (off < 0 || off >= sizeof(struct pt_regs))
1329 		return false;
1330 	if (type != BPF_READ)
1331 		return false;
1332 	if (off % size != 0)
1333 		return false;
1334 	/*
1335 	 * Assertion for 32 bit to make sure last 8 byte access
1336 	 * (BPF_DW) to the last 4 byte member is disallowed.
1337 	 */
1338 	if (off + size > sizeof(struct pt_regs))
1339 		return false;
1340 
1341 	return true;
1342 }
1343 
1344 const struct bpf_verifier_ops kprobe_verifier_ops = {
1345 	.get_func_proto  = kprobe_prog_func_proto,
1346 	.is_valid_access = kprobe_prog_is_valid_access,
1347 };
1348 
1349 const struct bpf_prog_ops kprobe_prog_ops = {
1350 };
1351 
1352 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1353 	   u64, flags, void *, data, u64, size)
1354 {
1355 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1356 
1357 	/*
1358 	 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1359 	 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1360 	 * from there and call the same bpf_perf_event_output() helper inline.
1361 	 */
1362 	return ____bpf_perf_event_output(regs, map, flags, data, size);
1363 }
1364 
1365 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1366 	.func		= bpf_perf_event_output_tp,
1367 	.gpl_only	= true,
1368 	.ret_type	= RET_INTEGER,
1369 	.arg1_type	= ARG_PTR_TO_CTX,
1370 	.arg2_type	= ARG_CONST_MAP_PTR,
1371 	.arg3_type	= ARG_ANYTHING,
1372 	.arg4_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1373 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
1374 };
1375 
1376 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1377 	   u64, flags)
1378 {
1379 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1380 
1381 	/*
1382 	 * Same comment as in bpf_perf_event_output_tp(), only that this time
1383 	 * the other helper's function body cannot be inlined due to being
1384 	 * external, thus we need to call raw helper function.
1385 	 */
1386 	return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1387 			       flags, 0, 0);
1388 }
1389 
1390 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1391 	.func		= bpf_get_stackid_tp,
1392 	.gpl_only	= true,
1393 	.ret_type	= RET_INTEGER,
1394 	.arg1_type	= ARG_PTR_TO_CTX,
1395 	.arg2_type	= ARG_CONST_MAP_PTR,
1396 	.arg3_type	= ARG_ANYTHING,
1397 };
1398 
1399 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1400 	   u64, flags)
1401 {
1402 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1403 
1404 	return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1405 			     (unsigned long) size, flags, 0);
1406 }
1407 
1408 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1409 	.func		= bpf_get_stack_tp,
1410 	.gpl_only	= true,
1411 	.ret_type	= RET_INTEGER,
1412 	.arg1_type	= ARG_PTR_TO_CTX,
1413 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
1414 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
1415 	.arg4_type	= ARG_ANYTHING,
1416 };
1417 
1418 static const struct bpf_func_proto *
1419 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1420 {
1421 	switch (func_id) {
1422 	case BPF_FUNC_perf_event_output:
1423 		return &bpf_perf_event_output_proto_tp;
1424 	case BPF_FUNC_get_stackid:
1425 		return &bpf_get_stackid_proto_tp;
1426 	case BPF_FUNC_get_stack:
1427 		return &bpf_get_stack_proto_tp;
1428 	case BPF_FUNC_get_attach_cookie:
1429 		return &bpf_get_attach_cookie_proto_trace;
1430 	default:
1431 		return bpf_tracing_func_proto(func_id, prog);
1432 	}
1433 }
1434 
1435 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1436 				    const struct bpf_prog *prog,
1437 				    struct bpf_insn_access_aux *info)
1438 {
1439 	if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1440 		return false;
1441 	if (type != BPF_READ)
1442 		return false;
1443 	if (off % size != 0)
1444 		return false;
1445 
1446 	BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1447 	return true;
1448 }
1449 
1450 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1451 	.get_func_proto  = tp_prog_func_proto,
1452 	.is_valid_access = tp_prog_is_valid_access,
1453 };
1454 
1455 const struct bpf_prog_ops tracepoint_prog_ops = {
1456 };
1457 
1458 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1459 	   struct bpf_perf_event_value *, buf, u32, size)
1460 {
1461 	int err = -EINVAL;
1462 
1463 	if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1464 		goto clear;
1465 	err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1466 				    &buf->running);
1467 	if (unlikely(err))
1468 		goto clear;
1469 	return 0;
1470 clear:
1471 	memset(buf, 0, size);
1472 	return err;
1473 }
1474 
1475 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1476          .func           = bpf_perf_prog_read_value,
1477          .gpl_only       = true,
1478          .ret_type       = RET_INTEGER,
1479          .arg1_type      = ARG_PTR_TO_CTX,
1480          .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
1481          .arg3_type      = ARG_CONST_SIZE,
1482 };
1483 
1484 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1485 	   void *, buf, u32, size, u64, flags)
1486 {
1487 	static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1488 	struct perf_branch_stack *br_stack = ctx->data->br_stack;
1489 	u32 to_copy;
1490 
1491 	if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1492 		return -EINVAL;
1493 
1494 	if (unlikely(!br_stack))
1495 		return -ENOENT;
1496 
1497 	if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1498 		return br_stack->nr * br_entry_size;
1499 
1500 	if (!buf || (size % br_entry_size != 0))
1501 		return -EINVAL;
1502 
1503 	to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1504 	memcpy(buf, br_stack->entries, to_copy);
1505 
1506 	return to_copy;
1507 }
1508 
1509 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1510 	.func           = bpf_read_branch_records,
1511 	.gpl_only       = true,
1512 	.ret_type       = RET_INTEGER,
1513 	.arg1_type      = ARG_PTR_TO_CTX,
1514 	.arg2_type      = ARG_PTR_TO_MEM_OR_NULL,
1515 	.arg3_type      = ARG_CONST_SIZE_OR_ZERO,
1516 	.arg4_type      = ARG_ANYTHING,
1517 };
1518 
1519 static const struct bpf_func_proto *
1520 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1521 {
1522 	switch (func_id) {
1523 	case BPF_FUNC_perf_event_output:
1524 		return &bpf_perf_event_output_proto_tp;
1525 	case BPF_FUNC_get_stackid:
1526 		return &bpf_get_stackid_proto_pe;
1527 	case BPF_FUNC_get_stack:
1528 		return &bpf_get_stack_proto_pe;
1529 	case BPF_FUNC_perf_prog_read_value:
1530 		return &bpf_perf_prog_read_value_proto;
1531 	case BPF_FUNC_read_branch_records:
1532 		return &bpf_read_branch_records_proto;
1533 	case BPF_FUNC_get_attach_cookie:
1534 		return &bpf_get_attach_cookie_proto_pe;
1535 	default:
1536 		return bpf_tracing_func_proto(func_id, prog);
1537 	}
1538 }
1539 
1540 /*
1541  * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1542  * to avoid potential recursive reuse issue when/if tracepoints are added
1543  * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1544  *
1545  * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1546  * in normal, irq, and nmi context.
1547  */
1548 struct bpf_raw_tp_regs {
1549 	struct pt_regs regs[3];
1550 };
1551 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1552 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1553 static struct pt_regs *get_bpf_raw_tp_regs(void)
1554 {
1555 	struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1556 	int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1557 
1558 	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1559 		this_cpu_dec(bpf_raw_tp_nest_level);
1560 		return ERR_PTR(-EBUSY);
1561 	}
1562 
1563 	return &tp_regs->regs[nest_level - 1];
1564 }
1565 
1566 static void put_bpf_raw_tp_regs(void)
1567 {
1568 	this_cpu_dec(bpf_raw_tp_nest_level);
1569 }
1570 
1571 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1572 	   struct bpf_map *, map, u64, flags, void *, data, u64, size)
1573 {
1574 	struct pt_regs *regs = get_bpf_raw_tp_regs();
1575 	int ret;
1576 
1577 	if (IS_ERR(regs))
1578 		return PTR_ERR(regs);
1579 
1580 	perf_fetch_caller_regs(regs);
1581 	ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1582 
1583 	put_bpf_raw_tp_regs();
1584 	return ret;
1585 }
1586 
1587 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1588 	.func		= bpf_perf_event_output_raw_tp,
1589 	.gpl_only	= true,
1590 	.ret_type	= RET_INTEGER,
1591 	.arg1_type	= ARG_PTR_TO_CTX,
1592 	.arg2_type	= ARG_CONST_MAP_PTR,
1593 	.arg3_type	= ARG_ANYTHING,
1594 	.arg4_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1595 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
1596 };
1597 
1598 extern const struct bpf_func_proto bpf_skb_output_proto;
1599 extern const struct bpf_func_proto bpf_xdp_output_proto;
1600 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1601 
1602 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1603 	   struct bpf_map *, map, u64, flags)
1604 {
1605 	struct pt_regs *regs = get_bpf_raw_tp_regs();
1606 	int ret;
1607 
1608 	if (IS_ERR(regs))
1609 		return PTR_ERR(regs);
1610 
1611 	perf_fetch_caller_regs(regs);
1612 	/* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1613 	ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1614 			      flags, 0, 0);
1615 	put_bpf_raw_tp_regs();
1616 	return ret;
1617 }
1618 
1619 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1620 	.func		= bpf_get_stackid_raw_tp,
1621 	.gpl_only	= true,
1622 	.ret_type	= RET_INTEGER,
1623 	.arg1_type	= ARG_PTR_TO_CTX,
1624 	.arg2_type	= ARG_CONST_MAP_PTR,
1625 	.arg3_type	= ARG_ANYTHING,
1626 };
1627 
1628 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1629 	   void *, buf, u32, size, u64, flags)
1630 {
1631 	struct pt_regs *regs = get_bpf_raw_tp_regs();
1632 	int ret;
1633 
1634 	if (IS_ERR(regs))
1635 		return PTR_ERR(regs);
1636 
1637 	perf_fetch_caller_regs(regs);
1638 	ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1639 			    (unsigned long) size, flags, 0);
1640 	put_bpf_raw_tp_regs();
1641 	return ret;
1642 }
1643 
1644 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1645 	.func		= bpf_get_stack_raw_tp,
1646 	.gpl_only	= true,
1647 	.ret_type	= RET_INTEGER,
1648 	.arg1_type	= ARG_PTR_TO_CTX,
1649 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1650 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
1651 	.arg4_type	= ARG_ANYTHING,
1652 };
1653 
1654 static const struct bpf_func_proto *
1655 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1656 {
1657 	switch (func_id) {
1658 	case BPF_FUNC_perf_event_output:
1659 		return &bpf_perf_event_output_proto_raw_tp;
1660 	case BPF_FUNC_get_stackid:
1661 		return &bpf_get_stackid_proto_raw_tp;
1662 	case BPF_FUNC_get_stack:
1663 		return &bpf_get_stack_proto_raw_tp;
1664 	default:
1665 		return bpf_tracing_func_proto(func_id, prog);
1666 	}
1667 }
1668 
1669 const struct bpf_func_proto *
1670 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1671 {
1672 	const struct bpf_func_proto *fn;
1673 
1674 	switch (func_id) {
1675 #ifdef CONFIG_NET
1676 	case BPF_FUNC_skb_output:
1677 		return &bpf_skb_output_proto;
1678 	case BPF_FUNC_xdp_output:
1679 		return &bpf_xdp_output_proto;
1680 	case BPF_FUNC_skc_to_tcp6_sock:
1681 		return &bpf_skc_to_tcp6_sock_proto;
1682 	case BPF_FUNC_skc_to_tcp_sock:
1683 		return &bpf_skc_to_tcp_sock_proto;
1684 	case BPF_FUNC_skc_to_tcp_timewait_sock:
1685 		return &bpf_skc_to_tcp_timewait_sock_proto;
1686 	case BPF_FUNC_skc_to_tcp_request_sock:
1687 		return &bpf_skc_to_tcp_request_sock_proto;
1688 	case BPF_FUNC_skc_to_udp6_sock:
1689 		return &bpf_skc_to_udp6_sock_proto;
1690 	case BPF_FUNC_skc_to_unix_sock:
1691 		return &bpf_skc_to_unix_sock_proto;
1692 	case BPF_FUNC_sk_storage_get:
1693 		return &bpf_sk_storage_get_tracing_proto;
1694 	case BPF_FUNC_sk_storage_delete:
1695 		return &bpf_sk_storage_delete_tracing_proto;
1696 	case BPF_FUNC_sock_from_file:
1697 		return &bpf_sock_from_file_proto;
1698 	case BPF_FUNC_get_socket_cookie:
1699 		return &bpf_get_socket_ptr_cookie_proto;
1700 	case BPF_FUNC_xdp_get_buff_len:
1701 		return &bpf_xdp_get_buff_len_trace_proto;
1702 #endif
1703 	case BPF_FUNC_seq_printf:
1704 		return prog->expected_attach_type == BPF_TRACE_ITER ?
1705 		       &bpf_seq_printf_proto :
1706 		       NULL;
1707 	case BPF_FUNC_seq_write:
1708 		return prog->expected_attach_type == BPF_TRACE_ITER ?
1709 		       &bpf_seq_write_proto :
1710 		       NULL;
1711 	case BPF_FUNC_seq_printf_btf:
1712 		return prog->expected_attach_type == BPF_TRACE_ITER ?
1713 		       &bpf_seq_printf_btf_proto :
1714 		       NULL;
1715 	case BPF_FUNC_d_path:
1716 		return &bpf_d_path_proto;
1717 	case BPF_FUNC_get_func_arg:
1718 		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1719 	case BPF_FUNC_get_func_ret:
1720 		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1721 	case BPF_FUNC_get_func_arg_cnt:
1722 		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
1723 	default:
1724 		fn = raw_tp_prog_func_proto(func_id, prog);
1725 		if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
1726 			fn = bpf_iter_get_func_proto(func_id, prog);
1727 		return fn;
1728 	}
1729 }
1730 
1731 static bool raw_tp_prog_is_valid_access(int off, int size,
1732 					enum bpf_access_type type,
1733 					const struct bpf_prog *prog,
1734 					struct bpf_insn_access_aux *info)
1735 {
1736 	return bpf_tracing_ctx_access(off, size, type);
1737 }
1738 
1739 static bool tracing_prog_is_valid_access(int off, int size,
1740 					 enum bpf_access_type type,
1741 					 const struct bpf_prog *prog,
1742 					 struct bpf_insn_access_aux *info)
1743 {
1744 	return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
1745 }
1746 
1747 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
1748 				     const union bpf_attr *kattr,
1749 				     union bpf_attr __user *uattr)
1750 {
1751 	return -ENOTSUPP;
1752 }
1753 
1754 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
1755 	.get_func_proto  = raw_tp_prog_func_proto,
1756 	.is_valid_access = raw_tp_prog_is_valid_access,
1757 };
1758 
1759 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
1760 #ifdef CONFIG_NET
1761 	.test_run = bpf_prog_test_run_raw_tp,
1762 #endif
1763 };
1764 
1765 const struct bpf_verifier_ops tracing_verifier_ops = {
1766 	.get_func_proto  = tracing_prog_func_proto,
1767 	.is_valid_access = tracing_prog_is_valid_access,
1768 };
1769 
1770 const struct bpf_prog_ops tracing_prog_ops = {
1771 	.test_run = bpf_prog_test_run_tracing,
1772 };
1773 
1774 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
1775 						 enum bpf_access_type type,
1776 						 const struct bpf_prog *prog,
1777 						 struct bpf_insn_access_aux *info)
1778 {
1779 	if (off == 0) {
1780 		if (size != sizeof(u64) || type != BPF_READ)
1781 			return false;
1782 		info->reg_type = PTR_TO_TP_BUFFER;
1783 	}
1784 	return raw_tp_prog_is_valid_access(off, size, type, prog, info);
1785 }
1786 
1787 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
1788 	.get_func_proto  = raw_tp_prog_func_proto,
1789 	.is_valid_access = raw_tp_writable_prog_is_valid_access,
1790 };
1791 
1792 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
1793 };
1794 
1795 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1796 				    const struct bpf_prog *prog,
1797 				    struct bpf_insn_access_aux *info)
1798 {
1799 	const int size_u64 = sizeof(u64);
1800 
1801 	if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
1802 		return false;
1803 	if (type != BPF_READ)
1804 		return false;
1805 	if (off % size != 0) {
1806 		if (sizeof(unsigned long) != 4)
1807 			return false;
1808 		if (size != 8)
1809 			return false;
1810 		if (off % size != 4)
1811 			return false;
1812 	}
1813 
1814 	switch (off) {
1815 	case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
1816 		bpf_ctx_record_field_size(info, size_u64);
1817 		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1818 			return false;
1819 		break;
1820 	case bpf_ctx_range(struct bpf_perf_event_data, addr):
1821 		bpf_ctx_record_field_size(info, size_u64);
1822 		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
1823 			return false;
1824 		break;
1825 	default:
1826 		if (size != sizeof(long))
1827 			return false;
1828 	}
1829 
1830 	return true;
1831 }
1832 
1833 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
1834 				      const struct bpf_insn *si,
1835 				      struct bpf_insn *insn_buf,
1836 				      struct bpf_prog *prog, u32 *target_size)
1837 {
1838 	struct bpf_insn *insn = insn_buf;
1839 
1840 	switch (si->off) {
1841 	case offsetof(struct bpf_perf_event_data, sample_period):
1842 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1843 						       data), si->dst_reg, si->src_reg,
1844 				      offsetof(struct bpf_perf_event_data_kern, data));
1845 		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1846 				      bpf_target_off(struct perf_sample_data, period, 8,
1847 						     target_size));
1848 		break;
1849 	case offsetof(struct bpf_perf_event_data, addr):
1850 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1851 						       data), si->dst_reg, si->src_reg,
1852 				      offsetof(struct bpf_perf_event_data_kern, data));
1853 		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
1854 				      bpf_target_off(struct perf_sample_data, addr, 8,
1855 						     target_size));
1856 		break;
1857 	default:
1858 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
1859 						       regs), si->dst_reg, si->src_reg,
1860 				      offsetof(struct bpf_perf_event_data_kern, regs));
1861 		*insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
1862 				      si->off);
1863 		break;
1864 	}
1865 
1866 	return insn - insn_buf;
1867 }
1868 
1869 const struct bpf_verifier_ops perf_event_verifier_ops = {
1870 	.get_func_proto		= pe_prog_func_proto,
1871 	.is_valid_access	= pe_prog_is_valid_access,
1872 	.convert_ctx_access	= pe_prog_convert_ctx_access,
1873 };
1874 
1875 const struct bpf_prog_ops perf_event_prog_ops = {
1876 };
1877 
1878 static DEFINE_MUTEX(bpf_event_mutex);
1879 
1880 #define BPF_TRACE_MAX_PROGS 64
1881 
1882 int perf_event_attach_bpf_prog(struct perf_event *event,
1883 			       struct bpf_prog *prog,
1884 			       u64 bpf_cookie)
1885 {
1886 	struct bpf_prog_array *old_array;
1887 	struct bpf_prog_array *new_array;
1888 	int ret = -EEXIST;
1889 
1890 	/*
1891 	 * Kprobe override only works if they are on the function entry,
1892 	 * and only if they are on the opt-in list.
1893 	 */
1894 	if (prog->kprobe_override &&
1895 	    (!trace_kprobe_on_func_entry(event->tp_event) ||
1896 	     !trace_kprobe_error_injectable(event->tp_event)))
1897 		return -EINVAL;
1898 
1899 	mutex_lock(&bpf_event_mutex);
1900 
1901 	if (event->prog)
1902 		goto unlock;
1903 
1904 	old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1905 	if (old_array &&
1906 	    bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
1907 		ret = -E2BIG;
1908 		goto unlock;
1909 	}
1910 
1911 	ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
1912 	if (ret < 0)
1913 		goto unlock;
1914 
1915 	/* set the new array to event->tp_event and set event->prog */
1916 	event->prog = prog;
1917 	event->bpf_cookie = bpf_cookie;
1918 	rcu_assign_pointer(event->tp_event->prog_array, new_array);
1919 	bpf_prog_array_free(old_array);
1920 
1921 unlock:
1922 	mutex_unlock(&bpf_event_mutex);
1923 	return ret;
1924 }
1925 
1926 void perf_event_detach_bpf_prog(struct perf_event *event)
1927 {
1928 	struct bpf_prog_array *old_array;
1929 	struct bpf_prog_array *new_array;
1930 	int ret;
1931 
1932 	mutex_lock(&bpf_event_mutex);
1933 
1934 	if (!event->prog)
1935 		goto unlock;
1936 
1937 	old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
1938 	ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
1939 	if (ret == -ENOENT)
1940 		goto unlock;
1941 	if (ret < 0) {
1942 		bpf_prog_array_delete_safe(old_array, event->prog);
1943 	} else {
1944 		rcu_assign_pointer(event->tp_event->prog_array, new_array);
1945 		bpf_prog_array_free(old_array);
1946 	}
1947 
1948 	bpf_prog_put(event->prog);
1949 	event->prog = NULL;
1950 
1951 unlock:
1952 	mutex_unlock(&bpf_event_mutex);
1953 }
1954 
1955 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
1956 {
1957 	struct perf_event_query_bpf __user *uquery = info;
1958 	struct perf_event_query_bpf query = {};
1959 	struct bpf_prog_array *progs;
1960 	u32 *ids, prog_cnt, ids_len;
1961 	int ret;
1962 
1963 	if (!perfmon_capable())
1964 		return -EPERM;
1965 	if (event->attr.type != PERF_TYPE_TRACEPOINT)
1966 		return -EINVAL;
1967 	if (copy_from_user(&query, uquery, sizeof(query)))
1968 		return -EFAULT;
1969 
1970 	ids_len = query.ids_len;
1971 	if (ids_len > BPF_TRACE_MAX_PROGS)
1972 		return -E2BIG;
1973 	ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
1974 	if (!ids)
1975 		return -ENOMEM;
1976 	/*
1977 	 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
1978 	 * is required when user only wants to check for uquery->prog_cnt.
1979 	 * There is no need to check for it since the case is handled
1980 	 * gracefully in bpf_prog_array_copy_info.
1981 	 */
1982 
1983 	mutex_lock(&bpf_event_mutex);
1984 	progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
1985 	ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
1986 	mutex_unlock(&bpf_event_mutex);
1987 
1988 	if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
1989 	    copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
1990 		ret = -EFAULT;
1991 
1992 	kfree(ids);
1993 	return ret;
1994 }
1995 
1996 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
1997 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
1998 
1999 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2000 {
2001 	struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2002 
2003 	for (; btp < __stop__bpf_raw_tp; btp++) {
2004 		if (!strcmp(btp->tp->name, name))
2005 			return btp;
2006 	}
2007 
2008 	return bpf_get_raw_tracepoint_module(name);
2009 }
2010 
2011 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2012 {
2013 	struct module *mod;
2014 
2015 	preempt_disable();
2016 	mod = __module_address((unsigned long)btp);
2017 	module_put(mod);
2018 	preempt_enable();
2019 }
2020 
2021 static __always_inline
2022 void __bpf_trace_run(struct bpf_prog *prog, u64 *args)
2023 {
2024 	cant_sleep();
2025 	rcu_read_lock();
2026 	(void) bpf_prog_run(prog, args);
2027 	rcu_read_unlock();
2028 }
2029 
2030 #define UNPACK(...)			__VA_ARGS__
2031 #define REPEAT_1(FN, DL, X, ...)	FN(X)
2032 #define REPEAT_2(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2033 #define REPEAT_3(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2034 #define REPEAT_4(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2035 #define REPEAT_5(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2036 #define REPEAT_6(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2037 #define REPEAT_7(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2038 #define REPEAT_8(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2039 #define REPEAT_9(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2040 #define REPEAT_10(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2041 #define REPEAT_11(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2042 #define REPEAT_12(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2043 #define REPEAT(X, FN, DL, ...)		REPEAT_##X(FN, DL, __VA_ARGS__)
2044 
2045 #define SARG(X)		u64 arg##X
2046 #define COPY(X)		args[X] = arg##X
2047 
2048 #define __DL_COM	(,)
2049 #define __DL_SEM	(;)
2050 
2051 #define __SEQ_0_11	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2052 
2053 #define BPF_TRACE_DEFN_x(x)						\
2054 	void bpf_trace_run##x(struct bpf_prog *prog,			\
2055 			      REPEAT(x, SARG, __DL_COM, __SEQ_0_11))	\
2056 	{								\
2057 		u64 args[x];						\
2058 		REPEAT(x, COPY, __DL_SEM, __SEQ_0_11);			\
2059 		__bpf_trace_run(prog, args);				\
2060 	}								\
2061 	EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2062 BPF_TRACE_DEFN_x(1);
2063 BPF_TRACE_DEFN_x(2);
2064 BPF_TRACE_DEFN_x(3);
2065 BPF_TRACE_DEFN_x(4);
2066 BPF_TRACE_DEFN_x(5);
2067 BPF_TRACE_DEFN_x(6);
2068 BPF_TRACE_DEFN_x(7);
2069 BPF_TRACE_DEFN_x(8);
2070 BPF_TRACE_DEFN_x(9);
2071 BPF_TRACE_DEFN_x(10);
2072 BPF_TRACE_DEFN_x(11);
2073 BPF_TRACE_DEFN_x(12);
2074 
2075 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2076 {
2077 	struct tracepoint *tp = btp->tp;
2078 
2079 	/*
2080 	 * check that program doesn't access arguments beyond what's
2081 	 * available in this tracepoint
2082 	 */
2083 	if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2084 		return -EINVAL;
2085 
2086 	if (prog->aux->max_tp_access > btp->writable_size)
2087 		return -EINVAL;
2088 
2089 	return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func,
2090 						   prog);
2091 }
2092 
2093 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2094 {
2095 	return __bpf_probe_register(btp, prog);
2096 }
2097 
2098 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog)
2099 {
2100 	return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog);
2101 }
2102 
2103 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2104 			    u32 *fd_type, const char **buf,
2105 			    u64 *probe_offset, u64 *probe_addr)
2106 {
2107 	bool is_tracepoint, is_syscall_tp;
2108 	struct bpf_prog *prog;
2109 	int flags, err = 0;
2110 
2111 	prog = event->prog;
2112 	if (!prog)
2113 		return -ENOENT;
2114 
2115 	/* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2116 	if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2117 		return -EOPNOTSUPP;
2118 
2119 	*prog_id = prog->aux->id;
2120 	flags = event->tp_event->flags;
2121 	is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2122 	is_syscall_tp = is_syscall_trace_event(event->tp_event);
2123 
2124 	if (is_tracepoint || is_syscall_tp) {
2125 		*buf = is_tracepoint ? event->tp_event->tp->name
2126 				     : event->tp_event->name;
2127 		*fd_type = BPF_FD_TYPE_TRACEPOINT;
2128 		*probe_offset = 0x0;
2129 		*probe_addr = 0x0;
2130 	} else {
2131 		/* kprobe/uprobe */
2132 		err = -EOPNOTSUPP;
2133 #ifdef CONFIG_KPROBE_EVENTS
2134 		if (flags & TRACE_EVENT_FL_KPROBE)
2135 			err = bpf_get_kprobe_info(event, fd_type, buf,
2136 						  probe_offset, probe_addr,
2137 						  event->attr.type == PERF_TYPE_TRACEPOINT);
2138 #endif
2139 #ifdef CONFIG_UPROBE_EVENTS
2140 		if (flags & TRACE_EVENT_FL_UPROBE)
2141 			err = bpf_get_uprobe_info(event, fd_type, buf,
2142 						  probe_offset,
2143 						  event->attr.type == PERF_TYPE_TRACEPOINT);
2144 #endif
2145 	}
2146 
2147 	return err;
2148 }
2149 
2150 static int __init send_signal_irq_work_init(void)
2151 {
2152 	int cpu;
2153 	struct send_signal_irq_work *work;
2154 
2155 	for_each_possible_cpu(cpu) {
2156 		work = per_cpu_ptr(&send_signal_work, cpu);
2157 		init_irq_work(&work->irq_work, do_bpf_send_signal);
2158 	}
2159 	return 0;
2160 }
2161 
2162 subsys_initcall(send_signal_irq_work_init);
2163 
2164 #ifdef CONFIG_MODULES
2165 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2166 			    void *module)
2167 {
2168 	struct bpf_trace_module *btm, *tmp;
2169 	struct module *mod = module;
2170 	int ret = 0;
2171 
2172 	if (mod->num_bpf_raw_events == 0 ||
2173 	    (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2174 		goto out;
2175 
2176 	mutex_lock(&bpf_module_mutex);
2177 
2178 	switch (op) {
2179 	case MODULE_STATE_COMING:
2180 		btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2181 		if (btm) {
2182 			btm->module = module;
2183 			list_add(&btm->list, &bpf_trace_modules);
2184 		} else {
2185 			ret = -ENOMEM;
2186 		}
2187 		break;
2188 	case MODULE_STATE_GOING:
2189 		list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2190 			if (btm->module == module) {
2191 				list_del(&btm->list);
2192 				kfree(btm);
2193 				break;
2194 			}
2195 		}
2196 		break;
2197 	}
2198 
2199 	mutex_unlock(&bpf_module_mutex);
2200 
2201 out:
2202 	return notifier_from_errno(ret);
2203 }
2204 
2205 static struct notifier_block bpf_module_nb = {
2206 	.notifier_call = bpf_event_notify,
2207 };
2208 
2209 static int __init bpf_event_init(void)
2210 {
2211 	register_module_notifier(&bpf_module_nb);
2212 	return 0;
2213 }
2214 
2215 fs_initcall(bpf_event_init);
2216 #endif /* CONFIG_MODULES */
2217 
2218 #ifdef CONFIG_FPROBE
2219 struct bpf_kprobe_multi_link {
2220 	struct bpf_link link;
2221 	struct fprobe fp;
2222 	unsigned long *addrs;
2223 	/*
2224 	 * The run_ctx here is used to get struct bpf_kprobe_multi_link in
2225 	 * get_attach_cookie helper, so it can't be used to store data.
2226 	 */
2227 	struct bpf_run_ctx run_ctx;
2228 	u64 *cookies;
2229 	u32 cnt;
2230 };
2231 
2232 static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2233 {
2234 	struct bpf_kprobe_multi_link *kmulti_link;
2235 
2236 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2237 	unregister_fprobe(&kmulti_link->fp);
2238 }
2239 
2240 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2241 {
2242 	struct bpf_kprobe_multi_link *kmulti_link;
2243 
2244 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2245 	kvfree(kmulti_link->addrs);
2246 	kvfree(kmulti_link->cookies);
2247 	kfree(kmulti_link);
2248 }
2249 
2250 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2251 	.release = bpf_kprobe_multi_link_release,
2252 	.dealloc = bpf_kprobe_multi_link_dealloc,
2253 };
2254 
2255 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2256 {
2257 	const struct bpf_kprobe_multi_link *link = priv;
2258 	unsigned long *addr_a = a, *addr_b = b;
2259 	u64 *cookie_a, *cookie_b;
2260 	unsigned long tmp1;
2261 	u64 tmp2;
2262 
2263 	cookie_a = link->cookies + (addr_a - link->addrs);
2264 	cookie_b = link->cookies + (addr_b - link->addrs);
2265 
2266 	/* swap addr_a/addr_b and cookie_a/cookie_b values */
2267 	tmp1 = *addr_a; *addr_a = *addr_b; *addr_b = tmp1;
2268 	tmp2 = *cookie_a; *cookie_a = *cookie_b; *cookie_b = tmp2;
2269 }
2270 
2271 static int __bpf_kprobe_multi_cookie_cmp(const void *a, const void *b)
2272 {
2273 	const unsigned long *addr_a = a, *addr_b = b;
2274 
2275 	if (*addr_a == *addr_b)
2276 		return 0;
2277 	return *addr_a < *addr_b ? -1 : 1;
2278 }
2279 
2280 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2281 {
2282 	return __bpf_kprobe_multi_cookie_cmp(a, b);
2283 }
2284 
2285 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx, u64 ip)
2286 {
2287 	struct bpf_kprobe_multi_link *link;
2288 	unsigned long *addr;
2289 	u64 *cookie;
2290 
2291 	if (WARN_ON_ONCE(!ctx))
2292 		return 0;
2293 	link = container_of(ctx, struct bpf_kprobe_multi_link, run_ctx);
2294 	if (!link->cookies)
2295 		return 0;
2296 	addr = bsearch(&ip, link->addrs, link->cnt, sizeof(ip),
2297 		       __bpf_kprobe_multi_cookie_cmp);
2298 	if (!addr)
2299 		return 0;
2300 	cookie = link->cookies + (addr - link->addrs);
2301 	return *cookie;
2302 }
2303 
2304 static int
2305 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2306 			   struct pt_regs *regs)
2307 {
2308 	struct bpf_run_ctx *old_run_ctx;
2309 	int err;
2310 
2311 	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2312 		err = 0;
2313 		goto out;
2314 	}
2315 
2316 	migrate_disable();
2317 	rcu_read_lock();
2318 	old_run_ctx = bpf_set_run_ctx(&link->run_ctx);
2319 	err = bpf_prog_run(link->link.prog, regs);
2320 	bpf_reset_run_ctx(old_run_ctx);
2321 	rcu_read_unlock();
2322 	migrate_enable();
2323 
2324  out:
2325 	__this_cpu_dec(bpf_prog_active);
2326 	return err;
2327 }
2328 
2329 static void
2330 kprobe_multi_link_handler(struct fprobe *fp, unsigned long entry_ip,
2331 			  struct pt_regs *regs)
2332 {
2333 	unsigned long saved_ip = instruction_pointer(regs);
2334 	struct bpf_kprobe_multi_link *link;
2335 
2336 	/*
2337 	 * Because fprobe's regs->ip is set to the next instruction of
2338 	 * dynamic-ftrace instruction, correct entry ip must be set, so
2339 	 * that the bpf program can access entry address via regs as same
2340 	 * as kprobes.
2341 	 *
2342 	 * Both kprobe and kretprobe see the entry ip of traced function
2343 	 * as instruction pointer.
2344 	 */
2345 	instruction_pointer_set(regs, entry_ip);
2346 
2347 	link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2348 	kprobe_multi_link_prog_run(link, regs);
2349 
2350 	instruction_pointer_set(regs, saved_ip);
2351 }
2352 
2353 static int
2354 kprobe_multi_resolve_syms(const void *usyms, u32 cnt,
2355 			  unsigned long *addrs)
2356 {
2357 	unsigned long addr, size;
2358 	const char **syms;
2359 	int err = -ENOMEM;
2360 	unsigned int i;
2361 	char *func;
2362 
2363 	size = cnt * sizeof(*syms);
2364 	syms = kvzalloc(size, GFP_KERNEL);
2365 	if (!syms)
2366 		return -ENOMEM;
2367 
2368 	func = kmalloc(KSYM_NAME_LEN, GFP_KERNEL);
2369 	if (!func)
2370 		goto error;
2371 
2372 	if (copy_from_user(syms, usyms, size)) {
2373 		err = -EFAULT;
2374 		goto error;
2375 	}
2376 
2377 	for (i = 0; i < cnt; i++) {
2378 		err = strncpy_from_user(func, syms[i], KSYM_NAME_LEN);
2379 		if (err == KSYM_NAME_LEN)
2380 			err = -E2BIG;
2381 		if (err < 0)
2382 			goto error;
2383 		err = -EINVAL;
2384 		addr = kallsyms_lookup_name(func);
2385 		if (!addr)
2386 			goto error;
2387 		if (!kallsyms_lookup_size_offset(addr, &size, NULL))
2388 			goto error;
2389 		addr = ftrace_location_range(addr, addr + size - 1);
2390 		if (!addr)
2391 			goto error;
2392 		addrs[i] = addr;
2393 	}
2394 
2395 	err = 0;
2396 error:
2397 	kvfree(syms);
2398 	kfree(func);
2399 	return err;
2400 }
2401 
2402 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2403 {
2404 	struct bpf_kprobe_multi_link *link = NULL;
2405 	struct bpf_link_primer link_primer;
2406 	void __user *ucookies;
2407 	unsigned long *addrs;
2408 	u32 flags, cnt, size;
2409 	void __user *uaddrs;
2410 	u64 *cookies = NULL;
2411 	void __user *usyms;
2412 	int err;
2413 
2414 	/* no support for 32bit archs yet */
2415 	if (sizeof(u64) != sizeof(void *))
2416 		return -EOPNOTSUPP;
2417 
2418 	if (prog->expected_attach_type != BPF_TRACE_KPROBE_MULTI)
2419 		return -EINVAL;
2420 
2421 	flags = attr->link_create.kprobe_multi.flags;
2422 	if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2423 		return -EINVAL;
2424 
2425 	uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2426 	usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2427 	if (!!uaddrs == !!usyms)
2428 		return -EINVAL;
2429 
2430 	cnt = attr->link_create.kprobe_multi.cnt;
2431 	if (!cnt)
2432 		return -EINVAL;
2433 
2434 	size = cnt * sizeof(*addrs);
2435 	addrs = kvmalloc(size, GFP_KERNEL);
2436 	if (!addrs)
2437 		return -ENOMEM;
2438 
2439 	if (uaddrs) {
2440 		if (copy_from_user(addrs, uaddrs, size)) {
2441 			err = -EFAULT;
2442 			goto error;
2443 		}
2444 	} else {
2445 		err = kprobe_multi_resolve_syms(usyms, cnt, addrs);
2446 		if (err)
2447 			goto error;
2448 	}
2449 
2450 	ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2451 	if (ucookies) {
2452 		cookies = kvmalloc(size, GFP_KERNEL);
2453 		if (!cookies) {
2454 			err = -ENOMEM;
2455 			goto error;
2456 		}
2457 		if (copy_from_user(cookies, ucookies, size)) {
2458 			err = -EFAULT;
2459 			goto error;
2460 		}
2461 	}
2462 
2463 	link = kzalloc(sizeof(*link), GFP_KERNEL);
2464 	if (!link) {
2465 		err = -ENOMEM;
2466 		goto error;
2467 	}
2468 
2469 	bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
2470 		      &bpf_kprobe_multi_link_lops, prog);
2471 
2472 	err = bpf_link_prime(&link->link, &link_primer);
2473 	if (err)
2474 		goto error;
2475 
2476 	if (flags & BPF_F_KPROBE_MULTI_RETURN)
2477 		link->fp.exit_handler = kprobe_multi_link_handler;
2478 	else
2479 		link->fp.entry_handler = kprobe_multi_link_handler;
2480 
2481 	link->addrs = addrs;
2482 	link->cookies = cookies;
2483 	link->cnt = cnt;
2484 
2485 	if (cookies) {
2486 		/*
2487 		 * Sorting addresses will trigger sorting cookies as well
2488 		 * (check bpf_kprobe_multi_cookie_swap). This way we can
2489 		 * find cookie based on the address in bpf_get_attach_cookie
2490 		 * helper.
2491 		 */
2492 		sort_r(addrs, cnt, sizeof(*addrs),
2493 		       bpf_kprobe_multi_cookie_cmp,
2494 		       bpf_kprobe_multi_cookie_swap,
2495 		       link);
2496 	}
2497 
2498 	err = register_fprobe_ips(&link->fp, addrs, cnt);
2499 	if (err) {
2500 		bpf_link_cleanup(&link_primer);
2501 		return err;
2502 	}
2503 
2504 	return bpf_link_settle(&link_primer);
2505 
2506 error:
2507 	kfree(link);
2508 	kvfree(addrs);
2509 	kvfree(cookies);
2510 	return err;
2511 }
2512 #else /* !CONFIG_FPROBE */
2513 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2514 {
2515 	return -EOPNOTSUPP;
2516 }
2517 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx, u64 ip)
2518 {
2519 	return 0;
2520 }
2521 #endif
2522