1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fprobe - Simple ftrace probe wrapper for function entry. 4 */ 5 #define pr_fmt(fmt) "fprobe: " fmt 6 7 #include <linux/err.h> 8 #include <linux/fprobe.h> 9 #include <linux/kallsyms.h> 10 #include <linux/kprobes.h> 11 #include <linux/list.h> 12 #include <linux/mutex.h> 13 #include <linux/slab.h> 14 #include <linux/sort.h> 15 16 #include <asm/fprobe.h> 17 18 #include "trace.h" 19 20 #define FPROBE_IP_HASH_BITS 8 21 #define FPROBE_IP_TABLE_SIZE (1 << FPROBE_IP_HASH_BITS) 22 23 #define FPROBE_HASH_BITS 6 24 #define FPROBE_TABLE_SIZE (1 << FPROBE_HASH_BITS) 25 26 #define SIZE_IN_LONG(x) ((x + sizeof(long) - 1) >> (sizeof(long) == 8 ? 3 : 2)) 27 28 /* 29 * fprobe_table: hold 'fprobe_hlist::hlist' for checking the fprobe still 30 * exists. The key is the address of fprobe instance. 31 * fprobe_ip_table: hold 'fprobe_hlist::array[*]' for searching the fprobe 32 * instance related to the funciton address. The key is the ftrace IP 33 * address. 34 * 35 * When unregistering the fprobe, fprobe_hlist::fp and fprobe_hlist::array[*].fp 36 * are set NULL and delete those from both hash tables (by hlist_del_rcu). 37 * After an RCU grace period, the fprobe_hlist itself will be released. 38 * 39 * fprobe_table and fprobe_ip_table can be accessed from either 40 * - Normal hlist traversal and RCU add/del under 'fprobe_mutex' is held. 41 * - RCU hlist traversal under disabling preempt 42 */ 43 static struct hlist_head fprobe_table[FPROBE_TABLE_SIZE]; 44 static struct hlist_head fprobe_ip_table[FPROBE_IP_TABLE_SIZE]; 45 static DEFINE_MUTEX(fprobe_mutex); 46 47 /* 48 * Find first fprobe in the hlist. It will be iterated twice in the entry 49 * probe, once for correcting the total required size, the second time is 50 * calling back the user handlers. 51 * Thus the hlist in the fprobe_table must be sorted and new probe needs to 52 * be added *before* the first fprobe. 53 */ 54 static struct fprobe_hlist_node *find_first_fprobe_node(unsigned long ip) 55 { 56 struct fprobe_hlist_node *node; 57 struct hlist_head *head; 58 59 head = &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)]; 60 hlist_for_each_entry_rcu(node, head, hlist, 61 lockdep_is_held(&fprobe_mutex)) { 62 if (node->addr == ip) 63 return node; 64 } 65 return NULL; 66 } 67 NOKPROBE_SYMBOL(find_first_fprobe_node); 68 69 /* Node insertion and deletion requires the fprobe_mutex */ 70 static void insert_fprobe_node(struct fprobe_hlist_node *node) 71 { 72 unsigned long ip = node->addr; 73 struct fprobe_hlist_node *next; 74 struct hlist_head *head; 75 76 lockdep_assert_held(&fprobe_mutex); 77 78 next = find_first_fprobe_node(ip); 79 if (next) { 80 hlist_add_before_rcu(&node->hlist, &next->hlist); 81 return; 82 } 83 head = &fprobe_ip_table[hash_ptr((void *)ip, FPROBE_IP_HASH_BITS)]; 84 hlist_add_head_rcu(&node->hlist, head); 85 } 86 87 /* Return true if there are synonims */ 88 static bool delete_fprobe_node(struct fprobe_hlist_node *node) 89 { 90 lockdep_assert_held(&fprobe_mutex); 91 92 WRITE_ONCE(node->fp, NULL); 93 hlist_del_rcu(&node->hlist); 94 return !!find_first_fprobe_node(node->addr); 95 } 96 97 /* Check existence of the fprobe */ 98 static bool is_fprobe_still_exist(struct fprobe *fp) 99 { 100 struct hlist_head *head; 101 struct fprobe_hlist *fph; 102 103 head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)]; 104 hlist_for_each_entry_rcu(fph, head, hlist, 105 lockdep_is_held(&fprobe_mutex)) { 106 if (fph->fp == fp) 107 return true; 108 } 109 return false; 110 } 111 NOKPROBE_SYMBOL(is_fprobe_still_exist); 112 113 static int add_fprobe_hash(struct fprobe *fp) 114 { 115 struct fprobe_hlist *fph = fp->hlist_array; 116 struct hlist_head *head; 117 118 lockdep_assert_held(&fprobe_mutex); 119 120 if (WARN_ON_ONCE(!fph)) 121 return -EINVAL; 122 123 if (is_fprobe_still_exist(fp)) 124 return -EEXIST; 125 126 head = &fprobe_table[hash_ptr(fp, FPROBE_HASH_BITS)]; 127 hlist_add_head_rcu(&fp->hlist_array->hlist, head); 128 return 0; 129 } 130 131 static int del_fprobe_hash(struct fprobe *fp) 132 { 133 struct fprobe_hlist *fph = fp->hlist_array; 134 135 lockdep_assert_held(&fprobe_mutex); 136 137 if (WARN_ON_ONCE(!fph)) 138 return -EINVAL; 139 140 if (!is_fprobe_still_exist(fp)) 141 return -ENOENT; 142 143 fph->fp = NULL; 144 hlist_del_rcu(&fph->hlist); 145 return 0; 146 } 147 148 #ifdef ARCH_DEFINE_ENCODE_FPROBE_HEADER 149 150 /* The arch should encode fprobe_header info into one unsigned long */ 151 #define FPROBE_HEADER_SIZE_IN_LONG 1 152 153 static inline bool write_fprobe_header(unsigned long *stack, 154 struct fprobe *fp, unsigned int size_words) 155 { 156 if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD || 157 !arch_fprobe_header_encodable(fp))) 158 return false; 159 160 *stack = arch_encode_fprobe_header(fp, size_words); 161 return true; 162 } 163 164 static inline void read_fprobe_header(unsigned long *stack, 165 struct fprobe **fp, unsigned int *size_words) 166 { 167 *fp = arch_decode_fprobe_header_fp(*stack); 168 *size_words = arch_decode_fprobe_header_size(*stack); 169 } 170 171 #else 172 173 /* Generic fprobe_header */ 174 struct __fprobe_header { 175 struct fprobe *fp; 176 unsigned long size_words; 177 } __packed; 178 179 #define FPROBE_HEADER_SIZE_IN_LONG SIZE_IN_LONG(sizeof(struct __fprobe_header)) 180 181 static inline bool write_fprobe_header(unsigned long *stack, 182 struct fprobe *fp, unsigned int size_words) 183 { 184 struct __fprobe_header *fph = (struct __fprobe_header *)stack; 185 186 if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD)) 187 return false; 188 189 fph->fp = fp; 190 fph->size_words = size_words; 191 return true; 192 } 193 194 static inline void read_fprobe_header(unsigned long *stack, 195 struct fprobe **fp, unsigned int *size_words) 196 { 197 struct __fprobe_header *fph = (struct __fprobe_header *)stack; 198 199 *fp = fph->fp; 200 *size_words = fph->size_words; 201 } 202 203 #endif 204 205 /* 206 * fprobe shadow stack management: 207 * Since fprobe shares a single fgraph_ops, it needs to share the stack entry 208 * among the probes on the same function exit. Note that a new probe can be 209 * registered before a target function is returning, we can not use the hash 210 * table to find the corresponding probes. Thus the probe address is stored on 211 * the shadow stack with its entry data size. 212 * 213 */ 214 static inline int __fprobe_handler(unsigned long ip, unsigned long parent_ip, 215 struct fprobe *fp, struct ftrace_regs *fregs, 216 void *data) 217 { 218 if (!fp->entry_handler) 219 return 0; 220 221 return fp->entry_handler(fp, ip, parent_ip, fregs, data); 222 } 223 224 static inline int __fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip, 225 struct fprobe *fp, struct ftrace_regs *fregs, 226 void *data) 227 { 228 int ret; 229 /* 230 * This user handler is shared with other kprobes and is not expected to be 231 * called recursively. So if any other kprobe handler is running, this will 232 * exit as kprobe does. See the section 'Share the callbacks with kprobes' 233 * in Documentation/trace/fprobe.rst for more information. 234 */ 235 if (unlikely(kprobe_running())) { 236 fp->nmissed++; 237 return 0; 238 } 239 240 kprobe_busy_begin(); 241 ret = __fprobe_handler(ip, parent_ip, fp, fregs, data); 242 kprobe_busy_end(); 243 return ret; 244 } 245 246 static int fprobe_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *gops, 247 struct ftrace_regs *fregs) 248 { 249 struct fprobe_hlist_node *node, *first; 250 unsigned long *fgraph_data = NULL; 251 unsigned long func = trace->func; 252 unsigned long ret_ip; 253 int reserved_words; 254 struct fprobe *fp; 255 int used, ret; 256 257 if (WARN_ON_ONCE(!fregs)) 258 return 0; 259 260 first = node = find_first_fprobe_node(func); 261 if (unlikely(!first)) 262 return 0; 263 264 reserved_words = 0; 265 hlist_for_each_entry_from_rcu(node, hlist) { 266 if (node->addr != func) 267 break; 268 fp = READ_ONCE(node->fp); 269 if (!fp || !fp->exit_handler) 270 continue; 271 /* 272 * Since fprobe can be enabled until the next loop, we ignore the 273 * fprobe's disabled flag in this loop. 274 */ 275 reserved_words += 276 FPROBE_HEADER_SIZE_IN_LONG + SIZE_IN_LONG(fp->entry_data_size); 277 } 278 node = first; 279 if (reserved_words) { 280 fgraph_data = fgraph_reserve_data(gops->idx, reserved_words * sizeof(long)); 281 if (unlikely(!fgraph_data)) { 282 hlist_for_each_entry_from_rcu(node, hlist) { 283 if (node->addr != func) 284 break; 285 fp = READ_ONCE(node->fp); 286 if (fp && !fprobe_disabled(fp)) 287 fp->nmissed++; 288 } 289 return 0; 290 } 291 } 292 293 /* 294 * TODO: recursion detection has been done in the fgraph. Thus we need 295 * to add a callback to increment missed counter. 296 */ 297 ret_ip = ftrace_regs_get_return_address(fregs); 298 used = 0; 299 hlist_for_each_entry_from_rcu(node, hlist) { 300 int data_size; 301 void *data; 302 303 if (node->addr != func) 304 break; 305 fp = READ_ONCE(node->fp); 306 if (!fp || fprobe_disabled(fp)) 307 continue; 308 309 data_size = fp->entry_data_size; 310 if (data_size && fp->exit_handler) 311 data = fgraph_data + used + FPROBE_HEADER_SIZE_IN_LONG; 312 else 313 data = NULL; 314 315 if (fprobe_shared_with_kprobes(fp)) 316 ret = __fprobe_kprobe_handler(func, ret_ip, fp, fregs, data); 317 else 318 ret = __fprobe_handler(func, ret_ip, fp, fregs, data); 319 320 /* If entry_handler returns !0, nmissed is not counted but skips exit_handler. */ 321 if (!ret && fp->exit_handler) { 322 int size_words = SIZE_IN_LONG(data_size); 323 324 if (write_fprobe_header(&fgraph_data[used], fp, size_words)) 325 used += FPROBE_HEADER_SIZE_IN_LONG + size_words; 326 } 327 } 328 if (used < reserved_words) 329 memset(fgraph_data + used, 0, reserved_words - used); 330 331 /* If any exit_handler is set, data must be used. */ 332 return used != 0; 333 } 334 NOKPROBE_SYMBOL(fprobe_entry); 335 336 static void fprobe_return(struct ftrace_graph_ret *trace, 337 struct fgraph_ops *gops, 338 struct ftrace_regs *fregs) 339 { 340 unsigned long *fgraph_data = NULL; 341 unsigned long ret_ip; 342 struct fprobe *fp; 343 int size, curr; 344 int size_words; 345 346 fgraph_data = (unsigned long *)fgraph_retrieve_data(gops->idx, &size); 347 if (WARN_ON_ONCE(!fgraph_data)) 348 return; 349 size_words = SIZE_IN_LONG(size); 350 ret_ip = ftrace_regs_get_instruction_pointer(fregs); 351 352 preempt_disable(); 353 354 curr = 0; 355 while (size_words > curr) { 356 read_fprobe_header(&fgraph_data[curr], &fp, &size); 357 if (!fp) 358 break; 359 curr += FPROBE_HEADER_SIZE_IN_LONG; 360 if (is_fprobe_still_exist(fp) && !fprobe_disabled(fp)) { 361 if (WARN_ON_ONCE(curr + size > size_words)) 362 break; 363 fp->exit_handler(fp, trace->func, ret_ip, fregs, 364 size ? fgraph_data + curr : NULL); 365 } 366 curr += size; 367 } 368 preempt_enable(); 369 } 370 NOKPROBE_SYMBOL(fprobe_return); 371 372 static struct fgraph_ops fprobe_graph_ops = { 373 .entryfunc = fprobe_entry, 374 .retfunc = fprobe_return, 375 }; 376 static int fprobe_graph_active; 377 378 /* Add @addrs to the ftrace filter and register fgraph if needed. */ 379 static int fprobe_graph_add_ips(unsigned long *addrs, int num) 380 { 381 int ret; 382 383 lockdep_assert_held(&fprobe_mutex); 384 385 ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0); 386 if (ret) 387 return ret; 388 389 if (!fprobe_graph_active) { 390 ret = register_ftrace_graph(&fprobe_graph_ops); 391 if (WARN_ON_ONCE(ret)) { 392 ftrace_free_filter(&fprobe_graph_ops.ops); 393 return ret; 394 } 395 } 396 fprobe_graph_active++; 397 return 0; 398 } 399 400 /* Remove @addrs from the ftrace filter and unregister fgraph if possible. */ 401 static void fprobe_graph_remove_ips(unsigned long *addrs, int num) 402 { 403 lockdep_assert_held(&fprobe_mutex); 404 405 fprobe_graph_active--; 406 /* Q: should we unregister it ? */ 407 if (!fprobe_graph_active) 408 unregister_ftrace_graph(&fprobe_graph_ops); 409 410 ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0); 411 } 412 413 static int symbols_cmp(const void *a, const void *b) 414 { 415 const char **str_a = (const char **) a; 416 const char **str_b = (const char **) b; 417 418 return strcmp(*str_a, *str_b); 419 } 420 421 /* Convert ftrace location address from symbols */ 422 static unsigned long *get_ftrace_locations(const char **syms, int num) 423 { 424 unsigned long *addrs; 425 426 /* Convert symbols to symbol address */ 427 addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL); 428 if (!addrs) 429 return ERR_PTR(-ENOMEM); 430 431 /* ftrace_lookup_symbols expects sorted symbols */ 432 sort(syms, num, sizeof(*syms), symbols_cmp, NULL); 433 434 if (!ftrace_lookup_symbols(syms, num, addrs)) 435 return addrs; 436 437 kfree(addrs); 438 return ERR_PTR(-ENOENT); 439 } 440 441 struct filter_match_data { 442 const char *filter; 443 const char *notfilter; 444 size_t index; 445 size_t size; 446 unsigned long *addrs; 447 }; 448 449 static int filter_match_callback(void *data, const char *name, unsigned long addr) 450 { 451 struct filter_match_data *match = data; 452 453 if (!glob_match(match->filter, name) || 454 (match->notfilter && glob_match(match->notfilter, name))) 455 return 0; 456 457 if (!ftrace_location(addr)) 458 return 0; 459 460 if (match->addrs) 461 match->addrs[match->index] = addr; 462 463 match->index++; 464 return match->index == match->size; 465 } 466 467 /* 468 * Make IP list from the filter/no-filter glob patterns. 469 * Return the number of matched symbols, or -ENOENT. 470 */ 471 static int ip_list_from_filter(const char *filter, const char *notfilter, 472 unsigned long *addrs, size_t size) 473 { 474 struct filter_match_data match = { .filter = filter, .notfilter = notfilter, 475 .index = 0, .size = size, .addrs = addrs}; 476 int ret; 477 478 ret = kallsyms_on_each_symbol(filter_match_callback, &match); 479 if (ret < 0) 480 return ret; 481 ret = module_kallsyms_on_each_symbol(NULL, filter_match_callback, &match); 482 if (ret < 0) 483 return ret; 484 485 return match.index ?: -ENOENT; 486 } 487 488 static void fprobe_fail_cleanup(struct fprobe *fp) 489 { 490 kfree(fp->hlist_array); 491 fp->hlist_array = NULL; 492 } 493 494 /* Initialize the fprobe data structure. */ 495 static int fprobe_init(struct fprobe *fp, unsigned long *addrs, int num) 496 { 497 struct fprobe_hlist *hlist_array; 498 unsigned long addr; 499 int size, i; 500 501 if (!fp || !addrs || num <= 0) 502 return -EINVAL; 503 504 size = ALIGN(fp->entry_data_size, sizeof(long)); 505 if (size > MAX_FPROBE_DATA_SIZE) 506 return -E2BIG; 507 fp->entry_data_size = size; 508 509 hlist_array = kzalloc(struct_size(hlist_array, array, num), GFP_KERNEL); 510 if (!hlist_array) 511 return -ENOMEM; 512 513 fp->nmissed = 0; 514 515 hlist_array->size = num; 516 fp->hlist_array = hlist_array; 517 hlist_array->fp = fp; 518 for (i = 0; i < num; i++) { 519 hlist_array->array[i].fp = fp; 520 addr = ftrace_location(addrs[i]); 521 if (!addr) { 522 fprobe_fail_cleanup(fp); 523 return -ENOENT; 524 } 525 hlist_array->array[i].addr = addr; 526 } 527 return 0; 528 } 529 530 #define FPROBE_IPS_MAX INT_MAX 531 532 /** 533 * register_fprobe() - Register fprobe to ftrace by pattern. 534 * @fp: A fprobe data structure to be registered. 535 * @filter: A wildcard pattern of probed symbols. 536 * @notfilter: A wildcard pattern of NOT probed symbols. 537 * 538 * Register @fp to ftrace for enabling the probe on the symbols matched to @filter. 539 * If @notfilter is not NULL, the symbols matched the @notfilter are not probed. 540 * 541 * Return 0 if @fp is registered successfully, -errno if not. 542 */ 543 int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter) 544 { 545 unsigned long *addrs; 546 int ret; 547 548 if (!fp || !filter) 549 return -EINVAL; 550 551 ret = ip_list_from_filter(filter, notfilter, NULL, FPROBE_IPS_MAX); 552 if (ret < 0) 553 return ret; 554 555 addrs = kcalloc(ret, sizeof(unsigned long), GFP_KERNEL); 556 if (!addrs) 557 return -ENOMEM; 558 ret = ip_list_from_filter(filter, notfilter, addrs, ret); 559 if (ret > 0) 560 ret = register_fprobe_ips(fp, addrs, ret); 561 562 kfree(addrs); 563 return ret; 564 } 565 EXPORT_SYMBOL_GPL(register_fprobe); 566 567 /** 568 * register_fprobe_ips() - Register fprobe to ftrace by address. 569 * @fp: A fprobe data structure to be registered. 570 * @addrs: An array of target function address. 571 * @num: The number of entries of @addrs. 572 * 573 * Register @fp to ftrace for enabling the probe on the address given by @addrs. 574 * The @addrs must be the addresses of ftrace location address, which may be 575 * the symbol address + arch-dependent offset. 576 * If you unsure what this mean, please use other registration functions. 577 * 578 * Return 0 if @fp is registered successfully, -errno if not. 579 */ 580 int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num) 581 { 582 struct fprobe_hlist *hlist_array; 583 int ret, i; 584 585 ret = fprobe_init(fp, addrs, num); 586 if (ret) 587 return ret; 588 589 mutex_lock(&fprobe_mutex); 590 591 hlist_array = fp->hlist_array; 592 ret = fprobe_graph_add_ips(addrs, num); 593 if (!ret) { 594 add_fprobe_hash(fp); 595 for (i = 0; i < hlist_array->size; i++) 596 insert_fprobe_node(&hlist_array->array[i]); 597 } 598 mutex_unlock(&fprobe_mutex); 599 600 if (ret) 601 fprobe_fail_cleanup(fp); 602 603 return ret; 604 } 605 EXPORT_SYMBOL_GPL(register_fprobe_ips); 606 607 /** 608 * register_fprobe_syms() - Register fprobe to ftrace by symbols. 609 * @fp: A fprobe data structure to be registered. 610 * @syms: An array of target symbols. 611 * @num: The number of entries of @syms. 612 * 613 * Register @fp to the symbols given by @syms array. This will be useful if 614 * you are sure the symbols exist in the kernel. 615 * 616 * Return 0 if @fp is registered successfully, -errno if not. 617 */ 618 int register_fprobe_syms(struct fprobe *fp, const char **syms, int num) 619 { 620 unsigned long *addrs; 621 int ret; 622 623 if (!fp || !syms || num <= 0) 624 return -EINVAL; 625 626 addrs = get_ftrace_locations(syms, num); 627 if (IS_ERR(addrs)) 628 return PTR_ERR(addrs); 629 630 ret = register_fprobe_ips(fp, addrs, num); 631 632 kfree(addrs); 633 634 return ret; 635 } 636 EXPORT_SYMBOL_GPL(register_fprobe_syms); 637 638 bool fprobe_is_registered(struct fprobe *fp) 639 { 640 if (!fp || !fp->hlist_array) 641 return false; 642 return true; 643 } 644 645 /** 646 * unregister_fprobe() - Unregister fprobe. 647 * @fp: A fprobe data structure to be unregistered. 648 * 649 * Unregister fprobe (and remove ftrace hooks from the function entries). 650 * 651 * Return 0 if @fp is unregistered successfully, -errno if not. 652 */ 653 int unregister_fprobe(struct fprobe *fp) 654 { 655 struct fprobe_hlist *hlist_array; 656 unsigned long *addrs = NULL; 657 int ret = 0, i, count; 658 659 mutex_lock(&fprobe_mutex); 660 if (!fp || !is_fprobe_still_exist(fp)) { 661 ret = -EINVAL; 662 goto out; 663 } 664 665 hlist_array = fp->hlist_array; 666 addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL); 667 if (!addrs) { 668 ret = -ENOMEM; /* TODO: Fallback to one-by-one loop */ 669 goto out; 670 } 671 672 /* Remove non-synonim ips from table and hash */ 673 count = 0; 674 for (i = 0; i < hlist_array->size; i++) { 675 if (!delete_fprobe_node(&hlist_array->array[i])) 676 addrs[count++] = hlist_array->array[i].addr; 677 } 678 del_fprobe_hash(fp); 679 680 if (count) 681 fprobe_graph_remove_ips(addrs, count); 682 683 kfree_rcu(hlist_array, rcu); 684 fp->hlist_array = NULL; 685 686 out: 687 mutex_unlock(&fprobe_mutex); 688 689 kfree(addrs); 690 return ret; 691 } 692 EXPORT_SYMBOL_GPL(unregister_fprobe); 693