xref: /linux-6.15/include/linux/module.h (revision febaa65c)
1bf49d9ddSMasahiro Yamada /* SPDX-License-Identifier: GPL-2.0-only */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Dynamic loading of modules into the kernel.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Rewritten by Richard Henderson <[email protected]> Dec 1996
61da177e4SLinus Torvalds  * Rewritten again by Rusty Russell, 2002
71da177e4SLinus Torvalds  */
8bf49d9ddSMasahiro Yamada 
9bf49d9ddSMasahiro Yamada #ifndef _LINUX_MODULE_H
10bf49d9ddSMasahiro Yamada #define _LINUX_MODULE_H
11bf49d9ddSMasahiro Yamada 
121da177e4SLinus Torvalds #include <linux/list.h>
131da177e4SLinus Torvalds #include <linux/stat.h>
149294523eSStephen Boyd #include <linux/buildid.h>
151da177e4SLinus Torvalds #include <linux/compiler.h>
161da177e4SLinus Torvalds #include <linux/cache.h>
171da177e4SLinus Torvalds #include <linux/kmod.h>
180fd972a7SPaul Gortmaker #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/elf.h>
201da177e4SLinus Torvalds #include <linux/stringify.h>
211da177e4SLinus Torvalds #include <linux/kobject.h>
221da177e4SLinus Torvalds #include <linux/moduleparam.h>
23b2e285fcSSteven Rostedt (Red Hat) #include <linux/jump_label.h>
24f5016932SPaul Gortmaker #include <linux/export.h>
2593c2e105SPeter Zijlstra #include <linux/rbtree_latch.h>
26663faf9fSMasami Hiramatsu #include <linux/error-injection.h>
279c0be3f6SMathieu Desnoyers #include <linux/tracepoint-defs.h>
28fe15b50cSPaul E. McKenney #include <linux/srcu.h>
299183c3f9SJosh Poimboeuf #include <linux/static_call_types.h>
307deabd67SJason Baron #include <linux/dynamic_debug.h>
311da177e4SLinus Torvalds 
32e1783a24SChristoph Lameter #include <linux/percpu.h>
331da177e4SLinus Torvalds #include <asm/module.h>
341da177e4SLinus Torvalds 
35730b69d2SRusty Russell #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
361da177e4SLinus Torvalds 
37e865d06bSSeunghun Lee struct modversion_info {
381da177e4SLinus Torvalds 	unsigned long crc;
391da177e4SLinus Torvalds 	char name[MODULE_NAME_LEN];
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds struct module;
430ef76537SPaul Gortmaker struct exception_table_entry;
441da177e4SLinus Torvalds 
454befb026SKay Sievers struct module_kobject {
464befb026SKay Sievers 	struct kobject kobj;
474befb026SKay Sievers 	struct module *mod;
484befb026SKay Sievers 	struct kobject *drivers_dir;
494befb026SKay Sievers 	struct module_param_attrs *mp;
50942e4431SLi Zhong 	struct completion *kobj_completion;
513859a271SKees Cook } __randomize_layout;
524befb026SKay Sievers 
531da177e4SLinus Torvalds struct module_attribute {
541da177e4SLinus Torvalds 	struct attribute attr;
55f3227ffdSThomas Weißschuh 	ssize_t (*show)(const struct module_attribute *, struct module_kobject *,
564befb026SKay Sievers 			char *);
57f3227ffdSThomas Weißschuh 	ssize_t (*store)(const struct module_attribute *, struct module_kobject *,
581da177e4SLinus Torvalds 			 const char *, size_t count);
59c988d2b2SMatt Domsch 	void (*setup)(struct module *, const char *);
60c988d2b2SMatt Domsch 	int (*test)(struct module *);
61c988d2b2SMatt Domsch 	void (*free)(struct module *);
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
64e94965edSDmitry Torokhov struct module_version_attribute {
65e94965edSDmitry Torokhov 	struct module_attribute mattr;
66e94965edSDmitry Torokhov 	const char *module_name;
67e94965edSDmitry Torokhov 	const char *version;
680801a007SJohan Hovold };
69e94965edSDmitry Torokhov 
70f3227ffdSThomas Weißschuh extern ssize_t __modver_version_show(const struct module_attribute *,
714befb026SKay Sievers 				     struct module_kobject *, char *);
729b73a584SDmitry Torokhov 
73f3227ffdSThomas Weißschuh extern const struct module_attribute module_uevent;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /* These are either module local, or the kernel's dummy ones. */
761da177e4SLinus Torvalds extern int init_module(void);
771da177e4SLinus Torvalds extern void cleanup_module(void);
781da177e4SLinus Torvalds 
790fd972a7SPaul Gortmaker #ifndef MODULE
800fd972a7SPaul Gortmaker /**
810fd972a7SPaul Gortmaker  * module_init() - driver initialization entry point
820fd972a7SPaul Gortmaker  * @x: function to be run at kernel boot time or module insertion
830fd972a7SPaul Gortmaker  *
840fd972a7SPaul Gortmaker  * module_init() will either be called during do_initcalls() (if
850fd972a7SPaul Gortmaker  * builtin) or at module insertion time (if a module).  There can only
860fd972a7SPaul Gortmaker  * be one per module.
870fd972a7SPaul Gortmaker  */
880fd972a7SPaul Gortmaker #define module_init(x)	__initcall(x);
890fd972a7SPaul Gortmaker 
900fd972a7SPaul Gortmaker /**
910fd972a7SPaul Gortmaker  * module_exit() - driver exit entry point
920fd972a7SPaul Gortmaker  * @x: function to be run when driver is removed
930fd972a7SPaul Gortmaker  *
940fd972a7SPaul Gortmaker  * module_exit() will wrap the driver clean-up code
950fd972a7SPaul Gortmaker  * with cleanup_module() when used with rmmod when
960fd972a7SPaul Gortmaker  * the driver is a module.  If the driver is statically
970fd972a7SPaul Gortmaker  * compiled into the kernel, module_exit() has no effect.
980fd972a7SPaul Gortmaker  * There can only be one per module.
990fd972a7SPaul Gortmaker  */
1000fd972a7SPaul Gortmaker #define module_exit(x)	__exitcall(x);
1010fd972a7SPaul Gortmaker 
1020fd972a7SPaul Gortmaker #else /* MODULE */
1030fd972a7SPaul Gortmaker 
1040fd972a7SPaul Gortmaker /*
1050fd972a7SPaul Gortmaker  * In most cases loadable modules do not need custom
1060fd972a7SPaul Gortmaker  * initcall levels. There are still some valid cases where
1070fd972a7SPaul Gortmaker  * a driver may be needed early if built in, and does not
1080fd972a7SPaul Gortmaker  * matter when built as a loadable module. Like bus
1090fd972a7SPaul Gortmaker  * snooping debug drivers.
1100fd972a7SPaul Gortmaker  */
1110fd972a7SPaul Gortmaker #define early_initcall(fn)		module_init(fn)
1120fd972a7SPaul Gortmaker #define core_initcall(fn)		module_init(fn)
1130fd972a7SPaul Gortmaker #define core_initcall_sync(fn)		module_init(fn)
1140fd972a7SPaul Gortmaker #define postcore_initcall(fn)		module_init(fn)
1150fd972a7SPaul Gortmaker #define postcore_initcall_sync(fn)	module_init(fn)
1160fd972a7SPaul Gortmaker #define arch_initcall(fn)		module_init(fn)
1170fd972a7SPaul Gortmaker #define subsys_initcall(fn)		module_init(fn)
1180fd972a7SPaul Gortmaker #define subsys_initcall_sync(fn)	module_init(fn)
1190fd972a7SPaul Gortmaker #define fs_initcall(fn)			module_init(fn)
1200fd972a7SPaul Gortmaker #define fs_initcall_sync(fn)		module_init(fn)
1210fd972a7SPaul Gortmaker #define rootfs_initcall(fn)		module_init(fn)
1220fd972a7SPaul Gortmaker #define device_initcall(fn)		module_init(fn)
1230fd972a7SPaul Gortmaker #define device_initcall_sync(fn)	module_init(fn)
1240fd972a7SPaul Gortmaker #define late_initcall(fn)		module_init(fn)
1250fd972a7SPaul Gortmaker #define late_initcall_sync(fn)		module_init(fn)
1260fd972a7SPaul Gortmaker 
1270fd972a7SPaul Gortmaker #define console_initcall(fn)		module_init(fn)
1280fd972a7SPaul Gortmaker 
1290fd972a7SPaul Gortmaker /* Each module must use one module_init(). */
1300fd972a7SPaul Gortmaker #define module_init(initfn)					\
1311f318a8bSArnd Bergmann 	static inline initcall_t __maybe_unused __inittest(void)		\
1320fd972a7SPaul Gortmaker 	{ return initfn; }					\
133cf68fffbSSami Tolvanen 	int init_module(void) __copy(initfn)			\
134cf68fffbSSami Tolvanen 		__attribute__((alias(#initfn)));		\
13592efda8eSSami Tolvanen 	___ADDRESSABLE(init_module, __initdata);
1360fd972a7SPaul Gortmaker 
1370fd972a7SPaul Gortmaker /* This is only required if you want to be unloadable. */
1380fd972a7SPaul Gortmaker #define module_exit(exitfn)					\
1391f318a8bSArnd Bergmann 	static inline exitcall_t __maybe_unused __exittest(void)		\
1400fd972a7SPaul Gortmaker 	{ return exitfn; }					\
141cf68fffbSSami Tolvanen 	void cleanup_module(void) __copy(exitfn)		\
142cf68fffbSSami Tolvanen 		__attribute__((alias(#exitfn)));		\
14392efda8eSSami Tolvanen 	___ADDRESSABLE(cleanup_module, __exitdata);
1440fd972a7SPaul Gortmaker 
1450fd972a7SPaul Gortmaker #endif
1460fd972a7SPaul Gortmaker 
1470fd972a7SPaul Gortmaker /* This means "can be init if no module support, otherwise module load
1480fd972a7SPaul Gortmaker    may call it." */
1490fd972a7SPaul Gortmaker #ifdef CONFIG_MODULES
1500fd972a7SPaul Gortmaker #define __init_or_module
1510fd972a7SPaul Gortmaker #define __initdata_or_module
1520fd972a7SPaul Gortmaker #define __initconst_or_module
1530fd972a7SPaul Gortmaker #define __INIT_OR_MODULE	.text
1540fd972a7SPaul Gortmaker #define __INITDATA_OR_MODULE	.data
1550fd972a7SPaul Gortmaker #define __INITRODATA_OR_MODULE	.section ".rodata","a",%progbits
1560fd972a7SPaul Gortmaker #else
1570fd972a7SPaul Gortmaker #define __init_or_module __init
1580fd972a7SPaul Gortmaker #define __initdata_or_module __initdata
1590fd972a7SPaul Gortmaker #define __initconst_or_module __initconst
1600fd972a7SPaul Gortmaker #define __INIT_OR_MODULE __INIT
1610fd972a7SPaul Gortmaker #define __INITDATA_OR_MODULE __INITDATA
1620fd972a7SPaul Gortmaker #define __INITRODATA_OR_MODULE __INITRODATA
1630fd972a7SPaul Gortmaker #endif /*CONFIG_MODULES*/
1640fd972a7SPaul Gortmaker 
1651da177e4SLinus Torvalds /* Generic info of form tag = "info" */
1661da177e4SLinus Torvalds #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* For userspace: you can also call me... */
1691da177e4SLinus Torvalds #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
1701da177e4SLinus Torvalds 
1717cb14ba7SAndreas Robinson /* Soft module dependencies. See man modprobe.d for details.
1727cb14ba7SAndreas Robinson  * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
1737cb14ba7SAndreas Robinson  */
1747cb14ba7SAndreas Robinson #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
1757cb14ba7SAndreas Robinson 
1761da177e4SLinus Torvalds /*
17761842868SJose Ignacio Tornos Martinez  * Weak module dependencies. See man modprobe.d for details.
17861842868SJose Ignacio Tornos Martinez  * Example: MODULE_WEAKDEP("module-foo")
17961842868SJose Ignacio Tornos Martinez  */
18061842868SJose Ignacio Tornos Martinez #define MODULE_WEAKDEP(_weakdep) MODULE_INFO(weakdep, _weakdep)
18161842868SJose Ignacio Tornos Martinez 
18261842868SJose Ignacio Tornos Martinez /*
1838b41fc44SMasahiro Yamada  * MODULE_FILE is used for generating modules.builtin
1848b41fc44SMasahiro Yamada  * So, make it no-op when this is being built as a module
1858b41fc44SMasahiro Yamada  */
1868b41fc44SMasahiro Yamada #ifdef MODULE
1878b41fc44SMasahiro Yamada #define MODULE_FILE
1888b41fc44SMasahiro Yamada #else
1898b41fc44SMasahiro Yamada #define MODULE_FILE	MODULE_INFO(file, KBUILD_MODFILE);
1908b41fc44SMasahiro Yamada #endif
1918b41fc44SMasahiro Yamada 
1928b41fc44SMasahiro Yamada /*
1931da177e4SLinus Torvalds  * The following license idents are currently accepted as indicating free
1941da177e4SLinus Torvalds  * software modules
1951da177e4SLinus Torvalds  *
196bf7fbeeaSThomas Gleixner  *	"GPL"				[GNU Public License v2]
1971da177e4SLinus Torvalds  *	"GPL v2"			[GNU Public License v2]
1981da177e4SLinus Torvalds  *	"GPL and additional rights"	[GNU Public License v2 rights and more]
1991da177e4SLinus Torvalds  *	"Dual BSD/GPL"			[GNU Public License v2
2001da177e4SLinus Torvalds  *					 or BSD license choice]
2018d27e908SXose Vazquez Perez  *	"Dual MIT/GPL"			[GNU Public License v2
2028d27e908SXose Vazquez Perez  *					 or MIT license choice]
2031da177e4SLinus Torvalds  *	"Dual MPL/GPL"			[GNU Public License v2
2041da177e4SLinus Torvalds  *					 or Mozilla license choice]
2051da177e4SLinus Torvalds  *
2061da177e4SLinus Torvalds  * The following other idents are available
2071da177e4SLinus Torvalds  *
2081da177e4SLinus Torvalds  *	"Proprietary"			[Non free products]
2091da177e4SLinus Torvalds  *
210bf7fbeeaSThomas Gleixner  * Both "GPL v2" and "GPL" (the latter also in dual licensed strings) are
211bf7fbeeaSThomas Gleixner  * merely stating that the module is licensed under the GPL v2, but are not
212bf7fbeeaSThomas Gleixner  * telling whether "GPL v2 only" or "GPL v2 or later". The reason why there
213bf7fbeeaSThomas Gleixner  * are two variants is a historic and failed attempt to convey more
214bf7fbeeaSThomas Gleixner  * information in the MODULE_LICENSE string. For module loading the
215bf7fbeeaSThomas Gleixner  * "only/or later" distinction is completely irrelevant and does neither
216bf7fbeeaSThomas Gleixner  * replace the proper license identifiers in the corresponding source file
217bf7fbeeaSThomas Gleixner  * nor amends them in any way. The sole purpose is to make the
218bf7fbeeaSThomas Gleixner  * 'Proprietary' flagging work and to refuse to bind symbols which are
219bf7fbeeaSThomas Gleixner  * exported with EXPORT_SYMBOL_GPL when a non free module is loaded.
220bf7fbeeaSThomas Gleixner  *
221bf7fbeeaSThomas Gleixner  * In the same way "BSD" is not a clear license information. It merely
222bf7fbeeaSThomas Gleixner  * states, that the module is licensed under one of the compatible BSD
223bf7fbeeaSThomas Gleixner  * license variants. The detailed and correct license information is again
224bf7fbeeaSThomas Gleixner  * to be found in the corresponding source files.
225bf7fbeeaSThomas Gleixner  *
2261da177e4SLinus Torvalds  * There are dual licensed components, but when running with Linux it is the
2271da177e4SLinus Torvalds  * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
2281da177e4SLinus Torvalds  * is a GPL combined work.
2291da177e4SLinus Torvalds  *
2301da177e4SLinus Torvalds  * This exists for several reasons
2311da177e4SLinus Torvalds  * 1.	So modinfo can show license info for users wanting to vet their setup
2321da177e4SLinus Torvalds  *	is free
2331da177e4SLinus Torvalds  * 2.	So the community can ignore bug reports including proprietary modules
2341da177e4SLinus Torvalds  * 3.	So vendors can do likewise based on their own policies
2351da177e4SLinus Torvalds  */
2368b41fc44SMasahiro Yamada #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
2371da177e4SLinus Torvalds 
2381d7015caSJohannes Berg /*
2391d7015caSJohannes Berg  * Author(s), use "Name <email>" or just "Name", for multiple
2401d7015caSJohannes Berg  * authors use multiple MODULE_AUTHOR() statements/lines.
2411d7015caSJohannes Berg  */
2421da177e4SLinus Torvalds #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /* What your module does. */
2451da177e4SLinus Torvalds #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
2461da177e4SLinus Torvalds 
247cff26a51SRusty Russell #ifdef MODULE
248cff26a51SRusty Russell /* Creates an alias so file2alias.c can find device table. */
2491da177e4SLinus Torvalds #define MODULE_DEVICE_TABLE(type, name)					\
250054a9cd3SMasahiro Yamada extern typeof(name) __mod_device_table__##type##__##name		\
251cff26a51SRusty Russell   __attribute__ ((unused, alias(__stringify(name))))
252cff26a51SRusty Russell #else  /* !MODULE */
253cff26a51SRusty Russell #define MODULE_DEVICE_TABLE(type, name)
254cff26a51SRusty Russell #endif
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /* Version of form [<epoch>:]<version>[-<extra-version>].
257e865d06bSSeunghun Lee  * Or for CVS/RCS ID version, everything but the number is stripped.
258e865d06bSSeunghun Lee  * <epoch>: A (small) unsigned integer which allows you to start versions
259e865d06bSSeunghun Lee  * anew. If not mentioned, it's zero.  eg. "2:1.0" is after
260e865d06bSSeunghun Lee  * "1:2.0".
2611da177e4SLinus Torvalds 
262e865d06bSSeunghun Lee  * <version>: The <version> may contain only alphanumerics and the
263e865d06bSSeunghun Lee  * character `.'.  Ordered by numeric sort for numeric parts,
264e865d06bSSeunghun Lee  * ascii sort for ascii parts (as per RPM or DEB algorithm).
265e865d06bSSeunghun Lee 
266e865d06bSSeunghun Lee  * <extraversion>: Like <version>, but inserted for local
267e865d06bSSeunghun Lee  * customizations, eg "rh3" or "rusty1".
268e865d06bSSeunghun Lee 
269e865d06bSSeunghun Lee  * Using this automatically adds a checksum of the .c files and the
270e865d06bSSeunghun Lee  * local headers in "srcversion".
2711da177e4SLinus Torvalds  */
272e94965edSDmitry Torokhov 
2733b90a5b2SRusty Russell #if defined(MODULE) || !defined(CONFIG_SYSFS)
2741da177e4SLinus Torvalds #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
275e94965edSDmitry Torokhov #else
276e94965edSDmitry Torokhov #define MODULE_VERSION(_version)					\
277898490c0SAlexey Gladkov 	MODULE_INFO(version, _version);					\
27838e3fe65SThomas Weißschuh 	static const struct module_version_attribute __modver_attr	\
279b112082cSJohan Hovold 		__used __section("__modver")				\
280b112082cSJohan Hovold 		__aligned(__alignof__(struct module_version_attribute)) \
281b112082cSJohan Hovold 		= {							\
282e94965edSDmitry Torokhov 			.mattr	= {					\
283e94965edSDmitry Torokhov 				.attr	= {				\
284e94965edSDmitry Torokhov 					.name	= "version",		\
285e94965edSDmitry Torokhov 					.mode	= S_IRUGO,		\
286e94965edSDmitry Torokhov 				},					\
287e94965edSDmitry Torokhov 				.show	= __modver_version_show,	\
288e94965edSDmitry Torokhov 			},						\
289e94965edSDmitry Torokhov 			.module_name	= KBUILD_MODNAME,		\
290e94965edSDmitry Torokhov 			.version	= _version,			\
2912d26c716SJohan Hovold 		}
292e94965edSDmitry Torokhov #endif
2931da177e4SLinus Torvalds 
294187afbedSJon Masters /* Optional firmware file (or files) needed by the module
295187afbedSJon Masters  * format is simply firmware file name.  Multiple firmware
296187afbedSJon Masters  * files require multiple MODULE_FIRMWARE() specifiers */
297187afbedSJon Masters #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
298187afbedSJon Masters 
299cdd30ebbSPeter Zijlstra #define MODULE_IMPORT_NS(ns)	MODULE_INFO(import_ns, ns)
3003e4d890aSLinus Torvalds 
3011da177e4SLinus Torvalds struct notifier_block;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds #ifdef CONFIG_MODULES
3041da177e4SLinus Torvalds 
3055ed10910SDave Young extern int modules_disabled; /* for sysctl */
3061da177e4SLinus Torvalds /* Get/put a kernel symbol (calls must be symmetric) */
3071da177e4SLinus Torvalds void *__symbol_get(const char *symbol);
3081da177e4SLinus Torvalds void *__symbol_get_gpl(const char *symbol);
3094c56eb33SMasahiro Yamada #define symbol_get(x)	({ \
3104c56eb33SMasahiro Yamada 	static const char __notrim[] \
3114c56eb33SMasahiro Yamada 		__used __section(".no_trim_symbol") = __stringify(x); \
3124c56eb33SMasahiro Yamada 	(typeof(&x))(__symbol_get(__stringify(x))); })
3131da177e4SLinus Torvalds 
314c8e21cedSRusty Russell /* modules using other modules: kdb wants to see this. */
315c8e21cedSRusty Russell struct module_use {
316c8e21cedSRusty Russell 	struct list_head source_list;
317c8e21cedSRusty Russell 	struct list_head target_list;
318c8e21cedSRusty Russell 	struct module *source, *target;
319c8e21cedSRusty Russell };
320c8e21cedSRusty Russell 
3210d21b0e3SRusty Russell enum module_state {
3220d21b0e3SRusty Russell 	MODULE_STATE_LIVE,	/* Normal state. */
3230d21b0e3SRusty Russell 	MODULE_STATE_COMING,	/* Full formed, running module_init. */
3240d21b0e3SRusty Russell 	MODULE_STATE_GOING,	/* Going away. */
3250d21b0e3SRusty Russell 	MODULE_STATE_UNFORMED,	/* Still setting it up. */
3261da177e4SLinus Torvalds };
3271da177e4SLinus Torvalds 
32893c2e105SPeter Zijlstra struct mod_tree_node {
32993c2e105SPeter Zijlstra 	struct module *mod;
33093c2e105SPeter Zijlstra 	struct latch_tree_node node;
33193c2e105SPeter Zijlstra };
33293c2e105SPeter Zijlstra 
333ac3b4328SSong Liu enum mod_mem_type {
334ac3b4328SSong Liu 	MOD_TEXT = 0,
335ac3b4328SSong Liu 	MOD_DATA,
336ac3b4328SSong Liu 	MOD_RODATA,
337ac3b4328SSong Liu 	MOD_RO_AFTER_INIT,
338ac3b4328SSong Liu 	MOD_INIT_TEXT,
339ac3b4328SSong Liu 	MOD_INIT_DATA,
340ac3b4328SSong Liu 	MOD_INIT_RODATA,
341ac3b4328SSong Liu 
342ac3b4328SSong Liu 	MOD_MEM_NUM_TYPES,
343ac3b4328SSong Liu 	MOD_INVALID = -1,
344ac3b4328SSong Liu };
345ac3b4328SSong Liu 
346ac3b4328SSong Liu #define mod_mem_type_is_init(type)	\
347ac3b4328SSong Liu 	((type) == MOD_INIT_TEXT ||	\
348ac3b4328SSong Liu 	 (type) == MOD_INIT_DATA ||	\
349ac3b4328SSong Liu 	 (type) == MOD_INIT_RODATA)
350ac3b4328SSong Liu 
351ac3b4328SSong Liu #define mod_mem_type_is_core(type) (!mod_mem_type_is_init(type))
352ac3b4328SSong Liu 
353ac3b4328SSong Liu #define mod_mem_type_is_text(type)	\
354ac3b4328SSong Liu 	 ((type) == MOD_TEXT ||		\
355ac3b4328SSong Liu 	  (type) == MOD_INIT_TEXT)
356ac3b4328SSong Liu 
357ac3b4328SSong Liu #define mod_mem_type_is_data(type) (!mod_mem_type_is_text(type))
358ac3b4328SSong Liu 
359ac3b4328SSong Liu #define mod_mem_type_is_core_data(type)	\
360ac3b4328SSong Liu 	(mod_mem_type_is_core(type) &&	\
361ac3b4328SSong Liu 	 mod_mem_type_is_data(type))
362ac3b4328SSong Liu 
363ac3b4328SSong Liu #define for_each_mod_mem_type(type)			\
364ac3b4328SSong Liu 	for (enum mod_mem_type (type) = 0;		\
365ac3b4328SSong Liu 	     (type) < MOD_MEM_NUM_TYPES; (type)++)
366ac3b4328SSong Liu 
367ac3b4328SSong Liu #define for_class_mod_mem_type(type, class)		\
368ac3b4328SSong Liu 	for_each_mod_mem_type(type)			\
369ac3b4328SSong Liu 		if (mod_mem_type_is_##class(type))
370ac3b4328SSong Liu 
371ac3b4328SSong Liu struct module_memory {
3727523e4dcSRusty Russell 	void *base;
3730c133b1eSMike Rapoport (Microsoft) 	void *rw_copy;
3740c133b1eSMike Rapoport (Microsoft) 	bool is_rox;
3757523e4dcSRusty Russell 	unsigned int size;
3767523e4dcSRusty Russell 
3777523e4dcSRusty Russell #ifdef CONFIG_MODULES_TREE_LOOKUP
3787523e4dcSRusty Russell 	struct mod_tree_node mtn;
3797523e4dcSRusty Russell #endif
3807523e4dcSRusty Russell };
3817523e4dcSRusty Russell 
3827523e4dcSRusty Russell #ifdef CONFIG_MODULES_TREE_LOOKUP
3837523e4dcSRusty Russell /* Only touch one cacheline for common rbtree-for-core-layout case. */
384ac3b4328SSong Liu #define __module_memory_align ____cacheline_aligned
3857523e4dcSRusty Russell #else
386ac3b4328SSong Liu #define __module_memory_align
3877523e4dcSRusty Russell #endif
3887523e4dcSRusty Russell 
3898244062eSRusty Russell struct mod_kallsyms {
3908244062eSRusty Russell 	Elf_Sym *symtab;
3918244062eSRusty Russell 	unsigned int num_symtab;
3928244062eSRusty Russell 	char *strtab;
3931c7651f4SEugene Loh 	char *typetab;
3948244062eSRusty Russell };
3958244062eSRusty Russell 
3961ce15ef4SJessica Yu #ifdef CONFIG_LIVEPATCH
397eca0edafSMarcos Paulo de Souza /**
3986486a57fSAlexey Dobriyan  * struct klp_modinfo - ELF information preserved from the livepatch module
399eca0edafSMarcos Paulo de Souza  *
4006486a57fSAlexey Dobriyan  * @hdr: ELF header
401eca0edafSMarcos Paulo de Souza  * @sechdrs: Section header table
402eca0edafSMarcos Paulo de Souza  * @secstrings: String table for the section headers
403eca0edafSMarcos Paulo de Souza  * @symndx: The symbol table section index
404eca0edafSMarcos Paulo de Souza  */
4051ce15ef4SJessica Yu struct klp_modinfo {
4061ce15ef4SJessica Yu 	Elf_Ehdr hdr;
4071ce15ef4SJessica Yu 	Elf_Shdr *sechdrs;
4081ce15ef4SJessica Yu 	char *secstrings;
4091ce15ef4SJessica Yu 	unsigned int symndx;
4101ce15ef4SJessica Yu };
4111ce15ef4SJessica Yu #endif
4121ce15ef4SJessica Yu 
413e865d06bSSeunghun Lee struct module {
4141da177e4SLinus Torvalds 	enum module_state state;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/* Member of list of modules */
4171da177e4SLinus Torvalds 	struct list_head list;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Unique handle for this module */
4201da177e4SLinus Torvalds 	char name[MODULE_NAME_LEN];
4211da177e4SLinus Torvalds 
4229294523eSStephen Boyd #ifdef CONFIG_STACKTRACE_BUILD_ID
4239294523eSStephen Boyd 	/* Module build ID */
4249294523eSStephen Boyd 	unsigned char build_id[BUILD_ID_SIZE_MAX];
4259294523eSStephen Boyd #endif
4269294523eSStephen Boyd 
4271da177e4SLinus Torvalds 	/* Sysfs stuff. */
4281da177e4SLinus Torvalds 	struct module_kobject mkobj;
42903e88ae1SGreg Kroah-Hartman 	struct module_attribute *modinfo_attrs;
430c988d2b2SMatt Domsch 	const char *version;
431c988d2b2SMatt Domsch 	const char *srcversion;
432270a6c4cSKay Sievers 	struct kobject *holders_dir;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/* Exported symbols */
4351da177e4SLinus Torvalds 	const struct kernel_symbol *syms;
4361cd9502eSMasahiro Yamada 	const u32 *crcs;
437af540689SRichard Kennedy 	unsigned int num_syms;
4381da177e4SLinus Torvalds 
43989245600SSami Tolvanen #ifdef CONFIG_ARCH_USES_CFI_TRAPS
44089245600SSami Tolvanen 	s32 *kcfi_traps;
44189245600SSami Tolvanen 	s32 *kcfi_traps_end;
442cf68fffbSSami Tolvanen #endif
443cf68fffbSSami Tolvanen 
444e180a6b7SRusty Russell 	/* Kernel parameters. */
445cf2fde7bSRusty Russell #ifdef CONFIG_SYSFS
446b51d23e4SDan Streetman 	struct mutex param_lock;
447cf2fde7bSRusty Russell #endif
448e180a6b7SRusty Russell 	struct kernel_param *kp;
449e180a6b7SRusty Russell 	unsigned int num_kp;
450e180a6b7SRusty Russell 
4511da177e4SLinus Torvalds 	/* GPL-only exported symbols. */
4521da177e4SLinus Torvalds 	unsigned int num_gpl_syms;
453af540689SRichard Kennedy 	const struct kernel_symbol *gpl_syms;
4541cd9502eSMasahiro Yamada 	const u32 *gpl_crcs;
455262e6ae7SChristoph Hellwig 	bool using_gplonly_symbols;
4561da177e4SLinus Torvalds 
457106a4ee2SRusty Russell #ifdef CONFIG_MODULE_SIG
458106a4ee2SRusty Russell 	/* Signature was verified. */
459106a4ee2SRusty Russell 	bool sig_ok;
460106a4ee2SRusty Russell #endif
461106a4ee2SRusty Russell 
462f2411da7SLuis R. Rodriguez 	bool async_probe_requested;
463f2411da7SLuis R. Rodriguez 
4641da177e4SLinus Torvalds 	/* Exception table */
4651da177e4SLinus Torvalds 	unsigned int num_exentries;
4665e458cc0SRusty Russell 	struct exception_table_entry *extable;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/* Startup function. */
4691da177e4SLinus Torvalds 	int (*init)(void);
4701da177e4SLinus Torvalds 
471ac3b4328SSong Liu 	struct module_memory mem[MOD_MEM_NUM_TYPES] __module_memory_align;
47284e1c6bbSmatthieu castet 
4731da177e4SLinus Torvalds 	/* Arch-specific module values */
4741da177e4SLinus Torvalds 	struct mod_arch_specific arch;
4751da177e4SLinus Torvalds 
4767fd8329bSPetr Mladek 	unsigned long taints;	/* same bits as kernel:taint_flags */
4772bc2d61aSRandy Dunlap 
4787664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
4797664c5a1SJeremy Fitzhardinge 	/* Support for BUG */
480af540689SRichard Kennedy 	unsigned num_bugs;
4817664c5a1SJeremy Fitzhardinge 	struct list_head bug_list;
4827664c5a1SJeremy Fitzhardinge 	struct bug_entry *bug_table;
4831da177e4SLinus Torvalds #endif
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
4868244062eSRusty Russell 	/* Protected by RCU and/or module_mutex: use rcu_dereference() */
4876080d608SMadhuparna Bhowmik 	struct mod_kallsyms __rcu *kallsyms;
4888244062eSRusty Russell 	struct mod_kallsyms core_kallsyms;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* Section attributes */
4911da177e4SLinus Torvalds 	struct module_sect_attrs *sect_attrs;
4926d760133SRoland McGrath 
4936d760133SRoland McGrath 	/* Notes attributes */
4946d760133SRoland McGrath 	struct module_notes_attrs *notes_attrs;
4951da177e4SLinus Torvalds #endif
4961da177e4SLinus Torvalds 
497a288bd65SRichard Kennedy 	/* The command line arguments (may be mangled).  People like
498a288bd65SRichard Kennedy 	   keeping pointers to this stuff */
499a288bd65SRichard Kennedy 	char *args;
500a288bd65SRichard Kennedy 
501259354deSTejun Heo #ifdef CONFIG_SMP
5021da177e4SLinus Torvalds 	/* Per-cpu data. */
503259354deSTejun Heo 	void __percpu *percpu;
504259354deSTejun Heo 	unsigned int percpu_size;
505259354deSTejun Heo #endif
50666e9b071SThomas Gleixner 	void *noinstr_text_start;
50766e9b071SThomas Gleixner 	unsigned int noinstr_text_size;
5081da177e4SLinus Torvalds 
50997e1c18eSMathieu Desnoyers #ifdef CONFIG_TRACEPOINTS
51097e1c18eSMathieu Desnoyers 	unsigned int num_tracepoints;
5119c0be3f6SMathieu Desnoyers 	tracepoint_ptr_t *tracepoints_ptrs;
51297e1c18eSMathieu Desnoyers #endif
513fe15b50cSPaul E. McKenney #ifdef CONFIG_TREE_SRCU
514fe15b50cSPaul E. McKenney 	unsigned int num_srcu_structs;
515fe15b50cSPaul E. McKenney 	struct srcu_struct **srcu_struct_ptrs;
516fe15b50cSPaul E. McKenney #endif
517a38d1107SMatt Mullins #ifdef CONFIG_BPF_EVENTS
518a38d1107SMatt Mullins 	unsigned int num_bpf_raw_events;
519a38d1107SMatt Mullins 	struct bpf_raw_event_map *bpf_raw_events;
520a38d1107SMatt Mullins #endif
52136e68442SAndrii Nakryiko #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
52236e68442SAndrii Nakryiko 	unsigned int btf_data_size;
523d4e48e3dSAlan Maguire 	unsigned int btf_base_data_size;
52436e68442SAndrii Nakryiko 	void *btf_data;
525d4e48e3dSAlan Maguire 	void *btf_base_data;
52636e68442SAndrii Nakryiko #endif
527e9666d10SMasahiro Yamada #ifdef CONFIG_JUMP_LABEL
528bf5438fcSJason Baron 	struct jump_entry *jump_entries;
529bf5438fcSJason Baron 	unsigned int num_jump_entries;
530bf5438fcSJason Baron #endif
531769b0441SFrederic Weisbecker #ifdef CONFIG_TRACING
5321ba28e02SLai Jiangshan 	unsigned int num_trace_bprintk_fmt;
533a288bd65SRichard Kennedy 	const char **trace_bprintk_fmt_start;
5341ba28e02SLai Jiangshan #endif
5356d723736SSteven Rostedt #ifdef CONFIG_EVENT_TRACING
5362425bcb9SSteven Rostedt (Red Hat) 	struct trace_event_call **trace_events;
5376d723736SSteven Rostedt 	unsigned int num_trace_events;
53899be647cSJeremy Linton 	struct trace_eval_map **trace_evals;
53999be647cSJeremy Linton 	unsigned int num_trace_evals;
5406d723736SSteven Rostedt #endif
54193eb677dSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
54293eb677dSSteven Rostedt 	unsigned int num_ftrace_callsites;
543a288bd65SRichard Kennedy 	unsigned long *ftrace_callsites;
54493eb677dSSteven Rostedt #endif
5451e6769b0SMasami Hiramatsu #ifdef CONFIG_KPROBES
5461e6769b0SMasami Hiramatsu 	void *kprobes_text_start;
5471e6769b0SMasami Hiramatsu 	unsigned int kprobes_text_size;
54816db6264SMasami Hiramatsu 	unsigned long *kprobe_blacklist;
54916db6264SMasami Hiramatsu 	unsigned int num_kprobe_blacklist;
5501e6769b0SMasami Hiramatsu #endif
5519183c3f9SJosh Poimboeuf #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
5529183c3f9SJosh Poimboeuf 	int num_static_call_sites;
5539183c3f9SJosh Poimboeuf 	struct static_call_site *static_call_sites;
5549183c3f9SJosh Poimboeuf #endif
5553d6e4462SJeremy Kerr #if IS_ENABLED(CONFIG_KUNIT)
556d81f0d7bSRae Moar 	int num_kunit_init_suites;
557d81f0d7bSRae Moar 	struct kunit_suite **kunit_init_suites;
5583d6e4462SJeremy Kerr 	int num_kunit_suites;
559e5857d39SDaniel Latypov 	struct kunit_suite **kunit_suites;
5603d6e4462SJeremy Kerr #endif
5613d6e4462SJeremy Kerr 
5621ba28e02SLai Jiangshan 
5638cb2c2dcSPetr Mladek #ifdef CONFIG_LIVEPATCH
5641ce15ef4SJessica Yu 	bool klp; /* Is this a livepatch module? */
5658cb2c2dcSPetr Mladek 	bool klp_alive;
5661ce15ef4SJessica Yu 
5676486a57fSAlexey Dobriyan 	/* ELF information */
5681ce15ef4SJessica Yu 	struct klp_modinfo *klp_info;
5698cb2c2dcSPetr Mladek #endif
5708cb2c2dcSPetr Mladek 
57133701557SChris Down #ifdef CONFIG_PRINTK_INDEX
57233701557SChris Down 	unsigned int printk_index_size;
57333701557SChris Down 	struct pi_entry **printk_index_start;
57433701557SChris Down #endif
57533701557SChris Down 
576af540689SRichard Kennedy #ifdef CONFIG_MODULE_UNLOAD
577af540689SRichard Kennedy 	/* What modules depend on me? */
5782c02dfe7SLinus Torvalds 	struct list_head source_list;
5792c02dfe7SLinus Torvalds 	/* What modules do I depend on? */
5802c02dfe7SLinus Torvalds 	struct list_head target_list;
581af540689SRichard Kennedy 
582af540689SRichard Kennedy 	/* Destruction function. */
583af540689SRichard Kennedy 	void (*exit)(void);
584af540689SRichard Kennedy 
5852f35c41fSMasami Hiramatsu 	atomic_t refcnt;
586af540689SRichard Kennedy #endif
587b99b87f7SPeter Oberparleiter 
588b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
589b99b87f7SPeter Oberparleiter 	/* Constructor functions. */
590b99b87f7SPeter Oberparleiter 	ctor_fn_t *ctors;
591b99b87f7SPeter Oberparleiter 	unsigned int num_ctors;
592b99b87f7SPeter Oberparleiter #endif
59392ace999SJosef Bacik 
594540adea3SMasami Hiramatsu #ifdef CONFIG_FUNCTION_ERROR_INJECTION
595663faf9fSMasami Hiramatsu 	struct error_injection_entry *ei_funcs;
596540adea3SMasami Hiramatsu 	unsigned int num_ei_funcs;
59792ace999SJosef Bacik #endif
5987deabd67SJason Baron #ifdef CONFIG_DYNAMIC_DEBUG_CORE
5997deabd67SJason Baron 	struct _ddebug_info dyndbg_info;
6007deabd67SJason Baron #endif
6013859a271SKees Cook } ____cacheline_aligned __randomize_layout;
602e61a1c1cSRoman Zippel #ifndef MODULE_ARCH_INIT
603e61a1c1cSRoman Zippel #define MODULE_ARCH_INIT {}
604e61a1c1cSRoman Zippel #endif
6051da177e4SLinus Torvalds 
60693d77e7fSVincent Whitchurch #ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE
60793d77e7fSVincent Whitchurch static inline unsigned long kallsyms_symbol_value(const Elf_Sym *sym)
60893d77e7fSVincent Whitchurch {
60993d77e7fSVincent Whitchurch 	return sym->st_value;
61093d77e7fSVincent Whitchurch }
61193d77e7fSVincent Whitchurch #endif
61293d77e7fSVincent Whitchurch 
6131da177e4SLinus Torvalds /* FIXME: It'd be nice to isolate modules during init, too, so they
6141da177e4SLinus Torvalds    aren't used before they (may) fail.  But presently too much code
6151da177e4SLinus Torvalds    (IDE & SCSI) require entry into the module during init.*/
616171d864eSYaowei Bai static inline bool module_is_live(struct module *mod)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	return mod->state != MODULE_STATE_GOING;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6217582b7beSMike Rapoport (IBM) static inline bool module_is_coming(struct module *mod)
6227582b7beSMike Rapoport (IBM) {
6237582b7beSMike Rapoport (IBM)         return mod->state == MODULE_STATE_COMING;
6247582b7beSMike Rapoport (IBM) }
6257582b7beSMike Rapoport (IBM) 
6261da177e4SLinus Torvalds struct module *__module_text_address(unsigned long addr);
627e610499eSRusty Russell struct module *__module_address(unsigned long addr);
628e610499eSRusty Russell bool is_module_address(unsigned long addr);
629383776faSThomas Gleixner bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
63010fad5e4STejun Heo bool is_module_percpu_address(unsigned long addr);
631e610499eSRusty Russell bool is_module_text_address(unsigned long addr);
6321da177e4SLinus Torvalds 
633ac3b4328SSong Liu static inline bool within_module_mem_type(unsigned long addr,
634ac3b4328SSong Liu 					  const struct module *mod,
635ac3b4328SSong Liu 					  enum mod_mem_type type)
636ac3b4328SSong Liu {
637ac3b4328SSong Liu 	unsigned long base, size;
638ac3b4328SSong Liu 
639ac3b4328SSong Liu 	base = (unsigned long)mod->mem[type].base;
640ac3b4328SSong Liu 	size = mod->mem[type].size;
641ac3b4328SSong Liu 	return addr - base < size;
642ac3b4328SSong Liu }
643ac3b4328SSong Liu 
64476681c8fSPetr Mladek static inline bool within_module_core(unsigned long addr,
64576681c8fSPetr Mladek 				      const struct module *mod)
646a06f6211SMasami Hiramatsu {
647ac3b4328SSong Liu 	for_class_mod_mem_type(type, core) {
648ac3b4328SSong Liu 		if (within_module_mem_type(addr, mod, type))
64901dc0386SChristophe Leroy 			return true;
650ac3b4328SSong Liu 	}
651ac3b4328SSong Liu 	return false;
652a06f6211SMasami Hiramatsu }
653a06f6211SMasami Hiramatsu 
65476681c8fSPetr Mladek static inline bool within_module_init(unsigned long addr,
65576681c8fSPetr Mladek 				      const struct module *mod)
656a06f6211SMasami Hiramatsu {
657ac3b4328SSong Liu 	for_class_mod_mem_type(type, init) {
658ac3b4328SSong Liu 		if (within_module_mem_type(addr, mod, type))
659ac3b4328SSong Liu 			return true;
660ac3b4328SSong Liu 	}
661ac3b4328SSong Liu 	return false;
662a06f6211SMasami Hiramatsu }
663a06f6211SMasami Hiramatsu 
66476681c8fSPetr Mladek static inline bool within_module(unsigned long addr, const struct module *mod)
6659b20a352SPetr Mladek {
6669b20a352SPetr Mladek 	return within_module_init(addr, mod) || within_module_core(addr, mod);
6679b20a352SPetr Mladek }
6689b20a352SPetr Mladek 
669*febaa65cSSebastian Andrzej Siewior /* Search for module by name: must be in a RCU critical section. */
670c6b37801STim Abbott struct module *find_module(const char *name);
671c6b37801STim Abbott 
672ca3574bdSEric W. Biederman extern void __noreturn __module_put_and_kthread_exit(struct module *mod,
673bf262dceSJiri Kosina 			long code);
674ca3574bdSEric W. Biederman #define module_put_and_kthread_exit(code) __module_put_and_kthread_exit(THIS_MODULE, code)
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds #ifdef CONFIG_MODULE_UNLOAD
677d5db139aSRusty Russell int module_refcount(struct module *mod);
6781da177e4SLinus Torvalds void __symbol_put(const char *symbol);
679996302c5SMasahiro Yamada #define symbol_put(x) __symbol_put(__stringify(x))
6801da177e4SLinus Torvalds void symbol_put_addr(void *addr);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds /* Sometimes we know we already have a refcount, and it's easier not
6831da177e4SLinus Torvalds    to handle the error case (which only happens with rmmod --wait). */
684d53799beSSteven Rostedt extern void __module_get(struct module *module);
6851da177e4SLinus Torvalds 
686557aafacSLuis Chamberlain /**
687557aafacSLuis Chamberlain  * try_module_get() - take module refcount unless module is being removed
688557aafacSLuis Chamberlain  * @module: the module we should check for
689557aafacSLuis Chamberlain  *
690557aafacSLuis Chamberlain  * Only try to get a module reference count if the module is not being removed.
6914515d08aSMarco Pagani  * This call will fail if the module is in the process of being removed.
692557aafacSLuis Chamberlain  *
693557aafacSLuis Chamberlain  * Care must also be taken to ensure the module exists and is alive prior to
694557aafacSLuis Chamberlain  * usage of this call. This can be gauranteed through two means:
695557aafacSLuis Chamberlain  *
696557aafacSLuis Chamberlain  * 1) Direct protection: you know an earlier caller must have increased the
697557aafacSLuis Chamberlain  *    module reference through __module_get(). This can typically be achieved
698557aafacSLuis Chamberlain  *    by having another entity other than the module itself increment the
699557aafacSLuis Chamberlain  *    module reference count.
700557aafacSLuis Chamberlain  *
701557aafacSLuis Chamberlain  * 2) Implied protection: there is an implied protection against module
702557aafacSLuis Chamberlain  *    removal. An example of this is the implied protection used by kernfs /
703557aafacSLuis Chamberlain  *    sysfs. The sysfs store / read file operations are guaranteed to exist
704557aafacSLuis Chamberlain  *    through the use of kernfs's active reference (see kernfs_active()) and a
705557aafacSLuis Chamberlain  *    sysfs / kernfs file removal cannot happen unless the same file is not
706557aafacSLuis Chamberlain  *    active. Therefore, if a sysfs file is being read or written to the module
707557aafacSLuis Chamberlain  *    which created it must still exist. It is therefore safe to use
708557aafacSLuis Chamberlain  *    try_module_get() on module sysfs store / read ops.
709557aafacSLuis Chamberlain  *
710557aafacSLuis Chamberlain  * One of the real values to try_module_get() is the module_is_live() check
711557aafacSLuis Chamberlain  * which ensures that the caller of try_module_get() can yield to userspace
712557aafacSLuis Chamberlain  * module removal requests and gracefully fail if the module is on its way out.
713557aafacSLuis Chamberlain  *
714557aafacSLuis Chamberlain  * Returns true if the reference count was successfully incremented.
715557aafacSLuis Chamberlain  */
716d53799beSSteven Rostedt extern bool try_module_get(struct module *module);
7171da177e4SLinus Torvalds 
718557aafacSLuis Chamberlain /**
719557aafacSLuis Chamberlain  * module_put() - release a reference count to a module
720557aafacSLuis Chamberlain  * @module: the module we should release a reference count for
721557aafacSLuis Chamberlain  *
722557aafacSLuis Chamberlain  * If you successfully bump a reference count to a module with try_module_get(),
723557aafacSLuis Chamberlain  * when you are finished you must call module_put() to release that reference
724557aafacSLuis Chamberlain  * count.
725557aafacSLuis Chamberlain  */
726f6a57033SAl Viro extern void module_put(struct module *module);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds #else /*!CONFIG_MODULE_UNLOAD*/
7298ba4fcdfSGao Feng static inline bool try_module_get(struct module *module)
7301da177e4SLinus Torvalds {
7311da177e4SLinus Torvalds 	return !module || module_is_live(module);
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds static inline void module_put(struct module *module)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds static inline void __module_get(struct module *module)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds #define symbol_put(x) do { } while (0)
7401da177e4SLinus Torvalds #define symbol_put_addr(p) do { } while (0)
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds #endif /* CONFIG_MODULE_UNLOAD */
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /* This is a #define so the string doesn't get put in every .o file */
7451da177e4SLinus Torvalds #define module_name(mod)			\
7461da177e4SLinus Torvalds ({						\
7471da177e4SLinus Torvalds 	struct module *__mod = (mod);		\
7481da177e4SLinus Torvalds 	__mod ? __mod->name : "kernel";		\
7491da177e4SLinus Torvalds })
7501da177e4SLinus Torvalds 
751b865ea64SSergey Senozhatsky /* Dereference module function descriptor */
752b865ea64SSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr);
753b865ea64SSergey Senozhatsky 
7541da177e4SLinus Torvalds int register_module_notifier(struct notifier_block *nb);
7551da177e4SLinus Torvalds int unregister_module_notifier(struct notifier_block *nb);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds extern void print_modules(void);
7581da177e4SLinus Torvalds 
75980c6e146SDmitry Torokhov static inline bool module_requested_async_probing(struct module *module)
76080c6e146SDmitry Torokhov {
76180c6e146SDmitry Torokhov 	return module && module->async_probe_requested;
76280c6e146SDmitry Torokhov }
76380c6e146SDmitry Torokhov 
7641be9473eSAaron Tomlin static inline bool is_livepatch_module(struct module *mod)
7651be9473eSAaron Tomlin {
7661ce15ef4SJessica Yu #ifdef CONFIG_LIVEPATCH
7671ce15ef4SJessica Yu 	return mod->klp;
7681be9473eSAaron Tomlin #else
7691ce15ef4SJessica Yu 	return false;
7701be9473eSAaron Tomlin #endif
7711ce15ef4SJessica Yu }
7721ce15ef4SJessica Yu 
7738db5da0bSMimi Zohar void set_module_sig_enforced(void);
774fda784e5SBruno E. O. Meneguele 
7750c133b1eSMike Rapoport (Microsoft) void *__module_writable_address(struct module *mod, void *loc);
7760c133b1eSMike Rapoport (Microsoft) 
7770c133b1eSMike Rapoport (Microsoft) static inline void *module_writable_address(struct module *mod, void *loc)
7780c133b1eSMike Rapoport (Microsoft) {
7794bcf2974SPetr Pavlu 	if (!IS_ENABLED(CONFIG_ARCH_HAS_EXECMEM_ROX) || !mod ||
7804bcf2974SPetr Pavlu 	    mod->state != MODULE_STATE_UNFORMED)
7810c133b1eSMike Rapoport (Microsoft) 		return loc;
7820c133b1eSMike Rapoport (Microsoft) 	return __module_writable_address(mod, loc);
7830c133b1eSMike Rapoport (Microsoft) }
7840c133b1eSMike Rapoport (Microsoft) 
7851da177e4SLinus Torvalds #else /* !CONFIG_MODULES... */
7861da177e4SLinus Torvalds 
787e610499eSRusty Russell static inline struct module *__module_address(unsigned long addr)
788e610499eSRusty Russell {
789e610499eSRusty Russell 	return NULL;
790e610499eSRusty Russell }
791e610499eSRusty Russell 
7921da177e4SLinus Torvalds static inline struct module *__module_text_address(unsigned long addr)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	return NULL;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
797e610499eSRusty Russell static inline bool is_module_address(unsigned long addr)
7984d435f9dSIngo Molnar {
799e610499eSRusty Russell 	return false;
800e610499eSRusty Russell }
801e610499eSRusty Russell 
802d5e50dafSRandy Dunlap static inline bool is_module_percpu_address(unsigned long addr)
803d5e50dafSRandy Dunlap {
804d5e50dafSRandy Dunlap 	return false;
805d5e50dafSRandy Dunlap }
806d5e50dafSRandy Dunlap 
807383776faSThomas Gleixner static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
808383776faSThomas Gleixner {
809383776faSThomas Gleixner 	return false;
810383776faSThomas Gleixner }
811383776faSThomas Gleixner 
812e610499eSRusty Russell static inline bool is_module_text_address(unsigned long addr)
813e610499eSRusty Russell {
814e610499eSRusty Russell 	return false;
8154d435f9dSIngo Molnar }
8164d435f9dSIngo Molnar 
817007ec26cSDavid Howells static inline bool within_module_core(unsigned long addr,
818007ec26cSDavid Howells 				      const struct module *mod)
819007ec26cSDavid Howells {
820007ec26cSDavid Howells 	return false;
821007ec26cSDavid Howells }
822007ec26cSDavid Howells 
823dadec066STri Vo static inline bool within_module_init(unsigned long addr,
824dadec066STri Vo 				      const struct module *mod)
825dadec066STri Vo {
826dadec066STri Vo 	return false;
827dadec066STri Vo }
828dadec066STri Vo 
829dadec066STri Vo static inline bool within_module(unsigned long addr, const struct module *mod)
830dadec066STri Vo {
831dadec066STri Vo 	return false;
832dadec066STri Vo }
833dadec066STri Vo 
8341da177e4SLinus Torvalds /* Get/put a kernel symbol (calls should be symmetric) */
83513150bc5SArd Biesheuvel #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak,visibility("hidden"))); &(x); })
8361da177e4SLinus Torvalds #define symbol_put(x) do { } while (0)
8371da177e4SLinus Torvalds #define symbol_put_addr(x) do { } while (0)
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds static inline void __module_get(struct module *module)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8438ba4fcdfSGao Feng static inline bool try_module_get(struct module *module)
8441da177e4SLinus Torvalds {
8458ba4fcdfSGao Feng 	return true;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds static inline void module_put(struct module *module)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds #define module_name(mod) "kernel"
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static inline int register_module_notifier(struct notifier_block *nb)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	/* no events will happen anyway, so this can always succeed */
8571da177e4SLinus Torvalds 	return 0;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds static inline int unregister_module_notifier(struct notifier_block *nb)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	return 0;
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
865ca3574bdSEric W. Biederman #define module_put_and_kthread_exit(code) kthread_exit(code)
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds static inline void print_modules(void)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds }
87080c6e146SDmitry Torokhov 
87180c6e146SDmitry Torokhov static inline bool module_requested_async_probing(struct module *module)
87280c6e146SDmitry Torokhov {
87380c6e146SDmitry Torokhov 	return false;
87480c6e146SDmitry Torokhov }
87580c6e146SDmitry Torokhov 
876fda784e5SBruno E. O. Meneguele 
8778db5da0bSMimi Zohar static inline void set_module_sig_enforced(void)
8788db5da0bSMimi Zohar {
8798db5da0bSMimi Zohar }
8808db5da0bSMimi Zohar 
881b865ea64SSergey Senozhatsky /* Dereference module function descriptor */
882b865ea64SSergey Senozhatsky static inline
883b865ea64SSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr)
884b865ea64SSergey Senozhatsky {
885b865ea64SSergey Senozhatsky 	return ptr;
886b865ea64SSergey Senozhatsky }
887b865ea64SSergey Senozhatsky 
8887582b7beSMike Rapoport (IBM) static inline bool module_is_coming(struct module *mod)
8897582b7beSMike Rapoport (IBM) {
8907582b7beSMike Rapoport (IBM) 	return false;
8917582b7beSMike Rapoport (IBM) }
8920c133b1eSMike Rapoport (Microsoft) 
8930c133b1eSMike Rapoport (Microsoft) static inline void *module_writable_address(struct module *mod, void *loc)
8940c133b1eSMike Rapoport (Microsoft) {
8950c133b1eSMike Rapoport (Microsoft) 	return loc;
8960c133b1eSMike Rapoport (Microsoft) }
897ef665c1aSRandy Dunlap #endif /* CONFIG_MODULES */
898ef665c1aSRandy Dunlap 
899ef665c1aSRandy Dunlap #ifdef CONFIG_SYSFS
9007405c1e1SGreg Kroah-Hartman extern struct kset *module_kset;
901042edf1eSThomas Weißschuh extern const struct kobj_type module_ktype;
902ef665c1aSRandy Dunlap #endif /* CONFIG_SYSFS */
903ef665c1aSRandy Dunlap 
9041da177e4SLinus Torvalds #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds #define __MODULE_STRING(x) __stringify(x)
9091da177e4SLinus Torvalds 
9100d9c25ddSAndrew Morton #ifdef CONFIG_GENERIC_BUG
9115336377dSLinus Torvalds void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
9120d9c25ddSAndrew Morton 			 struct module *);
9130d9c25ddSAndrew Morton void module_bug_cleanup(struct module *);
9140d9c25ddSAndrew Morton 
9150d9c25ddSAndrew Morton #else	/* !CONFIG_GENERIC_BUG */
9160d9c25ddSAndrew Morton 
9175336377dSLinus Torvalds static inline void module_bug_finalize(const Elf_Ehdr *hdr,
9180d9c25ddSAndrew Morton 					const Elf_Shdr *sechdrs,
9190d9c25ddSAndrew Morton 					struct module *mod)
9200d9c25ddSAndrew Morton {
9210d9c25ddSAndrew Morton }
9220d9c25ddSAndrew Morton static inline void module_bug_cleanup(struct module *mod) {}
9230d9c25ddSAndrew Morton #endif	/* CONFIG_GENERIC_BUG */
9240d9c25ddSAndrew Morton 
925aefb2f2eSBreno Leitao #ifdef CONFIG_MITIGATION_RETPOLINE
926caf7501aSAndi Kleen extern bool retpoline_module_ok(bool has_retpoline);
927caf7501aSAndi Kleen #else
928caf7501aSAndi Kleen static inline bool retpoline_module_ok(bool has_retpoline)
929caf7501aSAndi Kleen {
930caf7501aSAndi Kleen 	return true;
931caf7501aSAndi Kleen }
932caf7501aSAndi Kleen #endif
933caf7501aSAndi Kleen 
93459afdc7bSHerbert Xu #ifdef CONFIG_MODULE_SIG
9350c1e4280SAaron Tomlin bool is_module_sig_enforced(void);
9360c1e4280SAaron Tomlin 
93759afdc7bSHerbert Xu static inline bool module_sig_ok(struct module *module)
93859afdc7bSHerbert Xu {
93959afdc7bSHerbert Xu 	return module->sig_ok;
94059afdc7bSHerbert Xu }
94159afdc7bSHerbert Xu #else	/* !CONFIG_MODULE_SIG */
9420c1e4280SAaron Tomlin static inline bool is_module_sig_enforced(void)
9430c1e4280SAaron Tomlin {
9440c1e4280SAaron Tomlin 	return false;
9450c1e4280SAaron Tomlin }
9460c1e4280SAaron Tomlin 
94759afdc7bSHerbert Xu static inline bool module_sig_ok(struct module *module)
94859afdc7bSHerbert Xu {
94959afdc7bSHerbert Xu 	return true;
95059afdc7bSHerbert Xu }
95159afdc7bSHerbert Xu #endif	/* CONFIG_MODULE_SIG */
95259afdc7bSHerbert Xu 
95373feb8d5SJiri Olsa #if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS)
95407cc2c93SZhen Lei int module_kallsyms_on_each_symbol(const char *modname,
9553703bd54SZhen Lei 				   int (*fn)(void *, const char *, unsigned long),
9563e355205SChristoph Hellwig 				   void *data);
95734bf9347SViktor Malik 
95834bf9347SViktor Malik /* For kallsyms to ask for address resolution.  namebuf should be at
95934bf9347SViktor Malik  * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
96034bf9347SViktor Malik  * found, otherwise NULL.
96134bf9347SViktor Malik  */
9627e1f4eb9SArnd Bergmann int module_address_lookup(unsigned long addr,
96334bf9347SViktor Malik 			  unsigned long *symbolsize,
96434bf9347SViktor Malik 			  unsigned long *offset,
96534bf9347SViktor Malik 			  char **modname, const unsigned char **modbuildid,
96634bf9347SViktor Malik 			  char *namebuf);
96734bf9347SViktor Malik int lookup_module_symbol_name(unsigned long addr, char *symname);
96834bf9347SViktor Malik int lookup_module_symbol_attrs(unsigned long addr,
96934bf9347SViktor Malik 			       unsigned long *size,
97034bf9347SViktor Malik 			       unsigned long *offset,
97134bf9347SViktor Malik 			       char *modname,
97234bf9347SViktor Malik 			       char *name);
97334bf9347SViktor Malik 
97434bf9347SViktor Malik /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
97534bf9347SViktor Malik  * symnum out of range.
97634bf9347SViktor Malik  */
97734bf9347SViktor Malik int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
97834bf9347SViktor Malik 		       char *name, char *module_name, int *exported);
97934bf9347SViktor Malik 
98034bf9347SViktor Malik /* Look for this name: can be of form module:name. */
98134bf9347SViktor Malik unsigned long module_kallsyms_lookup_name(const char *name);
98234bf9347SViktor Malik 
98334bf9347SViktor Malik unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
98434bf9347SViktor Malik 
98534bf9347SViktor Malik #else	/* CONFIG_MODULES && CONFIG_KALLSYMS */
98634bf9347SViktor Malik 
98707cc2c93SZhen Lei static inline int module_kallsyms_on_each_symbol(const char *modname,
9883703bd54SZhen Lei 						 int (*fn)(void *, const char *, unsigned long),
98973feb8d5SJiri Olsa 						 void *data)
99073feb8d5SJiri Olsa {
99173feb8d5SJiri Olsa 	return -EOPNOTSUPP;
99273feb8d5SJiri Olsa }
99334bf9347SViktor Malik 
99434bf9347SViktor Malik /* For kallsyms to ask for address resolution.  NULL means not found. */
9957e1f4eb9SArnd Bergmann static inline int module_address_lookup(unsigned long addr,
99634bf9347SViktor Malik 						unsigned long *symbolsize,
99734bf9347SViktor Malik 						unsigned long *offset,
99834bf9347SViktor Malik 						char **modname,
99934bf9347SViktor Malik 						const unsigned char **modbuildid,
100034bf9347SViktor Malik 						char *namebuf)
100134bf9347SViktor Malik {
10027e1f4eb9SArnd Bergmann 	return 0;
100334bf9347SViktor Malik }
100434bf9347SViktor Malik 
100534bf9347SViktor Malik static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
100634bf9347SViktor Malik {
100734bf9347SViktor Malik 	return -ERANGE;
100834bf9347SViktor Malik }
100934bf9347SViktor Malik 
101034bf9347SViktor Malik static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
101134bf9347SViktor Malik 				     char *type, char *name,
101234bf9347SViktor Malik 				     char *module_name, int *exported)
101334bf9347SViktor Malik {
101434bf9347SViktor Malik 	return -ERANGE;
101534bf9347SViktor Malik }
101634bf9347SViktor Malik 
101734bf9347SViktor Malik static inline unsigned long module_kallsyms_lookup_name(const char *name)
101834bf9347SViktor Malik {
101934bf9347SViktor Malik 	return 0;
102034bf9347SViktor Malik }
102134bf9347SViktor Malik 
102234bf9347SViktor Malik static inline unsigned long find_kallsyms_symbol_value(struct module *mod,
102334bf9347SViktor Malik 						       const char *name)
102434bf9347SViktor Malik {
102534bf9347SViktor Malik 	return 0;
102634bf9347SViktor Malik }
102734bf9347SViktor Malik 
102873feb8d5SJiri Olsa #endif  /* CONFIG_MODULES && CONFIG_KALLSYMS */
10293e355205SChristoph Hellwig 
10301da177e4SLinus Torvalds #endif /* _LINUX_MODULE_H */
1031