xref: /linux/kernel/trace/bpf_trace.c (revision 58ecb3a789fdc2b015112a31a91aa674c040a5ba)
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_verifier.h>
10 #include <linux/bpf_perf_event.h>
11 #include <linux/btf.h>
12 #include <linux/filter.h>
13 #include <linux/uaccess.h>
14 #include <linux/ctype.h>
15 #include <linux/kprobes.h>
16 #include <linux/spinlock.h>
17 #include <linux/syscalls.h>
18 #include <linux/error-injection.h>
19 #include <linux/btf_ids.h>
20 #include <linux/bpf_lsm.h>
21 #include <linux/fprobe.h>
22 #include <linux/bsearch.h>
23 #include <linux/sort.h>
24 #include <linux/key.h>
25 #include <linux/verification.h>
26 #include <linux/namei.h>
27 
28 #include <net/bpf_sk_storage.h>
29 
30 #include <uapi/linux/bpf.h>
31 #include <uapi/linux/btf.h>
32 
33 #include <asm/tlb.h>
34 
35 #include "trace_probe.h"
36 #include "trace.h"
37 
38 #define CREATE_TRACE_POINTS
39 #include "bpf_trace.h"
40 
41 #define bpf_event_rcu_dereference(p)					\
42 	rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex))
43 
44 #define MAX_UPROBE_MULTI_CNT (1U << 20)
45 #define MAX_KPROBE_MULTI_CNT (1U << 20)
46 
47 #ifdef CONFIG_MODULES
48 struct bpf_trace_module {
49 	struct module *module;
50 	struct list_head list;
51 };
52 
53 static LIST_HEAD(bpf_trace_modules);
54 static DEFINE_MUTEX(bpf_module_mutex);
55 
56 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
57 {
58 	struct bpf_raw_event_map *btp, *ret = NULL;
59 	struct bpf_trace_module *btm;
60 	unsigned int i;
61 
62 	mutex_lock(&bpf_module_mutex);
63 	list_for_each_entry(btm, &bpf_trace_modules, list) {
64 		for (i = 0; i < btm->module->num_bpf_raw_events; ++i) {
65 			btp = &btm->module->bpf_raw_events[i];
66 			if (!strcmp(btp->tp->name, name)) {
67 				if (try_module_get(btm->module))
68 					ret = btp;
69 				goto out;
70 			}
71 		}
72 	}
73 out:
74 	mutex_unlock(&bpf_module_mutex);
75 	return ret;
76 }
77 #else
78 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
79 {
80 	return NULL;
81 }
82 #endif /* CONFIG_MODULES */
83 
84 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
85 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
86 
87 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
88 				  u64 flags, const struct btf **btf,
89 				  s32 *btf_id);
90 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx);
91 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
92 
93 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx);
94 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx);
95 
96 /**
97  * trace_call_bpf - invoke BPF program
98  * @call: tracepoint event
99  * @ctx: opaque context pointer
100  *
101  * kprobe handlers execute BPF programs via this helper.
102  * Can be used from static tracepoints in the future.
103  *
104  * Return: BPF programs always return an integer which is interpreted by
105  * kprobe handler as:
106  * 0 - return from kprobe (event is filtered out)
107  * 1 - store kprobe event into ring buffer
108  * Other values are reserved and currently alias to 1
109  */
110 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
111 {
112 	unsigned int ret;
113 
114 	cant_sleep();
115 
116 	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
117 		/*
118 		 * since some bpf program is already running on this cpu,
119 		 * don't call into another bpf program (same or different)
120 		 * and don't send kprobe event into ring-buffer,
121 		 * so return zero here
122 		 */
123 		rcu_read_lock();
124 		bpf_prog_inc_misses_counters(rcu_dereference(call->prog_array));
125 		rcu_read_unlock();
126 		ret = 0;
127 		goto out;
128 	}
129 
130 	/*
131 	 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock
132 	 * to all call sites, we did a bpf_prog_array_valid() there to check
133 	 * whether call->prog_array is empty or not, which is
134 	 * a heuristic to speed up execution.
135 	 *
136 	 * If bpf_prog_array_valid() fetched prog_array was
137 	 * non-NULL, we go into trace_call_bpf() and do the actual
138 	 * proper rcu_dereference() under RCU lock.
139 	 * If it turns out that prog_array is NULL then, we bail out.
140 	 * For the opposite, if the bpf_prog_array_valid() fetched pointer
141 	 * was NULL, you'll skip the prog_array with the risk of missing
142 	 * out of events when it was updated in between this and the
143 	 * rcu_dereference() which is accepted risk.
144 	 */
145 	rcu_read_lock();
146 	ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
147 				 ctx, bpf_prog_run);
148 	rcu_read_unlock();
149 
150  out:
151 	__this_cpu_dec(bpf_prog_active);
152 
153 	return ret;
154 }
155 
156 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
157 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
158 {
159 	regs_set_return_value(regs, rc);
160 	override_function_with_return(regs);
161 	return 0;
162 }
163 
164 static const struct bpf_func_proto bpf_override_return_proto = {
165 	.func		= bpf_override_return,
166 	.gpl_only	= true,
167 	.ret_type	= RET_INTEGER,
168 	.arg1_type	= ARG_PTR_TO_CTX,
169 	.arg2_type	= ARG_ANYTHING,
170 };
171 #endif
172 
173 static __always_inline int
174 bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr)
175 {
176 	int ret;
177 
178 	ret = copy_from_user_nofault(dst, unsafe_ptr, size);
179 	if (unlikely(ret < 0))
180 		memset(dst, 0, size);
181 	return ret;
182 }
183 
184 BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size,
185 	   const void __user *, unsafe_ptr)
186 {
187 	return bpf_probe_read_user_common(dst, size, unsafe_ptr);
188 }
189 
190 const struct bpf_func_proto bpf_probe_read_user_proto = {
191 	.func		= bpf_probe_read_user,
192 	.gpl_only	= true,
193 	.ret_type	= RET_INTEGER,
194 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
195 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
196 	.arg3_type	= ARG_ANYTHING,
197 };
198 
199 static __always_inline int
200 bpf_probe_read_user_str_common(void *dst, u32 size,
201 			       const void __user *unsafe_ptr)
202 {
203 	int ret;
204 
205 	/*
206 	 * NB: We rely on strncpy_from_user() not copying junk past the NUL
207 	 * terminator into `dst`.
208 	 *
209 	 * strncpy_from_user() does long-sized strides in the fast path. If the
210 	 * strncpy does not mask out the bytes after the NUL in `unsafe_ptr`,
211 	 * then there could be junk after the NUL in `dst`. If user takes `dst`
212 	 * and keys a hash map with it, then semantically identical strings can
213 	 * occupy multiple entries in the map.
214 	 */
215 	ret = strncpy_from_user_nofault(dst, unsafe_ptr, size);
216 	if (unlikely(ret < 0))
217 		memset(dst, 0, size);
218 	return ret;
219 }
220 
221 BPF_CALL_3(bpf_probe_read_user_str, void *, dst, u32, size,
222 	   const void __user *, unsafe_ptr)
223 {
224 	return bpf_probe_read_user_str_common(dst, size, unsafe_ptr);
225 }
226 
227 const struct bpf_func_proto bpf_probe_read_user_str_proto = {
228 	.func		= bpf_probe_read_user_str,
229 	.gpl_only	= true,
230 	.ret_type	= RET_INTEGER,
231 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
232 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
233 	.arg3_type	= ARG_ANYTHING,
234 };
235 
236 BPF_CALL_3(bpf_probe_read_kernel, void *, dst, u32, size,
237 	   const void *, unsafe_ptr)
238 {
239 	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
240 }
241 
242 const struct bpf_func_proto bpf_probe_read_kernel_proto = {
243 	.func		= bpf_probe_read_kernel,
244 	.gpl_only	= true,
245 	.ret_type	= RET_INTEGER,
246 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
247 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
248 	.arg3_type	= ARG_ANYTHING,
249 };
250 
251 static __always_inline int
252 bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr)
253 {
254 	int ret;
255 
256 	/*
257 	 * The strncpy_from_kernel_nofault() call will likely not fill the
258 	 * entire buffer, but that's okay in this circumstance as we're probing
259 	 * arbitrary memory anyway similar to bpf_probe_read_*() and might
260 	 * as well probe the stack. Thus, memory is explicitly cleared
261 	 * only in error case, so that improper users ignoring return
262 	 * code altogether don't copy garbage; otherwise length of string
263 	 * is returned that can be used for bpf_perf_event_output() et al.
264 	 */
265 	ret = strncpy_from_kernel_nofault(dst, unsafe_ptr, size);
266 	if (unlikely(ret < 0))
267 		memset(dst, 0, size);
268 	return ret;
269 }
270 
271 BPF_CALL_3(bpf_probe_read_kernel_str, void *, dst, u32, size,
272 	   const void *, unsafe_ptr)
273 {
274 	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
275 }
276 
277 const struct bpf_func_proto bpf_probe_read_kernel_str_proto = {
278 	.func		= bpf_probe_read_kernel_str,
279 	.gpl_only	= true,
280 	.ret_type	= RET_INTEGER,
281 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
282 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
283 	.arg3_type	= ARG_ANYTHING,
284 };
285 
286 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
287 BPF_CALL_3(bpf_probe_read_compat, void *, dst, u32, size,
288 	   const void *, unsafe_ptr)
289 {
290 	if ((unsigned long)unsafe_ptr < TASK_SIZE) {
291 		return bpf_probe_read_user_common(dst, size,
292 				(__force void __user *)unsafe_ptr);
293 	}
294 	return bpf_probe_read_kernel_common(dst, size, unsafe_ptr);
295 }
296 
297 static const struct bpf_func_proto bpf_probe_read_compat_proto = {
298 	.func		= bpf_probe_read_compat,
299 	.gpl_only	= true,
300 	.ret_type	= RET_INTEGER,
301 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
302 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
303 	.arg3_type	= ARG_ANYTHING,
304 };
305 
306 BPF_CALL_3(bpf_probe_read_compat_str, void *, dst, u32, size,
307 	   const void *, unsafe_ptr)
308 {
309 	if ((unsigned long)unsafe_ptr < TASK_SIZE) {
310 		return bpf_probe_read_user_str_common(dst, size,
311 				(__force void __user *)unsafe_ptr);
312 	}
313 	return bpf_probe_read_kernel_str_common(dst, size, unsafe_ptr);
314 }
315 
316 static const struct bpf_func_proto bpf_probe_read_compat_str_proto = {
317 	.func		= bpf_probe_read_compat_str,
318 	.gpl_only	= true,
319 	.ret_type	= RET_INTEGER,
320 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
321 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
322 	.arg3_type	= ARG_ANYTHING,
323 };
324 #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */
325 
326 BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src,
327 	   u32, size)
328 {
329 	/*
330 	 * Ensure we're in user context which is safe for the helper to
331 	 * run. This helper has no business in a kthread.
332 	 *
333 	 * access_ok() should prevent writing to non-user memory, but in
334 	 * some situations (nommu, temporary switch, etc) access_ok() does
335 	 * not provide enough validation, hence the check on KERNEL_DS.
336 	 *
337 	 * nmi_uaccess_okay() ensures the probe is not run in an interim
338 	 * state, when the task or mm are switched. This is specifically
339 	 * required to prevent the use of temporary mm.
340 	 */
341 
342 	if (unlikely(in_interrupt() ||
343 		     current->flags & (PF_KTHREAD | PF_EXITING)))
344 		return -EPERM;
345 	if (unlikely(!nmi_uaccess_okay()))
346 		return -EPERM;
347 
348 	return copy_to_user_nofault(unsafe_ptr, src, size);
349 }
350 
351 static const struct bpf_func_proto bpf_probe_write_user_proto = {
352 	.func		= bpf_probe_write_user,
353 	.gpl_only	= true,
354 	.ret_type	= RET_INTEGER,
355 	.arg1_type	= ARG_ANYTHING,
356 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
357 	.arg3_type	= ARG_CONST_SIZE,
358 };
359 
360 #define MAX_TRACE_PRINTK_VARARGS	3
361 #define BPF_TRACE_PRINTK_SIZE		1024
362 
363 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
364 	   u64, arg2, u64, arg3)
365 {
366 	u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 };
367 	struct bpf_bprintf_data data = {
368 		.get_bin_args	= true,
369 		.get_buf	= true,
370 	};
371 	int ret;
372 
373 	ret = bpf_bprintf_prepare(fmt, fmt_size, args,
374 				  MAX_TRACE_PRINTK_VARARGS, &data);
375 	if (ret < 0)
376 		return ret;
377 
378 	ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
379 
380 	trace_bpf_trace_printk(data.buf);
381 
382 	bpf_bprintf_cleanup(&data);
383 
384 	return ret;
385 }
386 
387 static const struct bpf_func_proto bpf_trace_printk_proto = {
388 	.func		= bpf_trace_printk,
389 	.gpl_only	= true,
390 	.ret_type	= RET_INTEGER,
391 	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
392 	.arg2_type	= ARG_CONST_SIZE,
393 };
394 
395 static void __set_printk_clr_event(void)
396 {
397 	/*
398 	 * This program might be calling bpf_trace_printk,
399 	 * so enable the associated bpf_trace/bpf_trace_printk event.
400 	 * Repeat this each time as it is possible a user has
401 	 * disabled bpf_trace_printk events.  By loading a program
402 	 * calling bpf_trace_printk() however the user has expressed
403 	 * the intent to see such events.
404 	 */
405 	if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
406 		pr_warn_ratelimited("could not enable bpf_trace_printk events");
407 }
408 
409 const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
410 {
411 	__set_printk_clr_event();
412 	return &bpf_trace_printk_proto;
413 }
414 
415 BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, args,
416 	   u32, data_len)
417 {
418 	struct bpf_bprintf_data data = {
419 		.get_bin_args	= true,
420 		.get_buf	= true,
421 	};
422 	int ret, num_args;
423 
424 	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
425 	    (data_len && !args))
426 		return -EINVAL;
427 	num_args = data_len / 8;
428 
429 	ret = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
430 	if (ret < 0)
431 		return ret;
432 
433 	ret = bstr_printf(data.buf, MAX_BPRINTF_BUF, fmt, data.bin_args);
434 
435 	trace_bpf_trace_printk(data.buf);
436 
437 	bpf_bprintf_cleanup(&data);
438 
439 	return ret;
440 }
441 
442 static const struct bpf_func_proto bpf_trace_vprintk_proto = {
443 	.func		= bpf_trace_vprintk,
444 	.gpl_only	= true,
445 	.ret_type	= RET_INTEGER,
446 	.arg1_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
447 	.arg2_type	= ARG_CONST_SIZE,
448 	.arg3_type	= ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
449 	.arg4_type	= ARG_CONST_SIZE_OR_ZERO,
450 };
451 
452 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void)
453 {
454 	__set_printk_clr_event();
455 	return &bpf_trace_vprintk_proto;
456 }
457 
458 BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size,
459 	   const void *, args, u32, data_len)
460 {
461 	struct bpf_bprintf_data data = {
462 		.get_bin_args	= true,
463 	};
464 	int err, num_args;
465 
466 	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
467 	    (data_len && !args))
468 		return -EINVAL;
469 	num_args = data_len / 8;
470 
471 	err = bpf_bprintf_prepare(fmt, fmt_size, args, num_args, &data);
472 	if (err < 0)
473 		return err;
474 
475 	seq_bprintf(m, fmt, data.bin_args);
476 
477 	bpf_bprintf_cleanup(&data);
478 
479 	return seq_has_overflowed(m) ? -EOVERFLOW : 0;
480 }
481 
482 BTF_ID_LIST_SINGLE(btf_seq_file_ids, struct, seq_file)
483 
484 static const struct bpf_func_proto bpf_seq_printf_proto = {
485 	.func		= bpf_seq_printf,
486 	.gpl_only	= true,
487 	.ret_type	= RET_INTEGER,
488 	.arg1_type	= ARG_PTR_TO_BTF_ID,
489 	.arg1_btf_id	= &btf_seq_file_ids[0],
490 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
491 	.arg3_type	= ARG_CONST_SIZE,
492 	.arg4_type      = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
493 	.arg5_type      = ARG_CONST_SIZE_OR_ZERO,
494 };
495 
496 BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len)
497 {
498 	return seq_write(m, data, len) ? -EOVERFLOW : 0;
499 }
500 
501 static const struct bpf_func_proto bpf_seq_write_proto = {
502 	.func		= bpf_seq_write,
503 	.gpl_only	= true,
504 	.ret_type	= RET_INTEGER,
505 	.arg1_type	= ARG_PTR_TO_BTF_ID,
506 	.arg1_btf_id	= &btf_seq_file_ids[0],
507 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
508 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
509 };
510 
511 BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
512 	   u32, btf_ptr_size, u64, flags)
513 {
514 	const struct btf *btf;
515 	s32 btf_id;
516 	int ret;
517 
518 	ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
519 	if (ret)
520 		return ret;
521 
522 	return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
523 }
524 
525 static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
526 	.func		= bpf_seq_printf_btf,
527 	.gpl_only	= true,
528 	.ret_type	= RET_INTEGER,
529 	.arg1_type	= ARG_PTR_TO_BTF_ID,
530 	.arg1_btf_id	= &btf_seq_file_ids[0],
531 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
532 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
533 	.arg4_type	= ARG_ANYTHING,
534 };
535 
536 static __always_inline int
537 get_map_perf_counter(struct bpf_map *map, u64 flags,
538 		     u64 *value, u64 *enabled, u64 *running)
539 {
540 	struct bpf_array *array = container_of(map, struct bpf_array, map);
541 	unsigned int cpu = smp_processor_id();
542 	u64 index = flags & BPF_F_INDEX_MASK;
543 	struct bpf_event_entry *ee;
544 
545 	if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
546 		return -EINVAL;
547 	if (index == BPF_F_CURRENT_CPU)
548 		index = cpu;
549 	if (unlikely(index >= array->map.max_entries))
550 		return -E2BIG;
551 
552 	ee = READ_ONCE(array->ptrs[index]);
553 	if (!ee)
554 		return -ENOENT;
555 
556 	return perf_event_read_local(ee->event, value, enabled, running);
557 }
558 
559 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
560 {
561 	u64 value = 0;
562 	int err;
563 
564 	err = get_map_perf_counter(map, flags, &value, NULL, NULL);
565 	/*
566 	 * this api is ugly since we miss [-22..-2] range of valid
567 	 * counter values, but that's uapi
568 	 */
569 	if (err)
570 		return err;
571 	return value;
572 }
573 
574 static const struct bpf_func_proto bpf_perf_event_read_proto = {
575 	.func		= bpf_perf_event_read,
576 	.gpl_only	= true,
577 	.ret_type	= RET_INTEGER,
578 	.arg1_type	= ARG_CONST_MAP_PTR,
579 	.arg2_type	= ARG_ANYTHING,
580 };
581 
582 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
583 	   struct bpf_perf_event_value *, buf, u32, size)
584 {
585 	int err = -EINVAL;
586 
587 	if (unlikely(size != sizeof(struct bpf_perf_event_value)))
588 		goto clear;
589 	err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled,
590 				   &buf->running);
591 	if (unlikely(err))
592 		goto clear;
593 	return 0;
594 clear:
595 	memset(buf, 0, size);
596 	return err;
597 }
598 
599 static const struct bpf_func_proto bpf_perf_event_read_value_proto = {
600 	.func		= bpf_perf_event_read_value,
601 	.gpl_only	= true,
602 	.ret_type	= RET_INTEGER,
603 	.arg1_type	= ARG_CONST_MAP_PTR,
604 	.arg2_type	= ARG_ANYTHING,
605 	.arg3_type	= ARG_PTR_TO_UNINIT_MEM,
606 	.arg4_type	= ARG_CONST_SIZE,
607 };
608 
609 static __always_inline u64
610 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
611 			u64 flags, struct perf_sample_data *sd)
612 {
613 	struct bpf_array *array = container_of(map, struct bpf_array, map);
614 	unsigned int cpu = smp_processor_id();
615 	u64 index = flags & BPF_F_INDEX_MASK;
616 	struct bpf_event_entry *ee;
617 	struct perf_event *event;
618 
619 	if (index == BPF_F_CURRENT_CPU)
620 		index = cpu;
621 	if (unlikely(index >= array->map.max_entries))
622 		return -E2BIG;
623 
624 	ee = READ_ONCE(array->ptrs[index]);
625 	if (!ee)
626 		return -ENOENT;
627 
628 	event = ee->event;
629 	if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
630 		     event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
631 		return -EINVAL;
632 
633 	if (unlikely(event->oncpu != cpu))
634 		return -EOPNOTSUPP;
635 
636 	return perf_event_output(event, sd, regs);
637 }
638 
639 /*
640  * Support executing tracepoints in normal, irq, and nmi context that each call
641  * bpf_perf_event_output
642  */
643 struct bpf_trace_sample_data {
644 	struct perf_sample_data sds[3];
645 };
646 
647 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_trace_sds);
648 static DEFINE_PER_CPU(int, bpf_trace_nest_level);
649 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
650 	   u64, flags, void *, data, u64, size)
651 {
652 	struct bpf_trace_sample_data *sds;
653 	struct perf_raw_record raw = {
654 		.frag = {
655 			.size = size,
656 			.data = data,
657 		},
658 	};
659 	struct perf_sample_data *sd;
660 	int nest_level, err;
661 
662 	preempt_disable();
663 	sds = this_cpu_ptr(&bpf_trace_sds);
664 	nest_level = this_cpu_inc_return(bpf_trace_nest_level);
665 
666 	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(sds->sds))) {
667 		err = -EBUSY;
668 		goto out;
669 	}
670 
671 	sd = &sds->sds[nest_level - 1];
672 
673 	if (unlikely(flags & ~(BPF_F_INDEX_MASK))) {
674 		err = -EINVAL;
675 		goto out;
676 	}
677 
678 	perf_sample_data_init(sd, 0, 0);
679 	perf_sample_save_raw_data(sd, &raw);
680 
681 	err = __bpf_perf_event_output(regs, map, flags, sd);
682 out:
683 	this_cpu_dec(bpf_trace_nest_level);
684 	preempt_enable();
685 	return err;
686 }
687 
688 static const struct bpf_func_proto bpf_perf_event_output_proto = {
689 	.func		= bpf_perf_event_output,
690 	.gpl_only	= true,
691 	.ret_type	= RET_INTEGER,
692 	.arg1_type	= ARG_PTR_TO_CTX,
693 	.arg2_type	= ARG_CONST_MAP_PTR,
694 	.arg3_type	= ARG_ANYTHING,
695 	.arg4_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
696 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
697 };
698 
699 static DEFINE_PER_CPU(int, bpf_event_output_nest_level);
700 struct bpf_nested_pt_regs {
701 	struct pt_regs regs[3];
702 };
703 static DEFINE_PER_CPU(struct bpf_nested_pt_regs, bpf_pt_regs);
704 static DEFINE_PER_CPU(struct bpf_trace_sample_data, bpf_misc_sds);
705 
706 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
707 		     void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy)
708 {
709 	struct perf_raw_frag frag = {
710 		.copy		= ctx_copy,
711 		.size		= ctx_size,
712 		.data		= ctx,
713 	};
714 	struct perf_raw_record raw = {
715 		.frag = {
716 			{
717 				.next	= ctx_size ? &frag : NULL,
718 			},
719 			.size	= meta_size,
720 			.data	= meta,
721 		},
722 	};
723 	struct perf_sample_data *sd;
724 	struct pt_regs *regs;
725 	int nest_level;
726 	u64 ret;
727 
728 	preempt_disable();
729 	nest_level = this_cpu_inc_return(bpf_event_output_nest_level);
730 
731 	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bpf_misc_sds.sds))) {
732 		ret = -EBUSY;
733 		goto out;
734 	}
735 	sd = this_cpu_ptr(&bpf_misc_sds.sds[nest_level - 1]);
736 	regs = this_cpu_ptr(&bpf_pt_regs.regs[nest_level - 1]);
737 
738 	perf_fetch_caller_regs(regs);
739 	perf_sample_data_init(sd, 0, 0);
740 	perf_sample_save_raw_data(sd, &raw);
741 
742 	ret = __bpf_perf_event_output(regs, map, flags, sd);
743 out:
744 	this_cpu_dec(bpf_event_output_nest_level);
745 	preempt_enable();
746 	return ret;
747 }
748 
749 BPF_CALL_0(bpf_get_current_task)
750 {
751 	return (long) current;
752 }
753 
754 const struct bpf_func_proto bpf_get_current_task_proto = {
755 	.func		= bpf_get_current_task,
756 	.gpl_only	= true,
757 	.ret_type	= RET_INTEGER,
758 };
759 
760 BPF_CALL_0(bpf_get_current_task_btf)
761 {
762 	return (unsigned long) current;
763 }
764 
765 const struct bpf_func_proto bpf_get_current_task_btf_proto = {
766 	.func		= bpf_get_current_task_btf,
767 	.gpl_only	= true,
768 	.ret_type	= RET_PTR_TO_BTF_ID_TRUSTED,
769 	.ret_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
770 };
771 
772 BPF_CALL_1(bpf_task_pt_regs, struct task_struct *, task)
773 {
774 	return (unsigned long) task_pt_regs(task);
775 }
776 
777 BTF_ID_LIST(bpf_task_pt_regs_ids)
778 BTF_ID(struct, pt_regs)
779 
780 const struct bpf_func_proto bpf_task_pt_regs_proto = {
781 	.func		= bpf_task_pt_regs,
782 	.gpl_only	= true,
783 	.arg1_type	= ARG_PTR_TO_BTF_ID,
784 	.arg1_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
785 	.ret_type	= RET_PTR_TO_BTF_ID,
786 	.ret_btf_id	= &bpf_task_pt_regs_ids[0],
787 };
788 
789 struct send_signal_irq_work {
790 	struct irq_work irq_work;
791 	struct task_struct *task;
792 	u32 sig;
793 	enum pid_type type;
794 	bool has_siginfo;
795 	struct kernel_siginfo info;
796 };
797 
798 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work);
799 
800 static void do_bpf_send_signal(struct irq_work *entry)
801 {
802 	struct send_signal_irq_work *work;
803 	struct kernel_siginfo *siginfo;
804 
805 	work = container_of(entry, struct send_signal_irq_work, irq_work);
806 	siginfo = work->has_siginfo ? &work->info : SEND_SIG_PRIV;
807 
808 	group_send_sig_info(work->sig, siginfo, work->task, work->type);
809 	put_task_struct(work->task);
810 }
811 
812 static int bpf_send_signal_common(u32 sig, enum pid_type type, struct task_struct *task, u64 value)
813 {
814 	struct send_signal_irq_work *work = NULL;
815 	struct kernel_siginfo info;
816 	struct kernel_siginfo *siginfo;
817 
818 	if (!task) {
819 		task = current;
820 		siginfo = SEND_SIG_PRIV;
821 	} else {
822 		clear_siginfo(&info);
823 		info.si_signo = sig;
824 		info.si_errno = 0;
825 		info.si_code = SI_KERNEL;
826 		info.si_pid = 0;
827 		info.si_uid = 0;
828 		info.si_value.sival_ptr = (void *)(unsigned long)value;
829 		siginfo = &info;
830 	}
831 
832 	/* Similar to bpf_probe_write_user, task needs to be
833 	 * in a sound condition and kernel memory access be
834 	 * permitted in order to send signal to the current
835 	 * task.
836 	 */
837 	if (unlikely(task->flags & (PF_KTHREAD | PF_EXITING)))
838 		return -EPERM;
839 	if (unlikely(!nmi_uaccess_okay()))
840 		return -EPERM;
841 	/* Task should not be pid=1 to avoid kernel panic. */
842 	if (unlikely(is_global_init(task)))
843 		return -EPERM;
844 
845 	if (irqs_disabled()) {
846 		/* Do an early check on signal validity. Otherwise,
847 		 * the error is lost in deferred irq_work.
848 		 */
849 		if (unlikely(!valid_signal(sig)))
850 			return -EINVAL;
851 
852 		work = this_cpu_ptr(&send_signal_work);
853 		if (irq_work_is_busy(&work->irq_work))
854 			return -EBUSY;
855 
856 		/* Add the current task, which is the target of sending signal,
857 		 * to the irq_work. The current task may change when queued
858 		 * irq works get executed.
859 		 */
860 		work->task = get_task_struct(task);
861 		work->has_siginfo = siginfo == &info;
862 		if (work->has_siginfo)
863 			copy_siginfo(&work->info, &info);
864 		work->sig = sig;
865 		work->type = type;
866 		irq_work_queue(&work->irq_work);
867 		return 0;
868 	}
869 
870 	return group_send_sig_info(sig, siginfo, task, type);
871 }
872 
873 BPF_CALL_1(bpf_send_signal, u32, sig)
874 {
875 	return bpf_send_signal_common(sig, PIDTYPE_TGID, NULL, 0);
876 }
877 
878 static const struct bpf_func_proto bpf_send_signal_proto = {
879 	.func		= bpf_send_signal,
880 	.gpl_only	= false,
881 	.ret_type	= RET_INTEGER,
882 	.arg1_type	= ARG_ANYTHING,
883 };
884 
885 BPF_CALL_1(bpf_send_signal_thread, u32, sig)
886 {
887 	return bpf_send_signal_common(sig, PIDTYPE_PID, NULL, 0);
888 }
889 
890 static const struct bpf_func_proto bpf_send_signal_thread_proto = {
891 	.func		= bpf_send_signal_thread,
892 	.gpl_only	= false,
893 	.ret_type	= RET_INTEGER,
894 	.arg1_type	= ARG_ANYTHING,
895 };
896 
897 BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)
898 {
899 	struct path copy;
900 	long len;
901 	char *p;
902 
903 	if (!sz)
904 		return 0;
905 
906 	/*
907 	 * The path pointer is verified as trusted and safe to use,
908 	 * but let's double check it's valid anyway to workaround
909 	 * potentially broken verifier.
910 	 */
911 	len = copy_from_kernel_nofault(&copy, path, sizeof(*path));
912 	if (len < 0)
913 		return len;
914 
915 	p = d_path(&copy, buf, sz);
916 	if (IS_ERR(p)) {
917 		len = PTR_ERR(p);
918 	} else {
919 		len = buf + sz - p;
920 		memmove(buf, p, len);
921 	}
922 
923 	return len;
924 }
925 
926 BTF_SET_START(btf_allowlist_d_path)
927 #ifdef CONFIG_SECURITY
928 BTF_ID(func, security_file_permission)
929 BTF_ID(func, security_inode_getattr)
930 BTF_ID(func, security_file_open)
931 #endif
932 #ifdef CONFIG_SECURITY_PATH
933 BTF_ID(func, security_path_truncate)
934 #endif
935 BTF_ID(func, vfs_truncate)
936 BTF_ID(func, vfs_fallocate)
937 BTF_ID(func, dentry_open)
938 BTF_ID(func, vfs_getattr)
939 BTF_ID(func, filp_close)
940 BTF_SET_END(btf_allowlist_d_path)
941 
942 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
943 {
944 	if (prog->type == BPF_PROG_TYPE_TRACING &&
945 	    prog->expected_attach_type == BPF_TRACE_ITER)
946 		return true;
947 
948 	if (prog->type == BPF_PROG_TYPE_LSM)
949 		return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
950 
951 	return btf_id_set_contains(&btf_allowlist_d_path,
952 				   prog->aux->attach_btf_id);
953 }
954 
955 BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
956 
957 static const struct bpf_func_proto bpf_d_path_proto = {
958 	.func		= bpf_d_path,
959 	.gpl_only	= false,
960 	.ret_type	= RET_INTEGER,
961 	.arg1_type	= ARG_PTR_TO_BTF_ID,
962 	.arg1_btf_id	= &bpf_d_path_btf_ids[0],
963 	.arg2_type	= ARG_PTR_TO_MEM,
964 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
965 	.allowed	= bpf_d_path_allowed,
966 };
967 
968 #define BTF_F_ALL	(BTF_F_COMPACT  | BTF_F_NONAME | \
969 			 BTF_F_PTR_RAW | BTF_F_ZERO)
970 
971 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
972 				  u64 flags, const struct btf **btf,
973 				  s32 *btf_id)
974 {
975 	const struct btf_type *t;
976 
977 	if (unlikely(flags & ~(BTF_F_ALL)))
978 		return -EINVAL;
979 
980 	if (btf_ptr_size != sizeof(struct btf_ptr))
981 		return -EINVAL;
982 
983 	*btf = bpf_get_btf_vmlinux();
984 
985 	if (IS_ERR_OR_NULL(*btf))
986 		return IS_ERR(*btf) ? PTR_ERR(*btf) : -EINVAL;
987 
988 	if (ptr->type_id > 0)
989 		*btf_id = ptr->type_id;
990 	else
991 		return -EINVAL;
992 
993 	if (*btf_id > 0)
994 		t = btf_type_by_id(*btf, *btf_id);
995 	if (*btf_id <= 0 || !t)
996 		return -ENOENT;
997 
998 	return 0;
999 }
1000 
1001 BPF_CALL_5(bpf_snprintf_btf, char *, str, u32, str_size, struct btf_ptr *, ptr,
1002 	   u32, btf_ptr_size, u64, flags)
1003 {
1004 	const struct btf *btf;
1005 	s32 btf_id;
1006 	int ret;
1007 
1008 	ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
1009 	if (ret)
1010 		return ret;
1011 
1012 	return btf_type_snprintf_show(btf, btf_id, ptr->ptr, str, str_size,
1013 				      flags);
1014 }
1015 
1016 const struct bpf_func_proto bpf_snprintf_btf_proto = {
1017 	.func		= bpf_snprintf_btf,
1018 	.gpl_only	= false,
1019 	.ret_type	= RET_INTEGER,
1020 	.arg1_type	= ARG_PTR_TO_MEM,
1021 	.arg2_type	= ARG_CONST_SIZE,
1022 	.arg3_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1023 	.arg4_type	= ARG_CONST_SIZE,
1024 	.arg5_type	= ARG_ANYTHING,
1025 };
1026 
1027 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
1028 {
1029 	/* This helper call is inlined by verifier. */
1030 	return ((u64 *)ctx)[-2];
1031 }
1032 
1033 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
1034 	.func		= bpf_get_func_ip_tracing,
1035 	.gpl_only	= true,
1036 	.ret_type	= RET_INTEGER,
1037 	.arg1_type	= ARG_PTR_TO_CTX,
1038 };
1039 
1040 #ifdef CONFIG_X86_KERNEL_IBT
1041 static unsigned long get_entry_ip(unsigned long fentry_ip)
1042 {
1043 	u32 instr;
1044 
1045 	/* We want to be extra safe in case entry ip is on the page edge,
1046 	 * but otherwise we need to avoid get_kernel_nofault()'s overhead.
1047 	 */
1048 	if ((fentry_ip & ~PAGE_MASK) < ENDBR_INSN_SIZE) {
1049 		if (get_kernel_nofault(instr, (u32 *)(fentry_ip - ENDBR_INSN_SIZE)))
1050 			return fentry_ip;
1051 	} else {
1052 		instr = *(u32 *)(fentry_ip - ENDBR_INSN_SIZE);
1053 	}
1054 	if (is_endbr(instr))
1055 		fentry_ip -= ENDBR_INSN_SIZE;
1056 	return fentry_ip;
1057 }
1058 #else
1059 #define get_entry_ip(fentry_ip) fentry_ip
1060 #endif
1061 
1062 BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
1063 {
1064 	struct bpf_trace_run_ctx *run_ctx __maybe_unused;
1065 	struct kprobe *kp;
1066 
1067 #ifdef CONFIG_UPROBES
1068 	run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1069 	if (run_ctx->is_uprobe)
1070 		return ((struct uprobe_dispatch_data *)current->utask->vaddr)->bp_addr;
1071 #endif
1072 
1073 	kp = kprobe_running();
1074 
1075 	if (!kp || !(kp->flags & KPROBE_FLAG_ON_FUNC_ENTRY))
1076 		return 0;
1077 
1078 	return get_entry_ip((uintptr_t)kp->addr);
1079 }
1080 
1081 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
1082 	.func		= bpf_get_func_ip_kprobe,
1083 	.gpl_only	= true,
1084 	.ret_type	= RET_INTEGER,
1085 	.arg1_type	= ARG_PTR_TO_CTX,
1086 };
1087 
1088 BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
1089 {
1090 	return bpf_kprobe_multi_entry_ip(current->bpf_ctx);
1091 }
1092 
1093 static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe_multi = {
1094 	.func		= bpf_get_func_ip_kprobe_multi,
1095 	.gpl_only	= false,
1096 	.ret_type	= RET_INTEGER,
1097 	.arg1_type	= ARG_PTR_TO_CTX,
1098 };
1099 
1100 BPF_CALL_1(bpf_get_attach_cookie_kprobe_multi, struct pt_regs *, regs)
1101 {
1102 	return bpf_kprobe_multi_cookie(current->bpf_ctx);
1103 }
1104 
1105 static const struct bpf_func_proto bpf_get_attach_cookie_proto_kmulti = {
1106 	.func		= bpf_get_attach_cookie_kprobe_multi,
1107 	.gpl_only	= false,
1108 	.ret_type	= RET_INTEGER,
1109 	.arg1_type	= ARG_PTR_TO_CTX,
1110 };
1111 
1112 BPF_CALL_1(bpf_get_func_ip_uprobe_multi, struct pt_regs *, regs)
1113 {
1114 	return bpf_uprobe_multi_entry_ip(current->bpf_ctx);
1115 }
1116 
1117 static const struct bpf_func_proto bpf_get_func_ip_proto_uprobe_multi = {
1118 	.func		= bpf_get_func_ip_uprobe_multi,
1119 	.gpl_only	= false,
1120 	.ret_type	= RET_INTEGER,
1121 	.arg1_type	= ARG_PTR_TO_CTX,
1122 };
1123 
1124 BPF_CALL_1(bpf_get_attach_cookie_uprobe_multi, struct pt_regs *, regs)
1125 {
1126 	return bpf_uprobe_multi_cookie(current->bpf_ctx);
1127 }
1128 
1129 static const struct bpf_func_proto bpf_get_attach_cookie_proto_umulti = {
1130 	.func		= bpf_get_attach_cookie_uprobe_multi,
1131 	.gpl_only	= false,
1132 	.ret_type	= RET_INTEGER,
1133 	.arg1_type	= ARG_PTR_TO_CTX,
1134 };
1135 
1136 BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx)
1137 {
1138 	struct bpf_trace_run_ctx *run_ctx;
1139 
1140 	run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1141 	return run_ctx->bpf_cookie;
1142 }
1143 
1144 static const struct bpf_func_proto bpf_get_attach_cookie_proto_trace = {
1145 	.func		= bpf_get_attach_cookie_trace,
1146 	.gpl_only	= false,
1147 	.ret_type	= RET_INTEGER,
1148 	.arg1_type	= ARG_PTR_TO_CTX,
1149 };
1150 
1151 BPF_CALL_1(bpf_get_attach_cookie_pe, struct bpf_perf_event_data_kern *, ctx)
1152 {
1153 	return ctx->event->bpf_cookie;
1154 }
1155 
1156 static const struct bpf_func_proto bpf_get_attach_cookie_proto_pe = {
1157 	.func		= bpf_get_attach_cookie_pe,
1158 	.gpl_only	= false,
1159 	.ret_type	= RET_INTEGER,
1160 	.arg1_type	= ARG_PTR_TO_CTX,
1161 };
1162 
1163 BPF_CALL_1(bpf_get_attach_cookie_tracing, void *, ctx)
1164 {
1165 	struct bpf_trace_run_ctx *run_ctx;
1166 
1167 	run_ctx = container_of(current->bpf_ctx, struct bpf_trace_run_ctx, run_ctx);
1168 	return run_ctx->bpf_cookie;
1169 }
1170 
1171 static const struct bpf_func_proto bpf_get_attach_cookie_proto_tracing = {
1172 	.func		= bpf_get_attach_cookie_tracing,
1173 	.gpl_only	= false,
1174 	.ret_type	= RET_INTEGER,
1175 	.arg1_type	= ARG_PTR_TO_CTX,
1176 };
1177 
1178 BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags)
1179 {
1180 	static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1181 	u32 entry_cnt = size / br_entry_size;
1182 
1183 	entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt);
1184 
1185 	if (unlikely(flags))
1186 		return -EINVAL;
1187 
1188 	if (!entry_cnt)
1189 		return -ENOENT;
1190 
1191 	return entry_cnt * br_entry_size;
1192 }
1193 
1194 static const struct bpf_func_proto bpf_get_branch_snapshot_proto = {
1195 	.func		= bpf_get_branch_snapshot,
1196 	.gpl_only	= true,
1197 	.ret_type	= RET_INTEGER,
1198 	.arg1_type	= ARG_PTR_TO_UNINIT_MEM,
1199 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
1200 };
1201 
1202 BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1203 {
1204 	/* This helper call is inlined by verifier. */
1205 	u64 nr_args = ((u64 *)ctx)[-1];
1206 
1207 	if ((u64) n >= nr_args)
1208 		return -EINVAL;
1209 	*value = ((u64 *)ctx)[n];
1210 	return 0;
1211 }
1212 
1213 static const struct bpf_func_proto bpf_get_func_arg_proto = {
1214 	.func		= get_func_arg,
1215 	.ret_type	= RET_INTEGER,
1216 	.arg1_type	= ARG_PTR_TO_CTX,
1217 	.arg2_type	= ARG_ANYTHING,
1218 	.arg3_type	= ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
1219 	.arg3_size	= sizeof(u64),
1220 };
1221 
1222 BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1223 {
1224 	/* This helper call is inlined by verifier. */
1225 	u64 nr_args = ((u64 *)ctx)[-1];
1226 
1227 	*value = ((u64 *)ctx)[nr_args];
1228 	return 0;
1229 }
1230 
1231 static const struct bpf_func_proto bpf_get_func_ret_proto = {
1232 	.func		= get_func_ret,
1233 	.ret_type	= RET_INTEGER,
1234 	.arg1_type	= ARG_PTR_TO_CTX,
1235 	.arg2_type	= ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
1236 	.arg2_size	= sizeof(u64),
1237 };
1238 
1239 BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1240 {
1241 	/* This helper call is inlined by verifier. */
1242 	return ((u64 *)ctx)[-1];
1243 }
1244 
1245 static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1246 	.func		= get_func_arg_cnt,
1247 	.ret_type	= RET_INTEGER,
1248 	.arg1_type	= ARG_PTR_TO_CTX,
1249 };
1250 
1251 #ifdef CONFIG_KEYS
1252 __bpf_kfunc_start_defs();
1253 
1254 /**
1255  * bpf_lookup_user_key - lookup a key by its serial
1256  * @serial: key handle serial number
1257  * @flags: lookup-specific flags
1258  *
1259  * Search a key with a given *serial* and the provided *flags*.
1260  * If found, increment the reference count of the key by one, and
1261  * return it in the bpf_key structure.
1262  *
1263  * The bpf_key structure must be passed to bpf_key_put() when done
1264  * with it, so that the key reference count is decremented and the
1265  * bpf_key structure is freed.
1266  *
1267  * Permission checks are deferred to the time the key is used by
1268  * one of the available key-specific kfuncs.
1269  *
1270  * Set *flags* with KEY_LOOKUP_CREATE, to attempt creating a requested
1271  * special keyring (e.g. session keyring), if it doesn't yet exist.
1272  * Set *flags* with KEY_LOOKUP_PARTIAL, to lookup a key without waiting
1273  * for the key construction, and to retrieve uninstantiated keys (keys
1274  * without data attached to them).
1275  *
1276  * Return: a bpf_key pointer with a valid key pointer if the key is found, a
1277  *         NULL pointer otherwise.
1278  */
1279 __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
1280 {
1281 	key_ref_t key_ref;
1282 	struct bpf_key *bkey;
1283 
1284 	if (flags & ~KEY_LOOKUP_ALL)
1285 		return NULL;
1286 
1287 	/*
1288 	 * Permission check is deferred until the key is used, as the
1289 	 * intent of the caller is unknown here.
1290 	 */
1291 	key_ref = lookup_user_key(serial, flags, KEY_DEFER_PERM_CHECK);
1292 	if (IS_ERR(key_ref))
1293 		return NULL;
1294 
1295 	bkey = kmalloc(sizeof(*bkey), GFP_KERNEL);
1296 	if (!bkey) {
1297 		key_put(key_ref_to_ptr(key_ref));
1298 		return NULL;
1299 	}
1300 
1301 	bkey->key = key_ref_to_ptr(key_ref);
1302 	bkey->has_ref = true;
1303 
1304 	return bkey;
1305 }
1306 
1307 /**
1308  * bpf_lookup_system_key - lookup a key by a system-defined ID
1309  * @id: key ID
1310  *
1311  * Obtain a bpf_key structure with a key pointer set to the passed key ID.
1312  * The key pointer is marked as invalid, to prevent bpf_key_put() from
1313  * attempting to decrement the key reference count on that pointer. The key
1314  * pointer set in such way is currently understood only by
1315  * verify_pkcs7_signature().
1316  *
1317  * Set *id* to one of the values defined in include/linux/verification.h:
1318  * 0 for the primary keyring (immutable keyring of system keys);
1319  * VERIFY_USE_SECONDARY_KEYRING for both the primary and secondary keyring
1320  * (where keys can be added only if they are vouched for by existing keys
1321  * in those keyrings); VERIFY_USE_PLATFORM_KEYRING for the platform
1322  * keyring (primarily used by the integrity subsystem to verify a kexec'ed
1323  * kerned image and, possibly, the initramfs signature).
1324  *
1325  * Return: a bpf_key pointer with an invalid key pointer set from the
1326  *         pre-determined ID on success, a NULL pointer otherwise
1327  */
1328 __bpf_kfunc struct bpf_key *bpf_lookup_system_key(u64 id)
1329 {
1330 	struct bpf_key *bkey;
1331 
1332 	if (system_keyring_id_check(id) < 0)
1333 		return NULL;
1334 
1335 	bkey = kmalloc(sizeof(*bkey), GFP_ATOMIC);
1336 	if (!bkey)
1337 		return NULL;
1338 
1339 	bkey->key = (struct key *)(unsigned long)id;
1340 	bkey->has_ref = false;
1341 
1342 	return bkey;
1343 }
1344 
1345 /**
1346  * bpf_key_put - decrement key reference count if key is valid and free bpf_key
1347  * @bkey: bpf_key structure
1348  *
1349  * Decrement the reference count of the key inside *bkey*, if the pointer
1350  * is valid, and free *bkey*.
1351  */
1352 __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
1353 {
1354 	if (bkey->has_ref)
1355 		key_put(bkey->key);
1356 
1357 	kfree(bkey);
1358 }
1359 
1360 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1361 /**
1362  * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
1363  * @data_p: data to verify
1364  * @sig_p: signature of the data
1365  * @trusted_keyring: keyring with keys trusted for signature verification
1366  *
1367  * Verify the PKCS#7 signature *sig_ptr* against the supplied *data_ptr*
1368  * with keys in a keyring referenced by *trusted_keyring*.
1369  *
1370  * Return: 0 on success, a negative value on error.
1371  */
1372 __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_p,
1373 			       struct bpf_dynptr *sig_p,
1374 			       struct bpf_key *trusted_keyring)
1375 {
1376 	struct bpf_dynptr_kern *data_ptr = (struct bpf_dynptr_kern *)data_p;
1377 	struct bpf_dynptr_kern *sig_ptr = (struct bpf_dynptr_kern *)sig_p;
1378 	const void *data, *sig;
1379 	u32 data_len, sig_len;
1380 	int ret;
1381 
1382 	if (trusted_keyring->has_ref) {
1383 		/*
1384 		 * Do the permission check deferred in bpf_lookup_user_key().
1385 		 * See bpf_lookup_user_key() for more details.
1386 		 *
1387 		 * A call to key_task_permission() here would be redundant, as
1388 		 * it is already done by keyring_search() called by
1389 		 * find_asymmetric_key().
1390 		 */
1391 		ret = key_validate(trusted_keyring->key);
1392 		if (ret < 0)
1393 			return ret;
1394 	}
1395 
1396 	data_len = __bpf_dynptr_size(data_ptr);
1397 	data = __bpf_dynptr_data(data_ptr, data_len);
1398 	sig_len = __bpf_dynptr_size(sig_ptr);
1399 	sig = __bpf_dynptr_data(sig_ptr, sig_len);
1400 
1401 	return verify_pkcs7_signature(data, data_len, sig, sig_len,
1402 				      trusted_keyring->key,
1403 				      VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
1404 				      NULL);
1405 }
1406 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
1407 
1408 __bpf_kfunc_end_defs();
1409 
1410 BTF_KFUNCS_START(key_sig_kfunc_set)
1411 BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
1412 BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
1413 BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
1414 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
1415 BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
1416 #endif
1417 BTF_KFUNCS_END(key_sig_kfunc_set)
1418 
1419 static const struct btf_kfunc_id_set bpf_key_sig_kfunc_set = {
1420 	.owner = THIS_MODULE,
1421 	.set = &key_sig_kfunc_set,
1422 };
1423 
1424 static int __init bpf_key_sig_kfuncs_init(void)
1425 {
1426 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
1427 					 &bpf_key_sig_kfunc_set);
1428 }
1429 
1430 late_initcall(bpf_key_sig_kfuncs_init);
1431 #endif /* CONFIG_KEYS */
1432 
1433 static const struct bpf_func_proto *
1434 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1435 {
1436 	const struct bpf_func_proto *func_proto;
1437 
1438 	switch (func_id) {
1439 	case BPF_FUNC_map_lookup_elem:
1440 		return &bpf_map_lookup_elem_proto;
1441 	case BPF_FUNC_map_update_elem:
1442 		return &bpf_map_update_elem_proto;
1443 	case BPF_FUNC_map_delete_elem:
1444 		return &bpf_map_delete_elem_proto;
1445 	case BPF_FUNC_map_push_elem:
1446 		return &bpf_map_push_elem_proto;
1447 	case BPF_FUNC_map_pop_elem:
1448 		return &bpf_map_pop_elem_proto;
1449 	case BPF_FUNC_map_peek_elem:
1450 		return &bpf_map_peek_elem_proto;
1451 	case BPF_FUNC_map_lookup_percpu_elem:
1452 		return &bpf_map_lookup_percpu_elem_proto;
1453 	case BPF_FUNC_ktime_get_ns:
1454 		return &bpf_ktime_get_ns_proto;
1455 	case BPF_FUNC_ktime_get_boot_ns:
1456 		return &bpf_ktime_get_boot_ns_proto;
1457 	case BPF_FUNC_tail_call:
1458 		return &bpf_tail_call_proto;
1459 	case BPF_FUNC_get_current_task:
1460 		return &bpf_get_current_task_proto;
1461 	case BPF_FUNC_get_current_task_btf:
1462 		return &bpf_get_current_task_btf_proto;
1463 	case BPF_FUNC_task_pt_regs:
1464 		return &bpf_task_pt_regs_proto;
1465 	case BPF_FUNC_get_current_uid_gid:
1466 		return &bpf_get_current_uid_gid_proto;
1467 	case BPF_FUNC_get_current_comm:
1468 		return &bpf_get_current_comm_proto;
1469 	case BPF_FUNC_trace_printk:
1470 		return bpf_get_trace_printk_proto();
1471 	case BPF_FUNC_get_smp_processor_id:
1472 		return &bpf_get_smp_processor_id_proto;
1473 	case BPF_FUNC_get_numa_node_id:
1474 		return &bpf_get_numa_node_id_proto;
1475 	case BPF_FUNC_perf_event_read:
1476 		return &bpf_perf_event_read_proto;
1477 	case BPF_FUNC_get_prandom_u32:
1478 		return &bpf_get_prandom_u32_proto;
1479 	case BPF_FUNC_probe_read_user:
1480 		return &bpf_probe_read_user_proto;
1481 	case BPF_FUNC_probe_read_kernel:
1482 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1483 		       NULL : &bpf_probe_read_kernel_proto;
1484 	case BPF_FUNC_probe_read_user_str:
1485 		return &bpf_probe_read_user_str_proto;
1486 	case BPF_FUNC_probe_read_kernel_str:
1487 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1488 		       NULL : &bpf_probe_read_kernel_str_proto;
1489 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1490 	case BPF_FUNC_probe_read:
1491 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1492 		       NULL : &bpf_probe_read_compat_proto;
1493 	case BPF_FUNC_probe_read_str:
1494 		return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ?
1495 		       NULL : &bpf_probe_read_compat_str_proto;
1496 #endif
1497 #ifdef CONFIG_CGROUPS
1498 	case BPF_FUNC_cgrp_storage_get:
1499 		return &bpf_cgrp_storage_get_proto;
1500 	case BPF_FUNC_cgrp_storage_delete:
1501 		return &bpf_cgrp_storage_delete_proto;
1502 	case BPF_FUNC_current_task_under_cgroup:
1503 		return &bpf_current_task_under_cgroup_proto;
1504 #endif
1505 	case BPF_FUNC_send_signal:
1506 		return &bpf_send_signal_proto;
1507 	case BPF_FUNC_send_signal_thread:
1508 		return &bpf_send_signal_thread_proto;
1509 	case BPF_FUNC_perf_event_read_value:
1510 		return &bpf_perf_event_read_value_proto;
1511 	case BPF_FUNC_ringbuf_output:
1512 		return &bpf_ringbuf_output_proto;
1513 	case BPF_FUNC_ringbuf_reserve:
1514 		return &bpf_ringbuf_reserve_proto;
1515 	case BPF_FUNC_ringbuf_submit:
1516 		return &bpf_ringbuf_submit_proto;
1517 	case BPF_FUNC_ringbuf_discard:
1518 		return &bpf_ringbuf_discard_proto;
1519 	case BPF_FUNC_ringbuf_query:
1520 		return &bpf_ringbuf_query_proto;
1521 	case BPF_FUNC_jiffies64:
1522 		return &bpf_jiffies64_proto;
1523 	case BPF_FUNC_get_task_stack:
1524 		return prog->sleepable ? &bpf_get_task_stack_sleepable_proto
1525 				       : &bpf_get_task_stack_proto;
1526 	case BPF_FUNC_copy_from_user:
1527 		return &bpf_copy_from_user_proto;
1528 	case BPF_FUNC_copy_from_user_task:
1529 		return &bpf_copy_from_user_task_proto;
1530 	case BPF_FUNC_snprintf_btf:
1531 		return &bpf_snprintf_btf_proto;
1532 	case BPF_FUNC_per_cpu_ptr:
1533 		return &bpf_per_cpu_ptr_proto;
1534 	case BPF_FUNC_this_cpu_ptr:
1535 		return &bpf_this_cpu_ptr_proto;
1536 	case BPF_FUNC_task_storage_get:
1537 		if (bpf_prog_check_recur(prog))
1538 			return &bpf_task_storage_get_recur_proto;
1539 		return &bpf_task_storage_get_proto;
1540 	case BPF_FUNC_task_storage_delete:
1541 		if (bpf_prog_check_recur(prog))
1542 			return &bpf_task_storage_delete_recur_proto;
1543 		return &bpf_task_storage_delete_proto;
1544 	case BPF_FUNC_for_each_map_elem:
1545 		return &bpf_for_each_map_elem_proto;
1546 	case BPF_FUNC_snprintf:
1547 		return &bpf_snprintf_proto;
1548 	case BPF_FUNC_get_func_ip:
1549 		return &bpf_get_func_ip_proto_tracing;
1550 	case BPF_FUNC_get_branch_snapshot:
1551 		return &bpf_get_branch_snapshot_proto;
1552 	case BPF_FUNC_find_vma:
1553 		return &bpf_find_vma_proto;
1554 	case BPF_FUNC_trace_vprintk:
1555 		return bpf_get_trace_vprintk_proto();
1556 	default:
1557 		break;
1558 	}
1559 
1560 	func_proto = bpf_base_func_proto(func_id, prog);
1561 	if (func_proto)
1562 		return func_proto;
1563 
1564 	if (!bpf_token_capable(prog->aux->token, CAP_SYS_ADMIN))
1565 		return NULL;
1566 
1567 	switch (func_id) {
1568 	case BPF_FUNC_probe_write_user:
1569 		return security_locked_down(LOCKDOWN_BPF_WRITE_USER) < 0 ?
1570 		       NULL : &bpf_probe_write_user_proto;
1571 	default:
1572 		return NULL;
1573 	}
1574 }
1575 
1576 static bool is_kprobe_multi(const struct bpf_prog *prog)
1577 {
1578 	return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ||
1579 	       prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION;
1580 }
1581 
1582 static inline bool is_kprobe_session(const struct bpf_prog *prog)
1583 {
1584 	return prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION;
1585 }
1586 
1587 static inline bool is_uprobe_multi(const struct bpf_prog *prog)
1588 {
1589 	return prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI ||
1590 	       prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION;
1591 }
1592 
1593 static inline bool is_uprobe_session(const struct bpf_prog *prog)
1594 {
1595 	return prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION;
1596 }
1597 
1598 static const struct bpf_func_proto *
1599 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1600 {
1601 	switch (func_id) {
1602 	case BPF_FUNC_perf_event_output:
1603 		return &bpf_perf_event_output_proto;
1604 	case BPF_FUNC_get_stackid:
1605 		return &bpf_get_stackid_proto;
1606 	case BPF_FUNC_get_stack:
1607 		return prog->sleepable ? &bpf_get_stack_sleepable_proto : &bpf_get_stack_proto;
1608 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
1609 	case BPF_FUNC_override_return:
1610 		return &bpf_override_return_proto;
1611 #endif
1612 	case BPF_FUNC_get_func_ip:
1613 		if (is_kprobe_multi(prog))
1614 			return &bpf_get_func_ip_proto_kprobe_multi;
1615 		if (is_uprobe_multi(prog))
1616 			return &bpf_get_func_ip_proto_uprobe_multi;
1617 		return &bpf_get_func_ip_proto_kprobe;
1618 	case BPF_FUNC_get_attach_cookie:
1619 		if (is_kprobe_multi(prog))
1620 			return &bpf_get_attach_cookie_proto_kmulti;
1621 		if (is_uprobe_multi(prog))
1622 			return &bpf_get_attach_cookie_proto_umulti;
1623 		return &bpf_get_attach_cookie_proto_trace;
1624 	default:
1625 		return bpf_tracing_func_proto(func_id, prog);
1626 	}
1627 }
1628 
1629 /* bpf+kprobe programs can access fields of 'struct pt_regs' */
1630 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1631 					const struct bpf_prog *prog,
1632 					struct bpf_insn_access_aux *info)
1633 {
1634 	if (off < 0 || off >= sizeof(struct pt_regs))
1635 		return false;
1636 	if (type != BPF_READ)
1637 		return false;
1638 	if (off % size != 0)
1639 		return false;
1640 	/*
1641 	 * Assertion for 32 bit to make sure last 8 byte access
1642 	 * (BPF_DW) to the last 4 byte member is disallowed.
1643 	 */
1644 	if (off + size > sizeof(struct pt_regs))
1645 		return false;
1646 
1647 	return true;
1648 }
1649 
1650 const struct bpf_verifier_ops kprobe_verifier_ops = {
1651 	.get_func_proto  = kprobe_prog_func_proto,
1652 	.is_valid_access = kprobe_prog_is_valid_access,
1653 };
1654 
1655 const struct bpf_prog_ops kprobe_prog_ops = {
1656 };
1657 
1658 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
1659 	   u64, flags, void *, data, u64, size)
1660 {
1661 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1662 
1663 	/*
1664 	 * r1 points to perf tracepoint buffer where first 8 bytes are hidden
1665 	 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it
1666 	 * from there and call the same bpf_perf_event_output() helper inline.
1667 	 */
1668 	return ____bpf_perf_event_output(regs, map, flags, data, size);
1669 }
1670 
1671 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = {
1672 	.func		= bpf_perf_event_output_tp,
1673 	.gpl_only	= true,
1674 	.ret_type	= RET_INTEGER,
1675 	.arg1_type	= ARG_PTR_TO_CTX,
1676 	.arg2_type	= ARG_CONST_MAP_PTR,
1677 	.arg3_type	= ARG_ANYTHING,
1678 	.arg4_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1679 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
1680 };
1681 
1682 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map,
1683 	   u64, flags)
1684 {
1685 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1686 
1687 	/*
1688 	 * Same comment as in bpf_perf_event_output_tp(), only that this time
1689 	 * the other helper's function body cannot be inlined due to being
1690 	 * external, thus we need to call raw helper function.
1691 	 */
1692 	return bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1693 			       flags, 0, 0);
1694 }
1695 
1696 static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
1697 	.func		= bpf_get_stackid_tp,
1698 	.gpl_only	= true,
1699 	.ret_type	= RET_INTEGER,
1700 	.arg1_type	= ARG_PTR_TO_CTX,
1701 	.arg2_type	= ARG_CONST_MAP_PTR,
1702 	.arg3_type	= ARG_ANYTHING,
1703 };
1704 
1705 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size,
1706 	   u64, flags)
1707 {
1708 	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
1709 
1710 	return bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1711 			     (unsigned long) size, flags, 0);
1712 }
1713 
1714 static const struct bpf_func_proto bpf_get_stack_proto_tp = {
1715 	.func		= bpf_get_stack_tp,
1716 	.gpl_only	= true,
1717 	.ret_type	= RET_INTEGER,
1718 	.arg1_type	= ARG_PTR_TO_CTX,
1719 	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
1720 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
1721 	.arg4_type	= ARG_ANYTHING,
1722 };
1723 
1724 static const struct bpf_func_proto *
1725 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1726 {
1727 	switch (func_id) {
1728 	case BPF_FUNC_perf_event_output:
1729 		return &bpf_perf_event_output_proto_tp;
1730 	case BPF_FUNC_get_stackid:
1731 		return &bpf_get_stackid_proto_tp;
1732 	case BPF_FUNC_get_stack:
1733 		return &bpf_get_stack_proto_tp;
1734 	case BPF_FUNC_get_attach_cookie:
1735 		return &bpf_get_attach_cookie_proto_trace;
1736 	default:
1737 		return bpf_tracing_func_proto(func_id, prog);
1738 	}
1739 }
1740 
1741 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type,
1742 				    const struct bpf_prog *prog,
1743 				    struct bpf_insn_access_aux *info)
1744 {
1745 	if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE)
1746 		return false;
1747 	if (type != BPF_READ)
1748 		return false;
1749 	if (off % size != 0)
1750 		return false;
1751 
1752 	BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64));
1753 	return true;
1754 }
1755 
1756 const struct bpf_verifier_ops tracepoint_verifier_ops = {
1757 	.get_func_proto  = tp_prog_func_proto,
1758 	.is_valid_access = tp_prog_is_valid_access,
1759 };
1760 
1761 const struct bpf_prog_ops tracepoint_prog_ops = {
1762 };
1763 
1764 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx,
1765 	   struct bpf_perf_event_value *, buf, u32, size)
1766 {
1767 	int err = -EINVAL;
1768 
1769 	if (unlikely(size != sizeof(struct bpf_perf_event_value)))
1770 		goto clear;
1771 	err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
1772 				    &buf->running);
1773 	if (unlikely(err))
1774 		goto clear;
1775 	return 0;
1776 clear:
1777 	memset(buf, 0, size);
1778 	return err;
1779 }
1780 
1781 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = {
1782          .func           = bpf_perf_prog_read_value,
1783          .gpl_only       = true,
1784          .ret_type       = RET_INTEGER,
1785          .arg1_type      = ARG_PTR_TO_CTX,
1786          .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
1787          .arg3_type      = ARG_CONST_SIZE,
1788 };
1789 
1790 BPF_CALL_4(bpf_read_branch_records, struct bpf_perf_event_data_kern *, ctx,
1791 	   void *, buf, u32, size, u64, flags)
1792 {
1793 	static const u32 br_entry_size = sizeof(struct perf_branch_entry);
1794 	struct perf_branch_stack *br_stack = ctx->data->br_stack;
1795 	u32 to_copy;
1796 
1797 	if (unlikely(flags & ~BPF_F_GET_BRANCH_RECORDS_SIZE))
1798 		return -EINVAL;
1799 
1800 	if (unlikely(!(ctx->data->sample_flags & PERF_SAMPLE_BRANCH_STACK)))
1801 		return -ENOENT;
1802 
1803 	if (unlikely(!br_stack))
1804 		return -ENOENT;
1805 
1806 	if (flags & BPF_F_GET_BRANCH_RECORDS_SIZE)
1807 		return br_stack->nr * br_entry_size;
1808 
1809 	if (!buf || (size % br_entry_size != 0))
1810 		return -EINVAL;
1811 
1812 	to_copy = min_t(u32, br_stack->nr * br_entry_size, size);
1813 	memcpy(buf, br_stack->entries, to_copy);
1814 
1815 	return to_copy;
1816 }
1817 
1818 static const struct bpf_func_proto bpf_read_branch_records_proto = {
1819 	.func           = bpf_read_branch_records,
1820 	.gpl_only       = true,
1821 	.ret_type       = RET_INTEGER,
1822 	.arg1_type      = ARG_PTR_TO_CTX,
1823 	.arg2_type      = ARG_PTR_TO_MEM_OR_NULL,
1824 	.arg3_type      = ARG_CONST_SIZE_OR_ZERO,
1825 	.arg4_type      = ARG_ANYTHING,
1826 };
1827 
1828 static const struct bpf_func_proto *
1829 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1830 {
1831 	switch (func_id) {
1832 	case BPF_FUNC_perf_event_output:
1833 		return &bpf_perf_event_output_proto_tp;
1834 	case BPF_FUNC_get_stackid:
1835 		return &bpf_get_stackid_proto_pe;
1836 	case BPF_FUNC_get_stack:
1837 		return &bpf_get_stack_proto_pe;
1838 	case BPF_FUNC_perf_prog_read_value:
1839 		return &bpf_perf_prog_read_value_proto;
1840 	case BPF_FUNC_read_branch_records:
1841 		return &bpf_read_branch_records_proto;
1842 	case BPF_FUNC_get_attach_cookie:
1843 		return &bpf_get_attach_cookie_proto_pe;
1844 	default:
1845 		return bpf_tracing_func_proto(func_id, prog);
1846 	}
1847 }
1848 
1849 /*
1850  * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp
1851  * to avoid potential recursive reuse issue when/if tracepoints are added
1852  * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack.
1853  *
1854  * Since raw tracepoints run despite bpf_prog_active, support concurrent usage
1855  * in normal, irq, and nmi context.
1856  */
1857 struct bpf_raw_tp_regs {
1858 	struct pt_regs regs[3];
1859 };
1860 static DEFINE_PER_CPU(struct bpf_raw_tp_regs, bpf_raw_tp_regs);
1861 static DEFINE_PER_CPU(int, bpf_raw_tp_nest_level);
1862 static struct pt_regs *get_bpf_raw_tp_regs(void)
1863 {
1864 	struct bpf_raw_tp_regs *tp_regs = this_cpu_ptr(&bpf_raw_tp_regs);
1865 	int nest_level = this_cpu_inc_return(bpf_raw_tp_nest_level);
1866 
1867 	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(tp_regs->regs))) {
1868 		this_cpu_dec(bpf_raw_tp_nest_level);
1869 		return ERR_PTR(-EBUSY);
1870 	}
1871 
1872 	return &tp_regs->regs[nest_level - 1];
1873 }
1874 
1875 static void put_bpf_raw_tp_regs(void)
1876 {
1877 	this_cpu_dec(bpf_raw_tp_nest_level);
1878 }
1879 
1880 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
1881 	   struct bpf_map *, map, u64, flags, void *, data, u64, size)
1882 {
1883 	struct pt_regs *regs = get_bpf_raw_tp_regs();
1884 	int ret;
1885 
1886 	if (IS_ERR(regs))
1887 		return PTR_ERR(regs);
1888 
1889 	perf_fetch_caller_regs(regs);
1890 	ret = ____bpf_perf_event_output(regs, map, flags, data, size);
1891 
1892 	put_bpf_raw_tp_regs();
1893 	return ret;
1894 }
1895 
1896 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
1897 	.func		= bpf_perf_event_output_raw_tp,
1898 	.gpl_only	= true,
1899 	.ret_type	= RET_INTEGER,
1900 	.arg1_type	= ARG_PTR_TO_CTX,
1901 	.arg2_type	= ARG_CONST_MAP_PTR,
1902 	.arg3_type	= ARG_ANYTHING,
1903 	.arg4_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1904 	.arg5_type	= ARG_CONST_SIZE_OR_ZERO,
1905 };
1906 
1907 extern const struct bpf_func_proto bpf_skb_output_proto;
1908 extern const struct bpf_func_proto bpf_xdp_output_proto;
1909 extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
1910 
1911 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
1912 	   struct bpf_map *, map, u64, flags)
1913 {
1914 	struct pt_regs *regs = get_bpf_raw_tp_regs();
1915 	int ret;
1916 
1917 	if (IS_ERR(regs))
1918 		return PTR_ERR(regs);
1919 
1920 	perf_fetch_caller_regs(regs);
1921 	/* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */
1922 	ret = bpf_get_stackid((unsigned long) regs, (unsigned long) map,
1923 			      flags, 0, 0);
1924 	put_bpf_raw_tp_regs();
1925 	return ret;
1926 }
1927 
1928 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = {
1929 	.func		= bpf_get_stackid_raw_tp,
1930 	.gpl_only	= true,
1931 	.ret_type	= RET_INTEGER,
1932 	.arg1_type	= ARG_PTR_TO_CTX,
1933 	.arg2_type	= ARG_CONST_MAP_PTR,
1934 	.arg3_type	= ARG_ANYTHING,
1935 };
1936 
1937 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args,
1938 	   void *, buf, u32, size, u64, flags)
1939 {
1940 	struct pt_regs *regs = get_bpf_raw_tp_regs();
1941 	int ret;
1942 
1943 	if (IS_ERR(regs))
1944 		return PTR_ERR(regs);
1945 
1946 	perf_fetch_caller_regs(regs);
1947 	ret = bpf_get_stack((unsigned long) regs, (unsigned long) buf,
1948 			    (unsigned long) size, flags, 0);
1949 	put_bpf_raw_tp_regs();
1950 	return ret;
1951 }
1952 
1953 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
1954 	.func		= bpf_get_stack_raw_tp,
1955 	.gpl_only	= true,
1956 	.ret_type	= RET_INTEGER,
1957 	.arg1_type	= ARG_PTR_TO_CTX,
1958 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
1959 	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
1960 	.arg4_type	= ARG_ANYTHING,
1961 };
1962 
1963 static const struct bpf_func_proto *
1964 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1965 {
1966 	switch (func_id) {
1967 	case BPF_FUNC_perf_event_output:
1968 		return &bpf_perf_event_output_proto_raw_tp;
1969 	case BPF_FUNC_get_stackid:
1970 		return &bpf_get_stackid_proto_raw_tp;
1971 	case BPF_FUNC_get_stack:
1972 		return &bpf_get_stack_proto_raw_tp;
1973 	case BPF_FUNC_get_attach_cookie:
1974 		return &bpf_get_attach_cookie_proto_tracing;
1975 	default:
1976 		return bpf_tracing_func_proto(func_id, prog);
1977 	}
1978 }
1979 
1980 const struct bpf_func_proto *
1981 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1982 {
1983 	const struct bpf_func_proto *fn;
1984 
1985 	switch (func_id) {
1986 #ifdef CONFIG_NET
1987 	case BPF_FUNC_skb_output:
1988 		return &bpf_skb_output_proto;
1989 	case BPF_FUNC_xdp_output:
1990 		return &bpf_xdp_output_proto;
1991 	case BPF_FUNC_skc_to_tcp6_sock:
1992 		return &bpf_skc_to_tcp6_sock_proto;
1993 	case BPF_FUNC_skc_to_tcp_sock:
1994 		return &bpf_skc_to_tcp_sock_proto;
1995 	case BPF_FUNC_skc_to_tcp_timewait_sock:
1996 		return &bpf_skc_to_tcp_timewait_sock_proto;
1997 	case BPF_FUNC_skc_to_tcp_request_sock:
1998 		return &bpf_skc_to_tcp_request_sock_proto;
1999 	case BPF_FUNC_skc_to_udp6_sock:
2000 		return &bpf_skc_to_udp6_sock_proto;
2001 	case BPF_FUNC_skc_to_unix_sock:
2002 		return &bpf_skc_to_unix_sock_proto;
2003 	case BPF_FUNC_skc_to_mptcp_sock:
2004 		return &bpf_skc_to_mptcp_sock_proto;
2005 	case BPF_FUNC_sk_storage_get:
2006 		return &bpf_sk_storage_get_tracing_proto;
2007 	case BPF_FUNC_sk_storage_delete:
2008 		return &bpf_sk_storage_delete_tracing_proto;
2009 	case BPF_FUNC_sock_from_file:
2010 		return &bpf_sock_from_file_proto;
2011 	case BPF_FUNC_get_socket_cookie:
2012 		return &bpf_get_socket_ptr_cookie_proto;
2013 	case BPF_FUNC_xdp_get_buff_len:
2014 		return &bpf_xdp_get_buff_len_trace_proto;
2015 #endif
2016 	case BPF_FUNC_seq_printf:
2017 		return prog->expected_attach_type == BPF_TRACE_ITER ?
2018 		       &bpf_seq_printf_proto :
2019 		       NULL;
2020 	case BPF_FUNC_seq_write:
2021 		return prog->expected_attach_type == BPF_TRACE_ITER ?
2022 		       &bpf_seq_write_proto :
2023 		       NULL;
2024 	case BPF_FUNC_seq_printf_btf:
2025 		return prog->expected_attach_type == BPF_TRACE_ITER ?
2026 		       &bpf_seq_printf_btf_proto :
2027 		       NULL;
2028 	case BPF_FUNC_d_path:
2029 		return &bpf_d_path_proto;
2030 	case BPF_FUNC_get_func_arg:
2031 		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
2032 	case BPF_FUNC_get_func_ret:
2033 		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
2034 	case BPF_FUNC_get_func_arg_cnt:
2035 		return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
2036 	case BPF_FUNC_get_attach_cookie:
2037 		if (prog->type == BPF_PROG_TYPE_TRACING &&
2038 		    prog->expected_attach_type == BPF_TRACE_RAW_TP)
2039 			return &bpf_get_attach_cookie_proto_tracing;
2040 		return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto_tracing : NULL;
2041 	default:
2042 		fn = raw_tp_prog_func_proto(func_id, prog);
2043 		if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
2044 			fn = bpf_iter_get_func_proto(func_id, prog);
2045 		return fn;
2046 	}
2047 }
2048 
2049 static bool raw_tp_prog_is_valid_access(int off, int size,
2050 					enum bpf_access_type type,
2051 					const struct bpf_prog *prog,
2052 					struct bpf_insn_access_aux *info)
2053 {
2054 	return bpf_tracing_ctx_access(off, size, type);
2055 }
2056 
2057 static bool tracing_prog_is_valid_access(int off, int size,
2058 					 enum bpf_access_type type,
2059 					 const struct bpf_prog *prog,
2060 					 struct bpf_insn_access_aux *info)
2061 {
2062 	return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
2063 }
2064 
2065 int __weak bpf_prog_test_run_tracing(struct bpf_prog *prog,
2066 				     const union bpf_attr *kattr,
2067 				     union bpf_attr __user *uattr)
2068 {
2069 	return -ENOTSUPP;
2070 }
2071 
2072 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = {
2073 	.get_func_proto  = raw_tp_prog_func_proto,
2074 	.is_valid_access = raw_tp_prog_is_valid_access,
2075 };
2076 
2077 const struct bpf_prog_ops raw_tracepoint_prog_ops = {
2078 #ifdef CONFIG_NET
2079 	.test_run = bpf_prog_test_run_raw_tp,
2080 #endif
2081 };
2082 
2083 const struct bpf_verifier_ops tracing_verifier_ops = {
2084 	.get_func_proto  = tracing_prog_func_proto,
2085 	.is_valid_access = tracing_prog_is_valid_access,
2086 };
2087 
2088 const struct bpf_prog_ops tracing_prog_ops = {
2089 	.test_run = bpf_prog_test_run_tracing,
2090 };
2091 
2092 static bool raw_tp_writable_prog_is_valid_access(int off, int size,
2093 						 enum bpf_access_type type,
2094 						 const struct bpf_prog *prog,
2095 						 struct bpf_insn_access_aux *info)
2096 {
2097 	if (off == 0) {
2098 		if (size != sizeof(u64) || type != BPF_READ)
2099 			return false;
2100 		info->reg_type = PTR_TO_TP_BUFFER;
2101 	}
2102 	return raw_tp_prog_is_valid_access(off, size, type, prog, info);
2103 }
2104 
2105 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = {
2106 	.get_func_proto  = raw_tp_prog_func_proto,
2107 	.is_valid_access = raw_tp_writable_prog_is_valid_access,
2108 };
2109 
2110 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = {
2111 };
2112 
2113 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
2114 				    const struct bpf_prog *prog,
2115 				    struct bpf_insn_access_aux *info)
2116 {
2117 	const int size_u64 = sizeof(u64);
2118 
2119 	if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
2120 		return false;
2121 	if (type != BPF_READ)
2122 		return false;
2123 	if (off % size != 0) {
2124 		if (sizeof(unsigned long) != 4)
2125 			return false;
2126 		if (size != 8)
2127 			return false;
2128 		if (off % size != 4)
2129 			return false;
2130 	}
2131 
2132 	switch (off) {
2133 	case bpf_ctx_range(struct bpf_perf_event_data, sample_period):
2134 		bpf_ctx_record_field_size(info, size_u64);
2135 		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2136 			return false;
2137 		break;
2138 	case bpf_ctx_range(struct bpf_perf_event_data, addr):
2139 		bpf_ctx_record_field_size(info, size_u64);
2140 		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
2141 			return false;
2142 		break;
2143 	default:
2144 		if (size != sizeof(long))
2145 			return false;
2146 	}
2147 
2148 	return true;
2149 }
2150 
2151 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
2152 				      const struct bpf_insn *si,
2153 				      struct bpf_insn *insn_buf,
2154 				      struct bpf_prog *prog, u32 *target_size)
2155 {
2156 	struct bpf_insn *insn = insn_buf;
2157 
2158 	switch (si->off) {
2159 	case offsetof(struct bpf_perf_event_data, sample_period):
2160 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2161 						       data), si->dst_reg, si->src_reg,
2162 				      offsetof(struct bpf_perf_event_data_kern, data));
2163 		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2164 				      bpf_target_off(struct perf_sample_data, period, 8,
2165 						     target_size));
2166 		break;
2167 	case offsetof(struct bpf_perf_event_data, addr):
2168 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2169 						       data), si->dst_reg, si->src_reg,
2170 				      offsetof(struct bpf_perf_event_data_kern, data));
2171 		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
2172 				      bpf_target_off(struct perf_sample_data, addr, 8,
2173 						     target_size));
2174 		break;
2175 	default:
2176 		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
2177 						       regs), si->dst_reg, si->src_reg,
2178 				      offsetof(struct bpf_perf_event_data_kern, regs));
2179 		*insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg,
2180 				      si->off);
2181 		break;
2182 	}
2183 
2184 	return insn - insn_buf;
2185 }
2186 
2187 const struct bpf_verifier_ops perf_event_verifier_ops = {
2188 	.get_func_proto		= pe_prog_func_proto,
2189 	.is_valid_access	= pe_prog_is_valid_access,
2190 	.convert_ctx_access	= pe_prog_convert_ctx_access,
2191 };
2192 
2193 const struct bpf_prog_ops perf_event_prog_ops = {
2194 };
2195 
2196 static DEFINE_MUTEX(bpf_event_mutex);
2197 
2198 #define BPF_TRACE_MAX_PROGS 64
2199 
2200 int perf_event_attach_bpf_prog(struct perf_event *event,
2201 			       struct bpf_prog *prog,
2202 			       u64 bpf_cookie)
2203 {
2204 	struct bpf_prog_array *old_array;
2205 	struct bpf_prog_array *new_array;
2206 	int ret = -EEXIST;
2207 
2208 	/*
2209 	 * Kprobe override only works if they are on the function entry,
2210 	 * and only if they are on the opt-in list.
2211 	 */
2212 	if (prog->kprobe_override &&
2213 	    (!trace_kprobe_on_func_entry(event->tp_event) ||
2214 	     !trace_kprobe_error_injectable(event->tp_event)))
2215 		return -EINVAL;
2216 
2217 	mutex_lock(&bpf_event_mutex);
2218 
2219 	if (event->prog)
2220 		goto unlock;
2221 
2222 	old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2223 	if (old_array &&
2224 	    bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) {
2225 		ret = -E2BIG;
2226 		goto unlock;
2227 	}
2228 
2229 	ret = bpf_prog_array_copy(old_array, NULL, prog, bpf_cookie, &new_array);
2230 	if (ret < 0)
2231 		goto unlock;
2232 
2233 	/* set the new array to event->tp_event and set event->prog */
2234 	event->prog = prog;
2235 	event->bpf_cookie = bpf_cookie;
2236 	rcu_assign_pointer(event->tp_event->prog_array, new_array);
2237 	bpf_prog_array_free_sleepable(old_array);
2238 
2239 unlock:
2240 	mutex_unlock(&bpf_event_mutex);
2241 	return ret;
2242 }
2243 
2244 void perf_event_detach_bpf_prog(struct perf_event *event)
2245 {
2246 	struct bpf_prog_array *old_array;
2247 	struct bpf_prog_array *new_array;
2248 	int ret;
2249 
2250 	mutex_lock(&bpf_event_mutex);
2251 
2252 	if (!event->prog)
2253 		goto unlock;
2254 
2255 	old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2256 	if (!old_array)
2257 		goto put;
2258 
2259 	ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2260 	if (ret < 0) {
2261 		bpf_prog_array_delete_safe(old_array, event->prog);
2262 	} else {
2263 		rcu_assign_pointer(event->tp_event->prog_array, new_array);
2264 		bpf_prog_array_free_sleepable(old_array);
2265 	}
2266 
2267 put:
2268 	/*
2269 	 * It could be that the bpf_prog is not sleepable (and will be freed
2270 	 * via normal RCU), but is called from a point that supports sleepable
2271 	 * programs and uses tasks-trace-RCU.
2272 	 */
2273 	synchronize_rcu_tasks_trace();
2274 
2275 	bpf_prog_put(event->prog);
2276 	event->prog = NULL;
2277 
2278 unlock:
2279 	mutex_unlock(&bpf_event_mutex);
2280 }
2281 
2282 int perf_event_query_prog_array(struct perf_event *event, void __user *info)
2283 {
2284 	struct perf_event_query_bpf __user *uquery = info;
2285 	struct perf_event_query_bpf query = {};
2286 	struct bpf_prog_array *progs;
2287 	u32 *ids, prog_cnt, ids_len;
2288 	int ret;
2289 
2290 	if (!perfmon_capable())
2291 		return -EPERM;
2292 	if (event->attr.type != PERF_TYPE_TRACEPOINT)
2293 		return -EINVAL;
2294 	if (copy_from_user(&query, uquery, sizeof(query)))
2295 		return -EFAULT;
2296 
2297 	ids_len = query.ids_len;
2298 	if (ids_len > BPF_TRACE_MAX_PROGS)
2299 		return -E2BIG;
2300 	ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN);
2301 	if (!ids)
2302 		return -ENOMEM;
2303 	/*
2304 	 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which
2305 	 * is required when user only wants to check for uquery->prog_cnt.
2306 	 * There is no need to check for it since the case is handled
2307 	 * gracefully in bpf_prog_array_copy_info.
2308 	 */
2309 
2310 	mutex_lock(&bpf_event_mutex);
2311 	progs = bpf_event_rcu_dereference(event->tp_event->prog_array);
2312 	ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt);
2313 	mutex_unlock(&bpf_event_mutex);
2314 
2315 	if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) ||
2316 	    copy_to_user(uquery->ids, ids, ids_len * sizeof(u32)))
2317 		ret = -EFAULT;
2318 
2319 	kfree(ids);
2320 	return ret;
2321 }
2322 
2323 extern struct bpf_raw_event_map __start__bpf_raw_tp[];
2324 extern struct bpf_raw_event_map __stop__bpf_raw_tp[];
2325 
2326 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
2327 {
2328 	struct bpf_raw_event_map *btp = __start__bpf_raw_tp;
2329 
2330 	for (; btp < __stop__bpf_raw_tp; btp++) {
2331 		if (!strcmp(btp->tp->name, name))
2332 			return btp;
2333 	}
2334 
2335 	return bpf_get_raw_tracepoint_module(name);
2336 }
2337 
2338 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
2339 {
2340 	struct module *mod;
2341 
2342 	preempt_disable();
2343 	mod = __module_address((unsigned long)btp);
2344 	module_put(mod);
2345 	preempt_enable();
2346 }
2347 
2348 static __always_inline
2349 void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
2350 {
2351 	struct bpf_prog *prog = link->link.prog;
2352 	struct bpf_run_ctx *old_run_ctx;
2353 	struct bpf_trace_run_ctx run_ctx;
2354 
2355 	cant_sleep();
2356 	if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
2357 		bpf_prog_inc_misses_counter(prog);
2358 		goto out;
2359 	}
2360 
2361 	run_ctx.bpf_cookie = link->cookie;
2362 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2363 
2364 	rcu_read_lock();
2365 	(void) bpf_prog_run(prog, args);
2366 	rcu_read_unlock();
2367 
2368 	bpf_reset_run_ctx(old_run_ctx);
2369 out:
2370 	this_cpu_dec(*(prog->active));
2371 }
2372 
2373 #define UNPACK(...)			__VA_ARGS__
2374 #define REPEAT_1(FN, DL, X, ...)	FN(X)
2375 #define REPEAT_2(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__)
2376 #define REPEAT_3(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__)
2377 #define REPEAT_4(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__)
2378 #define REPEAT_5(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__)
2379 #define REPEAT_6(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__)
2380 #define REPEAT_7(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__)
2381 #define REPEAT_8(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__)
2382 #define REPEAT_9(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__)
2383 #define REPEAT_10(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__)
2384 #define REPEAT_11(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__)
2385 #define REPEAT_12(FN, DL, X, ...)	FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__)
2386 #define REPEAT(X, FN, DL, ...)		REPEAT_##X(FN, DL, __VA_ARGS__)
2387 
2388 #define SARG(X)		u64 arg##X
2389 #define COPY(X)		args[X] = arg##X
2390 
2391 #define __DL_COM	(,)
2392 #define __DL_SEM	(;)
2393 
2394 #define __SEQ_0_11	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2395 
2396 #define BPF_TRACE_DEFN_x(x)						\
2397 	void bpf_trace_run##x(struct bpf_raw_tp_link *link,		\
2398 			      REPEAT(x, SARG, __DL_COM, __SEQ_0_11))	\
2399 	{								\
2400 		u64 args[x];						\
2401 		REPEAT(x, COPY, __DL_SEM, __SEQ_0_11);			\
2402 		__bpf_trace_run(link, args);				\
2403 	}								\
2404 	EXPORT_SYMBOL_GPL(bpf_trace_run##x)
2405 BPF_TRACE_DEFN_x(1);
2406 BPF_TRACE_DEFN_x(2);
2407 BPF_TRACE_DEFN_x(3);
2408 BPF_TRACE_DEFN_x(4);
2409 BPF_TRACE_DEFN_x(5);
2410 BPF_TRACE_DEFN_x(6);
2411 BPF_TRACE_DEFN_x(7);
2412 BPF_TRACE_DEFN_x(8);
2413 BPF_TRACE_DEFN_x(9);
2414 BPF_TRACE_DEFN_x(10);
2415 BPF_TRACE_DEFN_x(11);
2416 BPF_TRACE_DEFN_x(12);
2417 
2418 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
2419 {
2420 	struct tracepoint *tp = btp->tp;
2421 	struct bpf_prog *prog = link->link.prog;
2422 
2423 	/*
2424 	 * check that program doesn't access arguments beyond what's
2425 	 * available in this tracepoint
2426 	 */
2427 	if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64))
2428 		return -EINVAL;
2429 
2430 	if (prog->aux->max_tp_access > btp->writable_size)
2431 		return -EINVAL;
2432 
2433 	return tracepoint_probe_register_may_exist(tp, (void *)btp->bpf_func, link);
2434 }
2435 
2436 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_raw_tp_link *link)
2437 {
2438 	return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, link);
2439 }
2440 
2441 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
2442 			    u32 *fd_type, const char **buf,
2443 			    u64 *probe_offset, u64 *probe_addr,
2444 			    unsigned long *missed)
2445 {
2446 	bool is_tracepoint, is_syscall_tp;
2447 	struct bpf_prog *prog;
2448 	int flags, err = 0;
2449 
2450 	prog = event->prog;
2451 	if (!prog)
2452 		return -ENOENT;
2453 
2454 	/* not supporting BPF_PROG_TYPE_PERF_EVENT yet */
2455 	if (prog->type == BPF_PROG_TYPE_PERF_EVENT)
2456 		return -EOPNOTSUPP;
2457 
2458 	*prog_id = prog->aux->id;
2459 	flags = event->tp_event->flags;
2460 	is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT;
2461 	is_syscall_tp = is_syscall_trace_event(event->tp_event);
2462 
2463 	if (is_tracepoint || is_syscall_tp) {
2464 		*buf = is_tracepoint ? event->tp_event->tp->name
2465 				     : event->tp_event->name;
2466 		/* We allow NULL pointer for tracepoint */
2467 		if (fd_type)
2468 			*fd_type = BPF_FD_TYPE_TRACEPOINT;
2469 		if (probe_offset)
2470 			*probe_offset = 0x0;
2471 		if (probe_addr)
2472 			*probe_addr = 0x0;
2473 	} else {
2474 		/* kprobe/uprobe */
2475 		err = -EOPNOTSUPP;
2476 #ifdef CONFIG_KPROBE_EVENTS
2477 		if (flags & TRACE_EVENT_FL_KPROBE)
2478 			err = bpf_get_kprobe_info(event, fd_type, buf,
2479 						  probe_offset, probe_addr, missed,
2480 						  event->attr.type == PERF_TYPE_TRACEPOINT);
2481 #endif
2482 #ifdef CONFIG_UPROBE_EVENTS
2483 		if (flags & TRACE_EVENT_FL_UPROBE)
2484 			err = bpf_get_uprobe_info(event, fd_type, buf,
2485 						  probe_offset, probe_addr,
2486 						  event->attr.type == PERF_TYPE_TRACEPOINT);
2487 #endif
2488 	}
2489 
2490 	return err;
2491 }
2492 
2493 static int __init send_signal_irq_work_init(void)
2494 {
2495 	int cpu;
2496 	struct send_signal_irq_work *work;
2497 
2498 	for_each_possible_cpu(cpu) {
2499 		work = per_cpu_ptr(&send_signal_work, cpu);
2500 		init_irq_work(&work->irq_work, do_bpf_send_signal);
2501 	}
2502 	return 0;
2503 }
2504 
2505 subsys_initcall(send_signal_irq_work_init);
2506 
2507 #ifdef CONFIG_MODULES
2508 static int bpf_event_notify(struct notifier_block *nb, unsigned long op,
2509 			    void *module)
2510 {
2511 	struct bpf_trace_module *btm, *tmp;
2512 	struct module *mod = module;
2513 	int ret = 0;
2514 
2515 	if (mod->num_bpf_raw_events == 0 ||
2516 	    (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING))
2517 		goto out;
2518 
2519 	mutex_lock(&bpf_module_mutex);
2520 
2521 	switch (op) {
2522 	case MODULE_STATE_COMING:
2523 		btm = kzalloc(sizeof(*btm), GFP_KERNEL);
2524 		if (btm) {
2525 			btm->module = module;
2526 			list_add(&btm->list, &bpf_trace_modules);
2527 		} else {
2528 			ret = -ENOMEM;
2529 		}
2530 		break;
2531 	case MODULE_STATE_GOING:
2532 		list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) {
2533 			if (btm->module == module) {
2534 				list_del(&btm->list);
2535 				kfree(btm);
2536 				break;
2537 			}
2538 		}
2539 		break;
2540 	}
2541 
2542 	mutex_unlock(&bpf_module_mutex);
2543 
2544 out:
2545 	return notifier_from_errno(ret);
2546 }
2547 
2548 static struct notifier_block bpf_module_nb = {
2549 	.notifier_call = bpf_event_notify,
2550 };
2551 
2552 static int __init bpf_event_init(void)
2553 {
2554 	register_module_notifier(&bpf_module_nb);
2555 	return 0;
2556 }
2557 
2558 fs_initcall(bpf_event_init);
2559 #endif /* CONFIG_MODULES */
2560 
2561 struct bpf_session_run_ctx {
2562 	struct bpf_run_ctx run_ctx;
2563 	bool is_return;
2564 	void *data;
2565 };
2566 
2567 #ifdef CONFIG_FPROBE
2568 struct bpf_kprobe_multi_link {
2569 	struct bpf_link link;
2570 	struct fprobe fp;
2571 	unsigned long *addrs;
2572 	u64 *cookies;
2573 	u32 cnt;
2574 	u32 mods_cnt;
2575 	struct module **mods;
2576 	u32 flags;
2577 };
2578 
2579 struct bpf_kprobe_multi_run_ctx {
2580 	struct bpf_session_run_ctx session_ctx;
2581 	struct bpf_kprobe_multi_link *link;
2582 	unsigned long entry_ip;
2583 };
2584 
2585 struct user_syms {
2586 	const char **syms;
2587 	char *buf;
2588 };
2589 
2590 static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt)
2591 {
2592 	unsigned long __user usymbol;
2593 	const char **syms = NULL;
2594 	char *buf = NULL, *p;
2595 	int err = -ENOMEM;
2596 	unsigned int i;
2597 
2598 	syms = kvmalloc_array(cnt, sizeof(*syms), GFP_KERNEL);
2599 	if (!syms)
2600 		goto error;
2601 
2602 	buf = kvmalloc_array(cnt, KSYM_NAME_LEN, GFP_KERNEL);
2603 	if (!buf)
2604 		goto error;
2605 
2606 	for (p = buf, i = 0; i < cnt; i++) {
2607 		if (__get_user(usymbol, usyms + i)) {
2608 			err = -EFAULT;
2609 			goto error;
2610 		}
2611 		err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN);
2612 		if (err == KSYM_NAME_LEN)
2613 			err = -E2BIG;
2614 		if (err < 0)
2615 			goto error;
2616 		syms[i] = p;
2617 		p += err + 1;
2618 	}
2619 
2620 	us->syms = syms;
2621 	us->buf = buf;
2622 	return 0;
2623 
2624 error:
2625 	if (err) {
2626 		kvfree(syms);
2627 		kvfree(buf);
2628 	}
2629 	return err;
2630 }
2631 
2632 static void kprobe_multi_put_modules(struct module **mods, u32 cnt)
2633 {
2634 	u32 i;
2635 
2636 	for (i = 0; i < cnt; i++)
2637 		module_put(mods[i]);
2638 }
2639 
2640 static void free_user_syms(struct user_syms *us)
2641 {
2642 	kvfree(us->syms);
2643 	kvfree(us->buf);
2644 }
2645 
2646 static void bpf_kprobe_multi_link_release(struct bpf_link *link)
2647 {
2648 	struct bpf_kprobe_multi_link *kmulti_link;
2649 
2650 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2651 	unregister_fprobe(&kmulti_link->fp);
2652 	kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
2653 }
2654 
2655 static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
2656 {
2657 	struct bpf_kprobe_multi_link *kmulti_link;
2658 
2659 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2660 	kvfree(kmulti_link->addrs);
2661 	kvfree(kmulti_link->cookies);
2662 	kfree(kmulti_link->mods);
2663 	kfree(kmulti_link);
2664 }
2665 
2666 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
2667 						struct bpf_link_info *info)
2668 {
2669 	u64 __user *ucookies = u64_to_user_ptr(info->kprobe_multi.cookies);
2670 	u64 __user *uaddrs = u64_to_user_ptr(info->kprobe_multi.addrs);
2671 	struct bpf_kprobe_multi_link *kmulti_link;
2672 	u32 ucount = info->kprobe_multi.count;
2673 	int err = 0, i;
2674 
2675 	if (!uaddrs ^ !ucount)
2676 		return -EINVAL;
2677 	if (ucookies && !ucount)
2678 		return -EINVAL;
2679 
2680 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
2681 	info->kprobe_multi.count = kmulti_link->cnt;
2682 	info->kprobe_multi.flags = kmulti_link->flags;
2683 	info->kprobe_multi.missed = kmulti_link->fp.nmissed;
2684 
2685 	if (!uaddrs)
2686 		return 0;
2687 	if (ucount < kmulti_link->cnt)
2688 		err = -ENOSPC;
2689 	else
2690 		ucount = kmulti_link->cnt;
2691 
2692 	if (ucookies) {
2693 		if (kmulti_link->cookies) {
2694 			if (copy_to_user(ucookies, kmulti_link->cookies, ucount * sizeof(u64)))
2695 				return -EFAULT;
2696 		} else {
2697 			for (i = 0; i < ucount; i++) {
2698 				if (put_user(0, ucookies + i))
2699 					return -EFAULT;
2700 			}
2701 		}
2702 	}
2703 
2704 	if (kallsyms_show_value(current_cred())) {
2705 		if (copy_to_user(uaddrs, kmulti_link->addrs, ucount * sizeof(u64)))
2706 			return -EFAULT;
2707 	} else {
2708 		for (i = 0; i < ucount; i++) {
2709 			if (put_user(0, uaddrs + i))
2710 				return -EFAULT;
2711 		}
2712 	}
2713 	return err;
2714 }
2715 
2716 static const struct bpf_link_ops bpf_kprobe_multi_link_lops = {
2717 	.release = bpf_kprobe_multi_link_release,
2718 	.dealloc_deferred = bpf_kprobe_multi_link_dealloc,
2719 	.fill_link_info = bpf_kprobe_multi_link_fill_link_info,
2720 };
2721 
2722 static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv)
2723 {
2724 	const struct bpf_kprobe_multi_link *link = priv;
2725 	unsigned long *addr_a = a, *addr_b = b;
2726 	u64 *cookie_a, *cookie_b;
2727 
2728 	cookie_a = link->cookies + (addr_a - link->addrs);
2729 	cookie_b = link->cookies + (addr_b - link->addrs);
2730 
2731 	/* swap addr_a/addr_b and cookie_a/cookie_b values */
2732 	swap(*addr_a, *addr_b);
2733 	swap(*cookie_a, *cookie_b);
2734 }
2735 
2736 static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b)
2737 {
2738 	const unsigned long *addr_a = a, *addr_b = b;
2739 
2740 	if (*addr_a == *addr_b)
2741 		return 0;
2742 	return *addr_a < *addr_b ? -1 : 1;
2743 }
2744 
2745 static int bpf_kprobe_multi_cookie_cmp(const void *a, const void *b, const void *priv)
2746 {
2747 	return bpf_kprobe_multi_addrs_cmp(a, b);
2748 }
2749 
2750 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
2751 {
2752 	struct bpf_kprobe_multi_run_ctx *run_ctx;
2753 	struct bpf_kprobe_multi_link *link;
2754 	u64 *cookie, entry_ip;
2755 	unsigned long *addr;
2756 
2757 	if (WARN_ON_ONCE(!ctx))
2758 		return 0;
2759 	run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
2760 			       session_ctx.run_ctx);
2761 	link = run_ctx->link;
2762 	if (!link->cookies)
2763 		return 0;
2764 	entry_ip = run_ctx->entry_ip;
2765 	addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip),
2766 		       bpf_kprobe_multi_addrs_cmp);
2767 	if (!addr)
2768 		return 0;
2769 	cookie = link->cookies + (addr - link->addrs);
2770 	return *cookie;
2771 }
2772 
2773 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
2774 {
2775 	struct bpf_kprobe_multi_run_ctx *run_ctx;
2776 
2777 	run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
2778 			       session_ctx.run_ctx);
2779 	return run_ctx->entry_ip;
2780 }
2781 
2782 static int
2783 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
2784 			   unsigned long entry_ip, struct pt_regs *regs,
2785 			   bool is_return, void *data)
2786 {
2787 	struct bpf_kprobe_multi_run_ctx run_ctx = {
2788 		.session_ctx = {
2789 			.is_return = is_return,
2790 			.data = data,
2791 		},
2792 		.link = link,
2793 		.entry_ip = entry_ip,
2794 	};
2795 	struct bpf_run_ctx *old_run_ctx;
2796 	int err;
2797 
2798 	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
2799 		bpf_prog_inc_misses_counter(link->link.prog);
2800 		err = 0;
2801 		goto out;
2802 	}
2803 
2804 	migrate_disable();
2805 	rcu_read_lock();
2806 	old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
2807 	err = bpf_prog_run(link->link.prog, regs);
2808 	bpf_reset_run_ctx(old_run_ctx);
2809 	rcu_read_unlock();
2810 	migrate_enable();
2811 
2812  out:
2813 	__this_cpu_dec(bpf_prog_active);
2814 	return err;
2815 }
2816 
2817 static int
2818 kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,
2819 			  unsigned long ret_ip, struct pt_regs *regs,
2820 			  void *data)
2821 {
2822 	struct bpf_kprobe_multi_link *link;
2823 	int err;
2824 
2825 	link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2826 	err = kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, false, data);
2827 	return is_kprobe_session(link->link.prog) ? err : 0;
2828 }
2829 
2830 static void
2831 kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,
2832 			       unsigned long ret_ip, struct pt_regs *regs,
2833 			       void *data)
2834 {
2835 	struct bpf_kprobe_multi_link *link;
2836 
2837 	link = container_of(fp, struct bpf_kprobe_multi_link, fp);
2838 	kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs, true, data);
2839 }
2840 
2841 static int symbols_cmp_r(const void *a, const void *b, const void *priv)
2842 {
2843 	const char **str_a = (const char **) a;
2844 	const char **str_b = (const char **) b;
2845 
2846 	return strcmp(*str_a, *str_b);
2847 }
2848 
2849 struct multi_symbols_sort {
2850 	const char **funcs;
2851 	u64 *cookies;
2852 };
2853 
2854 static void symbols_swap_r(void *a, void *b, int size, const void *priv)
2855 {
2856 	const struct multi_symbols_sort *data = priv;
2857 	const char **name_a = a, **name_b = b;
2858 
2859 	swap(*name_a, *name_b);
2860 
2861 	/* If defined, swap also related cookies. */
2862 	if (data->cookies) {
2863 		u64 *cookie_a, *cookie_b;
2864 
2865 		cookie_a = data->cookies + (name_a - data->funcs);
2866 		cookie_b = data->cookies + (name_b - data->funcs);
2867 		swap(*cookie_a, *cookie_b);
2868 	}
2869 }
2870 
2871 struct modules_array {
2872 	struct module **mods;
2873 	int mods_cnt;
2874 	int mods_cap;
2875 };
2876 
2877 static int add_module(struct modules_array *arr, struct module *mod)
2878 {
2879 	struct module **mods;
2880 
2881 	if (arr->mods_cnt == arr->mods_cap) {
2882 		arr->mods_cap = max(16, arr->mods_cap * 3 / 2);
2883 		mods = krealloc_array(arr->mods, arr->mods_cap, sizeof(*mods), GFP_KERNEL);
2884 		if (!mods)
2885 			return -ENOMEM;
2886 		arr->mods = mods;
2887 	}
2888 
2889 	arr->mods[arr->mods_cnt] = mod;
2890 	arr->mods_cnt++;
2891 	return 0;
2892 }
2893 
2894 static bool has_module(struct modules_array *arr, struct module *mod)
2895 {
2896 	int i;
2897 
2898 	for (i = arr->mods_cnt - 1; i >= 0; i--) {
2899 		if (arr->mods[i] == mod)
2900 			return true;
2901 	}
2902 	return false;
2903 }
2904 
2905 static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt)
2906 {
2907 	struct modules_array arr = {};
2908 	u32 i, err = 0;
2909 
2910 	for (i = 0; i < addrs_cnt; i++) {
2911 		struct module *mod;
2912 
2913 		preempt_disable();
2914 		mod = __module_address(addrs[i]);
2915 		/* Either no module or we it's already stored  */
2916 		if (!mod || has_module(&arr, mod)) {
2917 			preempt_enable();
2918 			continue;
2919 		}
2920 		if (!try_module_get(mod))
2921 			err = -EINVAL;
2922 		preempt_enable();
2923 		if (err)
2924 			break;
2925 		err = add_module(&arr, mod);
2926 		if (err) {
2927 			module_put(mod);
2928 			break;
2929 		}
2930 	}
2931 
2932 	/* We return either err < 0 in case of error, ... */
2933 	if (err) {
2934 		kprobe_multi_put_modules(arr.mods, arr.mods_cnt);
2935 		kfree(arr.mods);
2936 		return err;
2937 	}
2938 
2939 	/* or number of modules found if everything is ok. */
2940 	*mods = arr.mods;
2941 	return arr.mods_cnt;
2942 }
2943 
2944 static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
2945 {
2946 	u32 i;
2947 
2948 	for (i = 0; i < cnt; i++) {
2949 		if (!within_error_injection_list(addrs[i]))
2950 			return -EINVAL;
2951 	}
2952 	return 0;
2953 }
2954 
2955 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2956 {
2957 	struct bpf_kprobe_multi_link *link = NULL;
2958 	struct bpf_link_primer link_primer;
2959 	void __user *ucookies;
2960 	unsigned long *addrs;
2961 	u32 flags, cnt, size;
2962 	void __user *uaddrs;
2963 	u64 *cookies = NULL;
2964 	void __user *usyms;
2965 	int err;
2966 
2967 	/* no support for 32bit archs yet */
2968 	if (sizeof(u64) != sizeof(void *))
2969 		return -EOPNOTSUPP;
2970 
2971 	if (!is_kprobe_multi(prog))
2972 		return -EINVAL;
2973 
2974 	flags = attr->link_create.kprobe_multi.flags;
2975 	if (flags & ~BPF_F_KPROBE_MULTI_RETURN)
2976 		return -EINVAL;
2977 
2978 	uaddrs = u64_to_user_ptr(attr->link_create.kprobe_multi.addrs);
2979 	usyms = u64_to_user_ptr(attr->link_create.kprobe_multi.syms);
2980 	if (!!uaddrs == !!usyms)
2981 		return -EINVAL;
2982 
2983 	cnt = attr->link_create.kprobe_multi.cnt;
2984 	if (!cnt)
2985 		return -EINVAL;
2986 	if (cnt > MAX_KPROBE_MULTI_CNT)
2987 		return -E2BIG;
2988 
2989 	size = cnt * sizeof(*addrs);
2990 	addrs = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2991 	if (!addrs)
2992 		return -ENOMEM;
2993 
2994 	ucookies = u64_to_user_ptr(attr->link_create.kprobe_multi.cookies);
2995 	if (ucookies) {
2996 		cookies = kvmalloc_array(cnt, sizeof(*addrs), GFP_KERNEL);
2997 		if (!cookies) {
2998 			err = -ENOMEM;
2999 			goto error;
3000 		}
3001 		if (copy_from_user(cookies, ucookies, size)) {
3002 			err = -EFAULT;
3003 			goto error;
3004 		}
3005 	}
3006 
3007 	if (uaddrs) {
3008 		if (copy_from_user(addrs, uaddrs, size)) {
3009 			err = -EFAULT;
3010 			goto error;
3011 		}
3012 	} else {
3013 		struct multi_symbols_sort data = {
3014 			.cookies = cookies,
3015 		};
3016 		struct user_syms us;
3017 
3018 		err = copy_user_syms(&us, usyms, cnt);
3019 		if (err)
3020 			goto error;
3021 
3022 		if (cookies)
3023 			data.funcs = us.syms;
3024 
3025 		sort_r(us.syms, cnt, sizeof(*us.syms), symbols_cmp_r,
3026 		       symbols_swap_r, &data);
3027 
3028 		err = ftrace_lookup_symbols(us.syms, cnt, addrs);
3029 		free_user_syms(&us);
3030 		if (err)
3031 			goto error;
3032 	}
3033 
3034 	if (prog->kprobe_override && addrs_check_error_injection_list(addrs, cnt)) {
3035 		err = -EINVAL;
3036 		goto error;
3037 	}
3038 
3039 	link = kzalloc(sizeof(*link), GFP_KERNEL);
3040 	if (!link) {
3041 		err = -ENOMEM;
3042 		goto error;
3043 	}
3044 
3045 	bpf_link_init(&link->link, BPF_LINK_TYPE_KPROBE_MULTI,
3046 		      &bpf_kprobe_multi_link_lops, prog);
3047 
3048 	err = bpf_link_prime(&link->link, &link_primer);
3049 	if (err)
3050 		goto error;
3051 
3052 	if (!(flags & BPF_F_KPROBE_MULTI_RETURN))
3053 		link->fp.entry_handler = kprobe_multi_link_handler;
3054 	if ((flags & BPF_F_KPROBE_MULTI_RETURN) || is_kprobe_session(prog))
3055 		link->fp.exit_handler = kprobe_multi_link_exit_handler;
3056 	if (is_kprobe_session(prog))
3057 		link->fp.entry_data_size = sizeof(u64);
3058 
3059 	link->addrs = addrs;
3060 	link->cookies = cookies;
3061 	link->cnt = cnt;
3062 	link->flags = flags;
3063 
3064 	if (cookies) {
3065 		/*
3066 		 * Sorting addresses will trigger sorting cookies as well
3067 		 * (check bpf_kprobe_multi_cookie_swap). This way we can
3068 		 * find cookie based on the address in bpf_get_attach_cookie
3069 		 * helper.
3070 		 */
3071 		sort_r(addrs, cnt, sizeof(*addrs),
3072 		       bpf_kprobe_multi_cookie_cmp,
3073 		       bpf_kprobe_multi_cookie_swap,
3074 		       link);
3075 	}
3076 
3077 	err = get_modules_for_addrs(&link->mods, addrs, cnt);
3078 	if (err < 0) {
3079 		bpf_link_cleanup(&link_primer);
3080 		return err;
3081 	}
3082 	link->mods_cnt = err;
3083 
3084 	err = register_fprobe_ips(&link->fp, addrs, cnt);
3085 	if (err) {
3086 		kprobe_multi_put_modules(link->mods, link->mods_cnt);
3087 		bpf_link_cleanup(&link_primer);
3088 		return err;
3089 	}
3090 
3091 	return bpf_link_settle(&link_primer);
3092 
3093 error:
3094 	kfree(link);
3095 	kvfree(addrs);
3096 	kvfree(cookies);
3097 	return err;
3098 }
3099 #else /* !CONFIG_FPROBE */
3100 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3101 {
3102 	return -EOPNOTSUPP;
3103 }
3104 static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)
3105 {
3106 	return 0;
3107 }
3108 static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3109 {
3110 	return 0;
3111 }
3112 #endif
3113 
3114 #ifdef CONFIG_UPROBES
3115 struct bpf_uprobe_multi_link;
3116 
3117 struct bpf_uprobe {
3118 	struct bpf_uprobe_multi_link *link;
3119 	loff_t offset;
3120 	unsigned long ref_ctr_offset;
3121 	u64 cookie;
3122 	struct uprobe *uprobe;
3123 	struct uprobe_consumer consumer;
3124 	bool session;
3125 };
3126 
3127 struct bpf_uprobe_multi_link {
3128 	struct path path;
3129 	struct bpf_link link;
3130 	u32 cnt;
3131 	u32 flags;
3132 	struct bpf_uprobe *uprobes;
3133 	struct task_struct *task;
3134 };
3135 
3136 struct bpf_uprobe_multi_run_ctx {
3137 	struct bpf_session_run_ctx session_ctx;
3138 	unsigned long entry_ip;
3139 	struct bpf_uprobe *uprobe;
3140 };
3141 
3142 static void bpf_uprobe_unregister(struct bpf_uprobe *uprobes, u32 cnt)
3143 {
3144 	u32 i;
3145 
3146 	for (i = 0; i < cnt; i++)
3147 		uprobe_unregister_nosync(uprobes[i].uprobe, &uprobes[i].consumer);
3148 
3149 	if (cnt)
3150 		uprobe_unregister_sync();
3151 }
3152 
3153 static void bpf_uprobe_multi_link_release(struct bpf_link *link)
3154 {
3155 	struct bpf_uprobe_multi_link *umulti_link;
3156 
3157 	umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3158 	bpf_uprobe_unregister(umulti_link->uprobes, umulti_link->cnt);
3159 	if (umulti_link->task)
3160 		put_task_struct(umulti_link->task);
3161 	path_put(&umulti_link->path);
3162 }
3163 
3164 static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link)
3165 {
3166 	struct bpf_uprobe_multi_link *umulti_link;
3167 
3168 	umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3169 	kvfree(umulti_link->uprobes);
3170 	kfree(umulti_link);
3171 }
3172 
3173 static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link,
3174 						struct bpf_link_info *info)
3175 {
3176 	u64 __user *uref_ctr_offsets = u64_to_user_ptr(info->uprobe_multi.ref_ctr_offsets);
3177 	u64 __user *ucookies = u64_to_user_ptr(info->uprobe_multi.cookies);
3178 	u64 __user *uoffsets = u64_to_user_ptr(info->uprobe_multi.offsets);
3179 	u64 __user *upath = u64_to_user_ptr(info->uprobe_multi.path);
3180 	u32 upath_size = info->uprobe_multi.path_size;
3181 	struct bpf_uprobe_multi_link *umulti_link;
3182 	u32 ucount = info->uprobe_multi.count;
3183 	int err = 0, i;
3184 	char *p, *buf;
3185 	long left = 0;
3186 
3187 	if (!upath ^ !upath_size)
3188 		return -EINVAL;
3189 
3190 	if ((uoffsets || uref_ctr_offsets || ucookies) && !ucount)
3191 		return -EINVAL;
3192 
3193 	umulti_link = container_of(link, struct bpf_uprobe_multi_link, link);
3194 	info->uprobe_multi.count = umulti_link->cnt;
3195 	info->uprobe_multi.flags = umulti_link->flags;
3196 	info->uprobe_multi.pid = umulti_link->task ?
3197 				 task_pid_nr_ns(umulti_link->task, task_active_pid_ns(current)) : 0;
3198 
3199 	upath_size = upath_size ? min_t(u32, upath_size, PATH_MAX) : PATH_MAX;
3200 	buf = kmalloc(upath_size, GFP_KERNEL);
3201 	if (!buf)
3202 		return -ENOMEM;
3203 	p = d_path(&umulti_link->path, buf, upath_size);
3204 	if (IS_ERR(p)) {
3205 		kfree(buf);
3206 		return PTR_ERR(p);
3207 	}
3208 	upath_size = buf + upath_size - p;
3209 
3210 	if (upath)
3211 		left = copy_to_user(upath, p, upath_size);
3212 	kfree(buf);
3213 	if (left)
3214 		return -EFAULT;
3215 	info->uprobe_multi.path_size = upath_size;
3216 
3217 	if (!uoffsets && !ucookies && !uref_ctr_offsets)
3218 		return 0;
3219 
3220 	if (ucount < umulti_link->cnt)
3221 		err = -ENOSPC;
3222 	else
3223 		ucount = umulti_link->cnt;
3224 
3225 	for (i = 0; i < ucount; i++) {
3226 		if (uoffsets &&
3227 		    put_user(umulti_link->uprobes[i].offset, uoffsets + i))
3228 			return -EFAULT;
3229 		if (uref_ctr_offsets &&
3230 		    put_user(umulti_link->uprobes[i].ref_ctr_offset, uref_ctr_offsets + i))
3231 			return -EFAULT;
3232 		if (ucookies &&
3233 		    put_user(umulti_link->uprobes[i].cookie, ucookies + i))
3234 			return -EFAULT;
3235 	}
3236 
3237 	return err;
3238 }
3239 
3240 static const struct bpf_link_ops bpf_uprobe_multi_link_lops = {
3241 	.release = bpf_uprobe_multi_link_release,
3242 	.dealloc_deferred = bpf_uprobe_multi_link_dealloc,
3243 	.fill_link_info = bpf_uprobe_multi_link_fill_link_info,
3244 };
3245 
3246 static int uprobe_prog_run(struct bpf_uprobe *uprobe,
3247 			   unsigned long entry_ip,
3248 			   struct pt_regs *regs,
3249 			   bool is_return, void *data)
3250 {
3251 	struct bpf_uprobe_multi_link *link = uprobe->link;
3252 	struct bpf_uprobe_multi_run_ctx run_ctx = {
3253 		.session_ctx = {
3254 			.is_return = is_return,
3255 			.data = data,
3256 		},
3257 		.entry_ip = entry_ip,
3258 		.uprobe = uprobe,
3259 	};
3260 	struct bpf_prog *prog = link->link.prog;
3261 	bool sleepable = prog->sleepable;
3262 	struct bpf_run_ctx *old_run_ctx;
3263 	int err;
3264 
3265 	if (link->task && !same_thread_group(current, link->task))
3266 		return 0;
3267 
3268 	if (sleepable)
3269 		rcu_read_lock_trace();
3270 	else
3271 		rcu_read_lock();
3272 
3273 	migrate_disable();
3274 
3275 	old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
3276 	err = bpf_prog_run(link->link.prog, regs);
3277 	bpf_reset_run_ctx(old_run_ctx);
3278 
3279 	migrate_enable();
3280 
3281 	if (sleepable)
3282 		rcu_read_unlock_trace();
3283 	else
3284 		rcu_read_unlock();
3285 	return err;
3286 }
3287 
3288 static bool
3289 uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm)
3290 {
3291 	struct bpf_uprobe *uprobe;
3292 
3293 	uprobe = container_of(con, struct bpf_uprobe, consumer);
3294 	return uprobe->link->task->mm == mm;
3295 }
3296 
3297 static int
3298 uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs,
3299 			  __u64 *data)
3300 {
3301 	struct bpf_uprobe *uprobe;
3302 	int ret;
3303 
3304 	uprobe = container_of(con, struct bpf_uprobe, consumer);
3305 	ret = uprobe_prog_run(uprobe, instruction_pointer(regs), regs, false, data);
3306 	if (uprobe->session)
3307 		return ret ? UPROBE_HANDLER_IGNORE : 0;
3308 	return 0;
3309 }
3310 
3311 static int
3312 uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs,
3313 			      __u64 *data)
3314 {
3315 	struct bpf_uprobe *uprobe;
3316 
3317 	uprobe = container_of(con, struct bpf_uprobe, consumer);
3318 	uprobe_prog_run(uprobe, func, regs, true, data);
3319 	return 0;
3320 }
3321 
3322 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3323 {
3324 	struct bpf_uprobe_multi_run_ctx *run_ctx;
3325 
3326 	run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx,
3327 			       session_ctx.run_ctx);
3328 	return run_ctx->entry_ip;
3329 }
3330 
3331 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3332 {
3333 	struct bpf_uprobe_multi_run_ctx *run_ctx;
3334 
3335 	run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx,
3336 			       session_ctx.run_ctx);
3337 	return run_ctx->uprobe->cookie;
3338 }
3339 
3340 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3341 {
3342 	struct bpf_uprobe_multi_link *link = NULL;
3343 	unsigned long __user *uref_ctr_offsets;
3344 	struct bpf_link_primer link_primer;
3345 	struct bpf_uprobe *uprobes = NULL;
3346 	struct task_struct *task = NULL;
3347 	unsigned long __user *uoffsets;
3348 	u64 __user *ucookies;
3349 	void __user *upath;
3350 	u32 flags, cnt, i;
3351 	struct path path;
3352 	char *name;
3353 	pid_t pid;
3354 	int err;
3355 
3356 	/* no support for 32bit archs yet */
3357 	if (sizeof(u64) != sizeof(void *))
3358 		return -EOPNOTSUPP;
3359 
3360 	if (!is_uprobe_multi(prog))
3361 		return -EINVAL;
3362 
3363 	flags = attr->link_create.uprobe_multi.flags;
3364 	if (flags & ~BPF_F_UPROBE_MULTI_RETURN)
3365 		return -EINVAL;
3366 
3367 	/*
3368 	 * path, offsets and cnt are mandatory,
3369 	 * ref_ctr_offsets and cookies are optional
3370 	 */
3371 	upath = u64_to_user_ptr(attr->link_create.uprobe_multi.path);
3372 	uoffsets = u64_to_user_ptr(attr->link_create.uprobe_multi.offsets);
3373 	cnt = attr->link_create.uprobe_multi.cnt;
3374 	pid = attr->link_create.uprobe_multi.pid;
3375 
3376 	if (!upath || !uoffsets || !cnt || pid < 0)
3377 		return -EINVAL;
3378 	if (cnt > MAX_UPROBE_MULTI_CNT)
3379 		return -E2BIG;
3380 
3381 	uref_ctr_offsets = u64_to_user_ptr(attr->link_create.uprobe_multi.ref_ctr_offsets);
3382 	ucookies = u64_to_user_ptr(attr->link_create.uprobe_multi.cookies);
3383 
3384 	name = strndup_user(upath, PATH_MAX);
3385 	if (IS_ERR(name)) {
3386 		err = PTR_ERR(name);
3387 		return err;
3388 	}
3389 
3390 	err = kern_path(name, LOOKUP_FOLLOW, &path);
3391 	kfree(name);
3392 	if (err)
3393 		return err;
3394 
3395 	if (!d_is_reg(path.dentry)) {
3396 		err = -EBADF;
3397 		goto error_path_put;
3398 	}
3399 
3400 	if (pid) {
3401 		task = get_pid_task(find_vpid(pid), PIDTYPE_TGID);
3402 		if (!task) {
3403 			err = -ESRCH;
3404 			goto error_path_put;
3405 		}
3406 	}
3407 
3408 	err = -ENOMEM;
3409 
3410 	link = kzalloc(sizeof(*link), GFP_KERNEL);
3411 	uprobes = kvcalloc(cnt, sizeof(*uprobes), GFP_KERNEL);
3412 
3413 	if (!uprobes || !link)
3414 		goto error_free;
3415 
3416 	for (i = 0; i < cnt; i++) {
3417 		if (__get_user(uprobes[i].offset, uoffsets + i)) {
3418 			err = -EFAULT;
3419 			goto error_free;
3420 		}
3421 		if (uprobes[i].offset < 0) {
3422 			err = -EINVAL;
3423 			goto error_free;
3424 		}
3425 		if (uref_ctr_offsets && __get_user(uprobes[i].ref_ctr_offset, uref_ctr_offsets + i)) {
3426 			err = -EFAULT;
3427 			goto error_free;
3428 		}
3429 		if (ucookies && __get_user(uprobes[i].cookie, ucookies + i)) {
3430 			err = -EFAULT;
3431 			goto error_free;
3432 		}
3433 
3434 		uprobes[i].link = link;
3435 
3436 		if (!(flags & BPF_F_UPROBE_MULTI_RETURN))
3437 			uprobes[i].consumer.handler = uprobe_multi_link_handler;
3438 		if (flags & BPF_F_UPROBE_MULTI_RETURN || is_uprobe_session(prog))
3439 			uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler;
3440 		if (is_uprobe_session(prog))
3441 			uprobes[i].session = true;
3442 		if (pid)
3443 			uprobes[i].consumer.filter = uprobe_multi_link_filter;
3444 	}
3445 
3446 	link->cnt = cnt;
3447 	link->uprobes = uprobes;
3448 	link->path = path;
3449 	link->task = task;
3450 	link->flags = flags;
3451 
3452 	bpf_link_init(&link->link, BPF_LINK_TYPE_UPROBE_MULTI,
3453 		      &bpf_uprobe_multi_link_lops, prog);
3454 
3455 	for (i = 0; i < cnt; i++) {
3456 		uprobes[i].uprobe = uprobe_register(d_real_inode(link->path.dentry),
3457 						    uprobes[i].offset,
3458 						    uprobes[i].ref_ctr_offset,
3459 						    &uprobes[i].consumer);
3460 		if (IS_ERR(uprobes[i].uprobe)) {
3461 			err = PTR_ERR(uprobes[i].uprobe);
3462 			link->cnt = i;
3463 			goto error_unregister;
3464 		}
3465 	}
3466 
3467 	err = bpf_link_prime(&link->link, &link_primer);
3468 	if (err)
3469 		goto error_unregister;
3470 
3471 	return bpf_link_settle(&link_primer);
3472 
3473 error_unregister:
3474 	bpf_uprobe_unregister(uprobes, link->cnt);
3475 
3476 error_free:
3477 	kvfree(uprobes);
3478 	kfree(link);
3479 	if (task)
3480 		put_task_struct(task);
3481 error_path_put:
3482 	path_put(&path);
3483 	return err;
3484 }
3485 #else /* !CONFIG_UPROBES */
3486 int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3487 {
3488 	return -EOPNOTSUPP;
3489 }
3490 static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx)
3491 {
3492 	return 0;
3493 }
3494 static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
3495 {
3496 	return 0;
3497 }
3498 #endif /* CONFIG_UPROBES */
3499 
3500 __bpf_kfunc_start_defs();
3501 
3502 __bpf_kfunc bool bpf_session_is_return(void)
3503 {
3504 	struct bpf_session_run_ctx *session_ctx;
3505 
3506 	session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx);
3507 	return session_ctx->is_return;
3508 }
3509 
3510 __bpf_kfunc __u64 *bpf_session_cookie(void)
3511 {
3512 	struct bpf_session_run_ctx *session_ctx;
3513 
3514 	session_ctx = container_of(current->bpf_ctx, struct bpf_session_run_ctx, run_ctx);
3515 	return session_ctx->data;
3516 }
3517 
3518 __bpf_kfunc_end_defs();
3519 
3520 BTF_KFUNCS_START(kprobe_multi_kfunc_set_ids)
3521 BTF_ID_FLAGS(func, bpf_session_is_return)
3522 BTF_ID_FLAGS(func, bpf_session_cookie)
3523 BTF_KFUNCS_END(kprobe_multi_kfunc_set_ids)
3524 
3525 static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id)
3526 {
3527 	if (!btf_id_set8_contains(&kprobe_multi_kfunc_set_ids, kfunc_id))
3528 		return 0;
3529 
3530 	if (!is_kprobe_session(prog) && !is_uprobe_session(prog))
3531 		return -EACCES;
3532 
3533 	return 0;
3534 }
3535 
3536 static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = {
3537 	.owner = THIS_MODULE,
3538 	.set = &kprobe_multi_kfunc_set_ids,
3539 	.filter = bpf_kprobe_multi_filter,
3540 };
3541 
3542 static int __init bpf_kprobe_multi_kfuncs_init(void)
3543 {
3544 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_kfunc_set);
3545 }
3546 
3547 late_initcall(bpf_kprobe_multi_kfuncs_init);
3548 
3549 __bpf_kfunc_start_defs();
3550 
3551 __bpf_kfunc int bpf_send_signal_task(struct task_struct *task, int sig, enum pid_type type,
3552 				     u64 value)
3553 {
3554 	if (type != PIDTYPE_PID && type != PIDTYPE_TGID)
3555 		return -EINVAL;
3556 
3557 	return bpf_send_signal_common(sig, type, task, value);
3558 }
3559 
3560 __bpf_kfunc_end_defs();
3561