module.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Dynamic loading of modules into the kernel.
  4. *
  5. * Rewritten by Richard Henderson <[email protected]> Dec 1996
  6. * Rewritten again by Rusty Russell, 2002
  7. */
  8. #ifndef _LINUX_MODULE_H
  9. #define _LINUX_MODULE_H
  10. #include <linux/list.h>
  11. #include <linux/stat.h>
  12. #include <linux/buildid.h>
  13. #include <linux/compiler.h>
  14. #include <linux/cache.h>
  15. #include <linux/kmod.h>
  16. #include <linux/init.h>
  17. #include <linux/elf.h>
  18. #include <linux/stringify.h>
  19. #include <linux/kobject.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/jump_label.h>
  22. #include <linux/export.h>
  23. #include <linux/rbtree_latch.h>
  24. #include <linux/error-injection.h>
  25. #include <linux/tracepoint-defs.h>
  26. #include <linux/srcu.h>
  27. #include <linux/static_call_types.h>
  28. #include <linux/android_kabi.h>
  29. #include <linux/percpu.h>
  30. #include <asm/module.h>
  31. #define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
  32. struct modversion_info {
  33. unsigned long crc;
  34. char name[MODULE_NAME_LEN];
  35. };
  36. struct module;
  37. struct exception_table_entry;
  38. struct module_kobject {
  39. struct kobject kobj;
  40. struct module *mod;
  41. struct kobject *drivers_dir;
  42. struct module_param_attrs *mp;
  43. struct completion *kobj_completion;
  44. } __randomize_layout;
  45. struct module_attribute {
  46. struct attribute attr;
  47. ssize_t (*show)(struct module_attribute *, struct module_kobject *,
  48. char *);
  49. ssize_t (*store)(struct module_attribute *, struct module_kobject *,
  50. const char *, size_t count);
  51. void (*setup)(struct module *, const char *);
  52. int (*test)(struct module *);
  53. void (*free)(struct module *);
  54. };
  55. struct module_version_attribute {
  56. struct module_attribute mattr;
  57. const char *module_name;
  58. const char *version;
  59. };
  60. extern ssize_t __modver_version_show(struct module_attribute *,
  61. struct module_kobject *, char *);
  62. extern struct module_attribute module_uevent;
  63. /* These are either module local, or the kernel's dummy ones. */
  64. extern int init_module(void);
  65. extern void cleanup_module(void);
  66. #ifndef MODULE
  67. /**
  68. * module_init() - driver initialization entry point
  69. * @x: function to be run at kernel boot time or module insertion
  70. *
  71. * module_init() will either be called during do_initcalls() (if
  72. * builtin) or at module insertion time (if a module). There can only
  73. * be one per module.
  74. */
  75. #define module_init(x) __initcall(x);
  76. /**
  77. * module_exit() - driver exit entry point
  78. * @x: function to be run when driver is removed
  79. *
  80. * module_exit() will wrap the driver clean-up code
  81. * with cleanup_module() when used with rmmod when
  82. * the driver is a module. If the driver is statically
  83. * compiled into the kernel, module_exit() has no effect.
  84. * There can only be one per module.
  85. */
  86. #define module_exit(x) __exitcall(x);
  87. #else /* MODULE */
  88. /*
  89. * In most cases loadable modules do not need custom
  90. * initcall levels. There are still some valid cases where
  91. * a driver may be needed early if built in, and does not
  92. * matter when built as a loadable module. Like bus
  93. * snooping debug drivers.
  94. */
  95. #define early_initcall(fn) module_init(fn)
  96. #define core_initcall(fn) module_init(fn)
  97. #define core_initcall_sync(fn) module_init(fn)
  98. #define postcore_initcall(fn) module_init(fn)
  99. #define postcore_initcall_sync(fn) module_init(fn)
  100. #define arch_initcall(fn) module_init(fn)
  101. #define subsys_initcall(fn) module_init(fn)
  102. #define subsys_initcall_sync(fn) module_init(fn)
  103. #define fs_initcall(fn) module_init(fn)
  104. #define fs_initcall_sync(fn) module_init(fn)
  105. #define rootfs_initcall(fn) module_init(fn)
  106. #define device_initcall(fn) module_init(fn)
  107. #define device_initcall_sync(fn) module_init(fn)
  108. #define late_initcall(fn) module_init(fn)
  109. #define late_initcall_sync(fn) module_init(fn)
  110. #define console_initcall(fn) module_init(fn)
  111. /* Each module must use one module_init(). */
  112. #define module_init(initfn) \
  113. static inline initcall_t __maybe_unused __inittest(void) \
  114. { return initfn; } \
  115. int init_module(void) __copy(initfn) \
  116. __attribute__((alias(#initfn))); \
  117. ___ADDRESSABLE(init_module, __initdata);
  118. /* This is only required if you want to be unloadable. */
  119. #define module_exit(exitfn) \
  120. static inline exitcall_t __maybe_unused __exittest(void) \
  121. { return exitfn; } \
  122. void cleanup_module(void) __copy(exitfn) \
  123. __attribute__((alias(#exitfn))); \
  124. ___ADDRESSABLE(cleanup_module, __exitdata);
  125. #endif
  126. /* This means "can be init if no module support, otherwise module load
  127. may call it." */
  128. #ifdef CONFIG_MODULES
  129. #define __init_or_module
  130. #define __initdata_or_module
  131. #define __initconst_or_module
  132. #define __INIT_OR_MODULE .text
  133. #define __INITDATA_OR_MODULE .data
  134. #define __INITRODATA_OR_MODULE .section ".rodata","a",%progbits
  135. #else
  136. #define __init_or_module __init
  137. #define __initdata_or_module __initdata
  138. #define __initconst_or_module __initconst
  139. #define __INIT_OR_MODULE __INIT
  140. #define __INITDATA_OR_MODULE __INITDATA
  141. #define __INITRODATA_OR_MODULE __INITRODATA
  142. #endif /*CONFIG_MODULES*/
  143. /* Generic info of form tag = "info" */
  144. #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
  145. /* For userspace: you can also call me... */
  146. #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
  147. /* Soft module dependencies. See man modprobe.d for details.
  148. * Example: MODULE_SOFTDEP("pre: module-foo module-bar post: module-baz")
  149. */
  150. #define MODULE_SOFTDEP(_softdep) MODULE_INFO(softdep, _softdep)
  151. /*
  152. * MODULE_FILE is used for generating modules.builtin
  153. * So, make it no-op when this is being built as a module
  154. */
  155. #ifdef MODULE
  156. #define MODULE_FILE
  157. #else
  158. #define MODULE_FILE MODULE_INFO(file, KBUILD_MODFILE);
  159. #endif
  160. /*
  161. * The following license idents are currently accepted as indicating free
  162. * software modules
  163. *
  164. * "GPL" [GNU Public License v2]
  165. * "GPL v2" [GNU Public License v2]
  166. * "GPL and additional rights" [GNU Public License v2 rights and more]
  167. * "Dual BSD/GPL" [GNU Public License v2
  168. * or BSD license choice]
  169. * "Dual MIT/GPL" [GNU Public License v2
  170. * or MIT license choice]
  171. * "Dual MPL/GPL" [GNU Public License v2
  172. * or Mozilla license choice]
  173. *
  174. * The following other idents are available
  175. *
  176. * "Proprietary" [Non free products]
  177. *
  178. * Both "GPL v2" and "GPL" (the latter also in dual licensed strings) are
  179. * merely stating that the module is licensed under the GPL v2, but are not
  180. * telling whether "GPL v2 only" or "GPL v2 or later". The reason why there
  181. * are two variants is a historic and failed attempt to convey more
  182. * information in the MODULE_LICENSE string. For module loading the
  183. * "only/or later" distinction is completely irrelevant and does neither
  184. * replace the proper license identifiers in the corresponding source file
  185. * nor amends them in any way. The sole purpose is to make the
  186. * 'Proprietary' flagging work and to refuse to bind symbols which are
  187. * exported with EXPORT_SYMBOL_GPL when a non free module is loaded.
  188. *
  189. * In the same way "BSD" is not a clear license information. It merely
  190. * states, that the module is licensed under one of the compatible BSD
  191. * license variants. The detailed and correct license information is again
  192. * to be found in the corresponding source files.
  193. *
  194. * There are dual licensed components, but when running with Linux it is the
  195. * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
  196. * is a GPL combined work.
  197. *
  198. * This exists for several reasons
  199. * 1. So modinfo can show license info for users wanting to vet their setup
  200. * is free
  201. * 2. So the community can ignore bug reports including proprietary modules
  202. * 3. So vendors can do likewise based on their own policies
  203. */
  204. #define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
  205. /*
  206. * Author(s), use "Name <email>" or just "Name", for multiple
  207. * authors use multiple MODULE_AUTHOR() statements/lines.
  208. */
  209. #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
  210. /* What your module does. */
  211. #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
  212. #ifdef MODULE
  213. /* Creates an alias so file2alias.c can find device table. */
  214. #define MODULE_DEVICE_TABLE(type, name) \
  215. extern typeof(name) __mod_##type##__##name##_device_table \
  216. __attribute__ ((unused, alias(__stringify(name))))
  217. #else /* !MODULE */
  218. #define MODULE_DEVICE_TABLE(type, name)
  219. #endif
  220. /* Version of form [<epoch>:]<version>[-<extra-version>].
  221. * Or for CVS/RCS ID version, everything but the number is stripped.
  222. * <epoch>: A (small) unsigned integer which allows you to start versions
  223. * anew. If not mentioned, it's zero. eg. "2:1.0" is after
  224. * "1:2.0".
  225. * <version>: The <version> may contain only alphanumerics and the
  226. * character `.'. Ordered by numeric sort for numeric parts,
  227. * ascii sort for ascii parts (as per RPM or DEB algorithm).
  228. * <extraversion>: Like <version>, but inserted for local
  229. * customizations, eg "rh3" or "rusty1".
  230. * Using this automatically adds a checksum of the .c files and the
  231. * local headers in "srcversion".
  232. */
  233. #if defined(MODULE) || !defined(CONFIG_SYSFS)
  234. #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
  235. #else
  236. #define MODULE_VERSION(_version) \
  237. MODULE_INFO(version, _version); \
  238. static struct module_version_attribute __modver_attr \
  239. __used __section("__modver") \
  240. __aligned(__alignof__(struct module_version_attribute)) \
  241. = { \
  242. .mattr = { \
  243. .attr = { \
  244. .name = "version", \
  245. .mode = S_IRUGO, \
  246. }, \
  247. .show = __modver_version_show, \
  248. }, \
  249. .module_name = KBUILD_MODNAME, \
  250. .version = _version, \
  251. }
  252. #endif
  253. /* Optional firmware file (or files) needed by the module
  254. * format is simply firmware file name. Multiple firmware
  255. * files require multiple MODULE_FIRMWARE() specifiers */
  256. #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
  257. #define MODULE_IMPORT_NS(ns) MODULE_INFO(import_ns, __stringify(ns))
  258. struct notifier_block;
  259. #ifdef CONFIG_MODULES
  260. extern int modules_disabled; /* for sysctl */
  261. /* Get/put a kernel symbol (calls must be symmetric) */
  262. void *__symbol_get(const char *symbol);
  263. void *__symbol_get_gpl(const char *symbol);
  264. #define symbol_get(x) ((typeof(&x))(__symbol_get(__stringify(x))))
  265. /* modules using other modules: kdb wants to see this. */
  266. struct module_use {
  267. struct list_head source_list;
  268. struct list_head target_list;
  269. struct module *source, *target;
  270. };
  271. enum module_state {
  272. MODULE_STATE_LIVE, /* Normal state. */
  273. MODULE_STATE_COMING, /* Full formed, running module_init. */
  274. MODULE_STATE_GOING, /* Going away. */
  275. MODULE_STATE_UNFORMED, /* Still setting it up. */
  276. };
  277. struct mod_tree_node {
  278. struct module *mod;
  279. struct latch_tree_node node;
  280. };
  281. struct module_layout {
  282. /* The actual code + data. */
  283. void *base;
  284. /* Total size. */
  285. unsigned int size;
  286. /* The size of the executable code. */
  287. unsigned int text_size;
  288. /* Size of RO section of the module (text+rodata) */
  289. unsigned int ro_size;
  290. /* Size of RO after init section */
  291. unsigned int ro_after_init_size;
  292. #ifdef CONFIG_MODULES_TREE_LOOKUP
  293. struct mod_tree_node mtn;
  294. #endif
  295. };
  296. #ifdef CONFIG_MODULES_TREE_LOOKUP
  297. /* Only touch one cacheline for common rbtree-for-core-layout case. */
  298. #define __module_layout_align ____cacheline_aligned
  299. #else
  300. #define __module_layout_align
  301. #endif
  302. struct mod_kallsyms {
  303. Elf_Sym *symtab;
  304. unsigned int num_symtab;
  305. char *strtab;
  306. char *typetab;
  307. };
  308. #ifdef CONFIG_LIVEPATCH
  309. struct klp_modinfo {
  310. Elf_Ehdr hdr;
  311. Elf_Shdr *sechdrs;
  312. char *secstrings;
  313. unsigned int symndx;
  314. };
  315. #endif
  316. struct module {
  317. enum module_state state;
  318. /* Member of list of modules */
  319. struct list_head list;
  320. /* Unique handle for this module */
  321. char name[MODULE_NAME_LEN];
  322. #ifdef CONFIG_STACKTRACE_BUILD_ID
  323. /* Module build ID */
  324. unsigned char build_id[BUILD_ID_SIZE_MAX];
  325. #endif
  326. /* Sysfs stuff. */
  327. struct module_kobject mkobj;
  328. struct module_attribute *modinfo_attrs;
  329. const char *version;
  330. const char *srcversion;
  331. const char *scmversion;
  332. struct kobject *holders_dir;
  333. /* Exported symbols */
  334. const struct kernel_symbol *syms;
  335. const s32 *crcs;
  336. unsigned int num_syms;
  337. #ifdef CONFIG_ARCH_USES_CFI_TRAPS
  338. s32 *kcfi_traps;
  339. s32 *kcfi_traps_end;
  340. #endif
  341. /* Kernel parameters. */
  342. #ifdef CONFIG_SYSFS
  343. struct mutex param_lock;
  344. #endif
  345. struct kernel_param *kp;
  346. unsigned int num_kp;
  347. /* GPL-only exported symbols. */
  348. unsigned int num_gpl_syms;
  349. const struct kernel_symbol *gpl_syms;
  350. const s32 *gpl_crcs;
  351. bool using_gplonly_symbols;
  352. /*
  353. * Signature was verified. Unconditionally compiled in Android to
  354. * preserve ABI compatibility between kernels without module
  355. * signing enabled and signed modules.
  356. */
  357. bool sig_ok;
  358. bool async_probe_requested;
  359. /* Exception table */
  360. unsigned int num_exentries;
  361. struct exception_table_entry *extable;
  362. /* Startup function. */
  363. int (*init)(void);
  364. /* Core layout: rbtree is accessed frequently, so keep together. */
  365. struct module_layout core_layout __module_layout_align;
  366. struct module_layout init_layout;
  367. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  368. struct module_layout data_layout;
  369. #endif
  370. /* Arch-specific module values */
  371. struct mod_arch_specific arch;
  372. unsigned long taints; /* same bits as kernel:taint_flags */
  373. #ifdef CONFIG_GENERIC_BUG
  374. /* Support for BUG */
  375. unsigned num_bugs;
  376. struct list_head bug_list;
  377. struct bug_entry *bug_table;
  378. #endif
  379. #ifdef CONFIG_KALLSYMS
  380. /* Protected by RCU and/or module_mutex: use rcu_dereference() */
  381. struct mod_kallsyms __rcu *kallsyms;
  382. struct mod_kallsyms core_kallsyms;
  383. /* Section attributes */
  384. struct module_sect_attrs *sect_attrs;
  385. /* Notes attributes */
  386. struct module_notes_attrs *notes_attrs;
  387. #endif
  388. /* The command line arguments (may be mangled). People like
  389. keeping pointers to this stuff */
  390. char *args;
  391. #ifdef CONFIG_SMP
  392. /* Per-cpu data. */
  393. void __percpu *percpu;
  394. unsigned int percpu_size;
  395. #endif
  396. void *noinstr_text_start;
  397. unsigned int noinstr_text_size;
  398. #ifdef CONFIG_TRACEPOINTS
  399. unsigned int num_tracepoints;
  400. tracepoint_ptr_t *tracepoints_ptrs;
  401. #endif
  402. #ifdef CONFIG_TREE_SRCU
  403. unsigned int num_srcu_structs;
  404. struct srcu_struct **srcu_struct_ptrs;
  405. #endif
  406. #ifdef CONFIG_BPF_EVENTS
  407. unsigned int num_bpf_raw_events;
  408. struct bpf_raw_event_map *bpf_raw_events;
  409. #endif
  410. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  411. unsigned int btf_data_size;
  412. void *btf_data;
  413. #endif
  414. #ifdef CONFIG_JUMP_LABEL
  415. struct jump_entry *jump_entries;
  416. unsigned int num_jump_entries;
  417. #endif
  418. #ifdef CONFIG_TRACING
  419. unsigned int num_trace_bprintk_fmt;
  420. const char **trace_bprintk_fmt_start;
  421. #endif
  422. #ifdef CONFIG_EVENT_TRACING
  423. struct trace_event_call **trace_events;
  424. unsigned int num_trace_events;
  425. struct trace_eval_map **trace_evals;
  426. unsigned int num_trace_evals;
  427. #endif
  428. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  429. unsigned int num_ftrace_callsites;
  430. unsigned long *ftrace_callsites;
  431. #endif
  432. #ifdef CONFIG_KPROBES
  433. void *kprobes_text_start;
  434. unsigned int kprobes_text_size;
  435. unsigned long *kprobe_blacklist;
  436. unsigned int num_kprobe_blacklist;
  437. #endif
  438. #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
  439. int num_static_call_sites;
  440. struct static_call_site *static_call_sites;
  441. #endif
  442. #if IS_ENABLED(CONFIG_KUNIT)
  443. int num_kunit_suites;
  444. struct kunit_suite **kunit_suites;
  445. #endif
  446. #ifdef CONFIG_LIVEPATCH
  447. bool klp; /* Is this a livepatch module? */
  448. bool klp_alive;
  449. /* Elf information */
  450. struct klp_modinfo *klp_info;
  451. #endif
  452. #ifdef CONFIG_PRINTK_INDEX
  453. unsigned int printk_index_size;
  454. struct pi_entry **printk_index_start;
  455. #endif
  456. #ifdef CONFIG_MODULE_UNLOAD
  457. /* What modules depend on me? */
  458. struct list_head source_list;
  459. /* What modules do I depend on? */
  460. struct list_head target_list;
  461. /* Destruction function. */
  462. void (*exit)(void);
  463. atomic_t refcnt;
  464. #endif
  465. #ifdef CONFIG_CONSTRUCTORS
  466. /* Constructor functions. */
  467. ctor_fn_t *ctors;
  468. unsigned int num_ctors;
  469. #endif
  470. #ifdef CONFIG_FUNCTION_ERROR_INJECTION
  471. struct error_injection_entry *ei_funcs;
  472. unsigned int num_ei_funcs;
  473. #endif
  474. ANDROID_KABI_RESERVE(1);
  475. ANDROID_KABI_RESERVE(2);
  476. ANDROID_KABI_RESERVE(3);
  477. ANDROID_KABI_RESERVE(4);
  478. } ____cacheline_aligned __randomize_layout;
  479. #ifndef MODULE_ARCH_INIT
  480. #define MODULE_ARCH_INIT {}
  481. #endif
  482. #ifndef HAVE_ARCH_KALLSYMS_SYMBOL_VALUE
  483. static inline unsigned long kallsyms_symbol_value(const Elf_Sym *sym)
  484. {
  485. return sym->st_value;
  486. }
  487. #endif
  488. /* FIXME: It'd be nice to isolate modules during init, too, so they
  489. aren't used before they (may) fail. But presently too much code
  490. (IDE & SCSI) require entry into the module during init.*/
  491. static inline bool module_is_live(struct module *mod)
  492. {
  493. return mod->state != MODULE_STATE_GOING;
  494. }
  495. struct module *__module_text_address(unsigned long addr);
  496. struct module *__module_address(unsigned long addr);
  497. bool is_module_address(unsigned long addr);
  498. bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
  499. bool is_module_percpu_address(unsigned long addr);
  500. bool is_module_text_address(unsigned long addr);
  501. static inline bool within_module_core(unsigned long addr,
  502. const struct module *mod)
  503. {
  504. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  505. if ((unsigned long)mod->data_layout.base <= addr &&
  506. addr < (unsigned long)mod->data_layout.base + mod->data_layout.size)
  507. return true;
  508. #endif
  509. return (unsigned long)mod->core_layout.base <= addr &&
  510. addr < (unsigned long)mod->core_layout.base + mod->core_layout.size;
  511. }
  512. static inline bool within_module_init(unsigned long addr,
  513. const struct module *mod)
  514. {
  515. return (unsigned long)mod->init_layout.base <= addr &&
  516. addr < (unsigned long)mod->init_layout.base + mod->init_layout.size;
  517. }
  518. static inline bool within_module(unsigned long addr, const struct module *mod)
  519. {
  520. return within_module_init(addr, mod) || within_module_core(addr, mod);
  521. }
  522. /* Search for module by name: must be in a RCU-sched critical section. */
  523. struct module *find_module(const char *name);
  524. /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if
  525. symnum out of range. */
  526. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  527. char *name, char *module_name, int *exported);
  528. /* Look for this name: can be of form module:name. */
  529. unsigned long module_kallsyms_lookup_name(const char *name);
  530. extern void __noreturn __module_put_and_kthread_exit(struct module *mod,
  531. long code);
  532. #define module_put_and_kthread_exit(code) __module_put_and_kthread_exit(THIS_MODULE, code)
  533. #ifdef CONFIG_MODULE_UNLOAD
  534. int module_refcount(struct module *mod);
  535. void __symbol_put(const char *symbol);
  536. #define symbol_put(x) __symbol_put(__stringify(x))
  537. void symbol_put_addr(void *addr);
  538. /* Sometimes we know we already have a refcount, and it's easier not
  539. to handle the error case (which only happens with rmmod --wait). */
  540. extern void __module_get(struct module *module);
  541. /* This is the Right Way to get a module: if it fails, it's being removed,
  542. * so pretend it's not there. */
  543. extern bool try_module_get(struct module *module);
  544. extern void module_put(struct module *module);
  545. #else /*!CONFIG_MODULE_UNLOAD*/
  546. static inline bool try_module_get(struct module *module)
  547. {
  548. return !module || module_is_live(module);
  549. }
  550. static inline void module_put(struct module *module)
  551. {
  552. }
  553. static inline void __module_get(struct module *module)
  554. {
  555. }
  556. #define symbol_put(x) do { } while (0)
  557. #define symbol_put_addr(p) do { } while (0)
  558. #endif /* CONFIG_MODULE_UNLOAD */
  559. /* This is a #define so the string doesn't get put in every .o file */
  560. #define module_name(mod) \
  561. ({ \
  562. struct module *__mod = (mod); \
  563. __mod ? __mod->name : "kernel"; \
  564. })
  565. /* Dereference module function descriptor */
  566. void *dereference_module_function_descriptor(struct module *mod, void *ptr);
  567. /* For kallsyms to ask for address resolution. namebuf should be at
  568. * least KSYM_NAME_LEN long: a pointer to namebuf is returned if
  569. * found, otherwise NULL. */
  570. const char *module_address_lookup(unsigned long addr,
  571. unsigned long *symbolsize,
  572. unsigned long *offset,
  573. char **modname, const unsigned char **modbuildid,
  574. char *namebuf);
  575. int lookup_module_symbol_name(unsigned long addr, char *symname);
  576. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
  577. int register_module_notifier(struct notifier_block *nb);
  578. int unregister_module_notifier(struct notifier_block *nb);
  579. extern void print_modules(void);
  580. static inline bool module_requested_async_probing(struct module *module)
  581. {
  582. return module && module->async_probe_requested;
  583. }
  584. static inline bool is_livepatch_module(struct module *mod)
  585. {
  586. #ifdef CONFIG_LIVEPATCH
  587. return mod->klp;
  588. #else
  589. return false;
  590. #endif
  591. }
  592. void set_module_sig_enforced(void);
  593. #else /* !CONFIG_MODULES... */
  594. static inline struct module *__module_address(unsigned long addr)
  595. {
  596. return NULL;
  597. }
  598. static inline struct module *__module_text_address(unsigned long addr)
  599. {
  600. return NULL;
  601. }
  602. static inline bool is_module_address(unsigned long addr)
  603. {
  604. return false;
  605. }
  606. static inline bool is_module_percpu_address(unsigned long addr)
  607. {
  608. return false;
  609. }
  610. static inline bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  611. {
  612. return false;
  613. }
  614. static inline bool is_module_text_address(unsigned long addr)
  615. {
  616. return false;
  617. }
  618. static inline bool within_module_core(unsigned long addr,
  619. const struct module *mod)
  620. {
  621. return false;
  622. }
  623. static inline bool within_module_init(unsigned long addr,
  624. const struct module *mod)
  625. {
  626. return false;
  627. }
  628. static inline bool within_module(unsigned long addr, const struct module *mod)
  629. {
  630. return false;
  631. }
  632. /* Get/put a kernel symbol (calls should be symmetric) */
  633. #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak,visibility("hidden"))); &(x); })
  634. #define symbol_put(x) do { } while (0)
  635. #define symbol_put_addr(x) do { } while (0)
  636. static inline void __module_get(struct module *module)
  637. {
  638. }
  639. static inline bool try_module_get(struct module *module)
  640. {
  641. return true;
  642. }
  643. static inline void module_put(struct module *module)
  644. {
  645. }
  646. #define module_name(mod) "kernel"
  647. /* For kallsyms to ask for address resolution. NULL means not found. */
  648. static inline const char *module_address_lookup(unsigned long addr,
  649. unsigned long *symbolsize,
  650. unsigned long *offset,
  651. char **modname,
  652. const unsigned char **modbuildid,
  653. char *namebuf)
  654. {
  655. return NULL;
  656. }
  657. static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
  658. {
  659. return -ERANGE;
  660. }
  661. static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name)
  662. {
  663. return -ERANGE;
  664. }
  665. static inline int module_get_kallsym(unsigned int symnum, unsigned long *value,
  666. char *type, char *name,
  667. char *module_name, int *exported)
  668. {
  669. return -ERANGE;
  670. }
  671. static inline unsigned long module_kallsyms_lookup_name(const char *name)
  672. {
  673. return 0;
  674. }
  675. static inline int register_module_notifier(struct notifier_block *nb)
  676. {
  677. /* no events will happen anyway, so this can always succeed */
  678. return 0;
  679. }
  680. static inline int unregister_module_notifier(struct notifier_block *nb)
  681. {
  682. return 0;
  683. }
  684. #define module_put_and_kthread_exit(code) kthread_exit(code)
  685. static inline void print_modules(void)
  686. {
  687. }
  688. static inline bool module_requested_async_probing(struct module *module)
  689. {
  690. return false;
  691. }
  692. static inline void set_module_sig_enforced(void)
  693. {
  694. }
  695. /* Dereference module function descriptor */
  696. static inline
  697. void *dereference_module_function_descriptor(struct module *mod, void *ptr)
  698. {
  699. return ptr;
  700. }
  701. #endif /* CONFIG_MODULES */
  702. #ifdef CONFIG_SYSFS
  703. extern struct kset *module_kset;
  704. extern struct kobj_type module_ktype;
  705. extern int module_sysfs_initialized;
  706. #endif /* CONFIG_SYSFS */
  707. #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
  708. /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
  709. #define __MODULE_STRING(x) __stringify(x)
  710. #ifdef CONFIG_GENERIC_BUG
  711. void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
  712. struct module *);
  713. void module_bug_cleanup(struct module *);
  714. #else /* !CONFIG_GENERIC_BUG */
  715. static inline void module_bug_finalize(const Elf_Ehdr *hdr,
  716. const Elf_Shdr *sechdrs,
  717. struct module *mod)
  718. {
  719. }
  720. static inline void module_bug_cleanup(struct module *mod) {}
  721. #endif /* CONFIG_GENERIC_BUG */
  722. #ifdef CONFIG_RETPOLINE
  723. extern bool retpoline_module_ok(bool has_retpoline);
  724. #else
  725. static inline bool retpoline_module_ok(bool has_retpoline)
  726. {
  727. return true;
  728. }
  729. #endif
  730. #ifdef CONFIG_MODULE_SIG
  731. bool is_module_sig_enforced(void);
  732. static inline bool module_sig_ok(struct module *module)
  733. {
  734. return module->sig_ok;
  735. }
  736. #else /* !CONFIG_MODULE_SIG */
  737. static inline bool is_module_sig_enforced(void)
  738. {
  739. return false;
  740. }
  741. static inline bool module_sig_ok(struct module *module)
  742. {
  743. return true;
  744. }
  745. #endif /* CONFIG_MODULE_SIG */
  746. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  747. struct module *, unsigned long),
  748. void *data);
  749. #endif /* _LINUX_MODULE_H */