1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Fprobe-based tracing events 4 * Copyright (C) 2022 Google LLC. 5 */ 6 #define pr_fmt(fmt) "trace_fprobe: " fmt 7 #include <asm/ptrace.h> 8 9 #include <linux/fprobe.h> 10 #include <linux/module.h> 11 #include <linux/rculist.h> 12 #include <linux/security.h> 13 #include <linux/tracepoint.h> 14 #include <linux/uaccess.h> 15 16 #include "trace_dynevent.h" 17 #include "trace_probe.h" 18 #include "trace_probe_kernel.h" 19 #include "trace_probe_tmpl.h" 20 21 #define FPROBE_EVENT_SYSTEM "fprobes" 22 #define TRACEPOINT_EVENT_SYSTEM "tracepoints" 23 #define RETHOOK_MAXACTIVE_MAX 4096 24 #define TRACEPOINT_STUB ERR_PTR(-ENOENT) 25 26 static int trace_fprobe_create(const char *raw_command); 27 static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev); 28 static int trace_fprobe_release(struct dyn_event *ev); 29 static bool trace_fprobe_is_busy(struct dyn_event *ev); 30 static bool trace_fprobe_match(const char *system, const char *event, 31 int argc, const char **argv, struct dyn_event *ev); 32 33 static struct dyn_event_operations trace_fprobe_ops = { 34 .create = trace_fprobe_create, 35 .show = trace_fprobe_show, 36 .is_busy = trace_fprobe_is_busy, 37 .free = trace_fprobe_release, 38 .match = trace_fprobe_match, 39 }; 40 41 /* 42 * Fprobe event core functions 43 */ 44 struct trace_fprobe { 45 struct dyn_event devent; 46 struct fprobe fp; 47 const char *symbol; 48 struct tracepoint *tpoint; 49 struct module *mod; 50 struct trace_probe tp; 51 }; 52 53 static bool is_trace_fprobe(struct dyn_event *ev) 54 { 55 return ev->ops == &trace_fprobe_ops; 56 } 57 58 static struct trace_fprobe *to_trace_fprobe(struct dyn_event *ev) 59 { 60 return container_of(ev, struct trace_fprobe, devent); 61 } 62 63 /** 64 * for_each_trace_fprobe - iterate over the trace_fprobe list 65 * @pos: the struct trace_fprobe * for each entry 66 * @dpos: the struct dyn_event * to use as a loop cursor 67 */ 68 #define for_each_trace_fprobe(pos, dpos) \ 69 for_each_dyn_event(dpos) \ 70 if (is_trace_fprobe(dpos) && (pos = to_trace_fprobe(dpos))) 71 72 static bool trace_fprobe_is_return(struct trace_fprobe *tf) 73 { 74 return tf->fp.exit_handler != NULL; 75 } 76 77 static bool trace_fprobe_is_tracepoint(struct trace_fprobe *tf) 78 { 79 return tf->tpoint != NULL; 80 } 81 82 static const char *trace_fprobe_symbol(struct trace_fprobe *tf) 83 { 84 return tf->symbol ? tf->symbol : "unknown"; 85 } 86 87 static bool trace_fprobe_is_busy(struct dyn_event *ev) 88 { 89 struct trace_fprobe *tf = to_trace_fprobe(ev); 90 91 return trace_probe_is_enabled(&tf->tp); 92 } 93 94 static bool trace_fprobe_match_command_head(struct trace_fprobe *tf, 95 int argc, const char **argv) 96 { 97 char buf[MAX_ARGSTR_LEN + 1]; 98 99 if (!argc) 100 return true; 101 102 snprintf(buf, sizeof(buf), "%s", trace_fprobe_symbol(tf)); 103 if (strcmp(buf, argv[0])) 104 return false; 105 argc--; argv++; 106 107 return trace_probe_match_command_args(&tf->tp, argc, argv); 108 } 109 110 static bool trace_fprobe_match(const char *system, const char *event, 111 int argc, const char **argv, struct dyn_event *ev) 112 { 113 struct trace_fprobe *tf = to_trace_fprobe(ev); 114 115 if (event[0] != '\0' && strcmp(trace_probe_name(&tf->tp), event)) 116 return false; 117 118 if (system && strcmp(trace_probe_group_name(&tf->tp), system)) 119 return false; 120 121 return trace_fprobe_match_command_head(tf, argc, argv); 122 } 123 124 static bool trace_fprobe_is_registered(struct trace_fprobe *tf) 125 { 126 return fprobe_is_registered(&tf->fp); 127 } 128 129 /* 130 * Note that we don't verify the fetch_insn code, since it does not come 131 * from user space. 132 */ 133 static int 134 process_fetch_insn(struct fetch_insn *code, void *rec, void *edata, 135 void *dest, void *base) 136 { 137 struct pt_regs *regs = rec; 138 unsigned long val; 139 int ret; 140 141 retry: 142 /* 1st stage: get value from context */ 143 switch (code->op) { 144 case FETCH_OP_STACK: 145 val = regs_get_kernel_stack_nth(regs, code->param); 146 break; 147 case FETCH_OP_STACKP: 148 val = kernel_stack_pointer(regs); 149 break; 150 case FETCH_OP_RETVAL: 151 val = regs_return_value(regs); 152 break; 153 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 154 case FETCH_OP_ARG: 155 val = regs_get_kernel_argument(regs, code->param); 156 break; 157 case FETCH_OP_EDATA: 158 val = *(unsigned long *)((unsigned long)edata + code->offset); 159 break; 160 #endif 161 case FETCH_NOP_SYMBOL: /* Ignore a place holder */ 162 code++; 163 goto retry; 164 default: 165 ret = process_common_fetch_insn(code, &val); 166 if (ret < 0) 167 return ret; 168 } 169 code++; 170 171 return process_fetch_insn_bottom(code, val, dest, base); 172 } 173 NOKPROBE_SYMBOL(process_fetch_insn) 174 175 /* function entry handler */ 176 static nokprobe_inline void 177 __fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, 178 struct pt_regs *regs, 179 struct trace_event_file *trace_file) 180 { 181 struct fentry_trace_entry_head *entry; 182 struct trace_event_call *call = trace_probe_event_call(&tf->tp); 183 struct trace_event_buffer fbuffer; 184 int dsize; 185 186 if (WARN_ON_ONCE(call != trace_file->event_call)) 187 return; 188 189 if (trace_trigger_soft_disabled(trace_file)) 190 return; 191 192 dsize = __get_data_size(&tf->tp, regs, NULL); 193 194 entry = trace_event_buffer_reserve(&fbuffer, trace_file, 195 sizeof(*entry) + tf->tp.size + dsize); 196 if (!entry) 197 return; 198 199 fbuffer.regs = regs; 200 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event); 201 entry->ip = entry_ip; 202 store_trace_args(&entry[1], &tf->tp, regs, NULL, sizeof(*entry), dsize); 203 204 trace_event_buffer_commit(&fbuffer); 205 } 206 207 static void 208 fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, 209 struct pt_regs *regs) 210 { 211 struct event_file_link *link; 212 213 trace_probe_for_each_link_rcu(link, &tf->tp) 214 __fentry_trace_func(tf, entry_ip, regs, link->file); 215 } 216 NOKPROBE_SYMBOL(fentry_trace_func); 217 218 /* function exit handler */ 219 static int trace_fprobe_entry_handler(struct fprobe *fp, unsigned long entry_ip, 220 unsigned long ret_ip, struct ftrace_regs *fregs, 221 void *entry_data) 222 { 223 struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp); 224 struct pt_regs *regs = ftrace_get_regs(fregs); 225 226 if (regs && tf->tp.entry_arg) 227 store_trace_entry_data(entry_data, &tf->tp, regs); 228 229 return 0; 230 } 231 NOKPROBE_SYMBOL(trace_fprobe_entry_handler) 232 233 static nokprobe_inline void 234 __fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, 235 unsigned long ret_ip, struct pt_regs *regs, 236 void *entry_data, struct trace_event_file *trace_file) 237 { 238 struct fexit_trace_entry_head *entry; 239 struct trace_event_buffer fbuffer; 240 struct trace_event_call *call = trace_probe_event_call(&tf->tp); 241 int dsize; 242 243 if (WARN_ON_ONCE(call != trace_file->event_call)) 244 return; 245 246 if (trace_trigger_soft_disabled(trace_file)) 247 return; 248 249 dsize = __get_data_size(&tf->tp, regs, entry_data); 250 251 entry = trace_event_buffer_reserve(&fbuffer, trace_file, 252 sizeof(*entry) + tf->tp.size + dsize); 253 if (!entry) 254 return; 255 256 fbuffer.regs = regs; 257 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event); 258 entry->func = entry_ip; 259 entry->ret_ip = ret_ip; 260 store_trace_args(&entry[1], &tf->tp, regs, entry_data, sizeof(*entry), dsize); 261 262 trace_event_buffer_commit(&fbuffer); 263 } 264 265 static void 266 fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, 267 unsigned long ret_ip, struct pt_regs *regs, void *entry_data) 268 { 269 struct event_file_link *link; 270 271 trace_probe_for_each_link_rcu(link, &tf->tp) 272 __fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data, link->file); 273 } 274 NOKPROBE_SYMBOL(fexit_trace_func); 275 276 #ifdef CONFIG_PERF_EVENTS 277 278 static int fentry_perf_func(struct trace_fprobe *tf, unsigned long entry_ip, 279 struct pt_regs *regs) 280 { 281 struct trace_event_call *call = trace_probe_event_call(&tf->tp); 282 struct fentry_trace_entry_head *entry; 283 struct hlist_head *head; 284 int size, __size, dsize; 285 int rctx; 286 287 head = this_cpu_ptr(call->perf_events); 288 if (hlist_empty(head)) 289 return 0; 290 291 dsize = __get_data_size(&tf->tp, regs, NULL); 292 __size = sizeof(*entry) + tf->tp.size + dsize; 293 size = ALIGN(__size + sizeof(u32), sizeof(u64)); 294 size -= sizeof(u32); 295 296 entry = perf_trace_buf_alloc(size, NULL, &rctx); 297 if (!entry) 298 return 0; 299 300 entry->ip = entry_ip; 301 memset(&entry[1], 0, dsize); 302 store_trace_args(&entry[1], &tf->tp, regs, NULL, sizeof(*entry), dsize); 303 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, 304 head, NULL); 305 return 0; 306 } 307 NOKPROBE_SYMBOL(fentry_perf_func); 308 309 static void 310 fexit_perf_func(struct trace_fprobe *tf, unsigned long entry_ip, 311 unsigned long ret_ip, struct pt_regs *regs, 312 void *entry_data) 313 { 314 struct trace_event_call *call = trace_probe_event_call(&tf->tp); 315 struct fexit_trace_entry_head *entry; 316 struct hlist_head *head; 317 int size, __size, dsize; 318 int rctx; 319 320 head = this_cpu_ptr(call->perf_events); 321 if (hlist_empty(head)) 322 return; 323 324 dsize = __get_data_size(&tf->tp, regs, entry_data); 325 __size = sizeof(*entry) + tf->tp.size + dsize; 326 size = ALIGN(__size + sizeof(u32), sizeof(u64)); 327 size -= sizeof(u32); 328 329 entry = perf_trace_buf_alloc(size, NULL, &rctx); 330 if (!entry) 331 return; 332 333 entry->func = entry_ip; 334 entry->ret_ip = ret_ip; 335 store_trace_args(&entry[1], &tf->tp, regs, entry_data, sizeof(*entry), dsize); 336 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, 337 head, NULL); 338 } 339 NOKPROBE_SYMBOL(fexit_perf_func); 340 #endif /* CONFIG_PERF_EVENTS */ 341 342 static int fentry_dispatcher(struct fprobe *fp, unsigned long entry_ip, 343 unsigned long ret_ip, struct ftrace_regs *fregs, 344 void *entry_data) 345 { 346 struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp); 347 struct pt_regs *regs = ftrace_get_regs(fregs); 348 int ret = 0; 349 350 if (!regs) 351 return 0; 352 353 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) 354 fentry_trace_func(tf, entry_ip, regs); 355 #ifdef CONFIG_PERF_EVENTS 356 if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE)) 357 ret = fentry_perf_func(tf, entry_ip, regs); 358 #endif 359 return ret; 360 } 361 NOKPROBE_SYMBOL(fentry_dispatcher); 362 363 static void fexit_dispatcher(struct fprobe *fp, unsigned long entry_ip, 364 unsigned long ret_ip, struct pt_regs *regs, 365 void *entry_data) 366 { 367 struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp); 368 369 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) 370 fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data); 371 #ifdef CONFIG_PERF_EVENTS 372 if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE)) 373 fexit_perf_func(tf, entry_ip, ret_ip, regs, entry_data); 374 #endif 375 } 376 NOKPROBE_SYMBOL(fexit_dispatcher); 377 378 static void free_trace_fprobe(struct trace_fprobe *tf) 379 { 380 if (tf) { 381 trace_probe_cleanup(&tf->tp); 382 kfree(tf->symbol); 383 kfree(tf); 384 } 385 } 386 387 /* 388 * Allocate new trace_probe and initialize it (including fprobe). 389 */ 390 static struct trace_fprobe *alloc_trace_fprobe(const char *group, 391 const char *event, 392 const char *symbol, 393 struct tracepoint *tpoint, 394 struct module *mod, 395 int maxactive, 396 int nargs, bool is_return) 397 { 398 struct trace_fprobe *tf; 399 int ret = -ENOMEM; 400 401 tf = kzalloc(struct_size(tf, tp.args, nargs), GFP_KERNEL); 402 if (!tf) 403 return ERR_PTR(ret); 404 405 tf->symbol = kstrdup(symbol, GFP_KERNEL); 406 if (!tf->symbol) 407 goto error; 408 409 if (is_return) 410 tf->fp.exit_handler = fexit_dispatcher; 411 else 412 tf->fp.entry_handler = fentry_dispatcher; 413 414 tf->tpoint = tpoint; 415 tf->mod = mod; 416 tf->fp.nr_maxactive = maxactive; 417 418 ret = trace_probe_init(&tf->tp, event, group, false, nargs); 419 if (ret < 0) 420 goto error; 421 422 dyn_event_init(&tf->devent, &trace_fprobe_ops); 423 return tf; 424 error: 425 free_trace_fprobe(tf); 426 return ERR_PTR(ret); 427 } 428 429 static struct trace_fprobe *find_trace_fprobe(const char *event, 430 const char *group) 431 { 432 struct dyn_event *pos; 433 struct trace_fprobe *tf; 434 435 for_each_trace_fprobe(tf, pos) 436 if (strcmp(trace_probe_name(&tf->tp), event) == 0 && 437 strcmp(trace_probe_group_name(&tf->tp), group) == 0) 438 return tf; 439 return NULL; 440 } 441 442 static inline int __enable_trace_fprobe(struct trace_fprobe *tf) 443 { 444 if (trace_fprobe_is_registered(tf)) 445 enable_fprobe(&tf->fp); 446 447 return 0; 448 } 449 450 static void __disable_trace_fprobe(struct trace_probe *tp) 451 { 452 struct trace_fprobe *tf; 453 454 list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { 455 if (!trace_fprobe_is_registered(tf)) 456 continue; 457 disable_fprobe(&tf->fp); 458 } 459 } 460 461 /* 462 * Enable trace_probe 463 * if the file is NULL, enable "perf" handler, or enable "trace" handler. 464 */ 465 static int enable_trace_fprobe(struct trace_event_call *call, 466 struct trace_event_file *file) 467 { 468 struct trace_probe *tp; 469 struct trace_fprobe *tf; 470 bool enabled; 471 int ret = 0; 472 473 tp = trace_probe_primary_from_call(call); 474 if (WARN_ON_ONCE(!tp)) 475 return -ENODEV; 476 enabled = trace_probe_is_enabled(tp); 477 478 /* This also changes "enabled" state */ 479 if (file) { 480 ret = trace_probe_add_file(tp, file); 481 if (ret) 482 return ret; 483 } else 484 trace_probe_set_flag(tp, TP_FLAG_PROFILE); 485 486 if (!enabled) { 487 list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { 488 /* TODO: check the fprobe is gone */ 489 __enable_trace_fprobe(tf); 490 } 491 } 492 493 return 0; 494 } 495 496 /* 497 * Disable trace_probe 498 * if the file is NULL, disable "perf" handler, or disable "trace" handler. 499 */ 500 static int disable_trace_fprobe(struct trace_event_call *call, 501 struct trace_event_file *file) 502 { 503 struct trace_probe *tp; 504 505 tp = trace_probe_primary_from_call(call); 506 if (WARN_ON_ONCE(!tp)) 507 return -ENODEV; 508 509 if (file) { 510 if (!trace_probe_get_file_link(tp, file)) 511 return -ENOENT; 512 if (!trace_probe_has_single_file(tp)) 513 goto out; 514 trace_probe_clear_flag(tp, TP_FLAG_TRACE); 515 } else 516 trace_probe_clear_flag(tp, TP_FLAG_PROFILE); 517 518 if (!trace_probe_is_enabled(tp)) 519 __disable_trace_fprobe(tp); 520 521 out: 522 if (file) 523 /* 524 * Synchronization is done in below function. For perf event, 525 * file == NULL and perf_trace_event_unreg() calls 526 * tracepoint_synchronize_unregister() to ensure synchronize 527 * event. We don't need to care about it. 528 */ 529 trace_probe_remove_file(tp, file); 530 531 return 0; 532 } 533 534 /* Event entry printers */ 535 static enum print_line_t 536 print_fentry_event(struct trace_iterator *iter, int flags, 537 struct trace_event *event) 538 { 539 struct fentry_trace_entry_head *field; 540 struct trace_seq *s = &iter->seq; 541 struct trace_probe *tp; 542 543 field = (struct fentry_trace_entry_head *)iter->ent; 544 tp = trace_probe_primary_from_call( 545 container_of(event, struct trace_event_call, event)); 546 if (WARN_ON_ONCE(!tp)) 547 goto out; 548 549 trace_seq_printf(s, "%s: (", trace_probe_name(tp)); 550 551 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) 552 goto out; 553 554 trace_seq_putc(s, ')'); 555 556 if (trace_probe_print_args(s, tp->args, tp->nr_args, 557 (u8 *)&field[1], field) < 0) 558 goto out; 559 560 trace_seq_putc(s, '\n'); 561 out: 562 return trace_handle_return(s); 563 } 564 565 static enum print_line_t 566 print_fexit_event(struct trace_iterator *iter, int flags, 567 struct trace_event *event) 568 { 569 struct fexit_trace_entry_head *field; 570 struct trace_seq *s = &iter->seq; 571 struct trace_probe *tp; 572 573 field = (struct fexit_trace_entry_head *)iter->ent; 574 tp = trace_probe_primary_from_call( 575 container_of(event, struct trace_event_call, event)); 576 if (WARN_ON_ONCE(!tp)) 577 goto out; 578 579 trace_seq_printf(s, "%s: (", trace_probe_name(tp)); 580 581 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) 582 goto out; 583 584 trace_seq_puts(s, " <- "); 585 586 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) 587 goto out; 588 589 trace_seq_putc(s, ')'); 590 591 if (trace_probe_print_args(s, tp->args, tp->nr_args, 592 (u8 *)&field[1], field) < 0) 593 goto out; 594 595 trace_seq_putc(s, '\n'); 596 597 out: 598 return trace_handle_return(s); 599 } 600 601 static int fentry_event_define_fields(struct trace_event_call *event_call) 602 { 603 int ret; 604 struct fentry_trace_entry_head field; 605 struct trace_probe *tp; 606 607 tp = trace_probe_primary_from_call(event_call); 608 if (WARN_ON_ONCE(!tp)) 609 return -ENOENT; 610 611 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); 612 613 return traceprobe_define_arg_fields(event_call, sizeof(field), tp); 614 } 615 616 static int fexit_event_define_fields(struct trace_event_call *event_call) 617 { 618 int ret; 619 struct fexit_trace_entry_head field; 620 struct trace_probe *tp; 621 622 tp = trace_probe_primary_from_call(event_call); 623 if (WARN_ON_ONCE(!tp)) 624 return -ENOENT; 625 626 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); 627 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); 628 629 return traceprobe_define_arg_fields(event_call, sizeof(field), tp); 630 } 631 632 static struct trace_event_functions fentry_funcs = { 633 .trace = print_fentry_event 634 }; 635 636 static struct trace_event_functions fexit_funcs = { 637 .trace = print_fexit_event 638 }; 639 640 static struct trace_event_fields fentry_fields_array[] = { 641 { .type = TRACE_FUNCTION_TYPE, 642 .define_fields = fentry_event_define_fields }, 643 {} 644 }; 645 646 static struct trace_event_fields fexit_fields_array[] = { 647 { .type = TRACE_FUNCTION_TYPE, 648 .define_fields = fexit_event_define_fields }, 649 {} 650 }; 651 652 static int fprobe_register(struct trace_event_call *event, 653 enum trace_reg type, void *data); 654 655 static inline void init_trace_event_call(struct trace_fprobe *tf) 656 { 657 struct trace_event_call *call = trace_probe_event_call(&tf->tp); 658 659 if (trace_fprobe_is_return(tf)) { 660 call->event.funcs = &fexit_funcs; 661 call->class->fields_array = fexit_fields_array; 662 } else { 663 call->event.funcs = &fentry_funcs; 664 call->class->fields_array = fentry_fields_array; 665 } 666 667 call->flags = TRACE_EVENT_FL_FPROBE; 668 call->class->reg = fprobe_register; 669 } 670 671 static int register_fprobe_event(struct trace_fprobe *tf) 672 { 673 init_trace_event_call(tf); 674 675 return trace_probe_register_event_call(&tf->tp); 676 } 677 678 static int unregister_fprobe_event(struct trace_fprobe *tf) 679 { 680 return trace_probe_unregister_event_call(&tf->tp); 681 } 682 683 static int __regsiter_tracepoint_fprobe(struct trace_fprobe *tf) 684 { 685 struct tracepoint *tpoint = tf->tpoint; 686 unsigned long ip = (unsigned long)tpoint->probestub; 687 int ret; 688 689 /* 690 * Here, we do 2 steps to enable fprobe on a tracepoint. 691 * At first, put __probestub_##TP function on the tracepoint 692 * and put a fprobe on the stub function. 693 */ 694 ret = tracepoint_probe_register_prio_may_exist(tpoint, 695 tpoint->probestub, NULL, 0); 696 if (ret < 0) 697 return ret; 698 return register_fprobe_ips(&tf->fp, &ip, 1); 699 } 700 701 /* Internal register function - just handle fprobe and flags */ 702 static int __register_trace_fprobe(struct trace_fprobe *tf) 703 { 704 int i, ret; 705 706 /* Should we need new LOCKDOWN flag for fprobe? */ 707 ret = security_locked_down(LOCKDOWN_KPROBES); 708 if (ret) 709 return ret; 710 711 if (trace_fprobe_is_registered(tf)) 712 return -EINVAL; 713 714 for (i = 0; i < tf->tp.nr_args; i++) { 715 ret = traceprobe_update_arg(&tf->tp.args[i]); 716 if (ret) 717 return ret; 718 } 719 720 /* Set/clear disabled flag according to tp->flag */ 721 if (trace_probe_is_enabled(&tf->tp)) 722 tf->fp.flags &= ~FPROBE_FL_DISABLED; 723 else 724 tf->fp.flags |= FPROBE_FL_DISABLED; 725 726 if (trace_fprobe_is_tracepoint(tf)) { 727 728 /* This tracepoint is not loaded yet */ 729 if (tf->tpoint == TRACEPOINT_STUB) 730 return 0; 731 732 return __regsiter_tracepoint_fprobe(tf); 733 } 734 735 /* TODO: handle filter, nofilter or symbol list */ 736 return register_fprobe(&tf->fp, tf->symbol, NULL); 737 } 738 739 /* Internal unregister function - just handle fprobe and flags */ 740 static void __unregister_trace_fprobe(struct trace_fprobe *tf) 741 { 742 if (trace_fprobe_is_registered(tf)) { 743 unregister_fprobe(&tf->fp); 744 memset(&tf->fp, 0, sizeof(tf->fp)); 745 if (trace_fprobe_is_tracepoint(tf)) { 746 tracepoint_probe_unregister(tf->tpoint, 747 tf->tpoint->probestub, NULL); 748 tf->tpoint = NULL; 749 tf->mod = NULL; 750 } 751 } 752 } 753 754 /* TODO: make this trace_*probe common function */ 755 /* Unregister a trace_probe and probe_event */ 756 static int unregister_trace_fprobe(struct trace_fprobe *tf) 757 { 758 /* If other probes are on the event, just unregister fprobe */ 759 if (trace_probe_has_sibling(&tf->tp)) 760 goto unreg; 761 762 /* Enabled event can not be unregistered */ 763 if (trace_probe_is_enabled(&tf->tp)) 764 return -EBUSY; 765 766 /* If there's a reference to the dynamic event */ 767 if (trace_event_dyn_busy(trace_probe_event_call(&tf->tp))) 768 return -EBUSY; 769 770 /* Will fail if probe is being used by ftrace or perf */ 771 if (unregister_fprobe_event(tf)) 772 return -EBUSY; 773 774 unreg: 775 __unregister_trace_fprobe(tf); 776 dyn_event_remove(&tf->devent); 777 trace_probe_unlink(&tf->tp); 778 779 return 0; 780 } 781 782 static bool trace_fprobe_has_same_fprobe(struct trace_fprobe *orig, 783 struct trace_fprobe *comp) 784 { 785 struct trace_probe_event *tpe = orig->tp.event; 786 int i; 787 788 list_for_each_entry(orig, &tpe->probes, tp.list) { 789 if (strcmp(trace_fprobe_symbol(orig), 790 trace_fprobe_symbol(comp))) 791 continue; 792 793 /* 794 * trace_probe_compare_arg_type() ensured that nr_args and 795 * each argument name and type are same. Let's compare comm. 796 */ 797 for (i = 0; i < orig->tp.nr_args; i++) { 798 if (strcmp(orig->tp.args[i].comm, 799 comp->tp.args[i].comm)) 800 break; 801 } 802 803 if (i == orig->tp.nr_args) 804 return true; 805 } 806 807 return false; 808 } 809 810 static int append_trace_fprobe(struct trace_fprobe *tf, struct trace_fprobe *to) 811 { 812 int ret; 813 814 if (trace_fprobe_is_return(tf) != trace_fprobe_is_return(to) || 815 trace_fprobe_is_tracepoint(tf) != trace_fprobe_is_tracepoint(to)) { 816 trace_probe_log_set_index(0); 817 trace_probe_log_err(0, DIFF_PROBE_TYPE); 818 return -EEXIST; 819 } 820 ret = trace_probe_compare_arg_type(&tf->tp, &to->tp); 821 if (ret) { 822 /* Note that argument starts index = 2 */ 823 trace_probe_log_set_index(ret + 1); 824 trace_probe_log_err(0, DIFF_ARG_TYPE); 825 return -EEXIST; 826 } 827 if (trace_fprobe_has_same_fprobe(to, tf)) { 828 trace_probe_log_set_index(0); 829 trace_probe_log_err(0, SAME_PROBE); 830 return -EEXIST; 831 } 832 833 /* Append to existing event */ 834 ret = trace_probe_append(&tf->tp, &to->tp); 835 if (ret) 836 return ret; 837 838 ret = __register_trace_fprobe(tf); 839 if (ret) 840 trace_probe_unlink(&tf->tp); 841 else 842 dyn_event_add(&tf->devent, trace_probe_event_call(&tf->tp)); 843 844 return ret; 845 } 846 847 /* Register a trace_probe and probe_event */ 848 static int register_trace_fprobe(struct trace_fprobe *tf) 849 { 850 struct trace_fprobe *old_tf; 851 int ret; 852 853 mutex_lock(&event_mutex); 854 855 old_tf = find_trace_fprobe(trace_probe_name(&tf->tp), 856 trace_probe_group_name(&tf->tp)); 857 if (old_tf) { 858 ret = append_trace_fprobe(tf, old_tf); 859 goto end; 860 } 861 862 /* Register new event */ 863 ret = register_fprobe_event(tf); 864 if (ret) { 865 if (ret == -EEXIST) { 866 trace_probe_log_set_index(0); 867 trace_probe_log_err(0, EVENT_EXIST); 868 } else 869 pr_warn("Failed to register probe event(%d)\n", ret); 870 goto end; 871 } 872 873 /* Register fprobe */ 874 ret = __register_trace_fprobe(tf); 875 if (ret < 0) 876 unregister_fprobe_event(tf); 877 else 878 dyn_event_add(&tf->devent, trace_probe_event_call(&tf->tp)); 879 880 end: 881 mutex_unlock(&event_mutex); 882 return ret; 883 } 884 885 struct __find_tracepoint_cb_data { 886 const char *tp_name; 887 struct tracepoint *tpoint; 888 struct module *mod; 889 }; 890 891 static void __find_tracepoint_module_cb(struct tracepoint *tp, struct module *mod, void *priv) 892 { 893 struct __find_tracepoint_cb_data *data = priv; 894 895 if (!data->tpoint && !strcmp(data->tp_name, tp->name)) { 896 data->tpoint = tp; 897 if (!data->mod) { 898 data->mod = mod; 899 if (!try_module_get(data->mod)) { 900 data->tpoint = NULL; 901 data->mod = NULL; 902 } 903 } 904 } 905 } 906 907 static void __find_tracepoint_cb(struct tracepoint *tp, void *priv) 908 { 909 struct __find_tracepoint_cb_data *data = priv; 910 911 if (!data->tpoint && !strcmp(data->tp_name, tp->name)) 912 data->tpoint = tp; 913 } 914 915 /* 916 * Find a tracepoint from kernel and module. If the tracepoint is in a module, 917 * this increments the module refcount to prevent unloading until the 918 * trace_fprobe is registered to the list. After registering the trace_fprobe 919 * on the trace_fprobe list, the module refcount is decremented because 920 * tracepoint_probe_module_cb will handle it. 921 */ 922 static struct tracepoint *find_tracepoint(const char *tp_name, 923 struct module **tp_mod) 924 { 925 struct __find_tracepoint_cb_data data = { 926 .tp_name = tp_name, 927 .mod = NULL, 928 }; 929 930 for_each_kernel_tracepoint(__find_tracepoint_cb, &data); 931 932 if (!data.tpoint && IS_ENABLED(CONFIG_MODULES)) { 933 for_each_module_tracepoint(__find_tracepoint_module_cb, &data); 934 *tp_mod = data.mod; 935 } 936 937 return data.tpoint; 938 } 939 940 #ifdef CONFIG_MODULES 941 static void reenable_trace_fprobe(struct trace_fprobe *tf) 942 { 943 struct trace_probe *tp = &tf->tp; 944 945 list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) { 946 __enable_trace_fprobe(tf); 947 } 948 } 949 950 static struct tracepoint *find_tracepoint_in_module(struct module *mod, 951 const char *tp_name) 952 { 953 struct __find_tracepoint_cb_data data = { 954 .tp_name = tp_name, 955 .mod = mod, 956 }; 957 958 for_each_tracepoint_in_module(mod, __find_tracepoint_module_cb, &data); 959 return data.tpoint; 960 } 961 962 static int __tracepoint_probe_module_cb(struct notifier_block *self, 963 unsigned long val, void *data) 964 { 965 struct tp_module *tp_mod = data; 966 struct tracepoint *tpoint; 967 struct trace_fprobe *tf; 968 struct dyn_event *pos; 969 970 if (val != MODULE_STATE_GOING && val != MODULE_STATE_COMING) 971 return NOTIFY_DONE; 972 973 mutex_lock(&event_mutex); 974 for_each_trace_fprobe(tf, pos) { 975 if (val == MODULE_STATE_COMING && tf->tpoint == TRACEPOINT_STUB) { 976 tpoint = find_tracepoint_in_module(tp_mod->mod, tf->symbol); 977 if (tpoint) { 978 tf->tpoint = tpoint; 979 tf->mod = tp_mod->mod; 980 if (!WARN_ON_ONCE(__regsiter_tracepoint_fprobe(tf)) && 981 trace_probe_is_enabled(&tf->tp)) 982 reenable_trace_fprobe(tf); 983 } 984 } else if (val == MODULE_STATE_GOING && tp_mod->mod == tf->mod) { 985 tracepoint_probe_unregister(tf->tpoint, 986 tf->tpoint->probestub, NULL); 987 tf->tpoint = NULL; 988 tf->mod = NULL; 989 } 990 } 991 mutex_unlock(&event_mutex); 992 993 return NOTIFY_DONE; 994 } 995 996 static struct notifier_block tracepoint_module_nb = { 997 .notifier_call = __tracepoint_probe_module_cb, 998 }; 999 #endif /* CONFIG_MODULES */ 1000 1001 static int parse_symbol_and_return(int argc, const char *argv[], 1002 char **symbol, bool *is_return, 1003 bool is_tracepoint) 1004 { 1005 char *tmp = strchr(argv[1], '%'); 1006 int i; 1007 1008 if (tmp) { 1009 int len = tmp - argv[1]; 1010 1011 if (!is_tracepoint && !strcmp(tmp, "%return")) { 1012 *is_return = true; 1013 } else { 1014 trace_probe_log_err(len, BAD_ADDR_SUFFIX); 1015 return -EINVAL; 1016 } 1017 *symbol = kmemdup_nul(argv[1], len, GFP_KERNEL); 1018 } else 1019 *symbol = kstrdup(argv[1], GFP_KERNEL); 1020 if (!*symbol) 1021 return -ENOMEM; 1022 1023 if (*is_return) 1024 return 0; 1025 1026 /* If there is $retval, this should be a return fprobe. */ 1027 for (i = 2; i < argc; i++) { 1028 tmp = strstr(argv[i], "$retval"); 1029 if (tmp && !isalnum(tmp[7]) && tmp[7] != '_') { 1030 if (is_tracepoint) { 1031 trace_probe_log_set_index(i); 1032 trace_probe_log_err(tmp - argv[i], RETVAL_ON_PROBE); 1033 return -EINVAL; 1034 } 1035 *is_return = true; 1036 break; 1037 } 1038 } 1039 return 0; 1040 } 1041 1042 static int __trace_fprobe_create(int argc, const char *argv[]) 1043 { 1044 /* 1045 * Argument syntax: 1046 * - Add fentry probe: 1047 * f[:[GRP/][EVENT]] [MOD:]KSYM [FETCHARGS] 1048 * - Add fexit probe: 1049 * f[N][:[GRP/][EVENT]] [MOD:]KSYM%return [FETCHARGS] 1050 * - Add tracepoint probe: 1051 * t[:[GRP/][EVENT]] TRACEPOINT [FETCHARGS] 1052 * 1053 * Fetch args: 1054 * $retval : fetch return value 1055 * $stack : fetch stack address 1056 * $stackN : fetch Nth entry of stack (N:0-) 1057 * $argN : fetch Nth argument (N:1-) 1058 * $comm : fetch current task comm 1059 * @ADDR : fetch memory at ADDR (ADDR should be in kernel) 1060 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) 1061 * Dereferencing memory fetch: 1062 * +|-offs(ARG) : fetch memory at ARG +|- offs address. 1063 * Alias name of args: 1064 * NAME=FETCHARG : set NAME as alias of FETCHARG. 1065 * Type of args: 1066 * FETCHARG:TYPE : use TYPE instead of unsigned long. 1067 */ 1068 struct trace_fprobe *tf = NULL; 1069 int i, len, new_argc = 0, ret = 0; 1070 bool is_return = false; 1071 char *symbol = NULL; 1072 const char *event = NULL, *group = FPROBE_EVENT_SYSTEM; 1073 const char **new_argv = NULL; 1074 int maxactive = 0; 1075 char buf[MAX_EVENT_NAME_LEN]; 1076 char gbuf[MAX_EVENT_NAME_LEN]; 1077 char sbuf[KSYM_NAME_LEN]; 1078 char abuf[MAX_BTF_ARGS_LEN]; 1079 char *dbuf = NULL; 1080 bool is_tracepoint = false; 1081 struct module *tp_mod = NULL; 1082 struct tracepoint *tpoint = NULL; 1083 struct traceprobe_parse_context ctx = { 1084 .flags = TPARG_FL_KERNEL | TPARG_FL_FPROBE, 1085 }; 1086 1087 if ((argv[0][0] != 'f' && argv[0][0] != 't') || argc < 2) 1088 return -ECANCELED; 1089 1090 if (argv[0][0] == 't') { 1091 is_tracepoint = true; 1092 group = TRACEPOINT_EVENT_SYSTEM; 1093 } 1094 1095 trace_probe_log_init("trace_fprobe", argc, argv); 1096 1097 event = strchr(&argv[0][1], ':'); 1098 if (event) 1099 event++; 1100 1101 if (isdigit(argv[0][1])) { 1102 if (event) 1103 len = event - &argv[0][1] - 1; 1104 else 1105 len = strlen(&argv[0][1]); 1106 if (len > MAX_EVENT_NAME_LEN - 1) { 1107 trace_probe_log_err(1, BAD_MAXACT); 1108 goto parse_error; 1109 } 1110 memcpy(buf, &argv[0][1], len); 1111 buf[len] = '\0'; 1112 ret = kstrtouint(buf, 0, &maxactive); 1113 if (ret || !maxactive) { 1114 trace_probe_log_err(1, BAD_MAXACT); 1115 goto parse_error; 1116 } 1117 /* fprobe rethook instances are iterated over via a list. The 1118 * maximum should stay reasonable. 1119 */ 1120 if (maxactive > RETHOOK_MAXACTIVE_MAX) { 1121 trace_probe_log_err(1, MAXACT_TOO_BIG); 1122 goto parse_error; 1123 } 1124 } 1125 1126 trace_probe_log_set_index(1); 1127 1128 /* a symbol(or tracepoint) must be specified */ 1129 ret = parse_symbol_and_return(argc, argv, &symbol, &is_return, is_tracepoint); 1130 if (ret < 0) 1131 goto parse_error; 1132 1133 if (!is_return && maxactive) { 1134 trace_probe_log_set_index(0); 1135 trace_probe_log_err(1, BAD_MAXACT_TYPE); 1136 goto parse_error; 1137 } 1138 1139 trace_probe_log_set_index(0); 1140 if (event) { 1141 ret = traceprobe_parse_event_name(&event, &group, gbuf, 1142 event - argv[0]); 1143 if (ret) 1144 goto parse_error; 1145 } 1146 1147 if (!event) { 1148 /* Make a new event name */ 1149 if (is_tracepoint) 1150 snprintf(buf, MAX_EVENT_NAME_LEN, "%s%s", 1151 isdigit(*symbol) ? "_" : "", symbol); 1152 else 1153 snprintf(buf, MAX_EVENT_NAME_LEN, "%s__%s", symbol, 1154 is_return ? "exit" : "entry"); 1155 sanitize_event_name(buf); 1156 event = buf; 1157 } 1158 1159 if (is_return) 1160 ctx.flags |= TPARG_FL_RETURN; 1161 else 1162 ctx.flags |= TPARG_FL_FENTRY; 1163 1164 if (is_tracepoint) { 1165 ctx.flags |= TPARG_FL_TPOINT; 1166 tpoint = find_tracepoint(symbol, &tp_mod); 1167 if (tpoint) { 1168 ctx.funcname = kallsyms_lookup( 1169 (unsigned long)tpoint->probestub, 1170 NULL, NULL, NULL, sbuf); 1171 } else if (IS_ENABLED(CONFIG_MODULES)) { 1172 /* This *may* be loaded afterwards */ 1173 tpoint = TRACEPOINT_STUB; 1174 ctx.funcname = symbol; 1175 } else { 1176 trace_probe_log_set_index(1); 1177 trace_probe_log_err(0, NO_TRACEPOINT); 1178 goto parse_error; 1179 } 1180 } else 1181 ctx.funcname = symbol; 1182 1183 argc -= 2; argv += 2; 1184 new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc, 1185 abuf, MAX_BTF_ARGS_LEN, &ctx); 1186 if (IS_ERR(new_argv)) { 1187 ret = PTR_ERR(new_argv); 1188 new_argv = NULL; 1189 goto out; 1190 } 1191 if (new_argv) { 1192 argc = new_argc; 1193 argv = new_argv; 1194 } 1195 if (argc > MAX_TRACE_ARGS) { 1196 ret = -E2BIG; 1197 goto out; 1198 } 1199 1200 ret = traceprobe_expand_dentry_args(argc, argv, &dbuf); 1201 if (ret) 1202 goto out; 1203 1204 /* setup a probe */ 1205 tf = alloc_trace_fprobe(group, event, symbol, tpoint, tp_mod, 1206 maxactive, argc, is_return); 1207 if (IS_ERR(tf)) { 1208 ret = PTR_ERR(tf); 1209 /* This must return -ENOMEM, else there is a bug */ 1210 WARN_ON_ONCE(ret != -ENOMEM); 1211 goto out; /* We know tf is not allocated */ 1212 } 1213 1214 /* parse arguments */ 1215 for (i = 0; i < argc; i++) { 1216 trace_probe_log_set_index(i + 2); 1217 ctx.offset = 0; 1218 ret = traceprobe_parse_probe_arg(&tf->tp, i, argv[i], &ctx); 1219 if (ret) 1220 goto error; /* This can be -ENOMEM */ 1221 } 1222 1223 if (is_return && tf->tp.entry_arg) { 1224 tf->fp.entry_handler = trace_fprobe_entry_handler; 1225 tf->fp.entry_data_size = traceprobe_get_entry_data_size(&tf->tp); 1226 } 1227 1228 ret = traceprobe_set_print_fmt(&tf->tp, 1229 is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL); 1230 if (ret < 0) 1231 goto error; 1232 1233 ret = register_trace_fprobe(tf); 1234 if (ret) { 1235 trace_probe_log_set_index(1); 1236 if (ret == -EILSEQ) 1237 trace_probe_log_err(0, BAD_INSN_BNDRY); 1238 else if (ret == -ENOENT) 1239 trace_probe_log_err(0, BAD_PROBE_ADDR); 1240 else if (ret != -ENOMEM && ret != -EEXIST) 1241 trace_probe_log_err(0, FAIL_REG_PROBE); 1242 goto error; 1243 } 1244 1245 out: 1246 if (tp_mod) 1247 module_put(tp_mod); 1248 traceprobe_finish_parse(&ctx); 1249 trace_probe_log_clear(); 1250 kfree(new_argv); 1251 kfree(symbol); 1252 kfree(dbuf); 1253 return ret; 1254 1255 parse_error: 1256 ret = -EINVAL; 1257 error: 1258 free_trace_fprobe(tf); 1259 goto out; 1260 } 1261 1262 static int trace_fprobe_create(const char *raw_command) 1263 { 1264 return trace_probe_create(raw_command, __trace_fprobe_create); 1265 } 1266 1267 static int trace_fprobe_release(struct dyn_event *ev) 1268 { 1269 struct trace_fprobe *tf = to_trace_fprobe(ev); 1270 int ret = unregister_trace_fprobe(tf); 1271 1272 if (!ret) 1273 free_trace_fprobe(tf); 1274 return ret; 1275 } 1276 1277 static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev) 1278 { 1279 struct trace_fprobe *tf = to_trace_fprobe(ev); 1280 int i; 1281 1282 if (trace_fprobe_is_tracepoint(tf)) 1283 seq_putc(m, 't'); 1284 else 1285 seq_putc(m, 'f'); 1286 if (trace_fprobe_is_return(tf) && tf->fp.nr_maxactive) 1287 seq_printf(m, "%d", tf->fp.nr_maxactive); 1288 seq_printf(m, ":%s/%s", trace_probe_group_name(&tf->tp), 1289 trace_probe_name(&tf->tp)); 1290 1291 seq_printf(m, " %s%s", trace_fprobe_symbol(tf), 1292 trace_fprobe_is_return(tf) ? "%return" : ""); 1293 1294 for (i = 0; i < tf->tp.nr_args; i++) 1295 seq_printf(m, " %s=%s", tf->tp.args[i].name, tf->tp.args[i].comm); 1296 seq_putc(m, '\n'); 1297 1298 return 0; 1299 } 1300 1301 /* 1302 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex. 1303 */ 1304 static int fprobe_register(struct trace_event_call *event, 1305 enum trace_reg type, void *data) 1306 { 1307 struct trace_event_file *file = data; 1308 1309 switch (type) { 1310 case TRACE_REG_REGISTER: 1311 return enable_trace_fprobe(event, file); 1312 case TRACE_REG_UNREGISTER: 1313 return disable_trace_fprobe(event, file); 1314 1315 #ifdef CONFIG_PERF_EVENTS 1316 case TRACE_REG_PERF_REGISTER: 1317 return enable_trace_fprobe(event, NULL); 1318 case TRACE_REG_PERF_UNREGISTER: 1319 return disable_trace_fprobe(event, NULL); 1320 case TRACE_REG_PERF_OPEN: 1321 case TRACE_REG_PERF_CLOSE: 1322 case TRACE_REG_PERF_ADD: 1323 case TRACE_REG_PERF_DEL: 1324 return 0; 1325 #endif 1326 } 1327 return 0; 1328 } 1329 1330 /* 1331 * Register dynevent at core_initcall. This allows kernel to setup fprobe 1332 * events in postcore_initcall without tracefs. 1333 */ 1334 static __init int init_fprobe_trace_early(void) 1335 { 1336 int ret; 1337 1338 ret = dyn_event_register(&trace_fprobe_ops); 1339 if (ret) 1340 return ret; 1341 1342 #ifdef CONFIG_MODULES 1343 ret = register_tracepoint_module_notifier(&tracepoint_module_nb); 1344 if (ret) 1345 return ret; 1346 #endif 1347 1348 return 0; 1349 } 1350 core_initcall(init_fprobe_trace_early); 1351