1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Dynamic loading of modules into the kernel. 4 * 5 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996 6 * Rewritten again by Rusty Russell, 2002 7 */ 8 9 #ifndef _LINUX_MODULE_H 10 #define _LINUX_MODULE_H 11 12 #include <linux/list.h> 13 #include <linux/stat.h> 14 #include <linux/buildid.h> 15 #include <linux/compiler.h> 16 #include <linux/cache.h> 17 #include <linux/cleanup.h> 18 #include <linux/kmod.h> 19 #include <linux/init.h> 20 #include <linux/elf.h> 21 #include <linux/stringify.h> 22 #include <linux/kobject.h> 23 #include <linux/moduleparam.h> 24 #include <linux/jump_label.h> 25 #include <linux/export.h> 26 #include <linux/rbtree_latch.h> 27 #include <linux/error-injection.h> 28 #include <linux/tracepoint-defs.h> 29 #include <linux/srcu.h> 30 #include <linux/static_call_types.h> 31 #include <linux/dynamic_debug.h> 32 33 #include <linux/percpu.h> 34 #include <asm/module.h> 35 36 #define MODULE_NAME_LEN __MODULE_NAME_LEN 37 38 struct modversion_info { 39 unsigned long crc; 40 char name[MODULE_NAME_LEN]; 41 }; 42 43 struct module; 44 struct exception_table_entry; 45 46 struct module_kobject { 47 struct kobject kobj; 48 struct module *mod; 49 struct kobject *drivers_dir; 50 struct module_param_attrs *mp; 51 struct completion *kobj_completion; 52 } __randomize_layout; 53 54 struct module_attribute { 55 struct attribute attr; 56 ssize_t (*show)(const struct module_attribute *, struct module_kobject *, 57 char *); 58 ssize_t (*store)(const struct module_attribute *, struct module_kobject *, 59 const char *, size_t count); 60 void (*setup)(struct module *, const char *); 61 int (*test)(struct module *); 62 void (*free)(struct module *); 63 }; 64 65 struct module_version_attribute { 66 struct module_attribute mattr; 67 const char *module_name; 68 const char *version; 69 }; 70 71 extern ssize_t __modver_version_show(const struct module_attribute *, 72 struct module_kobject *, char *); 73 74 extern const struct module_attribute module_uevent; 75 76 /* These are either module local, or the kernel's dummy ones. */ 77 extern int init_module(void); 78 extern void cleanup_module(void); 79 80 #ifndef MODULE 81 /** 82 * module_init() - driver initialization entry point 83 * @x: function to be run at kernel boot time or module insertion 84 * 85 * module_init() will either be called during do_initcalls() (if 86 * builtin) or at module insertion time (if a module). There can only 87 * be one per module. 88 */ 89 #define module_init(x) __initcall(x); 90 91 /** 92 * module_exit() - driver exit entry point 93 * @x: function to be run when driver is removed 94 * 95 * module_exit() will wrap the driver clean-up code 96 * with cleanup_module() when used with rmmod when 97 * the driver is a module. If the driver is statically 98 * compiled into the kernel, module_exit() has no effect. 99 * There can only be one per module. 100 */ 101 #define module_exit(x) __exitcall(x); 102 103 #else /* MODULE */ 104 105 /* 106 * In most cases loadable modules do not need custom 107 * initcall levels. There are still some valid cases where 108 * a driver may be needed early if built in, and does not 109 * matter when built as a loadable module. Like bus 110 * snooping debug drivers. 111 */ 112 #define early_initcall(fn) module_init(fn) 113 #define core_initcall(fn) module_init(fn) 114 #define core_initcall_sync(fn) module_init(fn) 115 #define postcore_initcall(fn) module_init(fn) 116 #define postcore_initcall_sync(fn) module_init(fn) 117 #define arch_initcall(fn) module_init(fn) 118 #define subsys_initcall(fn) module_init(fn) 119 #define subsys_initcall_sync(fn) module_init(fn) 120 #define fs_initcall(fn) module_init(fn) 121 #define fs_initcall_sync(fn) module_init(fn) 122 #define rootfs_initcall(fn) module_init(fn) 123 #define device_initcall(fn) module_init(fn) 124 #define device_initcall_sync(fn) module_init(fn) 125 #define late_initcall(fn) module_init(fn) 126 #define late_initcall_sync(fn) module_init(fn) 127 128 #define console_initcall(fn) module_init(fn) 129 130 /* Each module must use one module_init(). */ 131 #define module_init(initfn) \ 132 static inline initcall_t __maybe_unused __inittest(void) \ 133 { return initfn; } \ 134 int init_module(void) __copy(initfn) \ 135 __attribute__((alias(#initfn))); \ 136 ___ADDRESSABLE(init_module, __initdata); 137 138 /* This is only required if you want to be unloadable. */ 139 #define module_exit(exitfn) \ 140 static inline exitcall_t __maybe_unused __exittest(void) \ 141 { return exitfn; } \ 142 void cleanup_module(void) __copy(exitfn) \ 143 __attribute__((alias(#exitfn))); \ 144 ___ADDRESSABLE(cleanup_module, __exitdata); 145 146 #endif 147 148 /* This means "can be init if no module support, otherwise module load 149 may call it." */ 150 #ifdef CONFIG_MODULES 151 #define __init_or_module 152 #define __initdata_or_module 153 #define __initconst_or_module 154 #define __INIT_OR_MODULE .text 155 #define __INITDATA_OR_MODULE .data 156 #define __INITRODATA_OR_MODULE .section ".rodata","a",%progbits 157 #else 158 #define __init_or_module __init 159 #define __initdata_or_module __initdata 160 #define __initconst_or_module __initconst 161 #define __INIT_OR_MODULE __INIT 162 #define __INITDATA_OR_MODULE __INITDATA 163 #define __INITRODATA_OR_MODULE __INITRODATA 164 #endif /*CONFIG_MODULES*/ 165 166 struct module_kobject *lookup_or_create_module_kobject(const char *name); 167 168 /* For userspace: you can also call me... */ 169 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias) 170 171 /* Soft module dependencies. See man modprobe.d for details. 172 * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz") 173 */ 174 #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep) 175 176 /* 177 * Weak module dependencies. See man modprobe.d for details. 178 * Example: MODULE_WEAKDEP("module-foo") 179 */ 180 #define MODULE_WEAKDEP(_weakdep) MODULE_INFO(weakdep, _weakdep) 181 182 /* 183 * MODULE_FILE is used for generating modules.builtin 184 * So, make it no-op when this is being built as a module 185 */ 186 #ifdef MODULE 187 #define MODULE_FILE 188 #else 189 #define MODULE_FILE MODULE_INFO(file, KBUILD_MODFILE); 190 #endif 191 192 /* 193 * The following license idents are currently accepted as indicating free 194 * software modules 195 * 196 * "GPL" [GNU Public License v2] 197 * "GPL v2" [GNU Public License v2] 198 * "GPL and additional rights" [GNU Public License v2 rights and more] 199 * "Dual BSD/GPL" [GNU Public License v2 200 * or BSD license choice] 201 * "Dual MIT/GPL" [GNU Public License v2 202 * or MIT license choice] 203 * "Dual MPL/GPL" [GNU Public License v2 204 * or Mozilla license choice] 205 * 206 * The following other idents are available 207 * 208 * "Proprietary" [Non free products] 209 * 210 * Both "GPL v2" and "GPL" (the latter also in dual licensed strings) are 211 * merely stating that the module is licensed under the GPL v2, but are not 212 * telling whether "GPL v2 only" or "GPL v2 or later". The reason why there 213 * are two variants is a historic and failed attempt to convey more 214 * information in the MODULE_LICENSE string. For module loading the 215 * "only/or later" distinction is completely irrelevant and does neither 216 * replace the proper license identifiers in the corresponding source file 217 * nor amends them in any way. The sole purpose is to make the 218 * 'Proprietary' flagging work and to refuse to bind symbols which are 219 * exported with EXPORT_SYMBOL_GPL when a non free module is loaded. 220 * 221 * In the same way "BSD" is not a clear license information. It merely 222 * states, that the module is licensed under one of the compatible BSD 223 * license variants. The detailed and correct license information is again 224 * to be found in the corresponding source files. 225 * 226 * There are dual licensed components, but when running with Linux it is the 227 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL 228 * is a GPL combined work. 229 * 230 * This exists for several reasons 231 * 1. So modinfo can show license info for users wanting to vet their setup 232 * is free 233 * 2. So the community can ignore bug reports including proprietary modules 234 * 3. So vendors can do likewise based on their own policies 235 */ 236 #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license) 237 238 /* 239 * Author(s), use "Name <email>" or just "Name", for multiple 240 * authors use multiple MODULE_AUTHOR() statements/lines. 241 */ 242 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author) 243 244 /* What your module does. */ 245 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description) 246 247 /* 248 * Format: __mod_device_table__kmod_<modname>__<type>__<name> 249 * Parts of the string `__kmod_` and `__` are used as delimiters when parsing 250 * a symbol in file2alias.c 251 */ 252 #define __mod_device_table(type, name) \ 253 __PASTE(__mod_device_table__, \ 254 __PASTE(__KBUILD_MODNAME, \ 255 __PASTE(__, \ 256 __PASTE(type, \ 257 __PASTE(__, name))))) 258 259 /* Creates an alias so file2alias.c can find device table. */ 260 #define MODULE_DEVICE_TABLE(type, name) \ 261 static typeof(name) __mod_device_table(type, name) \ 262 __attribute__ ((used, alias(__stringify(name)))) 263 264 /* Version of form [<epoch>:]<version>[-<extra-version>]. 265 * Or for CVS/RCS ID version, everything but the number is stripped. 266 * <epoch>: A (small) unsigned integer which allows you to start versions 267 * anew. If not mentioned, it's zero. eg. "2:1.0" is after 268 * "1:2.0". 269 270 * <version>: The <version> may contain only alphanumerics and the 271 * character `.'. Ordered by numeric sort for numeric parts, 272 * ascii sort for ascii parts (as per RPM or DEB algorithm). 273 274 * <extraversion>: Like <version>, but inserted for local 275 * customizations, eg "rh3" or "rusty1". 276 277 * Using this automatically adds a checksum of the .c files and the 278 * local headers in "srcversion". 279 */ 280 281 #if defined(MODULE) || !defined(CONFIG_SYSFS) 282 #define MODULE_VERSION(_version) MODULE_INFO(version, _version) 283 #else 284 #define MODULE_VERSION(_version) \ 285 MODULE_INFO(version, _version); \ 286 static const struct module_version_attribute __modver_attr \ 287 __used __section("__modver") \ 288 __aligned(__alignof__(struct module_version_attribute)) \ 289 = { \ 290 .mattr = { \ 291 .attr = { \ 292 .name = "version", \ 293 .mode = S_IRUGO, \ 294 }, \ 295 .show = __modver_version_show, \ 296 }, \ 297 .module_name = KBUILD_MODNAME, \ 298 .version = _version, \ 299 } 300 #endif 301 302 /* Optional firmware file (or files) needed by the module 303 * format is simply firmware file name. Multiple firmware 304 * files require multiple MODULE_FIRMWARE() specifiers */ 305 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware) 306 307 #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, ns) 308 309 struct notifier_block; 310 311 enum module_state { 312 MODULE_STATE_LIVE, /* Normal state. */ 313 MODULE_STATE_COMING, /* Full formed, running module_init. */ 314 MODULE_STATE_GOING, /* Going away. */ 315 MODULE_STATE_UNFORMED, /* Still setting it up. */ 316 }; 317 318 struct mod_tree_node { 319 struct module *mod; 320 struct latch_tree_node node; 321 }; 322 323 enum mod_mem_type { 324 MOD_TEXT = 0, 325 MOD_DATA, 326 MOD_RODATA, 327 MOD_RO_AFTER_INIT, 328 MOD_INIT_TEXT, 329 MOD_INIT_DATA, 330 MOD_INIT_RODATA, 331 332 MOD_MEM_NUM_TYPES, 333 MOD_INVALID = -1, 334 }; 335 336 #define mod_mem_type_is_init(type) \ 337 ((type) == MOD_INIT_TEXT || \ 338 (type) == MOD_INIT_DATA || \ 339 (type) == MOD_INIT_RODATA) 340 341 #define mod_mem_type_is_core(type) (!mod_mem_type_is_init(type)) 342 343 #define mod_mem_type_is_text(type) \ 344 ((type) == MOD_TEXT || \ 345 (type) == MOD_INIT_TEXT) 346 347 #define mod_mem_type_is_data(type) (!mod_mem_type_is_text(type)) 348 349 #define mod_mem_type_is_core_data(type) \ 350 (mod_mem_type_is_core(type) && \ 351 mod_mem_type_is_data(type)) 352 353 #define for_each_mod_mem_type(type) \ 354 for (enum mod_mem_type (type) = 0; \ 355 (type) < MOD_MEM_NUM_TYPES; (type)++) 356 357 #define for_class_mod_mem_type(type, class) \ 358 for_each_mod_mem_type(type) \ 359 if (mod_mem_type_is_##class(type)) 360 361 struct module_memory { 362 void *base; 363 bool is_rox; 364 unsigned int size; 365 366 #ifdef CONFIG_MODULES_TREE_LOOKUP 367 struct mod_tree_node mtn; 368 #endif 369 }; 370 371 #ifdef CONFIG_MODULES_TREE_LOOKUP 372 /* Only touch one cacheline for common rbtree-for-core-layout case. */ 373 #define __module_memory_align ____cacheline_aligned 374 #else 375 #define __module_memory_align 376 #endif 377 378 struct mod_kallsyms { 379 Elf_Sym *symtab; 380 unsigned int num_symtab; 381 char *strtab; 382 char *typetab; 383 }; 384 385 #ifdef CONFIG_LIVEPATCH 386 /** 387 * struct klp_modinfo - ELF information preserved from the livepatch module 388 * 389 * @hdr: ELF header 390 * @sechdrs: Section header table 391 * @secstrings: String table for the section headers 392 * @symndx: The symbol table section index 393 */ 394 struct klp_modinfo { 395 Elf_Ehdr hdr; 396 Elf_Shdr *sechdrs; 397 char *secstrings; 398 unsigned int symndx; 399 }; 400 #endif 401 402 struct module { 403 enum module_state state; 404 405 /* Member of list of modules */ 406 struct list_head list; 407 408 /* Unique handle for this module */ 409 char name[MODULE_NAME_LEN]; 410 411 #ifdef CONFIG_STACKTRACE_BUILD_ID 412 /* Module build ID */ 413 unsigned char build_id[BUILD_ID_SIZE_MAX]; 414 #endif 415 416 /* Sysfs stuff. */ 417 struct module_kobject mkobj; 418 struct module_attribute *modinfo_attrs; 419 const char *version; 420 const char *srcversion; 421 struct kobject *holders_dir; 422 423 /* Exported symbols */ 424 const struct kernel_symbol *syms; 425 const u32 *crcs; 426 unsigned int num_syms; 427 428 #ifdef CONFIG_ARCH_USES_CFI_TRAPS 429 s32 *kcfi_traps; 430 s32 *kcfi_traps_end; 431 #endif 432 433 /* Kernel parameters. */ 434 #ifdef CONFIG_SYSFS 435 struct mutex param_lock; 436 #endif 437 struct kernel_param *kp; 438 unsigned int num_kp; 439 440 /* GPL-only exported symbols. */ 441 unsigned int num_gpl_syms; 442 const struct kernel_symbol *gpl_syms; 443 const u32 *gpl_crcs; 444 bool using_gplonly_symbols; 445 446 #ifdef CONFIG_MODULE_SIG 447 /* Signature was verified. */ 448 bool sig_ok; 449 #endif 450 451 bool async_probe_requested; 452 453 /* Exception table */ 454 unsigned int num_exentries; 455 struct exception_table_entry *extable; 456 457 /* Startup function. */ 458 int (*init)(void); 459 460 struct module_memory mem[MOD_MEM_NUM_TYPES] __module_memory_align; 461 462 /* Arch-specific module values */ 463 struct mod_arch_specific arch; 464 465 unsigned long taints; /* same bits as kernel:taint_flags */ 466 467 #ifdef CONFIG_GENERIC_BUG 468 /* Support for BUG */ 469 unsigned num_bugs; 470 struct list_head bug_list; 471 struct bug_entry *bug_table; 472 #endif 473 474 #ifdef CONFIG_KALLSYMS 475 /* Protected by RCU and/or module_mutex: use rcu_dereference() */ 476 struct mod_kallsyms __rcu *kallsyms; 477 struct mod_kallsyms core_kallsyms; 478 479 /* Section attributes */ 480 struct module_sect_attrs *sect_attrs; 481 482 /* Notes attributes */ 483 struct module_notes_attrs *notes_attrs; 484 #endif 485 486 /* The command line arguments (may be mangled). People like 487 keeping pointers to this stuff */ 488 char *args; 489 490 #ifdef CONFIG_SMP 491 /* Per-cpu data. */ 492 void __percpu *percpu; 493 unsigned int percpu_size; 494 #endif 495 void *noinstr_text_start; 496 unsigned int noinstr_text_size; 497 498 #ifdef CONFIG_TRACEPOINTS 499 unsigned int num_tracepoints; 500 tracepoint_ptr_t *tracepoints_ptrs; 501 #endif 502 #ifdef CONFIG_TREE_SRCU 503 unsigned int num_srcu_structs; 504 struct srcu_struct **srcu_struct_ptrs; 505 #endif 506 #ifdef CONFIG_BPF_EVENTS 507 unsigned int num_bpf_raw_events; 508 struct bpf_raw_event_map *bpf_raw_events; 509 #endif 510 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 511 unsigned int btf_data_size; 512 unsigned int btf_base_data_size; 513 void *btf_data; 514 void *btf_base_data; 515 #endif 516 #ifdef CONFIG_JUMP_LABEL 517 struct jump_entry *jump_entries; 518 unsigned int num_jump_entries; 519 #endif 520 #ifdef CONFIG_TRACING 521 unsigned int num_trace_bprintk_fmt; 522 const char **trace_bprintk_fmt_start; 523 #endif 524 #ifdef CONFIG_EVENT_TRACING 525 struct trace_event_call **trace_events; 526 unsigned int num_trace_events; 527 struct trace_eval_map **trace_evals; 528 unsigned int num_trace_evals; 529 #endif 530 #ifdef CONFIG_DYNAMIC_FTRACE 531 unsigned int num_ftrace_callsites; 532 unsigned long *ftrace_callsites; 533 #endif 534 #ifdef CONFIG_KPROBES 535 void *kprobes_text_start; 536 unsigned int kprobes_text_size; 537 unsigned long *kprobe_blacklist; 538 unsigned int num_kprobe_blacklist; 539 #endif 540 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE 541 int num_static_call_sites; 542 struct static_call_site *static_call_sites; 543 #endif 544 #if IS_ENABLED(CONFIG_KUNIT) 545 int num_kunit_init_suites; 546 struct kunit_suite **kunit_init_suites; 547 int num_kunit_suites; 548 struct kunit_suite **kunit_suites; 549 #endif 550 551 552 #ifdef CONFIG_LIVEPATCH 553 bool klp; /* Is this a livepatch module? */ 554 bool klp_alive; 555 556 /* ELF information */ 557 struct klp_modinfo *klp_info; 558 #endif 559 560 #ifdef CONFIG_PRINTK_INDEX 561 unsigned int printk_index_size; 562 struct pi_entry **printk_index_start; 563 #endif 564 565 #ifdef CONFIG_MODULE_UNLOAD 566 /* What modules depend on me? */ 567 struct list_head source_list; 568 /* What modules do I depend on? */ 569 struct list_head target_list; 570 571 /* Destruction function. */ 572 void (*exit)(void); 573 574 atomic_t refcnt; 575 #endif 576 577 #ifdef CONFIG_CONSTRUCTORS 578 /* Constructor functions. */ 579 ctor_fn_t *ctors; 580 unsigned int num_ctors; 581 #endif 582 583 #ifdef CONFIG_FUNCTION_ERROR_INJECTION 584 struct error_injection_entry *ei_funcs; 585 unsigned int num_ei_funcs; 586 #endif 587 #ifdef CONFIG_DYNAMIC_DEBUG_CORE 588 struct _ddebug_info dyndbg_info; 589 #endif 590 } ____cacheline_aligned __randomize_layout; 591 #ifndef MODULE_ARCH_INIT 592 #define MODULE_ARCH_INIT {} 593 #endif 594 595 #ifdef CONFIG_MODULES 596 597 /* Get/put a kernel symbol (calls must be symmetric) */ 598 void *__symbol_get(const char *symbol); 599 void *__symbol_get_gpl(const char *symbol); 600 #define symbol_get(x) ({ \ 601 static const char __notrim[] \ 602 __used __section(".no_trim_symbol") = __stringify(x); \ 603 (typeof(&x))(__symbol_get(__stringify(x))); }) 604 605 #ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE 606 static inline unsigned long kallsyms_symbol_value(const Elf_Sym *sym) 607 { 608 return sym->st_value; 609 } 610 #endif 611 612 /* FIXME: It'd be nice to isolate modules during init, too, so they 613 aren't used before they (may) fail. But presently too much code 614 (IDE & SCSI) require entry into the module during init.*/ 615 static inline bool module_is_live(struct module *mod) 616 { 617 return mod->state != MODULE_STATE_GOING; 618 } 619 620 static inline bool module_is_coming(struct module *mod) 621 { 622 return mod->state == MODULE_STATE_COMING; 623 } 624 625 struct module *__module_text_address(unsigned long addr); 626 struct module *__module_address(unsigned long addr); 627 bool is_module_address(unsigned long addr); 628 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr); 629 bool is_module_percpu_address(unsigned long addr); 630 bool is_module_text_address(unsigned long addr); 631 632 static inline bool within_module_mem_type(unsigned long addr, 633 const struct module *mod, 634 enum mod_mem_type type) 635 { 636 unsigned long base, size; 637 638 base = (unsigned long)mod->mem[type].base; 639 size = mod->mem[type].size; 640 return addr - base < size; 641 } 642 643 static inline bool within_module_core(unsigned long addr, 644 const struct module *mod) 645 { 646 for_class_mod_mem_type(type, core) { 647 if (within_module_mem_type(addr, mod, type)) 648 return true; 649 } 650 return false; 651 } 652 653 static inline bool within_module_init(unsigned long addr, 654 const struct module *mod) 655 { 656 for_class_mod_mem_type(type, init) { 657 if (within_module_mem_type(addr, mod, type)) 658 return true; 659 } 660 return false; 661 } 662 663 static inline bool within_module(unsigned long addr, const struct module *mod) 664 { 665 return within_module_init(addr, mod) || within_module_core(addr, mod); 666 } 667 668 /* Search for module by name: must be in a RCU critical section. */ 669 struct module *find_module(const char *name); 670 671 extern void __noreturn __module_put_and_kthread_exit(struct module *mod, 672 long code); 673 #define module_put_and_kthread_exit(code) __module_put_and_kthread_exit(THIS_MODULE, code) 674 675 #ifdef CONFIG_MODULE_UNLOAD 676 int module_refcount(struct module *mod); 677 void __symbol_put(const char *symbol); 678 #define symbol_put(x) __symbol_put(__stringify(x)) 679 void symbol_put_addr(void *addr); 680 681 /* Sometimes we know we already have a refcount, and it's easier not 682 to handle the error case (which only happens with rmmod --wait). */ 683 extern void __module_get(struct module *module); 684 685 /** 686 * try_module_get() - take module refcount unless module is being removed 687 * @module: the module we should check for 688 * 689 * Only try to get a module reference count if the module is not being removed. 690 * This call will fail if the module is in the process of being removed. 691 * 692 * Care must also be taken to ensure the module exists and is alive prior to 693 * usage of this call. This can be gauranteed through two means: 694 * 695 * 1) Direct protection: you know an earlier caller must have increased the 696 * module reference through __module_get(). This can typically be achieved 697 * by having another entity other than the module itself increment the 698 * module reference count. 699 * 700 * 2) Implied protection: there is an implied protection against module 701 * removal. An example of this is the implied protection used by kernfs / 702 * sysfs. The sysfs store / read file operations are guaranteed to exist 703 * through the use of kernfs's active reference (see kernfs_active()) and a 704 * sysfs / kernfs file removal cannot happen unless the same file is not 705 * active. Therefore, if a sysfs file is being read or written to the module 706 * which created it must still exist. It is therefore safe to use 707 * try_module_get() on module sysfs store / read ops. 708 * 709 * One of the real values to try_module_get() is the module_is_live() check 710 * which ensures that the caller of try_module_get() can yield to userspace 711 * module removal requests and gracefully fail if the module is on its way out. 712 * 713 * Returns true if the reference count was successfully incremented. 714 */ 715 extern bool try_module_get(struct module *module); 716 717 /** 718 * module_put() - release a reference count to a module 719 * @module: the module we should release a reference count for 720 * 721 * If you successfully bump a reference count to a module with try_module_get(), 722 * when you are finished you must call module_put() to release that reference 723 * count. 724 */ 725 extern void module_put(struct module *module); 726 727 #else /*!CONFIG_MODULE_UNLOAD*/ 728 static inline bool try_module_get(struct module *module) 729 { 730 return !module || module_is_live(module); 731 } 732 static inline void module_put(struct module *module) 733 { 734 } 735 static inline void __module_get(struct module *module) 736 { 737 } 738 #define symbol_put(x) do { } while (0) 739 #define symbol_put_addr(p) do { } while (0) 740 741 #endif /* CONFIG_MODULE_UNLOAD */ 742 743 /* This is a #define so the string doesn't get put in every .o file */ 744 #define module_name(mod) \ 745 ({ \ 746 struct module *__mod = (mod); \ 747 __mod ? __mod->name : "kernel"; \ 748 }) 749 750 /* Dereference module function descriptor */ 751 void *dereference_module_function_descriptor(struct module *mod, void *ptr); 752 753 int register_module_notifier(struct notifier_block *nb); 754 int unregister_module_notifier(struct notifier_block *nb); 755 756 extern void print_modules(void); 757 758 static inline bool module_requested_async_probing(struct module *module) 759 { 760 return module && module->async_probe_requested; 761 } 762 763 static inline bool is_livepatch_module(struct module *mod) 764 { 765 #ifdef CONFIG_LIVEPATCH 766 return mod->klp; 767 #else 768 return false; 769 #endif 770 } 771 772 void set_module_sig_enforced(void); 773 774 void module_for_each_mod(int(*func)(struct module *mod, void *data), void *data); 775 776 #else /* !CONFIG_MODULES... */ 777 778 static inline struct module *__module_address(unsigned long addr) 779 { 780 return NULL; 781 } 782 783 static inline struct module *__module_text_address(unsigned long addr) 784 { 785 return NULL; 786 } 787 788 static inline bool is_module_address(unsigned long addr) 789 { 790 return false; 791 } 792 793 static inline bool is_module_percpu_address(unsigned long addr) 794 { 795 return false; 796 } 797 798 static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr) 799 { 800 return false; 801 } 802 803 static inline bool is_module_text_address(unsigned long addr) 804 { 805 return false; 806 } 807 808 static inline bool within_module_core(unsigned long addr, 809 const struct module *mod) 810 { 811 return false; 812 } 813 814 static inline bool within_module_init(unsigned long addr, 815 const struct module *mod) 816 { 817 return false; 818 } 819 820 static inline bool within_module(unsigned long addr, const struct module *mod) 821 { 822 return false; 823 } 824 825 /* Get/put a kernel symbol (calls should be symmetric) */ 826 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak,visibility("hidden"))); &(x); }) 827 #define symbol_put(x) do { } while (0) 828 #define symbol_put_addr(x) do { } while (0) 829 830 static inline void __module_get(struct module *module) 831 { 832 } 833 834 static inline bool try_module_get(struct module *module) 835 { 836 return true; 837 } 838 839 static inline void module_put(struct module *module) 840 { 841 } 842 843 #define module_name(mod) "kernel" 844 845 static inline int register_module_notifier(struct notifier_block *nb) 846 { 847 /* no events will happen anyway, so this can always succeed */ 848 return 0; 849 } 850 851 static inline int unregister_module_notifier(struct notifier_block *nb) 852 { 853 return 0; 854 } 855 856 #define module_put_and_kthread_exit(code) kthread_exit(code) 857 858 static inline void print_modules(void) 859 { 860 } 861 862 static inline bool module_requested_async_probing(struct module *module) 863 { 864 return false; 865 } 866 867 868 static inline void set_module_sig_enforced(void) 869 { 870 } 871 872 /* Dereference module function descriptor */ 873 static inline 874 void *dereference_module_function_descriptor(struct module *mod, void *ptr) 875 { 876 return ptr; 877 } 878 879 static inline bool module_is_coming(struct module *mod) 880 { 881 return false; 882 } 883 884 static inline void module_for_each_mod(int(*func)(struct module *mod, void *data), void *data) 885 { 886 } 887 #endif /* CONFIG_MODULES */ 888 889 #ifdef CONFIG_SYSFS 890 extern struct kset *module_kset; 891 extern const struct kobj_type module_ktype; 892 #endif /* CONFIG_SYSFS */ 893 894 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x) 895 896 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */ 897 898 #define __MODULE_STRING(x) __stringify(x) 899 900 #ifdef CONFIG_GENERIC_BUG 901 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *, 902 struct module *); 903 void module_bug_cleanup(struct module *); 904 905 #else /* !CONFIG_GENERIC_BUG */ 906 907 static inline void module_bug_finalize(const Elf_Ehdr *hdr, 908 const Elf_Shdr *sechdrs, 909 struct module *mod) 910 { 911 } 912 static inline void module_bug_cleanup(struct module *mod) {} 913 #endif /* CONFIG_GENERIC_BUG */ 914 915 #ifdef CONFIG_MITIGATION_RETPOLINE 916 extern bool retpoline_module_ok(bool has_retpoline); 917 #else 918 static inline bool retpoline_module_ok(bool has_retpoline) 919 { 920 return true; 921 } 922 #endif 923 924 #ifdef CONFIG_MODULE_SIG 925 bool is_module_sig_enforced(void); 926 927 static inline bool module_sig_ok(struct module *module) 928 { 929 return module->sig_ok; 930 } 931 #else /* !CONFIG_MODULE_SIG */ 932 static inline bool is_module_sig_enforced(void) 933 { 934 return false; 935 } 936 937 static inline bool module_sig_ok(struct module *module) 938 { 939 return true; 940 } 941 #endif /* CONFIG_MODULE_SIG */ 942 943 #if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS) 944 int module_kallsyms_on_each_symbol(const char *modname, 945 int (*fn)(void *, const char *, unsigned long), 946 void *data); 947 948 /* For kallsyms to ask for address resolution. namebuf should be at 949 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if 950 * found, otherwise NULL. 951 */ 952 int module_address_lookup(unsigned long addr, 953 unsigned long *symbolsize, 954 unsigned long *offset, 955 char **modname, const unsigned char **modbuildid, 956 char *namebuf); 957 int lookup_module_symbol_name(unsigned long addr, char *symname); 958 int lookup_module_symbol_attrs(unsigned long addr, 959 unsigned long *size, 960 unsigned long *offset, 961 char *modname, 962 char *name); 963 964 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if 965 * symnum out of range. 966 */ 967 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 968 char *name, char *module_name, int *exported); 969 970 /* Look for this name: can be of form module:name. */ 971 unsigned long module_kallsyms_lookup_name(const char *name); 972 973 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name); 974 975 #else /* CONFIG_MODULES && CONFIG_KALLSYMS */ 976 977 static inline int module_kallsyms_on_each_symbol(const char *modname, 978 int (*fn)(void *, const char *, unsigned long), 979 void *data) 980 { 981 return -EOPNOTSUPP; 982 } 983 984 /* For kallsyms to ask for address resolution. NULL means not found. */ 985 static inline int module_address_lookup(unsigned long addr, 986 unsigned long *symbolsize, 987 unsigned long *offset, 988 char **modname, 989 const unsigned char **modbuildid, 990 char *namebuf) 991 { 992 return 0; 993 } 994 995 static inline int lookup_module_symbol_name(unsigned long addr, char *symname) 996 { 997 return -ERANGE; 998 } 999 1000 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value, 1001 char *type, char *name, 1002 char *module_name, int *exported) 1003 { 1004 return -ERANGE; 1005 } 1006 1007 static inline unsigned long module_kallsyms_lookup_name(const char *name) 1008 { 1009 return 0; 1010 } 1011 1012 static inline unsigned long find_kallsyms_symbol_value(struct module *mod, 1013 const char *name) 1014 { 1015 return 0; 1016 } 1017 1018 #endif /* CONFIG_MODULES && CONFIG_KALLSYMS */ 1019 1020 /* Define __free(module_put) macro for struct module *. */ 1021 DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T)) 1022 1023 #endif /* _LINUX_MODULE_H */ 1024