1
2
3
4
5
6
7
8
9#ifndef _LINUX_MODULE_H
10#define _LINUX_MODULE_H
11
12#include <linux/list.h>
13#include <linux/stat.h>
14#include <linux/compiler.h>
15#include <linux/cache.h>
16#include <linux/kmod.h>
17#include <linux/init.h>
18#include <linux/elf.h>
19#include <linux/stringify.h>
20#include <linux/kobject.h>
21#include <linux/moduleparam.h>
22#include <linux/jump_label.h>
23#include <linux/export.h>
24#include <linux/rbtree_latch.h>
25#include <linux/error-injection.h>
26#include <linux/tracepoint-defs.h>
27#include <linux/srcu.h>
28#include <linux/static_call_types.h>
29
30#include <linux/percpu.h>
31#include <asm/module.h>
32
33
34#define MODULE_SUPPORTED_DEVICE(name)
35
36#define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
37
38struct modversion_info {
39 unsigned long crc;
40 char name[MODULE_NAME_LEN];
41};
42
43struct module;
44struct exception_table_entry;
45
46struct module_kobject {
47 struct kobject kobj;
48 struct module *mod;
49 struct kobject *drivers_dir;
50 struct module_param_attrs *mp;
51 struct completion *kobj_completion;
52} __randomize_layout;
53
54struct module_attribute {
55 struct attribute attr;
56 ssize_t (*show)(struct module_attribute *, struct module_kobject *,
57 char *);
58 ssize_t (*store)(struct module_attribute *, struct module_kobject *,
59 const char *, size_t count);
60 void (*setup)(struct module *, const char *);
61 int (*test)(struct module *);
62 void (*free)(struct module *);
63};
64
65struct module_version_attribute {
66 struct module_attribute mattr;
67 const char *module_name;
68 const char *version;
69};
70
71extern ssize_t __modver_version_show(struct module_attribute *,
72 struct module_kobject *, char *);
73
74extern struct module_attribute module_uevent;
75
76
77extern int init_module(void);
78extern void cleanup_module(void);
79
80#ifndef MODULE
81
82
83
84
85
86
87
88
89#define module_init(x) __initcall(x);
90
91
92
93
94
95
96
97
98
99
100
101#define module_exit(x) __exitcall(x);
102
103#else
104
105
106
107
108
109
110
111
112#define early_initcall(fn) module_init(fn)
113#define core_initcall(fn) module_init(fn)
114#define core_initcall_sync(fn) module_init(fn)
115#define postcore_initcall(fn) module_init(fn)
116#define postcore_initcall_sync(fn) module_init(fn)
117#define arch_initcall(fn) module_init(fn)
118#define subsys_initcall(fn) module_init(fn)
119#define subsys_initcall_sync(fn) module_init(fn)
120#define fs_initcall(fn) module_init(fn)
121#define fs_initcall_sync(fn) module_init(fn)
122#define rootfs_initcall(fn) module_init(fn)
123#define device_initcall(fn) module_init(fn)
124#define device_initcall_sync(fn) module_init(fn)
125#define late_initcall(fn) module_init(fn)
126#define late_initcall_sync(fn) module_init(fn)
127
128#define console_initcall(fn) module_init(fn)
129
130
131#define module_init(initfn) \
132 static inline initcall_t __maybe_unused __inittest(void) \
133 { return initfn; } \
134 int init_module(void) __copy(initfn) __attribute__((alias(#initfn)));
135
136
137#define module_exit(exitfn) \
138 static inline exitcall_t __maybe_unused __exittest(void) \
139 { return exitfn; } \
140 void cleanup_module(void) __copy(exitfn) __attribute__((alias(#exitfn)));
141
142#endif
143
144
145
146#ifdef CONFIG_MODULES
147#define __init_or_module
148#define __initdata_or_module
149#define __initconst_or_module
150#define __INIT_OR_MODULE .text
151#define __INITDATA_OR_MODULE .data
152#define __INITRODATA_OR_MODULE .section ".rodata","a",%progbits
153#else
154#define __init_or_module __init
155#define __initdata_or_module __initdata
156#define __initconst_or_module __initconst
157#define __INIT_OR_MODULE __INIT
158#define __INITDATA_OR_MODULE __INITDATA
159#define __INITRODATA_OR_MODULE __INITRODATA
160#endif
161
162
163#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
164
165
166#define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
167
168
169
170
171#define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
172
173
174
175
176
177#ifdef MODULE
178#define MODULE_FILE
179#else
180#define MODULE_FILE MODULE_INFO(file, KBUILD_MODFILE);
181#endif
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227#define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
228
229
230
231
232
233#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
234
235
236#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
237
238#ifdef MODULE
239
240#define MODULE_DEVICE_TABLE(type, name) \
241extern typeof(name) __mod_##type##__##name##_device_table \
242 __attribute__ ((unused, alias(__stringify(name))))
243#else
244#define MODULE_DEVICE_TABLE(type, name)
245#endif
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264#if defined(MODULE) || !defined(CONFIG_SYSFS)
265#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
266#else
267#define MODULE_VERSION(_version) \
268 MODULE_INFO(version, _version); \
269 static struct module_version_attribute __modver_attr \
270 __used __section("__modver") \
271 __aligned(__alignof__(struct module_version_attribute)) \
272 = { \
273 .mattr = { \
274 .attr = { \
275 .name = "version", \
276 .mode = S_IRUGO, \
277 }, \
278 .show = __modver_version_show, \
279 }, \
280 .module_name = KBUILD_MODNAME, \
281 .version = _version, \
282 }
283#endif
284
285
286
287
288#define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
289
290#define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, #ns)
291
292struct notifier_block;
293
294#ifdef CONFIG_MODULES
295
296extern int modules_disabled;
297
298void *__symbol_get(const char *symbol);
299void *__symbol_get_gpl(const char *symbol);
300#define symbol_get(x) ((typeof(&x))(__symbol_get(__stringify(x))))
301
302
303struct module_use {
304 struct list_head source_list;
305 struct list_head target_list;
306 struct module *source, *target;
307};
308
309enum module_state {
310 MODULE_STATE_LIVE,
311 MODULE_STATE_COMING,
312 MODULE_STATE_GOING,
313 MODULE_STATE_UNFORMED,
314};
315
316struct mod_tree_node {
317 struct module *mod;
318 struct latch_tree_node node;
319};
320
321struct module_layout {
322
323 void *base;
324
325 unsigned int size;
326
327 unsigned int text_size;
328
329 unsigned int ro_size;
330
331 unsigned int ro_after_init_size;
332
333#ifdef CONFIG_MODULES_TREE_LOOKUP
334 struct mod_tree_node mtn;
335#endif
336};
337
338#ifdef CONFIG_MODULES_TREE_LOOKUP
339
340#define __module_layout_align ____cacheline_aligned
341#else
342#define __module_layout_align
343#endif
344
345struct mod_kallsyms {
346 Elf_Sym *symtab;
347 unsigned int num_symtab;
348 char *strtab;
349 char *typetab;
350};
351
352#ifdef CONFIG_LIVEPATCH
353struct klp_modinfo {
354 Elf_Ehdr hdr;
355 Elf_Shdr *sechdrs;
356 char *secstrings;
357 unsigned int symndx;
358};
359#endif
360
361struct module {
362 enum module_state state;
363
364
365 struct list_head list;
366
367
368 char name[MODULE_NAME_LEN];
369
370
371 struct module_kobject mkobj;
372 struct module_attribute *modinfo_attrs;
373 const char *version;
374 const char *srcversion;
375 struct kobject *holders_dir;
376
377
378 const struct kernel_symbol *syms;
379 const s32 *crcs;
380 unsigned int num_syms;
381
382
383#ifdef CONFIG_SYSFS
384 struct mutex param_lock;
385#endif
386 struct kernel_param *kp;
387 unsigned int num_kp;
388
389
390 unsigned int num_gpl_syms;
391 const struct kernel_symbol *gpl_syms;
392 const s32 *gpl_crcs;
393 bool using_gplonly_symbols;
394
395#ifdef CONFIG_UNUSED_SYMBOLS
396
397 const struct kernel_symbol *unused_syms;
398 const s32 *unused_crcs;
399 unsigned int num_unused_syms;
400
401
402 unsigned int num_unused_gpl_syms;
403 const struct kernel_symbol *unused_gpl_syms;
404 const s32 *unused_gpl_crcs;
405#endif
406
407#ifdef CONFIG_MODULE_SIG
408
409 bool sig_ok;
410#endif
411
412 bool async_probe_requested;
413
414
415 const struct kernel_symbol *gpl_future_syms;
416 const s32 *gpl_future_crcs;
417 unsigned int num_gpl_future_syms;
418
419
420 unsigned int num_exentries;
421 struct exception_table_entry *extable;
422
423
424 int (*init)(void);
425
426
427 struct module_layout core_layout __module_layout_align;
428 struct module_layout init_layout;
429
430
431 struct mod_arch_specific arch;
432
433 unsigned long taints;
434
435#ifdef CONFIG_GENERIC_BUG
436
437 unsigned num_bugs;
438 struct list_head bug_list;
439 struct bug_entry *bug_table;
440#endif
441
442#ifdef CONFIG_KALLSYMS
443
444 struct mod_kallsyms __rcu *kallsyms;
445 struct mod_kallsyms core_kallsyms;
446
447
448 struct module_sect_attrs *sect_attrs;
449
450
451 struct module_notes_attrs *notes_attrs;
452#endif
453
454
455
456 char *args;
457
458#ifdef CONFIG_SMP
459
460 void __percpu *percpu;
461 unsigned int percpu_size;
462#endif
463 void *noinstr_text_start;
464 unsigned int noinstr_text_size;
465
466#ifdef CONFIG_TRACEPOINTS
467 unsigned int num_tracepoints;
468 tracepoint_ptr_t *tracepoints_ptrs;
469#endif
470#ifdef CONFIG_TREE_SRCU
471 unsigned int num_srcu_structs;
472 struct srcu_struct **srcu_struct_ptrs;
473#endif
474#ifdef CONFIG_BPF_EVENTS
475 unsigned int num_bpf_raw_events;
476 struct bpf_raw_event_map *bpf_raw_events;
477#endif
478#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
479 unsigned int btf_data_size;
480 void *btf_data;
481#endif
482#ifdef CONFIG_JUMP_LABEL
483 struct jump_entry *jump_entries;
484 unsigned int num_jump_entries;
485#endif
486#ifdef CONFIG_TRACING
487 unsigned int num_trace_bprintk_fmt;
488 const char **trace_bprintk_fmt_start;
489#endif
490#ifdef CONFIG_EVENT_TRACING
491 struct trace_event_call **trace_events;
492 unsigned int num_trace_events;
493 struct trace_eval_map **trace_evals;
494 unsigned int num_trace_evals;
495#endif
496#ifdef CONFIG_FTRACE_MCOUNT_RECORD
497 unsigned int num_ftrace_callsites;
498 unsigned long *ftrace_callsites;
499#endif
500#ifdef CONFIG_KPROBES
501 void *kprobes_text_start;
502 unsigned int kprobes_text_size;
503 unsigned long *kprobe_blacklist;
504 unsigned int num_kprobe_blacklist;
505#endif
506#ifdef CONFIG_HAVE_STATIC_CALL_INLINE
507 int num_static_call_sites;
508 struct static_call_site *static_call_sites;
509#endif
510
511#ifdef CONFIG_LIVEPATCH
512 bool klp;
513 bool klp_alive;
514
515
516 struct klp_modinfo *klp_info;
517#endif
518
519#ifdef CONFIG_MODULE_UNLOAD
520
521 struct list_head source_list;
522
523 struct list_head target_list;
524
525
526 void (*exit)(void);
527
528 atomic_t refcnt;
529#endif
530
531#ifdef CONFIG_CONSTRUCTORS
532
533 ctor_fn_t *ctors;
534 unsigned int num_ctors;
535#endif
536
537#ifdef CONFIG_FUNCTION_ERROR_INJECTION
538 struct error_injection_entry *ei_funcs;
539 unsigned int num_ei_funcs;
540#endif
541} ____cacheline_aligned __randomize_layout;
542#ifndef MODULE_ARCH_INIT
543#define MODULE_ARCH_INIT {}
544#endif
545
546#ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE
547static inline unsigned long kallsyms_symbol_value(const Elf_Sym *sym)
548{
549 return sym->st_value;
550}
551#endif
552
553extern struct mutex module_mutex;
554
555
556
557
558static inline bool module_is_live(struct module *mod)
559{
560 return mod->state != MODULE_STATE_GOING;
561}
562
563struct module *__module_text_address(unsigned long addr);
564struct module *__module_address(unsigned long addr);
565bool is_module_address(unsigned long addr);
566bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
567bool is_module_percpu_address(unsigned long addr);
568bool is_module_text_address(unsigned long addr);
569
570static inline bool within_module_core(unsigned long addr,
571 const struct module *mod)
572{
573 return (unsigned long)mod->core_layout.base <= addr &&
574 addr < (unsigned long)mod->core_layout.base + mod->core_layout.size;
575}
576
577static inline bool within_module_init(unsigned long addr,
578 const struct module *mod)
579{
580 return (unsigned long)mod->init_layout.base <= addr &&
581 addr < (unsigned long)mod->init_layout.base + mod->init_layout.size;
582}
583
584static inline bool within_module(unsigned long addr, const struct module *mod)
585{
586 return within_module_init(addr, mod) || within_module_core(addr, mod);
587}
588
589
590struct module *find_module(const char *name);
591
592struct symsearch {
593 const struct kernel_symbol *start, *stop;
594 const s32 *crcs;
595 enum mod_license {
596 NOT_GPL_ONLY,
597 GPL_ONLY,
598 WILL_BE_GPL_ONLY,
599 } license;
600 bool unused;
601};
602
603
604
605int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
606 char *name, char *module_name, int *exported);
607
608
609unsigned long module_kallsyms_lookup_name(const char *name);
610
611int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
612 struct module *, unsigned long),
613 void *data);
614
615extern void __noreturn __module_put_and_exit(struct module *mod,
616 long code);
617#define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code)
618
619#ifdef CONFIG_MODULE_UNLOAD
620int module_refcount(struct module *mod);
621void __symbol_put(const char *symbol);
622#define symbol_put(x) __symbol_put(__stringify(x))
623void symbol_put_addr(void *addr);
624
625
626
627extern void __module_get(struct module *module);
628
629
630
631extern bool try_module_get(struct module *module);
632
633extern void module_put(struct module *module);
634
635#else
636static inline bool try_module_get(struct module *module)
637{
638 return !module || module_is_live(module);
639}
640static inline void module_put(struct module *module)
641{
642}
643static inline void __module_get(struct module *module)
644{
645}
646#define symbol_put(x) do { } while (0)
647#define symbol_put_addr(p) do { } while (0)
648
649#endif
650
651
652#define module_name(mod) \
653({ \
654 struct module *__mod = (mod); \
655 __mod ? __mod->name : "kernel"; \
656})
657
658
659void *dereference_module_function_descriptor(struct module *mod, void *ptr);
660
661
662
663
664const char *module_address_lookup(unsigned long addr,
665 unsigned long *symbolsize,
666 unsigned long *offset,
667 char **modname,
668 char *namebuf);
669int lookup_module_symbol_name(unsigned long addr, char *symname);
670int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
671
672int register_module_notifier(struct notifier_block *nb);
673int unregister_module_notifier(struct notifier_block *nb);
674
675extern void print_modules(void);
676
677static inline bool module_requested_async_probing(struct module *module)
678{
679 return module && module->async_probe_requested;
680}
681
682#ifdef CONFIG_LIVEPATCH
683static inline bool is_livepatch_module(struct module *mod)
684{
685 return mod->klp;
686}
687#else
688static inline bool is_livepatch_module(struct module *mod)
689{
690 return false;
691}
692#endif
693
694bool is_module_sig_enforced(void);
695void set_module_sig_enforced(void);
696
697#else
698
699static inline struct module *__module_address(unsigned long addr)
700{
701 return NULL;
702}
703
704static inline struct module *__module_text_address(unsigned long addr)
705{
706 return NULL;
707}
708
709static inline bool is_module_address(unsigned long addr)
710{
711 return false;
712}
713
714static inline bool is_module_percpu_address(unsigned long addr)
715{
716 return false;
717}
718
719static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
720{
721 return false;
722}
723
724static inline bool is_module_text_address(unsigned long addr)
725{
726 return false;
727}
728
729static inline bool within_module_core(unsigned long addr,
730 const struct module *mod)
731{
732 return false;
733}
734
735static inline bool within_module_init(unsigned long addr,
736 const struct module *mod)
737{
738 return false;
739}
740
741static inline bool within_module(unsigned long addr, const struct module *mod)
742{
743 return false;
744}
745
746
747#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak,visibility("hidden"))); &(x); })
748#define symbol_put(x) do { } while (0)
749#define symbol_put_addr(x) do { } while (0)
750
751static inline void __module_get(struct module *module)
752{
753}
754
755static inline bool try_module_get(struct module *module)
756{
757 return true;
758}
759
760static inline void module_put(struct module *module)
761{
762}
763
764#define module_name(mod) "kernel"
765
766
767static inline const char *module_address_lookup(unsigned long addr,
768 unsigned long *symbolsize,
769 unsigned long *offset,
770 char **modname,
771 char *namebuf)
772{
773 return NULL;
774}
775
776static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
777{
778 return -ERANGE;
779}
780
781static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
782{
783 return -ERANGE;
784}
785
786static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
787 char *type, char *name,
788 char *module_name, int *exported)
789{
790 return -ERANGE;
791}
792
793static inline unsigned long module_kallsyms_lookup_name(const char *name)
794{
795 return 0;
796}
797
798static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
799 struct module *,
800 unsigned long),
801 void *data)
802{
803 return 0;
804}
805
806static inline int register_module_notifier(struct notifier_block *nb)
807{
808
809 return 0;
810}
811
812static inline int unregister_module_notifier(struct notifier_block *nb)
813{
814 return 0;
815}
816
817#define module_put_and_exit(code) do_exit(code)
818
819static inline void print_modules(void)
820{
821}
822
823static inline bool module_requested_async_probing(struct module *module)
824{
825 return false;
826}
827
828static inline bool is_module_sig_enforced(void)
829{
830 return false;
831}
832
833static inline void set_module_sig_enforced(void)
834{
835}
836
837
838static inline
839void *dereference_module_function_descriptor(struct module *mod, void *ptr)
840{
841 return ptr;
842}
843
844#endif
845
846#ifdef CONFIG_SYSFS
847extern struct kset *module_kset;
848extern struct kobj_type module_ktype;
849extern int module_sysfs_initialized;
850#endif
851
852#define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
853
854
855
856#define __MODULE_STRING(x) __stringify(x)
857
858#ifdef CONFIG_GENERIC_BUG
859void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
860 struct module *);
861void module_bug_cleanup(struct module *);
862
863#else
864
865static inline void module_bug_finalize(const Elf_Ehdr *hdr,
866 const Elf_Shdr *sechdrs,
867 struct module *mod)
868{
869}
870static inline void module_bug_cleanup(struct module *mod) {}
871#endif
872
873#ifdef CONFIG_RETPOLINE
874extern bool retpoline_module_ok(bool has_retpoline);
875#else
876static inline bool retpoline_module_ok(bool has_retpoline)
877{
878 return true;
879}
880#endif
881
882#ifdef CONFIG_MODULE_SIG
883static inline bool module_sig_ok(struct module *module)
884{
885 return module->sig_ok;
886}
887#else
888static inline bool module_sig_ok(struct module *module)
889{
890 return true;
891}
892#endif
893
894#endif
895