1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Kprobes-based tracing events 4 * 5 * Created by Masami Hiramatsu <mhiramat@redhat.com> 6 * 7 */ 8 #define pr_fmt(fmt) "trace_kprobe: " fmt 9 10 #include <linux/module.h> 11 #include <linux/uaccess.h> 12 #include <linux/rculist.h> 13 #include <linux/error-injection.h> 14 15 #include "trace_dynevent.h" 16 #include "trace_kprobe_selftest.h" 17 #include "trace_probe.h" 18 #include "trace_probe_tmpl.h" 19 20 #define KPROBE_EVENT_SYSTEM "kprobes" 21 #define KRETPROBE_MAXACTIVE_MAX 4096 22 23 static int trace_kprobe_create(int argc, const char **argv); 24 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev); 25 static int trace_kprobe_release(struct dyn_event *ev); 26 static bool trace_kprobe_is_busy(struct dyn_event *ev); 27 static bool trace_kprobe_match(const char *system, const char *event, 28 struct dyn_event *ev); 29 30 static struct dyn_event_operations trace_kprobe_ops = { 31 .create = trace_kprobe_create, 32 .show = trace_kprobe_show, 33 .is_busy = trace_kprobe_is_busy, 34 .free = trace_kprobe_release, 35 .match = trace_kprobe_match, 36 }; 37 38 /** 39 * Kprobe event core functions 40 */ 41 struct trace_kprobe { 42 struct dyn_event devent; 43 struct kretprobe rp; /* Use rp.kp for kprobe use */ 44 unsigned long __percpu *nhit; 45 const char *symbol; /* symbol name */ 46 struct trace_probe tp; 47 }; 48 49 static bool is_trace_kprobe(struct dyn_event *ev) 50 { 51 return ev->ops == &trace_kprobe_ops; 52 } 53 54 static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev) 55 { 56 return container_of(ev, struct trace_kprobe, devent); 57 } 58 59 /** 60 * for_each_trace_kprobe - iterate over the trace_kprobe list 61 * @pos: the struct trace_kprobe * for each entry 62 * @dpos: the struct dyn_event * to use as a loop cursor 63 */ 64 #define for_each_trace_kprobe(pos, dpos) \ 65 for_each_dyn_event(dpos) \ 66 if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos))) 67 68 #define SIZEOF_TRACE_KPROBE(n) \ 69 (offsetof(struct trace_kprobe, tp.args) + \ 70 (sizeof(struct probe_arg) * (n))) 71 72 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) 73 { 74 return tk->rp.handler != NULL; 75 } 76 77 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk) 78 { 79 return tk->symbol ? tk->symbol : "unknown"; 80 } 81 82 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk) 83 { 84 return tk->rp.kp.offset; 85 } 86 87 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk) 88 { 89 return !!(kprobe_gone(&tk->rp.kp)); 90 } 91 92 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk, 93 struct module *mod) 94 { 95 int len = strlen(mod->name); 96 const char *name = trace_kprobe_symbol(tk); 97 return strncmp(mod->name, name, len) == 0 && name[len] == ':'; 98 } 99 100 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) 101 { 102 char *p; 103 bool ret; 104 105 if (!tk->symbol) 106 return false; 107 p = strchr(tk->symbol, ':'); 108 if (!p) 109 return true; 110 *p = '\0'; 111 mutex_lock(&module_mutex); 112 ret = !!find_module(tk->symbol); 113 mutex_unlock(&module_mutex); 114 *p = ':'; 115 116 return ret; 117 } 118 119 static bool trace_kprobe_is_busy(struct dyn_event *ev) 120 { 121 struct trace_kprobe *tk = to_trace_kprobe(ev); 122 123 return trace_probe_is_enabled(&tk->tp); 124 } 125 126 static bool trace_kprobe_match(const char *system, const char *event, 127 struct dyn_event *ev) 128 { 129 struct trace_kprobe *tk = to_trace_kprobe(ev); 130 131 return strcmp(trace_event_name(&tk->tp.call), event) == 0 && 132 (!system || strcmp(tk->tp.call.class->system, system) == 0); 133 } 134 135 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) 136 { 137 unsigned long nhit = 0; 138 int cpu; 139 140 for_each_possible_cpu(cpu) 141 nhit += *per_cpu_ptr(tk->nhit, cpu); 142 143 return nhit; 144 } 145 146 /* Return 0 if it fails to find the symbol address */ 147 static nokprobe_inline 148 unsigned long trace_kprobe_address(struct trace_kprobe *tk) 149 { 150 unsigned long addr; 151 152 if (tk->symbol) { 153 addr = (unsigned long) 154 kallsyms_lookup_name(trace_kprobe_symbol(tk)); 155 if (addr) 156 addr += tk->rp.kp.offset; 157 } else { 158 addr = (unsigned long)tk->rp.kp.addr; 159 } 160 return addr; 161 } 162 163 bool trace_kprobe_on_func_entry(struct trace_event_call *call) 164 { 165 struct trace_kprobe *tk = (struct trace_kprobe *)call->data; 166 167 return kprobe_on_func_entry(tk->rp.kp.addr, 168 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name, 169 tk->rp.kp.addr ? 0 : tk->rp.kp.offset); 170 } 171 172 bool trace_kprobe_error_injectable(struct trace_event_call *call) 173 { 174 struct trace_kprobe *tk = (struct trace_kprobe *)call->data; 175 176 return within_error_injection_list(trace_kprobe_address(tk)); 177 } 178 179 static int register_kprobe_event(struct trace_kprobe *tk); 180 static int unregister_kprobe_event(struct trace_kprobe *tk); 181 182 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); 183 static int kretprobe_dispatcher(struct kretprobe_instance *ri, 184 struct pt_regs *regs); 185 186 /* 187 * Allocate new trace_probe and initialize it (including kprobes). 188 */ 189 static struct trace_kprobe *alloc_trace_kprobe(const char *group, 190 const char *event, 191 void *addr, 192 const char *symbol, 193 unsigned long offs, 194 int maxactive, 195 int nargs, bool is_return) 196 { 197 struct trace_kprobe *tk; 198 int ret = -ENOMEM; 199 200 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL); 201 if (!tk) 202 return ERR_PTR(ret); 203 204 tk->nhit = alloc_percpu(unsigned long); 205 if (!tk->nhit) 206 goto error; 207 208 if (symbol) { 209 tk->symbol = kstrdup(symbol, GFP_KERNEL); 210 if (!tk->symbol) 211 goto error; 212 tk->rp.kp.symbol_name = tk->symbol; 213 tk->rp.kp.offset = offs; 214 } else 215 tk->rp.kp.addr = addr; 216 217 if (is_return) 218 tk->rp.handler = kretprobe_dispatcher; 219 else 220 tk->rp.kp.pre_handler = kprobe_dispatcher; 221 222 tk->rp.maxactive = maxactive; 223 224 if (!event || !is_good_name(event)) { 225 ret = -EINVAL; 226 goto error; 227 } 228 229 tk->tp.call.class = &tk->tp.class; 230 tk->tp.call.name = kstrdup(event, GFP_KERNEL); 231 if (!tk->tp.call.name) 232 goto error; 233 234 if (!group || !is_good_name(group)) { 235 ret = -EINVAL; 236 goto error; 237 } 238 239 tk->tp.class.system = kstrdup(group, GFP_KERNEL); 240 if (!tk->tp.class.system) 241 goto error; 242 243 dyn_event_init(&tk->devent, &trace_kprobe_ops); 244 INIT_LIST_HEAD(&tk->tp.files); 245 return tk; 246 error: 247 kfree(tk->tp.call.name); 248 kfree(tk->symbol); 249 free_percpu(tk->nhit); 250 kfree(tk); 251 return ERR_PTR(ret); 252 } 253 254 static void free_trace_kprobe(struct trace_kprobe *tk) 255 { 256 int i; 257 258 if (!tk) 259 return; 260 261 for (i = 0; i < tk->tp.nr_args; i++) 262 traceprobe_free_probe_arg(&tk->tp.args[i]); 263 264 kfree(tk->tp.call.class->system); 265 kfree(tk->tp.call.name); 266 kfree(tk->symbol); 267 free_percpu(tk->nhit); 268 kfree(tk); 269 } 270 271 static struct trace_kprobe *find_trace_kprobe(const char *event, 272 const char *group) 273 { 274 struct dyn_event *pos; 275 struct trace_kprobe *tk; 276 277 for_each_trace_kprobe(tk, pos) 278 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 && 279 strcmp(tk->tp.call.class->system, group) == 0) 280 return tk; 281 return NULL; 282 } 283 284 static inline int __enable_trace_kprobe(struct trace_kprobe *tk) 285 { 286 int ret = 0; 287 288 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) { 289 if (trace_kprobe_is_return(tk)) 290 ret = enable_kretprobe(&tk->rp); 291 else 292 ret = enable_kprobe(&tk->rp.kp); 293 } 294 295 return ret; 296 } 297 298 /* 299 * Enable trace_probe 300 * if the file is NULL, enable "perf" handler, or enable "trace" handler. 301 */ 302 static int 303 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file) 304 { 305 struct event_file_link *link; 306 int ret = 0; 307 308 if (file) { 309 link = kmalloc(sizeof(*link), GFP_KERNEL); 310 if (!link) { 311 ret = -ENOMEM; 312 goto out; 313 } 314 315 link->file = file; 316 list_add_tail_rcu(&link->list, &tk->tp.files); 317 318 tk->tp.flags |= TP_FLAG_TRACE; 319 ret = __enable_trace_kprobe(tk); 320 if (ret) { 321 list_del_rcu(&link->list); 322 kfree(link); 323 tk->tp.flags &= ~TP_FLAG_TRACE; 324 } 325 326 } else { 327 tk->tp.flags |= TP_FLAG_PROFILE; 328 ret = __enable_trace_kprobe(tk); 329 if (ret) 330 tk->tp.flags &= ~TP_FLAG_PROFILE; 331 } 332 out: 333 return ret; 334 } 335 336 /* 337 * Disable trace_probe 338 * if the file is NULL, disable "perf" handler, or disable "trace" handler. 339 */ 340 static int 341 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file) 342 { 343 struct event_file_link *link = NULL; 344 int wait = 0; 345 int ret = 0; 346 347 if (file) { 348 link = find_event_file_link(&tk->tp, file); 349 if (!link) { 350 ret = -EINVAL; 351 goto out; 352 } 353 354 list_del_rcu(&link->list); 355 wait = 1; 356 if (!list_empty(&tk->tp.files)) 357 goto out; 358 359 tk->tp.flags &= ~TP_FLAG_TRACE; 360 } else 361 tk->tp.flags &= ~TP_FLAG_PROFILE; 362 363 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) { 364 if (trace_kprobe_is_return(tk)) 365 disable_kretprobe(&tk->rp); 366 else 367 disable_kprobe(&tk->rp.kp); 368 wait = 1; 369 } 370 371 /* 372 * if tk is not added to any list, it must be a local trace_kprobe 373 * created with perf_event_open. We don't need to wait for these 374 * trace_kprobes 375 */ 376 if (list_empty(&tk->devent.list)) 377 wait = 0; 378 out: 379 if (wait) { 380 /* 381 * Synchronize with kprobe_trace_func/kretprobe_trace_func 382 * to ensure disabled (all running handlers are finished). 383 * This is not only for kfree(), but also the caller, 384 * trace_remove_event_call() supposes it for releasing 385 * event_call related objects, which will be accessed in 386 * the kprobe_trace_func/kretprobe_trace_func. 387 */ 388 synchronize_rcu(); 389 kfree(link); /* Ignored if link == NULL */ 390 } 391 392 return ret; 393 } 394 395 #if defined(CONFIG_KPROBES_ON_FTRACE) && \ 396 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE) 397 static bool within_notrace_func(struct trace_kprobe *tk) 398 { 399 unsigned long offset, size, addr; 400 401 addr = trace_kprobe_address(tk); 402 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset)) 403 return false; 404 405 /* Get the entry address of the target function */ 406 addr -= offset; 407 408 /* 409 * Since ftrace_location_range() does inclusive range check, we need 410 * to subtract 1 byte from the end address. 411 */ 412 return !ftrace_location_range(addr, addr + size - 1); 413 } 414 #else 415 #define within_notrace_func(tk) (false) 416 #endif 417 418 /* Internal register function - just handle k*probes and flags */ 419 static int __register_trace_kprobe(struct trace_kprobe *tk) 420 { 421 int i, ret; 422 423 if (trace_probe_is_registered(&tk->tp)) 424 return -EINVAL; 425 426 if (within_notrace_func(tk)) { 427 pr_warn("Could not probe notrace function %s\n", 428 trace_kprobe_symbol(tk)); 429 return -EINVAL; 430 } 431 432 for (i = 0; i < tk->tp.nr_args; i++) { 433 ret = traceprobe_update_arg(&tk->tp.args[i]); 434 if (ret) 435 return ret; 436 } 437 438 /* Set/clear disabled flag according to tp->flag */ 439 if (trace_probe_is_enabled(&tk->tp)) 440 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED; 441 else 442 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED; 443 444 if (trace_kprobe_is_return(tk)) 445 ret = register_kretprobe(&tk->rp); 446 else 447 ret = register_kprobe(&tk->rp.kp); 448 449 if (ret == 0) { 450 tk->tp.flags |= TP_FLAG_REGISTERED; 451 } else if (ret == -EILSEQ) { 452 pr_warn("Probing address(0x%p) is not an instruction boundary.\n", 453 tk->rp.kp.addr); 454 ret = -EINVAL; 455 } 456 return ret; 457 } 458 459 /* Internal unregister function - just handle k*probes and flags */ 460 static void __unregister_trace_kprobe(struct trace_kprobe *tk) 461 { 462 if (trace_probe_is_registered(&tk->tp)) { 463 if (trace_kprobe_is_return(tk)) 464 unregister_kretprobe(&tk->rp); 465 else 466 unregister_kprobe(&tk->rp.kp); 467 tk->tp.flags &= ~TP_FLAG_REGISTERED; 468 /* Cleanup kprobe for reuse */ 469 if (tk->rp.kp.symbol_name) 470 tk->rp.kp.addr = NULL; 471 } 472 } 473 474 /* Unregister a trace_probe and probe_event */ 475 static int unregister_trace_kprobe(struct trace_kprobe *tk) 476 { 477 /* Enabled event can not be unregistered */ 478 if (trace_probe_is_enabled(&tk->tp)) 479 return -EBUSY; 480 481 /* Will fail if probe is being used by ftrace or perf */ 482 if (unregister_kprobe_event(tk)) 483 return -EBUSY; 484 485 __unregister_trace_kprobe(tk); 486 dyn_event_remove(&tk->devent); 487 488 return 0; 489 } 490 491 /* Register a trace_probe and probe_event */ 492 static int register_trace_kprobe(struct trace_kprobe *tk) 493 { 494 struct trace_kprobe *old_tk; 495 int ret; 496 497 mutex_lock(&event_mutex); 498 499 /* Delete old (same name) event if exist */ 500 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call), 501 tk->tp.call.class->system); 502 if (old_tk) { 503 ret = unregister_trace_kprobe(old_tk); 504 if (ret < 0) 505 goto end; 506 free_trace_kprobe(old_tk); 507 } 508 509 /* Register new event */ 510 ret = register_kprobe_event(tk); 511 if (ret) { 512 pr_warn("Failed to register probe event(%d)\n", ret); 513 goto end; 514 } 515 516 /* Register k*probe */ 517 ret = __register_trace_kprobe(tk); 518 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) { 519 pr_warn("This probe might be able to register after target module is loaded. Continue.\n"); 520 ret = 0; 521 } 522 523 if (ret < 0) 524 unregister_kprobe_event(tk); 525 else 526 dyn_event_add(&tk->devent); 527 528 end: 529 mutex_unlock(&event_mutex); 530 return ret; 531 } 532 533 /* Module notifier call back, checking event on the module */ 534 static int trace_kprobe_module_callback(struct notifier_block *nb, 535 unsigned long val, void *data) 536 { 537 struct module *mod = data; 538 struct dyn_event *pos; 539 struct trace_kprobe *tk; 540 int ret; 541 542 if (val != MODULE_STATE_COMING) 543 return NOTIFY_DONE; 544 545 /* Update probes on coming module */ 546 mutex_lock(&event_mutex); 547 for_each_trace_kprobe(tk, pos) { 548 if (trace_kprobe_within_module(tk, mod)) { 549 /* Don't need to check busy - this should have gone. */ 550 __unregister_trace_kprobe(tk); 551 ret = __register_trace_kprobe(tk); 552 if (ret) 553 pr_warn("Failed to re-register probe %s on %s: %d\n", 554 trace_event_name(&tk->tp.call), 555 mod->name, ret); 556 } 557 } 558 mutex_unlock(&event_mutex); 559 560 return NOTIFY_DONE; 561 } 562 563 static struct notifier_block trace_kprobe_module_nb = { 564 .notifier_call = trace_kprobe_module_callback, 565 .priority = 1 /* Invoked after kprobe module callback */ 566 }; 567 568 /* Convert certain expected symbols into '_' when generating event names */ 569 static inline void sanitize_event_name(char *name) 570 { 571 while (*name++ != '\0') 572 if (*name == ':' || *name == '.') 573 *name = '_'; 574 } 575 576 static int trace_kprobe_create(int argc, const char *argv[]) 577 { 578 /* 579 * Argument syntax: 580 * - Add kprobe: 581 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS] 582 * - Add kretprobe: 583 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS] 584 * Fetch args: 585 * $retval : fetch return value 586 * $stack : fetch stack address 587 * $stackN : fetch Nth of stack (N:0-) 588 * $comm : fetch current task comm 589 * @ADDR : fetch memory at ADDR (ADDR should be in kernel) 590 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) 591 * %REG : fetch register REG 592 * Dereferencing memory fetch: 593 * +|-offs(ARG) : fetch memory at ARG +|- offs address. 594 * Alias name of args: 595 * NAME=FETCHARG : set NAME as alias of FETCHARG. 596 * Type of args: 597 * FETCHARG:TYPE : use TYPE instead of unsigned long. 598 */ 599 struct trace_kprobe *tk; 600 int i, len, ret = 0; 601 bool is_return = false; 602 char *symbol = NULL, *tmp = NULL; 603 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM; 604 int maxactive = 0; 605 long offset = 0; 606 void *addr = NULL; 607 char buf[MAX_EVENT_NAME_LEN]; 608 unsigned int flags = TPARG_FL_KERNEL; 609 610 /* argc must be >= 1 */ 611 if (argv[0][0] == 'r') { 612 is_return = true; 613 flags |= TPARG_FL_RETURN; 614 } else if (argv[0][0] != 'p' || argc < 2) 615 return -ECANCELED; 616 617 event = strchr(&argv[0][1], ':'); 618 if (event) 619 event++; 620 621 if (is_return && isdigit(argv[0][1])) { 622 if (event) 623 len = event - &argv[0][1] - 1; 624 else 625 len = strlen(&argv[0][1]); 626 if (len > MAX_EVENT_NAME_LEN - 1) 627 return -E2BIG; 628 memcpy(buf, &argv[0][1], len); 629 buf[len] = '\0'; 630 ret = kstrtouint(buf, 0, &maxactive); 631 if (ret) { 632 pr_info("Failed to parse maxactive.\n"); 633 return ret; 634 } 635 /* kretprobes instances are iterated over via a list. The 636 * maximum should stay reasonable. 637 */ 638 if (maxactive > KRETPROBE_MAXACTIVE_MAX) { 639 pr_info("Maxactive is too big (%d > %d).\n", 640 maxactive, KRETPROBE_MAXACTIVE_MAX); 641 return -E2BIG; 642 } 643 } 644 645 /* try to parse an address. if that fails, try to read the 646 * input as a symbol. */ 647 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) { 648 /* Check whether uprobe event specified */ 649 if (strchr(argv[1], '/') && strchr(argv[1], ':')) 650 return -ECANCELED; 651 /* a symbol specified */ 652 symbol = kstrdup(argv[1], GFP_KERNEL); 653 if (!symbol) 654 return -ENOMEM; 655 /* TODO: support .init module functions */ 656 ret = traceprobe_split_symbol_offset(symbol, &offset); 657 if (ret || offset < 0 || offset > UINT_MAX) { 658 pr_info("Failed to parse either an address or a symbol.\n"); 659 goto out; 660 } 661 if (kprobe_on_func_entry(NULL, symbol, offset)) 662 flags |= TPARG_FL_FENTRY; 663 if (offset && is_return && !(flags & TPARG_FL_FENTRY)) { 664 pr_info("Given offset is not valid for return probe.\n"); 665 ret = -EINVAL; 666 goto out; 667 } 668 } 669 argc -= 2; argv += 2; 670 671 if (event) { 672 ret = traceprobe_parse_event_name(&event, &group, buf); 673 if (ret) 674 goto out; 675 } else { 676 /* Make a new event name */ 677 if (symbol) 678 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", 679 is_return ? 'r' : 'p', symbol, offset); 680 else 681 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", 682 is_return ? 'r' : 'p', addr); 683 sanitize_event_name(buf); 684 event = buf; 685 } 686 687 /* setup a probe */ 688 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive, 689 argc, is_return); 690 if (IS_ERR(tk)) { 691 pr_info("Failed to allocate trace_probe.(%d)\n", 692 (int)PTR_ERR(tk)); 693 ret = PTR_ERR(tk); 694 goto out; 695 } 696 697 /* parse arguments */ 698 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { 699 tmp = kstrdup(argv[i], GFP_KERNEL); 700 if (!tmp) { 701 ret = -ENOMEM; 702 goto error; 703 } 704 705 ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags); 706 kfree(tmp); 707 if (ret) 708 goto error; 709 } 710 711 ret = register_trace_kprobe(tk); 712 if (ret) 713 goto error; 714 out: 715 kfree(symbol); 716 return ret; 717 718 error: 719 free_trace_kprobe(tk); 720 goto out; 721 } 722 723 static int create_or_delete_trace_kprobe(int argc, char **argv) 724 { 725 int ret; 726 727 if (argv[0][0] == '-') 728 return dyn_event_release(argc, argv, &trace_kprobe_ops); 729 730 ret = trace_kprobe_create(argc, (const char **)argv); 731 return ret == -ECANCELED ? -EINVAL : ret; 732 } 733 734 static int trace_kprobe_release(struct dyn_event *ev) 735 { 736 struct trace_kprobe *tk = to_trace_kprobe(ev); 737 int ret = unregister_trace_kprobe(tk); 738 739 if (!ret) 740 free_trace_kprobe(tk); 741 return ret; 742 } 743 744 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev) 745 { 746 struct trace_kprobe *tk = to_trace_kprobe(ev); 747 int i; 748 749 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p'); 750 seq_printf(m, ":%s/%s", tk->tp.call.class->system, 751 trace_event_name(&tk->tp.call)); 752 753 if (!tk->symbol) 754 seq_printf(m, " 0x%p", tk->rp.kp.addr); 755 else if (tk->rp.kp.offset) 756 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk), 757 tk->rp.kp.offset); 758 else 759 seq_printf(m, " %s", trace_kprobe_symbol(tk)); 760 761 for (i = 0; i < tk->tp.nr_args; i++) 762 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm); 763 seq_putc(m, '\n'); 764 765 return 0; 766 } 767 768 static int probes_seq_show(struct seq_file *m, void *v) 769 { 770 struct dyn_event *ev = v; 771 772 if (!is_trace_kprobe(ev)) 773 return 0; 774 775 return trace_kprobe_show(m, ev); 776 } 777 778 static const struct seq_operations probes_seq_op = { 779 .start = dyn_event_seq_start, 780 .next = dyn_event_seq_next, 781 .stop = dyn_event_seq_stop, 782 .show = probes_seq_show 783 }; 784 785 static int probes_open(struct inode *inode, struct file *file) 786 { 787 int ret; 788 789 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { 790 ret = dyn_events_release_all(&trace_kprobe_ops); 791 if (ret < 0) 792 return ret; 793 } 794 795 return seq_open(file, &probes_seq_op); 796 } 797 798 static ssize_t probes_write(struct file *file, const char __user *buffer, 799 size_t count, loff_t *ppos) 800 { 801 return trace_parse_run_command(file, buffer, count, ppos, 802 create_or_delete_trace_kprobe); 803 } 804 805 static const struct file_operations kprobe_events_ops = { 806 .owner = THIS_MODULE, 807 .open = probes_open, 808 .read = seq_read, 809 .llseek = seq_lseek, 810 .release = seq_release, 811 .write = probes_write, 812 }; 813 814 /* Probes profiling interfaces */ 815 static int probes_profile_seq_show(struct seq_file *m, void *v) 816 { 817 struct dyn_event *ev = v; 818 struct trace_kprobe *tk; 819 820 if (!is_trace_kprobe(ev)) 821 return 0; 822 823 tk = to_trace_kprobe(ev); 824 seq_printf(m, " %-44s %15lu %15lu\n", 825 trace_event_name(&tk->tp.call), 826 trace_kprobe_nhit(tk), 827 tk->rp.kp.nmissed); 828 829 return 0; 830 } 831 832 static const struct seq_operations profile_seq_op = { 833 .start = dyn_event_seq_start, 834 .next = dyn_event_seq_next, 835 .stop = dyn_event_seq_stop, 836 .show = probes_profile_seq_show 837 }; 838 839 static int profile_open(struct inode *inode, struct file *file) 840 { 841 return seq_open(file, &profile_seq_op); 842 } 843 844 static const struct file_operations kprobe_profile_ops = { 845 .owner = THIS_MODULE, 846 .open = profile_open, 847 .read = seq_read, 848 .llseek = seq_lseek, 849 .release = seq_release, 850 }; 851 852 /* Kprobe specific fetch functions */ 853 854 /* Return the length of string -- including null terminal byte */ 855 static nokprobe_inline int 856 fetch_store_strlen(unsigned long addr) 857 { 858 mm_segment_t old_fs; 859 int ret, len = 0; 860 u8 c; 861 862 old_fs = get_fs(); 863 set_fs(KERNEL_DS); 864 pagefault_disable(); 865 866 do { 867 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1); 868 len++; 869 } while (c && ret == 0 && len < MAX_STRING_SIZE); 870 871 pagefault_enable(); 872 set_fs(old_fs); 873 874 return (ret < 0) ? ret : len; 875 } 876 877 /* 878 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max 879 * length and relative data location. 880 */ 881 static nokprobe_inline int 882 fetch_store_string(unsigned long addr, void *dest, void *base) 883 { 884 int maxlen = get_loc_len(*(u32 *)dest); 885 u8 *dst = get_loc_data(dest, base); 886 long ret; 887 888 if (unlikely(!maxlen)) 889 return -ENOMEM; 890 /* 891 * Try to get string again, since the string can be changed while 892 * probing. 893 */ 894 ret = strncpy_from_unsafe(dst, (void *)addr, maxlen); 895 896 if (ret >= 0) 897 *(u32 *)dest = make_data_loc(ret, (void *)dst - base); 898 return ret; 899 } 900 901 static nokprobe_inline int 902 probe_mem_read(void *dest, void *src, size_t size) 903 { 904 return probe_kernel_read(dest, src, size); 905 } 906 907 /* Note that we don't verify it, since the code does not come from user space */ 908 static int 909 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest, 910 void *base) 911 { 912 unsigned long val; 913 914 retry: 915 /* 1st stage: get value from context */ 916 switch (code->op) { 917 case FETCH_OP_REG: 918 val = regs_get_register(regs, code->param); 919 break; 920 case FETCH_OP_STACK: 921 val = regs_get_kernel_stack_nth(regs, code->param); 922 break; 923 case FETCH_OP_STACKP: 924 val = kernel_stack_pointer(regs); 925 break; 926 case FETCH_OP_RETVAL: 927 val = regs_return_value(regs); 928 break; 929 case FETCH_OP_IMM: 930 val = code->immediate; 931 break; 932 case FETCH_OP_COMM: 933 val = (unsigned long)current->comm; 934 break; 935 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 936 case FETCH_OP_ARG: 937 val = regs_get_kernel_argument(regs, code->param); 938 break; 939 #endif 940 case FETCH_NOP_SYMBOL: /* Ignore a place holder */ 941 code++; 942 goto retry; 943 default: 944 return -EILSEQ; 945 } 946 code++; 947 948 return process_fetch_insn_bottom(code, val, dest, base); 949 } 950 NOKPROBE_SYMBOL(process_fetch_insn) 951 952 /* Kprobe handler */ 953 static nokprobe_inline void 954 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs, 955 struct trace_event_file *trace_file) 956 { 957 struct kprobe_trace_entry_head *entry; 958 struct ring_buffer_event *event; 959 struct ring_buffer *buffer; 960 int size, dsize, pc; 961 unsigned long irq_flags; 962 struct trace_event_call *call = &tk->tp.call; 963 964 WARN_ON(call != trace_file->event_call); 965 966 if (trace_trigger_soft_disabled(trace_file)) 967 return; 968 969 local_save_flags(irq_flags); 970 pc = preempt_count(); 971 972 dsize = __get_data_size(&tk->tp, regs); 973 size = sizeof(*entry) + tk->tp.size + dsize; 974 975 event = trace_event_buffer_lock_reserve(&buffer, trace_file, 976 call->event.type, 977 size, irq_flags, pc); 978 if (!event) 979 return; 980 981 entry = ring_buffer_event_data(event); 982 entry->ip = (unsigned long)tk->rp.kp.addr; 983 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); 984 985 event_trigger_unlock_commit_regs(trace_file, buffer, event, 986 entry, irq_flags, pc, regs); 987 } 988 989 static void 990 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs) 991 { 992 struct event_file_link *link; 993 994 list_for_each_entry_rcu(link, &tk->tp.files, list) 995 __kprobe_trace_func(tk, regs, link->file); 996 } 997 NOKPROBE_SYMBOL(kprobe_trace_func); 998 999 /* Kretprobe handler */ 1000 static nokprobe_inline void 1001 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, 1002 struct pt_regs *regs, 1003 struct trace_event_file *trace_file) 1004 { 1005 struct kretprobe_trace_entry_head *entry; 1006 struct ring_buffer_event *event; 1007 struct ring_buffer *buffer; 1008 int size, pc, dsize; 1009 unsigned long irq_flags; 1010 struct trace_event_call *call = &tk->tp.call; 1011 1012 WARN_ON(call != trace_file->event_call); 1013 1014 if (trace_trigger_soft_disabled(trace_file)) 1015 return; 1016 1017 local_save_flags(irq_flags); 1018 pc = preempt_count(); 1019 1020 dsize = __get_data_size(&tk->tp, regs); 1021 size = sizeof(*entry) + tk->tp.size + dsize; 1022 1023 event = trace_event_buffer_lock_reserve(&buffer, trace_file, 1024 call->event.type, 1025 size, irq_flags, pc); 1026 if (!event) 1027 return; 1028 1029 entry = ring_buffer_event_data(event); 1030 entry->func = (unsigned long)tk->rp.kp.addr; 1031 entry->ret_ip = (unsigned long)ri->ret_addr; 1032 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); 1033 1034 event_trigger_unlock_commit_regs(trace_file, buffer, event, 1035 entry, irq_flags, pc, regs); 1036 } 1037 1038 static void 1039 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, 1040 struct pt_regs *regs) 1041 { 1042 struct event_file_link *link; 1043 1044 list_for_each_entry_rcu(link, &tk->tp.files, list) 1045 __kretprobe_trace_func(tk, ri, regs, link->file); 1046 } 1047 NOKPROBE_SYMBOL(kretprobe_trace_func); 1048 1049 /* Event entry printers */ 1050 static enum print_line_t 1051 print_kprobe_event(struct trace_iterator *iter, int flags, 1052 struct trace_event *event) 1053 { 1054 struct kprobe_trace_entry_head *field; 1055 struct trace_seq *s = &iter->seq; 1056 struct trace_probe *tp; 1057 1058 field = (struct kprobe_trace_entry_head *)iter->ent; 1059 tp = container_of(event, struct trace_probe, call.event); 1060 1061 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call)); 1062 1063 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) 1064 goto out; 1065 1066 trace_seq_putc(s, ')'); 1067 1068 if (print_probe_args(s, tp->args, tp->nr_args, 1069 (u8 *)&field[1], field) < 0) 1070 goto out; 1071 1072 trace_seq_putc(s, '\n'); 1073 out: 1074 return trace_handle_return(s); 1075 } 1076 1077 static enum print_line_t 1078 print_kretprobe_event(struct trace_iterator *iter, int flags, 1079 struct trace_event *event) 1080 { 1081 struct kretprobe_trace_entry_head *field; 1082 struct trace_seq *s = &iter->seq; 1083 struct trace_probe *tp; 1084 1085 field = (struct kretprobe_trace_entry_head *)iter->ent; 1086 tp = container_of(event, struct trace_probe, call.event); 1087 1088 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call)); 1089 1090 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) 1091 goto out; 1092 1093 trace_seq_puts(s, " <- "); 1094 1095 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) 1096 goto out; 1097 1098 trace_seq_putc(s, ')'); 1099 1100 if (print_probe_args(s, tp->args, tp->nr_args, 1101 (u8 *)&field[1], field) < 0) 1102 goto out; 1103 1104 trace_seq_putc(s, '\n'); 1105 1106 out: 1107 return trace_handle_return(s); 1108 } 1109 1110 1111 static int kprobe_event_define_fields(struct trace_event_call *event_call) 1112 { 1113 int ret; 1114 struct kprobe_trace_entry_head field; 1115 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; 1116 1117 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); 1118 1119 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp); 1120 } 1121 1122 static int kretprobe_event_define_fields(struct trace_event_call *event_call) 1123 { 1124 int ret; 1125 struct kretprobe_trace_entry_head field; 1126 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data; 1127 1128 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); 1129 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); 1130 1131 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp); 1132 } 1133 1134 #ifdef CONFIG_PERF_EVENTS 1135 1136 /* Kprobe profile handler */ 1137 static int 1138 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) 1139 { 1140 struct trace_event_call *call = &tk->tp.call; 1141 struct kprobe_trace_entry_head *entry; 1142 struct hlist_head *head; 1143 int size, __size, dsize; 1144 int rctx; 1145 1146 if (bpf_prog_array_valid(call)) { 1147 unsigned long orig_ip = instruction_pointer(regs); 1148 int ret; 1149 1150 ret = trace_call_bpf(call, regs); 1151 1152 /* 1153 * We need to check and see if we modified the pc of the 1154 * pt_regs, and if so return 1 so that we don't do the 1155 * single stepping. 1156 */ 1157 if (orig_ip != instruction_pointer(regs)) 1158 return 1; 1159 if (!ret) 1160 return 0; 1161 } 1162 1163 head = this_cpu_ptr(call->perf_events); 1164 if (hlist_empty(head)) 1165 return 0; 1166 1167 dsize = __get_data_size(&tk->tp, regs); 1168 __size = sizeof(*entry) + tk->tp.size + dsize; 1169 size = ALIGN(__size + sizeof(u32), sizeof(u64)); 1170 size -= sizeof(u32); 1171 1172 entry = perf_trace_buf_alloc(size, NULL, &rctx); 1173 if (!entry) 1174 return 0; 1175 1176 entry->ip = (unsigned long)tk->rp.kp.addr; 1177 memset(&entry[1], 0, dsize); 1178 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); 1179 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, 1180 head, NULL); 1181 return 0; 1182 } 1183 NOKPROBE_SYMBOL(kprobe_perf_func); 1184 1185 /* Kretprobe profile handler */ 1186 static void 1187 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, 1188 struct pt_regs *regs) 1189 { 1190 struct trace_event_call *call = &tk->tp.call; 1191 struct kretprobe_trace_entry_head *entry; 1192 struct hlist_head *head; 1193 int size, __size, dsize; 1194 int rctx; 1195 1196 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs)) 1197 return; 1198 1199 head = this_cpu_ptr(call->perf_events); 1200 if (hlist_empty(head)) 1201 return; 1202 1203 dsize = __get_data_size(&tk->tp, regs); 1204 __size = sizeof(*entry) + tk->tp.size + dsize; 1205 size = ALIGN(__size + sizeof(u32), sizeof(u64)); 1206 size -= sizeof(u32); 1207 1208 entry = perf_trace_buf_alloc(size, NULL, &rctx); 1209 if (!entry) 1210 return; 1211 1212 entry->func = (unsigned long)tk->rp.kp.addr; 1213 entry->ret_ip = (unsigned long)ri->ret_addr; 1214 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize); 1215 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, 1216 head, NULL); 1217 } 1218 NOKPROBE_SYMBOL(kretprobe_perf_func); 1219 1220 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type, 1221 const char **symbol, u64 *probe_offset, 1222 u64 *probe_addr, bool perf_type_tracepoint) 1223 { 1224 const char *pevent = trace_event_name(event->tp_event); 1225 const char *group = event->tp_event->class->system; 1226 struct trace_kprobe *tk; 1227 1228 if (perf_type_tracepoint) 1229 tk = find_trace_kprobe(pevent, group); 1230 else 1231 tk = event->tp_event->data; 1232 if (!tk) 1233 return -EINVAL; 1234 1235 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE 1236 : BPF_FD_TYPE_KPROBE; 1237 if (tk->symbol) { 1238 *symbol = tk->symbol; 1239 *probe_offset = tk->rp.kp.offset; 1240 *probe_addr = 0; 1241 } else { 1242 *symbol = NULL; 1243 *probe_offset = 0; 1244 *probe_addr = (unsigned long)tk->rp.kp.addr; 1245 } 1246 return 0; 1247 } 1248 #endif /* CONFIG_PERF_EVENTS */ 1249 1250 /* 1251 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex. 1252 * 1253 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe 1254 * lockless, but we can't race with this __init function. 1255 */ 1256 static int kprobe_register(struct trace_event_call *event, 1257 enum trace_reg type, void *data) 1258 { 1259 struct trace_kprobe *tk = (struct trace_kprobe *)event->data; 1260 struct trace_event_file *file = data; 1261 1262 switch (type) { 1263 case TRACE_REG_REGISTER: 1264 return enable_trace_kprobe(tk, file); 1265 case TRACE_REG_UNREGISTER: 1266 return disable_trace_kprobe(tk, file); 1267 1268 #ifdef CONFIG_PERF_EVENTS 1269 case TRACE_REG_PERF_REGISTER: 1270 return enable_trace_kprobe(tk, NULL); 1271 case TRACE_REG_PERF_UNREGISTER: 1272 return disable_trace_kprobe(tk, NULL); 1273 case TRACE_REG_PERF_OPEN: 1274 case TRACE_REG_PERF_CLOSE: 1275 case TRACE_REG_PERF_ADD: 1276 case TRACE_REG_PERF_DEL: 1277 return 0; 1278 #endif 1279 } 1280 return 0; 1281 } 1282 1283 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) 1284 { 1285 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp); 1286 int ret = 0; 1287 1288 raw_cpu_inc(*tk->nhit); 1289 1290 if (tk->tp.flags & TP_FLAG_TRACE) 1291 kprobe_trace_func(tk, regs); 1292 #ifdef CONFIG_PERF_EVENTS 1293 if (tk->tp.flags & TP_FLAG_PROFILE) 1294 ret = kprobe_perf_func(tk, regs); 1295 #endif 1296 return ret; 1297 } 1298 NOKPROBE_SYMBOL(kprobe_dispatcher); 1299 1300 static int 1301 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) 1302 { 1303 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp); 1304 1305 raw_cpu_inc(*tk->nhit); 1306 1307 if (tk->tp.flags & TP_FLAG_TRACE) 1308 kretprobe_trace_func(tk, ri, regs); 1309 #ifdef CONFIG_PERF_EVENTS 1310 if (tk->tp.flags & TP_FLAG_PROFILE) 1311 kretprobe_perf_func(tk, ri, regs); 1312 #endif 1313 return 0; /* We don't tweek kernel, so just return 0 */ 1314 } 1315 NOKPROBE_SYMBOL(kretprobe_dispatcher); 1316 1317 static struct trace_event_functions kretprobe_funcs = { 1318 .trace = print_kretprobe_event 1319 }; 1320 1321 static struct trace_event_functions kprobe_funcs = { 1322 .trace = print_kprobe_event 1323 }; 1324 1325 static inline void init_trace_event_call(struct trace_kprobe *tk, 1326 struct trace_event_call *call) 1327 { 1328 INIT_LIST_HEAD(&call->class->fields); 1329 if (trace_kprobe_is_return(tk)) { 1330 call->event.funcs = &kretprobe_funcs; 1331 call->class->define_fields = kretprobe_event_define_fields; 1332 } else { 1333 call->event.funcs = &kprobe_funcs; 1334 call->class->define_fields = kprobe_event_define_fields; 1335 } 1336 1337 call->flags = TRACE_EVENT_FL_KPROBE; 1338 call->class->reg = kprobe_register; 1339 call->data = tk; 1340 } 1341 1342 static int register_kprobe_event(struct trace_kprobe *tk) 1343 { 1344 struct trace_event_call *call = &tk->tp.call; 1345 int ret = 0; 1346 1347 init_trace_event_call(tk, call); 1348 1349 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) 1350 return -ENOMEM; 1351 ret = register_trace_event(&call->event); 1352 if (!ret) { 1353 kfree(call->print_fmt); 1354 return -ENODEV; 1355 } 1356 ret = trace_add_event_call(call); 1357 if (ret) { 1358 pr_info("Failed to register kprobe event: %s\n", 1359 trace_event_name(call)); 1360 kfree(call->print_fmt); 1361 unregister_trace_event(&call->event); 1362 } 1363 return ret; 1364 } 1365 1366 static int unregister_kprobe_event(struct trace_kprobe *tk) 1367 { 1368 int ret; 1369 1370 /* tp->event is unregistered in trace_remove_event_call() */ 1371 ret = trace_remove_event_call(&tk->tp.call); 1372 if (!ret) 1373 kfree(tk->tp.call.print_fmt); 1374 return ret; 1375 } 1376 1377 #ifdef CONFIG_PERF_EVENTS 1378 /* create a trace_kprobe, but don't add it to global lists */ 1379 struct trace_event_call * 1380 create_local_trace_kprobe(char *func, void *addr, unsigned long offs, 1381 bool is_return) 1382 { 1383 struct trace_kprobe *tk; 1384 int ret; 1385 char *event; 1386 1387 /* 1388 * local trace_kprobes are not added to dyn_event, so they are never 1389 * searched in find_trace_kprobe(). Therefore, there is no concern of 1390 * duplicated name here. 1391 */ 1392 event = func ? func : "DUMMY_EVENT"; 1393 1394 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func, 1395 offs, 0 /* maxactive */, 0 /* nargs */, 1396 is_return); 1397 1398 if (IS_ERR(tk)) { 1399 pr_info("Failed to allocate trace_probe.(%d)\n", 1400 (int)PTR_ERR(tk)); 1401 return ERR_CAST(tk); 1402 } 1403 1404 init_trace_event_call(tk, &tk->tp.call); 1405 1406 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) { 1407 ret = -ENOMEM; 1408 goto error; 1409 } 1410 1411 ret = __register_trace_kprobe(tk); 1412 if (ret < 0) { 1413 kfree(tk->tp.call.print_fmt); 1414 goto error; 1415 } 1416 1417 return &tk->tp.call; 1418 error: 1419 free_trace_kprobe(tk); 1420 return ERR_PTR(ret); 1421 } 1422 1423 void destroy_local_trace_kprobe(struct trace_event_call *event_call) 1424 { 1425 struct trace_kprobe *tk; 1426 1427 tk = container_of(event_call, struct trace_kprobe, tp.call); 1428 1429 if (trace_probe_is_enabled(&tk->tp)) { 1430 WARN_ON(1); 1431 return; 1432 } 1433 1434 __unregister_trace_kprobe(tk); 1435 1436 kfree(tk->tp.call.print_fmt); 1437 free_trace_kprobe(tk); 1438 } 1439 #endif /* CONFIG_PERF_EVENTS */ 1440 1441 /* Make a tracefs interface for controlling probe points */ 1442 static __init int init_kprobe_trace(void) 1443 { 1444 struct dentry *d_tracer; 1445 struct dentry *entry; 1446 int ret; 1447 1448 ret = dyn_event_register(&trace_kprobe_ops); 1449 if (ret) 1450 return ret; 1451 1452 if (register_module_notifier(&trace_kprobe_module_nb)) 1453 return -EINVAL; 1454 1455 d_tracer = tracing_init_dentry(); 1456 if (IS_ERR(d_tracer)) 1457 return 0; 1458 1459 entry = tracefs_create_file("kprobe_events", 0644, d_tracer, 1460 NULL, &kprobe_events_ops); 1461 1462 /* Event list interface */ 1463 if (!entry) 1464 pr_warn("Could not create tracefs 'kprobe_events' entry\n"); 1465 1466 /* Profile interface */ 1467 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer, 1468 NULL, &kprobe_profile_ops); 1469 1470 if (!entry) 1471 pr_warn("Could not create tracefs 'kprobe_profile' entry\n"); 1472 return 0; 1473 } 1474 fs_initcall(init_kprobe_trace); 1475 1476 1477 #ifdef CONFIG_FTRACE_STARTUP_TEST 1478 static __init struct trace_event_file * 1479 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr) 1480 { 1481 struct trace_event_file *file; 1482 1483 list_for_each_entry(file, &tr->events, list) 1484 if (file->event_call == &tk->tp.call) 1485 return file; 1486 1487 return NULL; 1488 } 1489 1490 /* 1491 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this 1492 * stage, we can do this lockless. 1493 */ 1494 static __init int kprobe_trace_self_tests_init(void) 1495 { 1496 int ret, warn = 0; 1497 int (*target)(int, int, int, int, int, int); 1498 struct trace_kprobe *tk; 1499 struct trace_event_file *file; 1500 1501 if (tracing_is_disabled()) 1502 return -ENODEV; 1503 1504 target = kprobe_trace_selftest_target; 1505 1506 pr_info("Testing kprobe tracing: "); 1507 1508 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)", 1509 create_or_delete_trace_kprobe); 1510 if (WARN_ON_ONCE(ret)) { 1511 pr_warn("error on probing function entry.\n"); 1512 warn++; 1513 } else { 1514 /* Enable trace point */ 1515 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); 1516 if (WARN_ON_ONCE(tk == NULL)) { 1517 pr_warn("error on getting new probe.\n"); 1518 warn++; 1519 } else { 1520 file = find_trace_probe_file(tk, top_trace_array()); 1521 if (WARN_ON_ONCE(file == NULL)) { 1522 pr_warn("error on getting probe file.\n"); 1523 warn++; 1524 } else 1525 enable_trace_kprobe(tk, file); 1526 } 1527 } 1528 1529 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval", 1530 create_or_delete_trace_kprobe); 1531 if (WARN_ON_ONCE(ret)) { 1532 pr_warn("error on probing function return.\n"); 1533 warn++; 1534 } else { 1535 /* Enable trace point */ 1536 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); 1537 if (WARN_ON_ONCE(tk == NULL)) { 1538 pr_warn("error on getting 2nd new probe.\n"); 1539 warn++; 1540 } else { 1541 file = find_trace_probe_file(tk, top_trace_array()); 1542 if (WARN_ON_ONCE(file == NULL)) { 1543 pr_warn("error on getting probe file.\n"); 1544 warn++; 1545 } else 1546 enable_trace_kprobe(tk, file); 1547 } 1548 } 1549 1550 if (warn) 1551 goto end; 1552 1553 ret = target(1, 2, 3, 4, 5, 6); 1554 1555 /* 1556 * Not expecting an error here, the check is only to prevent the 1557 * optimizer from removing the call to target() as otherwise there 1558 * are no side-effects and the call is never performed. 1559 */ 1560 if (ret != 21) 1561 warn++; 1562 1563 /* Disable trace points before removing it */ 1564 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM); 1565 if (WARN_ON_ONCE(tk == NULL)) { 1566 pr_warn("error on getting test probe.\n"); 1567 warn++; 1568 } else { 1569 if (trace_kprobe_nhit(tk) != 1) { 1570 pr_warn("incorrect number of testprobe hits\n"); 1571 warn++; 1572 } 1573 1574 file = find_trace_probe_file(tk, top_trace_array()); 1575 if (WARN_ON_ONCE(file == NULL)) { 1576 pr_warn("error on getting probe file.\n"); 1577 warn++; 1578 } else 1579 disable_trace_kprobe(tk, file); 1580 } 1581 1582 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM); 1583 if (WARN_ON_ONCE(tk == NULL)) { 1584 pr_warn("error on getting 2nd test probe.\n"); 1585 warn++; 1586 } else { 1587 if (trace_kprobe_nhit(tk) != 1) { 1588 pr_warn("incorrect number of testprobe2 hits\n"); 1589 warn++; 1590 } 1591 1592 file = find_trace_probe_file(tk, top_trace_array()); 1593 if (WARN_ON_ONCE(file == NULL)) { 1594 pr_warn("error on getting probe file.\n"); 1595 warn++; 1596 } else 1597 disable_trace_kprobe(tk, file); 1598 } 1599 1600 ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe); 1601 if (WARN_ON_ONCE(ret)) { 1602 pr_warn("error on deleting a probe.\n"); 1603 warn++; 1604 } 1605 1606 ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe); 1607 if (WARN_ON_ONCE(ret)) { 1608 pr_warn("error on deleting a probe.\n"); 1609 warn++; 1610 } 1611 1612 end: 1613 ret = dyn_events_release_all(&trace_kprobe_ops); 1614 if (WARN_ON_ONCE(ret)) { 1615 pr_warn("error on cleaning up probes.\n"); 1616 warn++; 1617 } 1618 /* 1619 * Wait for the optimizer work to finish. Otherwise it might fiddle 1620 * with probes in already freed __init text. 1621 */ 1622 wait_for_kprobe_optimizer(); 1623 if (warn) 1624 pr_cont("NG: Some tests are failed. Please check them.\n"); 1625 else 1626 pr_cont("OK\n"); 1627 return 0; 1628 } 1629 1630 late_initcall(kprobe_trace_self_tests_init); 1631 1632 #endif 1633