1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kallsyms.c: in-kernel printing of symbolic oopses and stack traces. 4 * 5 * Rewritten and vastly simplified by Rusty Russell for in-kernel 6 * module loader: 7 * Copyright 2002 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 8 * 9 * ChangeLog: 10 * 11 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com> 12 * Changed the compression method from stem compression to "table lookup" 13 * compression (see scripts/kallsyms.c for a more complete description) 14 */ 15 #include <linux/kallsyms.h> 16 #include <linux/init.h> 17 #include <linux/seq_file.h> 18 #include <linux/fs.h> 19 #include <linux/kdb.h> 20 #include <linux/err.h> 21 #include <linux/proc_fs.h> 22 #include <linux/sched.h> /* for cond_resched */ 23 #include <linux/ctype.h> 24 #include <linux/slab.h> 25 #include <linux/filter.h> 26 #include <linux/ftrace.h> 27 #include <linux/kprobes.h> 28 #include <linux/build_bug.h> 29 #include <linux/compiler.h> 30 #include <linux/module.h> 31 #include <linux/kernel.h> 32 #include <linux/bsearch.h> 33 #include <linux/btf_ids.h> 34 35 #include "kallsyms_internal.h" 36 37 /* 38 * Expand a compressed symbol data into the resulting uncompressed string, 39 * if uncompressed string is too long (>= maxlen), it will be truncated, 40 * given the offset to where the symbol is in the compressed stream. 41 */ 42 static unsigned int kallsyms_expand_symbol(unsigned int off, 43 char *result, size_t maxlen) 44 { 45 int len, skipped_first = 0; 46 const char *tptr; 47 const u8 *data; 48 49 /* Get the compressed symbol length from the first symbol byte. */ 50 data = &kallsyms_names[off]; 51 len = *data; 52 data++; 53 off++; 54 55 /* If MSB is 1, it is a "big" symbol, so needs an additional byte. */ 56 if ((len & 0x80) != 0) { 57 len = (len & 0x7F) | (*data << 7); 58 data++; 59 off++; 60 } 61 62 /* 63 * Update the offset to return the offset for the next symbol on 64 * the compressed stream. 65 */ 66 off += len; 67 68 /* 69 * For every byte on the compressed symbol data, copy the table 70 * entry for that byte. 71 */ 72 while (len) { 73 tptr = &kallsyms_token_table[kallsyms_token_index[*data]]; 74 data++; 75 len--; 76 77 while (*tptr) { 78 if (skipped_first) { 79 if (maxlen <= 1) 80 goto tail; 81 *result = *tptr; 82 result++; 83 maxlen--; 84 } else 85 skipped_first = 1; 86 tptr++; 87 } 88 } 89 90 tail: 91 if (maxlen) 92 *result = '\0'; 93 94 /* Return to offset to the next symbol. */ 95 return off; 96 } 97 98 /* 99 * Get symbol type information. This is encoded as a single char at the 100 * beginning of the symbol name. 101 */ 102 static char kallsyms_get_symbol_type(unsigned int off) 103 { 104 /* 105 * Get just the first code, look it up in the token table, 106 * and return the first char from this token. If MSB of length 107 * is 1, it is a "big" symbol, so needs an additional byte. 108 */ 109 if (kallsyms_names[off] & 0x80) 110 off++; 111 return kallsyms_token_table[kallsyms_token_index[kallsyms_names[off + 1]]]; 112 } 113 114 115 /* 116 * Find the offset on the compressed stream given and index in the 117 * kallsyms array. 118 */ 119 static unsigned int get_symbol_offset(unsigned long pos) 120 { 121 const u8 *name; 122 int i, len; 123 124 /* 125 * Use the closest marker we have. We have markers every 256 positions, 126 * so that should be close enough. 127 */ 128 name = &kallsyms_names[kallsyms_markers[pos >> 8]]; 129 130 /* 131 * Sequentially scan all the symbols up to the point we're searching 132 * for. Every symbol is stored in a [<len>][<len> bytes of data] format, 133 * so we just need to add the len to the current pointer for every 134 * symbol we wish to skip. 135 */ 136 for (i = 0; i < (pos & 0xFF); i++) { 137 len = *name; 138 139 /* 140 * If MSB is 1, it is a "big" symbol, so we need to look into 141 * the next byte (and skip it, too). 142 */ 143 if ((len & 0x80) != 0) 144 len = ((len & 0x7F) | (name[1] << 7)) + 1; 145 146 name = name + len + 1; 147 } 148 149 return name - kallsyms_names; 150 } 151 152 unsigned long kallsyms_sym_address(int idx) 153 { 154 /* non-relocatable 32-bit kernels just embed the value directly */ 155 if (!IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_RELOCATABLE)) 156 return (u32)kallsyms_offsets[idx]; 157 return (unsigned long)offset_to_ptr(kallsyms_offsets + idx); 158 } 159 160 static unsigned int get_symbol_seq(int index) 161 { 162 unsigned int i, seq = 0; 163 164 for (i = 0; i < 3; i++) 165 seq = (seq << 8) | kallsyms_seqs_of_names[3 * index + i]; 166 167 return seq; 168 } 169 170 static int kallsyms_lookup_names(const char *name, 171 unsigned int *start, 172 unsigned int *end) 173 { 174 int ret; 175 int low, mid, high; 176 unsigned int seq, off; 177 char namebuf[KSYM_NAME_LEN]; 178 179 low = 0; 180 high = kallsyms_num_syms - 1; 181 182 while (low <= high) { 183 mid = low + (high - low) / 2; 184 seq = get_symbol_seq(mid); 185 off = get_symbol_offset(seq); 186 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 187 ret = strcmp(name, namebuf); 188 if (ret > 0) 189 low = mid + 1; 190 else if (ret < 0) 191 high = mid - 1; 192 else 193 break; 194 } 195 196 if (low > high) 197 return -ESRCH; 198 199 low = mid; 200 while (low) { 201 seq = get_symbol_seq(low - 1); 202 off = get_symbol_offset(seq); 203 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 204 if (strcmp(name, namebuf)) 205 break; 206 low--; 207 } 208 *start = low; 209 210 if (end) { 211 high = mid; 212 while (high < kallsyms_num_syms - 1) { 213 seq = get_symbol_seq(high + 1); 214 off = get_symbol_offset(seq); 215 kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 216 if (strcmp(name, namebuf)) 217 break; 218 high++; 219 } 220 *end = high; 221 } 222 223 return 0; 224 } 225 226 /* Lookup the address for this symbol. Returns 0 if not found. */ 227 unsigned long kallsyms_lookup_name(const char *name) 228 { 229 int ret; 230 unsigned int i; 231 232 /* Skip the search for empty string. */ 233 if (!*name) 234 return 0; 235 236 ret = kallsyms_lookup_names(name, &i, NULL); 237 if (!ret) 238 return kallsyms_sym_address(get_symbol_seq(i)); 239 240 return module_kallsyms_lookup_name(name); 241 } 242 243 /* 244 * Iterate over all symbols in vmlinux. For symbols from modules use 245 * module_kallsyms_on_each_symbol instead. 246 */ 247 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long), 248 void *data) 249 { 250 char namebuf[KSYM_NAME_LEN]; 251 unsigned long i; 252 unsigned int off; 253 int ret; 254 255 for (i = 0, off = 0; i < kallsyms_num_syms; i++) { 256 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 257 ret = fn(data, namebuf, kallsyms_sym_address(i)); 258 if (ret != 0) 259 return ret; 260 cond_resched(); 261 } 262 return 0; 263 } 264 265 int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long), 266 const char *name, void *data) 267 { 268 int ret; 269 unsigned int i, start, end; 270 271 ret = kallsyms_lookup_names(name, &start, &end); 272 if (ret) 273 return 0; 274 275 for (i = start; !ret && i <= end; i++) { 276 ret = fn(data, kallsyms_sym_address(get_symbol_seq(i))); 277 cond_resched(); 278 } 279 280 return ret; 281 } 282 283 static unsigned long get_symbol_pos(unsigned long addr, 284 unsigned long *symbolsize, 285 unsigned long *offset) 286 { 287 unsigned long symbol_start = 0, symbol_end = 0; 288 unsigned long i, low, high, mid; 289 290 /* Do a binary search on the sorted kallsyms_offsets array. */ 291 low = 0; 292 high = kallsyms_num_syms; 293 294 while (high - low > 1) { 295 mid = low + (high - low) / 2; 296 if (kallsyms_sym_address(mid) <= addr) 297 low = mid; 298 else 299 high = mid; 300 } 301 302 /* 303 * Search for the first aliased symbol. Aliased 304 * symbols are symbols with the same address. 305 */ 306 while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) 307 --low; 308 309 symbol_start = kallsyms_sym_address(low); 310 311 /* Search for next non-aliased symbol. */ 312 for (i = low + 1; i < kallsyms_num_syms; i++) { 313 if (kallsyms_sym_address(i) > symbol_start) { 314 symbol_end = kallsyms_sym_address(i); 315 break; 316 } 317 } 318 319 /* If we found no next symbol, we use the end of the section. */ 320 if (!symbol_end) { 321 if (is_kernel_inittext(addr)) 322 symbol_end = (unsigned long)_einittext; 323 else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) 324 symbol_end = (unsigned long)_end; 325 else 326 symbol_end = (unsigned long)_etext; 327 } 328 329 if (symbolsize) 330 *symbolsize = symbol_end - symbol_start; 331 if (offset) 332 *offset = addr - symbol_start; 333 334 return low; 335 } 336 337 /* 338 * Lookup an address but don't bother to find any names. 339 */ 340 int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, 341 unsigned long *offset) 342 { 343 char namebuf[KSYM_NAME_LEN]; 344 345 if (is_ksym_addr(addr)) { 346 get_symbol_pos(addr, symbolsize, offset); 347 return 1; 348 } 349 return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) || 350 !!__bpf_address_lookup(addr, symbolsize, offset, namebuf); 351 } 352 353 static int kallsyms_lookup_buildid(unsigned long addr, 354 unsigned long *symbolsize, 355 unsigned long *offset, char **modname, 356 const unsigned char **modbuildid, char *namebuf) 357 { 358 int ret; 359 360 namebuf[KSYM_NAME_LEN - 1] = 0; 361 namebuf[0] = 0; 362 363 if (is_ksym_addr(addr)) { 364 unsigned long pos; 365 366 pos = get_symbol_pos(addr, symbolsize, offset); 367 /* Grab name */ 368 kallsyms_expand_symbol(get_symbol_offset(pos), 369 namebuf, KSYM_NAME_LEN); 370 if (modname) 371 *modname = NULL; 372 if (modbuildid) 373 *modbuildid = NULL; 374 375 return strlen(namebuf); 376 } 377 378 /* See if it's in a module or a BPF JITed image. */ 379 ret = module_address_lookup(addr, symbolsize, offset, 380 modname, modbuildid, namebuf); 381 if (!ret) 382 ret = bpf_address_lookup(addr, symbolsize, 383 offset, modname, namebuf); 384 385 if (!ret) 386 ret = ftrace_mod_address_lookup(addr, symbolsize, 387 offset, modname, namebuf); 388 389 return ret; 390 } 391 392 /* 393 * Lookup an address 394 * - modname is set to NULL if it's in the kernel. 395 * - We guarantee that the returned name is valid until we reschedule even if. 396 * It resides in a module. 397 * - We also guarantee that modname will be valid until rescheduled. 398 */ 399 const char *kallsyms_lookup(unsigned long addr, 400 unsigned long *symbolsize, 401 unsigned long *offset, 402 char **modname, char *namebuf) 403 { 404 int ret = kallsyms_lookup_buildid(addr, symbolsize, offset, modname, 405 NULL, namebuf); 406 407 if (!ret) 408 return NULL; 409 410 return namebuf; 411 } 412 413 int lookup_symbol_name(unsigned long addr, char *symname) 414 { 415 symname[0] = '\0'; 416 symname[KSYM_NAME_LEN - 1] = '\0'; 417 418 if (is_ksym_addr(addr)) { 419 unsigned long pos; 420 421 pos = get_symbol_pos(addr, NULL, NULL); 422 /* Grab name */ 423 kallsyms_expand_symbol(get_symbol_offset(pos), 424 symname, KSYM_NAME_LEN); 425 return 0; 426 } 427 /* See if it's in a module. */ 428 return lookup_module_symbol_name(addr, symname); 429 } 430 431 /* Look up a kernel symbol and return it in a text buffer. */ 432 static int __sprint_symbol(char *buffer, unsigned long address, 433 int symbol_offset, int add_offset, int add_buildid) 434 { 435 char *modname; 436 const unsigned char *buildid; 437 unsigned long offset, size; 438 int len; 439 440 address += symbol_offset; 441 len = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid, 442 buffer); 443 if (!len) 444 return sprintf(buffer, "0x%lx", address - symbol_offset); 445 446 offset -= symbol_offset; 447 448 if (add_offset) 449 len += sprintf(buffer + len, "+%#lx/%#lx", offset, size); 450 451 if (modname) { 452 len += sprintf(buffer + len, " [%s", modname); 453 #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) 454 if (add_buildid && buildid) { 455 /* build ID should match length of sprintf */ 456 #if IS_ENABLED(CONFIG_MODULES) 457 static_assert(sizeof(typeof_member(struct module, build_id)) == 20); 458 #endif 459 len += sprintf(buffer + len, " %20phN", buildid); 460 } 461 #endif 462 len += sprintf(buffer + len, "]"); 463 } 464 465 return len; 466 } 467 468 /** 469 * sprint_symbol - Look up a kernel symbol and return it in a text buffer 470 * @buffer: buffer to be stored 471 * @address: address to lookup 472 * 473 * This function looks up a kernel symbol with @address and stores its name, 474 * offset, size and module name to @buffer if possible. If no symbol was found, 475 * just saves its @address as is. 476 * 477 * This function returns the number of bytes stored in @buffer. 478 */ 479 int sprint_symbol(char *buffer, unsigned long address) 480 { 481 return __sprint_symbol(buffer, address, 0, 1, 0); 482 } 483 EXPORT_SYMBOL_GPL(sprint_symbol); 484 485 /** 486 * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer 487 * @buffer: buffer to be stored 488 * @address: address to lookup 489 * 490 * This function looks up a kernel symbol with @address and stores its name, 491 * offset, size, module name and module build ID to @buffer if possible. If no 492 * symbol was found, just saves its @address as is. 493 * 494 * This function returns the number of bytes stored in @buffer. 495 */ 496 int sprint_symbol_build_id(char *buffer, unsigned long address) 497 { 498 return __sprint_symbol(buffer, address, 0, 1, 1); 499 } 500 EXPORT_SYMBOL_GPL(sprint_symbol_build_id); 501 502 /** 503 * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer 504 * @buffer: buffer to be stored 505 * @address: address to lookup 506 * 507 * This function looks up a kernel symbol with @address and stores its name 508 * and module name to @buffer if possible. If no symbol was found, just saves 509 * its @address as is. 510 * 511 * This function returns the number of bytes stored in @buffer. 512 */ 513 int sprint_symbol_no_offset(char *buffer, unsigned long address) 514 { 515 return __sprint_symbol(buffer, address, 0, 0, 0); 516 } 517 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset); 518 519 /** 520 * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer 521 * @buffer: buffer to be stored 522 * @address: address to lookup 523 * 524 * This function is for stack backtrace and does the same thing as 525 * sprint_symbol() but with modified/decreased @address. If there is a 526 * tail-call to the function marked "noreturn", gcc optimized out code after 527 * the call so that the stack-saved return address could point outside of the 528 * caller. This function ensures that kallsyms will find the original caller 529 * by decreasing @address. 530 * 531 * This function returns the number of bytes stored in @buffer. 532 */ 533 int sprint_backtrace(char *buffer, unsigned long address) 534 { 535 return __sprint_symbol(buffer, address, -1, 1, 0); 536 } 537 538 /** 539 * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer 540 * @buffer: buffer to be stored 541 * @address: address to lookup 542 * 543 * This function is for stack backtrace and does the same thing as 544 * sprint_symbol() but with modified/decreased @address. If there is a 545 * tail-call to the function marked "noreturn", gcc optimized out code after 546 * the call so that the stack-saved return address could point outside of the 547 * caller. This function ensures that kallsyms will find the original caller 548 * by decreasing @address. This function also appends the module build ID to 549 * the @buffer if @address is within a kernel module. 550 * 551 * This function returns the number of bytes stored in @buffer. 552 */ 553 int sprint_backtrace_build_id(char *buffer, unsigned long address) 554 { 555 return __sprint_symbol(buffer, address, -1, 1, 1); 556 } 557 558 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ 559 struct kallsym_iter { 560 loff_t pos; 561 loff_t pos_mod_end; 562 loff_t pos_ftrace_mod_end; 563 loff_t pos_bpf_end; 564 unsigned long value; 565 unsigned int nameoff; /* If iterating in core kernel symbols. */ 566 char type; 567 char name[KSYM_NAME_LEN]; 568 char module_name[MODULE_NAME_LEN]; 569 int exported; 570 int show_value; 571 }; 572 573 static int get_ksymbol_mod(struct kallsym_iter *iter) 574 { 575 int ret = module_get_kallsym(iter->pos - kallsyms_num_syms, 576 &iter->value, &iter->type, 577 iter->name, iter->module_name, 578 &iter->exported); 579 if (ret < 0) { 580 iter->pos_mod_end = iter->pos; 581 return 0; 582 } 583 584 return 1; 585 } 586 587 /* 588 * ftrace_mod_get_kallsym() may also get symbols for pages allocated for ftrace 589 * purposes. In that case "__builtin__ftrace" is used as a module name, even 590 * though "__builtin__ftrace" is not a module. 591 */ 592 static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) 593 { 594 int ret = ftrace_mod_get_kallsym(iter->pos - iter->pos_mod_end, 595 &iter->value, &iter->type, 596 iter->name, iter->module_name, 597 &iter->exported); 598 if (ret < 0) { 599 iter->pos_ftrace_mod_end = iter->pos; 600 return 0; 601 } 602 603 return 1; 604 } 605 606 static int get_ksymbol_bpf(struct kallsym_iter *iter) 607 { 608 int ret; 609 610 strscpy(iter->module_name, "bpf", MODULE_NAME_LEN); 611 iter->exported = 0; 612 ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, 613 &iter->value, &iter->type, 614 iter->name); 615 if (ret < 0) { 616 iter->pos_bpf_end = iter->pos; 617 return 0; 618 } 619 620 return 1; 621 } 622 623 /* 624 * This uses "__builtin__kprobes" as a module name for symbols for pages 625 * allocated for kprobes' purposes, even though "__builtin__kprobes" is not a 626 * module. 627 */ 628 static int get_ksymbol_kprobe(struct kallsym_iter *iter) 629 { 630 strscpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); 631 iter->exported = 0; 632 return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, 633 &iter->value, &iter->type, 634 iter->name) < 0 ? 0 : 1; 635 } 636 637 /* Returns space to next name. */ 638 static unsigned long get_ksymbol_core(struct kallsym_iter *iter) 639 { 640 unsigned off = iter->nameoff; 641 642 iter->module_name[0] = '\0'; 643 iter->value = kallsyms_sym_address(iter->pos); 644 645 iter->type = kallsyms_get_symbol_type(off); 646 647 off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); 648 649 return off - iter->nameoff; 650 } 651 652 static void reset_iter(struct kallsym_iter *iter, loff_t new_pos) 653 { 654 iter->name[0] = '\0'; 655 iter->nameoff = get_symbol_offset(new_pos); 656 iter->pos = new_pos; 657 if (new_pos == 0) { 658 iter->pos_mod_end = 0; 659 iter->pos_ftrace_mod_end = 0; 660 iter->pos_bpf_end = 0; 661 } 662 } 663 664 /* 665 * The end position (last + 1) of each additional kallsyms section is recorded 666 * in iter->pos_..._end as each section is added, and so can be used to 667 * determine which get_ksymbol_...() function to call next. 668 */ 669 static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) 670 { 671 iter->pos = pos; 672 673 if ((!iter->pos_mod_end || iter->pos_mod_end > pos) && 674 get_ksymbol_mod(iter)) 675 return 1; 676 677 if ((!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > pos) && 678 get_ksymbol_ftrace_mod(iter)) 679 return 1; 680 681 if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) && 682 get_ksymbol_bpf(iter)) 683 return 1; 684 685 return get_ksymbol_kprobe(iter); 686 } 687 688 /* Returns false if pos at or past end of file. */ 689 static int update_iter(struct kallsym_iter *iter, loff_t pos) 690 { 691 /* Module symbols can be accessed randomly. */ 692 if (pos >= kallsyms_num_syms) 693 return update_iter_mod(iter, pos); 694 695 /* If we're not on the desired position, reset to new position. */ 696 if (pos != iter->pos) 697 reset_iter(iter, pos); 698 699 iter->nameoff += get_ksymbol_core(iter); 700 iter->pos++; 701 702 return 1; 703 } 704 705 static void *s_next(struct seq_file *m, void *p, loff_t *pos) 706 { 707 (*pos)++; 708 709 if (!update_iter(m->private, *pos)) 710 return NULL; 711 return p; 712 } 713 714 static void *s_start(struct seq_file *m, loff_t *pos) 715 { 716 if (!update_iter(m->private, *pos)) 717 return NULL; 718 return m->private; 719 } 720 721 static void s_stop(struct seq_file *m, void *p) 722 { 723 } 724 725 static int s_show(struct seq_file *m, void *p) 726 { 727 void *value; 728 struct kallsym_iter *iter = m->private; 729 730 /* Some debugging symbols have no name. Ignore them. */ 731 if (!iter->name[0]) 732 return 0; 733 734 value = iter->show_value ? (void *)iter->value : NULL; 735 736 if (iter->module_name[0]) { 737 char type; 738 739 /* 740 * Label it "global" if it is exported, 741 * "local" if not exported. 742 */ 743 type = iter->exported ? toupper(iter->type) : 744 tolower(iter->type); 745 seq_printf(m, "%px %c %s\t[%s]\n", value, 746 type, iter->name, iter->module_name); 747 } else 748 seq_printf(m, "%px %c %s\n", value, 749 iter->type, iter->name); 750 return 0; 751 } 752 753 static const struct seq_operations kallsyms_op = { 754 .start = s_start, 755 .next = s_next, 756 .stop = s_stop, 757 .show = s_show 758 }; 759 760 #ifdef CONFIG_BPF_SYSCALL 761 762 struct bpf_iter__ksym { 763 __bpf_md_ptr(struct bpf_iter_meta *, meta); 764 __bpf_md_ptr(struct kallsym_iter *, ksym); 765 }; 766 767 static int ksym_prog_seq_show(struct seq_file *m, bool in_stop) 768 { 769 struct bpf_iter__ksym ctx; 770 struct bpf_iter_meta meta; 771 struct bpf_prog *prog; 772 773 meta.seq = m; 774 prog = bpf_iter_get_info(&meta, in_stop); 775 if (!prog) 776 return 0; 777 778 ctx.meta = &meta; 779 ctx.ksym = m ? m->private : NULL; 780 return bpf_iter_run_prog(prog, &ctx); 781 } 782 783 static int bpf_iter_ksym_seq_show(struct seq_file *m, void *p) 784 { 785 return ksym_prog_seq_show(m, false); 786 } 787 788 static void bpf_iter_ksym_seq_stop(struct seq_file *m, void *p) 789 { 790 if (!p) 791 (void) ksym_prog_seq_show(m, true); 792 else 793 s_stop(m, p); 794 } 795 796 static const struct seq_operations bpf_iter_ksym_ops = { 797 .start = s_start, 798 .next = s_next, 799 .stop = bpf_iter_ksym_seq_stop, 800 .show = bpf_iter_ksym_seq_show, 801 }; 802 803 static int bpf_iter_ksym_init(void *priv_data, struct bpf_iter_aux_info *aux) 804 { 805 struct kallsym_iter *iter = priv_data; 806 807 reset_iter(iter, 0); 808 809 /* cache here as in kallsyms_open() case; use current process 810 * credentials to tell BPF iterators if values should be shown. 811 */ 812 iter->show_value = kallsyms_show_value(current_cred()); 813 814 return 0; 815 } 816 817 DEFINE_BPF_ITER_FUNC(ksym, struct bpf_iter_meta *meta, struct kallsym_iter *ksym) 818 819 static const struct bpf_iter_seq_info ksym_iter_seq_info = { 820 .seq_ops = &bpf_iter_ksym_ops, 821 .init_seq_private = bpf_iter_ksym_init, 822 .fini_seq_private = NULL, 823 .seq_priv_size = sizeof(struct kallsym_iter), 824 }; 825 826 static struct bpf_iter_reg ksym_iter_reg_info = { 827 .target = "ksym", 828 .feature = BPF_ITER_RESCHED, 829 .ctx_arg_info_size = 1, 830 .ctx_arg_info = { 831 { offsetof(struct bpf_iter__ksym, ksym), 832 PTR_TO_BTF_ID_OR_NULL }, 833 }, 834 .seq_info = &ksym_iter_seq_info, 835 }; 836 837 BTF_ID_LIST_SINGLE(btf_ksym_iter_id, struct, kallsym_iter) 838 839 static int __init bpf_ksym_iter_register(void) 840 { 841 ksym_iter_reg_info.ctx_arg_info[0].btf_id = *btf_ksym_iter_id; 842 return bpf_iter_reg_target(&ksym_iter_reg_info); 843 } 844 845 late_initcall(bpf_ksym_iter_register); 846 847 #endif /* CONFIG_BPF_SYSCALL */ 848 849 static int kallsyms_open(struct inode *inode, struct file *file) 850 { 851 /* 852 * We keep iterator in m->private, since normal case is to 853 * s_start from where we left off, so we avoid doing 854 * using get_symbol_offset for every symbol. 855 */ 856 struct kallsym_iter *iter; 857 iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); 858 if (!iter) 859 return -ENOMEM; 860 reset_iter(iter, 0); 861 862 /* 863 * Instead of checking this on every s_show() call, cache 864 * the result here at open time. 865 */ 866 iter->show_value = kallsyms_show_value(file->f_cred); 867 return 0; 868 } 869 870 #ifdef CONFIG_KGDB_KDB 871 const char *kdb_walk_kallsyms(loff_t *pos) 872 { 873 static struct kallsym_iter kdb_walk_kallsyms_iter; 874 if (*pos == 0) { 875 memset(&kdb_walk_kallsyms_iter, 0, 876 sizeof(kdb_walk_kallsyms_iter)); 877 reset_iter(&kdb_walk_kallsyms_iter, 0); 878 } 879 while (1) { 880 if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) 881 return NULL; 882 ++*pos; 883 /* Some debugging symbols have no name. Ignore them. */ 884 if (kdb_walk_kallsyms_iter.name[0]) 885 return kdb_walk_kallsyms_iter.name; 886 } 887 } 888 #endif /* CONFIG_KGDB_KDB */ 889 890 static const struct proc_ops kallsyms_proc_ops = { 891 .proc_open = kallsyms_open, 892 .proc_read = seq_read, 893 .proc_lseek = seq_lseek, 894 .proc_release = seq_release_private, 895 }; 896 897 static int __init kallsyms_init(void) 898 { 899 proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); 900 return 0; 901 } 902 device_initcall(kallsyms_init); 903