1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * uprobes-based tracing events 4 * 5 * Copyright (C) IBM Corporation, 2010-2012 6 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com> 7 */ 8 #define pr_fmt(fmt) "trace_uprobe: " fmt 9 10 #include <linux/bpf-cgroup.h> 11 #include <linux/security.h> 12 #include <linux/ctype.h> 13 #include <linux/module.h> 14 #include <linux/uaccess.h> 15 #include <linux/uprobes.h> 16 #include <linux/namei.h> 17 #include <linux/string.h> 18 #include <linux/rculist.h> 19 #include <linux/filter.h> 20 #include <linux/percpu.h> 21 22 #include "trace_dynevent.h" 23 #include "trace_probe.h" 24 #include "trace_probe_tmpl.h" 25 26 #define UPROBE_EVENT_SYSTEM "uprobes" 27 28 struct uprobe_trace_entry_head { 29 struct trace_entry ent; 30 unsigned long vaddr[]; 31 }; 32 33 #define SIZEOF_TRACE_ENTRY(is_return) \ 34 (sizeof(struct uprobe_trace_entry_head) + \ 35 sizeof(unsigned long) * (is_return ? 2 : 1)) 36 37 #define DATAOF_TRACE_ENTRY(entry, is_return) \ 38 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) 39 40 static int trace_uprobe_create(const char *raw_command); 41 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev); 42 static int trace_uprobe_release(struct dyn_event *ev); 43 static bool trace_uprobe_is_busy(struct dyn_event *ev); 44 static bool trace_uprobe_match(const char *system, const char *event, 45 int argc, const char **argv, struct dyn_event *ev); 46 47 static struct dyn_event_operations trace_uprobe_ops = { 48 .create = trace_uprobe_create, 49 .show = trace_uprobe_show, 50 .is_busy = trace_uprobe_is_busy, 51 .free = trace_uprobe_release, 52 .match = trace_uprobe_match, 53 }; 54 55 /* 56 * uprobe event core functions 57 */ 58 struct trace_uprobe { 59 struct dyn_event devent; 60 struct uprobe_consumer consumer; 61 struct path path; 62 char *filename; 63 struct uprobe *uprobe; 64 unsigned long offset; 65 unsigned long ref_ctr_offset; 66 unsigned long __percpu *nhits; 67 struct trace_probe tp; 68 }; 69 70 static bool is_trace_uprobe(struct dyn_event *ev) 71 { 72 return ev->ops == &trace_uprobe_ops; 73 } 74 75 static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev) 76 { 77 return container_of(ev, struct trace_uprobe, devent); 78 } 79 80 /** 81 * for_each_trace_uprobe - iterate over the trace_uprobe list 82 * @pos: the struct trace_uprobe * for each entry 83 * @dpos: the struct dyn_event * to use as a loop cursor 84 */ 85 #define for_each_trace_uprobe(pos, dpos) \ 86 for_each_dyn_event(dpos) \ 87 if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos))) 88 89 static int register_uprobe_event(struct trace_uprobe *tu); 90 static int unregister_uprobe_event(struct trace_uprobe *tu); 91 92 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); 93 static int uretprobe_dispatcher(struct uprobe_consumer *con, 94 unsigned long func, struct pt_regs *regs); 95 96 #ifdef CONFIG_STACK_GROWSUP 97 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) 98 { 99 return addr - (n * sizeof(long)); 100 } 101 #else 102 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) 103 { 104 return addr + (n * sizeof(long)); 105 } 106 #endif 107 108 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) 109 { 110 unsigned long ret; 111 unsigned long addr = user_stack_pointer(regs); 112 113 addr = adjust_stack_addr(addr, n); 114 115 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) 116 return 0; 117 118 return ret; 119 } 120 121 /* 122 * Uprobes-specific fetch functions 123 */ 124 static nokprobe_inline int 125 probe_mem_read(void *dest, void *src, size_t size) 126 { 127 void __user *vaddr = (void __force __user *)src; 128 129 return copy_from_user(dest, vaddr, size) ? -EFAULT : 0; 130 } 131 132 static nokprobe_inline int 133 probe_mem_read_user(void *dest, void *src, size_t size) 134 { 135 return probe_mem_read(dest, src, size); 136 } 137 138 /* 139 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max 140 * length and relative data location. 141 */ 142 static nokprobe_inline int 143 fetch_store_string(unsigned long addr, void *dest, void *base) 144 { 145 long ret; 146 u32 loc = *(u32 *)dest; 147 int maxlen = get_loc_len(loc); 148 u8 *dst = get_loc_data(dest, base); 149 void __user *src = (void __force __user *) addr; 150 151 if (unlikely(!maxlen)) 152 return -ENOMEM; 153 154 if (addr == FETCH_TOKEN_COMM) 155 ret = strscpy(dst, current->comm, maxlen); 156 else 157 ret = strncpy_from_user(dst, src, maxlen); 158 if (ret >= 0) { 159 if (ret == maxlen) 160 dst[ret - 1] = '\0'; 161 else 162 /* 163 * Include the terminating null byte. In this case it 164 * was copied by strncpy_from_user but not accounted 165 * for in ret. 166 */ 167 ret++; 168 *(u32 *)dest = make_data_loc(ret, (void *)dst - base); 169 } else 170 *(u32 *)dest = make_data_loc(0, (void *)dst - base); 171 172 return ret; 173 } 174 175 static nokprobe_inline int 176 fetch_store_string_user(unsigned long addr, void *dest, void *base) 177 { 178 return fetch_store_string(addr, dest, base); 179 } 180 181 /* Return the length of string -- including null terminal byte */ 182 static nokprobe_inline int 183 fetch_store_strlen(unsigned long addr) 184 { 185 int len; 186 void __user *vaddr = (void __force __user *) addr; 187 188 if (addr == FETCH_TOKEN_COMM) 189 len = strlen(current->comm) + 1; 190 else 191 len = strnlen_user(vaddr, MAX_STRING_SIZE); 192 193 return (len > MAX_STRING_SIZE) ? 0 : len; 194 } 195 196 static nokprobe_inline int 197 fetch_store_strlen_user(unsigned long addr) 198 { 199 return fetch_store_strlen(addr); 200 } 201 202 static unsigned long translate_user_vaddr(unsigned long file_offset) 203 { 204 unsigned long base_addr; 205 struct uprobe_dispatch_data *udd; 206 207 udd = (void *) current->utask->vaddr; 208 209 base_addr = udd->bp_addr - udd->tu->offset; 210 return base_addr + file_offset; 211 } 212 213 /* Note that we don't verify it, since the code does not come from user space */ 214 static int 215 process_fetch_insn(struct fetch_insn *code, void *rec, void *edata, 216 void *dest, void *base) 217 { 218 struct pt_regs *regs = rec; 219 unsigned long val; 220 int ret; 221 222 /* 1st stage: get value from context */ 223 switch (code->op) { 224 case FETCH_OP_REG: 225 val = regs_get_register(regs, code->param); 226 break; 227 case FETCH_OP_STACK: 228 val = get_user_stack_nth(regs, code->param); 229 break; 230 case FETCH_OP_STACKP: 231 val = user_stack_pointer(regs); 232 break; 233 case FETCH_OP_RETVAL: 234 val = regs_return_value(regs); 235 break; 236 case FETCH_OP_COMM: 237 val = FETCH_TOKEN_COMM; 238 break; 239 case FETCH_OP_FOFFS: 240 val = translate_user_vaddr(code->immediate); 241 break; 242 default: 243 ret = process_common_fetch_insn(code, &val); 244 if (ret < 0) 245 return ret; 246 } 247 code++; 248 249 return process_fetch_insn_bottom(code, val, dest, base); 250 } 251 NOKPROBE_SYMBOL(process_fetch_insn) 252 253 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) 254 { 255 rwlock_init(&filter->rwlock); 256 filter->nr_systemwide = 0; 257 INIT_LIST_HEAD(&filter->perf_events); 258 } 259 260 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) 261 { 262 return !filter->nr_systemwide && list_empty(&filter->perf_events); 263 } 264 265 static inline bool is_ret_probe(struct trace_uprobe *tu) 266 { 267 return tu->consumer.ret_handler != NULL; 268 } 269 270 static bool trace_uprobe_is_busy(struct dyn_event *ev) 271 { 272 struct trace_uprobe *tu = to_trace_uprobe(ev); 273 274 return trace_probe_is_enabled(&tu->tp); 275 } 276 277 static bool trace_uprobe_match_command_head(struct trace_uprobe *tu, 278 int argc, const char **argv) 279 { 280 char buf[MAX_ARGSTR_LEN + 1]; 281 int len; 282 283 if (!argc) 284 return true; 285 286 len = strlen(tu->filename); 287 if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':') 288 return false; 289 290 if (tu->ref_ctr_offset == 0) 291 snprintf(buf, sizeof(buf), "0x%0*lx", 292 (int)(sizeof(void *) * 2), tu->offset); 293 else 294 snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)", 295 (int)(sizeof(void *) * 2), tu->offset, 296 tu->ref_ctr_offset); 297 if (strcmp(buf, &argv[0][len + 1])) 298 return false; 299 300 argc--; argv++; 301 302 return trace_probe_match_command_args(&tu->tp, argc, argv); 303 } 304 305 static bool trace_uprobe_match(const char *system, const char *event, 306 int argc, const char **argv, struct dyn_event *ev) 307 { 308 struct trace_uprobe *tu = to_trace_uprobe(ev); 309 310 return (event[0] == '\0' || 311 strcmp(trace_probe_name(&tu->tp), event) == 0) && 312 (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) && 313 trace_uprobe_match_command_head(tu, argc, argv); 314 } 315 316 static nokprobe_inline struct trace_uprobe * 317 trace_uprobe_primary_from_call(struct trace_event_call *call) 318 { 319 struct trace_probe *tp; 320 321 tp = trace_probe_primary_from_call(call); 322 if (WARN_ON_ONCE(!tp)) 323 return NULL; 324 325 return container_of(tp, struct trace_uprobe, tp); 326 } 327 328 /* 329 * Allocate new trace_uprobe and initialize it (including uprobes). 330 */ 331 static struct trace_uprobe * 332 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) 333 { 334 struct trace_uprobe *tu; 335 int ret; 336 337 tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL); 338 if (!tu) 339 return ERR_PTR(-ENOMEM); 340 341 tu->nhits = alloc_percpu(unsigned long); 342 if (!tu->nhits) { 343 ret = -ENOMEM; 344 goto error; 345 } 346 347 ret = trace_probe_init(&tu->tp, event, group, true, nargs); 348 if (ret < 0) 349 goto error; 350 351 dyn_event_init(&tu->devent, &trace_uprobe_ops); 352 tu->consumer.handler = uprobe_dispatcher; 353 if (is_ret) 354 tu->consumer.ret_handler = uretprobe_dispatcher; 355 init_trace_uprobe_filter(tu->tp.event->filter); 356 return tu; 357 358 error: 359 free_percpu(tu->nhits); 360 kfree(tu); 361 362 return ERR_PTR(ret); 363 } 364 365 static void free_trace_uprobe(struct trace_uprobe *tu) 366 { 367 if (!tu) 368 return; 369 370 path_put(&tu->path); 371 trace_probe_cleanup(&tu->tp); 372 kfree(tu->filename); 373 free_percpu(tu->nhits); 374 kfree(tu); 375 } 376 377 static struct trace_uprobe *find_probe_event(const char *event, const char *group) 378 { 379 struct dyn_event *pos; 380 struct trace_uprobe *tu; 381 382 for_each_trace_uprobe(tu, pos) 383 if (strcmp(trace_probe_name(&tu->tp), event) == 0 && 384 strcmp(trace_probe_group_name(&tu->tp), group) == 0) 385 return tu; 386 387 return NULL; 388 } 389 390 /* Unregister a trace_uprobe and probe_event */ 391 static int unregister_trace_uprobe(struct trace_uprobe *tu) 392 { 393 int ret; 394 395 if (trace_probe_has_sibling(&tu->tp)) 396 goto unreg; 397 398 /* If there's a reference to the dynamic event */ 399 if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp))) 400 return -EBUSY; 401 402 ret = unregister_uprobe_event(tu); 403 if (ret) 404 return ret; 405 406 unreg: 407 dyn_event_remove(&tu->devent); 408 trace_probe_unlink(&tu->tp); 409 free_trace_uprobe(tu); 410 return 0; 411 } 412 413 static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig, 414 struct trace_uprobe *comp) 415 { 416 struct trace_probe_event *tpe = orig->tp.event; 417 struct inode *comp_inode = d_real_inode(comp->path.dentry); 418 int i; 419 420 list_for_each_entry(orig, &tpe->probes, tp.list) { 421 if (comp_inode != d_real_inode(orig->path.dentry) || 422 comp->offset != orig->offset) 423 continue; 424 425 /* 426 * trace_probe_compare_arg_type() ensured that nr_args and 427 * each argument name and type are same. Let's compare comm. 428 */ 429 for (i = 0; i < orig->tp.nr_args; i++) { 430 if (strcmp(orig->tp.args[i].comm, 431 comp->tp.args[i].comm)) 432 break; 433 } 434 435 if (i == orig->tp.nr_args) 436 return true; 437 } 438 439 return false; 440 } 441 442 static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to) 443 { 444 int ret; 445 446 ret = trace_probe_compare_arg_type(&tu->tp, &to->tp); 447 if (ret) { 448 /* Note that argument starts index = 2 */ 449 trace_probe_log_set_index(ret + 1); 450 trace_probe_log_err(0, DIFF_ARG_TYPE); 451 return -EEXIST; 452 } 453 if (trace_uprobe_has_same_uprobe(to, tu)) { 454 trace_probe_log_set_index(0); 455 trace_probe_log_err(0, SAME_PROBE); 456 return -EEXIST; 457 } 458 459 /* Append to existing event */ 460 ret = trace_probe_append(&tu->tp, &to->tp); 461 if (!ret) 462 dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); 463 464 return ret; 465 } 466 467 /* 468 * Uprobe with multiple reference counter is not allowed. i.e. 469 * If inode and offset matches, reference counter offset *must* 470 * match as well. Though, there is one exception: If user is 471 * replacing old trace_uprobe with new one(same group/event), 472 * then we allow same uprobe with new reference counter as far 473 * as the new one does not conflict with any other existing 474 * ones. 475 */ 476 static int validate_ref_ctr_offset(struct trace_uprobe *new) 477 { 478 struct dyn_event *pos; 479 struct trace_uprobe *tmp; 480 struct inode *new_inode = d_real_inode(new->path.dentry); 481 482 for_each_trace_uprobe(tmp, pos) { 483 if (new_inode == d_real_inode(tmp->path.dentry) && 484 new->offset == tmp->offset && 485 new->ref_ctr_offset != tmp->ref_ctr_offset) { 486 pr_warn("Reference counter offset mismatch."); 487 return -EINVAL; 488 } 489 } 490 return 0; 491 } 492 493 /* Register a trace_uprobe and probe_event */ 494 static int register_trace_uprobe(struct trace_uprobe *tu) 495 { 496 struct trace_uprobe *old_tu; 497 int ret; 498 499 mutex_lock(&event_mutex); 500 501 ret = validate_ref_ctr_offset(tu); 502 if (ret) 503 goto end; 504 505 /* register as an event */ 506 old_tu = find_probe_event(trace_probe_name(&tu->tp), 507 trace_probe_group_name(&tu->tp)); 508 if (old_tu) { 509 if (is_ret_probe(tu) != is_ret_probe(old_tu)) { 510 trace_probe_log_set_index(0); 511 trace_probe_log_err(0, DIFF_PROBE_TYPE); 512 ret = -EEXIST; 513 } else { 514 ret = append_trace_uprobe(tu, old_tu); 515 } 516 goto end; 517 } 518 519 ret = register_uprobe_event(tu); 520 if (ret) { 521 if (ret == -EEXIST) { 522 trace_probe_log_set_index(0); 523 trace_probe_log_err(0, EVENT_EXIST); 524 } else 525 pr_warn("Failed to register probe event(%d)\n", ret); 526 goto end; 527 } 528 529 dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); 530 531 end: 532 mutex_unlock(&event_mutex); 533 534 return ret; 535 } 536 537 /* 538 * Argument syntax: 539 * - Add uprobe: p|r[:[GRP/][EVENT]] PATH:OFFSET[%return][(REF)] [FETCHARGS] 540 */ 541 static int __trace_uprobe_create(int argc, const char **argv) 542 { 543 struct trace_uprobe *tu; 544 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM; 545 char *arg, *filename, *rctr, *rctr_end, *tmp; 546 char buf[MAX_EVENT_NAME_LEN]; 547 char gbuf[MAX_EVENT_NAME_LEN]; 548 enum probe_print_type ptype; 549 struct path path; 550 unsigned long offset, ref_ctr_offset; 551 bool is_return = false; 552 int i, ret; 553 554 ref_ctr_offset = 0; 555 556 switch (argv[0][0]) { 557 case 'r': 558 is_return = true; 559 break; 560 case 'p': 561 break; 562 default: 563 return -ECANCELED; 564 } 565 566 if (argc < 2) 567 return -ECANCELED; 568 569 if (argv[0][1] == ':') 570 event = &argv[0][2]; 571 572 if (!strchr(argv[1], '/')) 573 return -ECANCELED; 574 575 filename = kstrdup(argv[1], GFP_KERNEL); 576 if (!filename) 577 return -ENOMEM; 578 579 /* Find the last occurrence, in case the path contains ':' too. */ 580 arg = strrchr(filename, ':'); 581 if (!arg || !isdigit(arg[1])) { 582 kfree(filename); 583 return -ECANCELED; 584 } 585 586 trace_probe_log_init("trace_uprobe", argc, argv); 587 trace_probe_log_set_index(1); /* filename is the 2nd argument */ 588 589 *arg++ = '\0'; 590 ret = kern_path(filename, LOOKUP_FOLLOW, &path); 591 if (ret) { 592 trace_probe_log_err(0, FILE_NOT_FOUND); 593 kfree(filename); 594 trace_probe_log_clear(); 595 return ret; 596 } 597 if (!d_is_reg(path.dentry)) { 598 trace_probe_log_err(0, NO_REGULAR_FILE); 599 ret = -EINVAL; 600 goto fail_address_parse; 601 } 602 603 /* Parse reference counter offset if specified. */ 604 rctr = strchr(arg, '('); 605 if (rctr) { 606 rctr_end = strchr(rctr, ')'); 607 if (!rctr_end) { 608 ret = -EINVAL; 609 rctr_end = rctr + strlen(rctr); 610 trace_probe_log_err(rctr_end - filename, 611 REFCNT_OPEN_BRACE); 612 goto fail_address_parse; 613 } else if (rctr_end[1] != '\0') { 614 ret = -EINVAL; 615 trace_probe_log_err(rctr_end + 1 - filename, 616 BAD_REFCNT_SUFFIX); 617 goto fail_address_parse; 618 } 619 620 *rctr++ = '\0'; 621 *rctr_end = '\0'; 622 ret = kstrtoul(rctr, 0, &ref_ctr_offset); 623 if (ret) { 624 trace_probe_log_err(rctr - filename, BAD_REFCNT); 625 goto fail_address_parse; 626 } 627 } 628 629 /* Check if there is %return suffix */ 630 tmp = strchr(arg, '%'); 631 if (tmp) { 632 if (!strcmp(tmp, "%return")) { 633 *tmp = '\0'; 634 is_return = true; 635 } else { 636 trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX); 637 ret = -EINVAL; 638 goto fail_address_parse; 639 } 640 } 641 642 /* Parse uprobe offset. */ 643 ret = kstrtoul(arg, 0, &offset); 644 if (ret) { 645 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS); 646 goto fail_address_parse; 647 } 648 649 /* setup a probe */ 650 trace_probe_log_set_index(0); 651 if (event) { 652 ret = traceprobe_parse_event_name(&event, &group, gbuf, 653 event - argv[0]); 654 if (ret) 655 goto fail_address_parse; 656 } 657 658 if (!event) { 659 char *tail; 660 char *ptr; 661 662 tail = kstrdup(kbasename(filename), GFP_KERNEL); 663 if (!tail) { 664 ret = -ENOMEM; 665 goto fail_address_parse; 666 } 667 668 ptr = strpbrk(tail, ".-_"); 669 if (ptr) 670 *ptr = '\0'; 671 672 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); 673 event = buf; 674 kfree(tail); 675 } 676 677 argc -= 2; 678 argv += 2; 679 680 tu = alloc_trace_uprobe(group, event, argc, is_return); 681 if (IS_ERR(tu)) { 682 ret = PTR_ERR(tu); 683 /* This must return -ENOMEM otherwise there is a bug */ 684 WARN_ON_ONCE(ret != -ENOMEM); 685 goto fail_address_parse; 686 } 687 tu->offset = offset; 688 tu->ref_ctr_offset = ref_ctr_offset; 689 tu->path = path; 690 tu->filename = filename; 691 692 /* parse arguments */ 693 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { 694 struct traceprobe_parse_context ctx = { 695 .flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER, 696 }; 697 698 trace_probe_log_set_index(i + 2); 699 ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], &ctx); 700 traceprobe_finish_parse(&ctx); 701 if (ret) 702 goto error; 703 } 704 705 ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; 706 ret = traceprobe_set_print_fmt(&tu->tp, ptype); 707 if (ret < 0) 708 goto error; 709 710 ret = register_trace_uprobe(tu); 711 if (!ret) 712 goto out; 713 714 error: 715 free_trace_uprobe(tu); 716 out: 717 trace_probe_log_clear(); 718 return ret; 719 720 fail_address_parse: 721 trace_probe_log_clear(); 722 path_put(&path); 723 kfree(filename); 724 725 return ret; 726 } 727 728 int trace_uprobe_create(const char *raw_command) 729 { 730 return trace_probe_create(raw_command, __trace_uprobe_create); 731 } 732 733 static int create_or_delete_trace_uprobe(const char *raw_command) 734 { 735 int ret; 736 737 if (raw_command[0] == '-') 738 return dyn_event_release(raw_command, &trace_uprobe_ops); 739 740 ret = trace_uprobe_create(raw_command); 741 return ret == -ECANCELED ? -EINVAL : ret; 742 } 743 744 static int trace_uprobe_release(struct dyn_event *ev) 745 { 746 struct trace_uprobe *tu = to_trace_uprobe(ev); 747 748 return unregister_trace_uprobe(tu); 749 } 750 751 /* Probes listing interfaces */ 752 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev) 753 { 754 struct trace_uprobe *tu = to_trace_uprobe(ev); 755 char c = is_ret_probe(tu) ? 'r' : 'p'; 756 int i; 757 758 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp), 759 trace_probe_name(&tu->tp), tu->filename, 760 (int)(sizeof(void *) * 2), tu->offset); 761 762 if (tu->ref_ctr_offset) 763 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset); 764 765 for (i = 0; i < tu->tp.nr_args; i++) 766 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); 767 768 seq_putc(m, '\n'); 769 return 0; 770 } 771 772 static int probes_seq_show(struct seq_file *m, void *v) 773 { 774 struct dyn_event *ev = v; 775 776 if (!is_trace_uprobe(ev)) 777 return 0; 778 779 return trace_uprobe_show(m, ev); 780 } 781 782 static const struct seq_operations probes_seq_op = { 783 .start = dyn_event_seq_start, 784 .next = dyn_event_seq_next, 785 .stop = dyn_event_seq_stop, 786 .show = probes_seq_show 787 }; 788 789 static int probes_open(struct inode *inode, struct file *file) 790 { 791 int ret; 792 793 ret = security_locked_down(LOCKDOWN_TRACEFS); 794 if (ret) 795 return ret; 796 797 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 798 ret = dyn_events_release_all(&trace_uprobe_ops); 799 if (ret) 800 return ret; 801 } 802 803 return seq_open(file, &probes_seq_op); 804 } 805 806 static ssize_t probes_write(struct file *file, const char __user *buffer, 807 size_t count, loff_t *ppos) 808 { 809 return trace_parse_run_command(file, buffer, count, ppos, 810 create_or_delete_trace_uprobe); 811 } 812 813 static const struct file_operations uprobe_events_ops = { 814 .owner = THIS_MODULE, 815 .open = probes_open, 816 .read = seq_read, 817 .llseek = seq_lseek, 818 .release = seq_release, 819 .write = probes_write, 820 }; 821 822 /* Probes profiling interfaces */ 823 static int probes_profile_seq_show(struct seq_file *m, void *v) 824 { 825 struct dyn_event *ev = v; 826 struct trace_uprobe *tu; 827 unsigned long nhits; 828 int cpu; 829 830 if (!is_trace_uprobe(ev)) 831 return 0; 832 833 tu = to_trace_uprobe(ev); 834 835 nhits = 0; 836 for_each_possible_cpu(cpu) { 837 nhits += per_cpu(*tu->nhits, cpu); 838 } 839 840 seq_printf(m, " %s %-44s %15lu\n", tu->filename, 841 trace_probe_name(&tu->tp), nhits); 842 return 0; 843 } 844 845 static const struct seq_operations profile_seq_op = { 846 .start = dyn_event_seq_start, 847 .next = dyn_event_seq_next, 848 .stop = dyn_event_seq_stop, 849 .show = probes_profile_seq_show 850 }; 851 852 static int profile_open(struct inode *inode, struct file *file) 853 { 854 int ret; 855 856 ret = security_locked_down(LOCKDOWN_TRACEFS); 857 if (ret) 858 return ret; 859 860 return seq_open(file, &profile_seq_op); 861 } 862 863 static const struct file_operations uprobe_profile_ops = { 864 .owner = THIS_MODULE, 865 .open = profile_open, 866 .read = seq_read, 867 .llseek = seq_lseek, 868 .release = seq_release, 869 }; 870 871 struct uprobe_cpu_buffer { 872 struct mutex mutex; 873 void *buf; 874 int dsize; 875 }; 876 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; 877 static int uprobe_buffer_refcnt; 878 879 static int uprobe_buffer_init(void) 880 { 881 int cpu, err_cpu; 882 883 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); 884 if (uprobe_cpu_buffer == NULL) 885 return -ENOMEM; 886 887 for_each_possible_cpu(cpu) { 888 struct page *p = alloc_pages_node(cpu_to_node(cpu), 889 GFP_KERNEL, 0); 890 if (p == NULL) { 891 err_cpu = cpu; 892 goto err; 893 } 894 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); 895 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); 896 } 897 898 return 0; 899 900 err: 901 for_each_possible_cpu(cpu) { 902 if (cpu == err_cpu) 903 break; 904 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); 905 } 906 907 free_percpu(uprobe_cpu_buffer); 908 return -ENOMEM; 909 } 910 911 static int uprobe_buffer_enable(void) 912 { 913 int ret = 0; 914 915 BUG_ON(!mutex_is_locked(&event_mutex)); 916 917 if (uprobe_buffer_refcnt++ == 0) { 918 ret = uprobe_buffer_init(); 919 if (ret < 0) 920 uprobe_buffer_refcnt--; 921 } 922 923 return ret; 924 } 925 926 static void uprobe_buffer_disable(void) 927 { 928 int cpu; 929 930 BUG_ON(!mutex_is_locked(&event_mutex)); 931 932 if (--uprobe_buffer_refcnt == 0) { 933 for_each_possible_cpu(cpu) 934 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, 935 cpu)->buf); 936 937 free_percpu(uprobe_cpu_buffer); 938 uprobe_cpu_buffer = NULL; 939 } 940 } 941 942 static struct uprobe_cpu_buffer *uprobe_buffer_get(void) 943 { 944 struct uprobe_cpu_buffer *ucb; 945 int cpu; 946 947 cpu = raw_smp_processor_id(); 948 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); 949 950 /* 951 * Use per-cpu buffers for fastest access, but we might migrate 952 * so the mutex makes sure we have sole access to it. 953 */ 954 mutex_lock(&ucb->mutex); 955 956 return ucb; 957 } 958 959 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) 960 { 961 if (!ucb) 962 return; 963 mutex_unlock(&ucb->mutex); 964 } 965 966 static struct uprobe_cpu_buffer *prepare_uprobe_buffer(struct trace_uprobe *tu, 967 struct pt_regs *regs, 968 struct uprobe_cpu_buffer **ucbp) 969 { 970 struct uprobe_cpu_buffer *ucb; 971 int dsize, esize; 972 973 if (*ucbp) 974 return *ucbp; 975 976 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 977 dsize = __get_data_size(&tu->tp, regs, NULL); 978 979 ucb = uprobe_buffer_get(); 980 ucb->dsize = tu->tp.size + dsize; 981 982 store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize); 983 984 *ucbp = ucb; 985 return ucb; 986 } 987 988 static void __uprobe_trace_func(struct trace_uprobe *tu, 989 unsigned long func, struct pt_regs *regs, 990 struct uprobe_cpu_buffer *ucb, 991 struct trace_event_file *trace_file) 992 { 993 struct uprobe_trace_entry_head *entry; 994 struct trace_event_buffer fbuffer; 995 void *data; 996 int size, esize; 997 struct trace_event_call *call = trace_probe_event_call(&tu->tp); 998 999 WARN_ON(call != trace_file->event_call); 1000 1001 if (WARN_ON_ONCE(ucb->dsize > PAGE_SIZE)) 1002 return; 1003 1004 if (trace_trigger_soft_disabled(trace_file)) 1005 return; 1006 1007 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 1008 size = esize + ucb->dsize; 1009 entry = trace_event_buffer_reserve(&fbuffer, trace_file, size); 1010 if (!entry) 1011 return; 1012 1013 if (is_ret_probe(tu)) { 1014 entry->vaddr[0] = func; 1015 entry->vaddr[1] = instruction_pointer(regs); 1016 data = DATAOF_TRACE_ENTRY(entry, true); 1017 } else { 1018 entry->vaddr[0] = instruction_pointer(regs); 1019 data = DATAOF_TRACE_ENTRY(entry, false); 1020 } 1021 1022 memcpy(data, ucb->buf, ucb->dsize); 1023 1024 trace_event_buffer_commit(&fbuffer); 1025 } 1026 1027 /* uprobe handler */ 1028 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs, 1029 struct uprobe_cpu_buffer **ucbp) 1030 { 1031 struct event_file_link *link; 1032 struct uprobe_cpu_buffer *ucb; 1033 1034 if (is_ret_probe(tu)) 1035 return 0; 1036 1037 ucb = prepare_uprobe_buffer(tu, regs, ucbp); 1038 1039 rcu_read_lock(); 1040 trace_probe_for_each_link_rcu(link, &tu->tp) 1041 __uprobe_trace_func(tu, 0, regs, ucb, link->file); 1042 rcu_read_unlock(); 1043 1044 return 0; 1045 } 1046 1047 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, 1048 struct pt_regs *regs, 1049 struct uprobe_cpu_buffer **ucbp) 1050 { 1051 struct event_file_link *link; 1052 struct uprobe_cpu_buffer *ucb; 1053 1054 ucb = prepare_uprobe_buffer(tu, regs, ucbp); 1055 1056 rcu_read_lock(); 1057 trace_probe_for_each_link_rcu(link, &tu->tp) 1058 __uprobe_trace_func(tu, func, regs, ucb, link->file); 1059 rcu_read_unlock(); 1060 } 1061 1062 /* Event entry printers */ 1063 static enum print_line_t 1064 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) 1065 { 1066 struct uprobe_trace_entry_head *entry; 1067 struct trace_seq *s = &iter->seq; 1068 struct trace_uprobe *tu; 1069 u8 *data; 1070 1071 entry = (struct uprobe_trace_entry_head *)iter->ent; 1072 tu = trace_uprobe_primary_from_call( 1073 container_of(event, struct trace_event_call, event)); 1074 if (unlikely(!tu)) 1075 goto out; 1076 1077 if (is_ret_probe(tu)) { 1078 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", 1079 trace_probe_name(&tu->tp), 1080 entry->vaddr[1], entry->vaddr[0]); 1081 data = DATAOF_TRACE_ENTRY(entry, true); 1082 } else { 1083 trace_seq_printf(s, "%s: (0x%lx)", 1084 trace_probe_name(&tu->tp), 1085 entry->vaddr[0]); 1086 data = DATAOF_TRACE_ENTRY(entry, false); 1087 } 1088 1089 if (trace_probe_print_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0) 1090 goto out; 1091 1092 trace_seq_putc(s, '\n'); 1093 1094 out: 1095 return trace_handle_return(s); 1096 } 1097 1098 typedef bool (*filter_func_t)(struct uprobe_consumer *self, struct mm_struct *mm); 1099 1100 static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter) 1101 { 1102 struct inode *inode = d_real_inode(tu->path.dentry); 1103 struct uprobe *uprobe; 1104 1105 tu->consumer.filter = filter; 1106 uprobe = uprobe_register(inode, tu->offset, tu->ref_ctr_offset, &tu->consumer); 1107 if (IS_ERR(uprobe)) 1108 return PTR_ERR(uprobe); 1109 1110 tu->uprobe = uprobe; 1111 return 0; 1112 } 1113 1114 static void __probe_event_disable(struct trace_probe *tp) 1115 { 1116 struct trace_uprobe *tu; 1117 bool sync = false; 1118 1119 tu = container_of(tp, struct trace_uprobe, tp); 1120 WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); 1121 1122 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { 1123 if (!tu->uprobe) 1124 continue; 1125 1126 uprobe_unregister_nosync(tu->uprobe, &tu->consumer); 1127 sync = true; 1128 tu->uprobe = NULL; 1129 } 1130 if (sync) 1131 uprobe_unregister_sync(); 1132 } 1133 1134 static int probe_event_enable(struct trace_event_call *call, 1135 struct trace_event_file *file, filter_func_t filter) 1136 { 1137 struct trace_probe *tp; 1138 struct trace_uprobe *tu; 1139 bool enabled; 1140 int ret; 1141 1142 tp = trace_probe_primary_from_call(call); 1143 if (WARN_ON_ONCE(!tp)) 1144 return -ENODEV; 1145 enabled = trace_probe_is_enabled(tp); 1146 1147 /* This may also change "enabled" state */ 1148 if (file) { 1149 if (trace_probe_test_flag(tp, TP_FLAG_PROFILE)) 1150 return -EINTR; 1151 1152 ret = trace_probe_add_file(tp, file); 1153 if (ret < 0) 1154 return ret; 1155 } else { 1156 if (trace_probe_test_flag(tp, TP_FLAG_TRACE)) 1157 return -EINTR; 1158 1159 trace_probe_set_flag(tp, TP_FLAG_PROFILE); 1160 } 1161 1162 tu = container_of(tp, struct trace_uprobe, tp); 1163 WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); 1164 1165 if (enabled) 1166 return 0; 1167 1168 ret = uprobe_buffer_enable(); 1169 if (ret) 1170 goto err_flags; 1171 1172 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { 1173 ret = trace_uprobe_enable(tu, filter); 1174 if (ret) { 1175 __probe_event_disable(tp); 1176 goto err_buffer; 1177 } 1178 } 1179 1180 return 0; 1181 1182 err_buffer: 1183 uprobe_buffer_disable(); 1184 1185 err_flags: 1186 if (file) 1187 trace_probe_remove_file(tp, file); 1188 else 1189 trace_probe_clear_flag(tp, TP_FLAG_PROFILE); 1190 1191 return ret; 1192 } 1193 1194 static void probe_event_disable(struct trace_event_call *call, 1195 struct trace_event_file *file) 1196 { 1197 struct trace_probe *tp; 1198 1199 tp = trace_probe_primary_from_call(call); 1200 if (WARN_ON_ONCE(!tp)) 1201 return; 1202 1203 if (!trace_probe_is_enabled(tp)) 1204 return; 1205 1206 if (file) { 1207 if (trace_probe_remove_file(tp, file) < 0) 1208 return; 1209 1210 if (trace_probe_is_enabled(tp)) 1211 return; 1212 } else 1213 trace_probe_clear_flag(tp, TP_FLAG_PROFILE); 1214 1215 __probe_event_disable(tp); 1216 uprobe_buffer_disable(); 1217 } 1218 1219 static int uprobe_event_define_fields(struct trace_event_call *event_call) 1220 { 1221 int ret, size; 1222 struct uprobe_trace_entry_head field; 1223 struct trace_uprobe *tu; 1224 1225 tu = trace_uprobe_primary_from_call(event_call); 1226 if (unlikely(!tu)) 1227 return -ENODEV; 1228 1229 if (is_ret_probe(tu)) { 1230 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); 1231 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); 1232 size = SIZEOF_TRACE_ENTRY(true); 1233 } else { 1234 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); 1235 size = SIZEOF_TRACE_ENTRY(false); 1236 } 1237 1238 return traceprobe_define_arg_fields(event_call, size, &tu->tp); 1239 } 1240 1241 #ifdef CONFIG_PERF_EVENTS 1242 static bool 1243 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) 1244 { 1245 struct perf_event *event; 1246 1247 list_for_each_entry(event, &filter->perf_events, hw.tp_list) { 1248 if (event->hw.target->mm == mm) 1249 return true; 1250 } 1251 1252 return false; 1253 } 1254 1255 static inline bool 1256 trace_uprobe_filter_event(struct trace_uprobe_filter *filter, 1257 struct perf_event *event) 1258 { 1259 return __uprobe_perf_filter(filter, event->hw.target->mm); 1260 } 1261 1262 static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter, 1263 struct perf_event *event) 1264 { 1265 bool done; 1266 1267 write_lock(&filter->rwlock); 1268 if (event->hw.target) { 1269 list_del(&event->hw.tp_list); 1270 done = filter->nr_systemwide || 1271 (event->hw.target->flags & PF_EXITING) || 1272 trace_uprobe_filter_event(filter, event); 1273 } else { 1274 filter->nr_systemwide--; 1275 done = filter->nr_systemwide; 1276 } 1277 write_unlock(&filter->rwlock); 1278 1279 return done; 1280 } 1281 1282 /* This returns true if the filter always covers target mm */ 1283 static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter, 1284 struct perf_event *event) 1285 { 1286 bool done; 1287 1288 write_lock(&filter->rwlock); 1289 if (event->hw.target) { 1290 /* 1291 * event->parent != NULL means copy_process(), we can avoid 1292 * uprobe_apply(). current->mm must be probed and we can rely 1293 * on dup_mmap() which preserves the already installed bp's. 1294 * 1295 * attr.enable_on_exec means that exec/mmap will install the 1296 * breakpoints we need. 1297 */ 1298 done = filter->nr_systemwide || 1299 event->parent || event->attr.enable_on_exec || 1300 trace_uprobe_filter_event(filter, event); 1301 list_add(&event->hw.tp_list, &filter->perf_events); 1302 } else { 1303 done = filter->nr_systemwide; 1304 filter->nr_systemwide++; 1305 } 1306 write_unlock(&filter->rwlock); 1307 1308 return done; 1309 } 1310 1311 static int uprobe_perf_close(struct trace_event_call *call, 1312 struct perf_event *event) 1313 { 1314 struct trace_probe *tp; 1315 struct trace_uprobe *tu; 1316 int ret = 0; 1317 1318 tp = trace_probe_primary_from_call(call); 1319 if (WARN_ON_ONCE(!tp)) 1320 return -ENODEV; 1321 1322 tu = container_of(tp, struct trace_uprobe, tp); 1323 if (trace_uprobe_filter_remove(tu->tp.event->filter, event)) 1324 return 0; 1325 1326 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { 1327 ret = uprobe_apply(tu->uprobe, &tu->consumer, false); 1328 if (ret) 1329 break; 1330 } 1331 1332 return ret; 1333 } 1334 1335 static int uprobe_perf_open(struct trace_event_call *call, 1336 struct perf_event *event) 1337 { 1338 struct trace_probe *tp; 1339 struct trace_uprobe *tu; 1340 int err = 0; 1341 1342 tp = trace_probe_primary_from_call(call); 1343 if (WARN_ON_ONCE(!tp)) 1344 return -ENODEV; 1345 1346 tu = container_of(tp, struct trace_uprobe, tp); 1347 if (trace_uprobe_filter_add(tu->tp.event->filter, event)) 1348 return 0; 1349 1350 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { 1351 err = uprobe_apply(tu->uprobe, &tu->consumer, true); 1352 if (err) { 1353 uprobe_perf_close(call, event); 1354 break; 1355 } 1356 } 1357 1358 return err; 1359 } 1360 1361 static bool uprobe_perf_filter(struct uprobe_consumer *uc, struct mm_struct *mm) 1362 { 1363 struct trace_uprobe_filter *filter; 1364 struct trace_uprobe *tu; 1365 int ret; 1366 1367 tu = container_of(uc, struct trace_uprobe, consumer); 1368 filter = tu->tp.event->filter; 1369 1370 /* 1371 * speculative short-circuiting check to avoid unnecessarily taking 1372 * filter->rwlock below, if the uprobe has system-wide consumer 1373 */ 1374 if (READ_ONCE(filter->nr_systemwide)) 1375 return true; 1376 1377 read_lock(&filter->rwlock); 1378 ret = __uprobe_perf_filter(filter, mm); 1379 read_unlock(&filter->rwlock); 1380 1381 return ret; 1382 } 1383 1384 static void __uprobe_perf_func(struct trace_uprobe *tu, 1385 unsigned long func, struct pt_regs *regs, 1386 struct uprobe_cpu_buffer **ucbp) 1387 { 1388 struct trace_event_call *call = trace_probe_event_call(&tu->tp); 1389 struct uprobe_trace_entry_head *entry; 1390 struct uprobe_cpu_buffer *ucb; 1391 struct hlist_head *head; 1392 void *data; 1393 int size, esize; 1394 int rctx; 1395 1396 #ifdef CONFIG_BPF_EVENTS 1397 if (bpf_prog_array_valid(call)) { 1398 u32 ret; 1399 1400 ret = bpf_prog_run_array_uprobe(call->prog_array, regs, bpf_prog_run); 1401 if (!ret) 1402 return; 1403 } 1404 #endif /* CONFIG_BPF_EVENTS */ 1405 1406 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); 1407 1408 ucb = prepare_uprobe_buffer(tu, regs, ucbp); 1409 size = esize + ucb->dsize; 1410 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); 1411 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) 1412 return; 1413 1414 preempt_disable(); 1415 head = this_cpu_ptr(call->perf_events); 1416 if (hlist_empty(head)) 1417 goto out; 1418 1419 entry = perf_trace_buf_alloc(size, NULL, &rctx); 1420 if (!entry) 1421 goto out; 1422 1423 if (is_ret_probe(tu)) { 1424 entry->vaddr[0] = func; 1425 entry->vaddr[1] = instruction_pointer(regs); 1426 data = DATAOF_TRACE_ENTRY(entry, true); 1427 } else { 1428 entry->vaddr[0] = instruction_pointer(regs); 1429 data = DATAOF_TRACE_ENTRY(entry, false); 1430 } 1431 1432 memcpy(data, ucb->buf, ucb->dsize); 1433 1434 if (size - esize > ucb->dsize) 1435 memset(data + ucb->dsize, 0, size - esize - ucb->dsize); 1436 1437 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, 1438 head, NULL); 1439 out: 1440 preempt_enable(); 1441 } 1442 1443 /* uprobe profile handler */ 1444 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, 1445 struct uprobe_cpu_buffer **ucbp) 1446 { 1447 if (!uprobe_perf_filter(&tu->consumer, current->mm)) 1448 return UPROBE_HANDLER_REMOVE; 1449 1450 if (!is_ret_probe(tu)) 1451 __uprobe_perf_func(tu, 0, regs, ucbp); 1452 return 0; 1453 } 1454 1455 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, 1456 struct pt_regs *regs, 1457 struct uprobe_cpu_buffer **ucbp) 1458 { 1459 __uprobe_perf_func(tu, func, regs, ucbp); 1460 } 1461 1462 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, 1463 const char **filename, u64 *probe_offset, 1464 u64 *probe_addr, bool perf_type_tracepoint) 1465 { 1466 const char *pevent = trace_event_name(event->tp_event); 1467 const char *group = event->tp_event->class->system; 1468 struct trace_uprobe *tu; 1469 1470 if (perf_type_tracepoint) 1471 tu = find_probe_event(pevent, group); 1472 else 1473 tu = trace_uprobe_primary_from_call(event->tp_event); 1474 if (!tu) 1475 return -EINVAL; 1476 1477 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE 1478 : BPF_FD_TYPE_UPROBE; 1479 *filename = tu->filename; 1480 *probe_offset = tu->offset; 1481 *probe_addr = 0; 1482 return 0; 1483 } 1484 #endif /* CONFIG_PERF_EVENTS */ 1485 1486 static int 1487 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, 1488 void *data) 1489 { 1490 struct trace_event_file *file = data; 1491 1492 switch (type) { 1493 case TRACE_REG_REGISTER: 1494 return probe_event_enable(event, file, NULL); 1495 1496 case TRACE_REG_UNREGISTER: 1497 probe_event_disable(event, file); 1498 return 0; 1499 1500 #ifdef CONFIG_PERF_EVENTS 1501 case TRACE_REG_PERF_REGISTER: 1502 return probe_event_enable(event, NULL, uprobe_perf_filter); 1503 1504 case TRACE_REG_PERF_UNREGISTER: 1505 probe_event_disable(event, NULL); 1506 return 0; 1507 1508 case TRACE_REG_PERF_OPEN: 1509 return uprobe_perf_open(event, data); 1510 1511 case TRACE_REG_PERF_CLOSE: 1512 return uprobe_perf_close(event, data); 1513 1514 #endif 1515 default: 1516 return 0; 1517 } 1518 } 1519 1520 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) 1521 { 1522 struct trace_uprobe *tu; 1523 struct uprobe_dispatch_data udd; 1524 struct uprobe_cpu_buffer *ucb = NULL; 1525 int ret = 0; 1526 1527 tu = container_of(con, struct trace_uprobe, consumer); 1528 1529 this_cpu_inc(*tu->nhits); 1530 1531 udd.tu = tu; 1532 udd.bp_addr = instruction_pointer(regs); 1533 1534 current->utask->vaddr = (unsigned long) &udd; 1535 1536 if (WARN_ON_ONCE(!uprobe_cpu_buffer)) 1537 return 0; 1538 1539 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) 1540 ret |= uprobe_trace_func(tu, regs, &ucb); 1541 1542 #ifdef CONFIG_PERF_EVENTS 1543 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) 1544 ret |= uprobe_perf_func(tu, regs, &ucb); 1545 #endif 1546 uprobe_buffer_put(ucb); 1547 return ret; 1548 } 1549 1550 static int uretprobe_dispatcher(struct uprobe_consumer *con, 1551 unsigned long func, struct pt_regs *regs) 1552 { 1553 struct trace_uprobe *tu; 1554 struct uprobe_dispatch_data udd; 1555 struct uprobe_cpu_buffer *ucb = NULL; 1556 1557 tu = container_of(con, struct trace_uprobe, consumer); 1558 1559 udd.tu = tu; 1560 udd.bp_addr = func; 1561 1562 current->utask->vaddr = (unsigned long) &udd; 1563 1564 if (WARN_ON_ONCE(!uprobe_cpu_buffer)) 1565 return 0; 1566 1567 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) 1568 uretprobe_trace_func(tu, func, regs, &ucb); 1569 1570 #ifdef CONFIG_PERF_EVENTS 1571 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) 1572 uretprobe_perf_func(tu, func, regs, &ucb); 1573 #endif 1574 uprobe_buffer_put(ucb); 1575 return 0; 1576 } 1577 1578 static struct trace_event_functions uprobe_funcs = { 1579 .trace = print_uprobe_event 1580 }; 1581 1582 static struct trace_event_fields uprobe_fields_array[] = { 1583 { .type = TRACE_FUNCTION_TYPE, 1584 .define_fields = uprobe_event_define_fields }, 1585 {} 1586 }; 1587 1588 static inline void init_trace_event_call(struct trace_uprobe *tu) 1589 { 1590 struct trace_event_call *call = trace_probe_event_call(&tu->tp); 1591 call->event.funcs = &uprobe_funcs; 1592 call->class->fields_array = uprobe_fields_array; 1593 1594 call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY; 1595 call->class->reg = trace_uprobe_register; 1596 } 1597 1598 static int register_uprobe_event(struct trace_uprobe *tu) 1599 { 1600 init_trace_event_call(tu); 1601 1602 return trace_probe_register_event_call(&tu->tp); 1603 } 1604 1605 static int unregister_uprobe_event(struct trace_uprobe *tu) 1606 { 1607 return trace_probe_unregister_event_call(&tu->tp); 1608 } 1609 1610 #ifdef CONFIG_PERF_EVENTS 1611 struct trace_event_call * 1612 create_local_trace_uprobe(char *name, unsigned long offs, 1613 unsigned long ref_ctr_offset, bool is_return) 1614 { 1615 enum probe_print_type ptype; 1616 struct trace_uprobe *tu; 1617 struct path path; 1618 int ret; 1619 1620 ret = kern_path(name, LOOKUP_FOLLOW, &path); 1621 if (ret) 1622 return ERR_PTR(ret); 1623 1624 if (!d_is_reg(path.dentry)) { 1625 path_put(&path); 1626 return ERR_PTR(-EINVAL); 1627 } 1628 1629 /* 1630 * local trace_kprobes are not added to dyn_event, so they are never 1631 * searched in find_trace_kprobe(). Therefore, there is no concern of 1632 * duplicated name "DUMMY_EVENT" here. 1633 */ 1634 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0, 1635 is_return); 1636 1637 if (IS_ERR(tu)) { 1638 pr_info("Failed to allocate trace_uprobe.(%d)\n", 1639 (int)PTR_ERR(tu)); 1640 path_put(&path); 1641 return ERR_CAST(tu); 1642 } 1643 1644 tu->offset = offs; 1645 tu->path = path; 1646 tu->ref_ctr_offset = ref_ctr_offset; 1647 tu->filename = kstrdup(name, GFP_KERNEL); 1648 if (!tu->filename) { 1649 ret = -ENOMEM; 1650 goto error; 1651 } 1652 1653 init_trace_event_call(tu); 1654 1655 ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; 1656 if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) { 1657 ret = -ENOMEM; 1658 goto error; 1659 } 1660 1661 return trace_probe_event_call(&tu->tp); 1662 error: 1663 free_trace_uprobe(tu); 1664 return ERR_PTR(ret); 1665 } 1666 1667 void destroy_local_trace_uprobe(struct trace_event_call *event_call) 1668 { 1669 struct trace_uprobe *tu; 1670 1671 tu = trace_uprobe_primary_from_call(event_call); 1672 1673 free_trace_uprobe(tu); 1674 } 1675 #endif /* CONFIG_PERF_EVENTS */ 1676 1677 /* Make a trace interface for controlling probe points */ 1678 static __init int init_uprobe_trace(void) 1679 { 1680 int ret; 1681 1682 ret = dyn_event_register(&trace_uprobe_ops); 1683 if (ret) 1684 return ret; 1685 1686 ret = tracing_init_dentry(); 1687 if (ret) 1688 return 0; 1689 1690 trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL, 1691 NULL, &uprobe_events_ops); 1692 /* Profile interface */ 1693 trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL, 1694 NULL, &uprobe_profile_ops); 1695 return 0; 1696 } 1697 1698 fs_initcall(init_uprobe_trace); 1699