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/kmod.h>
18 #include <linux/init.h>
19 #include <linux/elf.h>
20 #include <linux/stringify.h>
21 #include <linux/kobject.h>
22 #include <linux/moduleparam.h>
23 #include <linux/jump_label.h>
24 #include <linux/export.h>
25 #include <linux/rbtree_latch.h>
26 #include <linux/error-injection.h>
27 #include <linux/tracepoint-defs.h>
28 #include <linux/srcu.h>
29 #include <linux/static_call_types.h>
30 #include <linux/dynamic_debug.h>
31
32 #include <linux/percpu.h>
33 #include <asm/module.h>
34
35 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
36
37 struct modversion_info {
38 unsigned long crc;
39 char name[MODULE_NAME_LEN];
40 };
41
42 struct module;
43 struct exception_table_entry;
44
45 struct module_kobject {
46 struct kobject kobj;
47 struct module *mod;
48 struct kobject *drivers_dir;
49 struct module_param_attrs *mp;
50 struct completion *kobj_completion;
51 } __randomize_layout;
52
53 struct module_attribute {
54 struct attribute attr;
55 ssize_t (*show)(const struct module_attribute *, struct module_kobject *,
56 char *);
57 ssize_t (*store)(const struct module_attribute *, struct module_kobject *,
58 const char *, size_t count);
59 void (*setup)(struct module *, const char *);
60 int (*test)(struct module *);
61 void (*free)(struct module *);
62 };
63
64 struct module_version_attribute {
65 struct module_attribute mattr;
66 const char *module_name;
67 const char *version;
68 };
69
70 extern ssize_t __modver_version_show(const struct module_attribute *,
71 struct module_kobject *, char *);
72
73 extern const struct module_attribute module_uevent;
74
75 /* These are either module local, or the kernel's dummy ones. */
76 extern int init_module(void);
77 extern void cleanup_module(void);
78
79 #ifndef MODULE
80 /**
81 * module_init() - driver initialization entry point
82 * @x: function to be run at kernel boot time or module insertion
83 *
84 * module_init() will either be called during do_initcalls() (if
85 * builtin) or at module insertion time (if a module). There can only
86 * be one per module.
87 */
88 #define module_init(x) __initcall(x);
89
90 /**
91 * module_exit() - driver exit entry point
92 * @x: function to be run when driver is removed
93 *
94 * module_exit() will wrap the driver clean-up code
95 * with cleanup_module() when used with rmmod when
96 * the driver is a module. If the driver is statically
97 * compiled into the kernel, module_exit() has no effect.
98 * There can only be one per module.
99 */
100 #define module_exit(x) __exitcall(x);
101
102 #else /* MODULE */
103
104 /*
105 * In most cases loadable modules do not need custom
106 * initcall levels. There are still some valid cases where
107 * a driver may be needed early if built in, and does not
108 * matter when built as a loadable module. Like bus
109 * snooping debug drivers.
110 */
111 #define early_initcall(fn) module_init(fn)
112 #define core_initcall(fn) module_init(fn)
113 #define core_initcall_sync(fn) module_init(fn)
114 #define postcore_initcall(fn) module_init(fn)
115 #define postcore_initcall_sync(fn) module_init(fn)
116 #define arch_initcall(fn) module_init(fn)
117 #define subsys_initcall(fn) module_init(fn)
118 #define subsys_initcall_sync(fn) module_init(fn)
119 #define fs_initcall(fn) module_init(fn)
120 #define fs_initcall_sync(fn) module_init(fn)
121 #define rootfs_initcall(fn) module_init(fn)
122 #define device_initcall(fn) module_init(fn)
123 #define device_initcall_sync(fn) module_init(fn)
124 #define late_initcall(fn) module_init(fn)
125 #define late_initcall_sync(fn) module_init(fn)
126
127 #define console_initcall(fn) module_init(fn)
128
129 /* Each module must use one module_init(). */
130 #define module_init(initfn) \
131 static inline initcall_t __maybe_unused __inittest(void) \
132 { return initfn; } \
133 int init_module(void) __copy(initfn) \
134 __attribute__((alias(#initfn))); \
135 ___ADDRESSABLE(init_module, __initdata);
136
137 /* This is only required if you want to be unloadable. */
138 #define module_exit(exitfn) \
139 static inline exitcall_t __maybe_unused __exittest(void) \
140 { return exitfn; } \
141 void cleanup_module(void) __copy(exitfn) \
142 __attribute__((alias(#exitfn))); \
143 ___ADDRESSABLE(cleanup_module, __exitdata);
144
145 #endif
146
147 /* This means "can be init if no module support, otherwise module load
148 may call it." */
149 #ifdef CONFIG_MODULES
150 #define __init_or_module
151 #define __initdata_or_module
152 #define __initconst_or_module
153 #define __INIT_OR_MODULE .text
154 #define __INITDATA_OR_MODULE .data
155 #define __INITRODATA_OR_MODULE .section ".rodata","a",%progbits
156 #else
157 #define __init_or_module __init
158 #define __initdata_or_module __initdata
159 #define __initconst_or_module __initconst
160 #define __INIT_OR_MODULE __INIT
161 #define __INITDATA_OR_MODULE __INITDATA
162 #define __INITRODATA_OR_MODULE __INITRODATA
163 #endif /*CONFIG_MODULES*/
164
165 /* Generic info of form tag = "info" */
166 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
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 #ifdef MODULE
248 /* Creates an alias so file2alias.c can find device table. */
249 #define MODULE_DEVICE_TABLE(type, name) \
250 extern typeof(name) __mod_device_table__##type##__##name \
251 __attribute__ ((unused, alias(__stringify(name))))
252 #else /* !MODULE */
253 #define MODULE_DEVICE_TABLE(type, name)
254 #endif
255
256 /* Version of form [<epoch>:]<version>[-<extra-version>].
257 * Or for CVS/RCS ID version, everything but the number is stripped.
258 * <epoch>: A (small) unsigned integer which allows you to start versions
259 * anew. If not mentioned, it's zero. eg. "2:1.0" is after
260 * "1:2.0".
261
262 * <version>: The <version> may contain only alphanumerics and the
263 * character `.'. Ordered by numeric sort for numeric parts,
264 * ascii sort for ascii parts (as per RPM or DEB algorithm).
265
266 * <extraversion>: Like <version>, but inserted for local
267 * customizations, eg "rh3" or "rusty1".
268
269 * Using this automatically adds a checksum of the .c files and the
270 * local headers in "srcversion".
271 */
272
273 #if defined(MODULE) || !defined(CONFIG_SYSFS)
274 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
275 #else
276 #define MODULE_VERSION(_version) \
277 MODULE_INFO(version, _version); \
278 static const struct module_version_attribute __modver_attr \
279 __used __section("__modver") \
280 __aligned(__alignof__(struct module_version_attribute)) \
281 = { \
282 .mattr = { \
283 .attr = { \
284 .name = "version", \
285 .mode = S_IRUGO, \
286 }, \
287 .show = __modver_version_show, \
288 }, \
289 .module_name = KBUILD_MODNAME, \
290 .version = _version, \
291 }
292 #endif
293
294 /* Optional firmware file (or files) needed by the module
295 * format is simply firmware file name. Multiple firmware
296 * files require multiple MODULE_FIRMWARE() specifiers */
297 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
298
299 #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, ns)
300
301 struct notifier_block;
302
303 #ifdef CONFIG_MODULES
304
305 extern int modules_disabled; /* for sysctl */
306 /* Get/put a kernel symbol (calls must be symmetric) */
307 void *__symbol_get(const char *symbol);
308 void *__symbol_get_gpl(const char *symbol);
309 #define symbol_get(x) ({ \
310 static const char __notrim[] \
311 __used __section(".no_trim_symbol") = __stringify(x); \
312 (typeof(&x))(__symbol_get(__stringify(x))); })
313
314 /* modules using other modules: kdb wants to see this. */
315 struct module_use {
316 struct list_head source_list;
317 struct list_head target_list;
318 struct module *source, *target;
319 };
320
321 enum module_state {
322 MODULE_STATE_LIVE, /* Normal state. */
323 MODULE_STATE_COMING, /* Full formed, running module_init. */
324 MODULE_STATE_GOING, /* Going away. */
325 MODULE_STATE_UNFORMED, /* Still setting it up. */
326 };
327
328 struct mod_tree_node {
329 struct module *mod;
330 struct latch_tree_node node;
331 };
332
333 enum mod_mem_type {
334 MOD_TEXT = 0,
335 MOD_DATA,
336 MOD_RODATA,
337 MOD_RO_AFTER_INIT,
338 MOD_INIT_TEXT,
339 MOD_INIT_DATA,
340 MOD_INIT_RODATA,
341
342 MOD_MEM_NUM_TYPES,
343 MOD_INVALID = -1,
344 };
345
346 #define mod_mem_type_is_init(type) \
347 ((type) == MOD_INIT_TEXT || \
348 (type) == MOD_INIT_DATA || \
349 (type) == MOD_INIT_RODATA)
350
351 #define mod_mem_type_is_core(type) (!mod_mem_type_is_init(type))
352
353 #define mod_mem_type_is_text(type) \
354 ((type) == MOD_TEXT || \
355 (type) == MOD_INIT_TEXT)
356
357 #define mod_mem_type_is_data(type) (!mod_mem_type_is_text(type))
358
359 #define mod_mem_type_is_core_data(type) \
360 (mod_mem_type_is_core(type) && \
361 mod_mem_type_is_data(type))
362
363 #define for_each_mod_mem_type(type) \
364 for (enum mod_mem_type (type) = 0; \
365 (type) < MOD_MEM_NUM_TYPES; (type)++)
366
367 #define for_class_mod_mem_type(type, class) \
368 for_each_mod_mem_type(type) \
369 if (mod_mem_type_is_##class(type))
370
371 struct module_memory {
372 void *base;
373 bool is_rox;
374 unsigned int size;
375
376 #ifdef CONFIG_MODULES_TREE_LOOKUP
377 struct mod_tree_node mtn;
378 #endif
379 };
380
381 #ifdef CONFIG_MODULES_TREE_LOOKUP
382 /* Only touch one cacheline for common rbtree-for-core-layout case. */
383 #define __module_memory_align ____cacheline_aligned
384 #else
385 #define __module_memory_align
386 #endif
387
388 struct mod_kallsyms {
389 Elf_Sym *symtab;
390 unsigned int num_symtab;
391 char *strtab;
392 char *typetab;
393 };
394
395 #ifdef CONFIG_LIVEPATCH
396 /**
397 * struct klp_modinfo - ELF information preserved from the livepatch module
398 *
399 * @hdr: ELF header
400 * @sechdrs: Section header table
401 * @secstrings: String table for the section headers
402 * @symndx: The symbol table section index
403 */
404 struct klp_modinfo {
405 Elf_Ehdr hdr;
406 Elf_Shdr *sechdrs;
407 char *secstrings;
408 unsigned int symndx;
409 };
410 #endif
411
412 struct module {
413 enum module_state state;
414
415 /* Member of list of modules */
416 struct list_head list;
417
418 /* Unique handle for this module */
419 char name[MODULE_NAME_LEN];
420
421 #ifdef CONFIG_STACKTRACE_BUILD_ID
422 /* Module build ID */
423 unsigned char build_id[BUILD_ID_SIZE_MAX];
424 #endif
425
426 /* Sysfs stuff. */
427 struct module_kobject mkobj;
428 struct module_attribute *modinfo_attrs;
429 const char *version;
430 const char *srcversion;
431 struct kobject *holders_dir;
432
433 /* Exported symbols */
434 const struct kernel_symbol *syms;
435 const u32 *crcs;
436 unsigned int num_syms;
437
438 #ifdef CONFIG_ARCH_USES_CFI_TRAPS
439 s32 *kcfi_traps;
440 s32 *kcfi_traps_end;
441 #endif
442
443 /* Kernel parameters. */
444 #ifdef CONFIG_SYSFS
445 struct mutex param_lock;
446 #endif
447 struct kernel_param *kp;
448 unsigned int num_kp;
449
450 /* GPL-only exported symbols. */
451 unsigned int num_gpl_syms;
452 const struct kernel_symbol *gpl_syms;
453 const u32 *gpl_crcs;
454 bool using_gplonly_symbols;
455
456 #ifdef CONFIG_MODULE_SIG
457 /* Signature was verified. */
458 bool sig_ok;
459 #endif
460
461 bool async_probe_requested;
462
463 /* Exception table */
464 unsigned int num_exentries;
465 struct exception_table_entry *extable;
466
467 /* Startup function. */
468 int (*init)(void);
469
470 struct module_memory mem[MOD_MEM_NUM_TYPES] __module_memory_align;
471
472 /* Arch-specific module values */
473 struct mod_arch_specific arch;
474
475 unsigned long taints; /* same bits as kernel:taint_flags */
476
477 #ifdef CONFIG_GENERIC_BUG
478 /* Support for BUG */
479 unsigned num_bugs;
480 struct list_head bug_list;
481 struct bug_entry *bug_table;
482 #endif
483
484 #ifdef CONFIG_KALLSYMS
485 /* Protected by RCU and/or module_mutex: use rcu_dereference() */
486 struct mod_kallsyms __rcu *kallsyms;
487 struct mod_kallsyms core_kallsyms;
488
489 /* Section attributes */
490 struct module_sect_attrs *sect_attrs;
491
492 /* Notes attributes */
493 struct module_notes_attrs *notes_attrs;
494 #endif
495
496 /* The command line arguments (may be mangled). People like
497 keeping pointers to this stuff */
498 char *args;
499
500 #ifdef CONFIG_SMP
501 /* Per-cpu data. */
502 void __percpu *percpu;
503 unsigned int percpu_size;
504 #endif
505 void *noinstr_text_start;
506 unsigned int noinstr_text_size;
507
508 #ifdef CONFIG_TRACEPOINTS
509 unsigned int num_tracepoints;
510 tracepoint_ptr_t *tracepoints_ptrs;
511 #endif
512 #ifdef CONFIG_TREE_SRCU
513 unsigned int num_srcu_structs;
514 struct srcu_struct **srcu_struct_ptrs;
515 #endif
516 #ifdef CONFIG_BPF_EVENTS
517 unsigned int num_bpf_raw_events;
518 struct bpf_raw_event_map *bpf_raw_events;
519 #endif
520 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
521 unsigned int btf_data_size;
522 unsigned int btf_base_data_size;
523 void *btf_data;
524 void *btf_base_data;
525 #endif
526 #ifdef CONFIG_JUMP_LABEL
527 struct jump_entry *jump_entries;
528 unsigned int num_jump_entries;
529 #endif
530 #ifdef CONFIG_TRACING
531 unsigned int num_trace_bprintk_fmt;
532 const char **trace_bprintk_fmt_start;
533 #endif
534 #ifdef CONFIG_EVENT_TRACING
535 struct trace_event_call **trace_events;
536 unsigned int num_trace_events;
537 struct trace_eval_map **trace_evals;
538 unsigned int num_trace_evals;
539 #endif
540 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
541 unsigned int num_ftrace_callsites;
542 unsigned long *ftrace_callsites;
543 #endif
544 #ifdef CONFIG_KPROBES
545 void *kprobes_text_start;
546 unsigned int kprobes_text_size;
547 unsigned long *kprobe_blacklist;
548 unsigned int num_kprobe_blacklist;
549 #endif
550 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
551 int num_static_call_sites;
552 struct static_call_site *static_call_sites;
553 #endif
554 #if IS_ENABLED(CONFIG_KUNIT)
555 int num_kunit_init_suites;
556 struct kunit_suite **kunit_init_suites;
557 int num_kunit_suites;
558 struct kunit_suite **kunit_suites;
559 #endif
560
561
562 #ifdef CONFIG_LIVEPATCH
563 bool klp; /* Is this a livepatch module? */
564 bool klp_alive;
565
566 /* ELF information */
567 struct klp_modinfo *klp_info;
568 #endif
569
570 #ifdef CONFIG_PRINTK_INDEX
571 unsigned int printk_index_size;
572 struct pi_entry **printk_index_start;
573 #endif
574
575 #ifdef CONFIG_MODULE_UNLOAD
576 /* What modules depend on me? */
577 struct list_head source_list;
578 /* What modules do I depend on? */
579 struct list_head target_list;
580
581 /* Destruction function. */
582 void (*exit)(void);
583
584 atomic_t refcnt;
585 #endif
586
587 #ifdef CONFIG_CONSTRUCTORS
588 /* Constructor functions. */
589 ctor_fn_t *ctors;
590 unsigned int num_ctors;
591 #endif
592
593 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
594 struct error_injection_entry *ei_funcs;
595 unsigned int num_ei_funcs;
596 #endif
597 #ifdef CONFIG_DYNAMIC_DEBUG_CORE
598 struct _ddebug_info dyndbg_info;
599 #endif
600 } ____cacheline_aligned __randomize_layout;
601 #ifndef MODULE_ARCH_INIT
602 #define MODULE_ARCH_INIT {}
603 #endif
604
605 #ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE
kallsyms_symbol_value(const Elf_Sym * sym)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.*/
module_is_live(struct module * mod)615 static inline bool module_is_live(struct module *mod)
616 {
617 return mod->state != MODULE_STATE_GOING;
618 }
619
module_is_coming(struct module * mod)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
within_module_mem_type(unsigned long addr,const struct module * mod,enum mod_mem_type type)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
within_module_core(unsigned long addr,const struct module * mod)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
within_module_init(unsigned long addr,const struct module * mod)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
within_module(unsigned long addr,const struct module * mod)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-sched 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*/
try_module_get(struct module * module)728 static inline bool try_module_get(struct module *module)
729 {
730 return !module || module_is_live(module);
731 }
module_put(struct module * module)732 static inline void module_put(struct module *module)
733 {
734 }
__module_get(struct module * module)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
module_requested_async_probing(struct module * module)758 static inline bool module_requested_async_probing(struct module *module)
759 {
760 return module && module->async_probe_requested;
761 }
762
is_livepatch_module(struct module * mod)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 #else /* !CONFIG_MODULES... */
775
__module_address(unsigned long addr)776 static inline struct module *__module_address(unsigned long addr)
777 {
778 return NULL;
779 }
780
__module_text_address(unsigned long addr)781 static inline struct module *__module_text_address(unsigned long addr)
782 {
783 return NULL;
784 }
785
is_module_address(unsigned long addr)786 static inline bool is_module_address(unsigned long addr)
787 {
788 return false;
789 }
790
is_module_percpu_address(unsigned long addr)791 static inline bool is_module_percpu_address(unsigned long addr)
792 {
793 return false;
794 }
795
__is_module_percpu_address(unsigned long addr,unsigned long * can_addr)796 static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
797 {
798 return false;
799 }
800
is_module_text_address(unsigned long addr)801 static inline bool is_module_text_address(unsigned long addr)
802 {
803 return false;
804 }
805
within_module_core(unsigned long addr,const struct module * mod)806 static inline bool within_module_core(unsigned long addr,
807 const struct module *mod)
808 {
809 return false;
810 }
811
within_module_init(unsigned long addr,const struct module * mod)812 static inline bool within_module_init(unsigned long addr,
813 const struct module *mod)
814 {
815 return false;
816 }
817
within_module(unsigned long addr,const struct module * mod)818 static inline bool within_module(unsigned long addr, const struct module *mod)
819 {
820 return false;
821 }
822
823 /* Get/put a kernel symbol (calls should be symmetric) */
824 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak,visibility("hidden"))); &(x); })
825 #define symbol_put(x) do { } while (0)
826 #define symbol_put_addr(x) do { } while (0)
827
__module_get(struct module * module)828 static inline void __module_get(struct module *module)
829 {
830 }
831
try_module_get(struct module * module)832 static inline bool try_module_get(struct module *module)
833 {
834 return true;
835 }
836
module_put(struct module * module)837 static inline void module_put(struct module *module)
838 {
839 }
840
841 #define module_name(mod) "kernel"
842
register_module_notifier(struct notifier_block * nb)843 static inline int register_module_notifier(struct notifier_block *nb)
844 {
845 /* no events will happen anyway, so this can always succeed */
846 return 0;
847 }
848
unregister_module_notifier(struct notifier_block * nb)849 static inline int unregister_module_notifier(struct notifier_block *nb)
850 {
851 return 0;
852 }
853
854 #define module_put_and_kthread_exit(code) kthread_exit(code)
855
print_modules(void)856 static inline void print_modules(void)
857 {
858 }
859
module_requested_async_probing(struct module * module)860 static inline bool module_requested_async_probing(struct module *module)
861 {
862 return false;
863 }
864
865
set_module_sig_enforced(void)866 static inline void set_module_sig_enforced(void)
867 {
868 }
869
870 /* Dereference module function descriptor */
871 static inline
dereference_module_function_descriptor(struct module * mod,void * ptr)872 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
873 {
874 return ptr;
875 }
876
module_is_coming(struct module * mod)877 static inline bool module_is_coming(struct module *mod)
878 {
879 return false;
880 }
881 #endif /* CONFIG_MODULES */
882
883 #ifdef CONFIG_SYSFS
884 extern struct kset *module_kset;
885 extern const struct kobj_type module_ktype;
886 #endif /* CONFIG_SYSFS */
887
888 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
889
890 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
891
892 #define __MODULE_STRING(x) __stringify(x)
893
894 #ifdef CONFIG_GENERIC_BUG
895 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
896 struct module *);
897 void module_bug_cleanup(struct module *);
898
899 #else /* !CONFIG_GENERIC_BUG */
900
module_bug_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)901 static inline void module_bug_finalize(const Elf_Ehdr *hdr,
902 const Elf_Shdr *sechdrs,
903 struct module *mod)
904 {
905 }
module_bug_cleanup(struct module * mod)906 static inline void module_bug_cleanup(struct module *mod) {}
907 #endif /* CONFIG_GENERIC_BUG */
908
909 #ifdef CONFIG_MITIGATION_RETPOLINE
910 extern bool retpoline_module_ok(bool has_retpoline);
911 #else
retpoline_module_ok(bool has_retpoline)912 static inline bool retpoline_module_ok(bool has_retpoline)
913 {
914 return true;
915 }
916 #endif
917
918 #ifdef CONFIG_MODULE_SIG
919 bool is_module_sig_enforced(void);
920
module_sig_ok(struct module * module)921 static inline bool module_sig_ok(struct module *module)
922 {
923 return module->sig_ok;
924 }
925 #else /* !CONFIG_MODULE_SIG */
is_module_sig_enforced(void)926 static inline bool is_module_sig_enforced(void)
927 {
928 return false;
929 }
930
module_sig_ok(struct module * module)931 static inline bool module_sig_ok(struct module *module)
932 {
933 return true;
934 }
935 #endif /* CONFIG_MODULE_SIG */
936
937 #if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS)
938 int module_kallsyms_on_each_symbol(const char *modname,
939 int (*fn)(void *, const char *, unsigned long),
940 void *data);
941
942 /* For kallsyms to ask for address resolution. namebuf should be at
943 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
944 * found, otherwise NULL.
945 */
946 int module_address_lookup(unsigned long addr,
947 unsigned long *symbolsize,
948 unsigned long *offset,
949 char **modname, const unsigned char **modbuildid,
950 char *namebuf);
951 int lookup_module_symbol_name(unsigned long addr, char *symname);
952 int lookup_module_symbol_attrs(unsigned long addr,
953 unsigned long *size,
954 unsigned long *offset,
955 char *modname,
956 char *name);
957
958 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
959 * symnum out of range.
960 */
961 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
962 char *name, char *module_name, int *exported);
963
964 /* Look for this name: can be of form module:name. */
965 unsigned long module_kallsyms_lookup_name(const char *name);
966
967 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
968
969 #else /* CONFIG_MODULES && CONFIG_KALLSYMS */
970
module_kallsyms_on_each_symbol(const char * modname,int (* fn)(void *,const char *,unsigned long),void * data)971 static inline int module_kallsyms_on_each_symbol(const char *modname,
972 int (*fn)(void *, const char *, unsigned long),
973 void *data)
974 {
975 return -EOPNOTSUPP;
976 }
977
978 /* For kallsyms to ask for address resolution. NULL means not found. */
module_address_lookup(unsigned long addr,unsigned long * symbolsize,unsigned long * offset,char ** modname,const unsigned char ** modbuildid,char * namebuf)979 static inline int module_address_lookup(unsigned long addr,
980 unsigned long *symbolsize,
981 unsigned long *offset,
982 char **modname,
983 const unsigned char **modbuildid,
984 char *namebuf)
985 {
986 return 0;
987 }
988
lookup_module_symbol_name(unsigned long addr,char * symname)989 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
990 {
991 return -ERANGE;
992 }
993
module_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * name,char * module_name,int * exported)994 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
995 char *type, char *name,
996 char *module_name, int *exported)
997 {
998 return -ERANGE;
999 }
1000
module_kallsyms_lookup_name(const char * name)1001 static inline unsigned long module_kallsyms_lookup_name(const char *name)
1002 {
1003 return 0;
1004 }
1005
find_kallsyms_symbol_value(struct module * mod,const char * name)1006 static inline unsigned long find_kallsyms_symbol_value(struct module *mod,
1007 const char *name)
1008 {
1009 return 0;
1010 }
1011
1012 #endif /* CONFIG_MODULES && CONFIG_KALLSYMS */
1013
1014 #endif /* _LINUX_MODULE_H */
1015