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 #include <linux/android_kabi.h>
32
33 #include <linux/percpu.h>
34 #include <asm/module.h>
35
36 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_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)(struct module_attribute *, struct module_kobject *,
57 char *);
58 ssize_t (*store)(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(struct module_attribute *,
72 struct module_kobject *, char *);
73
74 extern 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 /* Generic info of form tag = "info" */
167 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
168
169 /* For userspace: you can also call me... */
170 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
171
172 /* Soft module dependencies. See man modprobe.d for details.
173 * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
174 */
175 #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
176
177 /*
178 * MODULE_FILE is used for generating modules.builtin
179 * So, make it no-op when this is being built as a module
180 */
181 #ifdef MODULE
182 #define MODULE_FILE
183 #else
184 #define MODULE_FILE MODULE_INFO(file, KBUILD_MODFILE);
185 #endif
186
187 /*
188 * The following license idents are currently accepted as indicating free
189 * software modules
190 *
191 * "GPL" [GNU Public License v2]
192 * "GPL v2" [GNU Public License v2]
193 * "GPL and additional rights" [GNU Public License v2 rights and more]
194 * "Dual BSD/GPL" [GNU Public License v2
195 * or BSD license choice]
196 * "Dual MIT/GPL" [GNU Public License v2
197 * or MIT license choice]
198 * "Dual MPL/GPL" [GNU Public License v2
199 * or Mozilla license choice]
200 *
201 * The following other idents are available
202 *
203 * "Proprietary" [Non free products]
204 *
205 * Both "GPL v2" and "GPL" (the latter also in dual licensed strings) are
206 * merely stating that the module is licensed under the GPL v2, but are not
207 * telling whether "GPL v2 only" or "GPL v2 or later". The reason why there
208 * are two variants is a historic and failed attempt to convey more
209 * information in the MODULE_LICENSE string. For module loading the
210 * "only/or later" distinction is completely irrelevant and does neither
211 * replace the proper license identifiers in the corresponding source file
212 * nor amends them in any way. The sole purpose is to make the
213 * 'Proprietary' flagging work and to refuse to bind symbols which are
214 * exported with EXPORT_SYMBOL_GPL when a non free module is loaded.
215 *
216 * In the same way "BSD" is not a clear license information. It merely
217 * states, that the module is licensed under one of the compatible BSD
218 * license variants. The detailed and correct license information is again
219 * to be found in the corresponding source files.
220 *
221 * There are dual licensed components, but when running with Linux it is the
222 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
223 * is a GPL combined work.
224 *
225 * This exists for several reasons
226 * 1. So modinfo can show license info for users wanting to vet their setup
227 * is free
228 * 2. So the community can ignore bug reports including proprietary modules
229 * 3. So vendors can do likewise based on their own policies
230 */
231 #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
232
233 /*
234 * Author(s), use "Name <email>" or just "Name", for multiple
235 * authors use multiple MODULE_AUTHOR() statements/lines.
236 */
237 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
238
239 /* What your module does. */
240 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
241
242 #ifdef MODULE
243 /* Creates an alias so file2alias.c can find device table. */
244 #define MODULE_DEVICE_TABLE(type, name) \
245 extern typeof(name) __mod_##type##__##name##_device_table \
246 __attribute__ ((unused, alias(__stringify(name))))
247 #else /* !MODULE */
248 #define MODULE_DEVICE_TABLE(type, name)
249 #endif
250
251 /* Version of form [<epoch>:]<version>[-<extra-version>].
252 * Or for CVS/RCS ID version, everything but the number is stripped.
253 * <epoch>: A (small) unsigned integer which allows you to start versions
254 * anew. If not mentioned, it's zero. eg. "2:1.0" is after
255 * "1:2.0".
256
257 * <version>: The <version> may contain only alphanumerics and the
258 * character `.'. Ordered by numeric sort for numeric parts,
259 * ascii sort for ascii parts (as per RPM or DEB algorithm).
260
261 * <extraversion>: Like <version>, but inserted for local
262 * customizations, eg "rh3" or "rusty1".
263
264 * Using this automatically adds a checksum of the .c files and the
265 * local headers in "srcversion".
266 */
267
268 #if defined(MODULE) || !defined(CONFIG_SYSFS)
269 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
270 #else
271 #define MODULE_VERSION(_version) \
272 MODULE_INFO(version, _version); \
273 static struct module_version_attribute __modver_attr \
274 __used __section("__modver") \
275 __aligned(__alignof__(struct module_version_attribute)) \
276 = { \
277 .mattr = { \
278 .attr = { \
279 .name = "version", \
280 .mode = S_IRUGO, \
281 }, \
282 .show = __modver_version_show, \
283 }, \
284 .module_name = KBUILD_MODNAME, \
285 .version = _version, \
286 }
287 #endif
288
289 /* Optional firmware file (or files) needed by the module
290 * format is simply firmware file name. Multiple firmware
291 * files require multiple MODULE_FIRMWARE() specifiers */
292 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
293
294 #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, __stringify(ns))
295
296 struct notifier_block;
297
298 #ifdef CONFIG_MODULES
299
300 extern int modules_disabled; /* for sysctl */
301 /* Get/put a kernel symbol (calls must be symmetric) */
302 void *__symbol_get(const char *symbol);
303 void *__symbol_get_gpl(const char *symbol);
304 #define symbol_get(x) ((typeof(&x))(__symbol_get(__stringify(x))))
305
306 /* modules using other modules: kdb wants to see this. */
307 struct module_use {
308 struct list_head source_list;
309 struct list_head target_list;
310 struct module *source, *target;
311 };
312
313 enum module_state {
314 MODULE_STATE_LIVE, /* Normal state. */
315 MODULE_STATE_COMING, /* Full formed, running module_init. */
316 MODULE_STATE_GOING, /* Going away. */
317 MODULE_STATE_UNFORMED, /* Still setting it up. */
318 };
319
320 struct mod_tree_node {
321 struct module *mod;
322 struct latch_tree_node node;
323 };
324
325 enum mod_mem_type {
326 MOD_TEXT = 0,
327 MOD_DATA,
328 MOD_RODATA,
329 MOD_RO_AFTER_INIT,
330 MOD_INIT_TEXT,
331 MOD_INIT_DATA,
332 MOD_INIT_RODATA,
333
334 MOD_MEM_NUM_TYPES,
335 MOD_INVALID = -1,
336 };
337
338 #define mod_mem_type_is_init(type) \
339 ((type) == MOD_INIT_TEXT || \
340 (type) == MOD_INIT_DATA || \
341 (type) == MOD_INIT_RODATA)
342
343 #define mod_mem_type_is_core(type) (!mod_mem_type_is_init(type))
344
345 #define mod_mem_type_is_text(type) \
346 ((type) == MOD_TEXT || \
347 (type) == MOD_INIT_TEXT)
348
349 #define mod_mem_type_is_data(type) (!mod_mem_type_is_text(type))
350
351 #define mod_mem_type_is_core_data(type) \
352 (mod_mem_type_is_core(type) && \
353 mod_mem_type_is_data(type))
354
355 #define for_each_mod_mem_type(type) \
356 for (enum mod_mem_type (type) = 0; \
357 (type) < MOD_MEM_NUM_TYPES; (type)++)
358
359 #define for_class_mod_mem_type(type, class) \
360 for_each_mod_mem_type(type) \
361 if (mod_mem_type_is_##class(type))
362
363 struct module_memory {
364 void *base;
365 unsigned int size;
366
367 #ifdef CONFIG_MODULES_TREE_LOOKUP
368 struct mod_tree_node mtn;
369 #endif
370 };
371
372 #ifdef CONFIG_MODULES_TREE_LOOKUP
373 /* Only touch one cacheline for common rbtree-for-core-layout case. */
374 #define __module_memory_align ____cacheline_aligned
375 #else
376 #define __module_memory_align
377 #endif
378
379 struct mod_kallsyms {
380 Elf_Sym *symtab;
381 unsigned int num_symtab;
382 char *strtab;
383 char *typetab;
384 };
385
386 #ifdef CONFIG_LIVEPATCH
387 /**
388 * struct klp_modinfo - ELF information preserved from the livepatch module
389 *
390 * @hdr: ELF header
391 * @sechdrs: Section header table
392 * @secstrings: String table for the section headers
393 * @symndx: The symbol table section index
394 */
395 struct klp_modinfo {
396 Elf_Ehdr hdr;
397 Elf_Shdr *sechdrs;
398 char *secstrings;
399 unsigned int symndx;
400 };
401 #endif
402
403 struct module {
404 enum module_state state;
405
406 /* Member of list of modules */
407 struct list_head list;
408
409 /* Unique handle for this module */
410 char name[MODULE_NAME_LEN];
411
412 #ifdef CONFIG_STACKTRACE_BUILD_ID
413 /* Module build ID */
414 unsigned char build_id[BUILD_ID_SIZE_MAX];
415 #endif
416
417 /* Sysfs stuff. */
418 struct module_kobject mkobj;
419 struct module_attribute *modinfo_attrs;
420 const char *version;
421 const char *srcversion;
422 const char *scmversion;
423 struct kobject *holders_dir;
424
425 /* Exported symbols */
426 const struct kernel_symbol *syms;
427 const s32 *crcs;
428 unsigned int num_syms;
429
430 #ifdef CONFIG_ARCH_USES_CFI_TRAPS
431 s32 *kcfi_traps;
432 s32 *kcfi_traps_end;
433 #endif
434
435 /* Kernel parameters. */
436 #ifdef CONFIG_SYSFS
437 struct mutex param_lock;
438 #endif
439 struct kernel_param *kp;
440 unsigned int num_kp;
441
442 /* GPL-only exported symbols. */
443 unsigned int num_gpl_syms;
444 const struct kernel_symbol *gpl_syms;
445 const s32 *gpl_crcs;
446 bool using_gplonly_symbols;
447
448 /*
449 * Signature was verified. Unconditionally compiled in Android to
450 * preserve ABI compatibility between kernels without module
451 * signing enabled and signed modules.
452 */
453 bool sig_ok;
454
455 bool async_probe_requested;
456
457 /* Exception table */
458 unsigned int num_exentries;
459 struct exception_table_entry *extable;
460
461 /* Startup function. */
462 int (*init)(void);
463
464 struct module_memory mem[MOD_MEM_NUM_TYPES] __module_memory_align;
465
466 /* Arch-specific module values */
467 struct mod_arch_specific arch;
468
469 unsigned long taints; /* same bits as kernel:taint_flags */
470
471 #ifdef CONFIG_GENERIC_BUG
472 /* Support for BUG */
473 unsigned num_bugs;
474 struct list_head bug_list;
475 struct bug_entry *bug_table;
476 #endif
477
478 #ifdef CONFIG_KALLSYMS
479 /* Protected by RCU and/or module_mutex: use rcu_dereference() */
480 struct mod_kallsyms __rcu *kallsyms;
481 struct mod_kallsyms core_kallsyms;
482
483 /* Section attributes */
484 struct module_sect_attrs *sect_attrs;
485
486 /* Notes attributes */
487 struct module_notes_attrs *notes_attrs;
488 #endif
489
490 /* The command line arguments (may be mangled). People like
491 keeping pointers to this stuff */
492 char *args;
493
494 #ifdef CONFIG_SMP
495 /* Per-cpu data. */
496 void __percpu *percpu;
497 unsigned int percpu_size;
498 #endif
499 void *noinstr_text_start;
500 unsigned int noinstr_text_size;
501
502 #ifdef CONFIG_TRACEPOINTS
503 unsigned int num_tracepoints;
504 tracepoint_ptr_t *tracepoints_ptrs;
505 #endif
506 #ifdef CONFIG_TREE_SRCU
507 unsigned int num_srcu_structs;
508 struct srcu_struct **srcu_struct_ptrs;
509 #endif
510 #ifdef CONFIG_BPF_EVENTS
511 unsigned int num_bpf_raw_events;
512 struct bpf_raw_event_map *bpf_raw_events;
513 #endif
514 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
515 unsigned int btf_data_size;
516 void *btf_data;
517 #endif
518 #ifdef CONFIG_JUMP_LABEL
519 struct jump_entry *jump_entries;
520 unsigned int num_jump_entries;
521 #endif
522 #ifdef CONFIG_TRACING
523 unsigned int num_trace_bprintk_fmt;
524 const char **trace_bprintk_fmt_start;
525 #endif
526 #ifdef CONFIG_EVENT_TRACING
527 struct trace_event_call **trace_events;
528 unsigned int num_trace_events;
529 struct trace_eval_map **trace_evals;
530 unsigned int num_trace_evals;
531 #endif
532 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
533 unsigned int num_ftrace_callsites;
534 unsigned long *ftrace_callsites;
535 #endif
536 #ifdef CONFIG_KPROBES
537 void *kprobes_text_start;
538 unsigned int kprobes_text_size;
539 unsigned long *kprobe_blacklist;
540 unsigned int num_kprobe_blacklist;
541 #endif
542 #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
543 int num_static_call_sites;
544 struct static_call_site *static_call_sites;
545 #endif
546 #if IS_ENABLED(CONFIG_KUNIT)
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
591 ANDROID_KABI_RESERVE(1);
592 ANDROID_KABI_RESERVE(2);
593 ANDROID_KABI_RESERVE(3);
594 ANDROID_KABI_RESERVE(4);
595 } ____cacheline_aligned __randomize_layout;
596 #ifndef MODULE_ARCH_INIT
597 #define MODULE_ARCH_INIT {}
598 #endif
599
600 #ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE
kallsyms_symbol_value(const Elf_Sym * sym)601 static inline unsigned long kallsyms_symbol_value(const Elf_Sym *sym)
602 {
603 return sym->st_value;
604 }
605 #endif
606
607 /* FIXME: It'd be nice to isolate modules during init, too, so they
608 aren't used before they (may) fail. But presently too much code
609 (IDE & SCSI) require entry into the module during init.*/
module_is_live(struct module * mod)610 static inline bool module_is_live(struct module *mod)
611 {
612 return mod->state != MODULE_STATE_GOING;
613 }
614
615 struct module *__module_text_address(unsigned long addr);
616 struct module *__module_address(unsigned long addr);
617 bool is_module_address(unsigned long addr);
618 bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
619 bool is_module_percpu_address(unsigned long addr);
620 bool is_module_text_address(unsigned long addr);
621
within_module_mem_type(unsigned long addr,const struct module * mod,enum mod_mem_type type)622 static inline bool within_module_mem_type(unsigned long addr,
623 const struct module *mod,
624 enum mod_mem_type type)
625 {
626 unsigned long base, size;
627
628 base = (unsigned long)mod->mem[type].base;
629 size = mod->mem[type].size;
630 return addr - base < size;
631 }
632
within_module_core(unsigned long addr,const struct module * mod)633 static inline bool within_module_core(unsigned long addr,
634 const struct module *mod)
635 {
636 for_class_mod_mem_type(type, core) {
637 if (within_module_mem_type(addr, mod, type))
638 return true;
639 }
640 return false;
641 }
642
within_module_init(unsigned long addr,const struct module * mod)643 static inline bool within_module_init(unsigned long addr,
644 const struct module *mod)
645 {
646 for_class_mod_mem_type(type, init) {
647 if (within_module_mem_type(addr, mod, type))
648 return true;
649 }
650 return false;
651 }
652
within_module(unsigned long addr,const struct module * mod)653 static inline bool within_module(unsigned long addr, const struct module *mod)
654 {
655 return within_module_init(addr, mod) || within_module_core(addr, mod);
656 }
657
658 /* Search for module by name: must be in a RCU-sched critical section. */
659 struct module *find_module(const char *name);
660
661 extern void __noreturn __module_put_and_kthread_exit(struct module *mod,
662 long code);
663 #define module_put_and_kthread_exit(code) __module_put_and_kthread_exit(THIS_MODULE, code)
664
665 #ifdef CONFIG_MODULE_UNLOAD
666 int module_refcount(struct module *mod);
667 void __symbol_put(const char *symbol);
668 #define symbol_put(x) __symbol_put(__stringify(x))
669 void symbol_put_addr(void *addr);
670
671 /* Sometimes we know we already have a refcount, and it's easier not
672 to handle the error case (which only happens with rmmod --wait). */
673 extern void __module_get(struct module *module);
674
675 /**
676 * try_module_get() - take module refcount unless module is being removed
677 * @module: the module we should check for
678 *
679 * Only try to get a module reference count if the module is not being removed.
680 * This call will fail if the module is already being removed.
681 *
682 * Care must also be taken to ensure the module exists and is alive prior to
683 * usage of this call. This can be gauranteed through two means:
684 *
685 * 1) Direct protection: you know an earlier caller must have increased the
686 * module reference through __module_get(). This can typically be achieved
687 * by having another entity other than the module itself increment the
688 * module reference count.
689 *
690 * 2) Implied protection: there is an implied protection against module
691 * removal. An example of this is the implied protection used by kernfs /
692 * sysfs. The sysfs store / read file operations are guaranteed to exist
693 * through the use of kernfs's active reference (see kernfs_active()) and a
694 * sysfs / kernfs file removal cannot happen unless the same file is not
695 * active. Therefore, if a sysfs file is being read or written to the module
696 * which created it must still exist. It is therefore safe to use
697 * try_module_get() on module sysfs store / read ops.
698 *
699 * One of the real values to try_module_get() is the module_is_live() check
700 * which ensures that the caller of try_module_get() can yield to userspace
701 * module removal requests and gracefully fail if the module is on its way out.
702 *
703 * Returns true if the reference count was successfully incremented.
704 */
705 extern bool try_module_get(struct module *module);
706
707 /**
708 * module_put() - release a reference count to a module
709 * @module: the module we should release a reference count for
710 *
711 * If you successfully bump a reference count to a module with try_module_get(),
712 * when you are finished you must call module_put() to release that reference
713 * count.
714 */
715 extern void module_put(struct module *module);
716
717 #else /*!CONFIG_MODULE_UNLOAD*/
try_module_get(struct module * module)718 static inline bool try_module_get(struct module *module)
719 {
720 return !module || module_is_live(module);
721 }
module_put(struct module * module)722 static inline void module_put(struct module *module)
723 {
724 }
__module_get(struct module * module)725 static inline void __module_get(struct module *module)
726 {
727 }
728 #define symbol_put(x) do { } while (0)
729 #define symbol_put_addr(p) do { } while (0)
730
731 #endif /* CONFIG_MODULE_UNLOAD */
732
733 /* This is a #define so the string doesn't get put in every .o file */
734 #define module_name(mod) \
735 ({ \
736 struct module *__mod = (mod); \
737 __mod ? __mod->name : "kernel"; \
738 })
739
740 /* Dereference module function descriptor */
741 void *dereference_module_function_descriptor(struct module *mod, void *ptr);
742
743 int register_module_notifier(struct notifier_block *nb);
744 int unregister_module_notifier(struct notifier_block *nb);
745
746 extern void print_modules(void);
747
module_requested_async_probing(struct module * module)748 static inline bool module_requested_async_probing(struct module *module)
749 {
750 return module && module->async_probe_requested;
751 }
752
is_livepatch_module(struct module * mod)753 static inline bool is_livepatch_module(struct module *mod)
754 {
755 #ifdef CONFIG_LIVEPATCH
756 return mod->klp;
757 #else
758 return false;
759 #endif
760 }
761
762 void set_module_sig_enforced(void);
763
764 #else /* !CONFIG_MODULES... */
765
__module_address(unsigned long addr)766 static inline struct module *__module_address(unsigned long addr)
767 {
768 return NULL;
769 }
770
__module_text_address(unsigned long addr)771 static inline struct module *__module_text_address(unsigned long addr)
772 {
773 return NULL;
774 }
775
is_module_address(unsigned long addr)776 static inline bool is_module_address(unsigned long addr)
777 {
778 return false;
779 }
780
is_module_percpu_address(unsigned long addr)781 static inline bool is_module_percpu_address(unsigned long addr)
782 {
783 return false;
784 }
785
__is_module_percpu_address(unsigned long addr,unsigned long * can_addr)786 static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
787 {
788 return false;
789 }
790
is_module_text_address(unsigned long addr)791 static inline bool is_module_text_address(unsigned long addr)
792 {
793 return false;
794 }
795
within_module_core(unsigned long addr,const struct module * mod)796 static inline bool within_module_core(unsigned long addr,
797 const struct module *mod)
798 {
799 return false;
800 }
801
within_module_init(unsigned long addr,const struct module * mod)802 static inline bool within_module_init(unsigned long addr,
803 const struct module *mod)
804 {
805 return false;
806 }
807
within_module(unsigned long addr,const struct module * mod)808 static inline bool within_module(unsigned long addr, const struct module *mod)
809 {
810 return false;
811 }
812
813 /* Get/put a kernel symbol (calls should be symmetric) */
814 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak,visibility("hidden"))); &(x); })
815 #define symbol_put(x) do { } while (0)
816 #define symbol_put_addr(x) do { } while (0)
817
__module_get(struct module * module)818 static inline void __module_get(struct module *module)
819 {
820 }
821
try_module_get(struct module * module)822 static inline bool try_module_get(struct module *module)
823 {
824 return true;
825 }
826
module_put(struct module * module)827 static inline void module_put(struct module *module)
828 {
829 }
830
831 #define module_name(mod) "kernel"
832
register_module_notifier(struct notifier_block * nb)833 static inline int register_module_notifier(struct notifier_block *nb)
834 {
835 /* no events will happen anyway, so this can always succeed */
836 return 0;
837 }
838
unregister_module_notifier(struct notifier_block * nb)839 static inline int unregister_module_notifier(struct notifier_block *nb)
840 {
841 return 0;
842 }
843
844 #define module_put_and_kthread_exit(code) kthread_exit(code)
845
print_modules(void)846 static inline void print_modules(void)
847 {
848 }
849
module_requested_async_probing(struct module * module)850 static inline bool module_requested_async_probing(struct module *module)
851 {
852 return false;
853 }
854
855
set_module_sig_enforced(void)856 static inline void set_module_sig_enforced(void)
857 {
858 }
859
860 /* Dereference module function descriptor */
861 static inline
dereference_module_function_descriptor(struct module * mod,void * ptr)862 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
863 {
864 return ptr;
865 }
866
867 #endif /* CONFIG_MODULES */
868
869 #ifdef CONFIG_SYSFS
870 extern struct kset *module_kset;
871 extern const struct kobj_type module_ktype;
872 #endif /* CONFIG_SYSFS */
873
874 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
875
876 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
877
878 #define __MODULE_STRING(x) __stringify(x)
879
880 #ifdef CONFIG_GENERIC_BUG
881 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
882 struct module *);
883 void module_bug_cleanup(struct module *);
884
885 #else /* !CONFIG_GENERIC_BUG */
886
module_bug_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)887 static inline void module_bug_finalize(const Elf_Ehdr *hdr,
888 const Elf_Shdr *sechdrs,
889 struct module *mod)
890 {
891 }
module_bug_cleanup(struct module * mod)892 static inline void module_bug_cleanup(struct module *mod) {}
893 #endif /* CONFIG_GENERIC_BUG */
894
895 #ifdef CONFIG_RETPOLINE
896 extern bool retpoline_module_ok(bool has_retpoline);
897 #else
retpoline_module_ok(bool has_retpoline)898 static inline bool retpoline_module_ok(bool has_retpoline)
899 {
900 return true;
901 }
902 #endif
903
904 #ifdef CONFIG_MODULE_SIG
905 bool is_module_sig_enforced(void);
906
module_sig_ok(struct module * module)907 static inline bool module_sig_ok(struct module *module)
908 {
909 return module->sig_ok;
910 }
911 #else /* !CONFIG_MODULE_SIG */
is_module_sig_enforced(void)912 static inline bool is_module_sig_enforced(void)
913 {
914 return false;
915 }
916
module_sig_ok(struct module * module)917 static inline bool module_sig_ok(struct module *module)
918 {
919 return true;
920 }
921 #endif /* CONFIG_MODULE_SIG */
922
923 #if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS)
924 int module_kallsyms_on_each_symbol(const char *modname,
925 int (*fn)(void *, const char *, unsigned long),
926 void *data);
927
928 /* For kallsyms to ask for address resolution. namebuf should be at
929 * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
930 * found, otherwise NULL.
931 */
932 const char *module_address_lookup(unsigned long addr,
933 unsigned long *symbolsize,
934 unsigned long *offset,
935 char **modname, const unsigned char **modbuildid,
936 char *namebuf);
937 int lookup_module_symbol_name(unsigned long addr, char *symname);
938 int lookup_module_symbol_attrs(unsigned long addr,
939 unsigned long *size,
940 unsigned long *offset,
941 char *modname,
942 char *name);
943
944 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
945 * symnum out of range.
946 */
947 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
948 char *name, char *module_name, int *exported);
949
950 /* Look for this name: can be of form module:name. */
951 unsigned long module_kallsyms_lookup_name(const char *name);
952
953 unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
954
955 #else /* CONFIG_MODULES && CONFIG_KALLSYMS */
956
module_kallsyms_on_each_symbol(const char * modname,int (* fn)(void *,const char *,unsigned long),void * data)957 static inline int module_kallsyms_on_each_symbol(const char *modname,
958 int (*fn)(void *, const char *, unsigned long),
959 void *data)
960 {
961 return -EOPNOTSUPP;
962 }
963
964 /* 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)965 static inline const char *module_address_lookup(unsigned long addr,
966 unsigned long *symbolsize,
967 unsigned long *offset,
968 char **modname,
969 const unsigned char **modbuildid,
970 char *namebuf)
971 {
972 return NULL;
973 }
974
lookup_module_symbol_name(unsigned long addr,char * symname)975 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
976 {
977 return -ERANGE;
978 }
979
module_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * name,char * module_name,int * exported)980 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
981 char *type, char *name,
982 char *module_name, int *exported)
983 {
984 return -ERANGE;
985 }
986
module_kallsyms_lookup_name(const char * name)987 static inline unsigned long module_kallsyms_lookup_name(const char *name)
988 {
989 return 0;
990 }
991
find_kallsyms_symbol_value(struct module * mod,const char * name)992 static inline unsigned long find_kallsyms_symbol_value(struct module *mod,
993 const char *name)
994 {
995 return 0;
996 }
997
998 #endif /* CONFIG_MODULES && CONFIG_KALLSYMS */
999
1000 #endif /* _LINUX_MODULE_H */
1001