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