blob: 278e9b6762bb703c1f2c82809e1ad311db900c76 [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020050#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080051#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040052#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040053#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080054#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090055#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#if 0
58#define DEBUGP printk
59#else
60#define DEBUGP(fmt , a...)
61#endif
62
63#ifndef ARCH_SHF_SMALL
64#define ARCH_SHF_SMALL 0
65#endif
66
67/* If this is set, the section belongs in the init part of the module */
68#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
69
Rusty Russell24da1cb2007-07-15 23:41:46 -070070/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020071 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050072DEFINE_MUTEX(module_mutex);
73EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static LIST_HEAD(modules);
75
Stephen Rothwell19e45292009-04-14 17:27:18 +100076/* Block module loading/unloading? */
77int modules_disabled = 0;
78
Rusty Russellc9a3ba52008-01-29 17:13:18 -050079/* Waiting for a module to finish initializing? */
80static DECLARE_WAIT_QUEUE_HEAD(module_wq);
81
Alan Sterne041c682006-03-27 01:16:30 -080082static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Rusty Russelle6104992009-03-31 13:05:31 -060084/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050085static unsigned long module_addr_min = -1UL, module_addr_max = 0;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087int register_module_notifier(struct notifier_block * nb)
88{
Alan Sterne041c682006-03-27 01:16:30 -080089 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91EXPORT_SYMBOL(register_module_notifier);
92
93int unregister_module_notifier(struct notifier_block * nb)
94{
Alan Sterne041c682006-03-27 01:16:30 -080095 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97EXPORT_SYMBOL(unregister_module_notifier);
98
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080099/* We require a truly strong try_module_get(): 0 means failure due to
100 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static inline int strong_try_module_get(struct module *mod)
102{
103 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500104 return -EBUSY;
105 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500107 else
108 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700111static inline void add_taint_module(struct module *mod, unsigned flag)
112{
113 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700114 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700115}
116
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200117/*
118 * A thread that wants to hold a reference to a module only while it
119 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121void __module_put_and_exit(struct module *mod, long code)
122{
123 module_put(mod);
124 do_exit(code);
125}
126EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* Find a module section: 0 means not found. */
129static unsigned int find_sec(Elf_Ehdr *hdr,
130 Elf_Shdr *sechdrs,
131 const char *secstrings,
132 const char *name)
133{
134 unsigned int i;
135
136 for (i = 1; i < hdr->e_shnum; i++)
137 /* Alloc bit cleared means "ignore it." */
138 if ((sechdrs[i].sh_flags & SHF_ALLOC)
139 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
140 return i;
141 return 0;
142}
143
Rusty Russell5e458cc2008-10-22 10:00:13 -0500144/* Find a module section, or NULL. */
145static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
146 const char *secstrings, const char *name)
147{
148 /* Section 0 has sh_addr 0. */
149 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
150}
151
152/* Find a module section, or NULL. Fill in number of "objects" in section. */
153static void *section_objs(Elf_Ehdr *hdr,
154 Elf_Shdr *sechdrs,
155 const char *secstrings,
156 const char *name,
157 size_t object_size,
158 unsigned int *num)
159{
160 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
161
162 /* Section 0 has sh_addr 0 and sh_size 0. */
163 *num = sechdrs[sec].sh_size / object_size;
164 return (void *)sechdrs[sec].sh_addr;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Provided by the linker */
168extern const struct kernel_symbol __start___ksymtab[];
169extern const struct kernel_symbol __stop___ksymtab[];
170extern const struct kernel_symbol __start___ksymtab_gpl[];
171extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800172extern const struct kernel_symbol __start___ksymtab_gpl_future[];
173extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700174extern const struct kernel_symbol __start___ksymtab_gpl_future[];
175extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176extern const unsigned long __start___kcrctab[];
177extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800178extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500179#ifdef CONFIG_UNUSED_SYMBOLS
180extern const struct kernel_symbol __start___ksymtab_unused[];
181extern const struct kernel_symbol __stop___ksymtab_unused[];
182extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
183extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700184extern const unsigned long __start___kcrctab_unused[];
185extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188#ifndef CONFIG_MODVERSIONS
189#define symversion(base, idx) NULL
190#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800191#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
193
Rusty Russelldafd0942008-07-22 19:24:25 -0500194static bool each_symbol_in_section(const struct symsearch *arr,
195 unsigned int arrsize,
196 struct module *owner,
197 bool (*fn)(const struct symsearch *syms,
198 struct module *owner,
199 unsigned int symnum, void *data),
200 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100201{
Rusty Russelldafd0942008-07-22 19:24:25 -0500202 unsigned int i, j;
203
204 for (j = 0; j < arrsize; j++) {
205 for (i = 0; i < arr[j].stop - arr[j].start; i++)
206 if (fn(&arr[j], owner, i, data))
207 return true;
208 }
209
210 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100211}
212
Rusty Russelldafd0942008-07-22 19:24:25 -0500213/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500214bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
215 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700216{
Rusty Russelldafd0942008-07-22 19:24:25 -0500217 struct module *mod;
218 const struct symsearch arr[] = {
219 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
220 NOT_GPL_ONLY, false },
221 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
222 __start___kcrctab_gpl,
223 GPL_ONLY, false },
224 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
225 __start___kcrctab_gpl_future,
226 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500227#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500228 { __start___ksymtab_unused, __stop___ksymtab_unused,
229 __start___kcrctab_unused,
230 NOT_GPL_ONLY, true },
231 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
232 __start___kcrctab_unused_gpl,
233 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500234#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500235 };
236
237 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
238 return true;
239
Andi Kleend72b3752008-08-30 10:09:00 +0200240 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500241 struct symsearch arr[] = {
242 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
243 NOT_GPL_ONLY, false },
244 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
245 mod->gpl_crcs,
246 GPL_ONLY, false },
247 { mod->gpl_future_syms,
248 mod->gpl_future_syms + mod->num_gpl_future_syms,
249 mod->gpl_future_crcs,
250 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500251#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500252 { mod->unused_syms,
253 mod->unused_syms + mod->num_unused_syms,
254 mod->unused_crcs,
255 NOT_GPL_ONLY, true },
256 { mod->unused_gpl_syms,
257 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
258 mod->unused_gpl_crcs,
259 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500260#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500261 };
262
263 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
264 return true;
265 }
266 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700267}
Tim Abbottc6b37802008-12-05 19:03:59 -0500268EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700269
Rusty Russelldafd0942008-07-22 19:24:25 -0500270struct find_symbol_arg {
271 /* Input */
272 const char *name;
273 bool gplok;
274 bool warn;
275
276 /* Output */
277 struct module *owner;
278 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500279 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500280};
281
282static bool find_symbol_in_section(const struct symsearch *syms,
283 struct module *owner,
284 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500285{
Rusty Russelldafd0942008-07-22 19:24:25 -0500286 struct find_symbol_arg *fsa = data;
287
288 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
289 return false;
290
291 if (!fsa->gplok) {
292 if (syms->licence == GPL_ONLY)
293 return false;
294 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
295 printk(KERN_WARNING "Symbol %s is being used "
296 "by a non-GPL module, which will not "
297 "be allowed in the future\n", fsa->name);
298 printk(KERN_WARNING "Please see the file "
299 "Documentation/feature-removal-schedule.txt "
300 "in the kernel source tree for more details.\n");
301 }
302 }
303
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500304#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500305 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500306 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500307 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500308 printk(KERN_WARNING
309 "This symbol will go away in the future.\n");
310 printk(KERN_WARNING
311 "Please evalute if this is the right api to use and if "
312 "it really is, submit a report the linux kernel "
313 "mailinglist together with submitting your code for "
314 "inclusion.\n");
315 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500316#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500317
318 fsa->owner = owner;
319 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500320 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500321 return true;
322}
323
Tim Abbott414fd312008-12-05 19:03:56 -0500324/* Find a symbol and return it, along with, (optional) crc and
325 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500326const struct kernel_symbol *find_symbol(const char *name,
327 struct module **owner,
328 const unsigned long **crc,
329 bool gplok,
330 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Rusty Russelldafd0942008-07-22 19:24:25 -0500332 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Rusty Russelldafd0942008-07-22 19:24:25 -0500334 fsa.name = name;
335 fsa.gplok = gplok;
336 fsa.warn = warn;
337
338 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500339 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500340 *owner = fsa.owner;
341 if (crc)
342 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500343 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500347 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Tim Abbottc6b37802008-12-05 19:03:59 -0500349EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500352struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct module *mod;
355
356 list_for_each_entry(mod, &modules, list) {
357 if (strcmp(mod->name, name) == 0)
358 return mod;
359 }
360 return NULL;
361}
Tim Abbottc6b37802008-12-05 19:03:59 -0500362EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900365
366#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
367
368static void *percpu_modalloc(unsigned long size, unsigned long align,
369 const char *name)
370{
371 void *ptr;
372
373 if (align > PAGE_SIZE) {
374 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
375 name, align, PAGE_SIZE);
376 align = PAGE_SIZE;
377 }
378
Tejun Heoedcb4632009-03-06 14:33:59 +0900379 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900380 if (!ptr)
381 printk(KERN_WARNING
382 "Could not allocate %lu bytes percpu data\n", size);
383 return ptr;
384}
385
386static void percpu_modfree(void *freeme)
387{
388 free_percpu(freeme);
389}
390
391#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/* Number of blocks used and allocated. */
394static unsigned int pcpu_num_used, pcpu_num_allocated;
395/* Size of each block. -ve means used. */
396static int *pcpu_size;
397
398static int split_block(unsigned int i, unsigned short size)
399{
400 /* Reallocation required? */
401 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700402 int *new;
403
404 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
405 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!new)
407 return 0;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pcpu_size = new;
411 }
412
413 /* Insert a new subblock */
414 memmove(&pcpu_size[i+1], &pcpu_size[i],
415 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
416 pcpu_num_used++;
417
418 pcpu_size[i+1] -= size;
419 pcpu_size[i] = size;
420 return 1;
421}
422
423static inline unsigned int block_size(int val)
424{
425 if (val < 0)
426 return -val;
427 return val;
428}
429
Rusty Russell842bbaa2005-08-01 21:11:47 -0700430static void *percpu_modalloc(unsigned long size, unsigned long align,
431 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 unsigned long extra;
434 unsigned int i;
435 void *ptr;
436
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200437 if (align > PAGE_SIZE) {
438 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
439 name, align, PAGE_SIZE);
440 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 ptr = __per_cpu_start;
444 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
445 /* Extra for alignment requirement. */
446 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
447 BUG_ON(i == 0 && extra != 0);
448
449 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
450 continue;
451
452 /* Transfer extra to previous block. */
453 if (pcpu_size[i-1] < 0)
454 pcpu_size[i-1] -= extra;
455 else
456 pcpu_size[i-1] += extra;
457 pcpu_size[i] -= extra;
458 ptr += extra;
459
460 /* Split block if warranted */
461 if (pcpu_size[i] - size > sizeof(unsigned long))
462 if (!split_block(i, size))
463 return NULL;
464
465 /* Mark allocated */
466 pcpu_size[i] = -pcpu_size[i];
467 return ptr;
468 }
469
470 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
471 size);
472 return NULL;
473}
474
475static void percpu_modfree(void *freeme)
476{
477 unsigned int i;
478 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
479
480 /* First entry is core kernel percpu data. */
481 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
482 if (ptr == freeme) {
483 pcpu_size[i] = -pcpu_size[i];
484 goto free;
485 }
486 }
487 BUG();
488
489 free:
490 /* Merge with previous? */
491 if (pcpu_size[i-1] >= 0) {
492 pcpu_size[i-1] += pcpu_size[i];
493 pcpu_num_used--;
494 memmove(&pcpu_size[i], &pcpu_size[i+1],
495 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
496 i--;
497 }
498 /* Merge with next? */
499 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
500 pcpu_size[i] += pcpu_size[i+1];
501 pcpu_num_used--;
502 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
503 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
504 }
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static int percpu_modinit(void)
508{
509 pcpu_num_used = 2;
510 pcpu_num_allocated = 2;
511 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
512 GFP_KERNEL);
513 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +0200514 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Free room. */
516 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
517 if (pcpu_size[1] < 0) {
518 printk(KERN_ERR "No per-cpu room for modules.\n");
519 pcpu_num_used = 1;
520 }
521
522 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900525
Tejun Heofbf59bc2009-02-20 16:29:08 +0900526#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
527
Tejun Heo6b588c12009-02-20 16:29:07 +0900528static unsigned int find_pcpusec(Elf_Ehdr *hdr,
529 Elf_Shdr *sechdrs,
530 const char *secstrings)
531{
532 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
533}
534
535static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
536{
537 int cpu;
538
539 for_each_possible_cpu(cpu)
540 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900544
Rusty Russell842bbaa2005-08-01 21:11:47 -0700545static inline void *percpu_modalloc(unsigned long size, unsigned long align,
546 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 return NULL;
549}
550static inline void percpu_modfree(void *pcpuptr)
551{
552 BUG();
553}
554static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
555 Elf_Shdr *sechdrs,
556 const char *secstrings)
557{
558 return 0;
559}
560static inline void percpu_modcopy(void *pcpudst, const void *src,
561 unsigned long size)
562{
563 /* pcpusec should be 0, and size of that section should be 0. */
564 BUG_ON(size != 0);
565}
Tejun Heo6b588c12009-02-20 16:29:07 +0900566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#endif /* CONFIG_SMP */
568
Matt Domschc988d2b2005-06-23 22:05:15 -0700569#define MODINFO_ATTR(field) \
570static void setup_modinfo_##field(struct module *mod, const char *s) \
571{ \
572 mod->field = kstrdup(s, GFP_KERNEL); \
573} \
574static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
575 struct module *mod, char *buffer) \
576{ \
577 return sprintf(buffer, "%s\n", mod->field); \
578} \
579static int modinfo_##field##_exists(struct module *mod) \
580{ \
581 return mod->field != NULL; \
582} \
583static void free_modinfo_##field(struct module *mod) \
584{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700585 kfree(mod->field); \
586 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700587} \
588static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900589 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700590 .show = show_modinfo_##field, \
591 .setup = setup_modinfo_##field, \
592 .test = modinfo_##field##_exists, \
593 .free = free_modinfo_##field, \
594};
595
596MODINFO_ATTR(version);
597MODINFO_ATTR(srcversion);
598
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100599static char last_unloaded_module[MODULE_NAME_LEN+1];
600
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800601#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/* Init the unload section of the module. */
603static void module_unload_init(struct module *mod)
604{
Eric Dumazet720eba32009-02-03 13:31:36 +1030605 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030608 for_each_possible_cpu(cpu)
609 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030611 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Backwards compatibility macros put refcount during init. */
613 mod->waiter = current;
614}
615
616/* modules using other modules */
617struct module_use
618{
619 struct list_head list;
620 struct module *module_which_uses;
621};
622
623/* Does a already use b? */
624static int already_uses(struct module *a, struct module *b)
625{
626 struct module_use *use;
627
628 list_for_each_entry(use, &b->modules_which_use_me, list) {
629 if (use->module_which_uses == a) {
630 DEBUGP("%s uses %s!\n", a->name, b->name);
631 return 1;
632 }
633 }
634 DEBUGP("%s does not use %s!\n", a->name, b->name);
635 return 0;
636}
637
638/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500639int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500642 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (b == NULL || already_uses(a, b)) return 1;
645
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500646 /* If we're interrupted or time out, we fail. */
647 if (wait_event_interruptible_timeout(
648 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
649 30 * HZ) <= 0) {
650 printk("%s: gave up waiting for init of module %s.\n",
651 a->name, b->name);
652 return 0;
653 }
654
655 /* If strong_try_module_get() returned a different error, we fail. */
656 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658
659 DEBUGP("Allocating new usage for %s.\n", a->name);
660 use = kmalloc(sizeof(*use), GFP_ATOMIC);
661 if (!use) {
662 printk("%s: out of memory loading\n", a->name);
663 module_put(b);
664 return 0;
665 }
666
667 use->module_which_uses = a;
668 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100669 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 1;
671}
Tim Abbottc6b37802008-12-05 19:03:59 -0500672EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674/* Clear the unload stuff of the module. */
675static void module_unload_free(struct module *mod)
676{
677 struct module *i;
678
679 list_for_each_entry(i, &modules, list) {
680 struct module_use *use;
681
682 list_for_each_entry(use, &i->modules_which_use_me, list) {
683 if (use->module_which_uses == mod) {
684 DEBUGP("%s unusing %s\n", mod->name, i->name);
685 module_put(i);
686 list_del(&use->list);
687 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100688 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* There can be at most one match. */
690 break;
691 }
692 }
693 }
694}
695
696#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800697static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 int ret = (flags & O_TRUNC);
700 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800701 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return ret;
703}
704#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800705static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 return 0;
708}
709#endif /* CONFIG_MODULE_FORCE_UNLOAD */
710
711struct stopref
712{
713 struct module *mod;
714 int flags;
715 int *forced;
716};
717
718/* Whole machine is stopped with interrupts off when this runs. */
719static int __try_stop_module(void *_sref)
720{
721 struct stopref *sref = _sref;
722
Rusty Russellda39ba52008-07-22 19:24:25 -0500723 /* If it's not unused, quit unless we're forcing. */
724 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800725 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EWOULDBLOCK;
727 }
728
729 /* Mark it as dying. */
730 sref->mod->state = MODULE_STATE_GOING;
731 return 0;
732}
733
734static int try_stop_module(struct module *mod, int flags, int *forced)
735{
Rusty Russellda39ba52008-07-22 19:24:25 -0500736 if (flags & O_NONBLOCK) {
737 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500739 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500740 } else {
741 /* We don't need to stop the machine for this. */
742 mod->state = MODULE_STATE_GOING;
743 synchronize_sched();
744 return 0;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748unsigned int module_refcount(struct module *mod)
749{
Eric Dumazet720eba32009-02-03 13:31:36 +1030750 unsigned int total = 0;
751 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Eric Dumazet720eba32009-02-03 13:31:36 +1030753 for_each_possible_cpu(cpu)
754 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return total;
756}
757EXPORT_SYMBOL(module_refcount);
758
759/* This exists whether we can unload or not */
760static void free_module(struct module *mod);
761
762static void wait_for_zero_refcount(struct module *mod)
763{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500764 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800765 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 for (;;) {
767 DEBUGP("Looking at refcount...\n");
768 set_current_state(TASK_UNINTERRUPTIBLE);
769 if (module_refcount(mod) == 0)
770 break;
771 schedule();
772 }
773 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800774 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100777SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
778 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800781 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int ret, forced = 0;
783
Kees Cook3d433212009-04-02 15:49:29 -0700784 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800785 return -EPERM;
786
787 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
788 return -EFAULT;
789 name[MODULE_NAME_LEN-1] = '\0';
790
Heiko Carstens9e018922008-12-22 12:36:31 +0100791 /* Create stop_machine threads since free_module relies on
792 * a non-failing stop_machine call. */
793 ret = stop_machine_create();
794 if (ret)
795 return ret;
796
797 if (mutex_lock_interruptible(&module_mutex) != 0) {
798 ret = -EINTR;
799 goto out_stop;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 mod = find_module(name);
803 if (!mod) {
804 ret = -ENOENT;
805 goto out;
806 }
807
808 if (!list_empty(&mod->modules_which_use_me)) {
809 /* Other modules depend on us: get rid of them first. */
810 ret = -EWOULDBLOCK;
811 goto out;
812 }
813
814 /* Doing init or already dying? */
815 if (mod->state != MODULE_STATE_LIVE) {
816 /* FIXME: if (force), slam module count and wake up
817 waiter --RR */
818 DEBUGP("%s already dying\n", mod->name);
819 ret = -EBUSY;
820 goto out;
821 }
822
823 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700824 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800825 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!forced) {
827 /* This module can't be removed */
828 ret = -EBUSY;
829 goto out;
830 }
831 }
832
833 /* Set this up before setting mod->state */
834 mod->waiter = current;
835
836 /* Stop the machine so refcounts can't move and disable module. */
837 ret = try_stop_module(mod, flags, &forced);
838 if (ret != 0)
839 goto out;
840
841 /* Never wait if forced. */
842 if (!forced && module_refcount(mod) != 0)
843 wait_for_zero_refcount(mod);
844
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200845 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200847 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200849 blocking_notifier_call_chain(&module_notify_list,
850 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800851 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200852 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100853 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500854 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500855 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 free_module(mod);
857
858 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800859 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100860out_stop:
861 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return ret;
863}
864
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800865static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct module_use *use;
868 int printed_something = 0;
869
870 seq_printf(m, " %u ", module_refcount(mod));
871
872 /* Always include a trailing , so userspace can differentiate
873 between this and the old multi-field proc format. */
874 list_for_each_entry(use, &mod->modules_which_use_me, list) {
875 printed_something = 1;
876 seq_printf(m, "%s,", use->module_which_uses->name);
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (mod->init != NULL && mod->exit == NULL) {
880 printed_something = 1;
881 seq_printf(m, "[permanent],");
882 }
883
884 if (!printed_something)
885 seq_printf(m, "-");
886}
887
888void __symbol_put(const char *symbol)
889{
890 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Rusty Russell24da1cb2007-07-15 23:41:46 -0700892 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500893 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 BUG();
895 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700896 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898EXPORT_SYMBOL(__symbol_put);
899
900void symbol_put_addr(void *addr)
901{
Trent Piepho5e376612006-05-15 09:44:06 -0700902 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Trent Piepho5e376612006-05-15 09:44:06 -0700904 if (core_kernel_text((unsigned long)addr))
905 return;
906
Rusty Russella6e6abd2009-03-31 13:05:31 -0600907 /* module_text_address is safe here: we're supposed to have reference
908 * to module from symbol_get, so it can't go away. */
909 modaddr = __module_text_address((unsigned long)addr);
910 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700911 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913EXPORT_SYMBOL_GPL(symbol_put_addr);
914
915static ssize_t show_refcnt(struct module_attribute *mattr,
916 struct module *mod, char *buffer)
917{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400918 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900922 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 .show = show_refcnt,
924};
925
Al Virof6a57032006-10-18 01:47:25 -0400926void module_put(struct module *module)
927{
928 if (module) {
929 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030930 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400931 /* Maybe they're waiting for us to drop reference? */
932 if (unlikely(!module_is_live(module)))
933 wake_up_process(module->waiter);
934 put_cpu();
935 }
936}
937EXPORT_SYMBOL(module_put);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800940static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 /* We don't know the usage count, or what modules are using. */
943 seq_printf(m, " - -");
944}
945
946static inline void module_unload_free(struct module *mod)
947{
948}
949
Tim Abbottc6b37802008-12-05 19:03:59 -0500950int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500952 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
Tim Abbottc6b37802008-12-05 19:03:59 -0500954EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956static inline void module_unload_init(struct module *mod)
957{
958}
959#endif /* CONFIG_MODULE_UNLOAD */
960
Kay Sievers1f717402006-11-24 12:15:25 +0100961static ssize_t show_initstate(struct module_attribute *mattr,
962 struct module *mod, char *buffer)
963{
964 const char *state = "unknown";
965
966 switch (mod->state) {
967 case MODULE_STATE_LIVE:
968 state = "live";
969 break;
970 case MODULE_STATE_COMING:
971 state = "coming";
972 break;
973 case MODULE_STATE_GOING:
974 state = "going";
975 break;
976 }
977 return sprintf(buffer, "%s\n", state);
978}
979
980static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900981 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100982 .show = show_initstate,
983};
984
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800985static struct module_attribute *modinfo_attrs[] = {
986 &modinfo_version,
987 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100988 &initstate,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800989#ifdef CONFIG_MODULE_UNLOAD
990 &refcnt,
991#endif
992 NULL,
993};
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static const char vermagic[] = VERMAGIC_STRING;
996
Rusty Russellc6e665c2009-03-31 13:05:33 -0600997static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700998{
999#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001000 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001001 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1002 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001003 add_taint_module(mod, TAINT_FORCED_MODULE);
1004 return 0;
1005#else
1006 return -ENOEXEC;
1007#endif
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#ifdef CONFIG_MODVERSIONS
1011static int check_version(Elf_Shdr *sechdrs,
1012 unsigned int versindex,
1013 const char *symname,
1014 struct module *mod,
1015 const unsigned long *crc)
1016{
1017 unsigned int i, num_versions;
1018 struct modversion_info *versions;
1019
1020 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1021 if (!crc)
1022 return 1;
1023
Rusty Russella5dd6972008-05-09 16:24:21 +10001024 /* No versions at all? modprobe --force does this. */
1025 if (versindex == 0)
1026 return try_to_force_load(mod, symname) == 0;
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 versions = (void *) sechdrs[versindex].sh_addr;
1029 num_versions = sechdrs[versindex].sh_size
1030 / sizeof(struct modversion_info);
1031
1032 for (i = 0; i < num_versions; i++) {
1033 if (strcmp(versions[i].name, symname) != 0)
1034 continue;
1035
1036 if (versions[i].crc == *crc)
1037 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 DEBUGP("Found checksum %lX vs module %lX\n",
1039 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001040 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001042
Rusty Russella5dd6972008-05-09 16:24:21 +10001043 printk(KERN_WARNING "%s: no symbol version for %s\n",
1044 mod->name, symname);
1045 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001046
1047bad_version:
1048 printk("%s: disagrees about version of symbol %s\n",
1049 mod->name, symname);
1050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1054 unsigned int versindex,
1055 struct module *mod)
1056{
1057 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Rusty Russell8c8ef422009-03-31 13:05:34 -06001059 if (!find_symbol("module_layout", NULL, &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 BUG();
Rusty Russell8c8ef422009-03-31 13:05:34 -06001061 return check_version(sechdrs, versindex, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Rusty Russell91e37a72008-05-09 16:25:28 +10001064/* First part is kernel version, which we ignore if module has crcs. */
1065static inline int same_magic(const char *amagic, const char *bmagic,
1066 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Rusty Russell91e37a72008-05-09 16:25:28 +10001068 if (has_crcs) {
1069 amagic += strcspn(amagic, " ");
1070 bmagic += strcspn(bmagic, " ");
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return strcmp(amagic, bmagic) == 0;
1073}
1074#else
1075static inline int check_version(Elf_Shdr *sechdrs,
1076 unsigned int versindex,
1077 const char *symname,
1078 struct module *mod,
1079 const unsigned long *crc)
1080{
1081 return 1;
1082}
1083
1084static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1085 unsigned int versindex,
1086 struct module *mod)
1087{
1088 return 1;
1089}
1090
Rusty Russell91e37a72008-05-09 16:25:28 +10001091static inline int same_magic(const char *amagic, const char *bmagic,
1092 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 return strcmp(amagic, bmagic) == 0;
1095}
1096#endif /* CONFIG_MODVERSIONS */
1097
1098/* Resolve a symbol for this module. I.e. if we find one, record usage.
1099 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001100static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1101 unsigned int versindex,
1102 const char *name,
1103 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001106 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 const unsigned long *crc;
1108
Tim Abbott414fd312008-12-05 19:03:56 -05001109 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001110 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001111 /* use_module can fail due to OOM,
1112 or module initialization or unloading */
1113 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1115 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001116 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Tim Abbott414fd312008-12-05 19:03:56 -05001118 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/*
1122 * /sys/module/foo/sections stuff
1123 * J. Corbet <corbet@lwn.net>
1124 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001125#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001126struct module_sect_attr
1127{
1128 struct module_attribute mattr;
1129 char *name;
1130 unsigned long address;
1131};
1132
1133struct module_sect_attrs
1134{
1135 struct attribute_group grp;
1136 unsigned int nsections;
1137 struct module_sect_attr attrs[0];
1138};
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140static ssize_t module_sect_show(struct module_attribute *mattr,
1141 struct module *mod, char *buf)
1142{
1143 struct module_sect_attr *sattr =
1144 container_of(mattr, struct module_sect_attr, mattr);
1145 return sprintf(buf, "0x%lx\n", sattr->address);
1146}
1147
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001148static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1149{
Rusty Russella58730c2008-03-13 09:03:44 +00001150 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001151
1152 for (section = 0; section < sect_attrs->nsections; section++)
1153 kfree(sect_attrs->attrs[section].name);
1154 kfree(sect_attrs);
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static void add_sect_attrs(struct module *mod, unsigned int nsect,
1158 char *secstrings, Elf_Shdr *sechdrs)
1159{
1160 unsigned int nloaded = 0, i, size[2];
1161 struct module_sect_attrs *sect_attrs;
1162 struct module_sect_attr *sattr;
1163 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* Count loaded sections and allocate structures */
1166 for (i = 0; i < nsect; i++)
1167 if (sechdrs[i].sh_flags & SHF_ALLOC)
1168 nloaded++;
1169 size[0] = ALIGN(sizeof(*sect_attrs)
1170 + nloaded * sizeof(sect_attrs->attrs[0]),
1171 sizeof(sect_attrs->grp.attrs[0]));
1172 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001173 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1174 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return;
1176
1177 /* Setup section attributes. */
1178 sect_attrs->grp.name = "sections";
1179 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1180
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001181 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 sattr = &sect_attrs->attrs[0];
1183 gattr = &sect_attrs->grp.attrs[0];
1184 for (i = 0; i < nsect; i++) {
1185 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1186 continue;
1187 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001188 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1189 GFP_KERNEL);
1190 if (sattr->name == NULL)
1191 goto out;
1192 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 sattr->mattr.show = module_sect_show;
1194 sattr->mattr.store = NULL;
1195 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sattr->mattr.attr.mode = S_IRUGO;
1197 *(gattr++) = &(sattr++)->mattr.attr;
1198 }
1199 *gattr = NULL;
1200
1201 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1202 goto out;
1203
1204 mod->sect_attrs = sect_attrs;
1205 return;
1206 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001207 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static void remove_sect_attrs(struct module *mod)
1211{
1212 if (mod->sect_attrs) {
1213 sysfs_remove_group(&mod->mkobj.kobj,
1214 &mod->sect_attrs->grp);
1215 /* We are positive that no one is using any sect attrs
1216 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001217 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 mod->sect_attrs = NULL;
1219 }
1220}
1221
Roland McGrath6d760132007-10-16 23:26:40 -07001222/*
1223 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1224 */
1225
1226struct module_notes_attrs {
1227 struct kobject *dir;
1228 unsigned int notes;
1229 struct bin_attribute attrs[0];
1230};
1231
1232static ssize_t module_notes_read(struct kobject *kobj,
1233 struct bin_attribute *bin_attr,
1234 char *buf, loff_t pos, size_t count)
1235{
1236 /*
1237 * The caller checked the pos and count against our size.
1238 */
1239 memcpy(buf, bin_attr->private + pos, count);
1240 return count;
1241}
1242
1243static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1244 unsigned int i)
1245{
1246 if (notes_attrs->dir) {
1247 while (i-- > 0)
1248 sysfs_remove_bin_file(notes_attrs->dir,
1249 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001250 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001251 }
1252 kfree(notes_attrs);
1253}
1254
1255static void add_notes_attrs(struct module *mod, unsigned int nsect,
1256 char *secstrings, Elf_Shdr *sechdrs)
1257{
1258 unsigned int notes, loaded, i;
1259 struct module_notes_attrs *notes_attrs;
1260 struct bin_attribute *nattr;
1261
1262 /* Count notes sections and allocate structures. */
1263 notes = 0;
1264 for (i = 0; i < nsect; i++)
1265 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1266 (sechdrs[i].sh_type == SHT_NOTE))
1267 ++notes;
1268
1269 if (notes == 0)
1270 return;
1271
1272 notes_attrs = kzalloc(sizeof(*notes_attrs)
1273 + notes * sizeof(notes_attrs->attrs[0]),
1274 GFP_KERNEL);
1275 if (notes_attrs == NULL)
1276 return;
1277
1278 notes_attrs->notes = notes;
1279 nattr = &notes_attrs->attrs[0];
1280 for (loaded = i = 0; i < nsect; ++i) {
1281 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1282 continue;
1283 if (sechdrs[i].sh_type == SHT_NOTE) {
1284 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1285 nattr->attr.mode = S_IRUGO;
1286 nattr->size = sechdrs[i].sh_size;
1287 nattr->private = (void *) sechdrs[i].sh_addr;
1288 nattr->read = module_notes_read;
1289 ++nattr;
1290 }
1291 ++loaded;
1292 }
1293
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001294 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001295 if (!notes_attrs->dir)
1296 goto out;
1297
1298 for (i = 0; i < notes; ++i)
1299 if (sysfs_create_bin_file(notes_attrs->dir,
1300 &notes_attrs->attrs[i]))
1301 goto out;
1302
1303 mod->notes_attrs = notes_attrs;
1304 return;
1305
1306 out:
1307 free_notes_attrs(notes_attrs, i);
1308}
1309
1310static void remove_notes_attrs(struct module *mod)
1311{
1312 if (mod->notes_attrs)
1313 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1319 char *sectstrings, Elf_Shdr *sechdrs)
1320{
1321}
1322
1323static inline void remove_sect_attrs(struct module *mod)
1324{
1325}
Roland McGrath6d760132007-10-16 23:26:40 -07001326
1327static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1328 char *sectstrings, Elf_Shdr *sechdrs)
1329{
1330}
1331
1332static inline void remove_notes_attrs(struct module *mod)
1333{
1334}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Randy Dunlapef665c12007-02-13 15:19:06 -08001337#ifdef CONFIG_SYSFS
1338int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001339{
1340 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001341 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001342 int error = 0;
1343 int i;
1344
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001345 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1346 (ARRAY_SIZE(modinfo_attrs) + 1)),
1347 GFP_KERNEL);
1348 if (!mod->modinfo_attrs)
1349 return -ENOMEM;
1350
1351 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001352 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1353 if (!attr->test ||
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001354 (attr->test && attr->test(mod))) {
1355 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001356 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1357 ++temp_attr;
1358 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001359 }
1360 return error;
1361}
1362
Randy Dunlapef665c12007-02-13 15:19:06 -08001363void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001364{
1365 struct module_attribute *attr;
1366 int i;
1367
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001368 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1369 /* pick a field to test for end of list */
1370 if (!attr->attr.name)
1371 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001372 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001373 if (attr->free)
1374 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001375 }
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001376 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Randy Dunlapef665c12007-02-13 15:19:06 -08001379int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001382 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001384 if (!module_sysfs_initialized) {
1385 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001386 mod->name);
1387 err = -EINVAL;
1388 goto out;
1389 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001390
1391 kobj = kset_find_obj(module_kset, mod->name);
1392 if (kobj) {
1393 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1394 kobject_put(kobj);
1395 err = -EINVAL;
1396 goto out;
1397 }
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001400
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001401 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1402 mod->mkobj.kobj.kset = module_kset;
1403 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1404 "%s", mod->name);
1405 if (err)
1406 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001407
Kay Sievers97c146e2007-11-29 23:46:11 +01001408 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001409out:
1410 return err;
1411}
1412
Randy Dunlapef665c12007-02-13 15:19:06 -08001413int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001414 struct kernel_param *kparam,
1415 unsigned int num_params)
1416{
1417 int err;
1418
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001419 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001420 if (!mod->holders_dir) {
1421 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001422 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001423 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 err = module_param_sysfs_setup(mod, kparam, num_params);
1426 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001427 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Matt Domschc988d2b2005-06-23 22:05:15 -07001429 err = module_add_modinfo_attrs(mod);
1430 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001431 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001432
Kay Sieverse17e0f52006-11-24 12:15:25 +01001433 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435
Kay Sieverse17e0f52006-11-24 12:15:25 +01001436out_unreg_param:
1437 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001438out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001439 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001440out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001441 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return err;
1443}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001444
1445static void mod_sysfs_fini(struct module *mod)
1446{
1447 kobject_put(&mod->mkobj.kobj);
1448}
1449
1450#else /* CONFIG_SYSFS */
1451
1452static void mod_sysfs_fini(struct module *mod)
1453{
1454}
1455
1456#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458static void mod_kobject_remove(struct module *mod)
1459{
Matt Domschc988d2b2005-06-23 22:05:15 -07001460 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001462 kobject_put(mod->mkobj.drivers_dir);
1463 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001464 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467/*
1468 * unlink the module with the whole machine is stopped with interrupts off
1469 * - this defends against kallsyms not taking locks
1470 */
1471static int __unlink_module(void *_mod)
1472{
1473 struct module *mod = _mod;
1474 list_del(&mod->list);
1475 return 0;
1476}
1477
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001478/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479static void free_module(struct module *mod)
1480{
1481 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001482 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001483 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 remove_sect_attrs(mod);
1485 mod_kobject_remove(mod);
1486
1487 /* Arch-specific cleanup. */
1488 module_arch_cleanup(mod);
1489
1490 /* Module unload stuff */
1491 module_unload_free(mod);
1492
Rusty Russelle180a6b2009-03-31 13:05:29 -06001493 /* Free any allocated parameters. */
1494 destroy_params(mod->kp, mod->num_kp);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* This may be NULL, but that's OK */
1497 module_free(mod, mod->module_init);
1498 kfree(mod->args);
1499 if (mod->percpu)
1500 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301501#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1502 if (mod->refptr)
1503 percpu_modfree(mod->refptr);
1504#endif
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001505 /* Free lock-classes: */
1506 lockdep_free_key_range(mod->module_core, mod->core_size);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* Finally, free the core (containing the module structure) */
1509 module_free(mod, mod->module_core);
1510}
1511
1512void *__symbol_get(const char *symbol)
1513{
1514 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001515 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Rusty Russell24da1cb2007-07-15 23:41:46 -07001517 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001518 sym = find_symbol(symbol, &owner, NULL, true, true);
1519 if (sym && strong_try_module_get(owner))
1520 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001521 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Tim Abbott414fd312008-12-05 19:03:56 -05001523 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525EXPORT_SYMBOL_GPL(__symbol_get);
1526
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001527/*
1528 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001529 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001530 */
1531static int verify_export_symbols(struct module *mod)
1532{
Rusty Russellb2111042008-05-01 21:15:00 -05001533 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001534 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001535 const struct kernel_symbol *s;
1536 struct {
1537 const struct kernel_symbol *sym;
1538 unsigned int num;
1539 } arr[] = {
1540 { mod->syms, mod->num_syms },
1541 { mod->gpl_syms, mod->num_gpl_syms },
1542 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001543#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001544 { mod->unused_syms, mod->num_unused_syms },
1545 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001546#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001547 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001548
Rusty Russellb2111042008-05-01 21:15:00 -05001549 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1550 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001551 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001552 printk(KERN_ERR
1553 "%s: exports duplicate symbol %s"
1554 " (owned by %s)\n",
1555 mod->name, s->name, module_name(owner));
1556 return -ENOEXEC;
1557 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001558 }
Rusty Russellb2111042008-05-01 21:15:00 -05001559 }
1560 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001561}
1562
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001563/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static int simplify_symbols(Elf_Shdr *sechdrs,
1565 unsigned int symindex,
1566 const char *strtab,
1567 unsigned int versindex,
1568 unsigned int pcpuindex,
1569 struct module *mod)
1570{
1571 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1572 unsigned long secbase;
1573 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1574 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001575 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 for (i = 1; i < n; i++) {
1578 switch (sym[i].st_shndx) {
1579 case SHN_COMMON:
1580 /* We compiled with -fno-common. These are not
1581 supposed to happen. */
1582 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1583 printk("%s: please compile with -fno-common\n",
1584 mod->name);
1585 ret = -ENOEXEC;
1586 break;
1587
1588 case SHN_ABS:
1589 /* Don't need to do anything */
1590 DEBUGP("Absolute symbol: 0x%08lx\n",
1591 (long)sym[i].st_value);
1592 break;
1593
1594 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001595 ksym = resolve_symbol(sechdrs, versindex,
1596 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001598 if (ksym) {
1599 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /* Ok if weak. */
1604 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1605 break;
1606
1607 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1608 mod->name, strtab + sym[i].st_name);
1609 ret = -ENOENT;
1610 break;
1611
1612 default:
1613 /* Divert to percpu allocation if a percpu var. */
1614 if (sym[i].st_shndx == pcpuindex)
1615 secbase = (unsigned long)mod->percpu;
1616 else
1617 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1618 sym[i].st_value += secbase;
1619 break;
1620 }
1621 }
1622
1623 return ret;
1624}
1625
Helge Deller088af9a2008-12-31 12:31:18 +01001626/* Additional bytes needed by arch in front of individual sections */
1627unsigned int __weak arch_mod_section_prepend(struct module *mod,
1628 unsigned int section)
1629{
1630 /* default implementation just returns zero */
1631 return 0;
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001635static long get_offset(struct module *mod, unsigned int *size,
1636 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 long ret;
1639
Helge Deller088af9a2008-12-31 12:31:18 +01001640 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1642 *size = ret + sechdr->sh_size;
1643 return ret;
1644}
1645
1646/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1647 might -- code, read-only data, read-write data, small data. Tally
1648 sizes, and place the offsets into sh_entsize fields: high bit means it
1649 belongs in init. */
1650static void layout_sections(struct module *mod,
1651 const Elf_Ehdr *hdr,
1652 Elf_Shdr *sechdrs,
1653 const char *secstrings)
1654{
1655 static unsigned long const masks[][2] = {
1656 /* NOTE: all executable code must be the first section
1657 * in this array; otherwise modify the text_size
1658 * finder in the two loops below */
1659 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1660 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1661 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1662 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1663 };
1664 unsigned int m, i;
1665
1666 for (i = 0; i < hdr->e_shnum; i++)
1667 sechdrs[i].sh_entsize = ~0UL;
1668
1669 DEBUGP("Core section allocation order:\n");
1670 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1671 for (i = 0; i < hdr->e_shnum; ++i) {
1672 Elf_Shdr *s = &sechdrs[i];
1673
1674 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1675 || (s->sh_flags & masks[m][1])
1676 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001677 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001679 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 DEBUGP("\t%s\n", secstrings + s->sh_name);
1681 }
1682 if (m == 0)
1683 mod->core_text_size = mod->core_size;
1684 }
1685
1686 DEBUGP("Init section allocation order:\n");
1687 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1688 for (i = 0; i < hdr->e_shnum; ++i) {
1689 Elf_Shdr *s = &sechdrs[i];
1690
1691 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1692 || (s->sh_flags & masks[m][1])
1693 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001694 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001696 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 | INIT_OFFSET_MASK);
1698 DEBUGP("\t%s\n", secstrings + s->sh_name);
1699 }
1700 if (m == 0)
1701 mod->init_text_size = mod->init_size;
1702 }
1703}
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705static void set_license(struct module *mod, const char *license)
1706{
1707 if (!license)
1708 license = "unspecified";
1709
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001710 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001711 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001712 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001713 "kernel.\n", mod->name, license);
1714 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716}
1717
1718/* Parse tag=value strings from .modinfo section */
1719static char *next_string(char *string, unsigned long *secsize)
1720{
1721 /* Skip non-zero chars */
1722 while (string[0]) {
1723 string++;
1724 if ((*secsize)-- <= 1)
1725 return NULL;
1726 }
1727
1728 /* Skip any zero padding. */
1729 while (!string[0]) {
1730 string++;
1731 if ((*secsize)-- <= 1)
1732 return NULL;
1733 }
1734 return string;
1735}
1736
1737static char *get_modinfo(Elf_Shdr *sechdrs,
1738 unsigned int info,
1739 const char *tag)
1740{
1741 char *p;
1742 unsigned int taglen = strlen(tag);
1743 unsigned long size = sechdrs[info].sh_size;
1744
1745 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1746 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1747 return p + taglen + 1;
1748 }
1749 return NULL;
1750}
1751
Matt Domschc988d2b2005-06-23 22:05:15 -07001752static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1753 unsigned int infoindex)
1754{
1755 struct module_attribute *attr;
1756 int i;
1757
1758 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1759 if (attr->setup)
1760 attr->setup(mod,
1761 get_modinfo(sechdrs,
1762 infoindex,
1763 attr->attr.name));
1764 }
1765}
Matt Domschc988d2b2005-06-23 22:05:15 -07001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001768
1769/* lookup symbol in given range of kernel_symbols */
1770static const struct kernel_symbol *lookup_symbol(const char *name,
1771 const struct kernel_symbol *start,
1772 const struct kernel_symbol *stop)
1773{
1774 const struct kernel_symbol *ks = start;
1775 for (; ks < stop; ks++)
1776 if (strcmp(ks->name, name) == 0)
1777 return ks;
1778 return NULL;
1779}
1780
Tim Abbottca4787b2009-01-05 08:40:10 -06001781static int is_exported(const char *name, unsigned long value,
1782 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Tim Abbottca4787b2009-01-05 08:40:10 -06001784 const struct kernel_symbol *ks;
1785 if (!mod)
1786 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001787 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001788 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1789 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/* As per nm */
1793static char elf_type(const Elf_Sym *sym,
1794 Elf_Shdr *sechdrs,
1795 const char *secstrings,
1796 struct module *mod)
1797{
1798 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1799 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1800 return 'v';
1801 else
1802 return 'w';
1803 }
1804 if (sym->st_shndx == SHN_UNDEF)
1805 return 'U';
1806 if (sym->st_shndx == SHN_ABS)
1807 return 'a';
1808 if (sym->st_shndx >= SHN_LORESERVE)
1809 return '?';
1810 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1811 return 't';
1812 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1813 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1814 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1815 return 'r';
1816 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1817 return 'g';
1818 else
1819 return 'd';
1820 }
1821 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1822 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1823 return 's';
1824 else
1825 return 'b';
1826 }
Rusty Russell49502672009-03-31 13:05:36 -06001827 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 'n';
1829 return '?';
1830}
1831
1832static void add_kallsyms(struct module *mod,
1833 Elf_Shdr *sechdrs,
1834 unsigned int symindex,
1835 unsigned int strindex,
1836 const char *secstrings)
1837{
1838 unsigned int i;
1839
1840 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1841 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1842 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1843
1844 /* Set types up while we still have access to sections. */
1845 for (i = 0; i < mod->num_symtab; i++)
1846 mod->symtab[i].st_info
1847 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1848}
1849#else
1850static inline void add_kallsyms(struct module *mod,
1851 Elf_Shdr *sechdrs,
1852 unsigned int symindex,
1853 unsigned int strindex,
1854 const char *secstrings)
1855{
1856}
1857#endif /* CONFIG_KALLSYMS */
1858
Jason Barone9d376f2009-02-05 11:51:38 -05001859static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001860{
Jason Barone9d376f2009-02-05 11:51:38 -05001861#ifdef CONFIG_DYNAMIC_DEBUG
1862 if (ddebug_add_module(debug, num, debug->modname))
1863 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1864 debug->modname);
1865#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001866}
Jason Baron346e15b2008-08-12 16:46:19 -04001867
Rusty Russell3a642e92008-07-22 19:24:28 -05001868static void *module_alloc_update_bounds(unsigned long size)
1869{
1870 void *ret = module_alloc(size);
1871
1872 if (ret) {
1873 /* Update module bounds. */
1874 if ((unsigned long)ret < module_addr_min)
1875 module_addr_min = (unsigned long)ret;
1876 if ((unsigned long)ret + size > module_addr_max)
1877 module_addr_max = (unsigned long)ret + size;
1878 }
1879 return ret;
1880}
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882/* Allocate and load the module: note that size of section 0 is always
1883 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001884static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned long len,
1886 const char __user *uargs)
1887{
1888 Elf_Ehdr *hdr;
1889 Elf_Shdr *sechdrs;
1890 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001891 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001892 unsigned int i;
1893 unsigned int symindex = 0;
1894 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001895 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct module *mod;
1897 long err = 0;
1898 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Thomas Koeller378bac82005-09-06 15:17:11 -07001899 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1902 umod, len, uargs);
1903 if (len < sizeof(*hdr))
1904 return ERR_PTR(-ENOEXEC);
1905
1906 /* Suck in entire file: we'll want most of it. */
1907 /* vmalloc barfs on "unusual" numbers. Check here */
1908 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1909 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (copy_from_user(hdr, umod, len) != 0) {
1912 err = -EFAULT;
1913 goto free_hdr;
1914 }
1915
1916 /* Sanity checks against insmoding binaries or wrong arch,
1917 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001918 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 || hdr->e_type != ET_REL
1920 || !elf_check_arch(hdr)
1921 || hdr->e_shentsize != sizeof(*sechdrs)) {
1922 err = -ENOEXEC;
1923 goto free_hdr;
1924 }
1925
1926 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1927 goto truncated;
1928
1929 /* Convenience variables */
1930 sechdrs = (void *)hdr + hdr->e_shoff;
1931 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1932 sechdrs[0].sh_addr = 0;
1933
1934 for (i = 1; i < hdr->e_shnum; i++) {
1935 if (sechdrs[i].sh_type != SHT_NOBITS
1936 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1937 goto truncated;
1938
1939 /* Mark all sections sh_addr with their address in the
1940 temporary image. */
1941 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1942
1943 /* Internal symbols and strings. */
1944 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1945 symindex = i;
1946 strindex = sechdrs[i].sh_link;
1947 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1948 }
1949#ifndef CONFIG_MODULE_UNLOAD
1950 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06001951 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1953#endif
1954 }
1955
1956 modindex = find_sec(hdr, sechdrs, secstrings,
1957 ".gnu.linkonce.this_module");
1958 if (!modindex) {
1959 printk(KERN_WARNING "No module found in object\n");
1960 err = -ENOEXEC;
1961 goto free_hdr;
1962 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001963 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 mod = (void *)sechdrs[modindex].sh_addr;
1965
1966 if (symindex == 0) {
1967 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1968 mod->name);
1969 err = -ENOEXEC;
1970 goto free_hdr;
1971 }
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1974 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1975 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1976
Rusty Russellea01e792008-03-13 09:02:17 +00001977 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001979 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#ifdef CONFIG_KALLSYMS
1981 /* Keep symbol and string tables for decoding later. */
1982 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1983 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1984#endif
1985
1986 /* Check module struct version now, before we try to use module. */
1987 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1988 err = -ENOEXEC;
1989 goto free_hdr;
1990 }
1991
1992 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1993 /* This is allowed: modprobe --force will invalidate it. */
1994 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06001995 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07001996 if (err)
1997 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001998 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2000 mod->name, modmagic, vermagic);
2001 err = -ENOEXEC;
2002 goto free_hdr;
2003 }
2004
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002005 staging = get_modinfo(sechdrs, infoindex, "staging");
2006 if (staging) {
2007 add_taint_module(mod, TAINT_CRAP);
2008 printk(KERN_WARNING "%s: module is from the staging directory,"
2009 " the quality is unknown, you have been warned.\n",
2010 mod->name);
2011 }
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002014 args = strndup_user(uargs, ~0UL >> 1);
2015 if (IS_ERR(args)) {
2016 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto free_hdr;
2018 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (find_module(mod->name)) {
2021 err = -EEXIST;
2022 goto free_mod;
2023 }
2024
2025 mod->state = MODULE_STATE_COMING;
2026
2027 /* Allow arches to frob section contents and sizes. */
2028 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2029 if (err < 0)
2030 goto free_mod;
2031
2032 if (pcpuindex) {
2033 /* We have a special allocation for this section. */
2034 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002035 sechdrs[pcpuindex].sh_addralign,
2036 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (!percpu) {
2038 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002039 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2042 mod->percpu = percpu;
2043 }
2044
2045 /* Determine total sizes, and put offsets in sh_entsize. For now
2046 this is done generically; there doesn't appear to be any
2047 special cases for the architectures. */
2048 layout_sections(mod, hdr, sechdrs, secstrings);
2049
2050 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002051 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (!ptr) {
2053 err = -ENOMEM;
2054 goto free_percpu;
2055 }
2056 memset(ptr, 0, mod->core_size);
2057 mod->module_core = ptr;
2058
Rusty Russell3a642e92008-07-22 19:24:28 -05002059 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (!ptr && mod->init_size) {
2061 err = -ENOMEM;
2062 goto free_core;
2063 }
2064 memset(ptr, 0, mod->init_size);
2065 mod->module_init = ptr;
2066
2067 /* Transfer each section which specifies SHF_ALLOC */
2068 DEBUGP("final section addresses:\n");
2069 for (i = 0; i < hdr->e_shnum; i++) {
2070 void *dest;
2071
2072 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2073 continue;
2074
2075 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2076 dest = mod->module_init
2077 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2078 else
2079 dest = mod->module_core + sechdrs[i].sh_entsize;
2080
2081 if (sechdrs[i].sh_type != SHT_NOBITS)
2082 memcpy(dest, (void *)sechdrs[i].sh_addr,
2083 sechdrs[i].sh_size);
2084 /* Update sh_addr to point to copy in image. */
2085 sechdrs[i].sh_addr = (unsigned long)dest;
2086 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2087 }
2088 /* Module has been moved. */
2089 mod = (void *)sechdrs[modindex].sh_addr;
2090
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002091#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2092 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2093 mod->name);
2094 if (!mod->refptr) {
2095 err = -ENOMEM;
2096 goto free_init;
2097 }
2098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Now we've moved module, initialize linked lists, etc. */
2100 module_unload_init(mod);
2101
Kay Sievers97c146e2007-11-29 23:46:11 +01002102 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002103 err = mod_sysfs_init(mod);
2104 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002105 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /* Set up license info based on the info section */
2108 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2109
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002110 /*
2111 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2112 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2113 * using GPL-only symbols it needs.
2114 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002115 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002116 add_taint(TAINT_PROPRIETARY_MODULE);
2117
2118 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002119 if (strcmp(mod->name, "driverloader") == 0)
2120 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d612006-01-08 01:03:41 -08002121
Matt Domschc988d2b2005-06-23 22:05:15 -07002122 /* Set up MODINFO_ATTR fields */
2123 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /* Fix up syms, so that st_value is a pointer to location. */
2126 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2127 mod);
2128 if (err < 0)
2129 goto cleanup;
2130
Rusty Russell5e458cc2008-10-22 10:00:13 -05002131 /* Now we've got everything in the final locations, we can
2132 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002133 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2134 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002135 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2136 sizeof(*mod->syms), &mod->num_syms);
2137 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2138 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2139 sizeof(*mod->gpl_syms),
2140 &mod->num_gpl_syms);
2141 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2142 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2143 "__ksymtab_gpl_future",
2144 sizeof(*mod->gpl_future_syms),
2145 &mod->num_gpl_future_syms);
2146 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2147 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002149#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002150 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2151 "__ksymtab_unused",
2152 sizeof(*mod->unused_syms),
2153 &mod->num_unused_syms);
2154 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2155 "__kcrctab_unused");
2156 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2157 "__ksymtab_unused_gpl",
2158 sizeof(*mod->unused_gpl_syms),
2159 &mod->num_unused_gpl_syms);
2160 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2161 "__kcrctab_unused_gpl");
2162#endif
2163
2164#ifdef CONFIG_MARKERS
2165 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2166 sizeof(*mod->markers), &mod->num_markers);
2167#endif
2168#ifdef CONFIG_TRACEPOINTS
2169 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2170 "__tracepoints",
2171 sizeof(*mod->tracepoints),
2172 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002173#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002174#ifdef CONFIG_EVENT_TRACING
2175 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2176 "_ftrace_events",
2177 sizeof(*mod->trace_events),
2178 &mod->num_trace_events);
2179#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002180#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2181 /* sechdrs[0].sh_size is always zero */
2182 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2183 "__mcount_loc",
2184 sizeof(*mod->ftrace_callsites),
2185 &mod->num_ftrace_callsites);
2186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002188 if ((mod->num_syms && !mod->crcs)
2189 || (mod->num_gpl_syms && !mod->gpl_crcs)
2190 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002191#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002192 || (mod->num_unused_syms && !mod->unused_crcs)
2193 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002194#endif
2195 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002196 err = try_to_force_load(mod,
2197 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002198 if (err)
2199 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201#endif
2202
2203 /* Now do relocations. */
2204 for (i = 1; i < hdr->e_shnum; i++) {
2205 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2206 unsigned int info = sechdrs[i].sh_info;
2207
2208 /* Not a valid relocation section? */
2209 if (info >= hdr->e_shnum)
2210 continue;
2211
2212 /* Don't bother with non-allocated sections */
2213 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2214 continue;
2215
2216 if (sechdrs[i].sh_type == SHT_REL)
2217 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2218 else if (sechdrs[i].sh_type == SHT_RELA)
2219 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2220 mod);
2221 if (err < 0)
2222 goto cleanup;
2223 }
2224
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002225 /* Find duplicate symbols */
2226 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002227 if (err < 0)
2228 goto cleanup;
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002231 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2232 sizeof(*mod->extable), &mod->num_exentries);
2233 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 /* Finally, copy percpu area over. */
2236 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2237 sechdrs[pcpuindex].sh_size);
2238
2239 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2240
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002241 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002242 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002243 unsigned int num_debug;
2244
Rusty Russell5e458cc2008-10-22 10:00:13 -05002245 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2246 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002247 if (debug)
2248 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002249 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 err = module_finalize(hdr, sechdrs, mod);
2252 if (err < 0)
2253 goto cleanup;
2254
Thomas Koeller378bac82005-09-06 15:17:11 -07002255 /* flush the icache in correct context */
2256 old_fs = get_fs();
2257 set_fs(KERNEL_DS);
2258
2259 /*
2260 * Flush the instruction cache, since we've played with text.
2261 * Do it before processing of module parameters, so the module
2262 * can provide parameter accessor functions of its own.
2263 */
2264 if (mod->module_init)
2265 flush_icache_range((unsigned long)mod->module_init,
2266 (unsigned long)mod->module_init
2267 + mod->init_size);
2268 flush_icache_range((unsigned long)mod->module_core,
2269 (unsigned long)mod->module_core + mod->core_size);
2270
2271 set_fs(old_fs);
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002274 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002275 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2276 mod->name);
2277
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002278 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002279 * info during argument parsing. Noone should access us, since
2280 * strong_try_module_get() will fail.
2281 * lockdep/oops can run asynchronous, so use the RCU list insertion
2282 * function to insert in a way safe to concurrent readers.
2283 * The mutex protects against concurrent writers.
2284 */
2285 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002286
Rusty Russelle180a6b2009-03-31 13:05:29 -06002287 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002289 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Rusty Russelle180a6b2009-03-31 13:05:29 -06002291 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002293 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002295 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 /* Get rid of temporary copy */
2298 vfree(hdr);
2299
2300 /* Done! */
2301 return mod;
2302
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002303 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002304 /* Unlink carefully: kallsyms could be walking list. */
2305 list_del_rcu(&mod->list);
2306 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 module_arch_cleanup(mod);
2308 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002309 kobject_del(&mod->mkobj.kobj);
2310 kobject_put(&mod->mkobj.kobj);
2311 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302313#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002314 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302315 percpu_modfree(mod->refptr);
2316#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002317 module_free(mod, mod->module_init);
2318 free_core:
2319 module_free(mod, mod->module_core);
2320 /* mod will be freed with core. Don't access it beyond this line! */
2321 free_percpu:
2322 if (percpu)
2323 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 free_mod:
2325 kfree(args);
2326 free_hdr:
2327 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002328 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 truncated:
2331 printk(KERN_ERR "Module len %lu truncated\n", len);
2332 err = -ENOEXEC;
2333 goto free_hdr;
2334}
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002337SYSCALL_DEFINE3(init_module, void __user *, umod,
2338 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 struct module *mod;
2341 int ret = 0;
2342
2343 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002344 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return -EPERM;
2346
2347 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002348 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return -EINTR;
2350
2351 /* Do all the hard work */
2352 mod = load_module(umod, len, uargs);
2353 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002354 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return PTR_ERR(mod);
2356 }
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002359 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Alan Sterne041c682006-03-27 01:16:30 -08002361 blocking_notifier_call_chain(&module_notify_list,
2362 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 /* Start the module */
2365 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002366 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (ret < 0) {
2368 /* Init routine failed: abort. Try to protect us from
2369 buggy refcounters. */
2370 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002371 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002372 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002373 blocking_notifier_call_chain(&module_notify_list,
2374 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002375 mutex_lock(&module_mutex);
2376 free_module(mod);
2377 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002378 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return ret;
2380 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002381 if (ret > 0) {
2382 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2383 "it should follow 0/-E convention\n"
2384 KERN_WARNING "%s: loading module anyway...\n",
2385 __func__, mod->name, ret,
2386 __func__);
2387 dump_stack();
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Rusty Russell6c5db222008-03-10 11:43:52 -07002390 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002392 wake_up(&module_wq);
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08002393 blocking_notifier_call_chain(&module_notify_list,
2394 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002395
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002396 /* We need to finish all async code before the module init sequence is done */
2397 async_synchronize_full();
2398
Rusty Russell6c5db222008-03-10 11:43:52 -07002399 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* Drop initial reference. */
2401 module_put(mod);
2402 module_free(mod, mod->module_init);
2403 mod->module_init = NULL;
2404 mod->init_size = 0;
2405 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002406 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 return 0;
2409}
2410
2411static inline int within(unsigned long addr, void *start, unsigned long size)
2412{
2413 return ((void *)addr >= start && (void *)addr < start + size);
2414}
2415
2416#ifdef CONFIG_KALLSYMS
2417/*
2418 * This ignores the intensely annoying "mapping symbols" found
2419 * in ARM ELF files: $a, $t and $d.
2420 */
2421static inline int is_arm_mapping_symbol(const char *str)
2422{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002423 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 && (str[2] == '\0' || str[2] == '.');
2425}
2426
2427static const char *get_ksymbol(struct module *mod,
2428 unsigned long addr,
2429 unsigned long *size,
2430 unsigned long *offset)
2431{
2432 unsigned int i, best = 0;
2433 unsigned long nextval;
2434
2435 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002436 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002438 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2440
2441 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002442 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 for (i = 1; i < mod->num_symtab; i++) {
2444 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2445 continue;
2446
2447 /* We ignore unnamed symbols: they're uninformative
2448 * and inserted at a whim. */
2449 if (mod->symtab[i].st_value <= addr
2450 && mod->symtab[i].st_value > mod->symtab[best].st_value
2451 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2452 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2453 best = i;
2454 if (mod->symtab[i].st_value > addr
2455 && mod->symtab[i].st_value < nextval
2456 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2457 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2458 nextval = mod->symtab[i].st_value;
2459 }
2460
2461 if (!best)
2462 return NULL;
2463
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002464 if (size)
2465 *size = nextval - mod->symtab[best].st_value;
2466 if (offset)
2467 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return mod->strtab + mod->symtab[best].st_name;
2469}
2470
Rusty Russell6dd06c92008-01-29 17:13:22 -05002471/* For kallsyms to ask for address resolution. NULL means not found. Careful
2472 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002473const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002474 unsigned long *size,
2475 unsigned long *offset,
2476 char **modname,
2477 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002480 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Rusty Russellcb2a5202008-01-14 00:55:03 -08002482 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002483 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002484 if (within_module_init(addr, mod) ||
2485 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002486 if (modname)
2487 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002488 ret = get_ksymbol(mod, addr, size, offset);
2489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002492 /* Make a copy in here where it's safe */
2493 if (ret) {
2494 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2495 ret = namebuf;
2496 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002497 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002498 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002501int lookup_module_symbol_name(unsigned long addr, char *symname)
2502{
2503 struct module *mod;
2504
Rusty Russellcb2a5202008-01-14 00:55:03 -08002505 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002506 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002507 if (within_module_init(addr, mod) ||
2508 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002509 const char *sym;
2510
2511 sym = get_ksymbol(mod, addr, NULL, NULL);
2512 if (!sym)
2513 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002514 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002515 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002516 return 0;
2517 }
2518 }
2519out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002520 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002521 return -ERANGE;
2522}
2523
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002524int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2525 unsigned long *offset, char *modname, char *name)
2526{
2527 struct module *mod;
2528
Rusty Russellcb2a5202008-01-14 00:55:03 -08002529 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002530 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002531 if (within_module_init(addr, mod) ||
2532 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002533 const char *sym;
2534
2535 sym = get_ksymbol(mod, addr, size, offset);
2536 if (!sym)
2537 goto out;
2538 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002539 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002540 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002541 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002542 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002543 return 0;
2544 }
2545 }
2546out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002547 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002548 return -ERANGE;
2549}
2550
Alexey Dobriyanea078902007-05-08 00:28:39 -07002551int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2552 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 struct module *mod;
2555
Rusty Russellcb2a5202008-01-14 00:55:03 -08002556 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002557 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (symnum < mod->num_symtab) {
2559 *value = mod->symtab[symnum].st_value;
2560 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002561 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002562 KSYM_NAME_LEN);
2563 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002564 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002565 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568 symnum -= mod->num_symtab;
2569 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002570 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002571 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574static unsigned long mod_find_symname(struct module *mod, const char *name)
2575{
2576 unsigned int i;
2577
2578 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002579 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2580 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return mod->symtab[i].st_value;
2582 return 0;
2583}
2584
2585/* Look for this name: can be of form module:name. */
2586unsigned long module_kallsyms_lookup_name(const char *name)
2587{
2588 struct module *mod;
2589 char *colon;
2590 unsigned long ret = 0;
2591
2592 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002593 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if ((colon = strchr(name, ':')) != NULL) {
2595 *colon = '\0';
2596 if ((mod = find_module(name)) != NULL)
2597 ret = mod_find_symname(mod, colon+1);
2598 *colon = ':';
2599 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002600 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if ((ret = mod_find_symname(mod, name)) != 0)
2602 break;
2603 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002604 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return ret;
2606}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002607
2608int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2609 struct module *, unsigned long),
2610 void *data)
2611{
2612 struct module *mod;
2613 unsigned int i;
2614 int ret;
2615
2616 list_for_each_entry(mod, &modules, list) {
2617 for (i = 0; i < mod->num_symtab; i++) {
2618 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2619 mod, mod->symtab[i].st_value);
2620 if (ret != 0)
2621 return ret;
2622 }
2623 }
2624 return 0;
2625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626#endif /* CONFIG_KALLSYMS */
2627
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002628static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002629{
2630 int bx = 0;
2631
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002632 if (mod->taints ||
2633 mod->state == MODULE_STATE_GOING ||
2634 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002635 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002636 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002637 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002638 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002639 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002640 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002641 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002642 /*
2643 * TAINT_FORCED_RMMOD: could be added.
2644 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2645 * apply to modules.
2646 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002647
2648 /* Show a - for module-is-being-unloaded */
2649 if (mod->state == MODULE_STATE_GOING)
2650 buf[bx++] = '-';
2651 /* Show a + for module-is-being-loaded */
2652 if (mod->state == MODULE_STATE_COMING)
2653 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002654 buf[bx++] = ')';
2655 }
2656 buf[bx] = '\0';
2657
2658 return buf;
2659}
2660
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002661#ifdef CONFIG_PROC_FS
2662/* Called by the /proc file system to return a list of modules. */
2663static void *m_start(struct seq_file *m, loff_t *pos)
2664{
2665 mutex_lock(&module_mutex);
2666 return seq_list_start(&modules, *pos);
2667}
2668
2669static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2670{
2671 return seq_list_next(p, &modules, pos);
2672}
2673
2674static void m_stop(struct seq_file *m, void *p)
2675{
2676 mutex_unlock(&module_mutex);
2677}
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679static int m_show(struct seq_file *m, void *p)
2680{
2681 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002682 char buf[8];
2683
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002684 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 mod->name, mod->init_size + mod->core_size);
2686 print_unload_info(m, mod);
2687
2688 /* Informative for users. */
2689 seq_printf(m, " %s",
2690 mod->state == MODULE_STATE_GOING ? "Unloading":
2691 mod->state == MODULE_STATE_COMING ? "Loading":
2692 "Live");
2693 /* Used by oprofile and other similar tools. */
2694 seq_printf(m, " 0x%p", mod->module_core);
2695
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002696 /* Taints info */
2697 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002698 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 seq_printf(m, "\n");
2701 return 0;
2702}
2703
2704/* Format: modulename size refcount deps address
2705
2706 Where refcount is a number or -, and deps is a comma-separated list
2707 of depends or -.
2708*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002709static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 .start = m_start,
2711 .next = m_next,
2712 .stop = m_stop,
2713 .show = m_show
2714};
2715
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002716static int modules_open(struct inode *inode, struct file *file)
2717{
2718 return seq_open(file, &modules_op);
2719}
2720
2721static const struct file_operations proc_modules_operations = {
2722 .open = modules_open,
2723 .read = seq_read,
2724 .llseek = seq_lseek,
2725 .release = seq_release,
2726};
2727
2728static int __init proc_modules_init(void)
2729{
2730 proc_create("modules", 0, NULL, &proc_modules_operations);
2731 return 0;
2732}
2733module_init(proc_modules_init);
2734#endif
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736/* Given an address, look for it in the module exception tables. */
2737const struct exception_table_entry *search_module_extables(unsigned long addr)
2738{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 const struct exception_table_entry *e = NULL;
2740 struct module *mod;
2741
Rusty Russell24da1cb2007-07-15 23:41:46 -07002742 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002743 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (mod->num_exentries == 0)
2745 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 e = search_extable(mod->extable,
2748 mod->extable + mod->num_exentries - 1,
2749 addr);
2750 if (e)
2751 break;
2752 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002753 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002756 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return e;
2758}
2759
Ingo Molnar4d435f92006-07-03 00:24:24 -07002760/*
Rusty Russelle6104992009-03-31 13:05:31 -06002761 * is_module_address - is this address inside a module?
2762 * @addr: the address to check.
2763 *
2764 * See is_module_text_address() if you simply want to see if the address
2765 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002766 */
Rusty Russelle6104992009-03-31 13:05:31 -06002767bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002768{
Rusty Russelle6104992009-03-31 13:05:31 -06002769 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002770
Rusty Russell24da1cb2007-07-15 23:41:46 -07002771 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002772 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002773 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002774
Rusty Russelle6104992009-03-31 13:05:31 -06002775 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002776}
2777
Rusty Russelle6104992009-03-31 13:05:31 -06002778/*
2779 * __module_address - get the module which contains an address.
2780 * @addr: the address.
2781 *
2782 * Must be called with preempt disabled or module mutex held so that
2783 * module doesn't get freed during this.
2784 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002785struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 struct module *mod;
2788
Rusty Russell3a642e92008-07-22 19:24:28 -05002789 if (addr < module_addr_min || addr > module_addr_max)
2790 return NULL;
2791
Andi Kleend72b3752008-08-30 10:09:00 +02002792 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002793 if (within_module_core(addr, mod)
2794 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return mod;
2796 return NULL;
2797}
Tim Abbottc6b37802008-12-05 19:03:59 -05002798EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Rusty Russelle6104992009-03-31 13:05:31 -06002800/*
2801 * is_module_text_address - is this address inside module code?
2802 * @addr: the address to check.
2803 *
2804 * See is_module_address() if you simply want to see if the address is
2805 * anywhere in a module. See kernel_text_address() for testing if an
2806 * address corresponds to kernel or module code.
2807 */
2808bool is_module_text_address(unsigned long addr)
2809{
2810 bool ret;
2811
2812 preempt_disable();
2813 ret = __module_text_address(addr) != NULL;
2814 preempt_enable();
2815
2816 return ret;
2817}
2818
2819/*
2820 * __module_text_address - get the module whose code contains an address.
2821 * @addr: the address.
2822 *
2823 * Must be called with preempt disabled or module mutex held so that
2824 * module doesn't get freed during this.
2825 */
2826struct module *__module_text_address(unsigned long addr)
2827{
2828 struct module *mod = __module_address(addr);
2829 if (mod) {
2830 /* Make sure it's within the text section. */
2831 if (!within(addr, mod->module_init, mod->init_text_size)
2832 && !within(addr, mod->module_core, mod->core_text_size))
2833 mod = NULL;
2834 }
2835 return mod;
2836}
Tim Abbottc6b37802008-12-05 19:03:59 -05002837EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839/* Don't grab lock, we're oopsing. */
2840void print_modules(void)
2841{
2842 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002843 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002846 /* Most callers should already have preempt disabled, but make sure */
2847 preempt_disable();
2848 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002849 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002850 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002851 if (last_unloaded_module[0])
2852 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 printk("\n");
2854}
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06002857/* Generate the signature for all relevant module structures here.
2858 * If these change, we don't want to try to parse the module. */
2859void module_layout(struct module *mod,
2860 struct modversion_info *ver,
2861 struct kernel_param *kp,
2862 struct kernel_symbol *ks,
2863 struct marker *marker,
2864 struct tracepoint *tp)
2865{
2866}
2867EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002869
2870#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002871void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002872{
2873 struct module *mod;
2874
2875 mutex_lock(&module_mutex);
2876 list_for_each_entry(mod, &modules, list)
2877 if (!mod->taints)
2878 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002879 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002880 mutex_unlock(&module_mutex);
2881}
2882#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002883
2884#ifdef CONFIG_TRACEPOINTS
2885void module_update_tracepoints(void)
2886{
2887 struct module *mod;
2888
2889 mutex_lock(&module_mutex);
2890 list_for_each_entry(mod, &modules, list)
2891 if (!mod->taints)
2892 tracepoint_update_probe_range(mod->tracepoints,
2893 mod->tracepoints + mod->num_tracepoints);
2894 mutex_unlock(&module_mutex);
2895}
2896
2897/*
2898 * Returns 0 if current not found.
2899 * Returns 1 if current found.
2900 */
2901int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2902{
2903 struct module *iter_mod;
2904 int found = 0;
2905
2906 mutex_lock(&module_mutex);
2907 list_for_each_entry(iter_mod, &modules, list) {
2908 if (!iter_mod->taints) {
2909 /*
2910 * Sorted module list
2911 */
2912 if (iter_mod < iter->module)
2913 continue;
2914 else if (iter_mod > iter->module)
2915 iter->tracepoint = NULL;
2916 found = tracepoint_get_iter_range(&iter->tracepoint,
2917 iter_mod->tracepoints,
2918 iter_mod->tracepoints
2919 + iter_mod->num_tracepoints);
2920 if (found) {
2921 iter->module = iter_mod;
2922 break;
2923 }
2924 }
2925 }
2926 mutex_unlock(&module_mutex);
2927 return found;
2928}
2929#endif