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