xref: /linux-6.15/include/linux/module.h (revision b2e285fc)
11da177e4SLinus Torvalds #ifndef _LINUX_MODULE_H
21da177e4SLinus Torvalds #define _LINUX_MODULE_H
31da177e4SLinus Torvalds /*
41da177e4SLinus Torvalds  * Dynamic loading of modules into the kernel.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Rewritten by Richard Henderson <[email protected]> Dec 1996
71da177e4SLinus Torvalds  * Rewritten again by Rusty Russell, 2002
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds #include <linux/list.h>
101da177e4SLinus Torvalds #include <linux/stat.h>
111da177e4SLinus Torvalds #include <linux/compiler.h>
121da177e4SLinus Torvalds #include <linux/cache.h>
131da177e4SLinus Torvalds #include <linux/kmod.h>
141da177e4SLinus Torvalds #include <linux/elf.h>
151da177e4SLinus Torvalds #include <linux/stringify.h>
161da177e4SLinus Torvalds #include <linux/kobject.h>
171da177e4SLinus Torvalds #include <linux/moduleparam.h>
18*b2e285fcSSteven Rostedt (Red Hat) #include <linux/jump_label.h>
19f5016932SPaul Gortmaker #include <linux/export.h>
201da177e4SLinus Torvalds 
21e1783a24SChristoph Lameter #include <linux/percpu.h>
221da177e4SLinus Torvalds #include <asm/module.h>
231da177e4SLinus Torvalds 
24106a4ee2SRusty Russell /* In stripped ARM and x86-64 modules, ~ is surprisingly rare. */
25106a4ee2SRusty Russell #define MODULE_SIG_STRING "~Module signature appended~\n"
26106a4ee2SRusty Russell 
271da177e4SLinus Torvalds /* Not Yet Implemented */
281da177e4SLinus Torvalds #define MODULE_SUPPORTED_DEVICE(name)
291da177e4SLinus Torvalds 
30730b69d2SRusty Russell #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
311da177e4SLinus Torvalds 
32e865d06bSSeunghun Lee struct modversion_info {
331da177e4SLinus Torvalds 	unsigned long crc;
341da177e4SLinus Torvalds 	char name[MODULE_NAME_LEN];
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds struct module;
381da177e4SLinus Torvalds 
394befb026SKay Sievers struct module_kobject {
404befb026SKay Sievers 	struct kobject kobj;
414befb026SKay Sievers 	struct module *mod;
424befb026SKay Sievers 	struct kobject *drivers_dir;
434befb026SKay Sievers 	struct module_param_attrs *mp;
44942e4431SLi Zhong 	struct completion *kobj_completion;
454befb026SKay Sievers };
464befb026SKay Sievers 
471da177e4SLinus Torvalds struct module_attribute {
481da177e4SLinus Torvalds 	struct attribute attr;
494befb026SKay Sievers 	ssize_t (*show)(struct module_attribute *, struct module_kobject *,
504befb026SKay Sievers 			char *);
514befb026SKay Sievers 	ssize_t (*store)(struct module_attribute *, struct module_kobject *,
521da177e4SLinus Torvalds 			 const char *, size_t count);
53c988d2b2SMatt Domsch 	void (*setup)(struct module *, const char *);
54c988d2b2SMatt Domsch 	int (*test)(struct module *);
55c988d2b2SMatt Domsch 	void (*free)(struct module *);
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
58e94965edSDmitry Torokhov struct module_version_attribute {
59e94965edSDmitry Torokhov 	struct module_attribute mattr;
60e94965edSDmitry Torokhov 	const char *module_name;
61e94965edSDmitry Torokhov 	const char *version;
6298562ad8SDmitry Torokhov } __attribute__ ((__aligned__(sizeof(void *))));
63e94965edSDmitry Torokhov 
649b73a584SDmitry Torokhov extern ssize_t __modver_version_show(struct module_attribute *,
654befb026SKay Sievers 				     struct module_kobject *, char *);
669b73a584SDmitry Torokhov 
6788bfa324SKay Sievers extern struct module_attribute module_uevent;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* These are either module local, or the kernel's dummy ones. */
701da177e4SLinus Torvalds extern int init_module(void);
711da177e4SLinus Torvalds extern void cleanup_module(void);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Archs provide a method of finding the correct exception table. */
741da177e4SLinus Torvalds struct exception_table_entry;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds const struct exception_table_entry *
771da177e4SLinus Torvalds search_extable(const struct exception_table_entry *first,
781da177e4SLinus Torvalds 	       const struct exception_table_entry *last,
791da177e4SLinus Torvalds 	       unsigned long value);
801da177e4SLinus Torvalds void sort_extable(struct exception_table_entry *start,
811da177e4SLinus Torvalds 		  struct exception_table_entry *finish);
821da177e4SLinus Torvalds void sort_main_extable(void);
83ad6561dfSRusty Russell void trim_init_extable(struct module *m);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #ifdef MODULE
861da177e4SLinus Torvalds #define MODULE_GENERIC_TABLE(gtype, name)			\
871da177e4SLinus Torvalds extern const struct gtype##_id __mod_##gtype##_table		\
881da177e4SLinus Torvalds   __attribute__ ((unused, alias(__stringify(name))))
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #else  /* !MODULE */
911da177e4SLinus Torvalds #define MODULE_GENERIC_TABLE(gtype, name)
921da177e4SLinus Torvalds #endif
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Generic info of form tag = "info" */
951da177e4SLinus Torvalds #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* For userspace: you can also call me... */
981da177e4SLinus Torvalds #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
991da177e4SLinus Torvalds 
1007cb14ba7SAndreas Robinson /* Soft module dependencies. See man modprobe.d for details.
1017cb14ba7SAndreas Robinson  * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
1027cb14ba7SAndreas Robinson  */
1037cb14ba7SAndreas Robinson #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
1047cb14ba7SAndreas Robinson 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * The following license idents are currently accepted as indicating free
1071da177e4SLinus Torvalds  * software modules
1081da177e4SLinus Torvalds  *
1091da177e4SLinus Torvalds  *	"GPL"				[GNU Public License v2 or later]
1101da177e4SLinus Torvalds  *	"GPL v2"			[GNU Public License v2]
1111da177e4SLinus Torvalds  *	"GPL and additional rights"	[GNU Public License v2 rights and more]
1121da177e4SLinus Torvalds  *	"Dual BSD/GPL"			[GNU Public License v2
1131da177e4SLinus Torvalds  *					 or BSD license choice]
1148d27e908SXose Vazquez Perez  *	"Dual MIT/GPL"			[GNU Public License v2
1158d27e908SXose Vazquez Perez  *					 or MIT license choice]
1161da177e4SLinus Torvalds  *	"Dual MPL/GPL"			[GNU Public License v2
1171da177e4SLinus Torvalds  *					 or Mozilla license choice]
1181da177e4SLinus Torvalds  *
1191da177e4SLinus Torvalds  * The following other idents are available
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  *	"Proprietary"			[Non free products]
1221da177e4SLinus Torvalds  *
1231da177e4SLinus Torvalds  * There are dual licensed components, but when running with Linux it is the
1241da177e4SLinus Torvalds  * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
1251da177e4SLinus Torvalds  * is a GPL combined work.
1261da177e4SLinus Torvalds  *
1271da177e4SLinus Torvalds  * This exists for several reasons
1281da177e4SLinus Torvalds  * 1.	So modinfo can show license info for users wanting to vet their setup
1291da177e4SLinus Torvalds  *	is free
1301da177e4SLinus Torvalds  * 2.	So the community can ignore bug reports including proprietary modules
1311da177e4SLinus Torvalds  * 3.	So vendors can do likewise based on their own policies
1321da177e4SLinus Torvalds  */
1331da177e4SLinus Torvalds #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
1341da177e4SLinus Torvalds 
1351d7015caSJohannes Berg /*
1361d7015caSJohannes Berg  * Author(s), use "Name <email>" or just "Name", for multiple
1371d7015caSJohannes Berg  * authors use multiple MODULE_AUTHOR() statements/lines.
1381d7015caSJohannes Berg  */
1391da177e4SLinus Torvalds #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /* What your module does. */
1421da177e4SLinus Torvalds #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #define MODULE_DEVICE_TABLE(type, name)		\
1451da177e4SLinus Torvalds   MODULE_GENERIC_TABLE(type##_device, name)
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /* Version of form [<epoch>:]<version>[-<extra-version>].
148e865d06bSSeunghun Lee  * Or for CVS/RCS ID version, everything but the number is stripped.
149e865d06bSSeunghun Lee  * <epoch>: A (small) unsigned integer which allows you to start versions
150e865d06bSSeunghun Lee  * anew. If not mentioned, it's zero.  eg. "2:1.0" is after
151e865d06bSSeunghun Lee  * "1:2.0".
1521da177e4SLinus Torvalds 
153e865d06bSSeunghun Lee  * <version>: The <version> may contain only alphanumerics and the
154e865d06bSSeunghun Lee  * character `.'.  Ordered by numeric sort for numeric parts,
155e865d06bSSeunghun Lee  * ascii sort for ascii parts (as per RPM or DEB algorithm).
156e865d06bSSeunghun Lee 
157e865d06bSSeunghun Lee  * <extraversion>: Like <version>, but inserted for local
158e865d06bSSeunghun Lee  * customizations, eg "rh3" or "rusty1".
159e865d06bSSeunghun Lee 
160e865d06bSSeunghun Lee  * Using this automatically adds a checksum of the .c files and the
161e865d06bSSeunghun Lee  * local headers in "srcversion".
1621da177e4SLinus Torvalds  */
163e94965edSDmitry Torokhov 
1643b90a5b2SRusty Russell #if defined(MODULE) || !defined(CONFIG_SYSFS)
1651da177e4SLinus Torvalds #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
166e94965edSDmitry Torokhov #else
167e94965edSDmitry Torokhov #define MODULE_VERSION(_version)					\
168b4bc8428SDmitry Torokhov 	static struct module_version_attribute ___modver_attr = {	\
169e94965edSDmitry Torokhov 		.mattr	= {						\
170e94965edSDmitry Torokhov 			.attr	= {					\
171e94965edSDmitry Torokhov 				.name	= "version",			\
172e94965edSDmitry Torokhov 				.mode	= S_IRUGO,			\
173e94965edSDmitry Torokhov 			},						\
174e94965edSDmitry Torokhov 			.show	= __modver_version_show,		\
175e94965edSDmitry Torokhov 		},							\
176e94965edSDmitry Torokhov 		.module_name	= KBUILD_MODNAME,			\
177e94965edSDmitry Torokhov 		.version	= _version,				\
178b4bc8428SDmitry Torokhov 	};								\
179b4bc8428SDmitry Torokhov 	static const struct module_version_attribute			\
180b4bc8428SDmitry Torokhov 	__used __attribute__ ((__section__ ("__modver")))		\
181b4bc8428SDmitry Torokhov 	* __moduleparam_const __modver_attr = &___modver_attr
182e94965edSDmitry Torokhov #endif
1831da177e4SLinus Torvalds 
184187afbedSJon Masters /* Optional firmware file (or files) needed by the module
185187afbedSJon Masters  * format is simply firmware file name.  Multiple firmware
186187afbedSJon Masters  * files require multiple MODULE_FIRMWARE() specifiers */
187187afbedSJon Masters #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
188187afbedSJon Masters 
1891da177e4SLinus Torvalds /* Given an address, look for it in the exception tables */
1901da177e4SLinus Torvalds const struct exception_table_entry *search_exception_tables(unsigned long add);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds struct notifier_block;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds #ifdef CONFIG_MODULES
1951da177e4SLinus Torvalds 
1965ed10910SDave Young extern int modules_disabled; /* for sysctl */
1971da177e4SLinus Torvalds /* Get/put a kernel symbol (calls must be symmetric) */
1981da177e4SLinus Torvalds void *__symbol_get(const char *symbol);
1991da177e4SLinus Torvalds void *__symbol_get_gpl(const char *symbol);
200b92021b0SRusty Russell #define symbol_get(x) ((typeof(&x))(__symbol_get(VMLINUX_SYMBOL_STR(x))))
2011da177e4SLinus Torvalds 
202c8e21cedSRusty Russell /* modules using other modules: kdb wants to see this. */
203c8e21cedSRusty Russell struct module_use {
204c8e21cedSRusty Russell 	struct list_head source_list;
205c8e21cedSRusty Russell 	struct list_head target_list;
206c8e21cedSRusty Russell 	struct module *source, *target;
207c8e21cedSRusty Russell };
208c8e21cedSRusty Russell 
2090d21b0e3SRusty Russell enum module_state {
2100d21b0e3SRusty Russell 	MODULE_STATE_LIVE,	/* Normal state. */
2110d21b0e3SRusty Russell 	MODULE_STATE_COMING,	/* Full formed, running module_init. */
2120d21b0e3SRusty Russell 	MODULE_STATE_GOING,	/* Going away. */
2130d21b0e3SRusty Russell 	MODULE_STATE_UNFORMED,	/* Still setting it up. */
2141da177e4SLinus Torvalds };
2151da177e4SLinus Torvalds 
216bd77c047SEric Dumazet /**
217bd77c047SEric Dumazet  * struct module_ref - per cpu module reference counts
218bd77c047SEric Dumazet  * @incs: number of module get on this cpu
219bd77c047SEric Dumazet  * @decs: number of module put on this cpu
220bd77c047SEric Dumazet  *
221bd77c047SEric Dumazet  * We force an alignment on 8 or 16 bytes, so that alloc_percpu()
222bd77c047SEric Dumazet  * put @incs/@decs in same cache line, with no extra memory cost,
223bd77c047SEric Dumazet  * since alloc_percpu() is fine grained.
224bd77c047SEric Dumazet  */
225bd77c047SEric Dumazet struct module_ref {
226bd77c047SEric Dumazet 	unsigned long incs;
227bd77c047SEric Dumazet 	unsigned long decs;
228bd77c047SEric Dumazet } __attribute((aligned(2 * sizeof(unsigned long))));
229bd77c047SEric Dumazet 
230e865d06bSSeunghun Lee struct module {
2311da177e4SLinus Torvalds 	enum module_state state;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	/* Member of list of modules */
2341da177e4SLinus Torvalds 	struct list_head list;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* Unique handle for this module */
2371da177e4SLinus Torvalds 	char name[MODULE_NAME_LEN];
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* Sysfs stuff. */
2401da177e4SLinus Torvalds 	struct module_kobject mkobj;
24103e88ae1SGreg Kroah-Hartman 	struct module_attribute *modinfo_attrs;
242c988d2b2SMatt Domsch 	const char *version;
243c988d2b2SMatt Domsch 	const char *srcversion;
244270a6c4cSKay Sievers 	struct kobject *holders_dir;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/* Exported symbols */
2471da177e4SLinus Torvalds 	const struct kernel_symbol *syms;
2481da177e4SLinus Torvalds 	const unsigned long *crcs;
249af540689SRichard Kennedy 	unsigned int num_syms;
2501da177e4SLinus Torvalds 
251e180a6b7SRusty Russell 	/* Kernel parameters. */
252e180a6b7SRusty Russell 	struct kernel_param *kp;
253e180a6b7SRusty Russell 	unsigned int num_kp;
254e180a6b7SRusty Russell 
2551da177e4SLinus Torvalds 	/* GPL-only exported symbols. */
2561da177e4SLinus Torvalds 	unsigned int num_gpl_syms;
257af540689SRichard Kennedy 	const struct kernel_symbol *gpl_syms;
2581da177e4SLinus Torvalds 	const unsigned long *gpl_crcs;
2591da177e4SLinus Torvalds 
260f7f5b675SDenys Vlasenko #ifdef CONFIG_UNUSED_SYMBOLS
261f71d20e9SArjan van de Ven 	/* unused exported symbols. */
262f71d20e9SArjan van de Ven 	const struct kernel_symbol *unused_syms;
263f71d20e9SArjan van de Ven 	const unsigned long *unused_crcs;
264af540689SRichard Kennedy 	unsigned int num_unused_syms;
265af540689SRichard Kennedy 
266f71d20e9SArjan van de Ven 	/* GPL-only, unused exported symbols. */
267f71d20e9SArjan van de Ven 	unsigned int num_unused_gpl_syms;
268af540689SRichard Kennedy 	const struct kernel_symbol *unused_gpl_syms;
269f71d20e9SArjan van de Ven 	const unsigned long *unused_gpl_crcs;
270f7f5b675SDenys Vlasenko #endif
271f71d20e9SArjan van de Ven 
272106a4ee2SRusty Russell #ifdef CONFIG_MODULE_SIG
273106a4ee2SRusty Russell 	/* Signature was verified. */
274106a4ee2SRusty Russell 	bool sig_ok;
275106a4ee2SRusty Russell #endif
276106a4ee2SRusty Russell 
2779f28bb7eSGreg Kroah-Hartman 	/* symbols that will be GPL-only in the near future. */
2789f28bb7eSGreg Kroah-Hartman 	const struct kernel_symbol *gpl_future_syms;
2799f28bb7eSGreg Kroah-Hartman 	const unsigned long *gpl_future_crcs;
280af540689SRichard Kennedy 	unsigned int num_gpl_future_syms;
2819f28bb7eSGreg Kroah-Hartman 
2821da177e4SLinus Torvalds 	/* Exception table */
2831da177e4SLinus Torvalds 	unsigned int num_exentries;
2845e458cc0SRusty Russell 	struct exception_table_entry *extable;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/* Startup function. */
2871da177e4SLinus Torvalds 	int (*init)(void);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	/* If this is non-NULL, vfree after init() returns */
2901da177e4SLinus Torvalds 	void *module_init;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	/* Here is the actual code + data, vfree'd on unload. */
2931da177e4SLinus Torvalds 	void *module_core;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/* Here are the sizes of the init and core sections */
2962f0f2a33SDenys Vlasenko 	unsigned int init_size, core_size;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/* The size of the executable code in each section.  */
2992f0f2a33SDenys Vlasenko 	unsigned int init_text_size, core_text_size;
3001da177e4SLinus Torvalds 
30184e1c6bbSmatthieu castet 	/* Size of RO sections of the module (text+rodata) */
30284e1c6bbSmatthieu castet 	unsigned int init_ro_size, core_ro_size;
30384e1c6bbSmatthieu castet 
3041da177e4SLinus Torvalds 	/* Arch-specific module values */
3051da177e4SLinus Torvalds 	struct mod_arch_specific arch;
3061da177e4SLinus Torvalds 
3072bc2d61aSRandy Dunlap 	unsigned int taints;	/* same bits as kernel:tainted */
3082bc2d61aSRandy Dunlap 
3097664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
3107664c5a1SJeremy Fitzhardinge 	/* Support for BUG */
311af540689SRichard Kennedy 	unsigned num_bugs;
3127664c5a1SJeremy Fitzhardinge 	struct list_head bug_list;
3137664c5a1SJeremy Fitzhardinge 	struct bug_entry *bug_table;
3141da177e4SLinus Torvalds #endif
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3174a496226SJan Beulich 	/*
3184a496226SJan Beulich 	 * We keep the symbol and string tables for kallsyms.
3194a496226SJan Beulich 	 * The core_* fields below are temporary, loader-only (they
3204a496226SJan Beulich 	 * could really be discarded after module init).
3214a496226SJan Beulich 	 */
3224a496226SJan Beulich 	Elf_Sym *symtab, *core_symtab;
3234a496226SJan Beulich 	unsigned int num_symtab, core_num_syms;
324554bdfe5SJan Beulich 	char *strtab, *core_strtab;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Section attributes */
3271da177e4SLinus Torvalds 	struct module_sect_attrs *sect_attrs;
3286d760133SRoland McGrath 
3296d760133SRoland McGrath 	/* Notes attributes */
3306d760133SRoland McGrath 	struct module_notes_attrs *notes_attrs;
3311da177e4SLinus Torvalds #endif
3321da177e4SLinus Torvalds 
333a288bd65SRichard Kennedy 	/* The command line arguments (may be mangled).  People like
334a288bd65SRichard Kennedy 	   keeping pointers to this stuff */
335a288bd65SRichard Kennedy 	char *args;
336a288bd65SRichard Kennedy 
337259354deSTejun Heo #ifdef CONFIG_SMP
3381da177e4SLinus Torvalds 	/* Per-cpu data. */
339259354deSTejun Heo 	void __percpu *percpu;
340259354deSTejun Heo 	unsigned int percpu_size;
341259354deSTejun Heo #endif
3421da177e4SLinus Torvalds 
34397e1c18eSMathieu Desnoyers #ifdef CONFIG_TRACEPOINTS
34497e1c18eSMathieu Desnoyers 	unsigned int num_tracepoints;
345a288bd65SRichard Kennedy 	struct tracepoint * const *tracepoints_ptrs;
34697e1c18eSMathieu Desnoyers #endif
347bf5438fcSJason Baron #ifdef HAVE_JUMP_LABEL
348bf5438fcSJason Baron 	struct jump_entry *jump_entries;
349bf5438fcSJason Baron 	unsigned int num_jump_entries;
350bf5438fcSJason Baron #endif
351769b0441SFrederic Weisbecker #ifdef CONFIG_TRACING
3521ba28e02SLai Jiangshan 	unsigned int num_trace_bprintk_fmt;
353a288bd65SRichard Kennedy 	const char **trace_bprintk_fmt_start;
3541ba28e02SLai Jiangshan #endif
3556d723736SSteven Rostedt #ifdef CONFIG_EVENT_TRACING
356e4a9ea5eSSteven Rostedt 	struct ftrace_event_call **trace_events;
3576d723736SSteven Rostedt 	unsigned int num_trace_events;
3586d723736SSteven Rostedt #endif
35993eb677dSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD
36093eb677dSSteven Rostedt 	unsigned int num_ftrace_callsites;
361a288bd65SRichard Kennedy 	unsigned long *ftrace_callsites;
36293eb677dSSteven Rostedt #endif
3631ba28e02SLai Jiangshan 
364af540689SRichard Kennedy #ifdef CONFIG_MODULE_UNLOAD
365af540689SRichard Kennedy 	/* What modules depend on me? */
3662c02dfe7SLinus Torvalds 	struct list_head source_list;
3672c02dfe7SLinus Torvalds 	/* What modules do I depend on? */
3682c02dfe7SLinus Torvalds 	struct list_head target_list;
369af540689SRichard Kennedy 
370af540689SRichard Kennedy 	/* Destruction function. */
371af540689SRichard Kennedy 	void (*exit)(void);
372af540689SRichard Kennedy 
373bd77c047SEric Dumazet 	struct module_ref __percpu *refptr;
374af540689SRichard Kennedy #endif
375b99b87f7SPeter Oberparleiter 
376b99b87f7SPeter Oberparleiter #ifdef CONFIG_CONSTRUCTORS
377b99b87f7SPeter Oberparleiter 	/* Constructor functions. */
378b99b87f7SPeter Oberparleiter 	ctor_fn_t *ctors;
379b99b87f7SPeter Oberparleiter 	unsigned int num_ctors;
380b99b87f7SPeter Oberparleiter #endif
3811da177e4SLinus Torvalds };
382e61a1c1cSRoman Zippel #ifndef MODULE_ARCH_INIT
383e61a1c1cSRoman Zippel #define MODULE_ARCH_INIT {}
384e61a1c1cSRoman Zippel #endif
3851da177e4SLinus Torvalds 
386c6b37801STim Abbott extern struct mutex module_mutex;
387c6b37801STim Abbott 
3881da177e4SLinus Torvalds /* FIXME: It'd be nice to isolate modules during init, too, so they
3891da177e4SLinus Torvalds    aren't used before they (may) fail.  But presently too much code
3901da177e4SLinus Torvalds    (IDE & SCSI) require entry into the module during init.*/
3911da177e4SLinus Torvalds static inline int module_is_live(struct module *mod)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	return mod->state != MODULE_STATE_GOING;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds struct module *__module_text_address(unsigned long addr);
397e610499eSRusty Russell struct module *__module_address(unsigned long addr);
398e610499eSRusty Russell bool is_module_address(unsigned long addr);
39910fad5e4STejun Heo bool is_module_percpu_address(unsigned long addr);
400e610499eSRusty Russell bool is_module_text_address(unsigned long addr);
4011da177e4SLinus Torvalds 
40293843b37SSasha Levin static inline int within_module_core(unsigned long addr, const struct module *mod)
403a06f6211SMasami Hiramatsu {
404a06f6211SMasami Hiramatsu 	return (unsigned long)mod->module_core <= addr &&
405a06f6211SMasami Hiramatsu 	       addr < (unsigned long)mod->module_core + mod->core_size;
406a06f6211SMasami Hiramatsu }
407a06f6211SMasami Hiramatsu 
40893843b37SSasha Levin static inline int within_module_init(unsigned long addr, const struct module *mod)
409a06f6211SMasami Hiramatsu {
410a06f6211SMasami Hiramatsu 	return (unsigned long)mod->module_init <= addr &&
411a06f6211SMasami Hiramatsu 	       addr < (unsigned long)mod->module_init + mod->init_size;
412a06f6211SMasami Hiramatsu }
413a06f6211SMasami Hiramatsu 
414c6b37801STim Abbott /* Search for module by name: must hold module_mutex. */
415c6b37801STim Abbott struct module *find_module(const char *name);
416c6b37801STim Abbott 
417c6b37801STim Abbott struct symsearch {
418c6b37801STim Abbott 	const struct kernel_symbol *start, *stop;
419c6b37801STim Abbott 	const unsigned long *crcs;
420c6b37801STim Abbott 	enum {
421c6b37801STim Abbott 		NOT_GPL_ONLY,
422c6b37801STim Abbott 		GPL_ONLY,
423c6b37801STim Abbott 		WILL_BE_GPL_ONLY,
424c6b37801STim Abbott 	} licence;
425c6b37801STim Abbott 	bool unused;
426c6b37801STim Abbott };
427c6b37801STim Abbott 
428c6b37801STim Abbott /* Search for an exported symbol by name. */
429c6b37801STim Abbott const struct kernel_symbol *find_symbol(const char *name,
430c6b37801STim Abbott 					struct module **owner,
431c6b37801STim Abbott 					const unsigned long **crc,
432c6b37801STim Abbott 					bool gplok,
433c6b37801STim Abbott 					bool warn);
434c6b37801STim Abbott 
435c6b37801STim Abbott /* Walk the exported symbol table */
436de4d8d53SRusty Russell bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
437de4d8d53SRusty Russell 				    struct module *owner,
438de4d8d53SRusty Russell 				    void *data), void *data);
439c6b37801STim Abbott 
440ea07890aSAlexey Dobriyan /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
4411da177e4SLinus Torvalds    symnum out of range. */
442ea07890aSAlexey Dobriyan int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
443ea07890aSAlexey Dobriyan 			char *name, char *module_name, int *exported);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /* Look for this name: can be of form module:name. */
4461da177e4SLinus Torvalds unsigned long module_kallsyms_lookup_name(const char *name);
4471da177e4SLinus Torvalds 
44875a66614SAnders Kaseorg int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
44975a66614SAnders Kaseorg 					     struct module *, unsigned long),
45075a66614SAnders Kaseorg 				   void *data);
45175a66614SAnders Kaseorg 
4521da177e4SLinus Torvalds extern void __module_put_and_exit(struct module *mod, long code)
4531da177e4SLinus Torvalds 	__attribute__((noreturn));
45474e22facSJoe Perches #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code)
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds #ifdef CONFIG_MODULE_UNLOAD
457bd77c047SEric Dumazet unsigned long module_refcount(struct module *mod);
4581da177e4SLinus Torvalds void __symbol_put(const char *symbol);
459b92021b0SRusty Russell #define symbol_put(x) __symbol_put(VMLINUX_SYMBOL_STR(x))
4601da177e4SLinus Torvalds void symbol_put_addr(void *addr);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /* Sometimes we know we already have a refcount, and it's easier not
4631da177e4SLinus Torvalds    to handle the error case (which only happens with rmmod --wait). */
464d53799beSSteven Rostedt extern void __module_get(struct module *module);
4651da177e4SLinus Torvalds 
466d53799beSSteven Rostedt /* This is the Right Way to get a module: if it fails, it's being removed,
467d53799beSSteven Rostedt  * so pretend it's not there. */
468d53799beSSteven Rostedt extern bool try_module_get(struct module *module);
4691da177e4SLinus Torvalds 
470f6a57033SAl Viro extern void module_put(struct module *module);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds #else /*!CONFIG_MODULE_UNLOAD*/
4731da177e4SLinus Torvalds static inline int try_module_get(struct module *module)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	return !module || module_is_live(module);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds static inline void module_put(struct module *module)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds static inline void __module_get(struct module *module)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds #define symbol_put(x) do { } while (0)
4841da177e4SLinus Torvalds #define symbol_put_addr(p) do { } while (0)
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds #endif /* CONFIG_MODULE_UNLOAD */
487dfd62d1dSAnders Kaseorg int ref_module(struct module *a, struct module *b);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds /* This is a #define so the string doesn't get put in every .o file */
4901da177e4SLinus Torvalds #define module_name(mod)			\
4911da177e4SLinus Torvalds ({						\
4921da177e4SLinus Torvalds 	struct module *__mod = (mod);		\
4931da177e4SLinus Torvalds 	__mod ? __mod->name : "kernel";		\
4941da177e4SLinus Torvalds })
4951da177e4SLinus Torvalds 
4966dd06c9fSRusty Russell /* For kallsyms to ask for address resolution.  namebuf should be at
4976dd06c9fSRusty Russell  * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
4986dd06c9fSRusty Russell  * found, otherwise NULL. */
49992dfc9dcSAndrew Morton const char *module_address_lookup(unsigned long addr,
5001da177e4SLinus Torvalds 			    unsigned long *symbolsize,
5011da177e4SLinus Torvalds 			    unsigned long *offset,
5026dd06c9fSRusty Russell 			    char **modname,
5036dd06c9fSRusty Russell 			    char *namebuf);
5049d65cb4aSAlexey Dobriyan int lookup_module_symbol_name(unsigned long addr, char *symname);
505a5c43daeSAlexey Dobriyan int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /* For extable.c to search modules' exception tables. */
5081da177e4SLinus Torvalds const struct exception_table_entry *search_module_extables(unsigned long addr);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds int register_module_notifier(struct notifier_block *nb);
5111da177e4SLinus Torvalds int unregister_module_notifier(struct notifier_block *nb);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds extern void print_modules(void);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds #else /* !CONFIG_MODULES... */
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /* Given an address, look for it in the exception tables. */
5181da177e4SLinus Torvalds static inline const struct exception_table_entry *
5191da177e4SLinus Torvalds search_module_extables(unsigned long addr)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	return NULL;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
524e610499eSRusty Russell static inline struct module *__module_address(unsigned long addr)
525e610499eSRusty Russell {
526e610499eSRusty Russell 	return NULL;
527e610499eSRusty Russell }
528e610499eSRusty Russell 
5291da177e4SLinus Torvalds static inline struct module *__module_text_address(unsigned long addr)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	return NULL;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
534e610499eSRusty Russell static inline bool is_module_address(unsigned long addr)
5354d435f9dSIngo Molnar {
536e610499eSRusty Russell 	return false;
537e610499eSRusty Russell }
538e610499eSRusty Russell 
539d5e50dafSRandy Dunlap static inline bool is_module_percpu_address(unsigned long addr)
540d5e50dafSRandy Dunlap {
541d5e50dafSRandy Dunlap 	return false;
542d5e50dafSRandy Dunlap }
543d5e50dafSRandy Dunlap 
544e610499eSRusty Russell static inline bool is_module_text_address(unsigned long addr)
545e610499eSRusty Russell {
546e610499eSRusty Russell 	return false;
5474d435f9dSIngo Molnar }
5484d435f9dSIngo Molnar 
5491da177e4SLinus Torvalds /* Get/put a kernel symbol (calls should be symmetric) */
5501da177e4SLinus Torvalds #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
5511da177e4SLinus Torvalds #define symbol_put(x) do { } while (0)
5521da177e4SLinus Torvalds #define symbol_put_addr(x) do { } while (0)
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds static inline void __module_get(struct module *module)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds static inline int try_module_get(struct module *module)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	return 1;
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds static inline void module_put(struct module *module)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds #define module_name(mod) "kernel"
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /* For kallsyms to ask for address resolution.  NULL means not found. */
57092dfc9dcSAndrew Morton static inline const char *module_address_lookup(unsigned long addr,
5711da177e4SLinus Torvalds 					  unsigned long *symbolsize,
5721da177e4SLinus Torvalds 					  unsigned long *offset,
5736dd06c9fSRusty Russell 					  char **modname,
5746dd06c9fSRusty Russell 					  char *namebuf)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	return NULL;
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5799d65cb4aSAlexey Dobriyan static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
5809d65cb4aSAlexey Dobriyan {
5819d65cb4aSAlexey Dobriyan 	return -ERANGE;
5829d65cb4aSAlexey Dobriyan }
5839d65cb4aSAlexey Dobriyan 
584a5c43daeSAlexey Dobriyan static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
585a5c43daeSAlexey Dobriyan {
586a5c43daeSAlexey Dobriyan 	return -ERANGE;
587a5c43daeSAlexey Dobriyan }
588a5c43daeSAlexey Dobriyan 
589ea07890aSAlexey Dobriyan static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
590ea07890aSAlexey Dobriyan 					char *type, char *name,
591ea07890aSAlexey Dobriyan 					char *module_name, int *exported)
5921da177e4SLinus Torvalds {
593ea07890aSAlexey Dobriyan 	return -ERANGE;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds static inline unsigned long module_kallsyms_lookup_name(const char *name)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	return 0;
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
60175a66614SAnders Kaseorg static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
60275a66614SAnders Kaseorg 							   struct module *,
60375a66614SAnders Kaseorg 							   unsigned long),
60475a66614SAnders Kaseorg 						 void *data)
60575a66614SAnders Kaseorg {
60675a66614SAnders Kaseorg 	return 0;
60775a66614SAnders Kaseorg }
60875a66614SAnders Kaseorg 
6091da177e4SLinus Torvalds static inline int register_module_notifier(struct notifier_block *nb)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	/* no events will happen anyway, so this can always succeed */
6121da177e4SLinus Torvalds 	return 0;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds static inline int unregister_module_notifier(struct notifier_block *nb)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	return 0;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds #define module_put_and_exit(code) do_exit(code)
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds static inline void print_modules(void)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds }
625ef665c1aSRandy Dunlap #endif /* CONFIG_MODULES */
626ef665c1aSRandy Dunlap 
627ef665c1aSRandy Dunlap #ifdef CONFIG_SYSFS
6287405c1e1SGreg Kroah-Hartman extern struct kset *module_kset;
6297405c1e1SGreg Kroah-Hartman extern struct kobj_type module_ktype;
6307405c1e1SGreg Kroah-Hartman extern int module_sysfs_initialized;
631ef665c1aSRandy Dunlap #endif /* CONFIG_SYSFS */
632ef665c1aSRandy Dunlap 
6331da177e4SLinus Torvalds #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds #define __MODULE_STRING(x) __stringify(x)
6381da177e4SLinus Torvalds 
63984e1c6bbSmatthieu castet #ifdef CONFIG_DEBUG_SET_MODULE_RONX
64084e1c6bbSmatthieu castet extern void set_all_modules_text_rw(void);
64184e1c6bbSmatthieu castet extern void set_all_modules_text_ro(void);
64284e1c6bbSmatthieu castet #else
64384e1c6bbSmatthieu castet static inline void set_all_modules_text_rw(void) { }
64484e1c6bbSmatthieu castet static inline void set_all_modules_text_ro(void) { }
64584e1c6bbSmatthieu castet #endif
6460d9c25ddSAndrew Morton 
6470d9c25ddSAndrew Morton #ifdef CONFIG_GENERIC_BUG
6485336377dSLinus Torvalds void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
6490d9c25ddSAndrew Morton 			 struct module *);
6500d9c25ddSAndrew Morton void module_bug_cleanup(struct module *);
6510d9c25ddSAndrew Morton 
6520d9c25ddSAndrew Morton #else	/* !CONFIG_GENERIC_BUG */
6530d9c25ddSAndrew Morton 
6545336377dSLinus Torvalds static inline void module_bug_finalize(const Elf_Ehdr *hdr,
6550d9c25ddSAndrew Morton 					const Elf_Shdr *sechdrs,
6560d9c25ddSAndrew Morton 					struct module *mod)
6570d9c25ddSAndrew Morton {
6580d9c25ddSAndrew Morton }
6590d9c25ddSAndrew Morton static inline void module_bug_cleanup(struct module *mod) {}
6600d9c25ddSAndrew Morton #endif	/* CONFIG_GENERIC_BUG */
6610d9c25ddSAndrew Morton 
6621da177e4SLinus Torvalds #endif /* _LINUX_MODULE_H */
663