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