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