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