xref: /linux-6.15/include/linux/module.h (revision ff10fca5)
1 #ifndef _LINUX_MODULE_H
2 #define _LINUX_MODULE_H
3 /*
4  * Dynamic loading of modules into the kernel.
5  *
6  * Rewritten by Richard Henderson <[email protected]> Dec 1996
7  * Rewritten again by Rusty Russell, 2002
8  */
9 #include <linux/list.h>
10 #include <linux/stat.h>
11 #include <linux/compiler.h>
12 #include <linux/cache.h>
13 #include <linux/kmod.h>
14 #include <linux/elf.h>
15 #include <linux/stringify.h>
16 #include <linux/kobject.h>
17 #include <linux/moduleparam.h>
18 #include <linux/tracepoint.h>
19 
20 #include <linux/percpu.h>
21 #include <asm/module.h>
22 
23 #include <trace/events/module.h>
24 
25 /* Not Yet Implemented */
26 #define MODULE_SUPPORTED_DEVICE(name)
27 
28 /* Some toolchains use a `_' prefix for all user symbols. */
29 #ifdef CONFIG_SYMBOL_PREFIX
30 #define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
31 #else
32 #define MODULE_SYMBOL_PREFIX ""
33 #endif
34 
35 #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
36 
37 struct kernel_symbol
38 {
39 	unsigned long value;
40 	const char *name;
41 };
42 
43 struct modversion_info
44 {
45 	unsigned long crc;
46 	char name[MODULE_NAME_LEN];
47 };
48 
49 struct module;
50 
51 struct module_attribute {
52         struct attribute attr;
53         ssize_t (*show)(struct module_attribute *, struct module *, char *);
54         ssize_t (*store)(struct module_attribute *, struct module *,
55 			 const char *, size_t count);
56 	void (*setup)(struct module *, const char *);
57 	int (*test)(struct module *);
58 	void (*free)(struct module *);
59 };
60 
61 struct module_kobject
62 {
63 	struct kobject kobj;
64 	struct module *mod;
65 	struct kobject *drivers_dir;
66 	struct module_param_attrs *mp;
67 };
68 
69 /* These are either module local, or the kernel's dummy ones. */
70 extern int init_module(void);
71 extern void cleanup_module(void);
72 
73 /* Archs provide a method of finding the correct exception table. */
74 struct exception_table_entry;
75 
76 const struct exception_table_entry *
77 search_extable(const struct exception_table_entry *first,
78 	       const struct exception_table_entry *last,
79 	       unsigned long value);
80 void sort_extable(struct exception_table_entry *start,
81 		  struct exception_table_entry *finish);
82 void sort_main_extable(void);
83 void trim_init_extable(struct module *m);
84 
85 #ifdef MODULE
86 #define MODULE_GENERIC_TABLE(gtype,name)			\
87 extern const struct gtype##_id __mod_##gtype##_table		\
88   __attribute__ ((unused, alias(__stringify(name))))
89 
90 extern struct module __this_module;
91 #define THIS_MODULE (&__this_module)
92 #else  /* !MODULE */
93 #define MODULE_GENERIC_TABLE(gtype,name)
94 #define THIS_MODULE ((struct module *)0)
95 #endif
96 
97 /* Generic info of form tag = "info" */
98 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
99 
100 /* For userspace: you can also call me... */
101 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
102 
103 /*
104  * The following license idents are currently accepted as indicating free
105  * software modules
106  *
107  *	"GPL"				[GNU Public License v2 or later]
108  *	"GPL v2"			[GNU Public License v2]
109  *	"GPL and additional rights"	[GNU Public License v2 rights and more]
110  *	"Dual BSD/GPL"			[GNU Public License v2
111  *					 or BSD license choice]
112  *	"Dual MIT/GPL"			[GNU Public License v2
113  *					 or MIT license choice]
114  *	"Dual MPL/GPL"			[GNU Public License v2
115  *					 or Mozilla license choice]
116  *
117  * The following other idents are available
118  *
119  *	"Proprietary"			[Non free products]
120  *
121  * There are dual licensed components, but when running with Linux it is the
122  * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
123  * is a GPL combined work.
124  *
125  * This exists for several reasons
126  * 1.	So modinfo can show license info for users wanting to vet their setup
127  *	is free
128  * 2.	So the community can ignore bug reports including proprietary modules
129  * 3.	So vendors can do likewise based on their own policies
130  */
131 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
132 
133 /*
134  * Author(s), use "Name <email>" or just "Name", for multiple
135  * authors use multiple MODULE_AUTHOR() statements/lines.
136  */
137 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
138 
139 /* What your module does. */
140 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
141 
142 /* One for each parameter, describing how to use it.  Some files do
143    multiple of these per line, so can't just use MODULE_INFO. */
144 #define MODULE_PARM_DESC(_parm, desc) \
145 	__MODULE_INFO(parm, _parm, #_parm ":" desc)
146 
147 #define MODULE_DEVICE_TABLE(type,name)		\
148   MODULE_GENERIC_TABLE(type##_device,name)
149 
150 /* Version of form [<epoch>:]<version>[-<extra-version>].
151    Or for CVS/RCS ID version, everything but the number is stripped.
152   <epoch>: A (small) unsigned integer which allows you to start versions
153            anew. If not mentioned, it's zero.  eg. "2:1.0" is after
154 	   "1:2.0".
155   <version>: The <version> may contain only alphanumerics and the
156            character `.'.  Ordered by numeric sort for numeric parts,
157 	   ascii sort for ascii parts (as per RPM or DEB algorithm).
158   <extraversion>: Like <version>, but inserted for local
159            customizations, eg "rh3" or "rusty1".
160 
161   Using this automatically adds a checksum of the .c files and the
162   local headers in "srcversion".
163 */
164 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
165 
166 /* Optional firmware file (or files) needed by the module
167  * format is simply firmware file name.  Multiple firmware
168  * files require multiple MODULE_FIRMWARE() specifiers */
169 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
170 
171 /* Given an address, look for it in the exception tables */
172 const struct exception_table_entry *search_exception_tables(unsigned long add);
173 
174 struct notifier_block;
175 
176 #ifdef CONFIG_MODULES
177 
178 extern int modules_disabled; /* for sysctl */
179 /* Get/put a kernel symbol (calls must be symmetric) */
180 void *__symbol_get(const char *symbol);
181 void *__symbol_get_gpl(const char *symbol);
182 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
183 
184 /* modules using other modules: kdb wants to see this. */
185 struct module_use {
186 	struct list_head source_list;
187 	struct list_head target_list;
188 	struct module *source, *target;
189 };
190 
191 #ifndef __GENKSYMS__
192 #ifdef CONFIG_MODVERSIONS
193 /* Mark the CRC weak since genksyms apparently decides not to
194  * generate a checksums for some symbols */
195 #define __CRC_SYMBOL(sym, sec)					\
196 	extern void *__crc_##sym __attribute__((weak));		\
197 	static const unsigned long __kcrctab_##sym		\
198 	__used							\
199 	__attribute__((section("__kcrctab" sec), unused))	\
200 	= (unsigned long) &__crc_##sym;
201 #else
202 #define __CRC_SYMBOL(sym, sec)
203 #endif
204 
205 /* For every exported symbol, place a struct in the __ksymtab section */
206 #define __EXPORT_SYMBOL(sym, sec)				\
207 	extern typeof(sym) sym;					\
208 	__CRC_SYMBOL(sym, sec)					\
209 	static const char __kstrtab_##sym[]			\
210 	__attribute__((section("__ksymtab_strings"), aligned(1))) \
211 	= MODULE_SYMBOL_PREFIX #sym;                    	\
212 	static const struct kernel_symbol __ksymtab_##sym	\
213 	__used							\
214 	__attribute__((section("__ksymtab" sec), unused))	\
215 	= { (unsigned long)&sym, __kstrtab_##sym }
216 
217 #define EXPORT_SYMBOL(sym)					\
218 	__EXPORT_SYMBOL(sym, "")
219 
220 #define EXPORT_SYMBOL_GPL(sym)					\
221 	__EXPORT_SYMBOL(sym, "_gpl")
222 
223 #define EXPORT_SYMBOL_GPL_FUTURE(sym)				\
224 	__EXPORT_SYMBOL(sym, "_gpl_future")
225 
226 
227 #ifdef CONFIG_UNUSED_SYMBOLS
228 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
229 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
230 #else
231 #define EXPORT_UNUSED_SYMBOL(sym)
232 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
233 #endif
234 
235 #endif
236 
237 enum module_state
238 {
239 	MODULE_STATE_LIVE,
240 	MODULE_STATE_COMING,
241 	MODULE_STATE_GOING,
242 };
243 
244 struct module
245 {
246 	enum module_state state;
247 
248 	/* Member of list of modules */
249 	struct list_head list;
250 
251 	/* Unique handle for this module */
252 	char name[MODULE_NAME_LEN];
253 
254 	/* Sysfs stuff. */
255 	struct module_kobject mkobj;
256 	struct module_attribute *modinfo_attrs;
257 	const char *version;
258 	const char *srcversion;
259 	struct kobject *holders_dir;
260 
261 	/* Exported symbols */
262 	const struct kernel_symbol *syms;
263 	const unsigned long *crcs;
264 	unsigned int num_syms;
265 
266 	/* Kernel parameters. */
267 	struct kernel_param *kp;
268 	unsigned int num_kp;
269 
270 	/* GPL-only exported symbols. */
271 	unsigned int num_gpl_syms;
272 	const struct kernel_symbol *gpl_syms;
273 	const unsigned long *gpl_crcs;
274 
275 #ifdef CONFIG_UNUSED_SYMBOLS
276 	/* unused exported symbols. */
277 	const struct kernel_symbol *unused_syms;
278 	const unsigned long *unused_crcs;
279 	unsigned int num_unused_syms;
280 
281 	/* GPL-only, unused exported symbols. */
282 	unsigned int num_unused_gpl_syms;
283 	const struct kernel_symbol *unused_gpl_syms;
284 	const unsigned long *unused_gpl_crcs;
285 #endif
286 
287 	/* symbols that will be GPL-only in the near future. */
288 	const struct kernel_symbol *gpl_future_syms;
289 	const unsigned long *gpl_future_crcs;
290 	unsigned int num_gpl_future_syms;
291 
292 	/* Exception table */
293 	unsigned int num_exentries;
294 	struct exception_table_entry *extable;
295 
296 	/* Startup function. */
297 	int (*init)(void);
298 
299 	/* If this is non-NULL, vfree after init() returns */
300 	void *module_init;
301 
302 	/* Here is the actual code + data, vfree'd on unload. */
303 	void *module_core;
304 
305 	/* Here are the sizes of the init and core sections */
306 	unsigned int init_size, core_size;
307 
308 	/* The size of the executable code in each section.  */
309 	unsigned int init_text_size, core_text_size;
310 
311 	/* Arch-specific module values */
312 	struct mod_arch_specific arch;
313 
314 	unsigned int taints;	/* same bits as kernel:tainted */
315 
316 #ifdef CONFIG_GENERIC_BUG
317 	/* Support for BUG */
318 	unsigned num_bugs;
319 	struct list_head bug_list;
320 	struct bug_entry *bug_table;
321 #endif
322 
323 #ifdef CONFIG_KALLSYMS
324 	/*
325 	 * We keep the symbol and string tables for kallsyms.
326 	 * The core_* fields below are temporary, loader-only (they
327 	 * could really be discarded after module init).
328 	 */
329 	Elf_Sym *symtab, *core_symtab;
330 	unsigned int num_symtab, core_num_syms;
331 	char *strtab, *core_strtab;
332 
333 	/* Section attributes */
334 	struct module_sect_attrs *sect_attrs;
335 
336 	/* Notes attributes */
337 	struct module_notes_attrs *notes_attrs;
338 #endif
339 
340 #ifdef CONFIG_SMP
341 	/* Per-cpu data. */
342 	void __percpu *percpu;
343 	unsigned int percpu_size;
344 #endif
345 
346 	/* The command line arguments (may be mangled).  People like
347 	   keeping pointers to this stuff */
348 	char *args;
349 #ifdef CONFIG_TRACEPOINTS
350 	struct tracepoint *tracepoints;
351 	unsigned int num_tracepoints;
352 #endif
353 #ifdef HAVE_JUMP_LABEL
354 	struct jump_entry *jump_entries;
355 	unsigned int num_jump_entries;
356 #endif
357 #ifdef CONFIG_TRACING
358 	const char **trace_bprintk_fmt_start;
359 	unsigned int num_trace_bprintk_fmt;
360 #endif
361 #ifdef CONFIG_EVENT_TRACING
362 	struct ftrace_event_call *trace_events;
363 	unsigned int num_trace_events;
364 #endif
365 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
366 	unsigned long *ftrace_callsites;
367 	unsigned int num_ftrace_callsites;
368 #endif
369 
370 #ifdef CONFIG_MODULE_UNLOAD
371 	/* What modules depend on me? */
372 	struct list_head source_list;
373 	/* What modules do I depend on? */
374 	struct list_head target_list;
375 
376 	/* Who is waiting for us to be unloaded */
377 	struct task_struct *waiter;
378 
379 	/* Destruction function. */
380 	void (*exit)(void);
381 
382 	struct module_ref {
383 		unsigned int incs;
384 		unsigned int decs;
385 	} __percpu *refptr;
386 #endif
387 
388 #ifdef CONFIG_CONSTRUCTORS
389 	/* Constructor functions. */
390 	ctor_fn_t *ctors;
391 	unsigned int num_ctors;
392 #endif
393 };
394 #ifndef MODULE_ARCH_INIT
395 #define MODULE_ARCH_INIT {}
396 #endif
397 
398 extern struct mutex module_mutex;
399 
400 /* FIXME: It'd be nice to isolate modules during init, too, so they
401    aren't used before they (may) fail.  But presently too much code
402    (IDE & SCSI) require entry into the module during init.*/
403 static inline int module_is_live(struct module *mod)
404 {
405 	return mod->state != MODULE_STATE_GOING;
406 }
407 
408 struct module *__module_text_address(unsigned long addr);
409 struct module *__module_address(unsigned long addr);
410 bool is_module_address(unsigned long addr);
411 bool is_module_percpu_address(unsigned long addr);
412 bool is_module_text_address(unsigned long addr);
413 
414 static inline int within_module_core(unsigned long addr, struct module *mod)
415 {
416 	return (unsigned long)mod->module_core <= addr &&
417 	       addr < (unsigned long)mod->module_core + mod->core_size;
418 }
419 
420 static inline int within_module_init(unsigned long addr, struct module *mod)
421 {
422 	return (unsigned long)mod->module_init <= addr &&
423 	       addr < (unsigned long)mod->module_init + mod->init_size;
424 }
425 
426 /* Search for module by name: must hold module_mutex. */
427 struct module *find_module(const char *name);
428 
429 struct symsearch {
430 	const struct kernel_symbol *start, *stop;
431 	const unsigned long *crcs;
432 	enum {
433 		NOT_GPL_ONLY,
434 		GPL_ONLY,
435 		WILL_BE_GPL_ONLY,
436 	} licence;
437 	bool unused;
438 };
439 
440 /* Search for an exported symbol by name. */
441 const struct kernel_symbol *find_symbol(const char *name,
442 					struct module **owner,
443 					const unsigned long **crc,
444 					bool gplok,
445 					bool warn);
446 
447 /* Walk the exported symbol table */
448 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
449 			    unsigned int symnum, void *data), void *data);
450 
451 /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
452    symnum out of range. */
453 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
454 			char *name, char *module_name, int *exported);
455 
456 /* Look for this name: can be of form module:name. */
457 unsigned long module_kallsyms_lookup_name(const char *name);
458 
459 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
460 					     struct module *, unsigned long),
461 				   void *data);
462 
463 extern void __module_put_and_exit(struct module *mod, long code)
464 	__attribute__((noreturn));
465 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
466 
467 #ifdef CONFIG_MODULE_UNLOAD
468 unsigned int module_refcount(struct module *mod);
469 void __symbol_put(const char *symbol);
470 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
471 void symbol_put_addr(void *addr);
472 
473 /* Sometimes we know we already have a refcount, and it's easier not
474    to handle the error case (which only happens with rmmod --wait). */
475 static inline void __module_get(struct module *module)
476 {
477 	if (module) {
478 		preempt_disable();
479 		__this_cpu_inc(module->refptr->incs);
480 		trace_module_get(module, _THIS_IP_);
481 		preempt_enable();
482 	}
483 }
484 
485 static inline int try_module_get(struct module *module)
486 {
487 	int ret = 1;
488 
489 	if (module) {
490 		preempt_disable();
491 
492 		if (likely(module_is_live(module))) {
493 			__this_cpu_inc(module->refptr->incs);
494 			trace_module_get(module, _THIS_IP_);
495 		} else
496 			ret = 0;
497 
498 		preempt_enable();
499 	}
500 	return ret;
501 }
502 
503 extern void module_put(struct module *module);
504 
505 #else /*!CONFIG_MODULE_UNLOAD*/
506 static inline int try_module_get(struct module *module)
507 {
508 	return !module || module_is_live(module);
509 }
510 static inline void module_put(struct module *module)
511 {
512 }
513 static inline void __module_get(struct module *module)
514 {
515 }
516 #define symbol_put(x) do { } while(0)
517 #define symbol_put_addr(p) do { } while(0)
518 
519 #endif /* CONFIG_MODULE_UNLOAD */
520 int use_module(struct module *a, struct module *b);
521 
522 /* This is a #define so the string doesn't get put in every .o file */
523 #define module_name(mod)			\
524 ({						\
525 	struct module *__mod = (mod);		\
526 	__mod ? __mod->name : "kernel";		\
527 })
528 
529 /* For kallsyms to ask for address resolution.  namebuf should be at
530  * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
531  * found, otherwise NULL. */
532 const char *module_address_lookup(unsigned long addr,
533 			    unsigned long *symbolsize,
534 			    unsigned long *offset,
535 			    char **modname,
536 			    char *namebuf);
537 int lookup_module_symbol_name(unsigned long addr, char *symname);
538 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
539 
540 /* For extable.c to search modules' exception tables. */
541 const struct exception_table_entry *search_module_extables(unsigned long addr);
542 
543 int register_module_notifier(struct notifier_block * nb);
544 int unregister_module_notifier(struct notifier_block * nb);
545 
546 extern void print_modules(void);
547 
548 extern void module_update_tracepoints(void);
549 extern int module_get_iter_tracepoints(struct tracepoint_iter *iter);
550 
551 #else /* !CONFIG_MODULES... */
552 #define EXPORT_SYMBOL(sym)
553 #define EXPORT_SYMBOL_GPL(sym)
554 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
555 #define EXPORT_UNUSED_SYMBOL(sym)
556 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
557 
558 /* Given an address, look for it in the exception tables. */
559 static inline const struct exception_table_entry *
560 search_module_extables(unsigned long addr)
561 {
562 	return NULL;
563 }
564 
565 static inline struct module *__module_address(unsigned long addr)
566 {
567 	return NULL;
568 }
569 
570 static inline struct module *__module_text_address(unsigned long addr)
571 {
572 	return NULL;
573 }
574 
575 static inline bool is_module_address(unsigned long addr)
576 {
577 	return false;
578 }
579 
580 static inline bool is_module_percpu_address(unsigned long addr)
581 {
582 	return false;
583 }
584 
585 static inline bool is_module_text_address(unsigned long addr)
586 {
587 	return false;
588 }
589 
590 /* Get/put a kernel symbol (calls should be symmetric) */
591 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
592 #define symbol_put(x) do { } while(0)
593 #define symbol_put_addr(x) do { } while(0)
594 
595 static inline void __module_get(struct module *module)
596 {
597 }
598 
599 static inline int try_module_get(struct module *module)
600 {
601 	return 1;
602 }
603 
604 static inline void module_put(struct module *module)
605 {
606 }
607 
608 #define module_name(mod) "kernel"
609 
610 /* For kallsyms to ask for address resolution.  NULL means not found. */
611 static inline const char *module_address_lookup(unsigned long addr,
612 					  unsigned long *symbolsize,
613 					  unsigned long *offset,
614 					  char **modname,
615 					  char *namebuf)
616 {
617 	return NULL;
618 }
619 
620 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
621 {
622 	return -ERANGE;
623 }
624 
625 static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
626 {
627 	return -ERANGE;
628 }
629 
630 static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
631 					char *type, char *name,
632 					char *module_name, int *exported)
633 {
634 	return -ERANGE;
635 }
636 
637 static inline unsigned long module_kallsyms_lookup_name(const char *name)
638 {
639 	return 0;
640 }
641 
642 static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
643 							   struct module *,
644 							   unsigned long),
645 						 void *data)
646 {
647 	return 0;
648 }
649 
650 static inline int register_module_notifier(struct notifier_block * nb)
651 {
652 	/* no events will happen anyway, so this can always succeed */
653 	return 0;
654 }
655 
656 static inline int unregister_module_notifier(struct notifier_block * nb)
657 {
658 	return 0;
659 }
660 
661 #define module_put_and_exit(code) do_exit(code)
662 
663 static inline void print_modules(void)
664 {
665 }
666 
667 static inline void module_update_tracepoints(void)
668 {
669 }
670 
671 static inline int module_get_iter_tracepoints(struct tracepoint_iter *iter)
672 {
673 	return 0;
674 }
675 
676 #endif /* CONFIG_MODULES */
677 
678 #ifdef CONFIG_SYSFS
679 extern struct kset *module_kset;
680 extern struct kobj_type module_ktype;
681 extern int module_sysfs_initialized;
682 #endif /* CONFIG_SYSFS */
683 
684 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
685 
686 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
687 
688 #define __MODULE_STRING(x) __stringify(x)
689 
690 
691 #ifdef CONFIG_GENERIC_BUG
692 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
693 			 struct module *);
694 void module_bug_cleanup(struct module *);
695 
696 #else	/* !CONFIG_GENERIC_BUG */
697 
698 static inline void module_bug_finalize(const Elf_Ehdr *hdr,
699 					const Elf_Shdr *sechdrs,
700 					struct module *mod)
701 {
702 }
703 static inline void module_bug_cleanup(struct module *mod) {}
704 #endif	/* CONFIG_GENERIC_BUG */
705 
706 #endif /* _LINUX_MODULE_H */
707