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