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/filter.h> 11 #include <linux/uaccess.h> 12 #include <linux/ctype.h> 13 #include <linux/kprobes.h> 14 #include <linux/syscalls.h> 15 #include <linux/error-injection.h> 16 17 #include <asm/tlb.h> 18 19 #include "trace_probe.h" 20 #include "trace.h" 21 22 #define bpf_event_rcu_dereference(p) \ 23 rcu_dereference_protected(p, lockdep_is_held(&bpf_event_mutex)) 24 25 #ifdef CONFIG_MODULES 26 struct bpf_trace_module { 27 struct module *module; 28 struct list_head list; 29 }; 30 31 static LIST_HEAD(bpf_trace_modules); 32 static DEFINE_MUTEX(bpf_module_mutex); 33 34 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) 35 { 36 struct bpf_raw_event_map *btp, *ret = NULL; 37 struct bpf_trace_module *btm; 38 unsigned int i; 39 40 mutex_lock(&bpf_module_mutex); 41 list_for_each_entry(btm, &bpf_trace_modules, list) { 42 for (i = 0; i < btm->module->num_bpf_raw_events; ++i) { 43 btp = &btm->module->bpf_raw_events[i]; 44 if (!strcmp(btp->tp->name, name)) { 45 if (try_module_get(btm->module)) 46 ret = btp; 47 goto out; 48 } 49 } 50 } 51 out: 52 mutex_unlock(&bpf_module_mutex); 53 return ret; 54 } 55 #else 56 static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name) 57 { 58 return NULL; 59 } 60 #endif /* CONFIG_MODULES */ 61 62 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 63 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5); 64 65 /** 66 * trace_call_bpf - invoke BPF program 67 * @call: tracepoint event 68 * @ctx: opaque context pointer 69 * 70 * kprobe handlers execute BPF programs via this helper. 71 * Can be used from static tracepoints in the future. 72 * 73 * Return: BPF programs always return an integer which is interpreted by 74 * kprobe handler as: 75 * 0 - return from kprobe (event is filtered out) 76 * 1 - store kprobe event into ring buffer 77 * Other values are reserved and currently alias to 1 78 */ 79 unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx) 80 { 81 unsigned int ret; 82 83 if (in_nmi()) /* not supported yet */ 84 return 1; 85 86 preempt_disable(); 87 88 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { 89 /* 90 * since some bpf program is already running on this cpu, 91 * don't call into another bpf program (same or different) 92 * and don't send kprobe event into ring-buffer, 93 * so return zero here 94 */ 95 ret = 0; 96 goto out; 97 } 98 99 /* 100 * Instead of moving rcu_read_lock/rcu_dereference/rcu_read_unlock 101 * to all call sites, we did a bpf_prog_array_valid() there to check 102 * whether call->prog_array is empty or not, which is 103 * a heurisitc to speed up execution. 104 * 105 * If bpf_prog_array_valid() fetched prog_array was 106 * non-NULL, we go into trace_call_bpf() and do the actual 107 * proper rcu_dereference() under RCU lock. 108 * If it turns out that prog_array is NULL then, we bail out. 109 * For the opposite, if the bpf_prog_array_valid() fetched pointer 110 * was NULL, you'll skip the prog_array with the risk of missing 111 * out of events when it was updated in between this and the 112 * rcu_dereference() which is accepted risk. 113 */ 114 ret = BPF_PROG_RUN_ARRAY_CHECK(call->prog_array, ctx, BPF_PROG_RUN); 115 116 out: 117 __this_cpu_dec(bpf_prog_active); 118 preempt_enable(); 119 120 return ret; 121 } 122 EXPORT_SYMBOL_GPL(trace_call_bpf); 123 124 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 125 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc) 126 { 127 regs_set_return_value(regs, rc); 128 override_function_with_return(regs); 129 return 0; 130 } 131 132 static const struct bpf_func_proto bpf_override_return_proto = { 133 .func = bpf_override_return, 134 .gpl_only = true, 135 .ret_type = RET_INTEGER, 136 .arg1_type = ARG_PTR_TO_CTX, 137 .arg2_type = ARG_ANYTHING, 138 }; 139 #endif 140 141 BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr) 142 { 143 int ret; 144 145 ret = probe_kernel_read(dst, unsafe_ptr, size); 146 if (unlikely(ret < 0)) 147 memset(dst, 0, size); 148 149 return ret; 150 } 151 152 static const struct bpf_func_proto bpf_probe_read_proto = { 153 .func = bpf_probe_read, 154 .gpl_only = true, 155 .ret_type = RET_INTEGER, 156 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 157 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 158 .arg3_type = ARG_ANYTHING, 159 }; 160 161 BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, 162 u32, size) 163 { 164 /* 165 * Ensure we're in user context which is safe for the helper to 166 * run. This helper has no business in a kthread. 167 * 168 * access_ok() should prevent writing to non-user memory, but in 169 * some situations (nommu, temporary switch, etc) access_ok() does 170 * not provide enough validation, hence the check on KERNEL_DS. 171 * 172 * nmi_uaccess_okay() ensures the probe is not run in an interim 173 * state, when the task or mm are switched. This is specifically 174 * required to prevent the use of temporary mm. 175 */ 176 177 if (unlikely(in_interrupt() || 178 current->flags & (PF_KTHREAD | PF_EXITING))) 179 return -EPERM; 180 if (unlikely(uaccess_kernel())) 181 return -EPERM; 182 if (unlikely(!nmi_uaccess_okay())) 183 return -EPERM; 184 if (!access_ok(unsafe_ptr, size)) 185 return -EPERM; 186 187 return probe_kernel_write(unsafe_ptr, src, size); 188 } 189 190 static const struct bpf_func_proto bpf_probe_write_user_proto = { 191 .func = bpf_probe_write_user, 192 .gpl_only = true, 193 .ret_type = RET_INTEGER, 194 .arg1_type = ARG_ANYTHING, 195 .arg2_type = ARG_PTR_TO_MEM, 196 .arg3_type = ARG_CONST_SIZE, 197 }; 198 199 static const struct bpf_func_proto *bpf_get_probe_write_proto(void) 200 { 201 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!", 202 current->comm, task_pid_nr(current)); 203 204 return &bpf_probe_write_user_proto; 205 } 206 207 /* 208 * Only limited trace_printk() conversion specifiers allowed: 209 * %d %i %u %x %ld %li %lu %lx %lld %lli %llu %llx %p %s 210 */ 211 BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, 212 u64, arg2, u64, arg3) 213 { 214 bool str_seen = false; 215 int mod[3] = {}; 216 int fmt_cnt = 0; 217 u64 unsafe_addr; 218 char buf[64]; 219 int i; 220 221 /* 222 * bpf_check()->check_func_arg()->check_stack_boundary() 223 * guarantees that fmt points to bpf program stack, 224 * fmt_size bytes of it were initialized and fmt_size > 0 225 */ 226 if (fmt[--fmt_size] != 0) 227 return -EINVAL; 228 229 /* check format string for allowed specifiers */ 230 for (i = 0; i < fmt_size; i++) { 231 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) 232 return -EINVAL; 233 234 if (fmt[i] != '%') 235 continue; 236 237 if (fmt_cnt >= 3) 238 return -EINVAL; 239 240 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */ 241 i++; 242 if (fmt[i] == 'l') { 243 mod[fmt_cnt]++; 244 i++; 245 } else if (fmt[i] == 'p' || fmt[i] == 's') { 246 mod[fmt_cnt]++; 247 /* disallow any further format extensions */ 248 if (fmt[i + 1] != 0 && 249 !isspace(fmt[i + 1]) && 250 !ispunct(fmt[i + 1])) 251 return -EINVAL; 252 fmt_cnt++; 253 if (fmt[i] == 's') { 254 if (str_seen) 255 /* allow only one '%s' per fmt string */ 256 return -EINVAL; 257 str_seen = true; 258 259 switch (fmt_cnt) { 260 case 1: 261 unsafe_addr = arg1; 262 arg1 = (long) buf; 263 break; 264 case 2: 265 unsafe_addr = arg2; 266 arg2 = (long) buf; 267 break; 268 case 3: 269 unsafe_addr = arg3; 270 arg3 = (long) buf; 271 break; 272 } 273 buf[0] = 0; 274 strncpy_from_unsafe(buf, 275 (void *) (long) unsafe_addr, 276 sizeof(buf)); 277 } 278 continue; 279 } 280 281 if (fmt[i] == 'l') { 282 mod[fmt_cnt]++; 283 i++; 284 } 285 286 if (fmt[i] != 'i' && fmt[i] != 'd' && 287 fmt[i] != 'u' && fmt[i] != 'x') 288 return -EINVAL; 289 fmt_cnt++; 290 } 291 292 /* Horrid workaround for getting va_list handling working with different 293 * argument type combinations generically for 32 and 64 bit archs. 294 */ 295 #define __BPF_TP_EMIT() __BPF_ARG3_TP() 296 #define __BPF_TP(...) \ 297 __trace_printk(0 /* Fake ip */, \ 298 fmt, ##__VA_ARGS__) 299 300 #define __BPF_ARG1_TP(...) \ 301 ((mod[0] == 2 || (mod[0] == 1 && __BITS_PER_LONG == 64)) \ 302 ? __BPF_TP(arg1, ##__VA_ARGS__) \ 303 : ((mod[0] == 1 || (mod[0] == 0 && __BITS_PER_LONG == 32)) \ 304 ? __BPF_TP((long)arg1, ##__VA_ARGS__) \ 305 : __BPF_TP((u32)arg1, ##__VA_ARGS__))) 306 307 #define __BPF_ARG2_TP(...) \ 308 ((mod[1] == 2 || (mod[1] == 1 && __BITS_PER_LONG == 64)) \ 309 ? __BPF_ARG1_TP(arg2, ##__VA_ARGS__) \ 310 : ((mod[1] == 1 || (mod[1] == 0 && __BITS_PER_LONG == 32)) \ 311 ? __BPF_ARG1_TP((long)arg2, ##__VA_ARGS__) \ 312 : __BPF_ARG1_TP((u32)arg2, ##__VA_ARGS__))) 313 314 #define __BPF_ARG3_TP(...) \ 315 ((mod[2] == 2 || (mod[2] == 1 && __BITS_PER_LONG == 64)) \ 316 ? __BPF_ARG2_TP(arg3, ##__VA_ARGS__) \ 317 : ((mod[2] == 1 || (mod[2] == 0 && __BITS_PER_LONG == 32)) \ 318 ? __BPF_ARG2_TP((long)arg3, ##__VA_ARGS__) \ 319 : __BPF_ARG2_TP((u32)arg3, ##__VA_ARGS__))) 320 321 return __BPF_TP_EMIT(); 322 } 323 324 static const struct bpf_func_proto bpf_trace_printk_proto = { 325 .func = bpf_trace_printk, 326 .gpl_only = true, 327 .ret_type = RET_INTEGER, 328 .arg1_type = ARG_PTR_TO_MEM, 329 .arg2_type = ARG_CONST_SIZE, 330 }; 331 332 const struct bpf_func_proto *bpf_get_trace_printk_proto(void) 333 { 334 /* 335 * this program might be calling bpf_trace_printk, 336 * so allocate per-cpu printk buffers 337 */ 338 trace_printk_init_buffers(); 339 340 return &bpf_trace_printk_proto; 341 } 342 343 static __always_inline int 344 get_map_perf_counter(struct bpf_map *map, u64 flags, 345 u64 *value, u64 *enabled, u64 *running) 346 { 347 struct bpf_array *array = container_of(map, struct bpf_array, map); 348 unsigned int cpu = smp_processor_id(); 349 u64 index = flags & BPF_F_INDEX_MASK; 350 struct bpf_event_entry *ee; 351 352 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) 353 return -EINVAL; 354 if (index == BPF_F_CURRENT_CPU) 355 index = cpu; 356 if (unlikely(index >= array->map.max_entries)) 357 return -E2BIG; 358 359 ee = READ_ONCE(array->ptrs[index]); 360 if (!ee) 361 return -ENOENT; 362 363 return perf_event_read_local(ee->event, value, enabled, running); 364 } 365 366 BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags) 367 { 368 u64 value = 0; 369 int err; 370 371 err = get_map_perf_counter(map, flags, &value, NULL, NULL); 372 /* 373 * this api is ugly since we miss [-22..-2] range of valid 374 * counter values, but that's uapi 375 */ 376 if (err) 377 return err; 378 return value; 379 } 380 381 static const struct bpf_func_proto bpf_perf_event_read_proto = { 382 .func = bpf_perf_event_read, 383 .gpl_only = true, 384 .ret_type = RET_INTEGER, 385 .arg1_type = ARG_CONST_MAP_PTR, 386 .arg2_type = ARG_ANYTHING, 387 }; 388 389 BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags, 390 struct bpf_perf_event_value *, buf, u32, size) 391 { 392 int err = -EINVAL; 393 394 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 395 goto clear; 396 err = get_map_perf_counter(map, flags, &buf->counter, &buf->enabled, 397 &buf->running); 398 if (unlikely(err)) 399 goto clear; 400 return 0; 401 clear: 402 memset(buf, 0, size); 403 return err; 404 } 405 406 static const struct bpf_func_proto bpf_perf_event_read_value_proto = { 407 .func = bpf_perf_event_read_value, 408 .gpl_only = true, 409 .ret_type = RET_INTEGER, 410 .arg1_type = ARG_CONST_MAP_PTR, 411 .arg2_type = ARG_ANYTHING, 412 .arg3_type = ARG_PTR_TO_UNINIT_MEM, 413 .arg4_type = ARG_CONST_SIZE, 414 }; 415 416 static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd); 417 418 static __always_inline u64 419 __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, 420 u64 flags, struct perf_sample_data *sd) 421 { 422 struct bpf_array *array = container_of(map, struct bpf_array, map); 423 unsigned int cpu = smp_processor_id(); 424 u64 index = flags & BPF_F_INDEX_MASK; 425 struct bpf_event_entry *ee; 426 struct perf_event *event; 427 428 if (index == BPF_F_CURRENT_CPU) 429 index = cpu; 430 if (unlikely(index >= array->map.max_entries)) 431 return -E2BIG; 432 433 ee = READ_ONCE(array->ptrs[index]); 434 if (!ee) 435 return -ENOENT; 436 437 event = ee->event; 438 if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || 439 event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) 440 return -EINVAL; 441 442 if (unlikely(event->oncpu != cpu)) 443 return -EOPNOTSUPP; 444 445 return perf_event_output(event, sd, regs); 446 } 447 448 BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, 449 u64, flags, void *, data, u64, size) 450 { 451 struct perf_sample_data *sd = this_cpu_ptr(&bpf_trace_sd); 452 struct perf_raw_record raw = { 453 .frag = { 454 .size = size, 455 .data = data, 456 }, 457 }; 458 459 if (unlikely(flags & ~(BPF_F_INDEX_MASK))) 460 return -EINVAL; 461 462 perf_sample_data_init(sd, 0, 0); 463 sd->raw = &raw; 464 465 return __bpf_perf_event_output(regs, map, flags, sd); 466 } 467 468 static const struct bpf_func_proto bpf_perf_event_output_proto = { 469 .func = bpf_perf_event_output, 470 .gpl_only = true, 471 .ret_type = RET_INTEGER, 472 .arg1_type = ARG_PTR_TO_CTX, 473 .arg2_type = ARG_CONST_MAP_PTR, 474 .arg3_type = ARG_ANYTHING, 475 .arg4_type = ARG_PTR_TO_MEM, 476 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 477 }; 478 479 static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs); 480 static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd); 481 482 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, 483 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy) 484 { 485 struct perf_sample_data *sd = this_cpu_ptr(&bpf_misc_sd); 486 struct pt_regs *regs = this_cpu_ptr(&bpf_pt_regs); 487 struct perf_raw_frag frag = { 488 .copy = ctx_copy, 489 .size = ctx_size, 490 .data = ctx, 491 }; 492 struct perf_raw_record raw = { 493 .frag = { 494 { 495 .next = ctx_size ? &frag : NULL, 496 }, 497 .size = meta_size, 498 .data = meta, 499 }, 500 }; 501 502 perf_fetch_caller_regs(regs); 503 perf_sample_data_init(sd, 0, 0); 504 sd->raw = &raw; 505 506 return __bpf_perf_event_output(regs, map, flags, sd); 507 } 508 509 BPF_CALL_0(bpf_get_current_task) 510 { 511 return (long) current; 512 } 513 514 static const struct bpf_func_proto bpf_get_current_task_proto = { 515 .func = bpf_get_current_task, 516 .gpl_only = true, 517 .ret_type = RET_INTEGER, 518 }; 519 520 BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) 521 { 522 struct bpf_array *array = container_of(map, struct bpf_array, map); 523 struct cgroup *cgrp; 524 525 if (unlikely(idx >= array->map.max_entries)) 526 return -E2BIG; 527 528 cgrp = READ_ONCE(array->ptrs[idx]); 529 if (unlikely(!cgrp)) 530 return -EAGAIN; 531 532 return task_under_cgroup_hierarchy(current, cgrp); 533 } 534 535 static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { 536 .func = bpf_current_task_under_cgroup, 537 .gpl_only = false, 538 .ret_type = RET_INTEGER, 539 .arg1_type = ARG_CONST_MAP_PTR, 540 .arg2_type = ARG_ANYTHING, 541 }; 542 543 BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, 544 const void *, unsafe_ptr) 545 { 546 int ret; 547 548 /* 549 * The strncpy_from_unsafe() call will likely not fill the entire 550 * buffer, but that's okay in this circumstance as we're probing 551 * arbitrary memory anyway similar to bpf_probe_read() and might 552 * as well probe the stack. Thus, memory is explicitly cleared 553 * only in error case, so that improper users ignoring return 554 * code altogether don't copy garbage; otherwise length of string 555 * is returned that can be used for bpf_perf_event_output() et al. 556 */ 557 ret = strncpy_from_unsafe(dst, unsafe_ptr, size); 558 if (unlikely(ret < 0)) 559 memset(dst, 0, size); 560 561 return ret; 562 } 563 564 static const struct bpf_func_proto bpf_probe_read_str_proto = { 565 .func = bpf_probe_read_str, 566 .gpl_only = true, 567 .ret_type = RET_INTEGER, 568 .arg1_type = ARG_PTR_TO_UNINIT_MEM, 569 .arg2_type = ARG_CONST_SIZE_OR_ZERO, 570 .arg3_type = ARG_ANYTHING, 571 }; 572 573 struct send_signal_irq_work { 574 struct irq_work irq_work; 575 struct task_struct *task; 576 u32 sig; 577 }; 578 579 static DEFINE_PER_CPU(struct send_signal_irq_work, send_signal_work); 580 581 static void do_bpf_send_signal(struct irq_work *entry) 582 { 583 struct send_signal_irq_work *work; 584 585 work = container_of(entry, struct send_signal_irq_work, irq_work); 586 group_send_sig_info(work->sig, SEND_SIG_PRIV, work->task, PIDTYPE_TGID); 587 } 588 589 BPF_CALL_1(bpf_send_signal, u32, sig) 590 { 591 struct send_signal_irq_work *work = NULL; 592 593 /* Similar to bpf_probe_write_user, task needs to be 594 * in a sound condition and kernel memory access be 595 * permitted in order to send signal to the current 596 * task. 597 */ 598 if (unlikely(current->flags & (PF_KTHREAD | PF_EXITING))) 599 return -EPERM; 600 if (unlikely(uaccess_kernel())) 601 return -EPERM; 602 if (unlikely(!nmi_uaccess_okay())) 603 return -EPERM; 604 605 if (in_nmi()) { 606 /* Do an early check on signal validity. Otherwise, 607 * the error is lost in deferred irq_work. 608 */ 609 if (unlikely(!valid_signal(sig))) 610 return -EINVAL; 611 612 work = this_cpu_ptr(&send_signal_work); 613 if (work->irq_work.flags & IRQ_WORK_BUSY) 614 return -EBUSY; 615 616 /* Add the current task, which is the target of sending signal, 617 * to the irq_work. The current task may change when queued 618 * irq works get executed. 619 */ 620 work->task = current; 621 work->sig = sig; 622 irq_work_queue(&work->irq_work); 623 return 0; 624 } 625 626 return group_send_sig_info(sig, SEND_SIG_PRIV, current, PIDTYPE_TGID); 627 } 628 629 static const struct bpf_func_proto bpf_send_signal_proto = { 630 .func = bpf_send_signal, 631 .gpl_only = false, 632 .ret_type = RET_INTEGER, 633 .arg1_type = ARG_ANYTHING, 634 }; 635 636 static const struct bpf_func_proto * 637 tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 638 { 639 switch (func_id) { 640 case BPF_FUNC_map_lookup_elem: 641 return &bpf_map_lookup_elem_proto; 642 case BPF_FUNC_map_update_elem: 643 return &bpf_map_update_elem_proto; 644 case BPF_FUNC_map_delete_elem: 645 return &bpf_map_delete_elem_proto; 646 case BPF_FUNC_map_push_elem: 647 return &bpf_map_push_elem_proto; 648 case BPF_FUNC_map_pop_elem: 649 return &bpf_map_pop_elem_proto; 650 case BPF_FUNC_map_peek_elem: 651 return &bpf_map_peek_elem_proto; 652 case BPF_FUNC_probe_read: 653 return &bpf_probe_read_proto; 654 case BPF_FUNC_ktime_get_ns: 655 return &bpf_ktime_get_ns_proto; 656 case BPF_FUNC_tail_call: 657 return &bpf_tail_call_proto; 658 case BPF_FUNC_get_current_pid_tgid: 659 return &bpf_get_current_pid_tgid_proto; 660 case BPF_FUNC_get_current_task: 661 return &bpf_get_current_task_proto; 662 case BPF_FUNC_get_current_uid_gid: 663 return &bpf_get_current_uid_gid_proto; 664 case BPF_FUNC_get_current_comm: 665 return &bpf_get_current_comm_proto; 666 case BPF_FUNC_trace_printk: 667 return bpf_get_trace_printk_proto(); 668 case BPF_FUNC_get_smp_processor_id: 669 return &bpf_get_smp_processor_id_proto; 670 case BPF_FUNC_get_numa_node_id: 671 return &bpf_get_numa_node_id_proto; 672 case BPF_FUNC_perf_event_read: 673 return &bpf_perf_event_read_proto; 674 case BPF_FUNC_probe_write_user: 675 return bpf_get_probe_write_proto(); 676 case BPF_FUNC_current_task_under_cgroup: 677 return &bpf_current_task_under_cgroup_proto; 678 case BPF_FUNC_get_prandom_u32: 679 return &bpf_get_prandom_u32_proto; 680 case BPF_FUNC_probe_read_str: 681 return &bpf_probe_read_str_proto; 682 #ifdef CONFIG_CGROUPS 683 case BPF_FUNC_get_current_cgroup_id: 684 return &bpf_get_current_cgroup_id_proto; 685 #endif 686 case BPF_FUNC_send_signal: 687 return &bpf_send_signal_proto; 688 default: 689 return NULL; 690 } 691 } 692 693 static const struct bpf_func_proto * 694 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 695 { 696 switch (func_id) { 697 case BPF_FUNC_perf_event_output: 698 return &bpf_perf_event_output_proto; 699 case BPF_FUNC_get_stackid: 700 return &bpf_get_stackid_proto; 701 case BPF_FUNC_get_stack: 702 return &bpf_get_stack_proto; 703 case BPF_FUNC_perf_event_read_value: 704 return &bpf_perf_event_read_value_proto; 705 #ifdef CONFIG_BPF_KPROBE_OVERRIDE 706 case BPF_FUNC_override_return: 707 return &bpf_override_return_proto; 708 #endif 709 default: 710 return tracing_func_proto(func_id, prog); 711 } 712 } 713 714 /* bpf+kprobe programs can access fields of 'struct pt_regs' */ 715 static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 716 const struct bpf_prog *prog, 717 struct bpf_insn_access_aux *info) 718 { 719 if (off < 0 || off >= sizeof(struct pt_regs)) 720 return false; 721 if (type != BPF_READ) 722 return false; 723 if (off % size != 0) 724 return false; 725 /* 726 * Assertion for 32 bit to make sure last 8 byte access 727 * (BPF_DW) to the last 4 byte member is disallowed. 728 */ 729 if (off + size > sizeof(struct pt_regs)) 730 return false; 731 732 return true; 733 } 734 735 const struct bpf_verifier_ops kprobe_verifier_ops = { 736 .get_func_proto = kprobe_prog_func_proto, 737 .is_valid_access = kprobe_prog_is_valid_access, 738 }; 739 740 const struct bpf_prog_ops kprobe_prog_ops = { 741 }; 742 743 BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map, 744 u64, flags, void *, data, u64, size) 745 { 746 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 747 748 /* 749 * r1 points to perf tracepoint buffer where first 8 bytes are hidden 750 * from bpf program and contain a pointer to 'struct pt_regs'. Fetch it 751 * from there and call the same bpf_perf_event_output() helper inline. 752 */ 753 return ____bpf_perf_event_output(regs, map, flags, data, size); 754 } 755 756 static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { 757 .func = bpf_perf_event_output_tp, 758 .gpl_only = true, 759 .ret_type = RET_INTEGER, 760 .arg1_type = ARG_PTR_TO_CTX, 761 .arg2_type = ARG_CONST_MAP_PTR, 762 .arg3_type = ARG_ANYTHING, 763 .arg4_type = ARG_PTR_TO_MEM, 764 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 765 }; 766 767 BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, 768 u64, flags) 769 { 770 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 771 772 /* 773 * Same comment as in bpf_perf_event_output_tp(), only that this time 774 * the other helper's function body cannot be inlined due to being 775 * external, thus we need to call raw helper function. 776 */ 777 return bpf_get_stackid((unsigned long) regs, (unsigned long) map, 778 flags, 0, 0); 779 } 780 781 static const struct bpf_func_proto bpf_get_stackid_proto_tp = { 782 .func = bpf_get_stackid_tp, 783 .gpl_only = true, 784 .ret_type = RET_INTEGER, 785 .arg1_type = ARG_PTR_TO_CTX, 786 .arg2_type = ARG_CONST_MAP_PTR, 787 .arg3_type = ARG_ANYTHING, 788 }; 789 790 BPF_CALL_4(bpf_get_stack_tp, void *, tp_buff, void *, buf, u32, size, 791 u64, flags) 792 { 793 struct pt_regs *regs = *(struct pt_regs **)tp_buff; 794 795 return bpf_get_stack((unsigned long) regs, (unsigned long) buf, 796 (unsigned long) size, flags, 0); 797 } 798 799 static const struct bpf_func_proto bpf_get_stack_proto_tp = { 800 .func = bpf_get_stack_tp, 801 .gpl_only = true, 802 .ret_type = RET_INTEGER, 803 .arg1_type = ARG_PTR_TO_CTX, 804 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 805 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 806 .arg4_type = ARG_ANYTHING, 807 }; 808 809 static const struct bpf_func_proto * 810 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 811 { 812 switch (func_id) { 813 case BPF_FUNC_perf_event_output: 814 return &bpf_perf_event_output_proto_tp; 815 case BPF_FUNC_get_stackid: 816 return &bpf_get_stackid_proto_tp; 817 case BPF_FUNC_get_stack: 818 return &bpf_get_stack_proto_tp; 819 default: 820 return tracing_func_proto(func_id, prog); 821 } 822 } 823 824 static bool tp_prog_is_valid_access(int off, int size, enum bpf_access_type type, 825 const struct bpf_prog *prog, 826 struct bpf_insn_access_aux *info) 827 { 828 if (off < sizeof(void *) || off >= PERF_MAX_TRACE_SIZE) 829 return false; 830 if (type != BPF_READ) 831 return false; 832 if (off % size != 0) 833 return false; 834 835 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(__u64)); 836 return true; 837 } 838 839 const struct bpf_verifier_ops tracepoint_verifier_ops = { 840 .get_func_proto = tp_prog_func_proto, 841 .is_valid_access = tp_prog_is_valid_access, 842 }; 843 844 const struct bpf_prog_ops tracepoint_prog_ops = { 845 }; 846 847 BPF_CALL_3(bpf_perf_prog_read_value, struct bpf_perf_event_data_kern *, ctx, 848 struct bpf_perf_event_value *, buf, u32, size) 849 { 850 int err = -EINVAL; 851 852 if (unlikely(size != sizeof(struct bpf_perf_event_value))) 853 goto clear; 854 err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled, 855 &buf->running); 856 if (unlikely(err)) 857 goto clear; 858 return 0; 859 clear: 860 memset(buf, 0, size); 861 return err; 862 } 863 864 static const struct bpf_func_proto bpf_perf_prog_read_value_proto = { 865 .func = bpf_perf_prog_read_value, 866 .gpl_only = true, 867 .ret_type = RET_INTEGER, 868 .arg1_type = ARG_PTR_TO_CTX, 869 .arg2_type = ARG_PTR_TO_UNINIT_MEM, 870 .arg3_type = ARG_CONST_SIZE, 871 }; 872 873 static const struct bpf_func_proto * 874 pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 875 { 876 switch (func_id) { 877 case BPF_FUNC_perf_event_output: 878 return &bpf_perf_event_output_proto_tp; 879 case BPF_FUNC_get_stackid: 880 return &bpf_get_stackid_proto_tp; 881 case BPF_FUNC_get_stack: 882 return &bpf_get_stack_proto_tp; 883 case BPF_FUNC_perf_prog_read_value: 884 return &bpf_perf_prog_read_value_proto; 885 default: 886 return tracing_func_proto(func_id, prog); 887 } 888 } 889 890 /* 891 * bpf_raw_tp_regs are separate from bpf_pt_regs used from skb/xdp 892 * to avoid potential recursive reuse issue when/if tracepoints are added 893 * inside bpf_*_event_output, bpf_get_stackid and/or bpf_get_stack 894 */ 895 static DEFINE_PER_CPU(struct pt_regs, bpf_raw_tp_regs); 896 BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args, 897 struct bpf_map *, map, u64, flags, void *, data, u64, size) 898 { 899 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); 900 901 perf_fetch_caller_regs(regs); 902 return ____bpf_perf_event_output(regs, map, flags, data, size); 903 } 904 905 static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { 906 .func = bpf_perf_event_output_raw_tp, 907 .gpl_only = true, 908 .ret_type = RET_INTEGER, 909 .arg1_type = ARG_PTR_TO_CTX, 910 .arg2_type = ARG_CONST_MAP_PTR, 911 .arg3_type = ARG_ANYTHING, 912 .arg4_type = ARG_PTR_TO_MEM, 913 .arg5_type = ARG_CONST_SIZE_OR_ZERO, 914 }; 915 916 BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args, 917 struct bpf_map *, map, u64, flags) 918 { 919 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); 920 921 perf_fetch_caller_regs(regs); 922 /* similar to bpf_perf_event_output_tp, but pt_regs fetched differently */ 923 return bpf_get_stackid((unsigned long) regs, (unsigned long) map, 924 flags, 0, 0); 925 } 926 927 static const struct bpf_func_proto bpf_get_stackid_proto_raw_tp = { 928 .func = bpf_get_stackid_raw_tp, 929 .gpl_only = true, 930 .ret_type = RET_INTEGER, 931 .arg1_type = ARG_PTR_TO_CTX, 932 .arg2_type = ARG_CONST_MAP_PTR, 933 .arg3_type = ARG_ANYTHING, 934 }; 935 936 BPF_CALL_4(bpf_get_stack_raw_tp, struct bpf_raw_tracepoint_args *, args, 937 void *, buf, u32, size, u64, flags) 938 { 939 struct pt_regs *regs = this_cpu_ptr(&bpf_raw_tp_regs); 940 941 perf_fetch_caller_regs(regs); 942 return bpf_get_stack((unsigned long) regs, (unsigned long) buf, 943 (unsigned long) size, flags, 0); 944 } 945 946 static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { 947 .func = bpf_get_stack_raw_tp, 948 .gpl_only = true, 949 .ret_type = RET_INTEGER, 950 .arg1_type = ARG_PTR_TO_CTX, 951 .arg2_type = ARG_PTR_TO_MEM, 952 .arg3_type = ARG_CONST_SIZE_OR_ZERO, 953 .arg4_type = ARG_ANYTHING, 954 }; 955 956 static const struct bpf_func_proto * 957 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) 958 { 959 switch (func_id) { 960 case BPF_FUNC_perf_event_output: 961 return &bpf_perf_event_output_proto_raw_tp; 962 case BPF_FUNC_get_stackid: 963 return &bpf_get_stackid_proto_raw_tp; 964 case BPF_FUNC_get_stack: 965 return &bpf_get_stack_proto_raw_tp; 966 default: 967 return tracing_func_proto(func_id, prog); 968 } 969 } 970 971 static bool raw_tp_prog_is_valid_access(int off, int size, 972 enum bpf_access_type type, 973 const struct bpf_prog *prog, 974 struct bpf_insn_access_aux *info) 975 { 976 /* largest tracepoint in the kernel has 12 args */ 977 if (off < 0 || off >= sizeof(__u64) * 12) 978 return false; 979 if (type != BPF_READ) 980 return false; 981 if (off % size != 0) 982 return false; 983 return true; 984 } 985 986 const struct bpf_verifier_ops raw_tracepoint_verifier_ops = { 987 .get_func_proto = raw_tp_prog_func_proto, 988 .is_valid_access = raw_tp_prog_is_valid_access, 989 }; 990 991 const struct bpf_prog_ops raw_tracepoint_prog_ops = { 992 }; 993 994 static bool raw_tp_writable_prog_is_valid_access(int off, int size, 995 enum bpf_access_type type, 996 const struct bpf_prog *prog, 997 struct bpf_insn_access_aux *info) 998 { 999 if (off == 0) { 1000 if (size != sizeof(u64) || type != BPF_READ) 1001 return false; 1002 info->reg_type = PTR_TO_TP_BUFFER; 1003 } 1004 return raw_tp_prog_is_valid_access(off, size, type, prog, info); 1005 } 1006 1007 const struct bpf_verifier_ops raw_tracepoint_writable_verifier_ops = { 1008 .get_func_proto = raw_tp_prog_func_proto, 1009 .is_valid_access = raw_tp_writable_prog_is_valid_access, 1010 }; 1011 1012 const struct bpf_prog_ops raw_tracepoint_writable_prog_ops = { 1013 }; 1014 1015 static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type, 1016 const struct bpf_prog *prog, 1017 struct bpf_insn_access_aux *info) 1018 { 1019 const int size_u64 = sizeof(u64); 1020 1021 if (off < 0 || off >= sizeof(struct bpf_perf_event_data)) 1022 return false; 1023 if (type != BPF_READ) 1024 return false; 1025 if (off % size != 0) { 1026 if (sizeof(unsigned long) != 4) 1027 return false; 1028 if (size != 8) 1029 return false; 1030 if (off % size != 4) 1031 return false; 1032 } 1033 1034 switch (off) { 1035 case bpf_ctx_range(struct bpf_perf_event_data, sample_period): 1036 bpf_ctx_record_field_size(info, size_u64); 1037 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1038 return false; 1039 break; 1040 case bpf_ctx_range(struct bpf_perf_event_data, addr): 1041 bpf_ctx_record_field_size(info, size_u64); 1042 if (!bpf_ctx_narrow_access_ok(off, size, size_u64)) 1043 return false; 1044 break; 1045 default: 1046 if (size != sizeof(long)) 1047 return false; 1048 } 1049 1050 return true; 1051 } 1052 1053 static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, 1054 const struct bpf_insn *si, 1055 struct bpf_insn *insn_buf, 1056 struct bpf_prog *prog, u32 *target_size) 1057 { 1058 struct bpf_insn *insn = insn_buf; 1059 1060 switch (si->off) { 1061 case offsetof(struct bpf_perf_event_data, sample_period): 1062 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1063 data), si->dst_reg, si->src_reg, 1064 offsetof(struct bpf_perf_event_data_kern, data)); 1065 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1066 bpf_target_off(struct perf_sample_data, period, 8, 1067 target_size)); 1068 break; 1069 case offsetof(struct bpf_perf_event_data, addr): 1070 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1071 data), si->dst_reg, si->src_reg, 1072 offsetof(struct bpf_perf_event_data_kern, data)); 1073 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg, 1074 bpf_target_off(struct perf_sample_data, addr, 8, 1075 target_size)); 1076 break; 1077 default: 1078 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern, 1079 regs), si->dst_reg, si->src_reg, 1080 offsetof(struct bpf_perf_event_data_kern, regs)); 1081 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(long), si->dst_reg, si->dst_reg, 1082 si->off); 1083 break; 1084 } 1085 1086 return insn - insn_buf; 1087 } 1088 1089 const struct bpf_verifier_ops perf_event_verifier_ops = { 1090 .get_func_proto = pe_prog_func_proto, 1091 .is_valid_access = pe_prog_is_valid_access, 1092 .convert_ctx_access = pe_prog_convert_ctx_access, 1093 }; 1094 1095 const struct bpf_prog_ops perf_event_prog_ops = { 1096 }; 1097 1098 static DEFINE_MUTEX(bpf_event_mutex); 1099 1100 #define BPF_TRACE_MAX_PROGS 64 1101 1102 int perf_event_attach_bpf_prog(struct perf_event *event, 1103 struct bpf_prog *prog) 1104 { 1105 struct bpf_prog_array *old_array; 1106 struct bpf_prog_array *new_array; 1107 int ret = -EEXIST; 1108 1109 /* 1110 * Kprobe override only works if they are on the function entry, 1111 * and only if they are on the opt-in list. 1112 */ 1113 if (prog->kprobe_override && 1114 (!trace_kprobe_on_func_entry(event->tp_event) || 1115 !trace_kprobe_error_injectable(event->tp_event))) 1116 return -EINVAL; 1117 1118 mutex_lock(&bpf_event_mutex); 1119 1120 if (event->prog) 1121 goto unlock; 1122 1123 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1124 if (old_array && 1125 bpf_prog_array_length(old_array) >= BPF_TRACE_MAX_PROGS) { 1126 ret = -E2BIG; 1127 goto unlock; 1128 } 1129 1130 ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array); 1131 if (ret < 0) 1132 goto unlock; 1133 1134 /* set the new array to event->tp_event and set event->prog */ 1135 event->prog = prog; 1136 rcu_assign_pointer(event->tp_event->prog_array, new_array); 1137 bpf_prog_array_free(old_array); 1138 1139 unlock: 1140 mutex_unlock(&bpf_event_mutex); 1141 return ret; 1142 } 1143 1144 void perf_event_detach_bpf_prog(struct perf_event *event) 1145 { 1146 struct bpf_prog_array *old_array; 1147 struct bpf_prog_array *new_array; 1148 int ret; 1149 1150 mutex_lock(&bpf_event_mutex); 1151 1152 if (!event->prog) 1153 goto unlock; 1154 1155 old_array = bpf_event_rcu_dereference(event->tp_event->prog_array); 1156 ret = bpf_prog_array_copy(old_array, event->prog, NULL, &new_array); 1157 if (ret == -ENOENT) 1158 goto unlock; 1159 if (ret < 0) { 1160 bpf_prog_array_delete_safe(old_array, event->prog); 1161 } else { 1162 rcu_assign_pointer(event->tp_event->prog_array, new_array); 1163 bpf_prog_array_free(old_array); 1164 } 1165 1166 bpf_prog_put(event->prog); 1167 event->prog = NULL; 1168 1169 unlock: 1170 mutex_unlock(&bpf_event_mutex); 1171 } 1172 1173 int perf_event_query_prog_array(struct perf_event *event, void __user *info) 1174 { 1175 struct perf_event_query_bpf __user *uquery = info; 1176 struct perf_event_query_bpf query = {}; 1177 struct bpf_prog_array *progs; 1178 u32 *ids, prog_cnt, ids_len; 1179 int ret; 1180 1181 if (!capable(CAP_SYS_ADMIN)) 1182 return -EPERM; 1183 if (event->attr.type != PERF_TYPE_TRACEPOINT) 1184 return -EINVAL; 1185 if (copy_from_user(&query, uquery, sizeof(query))) 1186 return -EFAULT; 1187 1188 ids_len = query.ids_len; 1189 if (ids_len > BPF_TRACE_MAX_PROGS) 1190 return -E2BIG; 1191 ids = kcalloc(ids_len, sizeof(u32), GFP_USER | __GFP_NOWARN); 1192 if (!ids) 1193 return -ENOMEM; 1194 /* 1195 * The above kcalloc returns ZERO_SIZE_PTR when ids_len = 0, which 1196 * is required when user only wants to check for uquery->prog_cnt. 1197 * There is no need to check for it since the case is handled 1198 * gracefully in bpf_prog_array_copy_info. 1199 */ 1200 1201 mutex_lock(&bpf_event_mutex); 1202 progs = bpf_event_rcu_dereference(event->tp_event->prog_array); 1203 ret = bpf_prog_array_copy_info(progs, ids, ids_len, &prog_cnt); 1204 mutex_unlock(&bpf_event_mutex); 1205 1206 if (copy_to_user(&uquery->prog_cnt, &prog_cnt, sizeof(prog_cnt)) || 1207 copy_to_user(uquery->ids, ids, ids_len * sizeof(u32))) 1208 ret = -EFAULT; 1209 1210 kfree(ids); 1211 return ret; 1212 } 1213 1214 extern struct bpf_raw_event_map __start__bpf_raw_tp[]; 1215 extern struct bpf_raw_event_map __stop__bpf_raw_tp[]; 1216 1217 struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name) 1218 { 1219 struct bpf_raw_event_map *btp = __start__bpf_raw_tp; 1220 1221 for (; btp < __stop__bpf_raw_tp; btp++) { 1222 if (!strcmp(btp->tp->name, name)) 1223 return btp; 1224 } 1225 1226 return bpf_get_raw_tracepoint_module(name); 1227 } 1228 1229 void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp) 1230 { 1231 struct module *mod = __module_address((unsigned long)btp); 1232 1233 if (mod) 1234 module_put(mod); 1235 } 1236 1237 static __always_inline 1238 void __bpf_trace_run(struct bpf_prog *prog, u64 *args) 1239 { 1240 rcu_read_lock(); 1241 preempt_disable(); 1242 (void) BPF_PROG_RUN(prog, args); 1243 preempt_enable(); 1244 rcu_read_unlock(); 1245 } 1246 1247 #define UNPACK(...) __VA_ARGS__ 1248 #define REPEAT_1(FN, DL, X, ...) FN(X) 1249 #define REPEAT_2(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_1(FN, DL, __VA_ARGS__) 1250 #define REPEAT_3(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_2(FN, DL, __VA_ARGS__) 1251 #define REPEAT_4(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_3(FN, DL, __VA_ARGS__) 1252 #define REPEAT_5(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_4(FN, DL, __VA_ARGS__) 1253 #define REPEAT_6(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_5(FN, DL, __VA_ARGS__) 1254 #define REPEAT_7(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_6(FN, DL, __VA_ARGS__) 1255 #define REPEAT_8(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_7(FN, DL, __VA_ARGS__) 1256 #define REPEAT_9(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_8(FN, DL, __VA_ARGS__) 1257 #define REPEAT_10(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_9(FN, DL, __VA_ARGS__) 1258 #define REPEAT_11(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_10(FN, DL, __VA_ARGS__) 1259 #define REPEAT_12(FN, DL, X, ...) FN(X) UNPACK DL REPEAT_11(FN, DL, __VA_ARGS__) 1260 #define REPEAT(X, FN, DL, ...) REPEAT_##X(FN, DL, __VA_ARGS__) 1261 1262 #define SARG(X) u64 arg##X 1263 #define COPY(X) args[X] = arg##X 1264 1265 #define __DL_COM (,) 1266 #define __DL_SEM (;) 1267 1268 #define __SEQ_0_11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 1269 1270 #define BPF_TRACE_DEFN_x(x) \ 1271 void bpf_trace_run##x(struct bpf_prog *prog, \ 1272 REPEAT(x, SARG, __DL_COM, __SEQ_0_11)) \ 1273 { \ 1274 u64 args[x]; \ 1275 REPEAT(x, COPY, __DL_SEM, __SEQ_0_11); \ 1276 __bpf_trace_run(prog, args); \ 1277 } \ 1278 EXPORT_SYMBOL_GPL(bpf_trace_run##x) 1279 BPF_TRACE_DEFN_x(1); 1280 BPF_TRACE_DEFN_x(2); 1281 BPF_TRACE_DEFN_x(3); 1282 BPF_TRACE_DEFN_x(4); 1283 BPF_TRACE_DEFN_x(5); 1284 BPF_TRACE_DEFN_x(6); 1285 BPF_TRACE_DEFN_x(7); 1286 BPF_TRACE_DEFN_x(8); 1287 BPF_TRACE_DEFN_x(9); 1288 BPF_TRACE_DEFN_x(10); 1289 BPF_TRACE_DEFN_x(11); 1290 BPF_TRACE_DEFN_x(12); 1291 1292 static int __bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 1293 { 1294 struct tracepoint *tp = btp->tp; 1295 1296 /* 1297 * check that program doesn't access arguments beyond what's 1298 * available in this tracepoint 1299 */ 1300 if (prog->aux->max_ctx_offset > btp->num_args * sizeof(u64)) 1301 return -EINVAL; 1302 1303 if (prog->aux->max_tp_access > btp->writable_size) 1304 return -EINVAL; 1305 1306 return tracepoint_probe_register(tp, (void *)btp->bpf_func, prog); 1307 } 1308 1309 int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 1310 { 1311 return __bpf_probe_register(btp, prog); 1312 } 1313 1314 int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) 1315 { 1316 return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); 1317 } 1318 1319 int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, 1320 u32 *fd_type, const char **buf, 1321 u64 *probe_offset, u64 *probe_addr) 1322 { 1323 bool is_tracepoint, is_syscall_tp; 1324 struct bpf_prog *prog; 1325 int flags, err = 0; 1326 1327 prog = event->prog; 1328 if (!prog) 1329 return -ENOENT; 1330 1331 /* not supporting BPF_PROG_TYPE_PERF_EVENT yet */ 1332 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) 1333 return -EOPNOTSUPP; 1334 1335 *prog_id = prog->aux->id; 1336 flags = event->tp_event->flags; 1337 is_tracepoint = flags & TRACE_EVENT_FL_TRACEPOINT; 1338 is_syscall_tp = is_syscall_trace_event(event->tp_event); 1339 1340 if (is_tracepoint || is_syscall_tp) { 1341 *buf = is_tracepoint ? event->tp_event->tp->name 1342 : event->tp_event->name; 1343 *fd_type = BPF_FD_TYPE_TRACEPOINT; 1344 *probe_offset = 0x0; 1345 *probe_addr = 0x0; 1346 } else { 1347 /* kprobe/uprobe */ 1348 err = -EOPNOTSUPP; 1349 #ifdef CONFIG_KPROBE_EVENTS 1350 if (flags & TRACE_EVENT_FL_KPROBE) 1351 err = bpf_get_kprobe_info(event, fd_type, buf, 1352 probe_offset, probe_addr, 1353 event->attr.type == PERF_TYPE_TRACEPOINT); 1354 #endif 1355 #ifdef CONFIG_UPROBE_EVENTS 1356 if (flags & TRACE_EVENT_FL_UPROBE) 1357 err = bpf_get_uprobe_info(event, fd_type, buf, 1358 probe_offset, 1359 event->attr.type == PERF_TYPE_TRACEPOINT); 1360 #endif 1361 } 1362 1363 return err; 1364 } 1365 1366 #ifdef CONFIG_MODULES 1367 static int bpf_event_notify(struct notifier_block *nb, unsigned long op, 1368 void *module) 1369 { 1370 struct bpf_trace_module *btm, *tmp; 1371 struct module *mod = module; 1372 1373 if (mod->num_bpf_raw_events == 0 || 1374 (op != MODULE_STATE_COMING && op != MODULE_STATE_GOING)) 1375 return 0; 1376 1377 mutex_lock(&bpf_module_mutex); 1378 1379 switch (op) { 1380 case MODULE_STATE_COMING: 1381 btm = kzalloc(sizeof(*btm), GFP_KERNEL); 1382 if (btm) { 1383 btm->module = module; 1384 list_add(&btm->list, &bpf_trace_modules); 1385 } 1386 break; 1387 case MODULE_STATE_GOING: 1388 list_for_each_entry_safe(btm, tmp, &bpf_trace_modules, list) { 1389 if (btm->module == module) { 1390 list_del(&btm->list); 1391 kfree(btm); 1392 break; 1393 } 1394 } 1395 break; 1396 } 1397 1398 mutex_unlock(&bpf_module_mutex); 1399 1400 return 0; 1401 } 1402 1403 static struct notifier_block bpf_module_nb = { 1404 .notifier_call = bpf_event_notify, 1405 }; 1406 1407 static int __init bpf_event_init(void) 1408 { 1409 register_module_notifier(&bpf_module_nb); 1410 return 0; 1411 } 1412 1413 static int __init send_signal_irq_work_init(void) 1414 { 1415 int cpu; 1416 struct send_signal_irq_work *work; 1417 1418 for_each_possible_cpu(cpu) { 1419 work = per_cpu_ptr(&send_signal_work, cpu); 1420 init_irq_work(&work->irq_work, do_bpf_send_signal); 1421 } 1422 return 0; 1423 } 1424 1425 fs_initcall(bpf_event_init); 1426 subsys_initcall(send_signal_irq_work_init); 1427 #endif /* CONFIG_MODULES */ 1428