blob: 91fe6958b6e1b253f98c2ab08518693df00a1497 [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>
21#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070022#include <linux/kallsyms.h>
Roland McGrath6d760132007-10-16 23:26:40 -070023#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070024#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/elf.h>
28#include <linux/seq_file.h>
29#include <linux/syscalls.h>
30#include <linux/fcntl.h>
31#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/cpu.h>
34#include <linux/moduleparam.h>
35#include <linux/errno.h>
36#include <linux/err.h>
37#include <linux/vermagic.h>
38#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040039#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/stop_machine.h>
41#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070042#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080043#include <linux/mutex.h>
Jan Beulich4552d5d2006-06-26 13:57:28 +020044#include <linux/unwind.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46#include <asm/semaphore.h>
47#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020048#include <linux/license.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070050extern int module_sysfs_initialized;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#if 0
53#define DEBUGP printk
54#else
55#define DEBUGP(fmt , a...)
56#endif
57
58#ifndef ARCH_SHF_SMALL
59#define ARCH_SHF_SMALL 0
60#endif
61
62/* If this is set, the section belongs in the init part of the module */
63#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
64
Rusty Russell24da1cb2007-07-15 23:41:46 -070065/* List of modules, protected by module_mutex or preempt_disable
66 * (add/delete uses stop_machine). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080067static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static LIST_HEAD(modules);
69
Alan Sterne041c682006-03-27 01:16:30 -080070static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72int register_module_notifier(struct notifier_block * nb)
73{
Alan Sterne041c682006-03-27 01:16:30 -080074 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76EXPORT_SYMBOL(register_module_notifier);
77
78int unregister_module_notifier(struct notifier_block * nb)
79{
Alan Sterne041c682006-03-27 01:16:30 -080080 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82EXPORT_SYMBOL(unregister_module_notifier);
83
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080084/* We require a truly strong try_module_get(): 0 means failure due to
85 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static inline int strong_try_module_get(struct module *mod)
87{
88 if (mod && mod->state == MODULE_STATE_COMING)
89 return 0;
90 return try_module_get(mod);
91}
92
Florin Malitafa3ba2e82006-10-11 01:21:48 -070093static inline void add_taint_module(struct module *mod, unsigned flag)
94{
95 add_taint(flag);
96 mod->taints |= flag;
97}
98
Robert P. J. Day02a3e592007-05-09 07:26:28 +020099/*
100 * A thread that wants to hold a reference to a module only while it
101 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103void __module_put_and_exit(struct module *mod, long code)
104{
105 module_put(mod);
106 do_exit(code);
107}
108EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Find a module section: 0 means not found. */
111static unsigned int find_sec(Elf_Ehdr *hdr,
112 Elf_Shdr *sechdrs,
113 const char *secstrings,
114 const char *name)
115{
116 unsigned int i;
117
118 for (i = 1; i < hdr->e_shnum; i++)
119 /* Alloc bit cleared means "ignore it." */
120 if ((sechdrs[i].sh_flags & SHF_ALLOC)
121 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
122 return i;
123 return 0;
124}
125
126/* Provided by the linker */
127extern const struct kernel_symbol __start___ksymtab[];
128extern const struct kernel_symbol __stop___ksymtab[];
129extern const struct kernel_symbol __start___ksymtab_gpl[];
130extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800131extern const struct kernel_symbol __start___ksymtab_gpl_future[];
132extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700133extern const struct kernel_symbol __start___ksymtab_unused[];
134extern const struct kernel_symbol __stop___ksymtab_unused[];
135extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
136extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
137extern const struct kernel_symbol __start___ksymtab_gpl_future[];
138extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139extern const unsigned long __start___kcrctab[];
140extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800141extern const unsigned long __start___kcrctab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700142extern const unsigned long __start___kcrctab_unused[];
143extern const unsigned long __start___kcrctab_unused_gpl[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#ifndef CONFIG_MODVERSIONS
146#define symversion(base, idx) NULL
147#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800148#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100151/* lookup symbol in given range of kernel_symbols */
152static const struct kernel_symbol *lookup_symbol(const char *name,
153 const struct kernel_symbol *start,
154 const struct kernel_symbol *stop)
155{
156 const struct kernel_symbol *ks = start;
157 for (; ks < stop; ks++)
158 if (strcmp(ks->name, name) == 0)
159 return ks;
160 return NULL;
161}
162
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700163static void printk_unused_warning(const char *name)
164{
165 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
166 "however this module is using it.\n", name);
167 printk(KERN_WARNING "This symbol will go away in the future.\n");
168 printk(KERN_WARNING "Please evalute if this is the right api to use, "
169 "and if it really is, submit a report the linux kernel "
170 "mailinglist together with submitting your code for "
171 "inclusion.\n");
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/* Find a symbol, return value, crc and module which owns it */
175static unsigned long __find_symbol(const char *name,
176 struct module **owner,
177 const unsigned long **crc,
178 int gplok)
179{
180 struct module *mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100181 const struct kernel_symbol *ks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Daniel Walker22a8bde2007-10-18 03:06:07 -0700183 /* Core kernel first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *owner = NULL;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100185 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
186 if (ks) {
187 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
188 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100191 ks = lookup_symbol(name, __start___ksymtab_gpl,
192 __stop___ksymtab_gpl);
193 if (ks) {
194 *crc = symversion(__start___kcrctab_gpl,
195 (ks - __start___ksymtab_gpl));
196 return ks->value;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800199 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
200 __stop___ksymtab_gpl_future);
201 if (ks) {
202 if (!gplok) {
203 printk(KERN_WARNING "Symbol %s is being used "
204 "by a non-GPL module, which will not "
205 "be allowed in the future\n", name);
206 printk(KERN_WARNING "Please see the file "
207 "Documentation/feature-removal-schedule.txt "
208 "in the kernel source tree for more "
209 "details.\n");
210 }
211 *crc = symversion(__start___kcrctab_gpl_future,
212 (ks - __start___ksymtab_gpl_future));
213 return ks->value;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700216 ks = lookup_symbol(name, __start___ksymtab_unused,
217 __stop___ksymtab_unused);
218 if (ks) {
219 printk_unused_warning(name);
220 *crc = symversion(__start___kcrctab_unused,
221 (ks - __start___ksymtab_unused));
222 return ks->value;
223 }
224
225 if (gplok)
226 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
227 __stop___ksymtab_unused_gpl);
228 if (ks) {
229 printk_unused_warning(name);
230 *crc = symversion(__start___kcrctab_unused_gpl,
231 (ks - __start___ksymtab_unused_gpl));
232 return ks->value;
233 }
234
Daniel Walker22a8bde2007-10-18 03:06:07 -0700235 /* Now try modules. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 list_for_each_entry(mod, &modules, list) {
237 *owner = mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100238 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
239 if (ks) {
240 *crc = symversion(mod->crcs, (ks - mod->syms));
241 return ks->value;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100245 ks = lookup_symbol(name, mod->gpl_syms,
246 mod->gpl_syms + mod->num_gpl_syms);
247 if (ks) {
248 *crc = symversion(mod->gpl_crcs,
249 (ks - mod->gpl_syms));
250 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 }
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700253 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
254 if (ks) {
255 printk_unused_warning(name);
256 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
257 return ks->value;
258 }
259
260 if (gplok) {
261 ks = lookup_symbol(name, mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
263 if (ks) {
264 printk_unused_warning(name);
265 *crc = symversion(mod->unused_gpl_crcs,
266 (ks - mod->unused_gpl_syms));
267 return ks->value;
268 }
269 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800270 ks = lookup_symbol(name, mod->gpl_future_syms,
271 (mod->gpl_future_syms +
272 mod->num_gpl_future_syms));
273 if (ks) {
274 if (!gplok) {
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more "
281 "details.\n");
282 }
283 *crc = symversion(mod->gpl_future_crcs,
284 (ks - mod->gpl_future_syms));
285 return ks->value;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 DEBUGP("Failed to find symbol %s\n", name);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* Search for module by name: must hold module_mutex. */
293static struct module *find_module(const char *name)
294{
295 struct module *mod;
296
297 list_for_each_entry(mod, &modules, list) {
298 if (strcmp(mod->name, name) == 0)
299 return mod;
300 }
301 return NULL;
302}
303
304#ifdef CONFIG_SMP
305/* Number of blocks used and allocated. */
306static unsigned int pcpu_num_used, pcpu_num_allocated;
307/* Size of each block. -ve means used. */
308static int *pcpu_size;
309
310static int split_block(unsigned int i, unsigned short size)
311{
312 /* Reallocation required? */
313 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700314 int *new;
315
316 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
317 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!new)
319 return 0;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 pcpu_size = new;
323 }
324
325 /* Insert a new subblock */
326 memmove(&pcpu_size[i+1], &pcpu_size[i],
327 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
328 pcpu_num_used++;
329
330 pcpu_size[i+1] -= size;
331 pcpu_size[i] = size;
332 return 1;
333}
334
335static inline unsigned int block_size(int val)
336{
337 if (val < 0)
338 return -val;
339 return val;
340}
341
342/* Created by linker magic */
343extern char __per_cpu_start[], __per_cpu_end[];
344
Rusty Russell842bbaa2005-08-01 21:11:47 -0700345static void *percpu_modalloc(unsigned long size, unsigned long align,
346 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 unsigned long extra;
349 unsigned int i;
350 void *ptr;
351
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200352 if (align > PAGE_SIZE) {
353 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
354 name, align, PAGE_SIZE);
355 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 ptr = __per_cpu_start;
359 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
360 /* Extra for alignment requirement. */
361 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
362 BUG_ON(i == 0 && extra != 0);
363
364 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
365 continue;
366
367 /* Transfer extra to previous block. */
368 if (pcpu_size[i-1] < 0)
369 pcpu_size[i-1] -= extra;
370 else
371 pcpu_size[i-1] += extra;
372 pcpu_size[i] -= extra;
373 ptr += extra;
374
375 /* Split block if warranted */
376 if (pcpu_size[i] - size > sizeof(unsigned long))
377 if (!split_block(i, size))
378 return NULL;
379
380 /* Mark allocated */
381 pcpu_size[i] = -pcpu_size[i];
382 return ptr;
383 }
384
385 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
386 size);
387 return NULL;
388}
389
390static void percpu_modfree(void *freeme)
391{
392 unsigned int i;
393 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
394
395 /* First entry is core kernel percpu data. */
396 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
397 if (ptr == freeme) {
398 pcpu_size[i] = -pcpu_size[i];
399 goto free;
400 }
401 }
402 BUG();
403
404 free:
405 /* Merge with previous? */
406 if (pcpu_size[i-1] >= 0) {
407 pcpu_size[i-1] += pcpu_size[i];
408 pcpu_num_used--;
409 memmove(&pcpu_size[i], &pcpu_size[i+1],
410 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
411 i--;
412 }
413 /* Merge with next? */
414 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
415 pcpu_size[i] += pcpu_size[i+1];
416 pcpu_num_used--;
417 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
418 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
419 }
420}
421
422static unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 Elf_Shdr *sechdrs,
424 const char *secstrings)
425{
426 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
427}
428
429static int percpu_modinit(void)
430{
431 pcpu_num_used = 2;
432 pcpu_num_allocated = 2;
433 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
434 GFP_KERNEL);
435 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200436 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Free room. */
438 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
439 if (pcpu_size[1] < 0) {
440 printk(KERN_ERR "No per-cpu room for modules.\n");
441 pcpu_num_used = 1;
442 }
443
444 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446__initcall(percpu_modinit);
447#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700448static inline void *percpu_modalloc(unsigned long size, unsigned long align,
449 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 return NULL;
452}
453static inline void percpu_modfree(void *pcpuptr)
454{
455 BUG();
456}
457static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 Elf_Shdr *sechdrs,
459 const char *secstrings)
460{
461 return 0;
462}
463static inline void percpu_modcopy(void *pcpudst, const void *src,
464 unsigned long size)
465{
466 /* pcpusec should be 0, and size of that section should be 0. */
467 BUG_ON(size != 0);
468}
469#endif /* CONFIG_SMP */
470
Matt Domschc988d2b2005-06-23 22:05:15 -0700471#define MODINFO_ATTR(field) \
472static void setup_modinfo_##field(struct module *mod, const char *s) \
473{ \
474 mod->field = kstrdup(s, GFP_KERNEL); \
475} \
476static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
477 struct module *mod, char *buffer) \
478{ \
479 return sprintf(buffer, "%s\n", mod->field); \
480} \
481static int modinfo_##field##_exists(struct module *mod) \
482{ \
483 return mod->field != NULL; \
484} \
485static void free_modinfo_##field(struct module *mod) \
486{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700487 kfree(mod->field); \
488 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700489} \
490static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900491 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700492 .show = show_modinfo_##field, \
493 .setup = setup_modinfo_##field, \
494 .test = modinfo_##field##_exists, \
495 .free = free_modinfo_##field, \
496};
497
498MODINFO_ATTR(version);
499MODINFO_ATTR(srcversion);
500
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800501#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/* Init the unload section of the module. */
503static void module_unload_init(struct module *mod)
504{
505 unsigned int i;
506
507 INIT_LIST_HEAD(&mod->modules_which_use_me);
508 for (i = 0; i < NR_CPUS; i++)
509 local_set(&mod->ref[i].count, 0);
510 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700511 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Backwards compatibility macros put refcount during init. */
513 mod->waiter = current;
514}
515
516/* modules using other modules */
517struct module_use
518{
519 struct list_head list;
520 struct module *module_which_uses;
521};
522
523/* Does a already use b? */
524static int already_uses(struct module *a, struct module *b)
525{
526 struct module_use *use;
527
528 list_for_each_entry(use, &b->modules_which_use_me, list) {
529 if (use->module_which_uses == a) {
530 DEBUGP("%s uses %s!\n", a->name, b->name);
531 return 1;
532 }
533 }
534 DEBUGP("%s does not use %s!\n", a->name, b->name);
535 return 0;
536}
537
538/* Module a uses b */
539static int use_module(struct module *a, struct module *b)
540{
541 struct module_use *use;
Kay Sievers270a6c42007-01-18 13:26:15 +0100542 int no_warn;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (b == NULL || already_uses(a, b)) return 1;
545
546 if (!strong_try_module_get(b))
547 return 0;
548
549 DEBUGP("Allocating new usage for %s.\n", a->name);
550 use = kmalloc(sizeof(*use), GFP_ATOMIC);
551 if (!use) {
552 printk("%s: out of memory loading\n", a->name);
553 module_put(b);
554 return 0;
555 }
556
557 use->module_which_uses = a;
558 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100559 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 1;
561}
562
563/* Clear the unload stuff of the module. */
564static void module_unload_free(struct module *mod)
565{
566 struct module *i;
567
568 list_for_each_entry(i, &modules, list) {
569 struct module_use *use;
570
571 list_for_each_entry(use, &i->modules_which_use_me, list) {
572 if (use->module_which_uses == mod) {
573 DEBUGP("%s unusing %s\n", mod->name, i->name);
574 module_put(i);
575 list_del(&use->list);
576 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100577 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* There can be at most one match. */
579 break;
580 }
581 }
582 }
583}
584
585#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800586static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 int ret = (flags & O_TRUNC);
589 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800590 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return ret;
592}
593#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800594static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 return 0;
597}
598#endif /* CONFIG_MODULE_FORCE_UNLOAD */
599
600struct stopref
601{
602 struct module *mod;
603 int flags;
604 int *forced;
605};
606
607/* Whole machine is stopped with interrupts off when this runs. */
608static int __try_stop_module(void *_sref)
609{
610 struct stopref *sref = _sref;
611
612 /* If it's not unused, quit unless we are told to block. */
613 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800614 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EWOULDBLOCK;
616 }
617
618 /* Mark it as dying. */
619 sref->mod->state = MODULE_STATE_GOING;
620 return 0;
621}
622
623static int try_stop_module(struct module *mod, int flags, int *forced)
624{
625 struct stopref sref = { mod, flags, forced };
626
627 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
628}
629
630unsigned int module_refcount(struct module *mod)
631{
632 unsigned int i, total = 0;
633
634 for (i = 0; i < NR_CPUS; i++)
635 total += local_read(&mod->ref[i].count);
636 return total;
637}
638EXPORT_SYMBOL(module_refcount);
639
640/* This exists whether we can unload or not */
641static void free_module(struct module *mod);
642
643static void wait_for_zero_refcount(struct module *mod)
644{
645 /* Since we might sleep for some time, drop the semaphore first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800646 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 for (;;) {
648 DEBUGP("Looking at refcount...\n");
649 set_current_state(TASK_UNINTERRUPTIBLE);
650 if (module_refcount(mod) == 0)
651 break;
652 schedule();
653 }
654 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800655 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800658asmlinkage long
659sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800662 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int ret, forced = 0;
664
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800665 if (!capable(CAP_SYS_MODULE))
666 return -EPERM;
667
668 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
669 return -EFAULT;
670 name[MODULE_NAME_LEN-1] = '\0';
671
Ashutosh Naik6389a382006-03-23 03:00:46 -0800672 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINTR;
674
675 mod = find_module(name);
676 if (!mod) {
677 ret = -ENOENT;
678 goto out;
679 }
680
681 if (!list_empty(&mod->modules_which_use_me)) {
682 /* Other modules depend on us: get rid of them first. */
683 ret = -EWOULDBLOCK;
684 goto out;
685 }
686
687 /* Doing init or already dying? */
688 if (mod->state != MODULE_STATE_LIVE) {
689 /* FIXME: if (force), slam module count and wake up
690 waiter --RR */
691 DEBUGP("%s already dying\n", mod->name);
692 ret = -EBUSY;
693 goto out;
694 }
695
696 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700697 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800698 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!forced) {
700 /* This module can't be removed */
701 ret = -EBUSY;
702 goto out;
703 }
704 }
705
706 /* Set this up before setting mod->state */
707 mod->waiter = current;
708
709 /* Stop the machine so refcounts can't move and disable module. */
710 ret = try_stop_module(mod, flags, &forced);
711 if (ret != 0)
712 goto out;
713
714 /* Never wait if forced. */
715 if (!forced && module_refcount(mod) != 0)
716 wait_for_zero_refcount(mod);
717
718 /* Final destruction now noone is using it. */
719 if (mod->exit != NULL) {
Ashutosh Naik6389a382006-03-23 03:00:46 -0800720 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mod->exit();
Ashutosh Naik6389a382006-03-23 03:00:46 -0800722 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 free_module(mod);
725
726 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800727 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return ret;
729}
730
731static void print_unload_info(struct seq_file *m, struct module *mod)
732{
733 struct module_use *use;
734 int printed_something = 0;
735
736 seq_printf(m, " %u ", module_refcount(mod));
737
738 /* Always include a trailing , so userspace can differentiate
739 between this and the old multi-field proc format. */
740 list_for_each_entry(use, &mod->modules_which_use_me, list) {
741 printed_something = 1;
742 seq_printf(m, "%s,", use->module_which_uses->name);
743 }
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (mod->init != NULL && mod->exit == NULL) {
746 printed_something = 1;
747 seq_printf(m, "[permanent],");
748 }
749
750 if (!printed_something)
751 seq_printf(m, "-");
752}
753
754void __symbol_put(const char *symbol)
755{
756 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 const unsigned long *crc;
758
Rusty Russell24da1cb2007-07-15 23:41:46 -0700759 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!__find_symbol(symbol, &owner, &crc, 1))
761 BUG();
762 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700763 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765EXPORT_SYMBOL(__symbol_put);
766
767void symbol_put_addr(void *addr)
768{
Trent Piepho5e376612006-05-15 09:44:06 -0700769 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Trent Piepho5e376612006-05-15 09:44:06 -0700771 if (core_kernel_text((unsigned long)addr))
772 return;
773
774 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700776 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778EXPORT_SYMBOL_GPL(symbol_put_addr);
779
780static ssize_t show_refcnt(struct module_attribute *mattr,
781 struct module *mod, char *buffer)
782{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400783 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900787 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .show = show_refcnt,
789};
790
Al Virof6a57032006-10-18 01:47:25 -0400791void module_put(struct module *module)
792{
793 if (module) {
794 unsigned int cpu = get_cpu();
795 local_dec(&module->ref[cpu].count);
796 /* Maybe they're waiting for us to drop reference? */
797 if (unlikely(!module_is_live(module)))
798 wake_up_process(module->waiter);
799 put_cpu();
800 }
801}
802EXPORT_SYMBOL(module_put);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804#else /* !CONFIG_MODULE_UNLOAD */
805static void print_unload_info(struct seq_file *m, struct module *mod)
806{
807 /* We don't know the usage count, or what modules are using. */
808 seq_printf(m, " - -");
809}
810
811static inline void module_unload_free(struct module *mod)
812{
813}
814
815static inline int use_module(struct module *a, struct module *b)
816{
817 return strong_try_module_get(b);
818}
819
820static inline void module_unload_init(struct module *mod)
821{
822}
823#endif /* CONFIG_MODULE_UNLOAD */
824
Kay Sievers1f717402006-11-24 12:15:25 +0100825static ssize_t show_initstate(struct module_attribute *mattr,
826 struct module *mod, char *buffer)
827{
828 const char *state = "unknown";
829
830 switch (mod->state) {
831 case MODULE_STATE_LIVE:
832 state = "live";
833 break;
834 case MODULE_STATE_COMING:
835 state = "coming";
836 break;
837 case MODULE_STATE_GOING:
838 state = "going";
839 break;
840 }
841 return sprintf(buffer, "%s\n", state);
842}
843
844static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900845 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100846 .show = show_initstate,
847};
848
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800849static struct module_attribute *modinfo_attrs[] = {
850 &modinfo_version,
851 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100852 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800853#ifdef CONFIG_MODULE_UNLOAD
854 &refcnt,
855#endif
856 NULL,
857};
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static const char vermagic[] = VERMAGIC_STRING;
860
861#ifdef CONFIG_MODVERSIONS
862static int check_version(Elf_Shdr *sechdrs,
863 unsigned int versindex,
864 const char *symname,
865 struct module *mod,
866 const unsigned long *crc)
867{
868 unsigned int i, num_versions;
869 struct modversion_info *versions;
870
871 /* Exporting module didn't supply crcs? OK, we're already tainted. */
872 if (!crc)
873 return 1;
874
875 versions = (void *) sechdrs[versindex].sh_addr;
876 num_versions = sechdrs[versindex].sh_size
877 / sizeof(struct modversion_info);
878
879 for (i = 0; i < num_versions; i++) {
880 if (strcmp(versions[i].name, symname) != 0)
881 continue;
882
883 if (versions[i].crc == *crc)
884 return 1;
885 printk("%s: disagrees about version of symbol %s\n",
886 mod->name, symname);
887 DEBUGP("Found checksum %lX vs module %lX\n",
888 *crc, versions[i].crc);
889 return 0;
890 }
891 /* Not in module's version table. OK, but that taints the kernel. */
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700892 if (!(tainted & TAINT_FORCED_MODULE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 printk("%s: no version for \"%s\" found: kernel tainted.\n",
894 mod->name, symname);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700895 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 1;
897}
898
899static inline int check_modstruct_version(Elf_Shdr *sechdrs,
900 unsigned int versindex,
901 struct module *mod)
902{
903 const unsigned long *crc;
904 struct module *owner;
905
906 if (!__find_symbol("struct_module", &owner, &crc, 1))
907 BUG();
908 return check_version(sechdrs, versindex, "struct_module", mod,
909 crc);
910}
911
912/* First part is kernel version, which we ignore. */
913static inline int same_magic(const char *amagic, const char *bmagic)
914{
915 amagic += strcspn(amagic, " ");
916 bmagic += strcspn(bmagic, " ");
917 return strcmp(amagic, bmagic) == 0;
918}
919#else
920static inline int check_version(Elf_Shdr *sechdrs,
921 unsigned int versindex,
922 const char *symname,
923 struct module *mod,
924 const unsigned long *crc)
925{
926 return 1;
927}
928
929static inline int check_modstruct_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
931 struct module *mod)
932{
933 return 1;
934}
935
936static inline int same_magic(const char *amagic, const char *bmagic)
937{
938 return strcmp(amagic, bmagic) == 0;
939}
940#endif /* CONFIG_MODVERSIONS */
941
942/* Resolve a symbol for this module. I.e. if we find one, record usage.
943 Must be holding module_mutex. */
944static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
945 unsigned int versindex,
946 const char *name,
947 struct module *mod)
948{
949 struct module *owner;
950 unsigned long ret;
951 const unsigned long *crc;
952
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700953 ret = __find_symbol(name, &owner, &crc,
954 !(mod->taints & TAINT_PROPRIETARY_MODULE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ret) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800956 /* use_module can fail due to OOM,
957 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!check_version(sechdrs, versindex, name, mod, crc) ||
959 !use_module(mod, owner))
960 ret = 0;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return ret;
963}
964
965
966/*
967 * /sys/module/foo/sections stuff
968 * J. Corbet <corbet@lwn.net>
969 */
970#ifdef CONFIG_KALLSYMS
971static ssize_t module_sect_show(struct module_attribute *mattr,
972 struct module *mod, char *buf)
973{
974 struct module_sect_attr *sattr =
975 container_of(mattr, struct module_sect_attr, mattr);
976 return sprintf(buf, "0x%lx\n", sattr->address);
977}
978
Ian S. Nelson04b1db92006-09-29 02:01:31 -0700979static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
980{
981 int section;
982
983 for (section = 0; section < sect_attrs->nsections; section++)
984 kfree(sect_attrs->attrs[section].name);
985 kfree(sect_attrs);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void add_sect_attrs(struct module *mod, unsigned int nsect,
989 char *secstrings, Elf_Shdr *sechdrs)
990{
991 unsigned int nloaded = 0, i, size[2];
992 struct module_sect_attrs *sect_attrs;
993 struct module_sect_attr *sattr;
994 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* Count loaded sections and allocate structures */
997 for (i = 0; i < nsect; i++)
998 if (sechdrs[i].sh_flags & SHF_ALLOC)
999 nloaded++;
1000 size[0] = ALIGN(sizeof(*sect_attrs)
1001 + nloaded * sizeof(sect_attrs->attrs[0]),
1002 sizeof(sect_attrs->grp.attrs[0]));
1003 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001004 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1005 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return;
1007
1008 /* Setup section attributes. */
1009 sect_attrs->grp.name = "sections";
1010 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1011
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001012 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sattr = &sect_attrs->attrs[0];
1014 gattr = &sect_attrs->grp.attrs[0];
1015 for (i = 0; i < nsect; i++) {
1016 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1017 continue;
1018 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001019 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1020 GFP_KERNEL);
1021 if (sattr->name == NULL)
1022 goto out;
1023 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 sattr->mattr.show = module_sect_show;
1025 sattr->mattr.store = NULL;
1026 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sattr->mattr.attr.mode = S_IRUGO;
1028 *(gattr++) = &(sattr++)->mattr.attr;
1029 }
1030 *gattr = NULL;
1031
1032 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1033 goto out;
1034
1035 mod->sect_attrs = sect_attrs;
1036 return;
1037 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001038 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041static void remove_sect_attrs(struct module *mod)
1042{
1043 if (mod->sect_attrs) {
1044 sysfs_remove_group(&mod->mkobj.kobj,
1045 &mod->sect_attrs->grp);
1046 /* We are positive that no one is using any sect attrs
1047 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001048 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 mod->sect_attrs = NULL;
1050 }
1051}
1052
Roland McGrath6d760132007-10-16 23:26:40 -07001053/*
1054 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1055 */
1056
1057struct module_notes_attrs {
1058 struct kobject *dir;
1059 unsigned int notes;
1060 struct bin_attribute attrs[0];
1061};
1062
1063static ssize_t module_notes_read(struct kobject *kobj,
1064 struct bin_attribute *bin_attr,
1065 char *buf, loff_t pos, size_t count)
1066{
1067 /*
1068 * The caller checked the pos and count against our size.
1069 */
1070 memcpy(buf, bin_attr->private + pos, count);
1071 return count;
1072}
1073
1074static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1075 unsigned int i)
1076{
1077 if (notes_attrs->dir) {
1078 while (i-- > 0)
1079 sysfs_remove_bin_file(notes_attrs->dir,
1080 &notes_attrs->attrs[i]);
1081 kobject_del(notes_attrs->dir);
1082 }
1083 kfree(notes_attrs);
1084}
1085
1086static void add_notes_attrs(struct module *mod, unsigned int nsect,
1087 char *secstrings, Elf_Shdr *sechdrs)
1088{
1089 unsigned int notes, loaded, i;
1090 struct module_notes_attrs *notes_attrs;
1091 struct bin_attribute *nattr;
1092
1093 /* Count notes sections and allocate structures. */
1094 notes = 0;
1095 for (i = 0; i < nsect; i++)
1096 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1097 (sechdrs[i].sh_type == SHT_NOTE))
1098 ++notes;
1099
1100 if (notes == 0)
1101 return;
1102
1103 notes_attrs = kzalloc(sizeof(*notes_attrs)
1104 + notes * sizeof(notes_attrs->attrs[0]),
1105 GFP_KERNEL);
1106 if (notes_attrs == NULL)
1107 return;
1108
1109 notes_attrs->notes = notes;
1110 nattr = &notes_attrs->attrs[0];
1111 for (loaded = i = 0; i < nsect; ++i) {
1112 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1113 continue;
1114 if (sechdrs[i].sh_type == SHT_NOTE) {
1115 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1116 nattr->attr.mode = S_IRUGO;
1117 nattr->size = sechdrs[i].sh_size;
1118 nattr->private = (void *) sechdrs[i].sh_addr;
1119 nattr->read = module_notes_read;
1120 ++nattr;
1121 }
1122 ++loaded;
1123 }
1124
1125 notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes");
1126 if (!notes_attrs->dir)
1127 goto out;
1128
1129 for (i = 0; i < notes; ++i)
1130 if (sysfs_create_bin_file(notes_attrs->dir,
1131 &notes_attrs->attrs[i]))
1132 goto out;
1133
1134 mod->notes_attrs = notes_attrs;
1135 return;
1136
1137 out:
1138 free_notes_attrs(notes_attrs, i);
1139}
1140
1141static void remove_notes_attrs(struct module *mod)
1142{
1143 if (mod->notes_attrs)
1144 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1150 char *sectstrings, Elf_Shdr *sechdrs)
1151{
1152}
1153
1154static inline void remove_sect_attrs(struct module *mod)
1155{
1156}
Roland McGrath6d760132007-10-16 23:26:40 -07001157
1158static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1159 char *sectstrings, Elf_Shdr *sechdrs)
1160{
1161}
1162
1163static inline void remove_notes_attrs(struct module *mod)
1164{
1165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166#endif /* CONFIG_KALLSYMS */
1167
Randy Dunlapef665c12007-02-13 15:19:06 -08001168#ifdef CONFIG_SYSFS
1169int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001170{
1171 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001172 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001173 int error = 0;
1174 int i;
1175
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001176 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1177 (ARRAY_SIZE(modinfo_attrs) + 1)),
1178 GFP_KERNEL);
1179 if (!mod->modinfo_attrs)
1180 return -ENOMEM;
1181
1182 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001183 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1184 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001185 (attr->test && attr->test(mod))) {
1186 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001187 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1188 ++temp_attr;
1189 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001190 }
1191 return error;
1192}
1193
Randy Dunlapef665c12007-02-13 15:19:06 -08001194void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001195{
1196 struct module_attribute *attr;
1197 int i;
1198
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001199 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1200 /* pick a field to test for end of list */
1201 if (!attr->attr.name)
1202 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001203 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001204 if (attr->free)
1205 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001206 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001207 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001208}
Randy Dunlapef665c12007-02-13 15:19:06 -08001209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Randy Dunlapef665c12007-02-13 15:19:06 -08001211#ifdef CONFIG_SYSFS
1212int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 int err;
1215
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001216 if (!module_sysfs_initialized) {
1217 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001218 mod->name);
1219 err = -EINVAL;
1220 goto out;
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1223 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1224 if (err)
1225 goto out;
1226 kobj_set_kset_s(&mod->mkobj, module_subsys);
1227 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001228
Kay Sieverse17e0f52006-11-24 12:15:25 +01001229 kobject_init(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001230
1231out:
1232 return err;
1233}
1234
Randy Dunlapef665c12007-02-13 15:19:06 -08001235int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001236 struct kernel_param *kparam,
1237 unsigned int num_params)
1238{
1239 int err;
1240
1241 /* delay uevent until full sysfs population */
Kay Sieverse17e0f52006-11-24 12:15:25 +01001242 err = kobject_add(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (err)
1244 goto out;
1245
Kay Sievers270a6c42007-01-18 13:26:15 +01001246 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
Akinobu Mita240936e2007-04-26 00:12:09 -07001247 if (!mod->holders_dir) {
1248 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001249 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001250 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 err = module_param_sysfs_setup(mod, kparam, num_params);
1253 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001254 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Matt Domschc988d2b2005-06-23 22:05:15 -07001256 err = module_add_modinfo_attrs(mod);
1257 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001258 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001259
Kay Sieverse17e0f52006-11-24 12:15:25 +01001260 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262
Kay Sieverse17e0f52006-11-24 12:15:25 +01001263out_unreg_param:
1264 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001265out_unreg_holders:
1266 kobject_unregister(mod->holders_dir);
1267out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001268 kobject_del(&mod->mkobj.kobj);
1269 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270out:
1271 return err;
1272}
Randy Dunlapef665c12007-02-13 15:19:06 -08001273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275static void mod_kobject_remove(struct module *mod)
1276{
Matt Domschc988d2b2005-06-23 22:05:15 -07001277 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 module_param_sysfs_remove(mod);
Mariusz Kozlowskib92be9f2007-02-14 21:03:39 +01001279 kobject_unregister(mod->mkobj.drivers_dir);
1280 kobject_unregister(mod->holders_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 kobject_unregister(&mod->mkobj.kobj);
1282}
1283
1284/*
1285 * unlink the module with the whole machine is stopped with interrupts off
1286 * - this defends against kallsyms not taking locks
1287 */
1288static int __unlink_module(void *_mod)
1289{
1290 struct module *mod = _mod;
1291 list_del(&mod->list);
1292 return 0;
1293}
1294
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001295/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296static void free_module(struct module *mod)
1297{
1298 /* Delete from various lists */
1299 stop_machine_run(__unlink_module, mod, NR_CPUS);
Roland McGrath6d760132007-10-16 23:26:40 -07001300 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 remove_sect_attrs(mod);
1302 mod_kobject_remove(mod);
1303
Jan Beulich4552d5d2006-06-26 13:57:28 +02001304 unwind_remove_table(mod->unwind_info, 0);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Arch-specific cleanup. */
1307 module_arch_cleanup(mod);
1308
1309 /* Module unload stuff */
1310 module_unload_free(mod);
1311
1312 /* This may be NULL, but that's OK */
1313 module_free(mod, mod->module_init);
1314 kfree(mod->args);
1315 if (mod->percpu)
1316 percpu_modfree(mod->percpu);
1317
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001318 /* Free lock-classes: */
1319 lockdep_free_key_range(mod->module_core, mod->core_size);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* Finally, free the core (containing the module structure) */
1322 module_free(mod, mod->module_core);
1323}
1324
1325void *__symbol_get(const char *symbol)
1326{
1327 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001328 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 const unsigned long *crc;
1330
Rusty Russell24da1cb2007-07-15 23:41:46 -07001331 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 value = __find_symbol(symbol, &owner, &crc, 1);
1333 if (value && !strong_try_module_get(owner))
1334 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001335 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 return (void *)value;
1338}
1339EXPORT_SYMBOL_GPL(__symbol_get);
1340
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001341/*
1342 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001343 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001344 */
1345static int verify_export_symbols(struct module *mod)
1346{
1347 const char *name = NULL;
1348 unsigned long i, ret = 0;
1349 struct module *owner;
1350 const unsigned long *crc;
1351
1352 for (i = 0; i < mod->num_syms; i++)
Daniel Walker22a8bde2007-10-18 03:06:07 -07001353 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001354 name = mod->syms[i].name;
1355 ret = -ENOEXEC;
1356 goto dup;
1357 }
1358
1359 for (i = 0; i < mod->num_gpl_syms; i++)
Daniel Walker22a8bde2007-10-18 03:06:07 -07001360 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001361 name = mod->gpl_syms[i].name;
1362 ret = -ENOEXEC;
1363 goto dup;
1364 }
1365
1366dup:
1367 if (ret)
1368 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1369 mod->name, name, module_name(owner));
1370
1371 return ret;
1372}
1373
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001374/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375static int simplify_symbols(Elf_Shdr *sechdrs,
1376 unsigned int symindex,
1377 const char *strtab,
1378 unsigned int versindex,
1379 unsigned int pcpuindex,
1380 struct module *mod)
1381{
1382 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1383 unsigned long secbase;
1384 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1385 int ret = 0;
1386
1387 for (i = 1; i < n; i++) {
1388 switch (sym[i].st_shndx) {
1389 case SHN_COMMON:
1390 /* We compiled with -fno-common. These are not
1391 supposed to happen. */
1392 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1393 printk("%s: please compile with -fno-common\n",
1394 mod->name);
1395 ret = -ENOEXEC;
1396 break;
1397
1398 case SHN_ABS:
1399 /* Don't need to do anything */
1400 DEBUGP("Absolute symbol: 0x%08lx\n",
1401 (long)sym[i].st_value);
1402 break;
1403
1404 case SHN_UNDEF:
1405 sym[i].st_value
1406 = resolve_symbol(sechdrs, versindex,
1407 strtab + sym[i].st_name, mod);
1408
1409 /* Ok if resolved. */
1410 if (sym[i].st_value != 0)
1411 break;
1412 /* Ok if weak. */
1413 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1414 break;
1415
1416 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1417 mod->name, strtab + sym[i].st_name);
1418 ret = -ENOENT;
1419 break;
1420
1421 default:
1422 /* Divert to percpu allocation if a percpu var. */
1423 if (sym[i].st_shndx == pcpuindex)
1424 secbase = (unsigned long)mod->percpu;
1425 else
1426 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1427 sym[i].st_value += secbase;
1428 break;
1429 }
1430 }
1431
1432 return ret;
1433}
1434
1435/* Update size with this section: return offset. */
1436static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1437{
1438 long ret;
1439
1440 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1441 *size = ret + sechdr->sh_size;
1442 return ret;
1443}
1444
1445/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1446 might -- code, read-only data, read-write data, small data. Tally
1447 sizes, and place the offsets into sh_entsize fields: high bit means it
1448 belongs in init. */
1449static void layout_sections(struct module *mod,
1450 const Elf_Ehdr *hdr,
1451 Elf_Shdr *sechdrs,
1452 const char *secstrings)
1453{
1454 static unsigned long const masks[][2] = {
1455 /* NOTE: all executable code must be the first section
1456 * in this array; otherwise modify the text_size
1457 * finder in the two loops below */
1458 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1459 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1460 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1461 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1462 };
1463 unsigned int m, i;
1464
1465 for (i = 0; i < hdr->e_shnum; i++)
1466 sechdrs[i].sh_entsize = ~0UL;
1467
1468 DEBUGP("Core section allocation order:\n");
1469 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1470 for (i = 0; i < hdr->e_shnum; ++i) {
1471 Elf_Shdr *s = &sechdrs[i];
1472
1473 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1474 || (s->sh_flags & masks[m][1])
1475 || s->sh_entsize != ~0UL
1476 || strncmp(secstrings + s->sh_name,
1477 ".init", 5) == 0)
1478 continue;
1479 s->sh_entsize = get_offset(&mod->core_size, s);
1480 DEBUGP("\t%s\n", secstrings + s->sh_name);
1481 }
1482 if (m == 0)
1483 mod->core_text_size = mod->core_size;
1484 }
1485
1486 DEBUGP("Init section allocation order:\n");
1487 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1488 for (i = 0; i < hdr->e_shnum; ++i) {
1489 Elf_Shdr *s = &sechdrs[i];
1490
1491 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1492 || (s->sh_flags & masks[m][1])
1493 || s->sh_entsize != ~0UL
1494 || strncmp(secstrings + s->sh_name,
1495 ".init", 5) != 0)
1496 continue;
1497 s->sh_entsize = (get_offset(&mod->init_size, s)
1498 | INIT_OFFSET_MASK);
1499 DEBUGP("\t%s\n", secstrings + s->sh_name);
1500 }
1501 if (m == 0)
1502 mod->init_text_size = mod->init_size;
1503 }
1504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506static void set_license(struct module *mod, const char *license)
1507{
1508 if (!license)
1509 license = "unspecified";
1510
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001511 if (!license_is_gpl_compatible(license)) {
1512 if (!(tainted & TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001513 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001514 "kernel.\n", mod->name, license);
1515 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517}
1518
1519/* Parse tag=value strings from .modinfo section */
1520static char *next_string(char *string, unsigned long *secsize)
1521{
1522 /* Skip non-zero chars */
1523 while (string[0]) {
1524 string++;
1525 if ((*secsize)-- <= 1)
1526 return NULL;
1527 }
1528
1529 /* Skip any zero padding. */
1530 while (!string[0]) {
1531 string++;
1532 if ((*secsize)-- <= 1)
1533 return NULL;
1534 }
1535 return string;
1536}
1537
1538static char *get_modinfo(Elf_Shdr *sechdrs,
1539 unsigned int info,
1540 const char *tag)
1541{
1542 char *p;
1543 unsigned int taglen = strlen(tag);
1544 unsigned long size = sechdrs[info].sh_size;
1545
1546 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1547 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1548 return p + taglen + 1;
1549 }
1550 return NULL;
1551}
1552
Matt Domschc988d2b2005-06-23 22:05:15 -07001553static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1554 unsigned int infoindex)
1555{
1556 struct module_attribute *attr;
1557 int i;
1558
1559 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1560 if (attr->setup)
1561 attr->setup(mod,
1562 get_modinfo(sechdrs,
1563 infoindex,
1564 attr->attr.name));
1565 }
1566}
Matt Domschc988d2b2005-06-23 22:05:15 -07001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#ifdef CONFIG_KALLSYMS
Alexey Dobriyanea078902007-05-08 00:28:39 -07001569static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001571 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1572 return 1;
1573 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001574 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001576 else
1577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/* As per nm */
1581static char elf_type(const Elf_Sym *sym,
1582 Elf_Shdr *sechdrs,
1583 const char *secstrings,
1584 struct module *mod)
1585{
1586 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1587 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1588 return 'v';
1589 else
1590 return 'w';
1591 }
1592 if (sym->st_shndx == SHN_UNDEF)
1593 return 'U';
1594 if (sym->st_shndx == SHN_ABS)
1595 return 'a';
1596 if (sym->st_shndx >= SHN_LORESERVE)
1597 return '?';
1598 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1599 return 't';
1600 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1601 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1602 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1603 return 'r';
1604 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1605 return 'g';
1606 else
1607 return 'd';
1608 }
1609 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1610 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1611 return 's';
1612 else
1613 return 'b';
1614 }
1615 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1616 ".debug", strlen(".debug")) == 0)
1617 return 'n';
1618 return '?';
1619}
1620
1621static void add_kallsyms(struct module *mod,
1622 Elf_Shdr *sechdrs,
1623 unsigned int symindex,
1624 unsigned int strindex,
1625 const char *secstrings)
1626{
1627 unsigned int i;
1628
1629 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1630 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1631 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1632
1633 /* Set types up while we still have access to sections. */
1634 for (i = 0; i < mod->num_symtab; i++)
1635 mod->symtab[i].st_info
1636 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1637}
1638#else
1639static inline void add_kallsyms(struct module *mod,
1640 Elf_Shdr *sechdrs,
1641 unsigned int symindex,
1642 unsigned int strindex,
1643 const char *secstrings)
1644{
1645}
1646#endif /* CONFIG_KALLSYMS */
1647
1648/* Allocate and load the module: note that size of section 0 is always
1649 zero, and we rely on this for optional sections. */
1650static struct module *load_module(void __user *umod,
1651 unsigned long len,
1652 const char __user *uargs)
1653{
1654 Elf_Ehdr *hdr;
1655 Elf_Shdr *sechdrs;
1656 char *secstrings, *args, *modmagic, *strtab = NULL;
Andrew Morton84860f92006-06-28 04:26:46 -07001657 unsigned int i;
1658 unsigned int symindex = 0;
1659 unsigned int strindex = 0;
1660 unsigned int setupindex;
1661 unsigned int exindex;
1662 unsigned int exportindex;
1663 unsigned int modindex;
1664 unsigned int obsparmindex;
1665 unsigned int infoindex;
1666 unsigned int gplindex;
1667 unsigned int crcindex;
1668 unsigned int gplcrcindex;
1669 unsigned int versindex;
1670 unsigned int pcpuindex;
1671 unsigned int gplfutureindex;
1672 unsigned int gplfuturecrcindex;
1673 unsigned int unwindex = 0;
1674 unsigned int unusedindex;
1675 unsigned int unusedcrcindex;
1676 unsigned int unusedgplindex;
1677 unsigned int unusedgplcrcindex;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001678 unsigned int markersindex;
1679 unsigned int markersstringsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct module *mod;
1681 long err = 0;
1682 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1683 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001684 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1687 umod, len, uargs);
1688 if (len < sizeof(*hdr))
1689 return ERR_PTR(-ENOEXEC);
1690
1691 /* Suck in entire file: we'll want most of it. */
1692 /* vmalloc barfs on "unusual" numbers. Check here */
1693 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1694 return ERR_PTR(-ENOMEM);
1695 if (copy_from_user(hdr, umod, len) != 0) {
1696 err = -EFAULT;
1697 goto free_hdr;
1698 }
1699
1700 /* Sanity checks against insmoding binaries or wrong arch,
1701 weird elf version */
1702 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1703 || hdr->e_type != ET_REL
1704 || !elf_check_arch(hdr)
1705 || hdr->e_shentsize != sizeof(*sechdrs)) {
1706 err = -ENOEXEC;
1707 goto free_hdr;
1708 }
1709
1710 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1711 goto truncated;
1712
1713 /* Convenience variables */
1714 sechdrs = (void *)hdr + hdr->e_shoff;
1715 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1716 sechdrs[0].sh_addr = 0;
1717
1718 for (i = 1; i < hdr->e_shnum; i++) {
1719 if (sechdrs[i].sh_type != SHT_NOBITS
1720 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1721 goto truncated;
1722
1723 /* Mark all sections sh_addr with their address in the
1724 temporary image. */
1725 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1726
1727 /* Internal symbols and strings. */
1728 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1729 symindex = i;
1730 strindex = sechdrs[i].sh_link;
1731 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1732 }
1733#ifndef CONFIG_MODULE_UNLOAD
1734 /* Don't load .exit sections */
1735 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1736 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1737#endif
1738 }
1739
1740 modindex = find_sec(hdr, sechdrs, secstrings,
1741 ".gnu.linkonce.this_module");
1742 if (!modindex) {
1743 printk(KERN_WARNING "No module found in object\n");
1744 err = -ENOEXEC;
1745 goto free_hdr;
1746 }
1747 mod = (void *)sechdrs[modindex].sh_addr;
1748
1749 if (symindex == 0) {
1750 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1751 mod->name);
1752 err = -ENOEXEC;
1753 goto free_hdr;
1754 }
1755
1756 /* Optional sections */
1757 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1758 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001759 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001760 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1761 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1763 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001764 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001765 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1766 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1768 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1769 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1770 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1771 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1772 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001773#ifdef ARCH_UNWIND_SECTION_NAME
1774 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 /* Don't keep modinfo section */
1778 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1779#ifdef CONFIG_KALLSYMS
1780 /* Keep symbol and string tables for decoding later. */
1781 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1782 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1783#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001784 if (unwindex)
1785 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 /* Check module struct version now, before we try to use module. */
1788 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1789 err = -ENOEXEC;
1790 goto free_hdr;
1791 }
1792
1793 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1794 /* This is allowed: modprobe --force will invalidate it. */
1795 if (!modmagic) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001796 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1798 mod->name);
1799 } else if (!same_magic(modmagic, vermagic)) {
1800 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1801 mod->name, modmagic, vermagic);
1802 err = -ENOEXEC;
1803 goto free_hdr;
1804 }
1805
1806 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001807 args = strndup_user(uargs, ~0UL >> 1);
1808 if (IS_ERR(args)) {
1809 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto free_hdr;
1811 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (find_module(mod->name)) {
1814 err = -EEXIST;
1815 goto free_mod;
1816 }
1817
1818 mod->state = MODULE_STATE_COMING;
1819
1820 /* Allow arches to frob section contents and sizes. */
1821 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1822 if (err < 0)
1823 goto free_mod;
1824
1825 if (pcpuindex) {
1826 /* We have a special allocation for this section. */
1827 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001828 sechdrs[pcpuindex].sh_addralign,
1829 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (!percpu) {
1831 err = -ENOMEM;
1832 goto free_mod;
1833 }
1834 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1835 mod->percpu = percpu;
1836 }
1837
1838 /* Determine total sizes, and put offsets in sh_entsize. For now
1839 this is done generically; there doesn't appear to be any
1840 special cases for the architectures. */
1841 layout_sections(mod, hdr, sechdrs, secstrings);
1842
1843 /* Do the allocs. */
1844 ptr = module_alloc(mod->core_size);
1845 if (!ptr) {
1846 err = -ENOMEM;
1847 goto free_percpu;
1848 }
1849 memset(ptr, 0, mod->core_size);
1850 mod->module_core = ptr;
1851
1852 ptr = module_alloc(mod->init_size);
1853 if (!ptr && mod->init_size) {
1854 err = -ENOMEM;
1855 goto free_core;
1856 }
1857 memset(ptr, 0, mod->init_size);
1858 mod->module_init = ptr;
1859
1860 /* Transfer each section which specifies SHF_ALLOC */
1861 DEBUGP("final section addresses:\n");
1862 for (i = 0; i < hdr->e_shnum; i++) {
1863 void *dest;
1864
1865 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1866 continue;
1867
1868 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1869 dest = mod->module_init
1870 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1871 else
1872 dest = mod->module_core + sechdrs[i].sh_entsize;
1873
1874 if (sechdrs[i].sh_type != SHT_NOBITS)
1875 memcpy(dest, (void *)sechdrs[i].sh_addr,
1876 sechdrs[i].sh_size);
1877 /* Update sh_addr to point to copy in image. */
1878 sechdrs[i].sh_addr = (unsigned long)dest;
1879 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1880 }
1881 /* Module has been moved. */
1882 mod = (void *)sechdrs[modindex].sh_addr;
1883
1884 /* Now we've moved module, initialize linked lists, etc. */
1885 module_unload_init(mod);
1886
Kay Sievers270a6c42007-01-18 13:26:15 +01001887 /* Initialize kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07001888 err = mod_sysfs_init(mod);
1889 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001890 goto cleanup;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /* Set up license info based on the info section */
1893 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1894
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001895 if (strcmp(mod->name, "ndiswrapper") == 0)
Randy Dunlap0e2d57f2006-10-29 22:46:34 -08001896 add_taint(TAINT_PROPRIETARY_MODULE);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001897 if (strcmp(mod->name, "driverloader") == 0)
1898 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08001899
Matt Domschc988d2b2005-06-23 22:05:15 -07001900 /* Set up MODINFO_ATTR fields */
1901 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* Fix up syms, so that st_value is a pointer to location. */
1904 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1905 mod);
1906 if (err < 0)
1907 goto cleanup;
1908
1909 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1910 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1911 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1912 if (crcindex)
1913 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1914 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1915 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1916 if (gplcrcindex)
1917 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001918 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1919 sizeof(*mod->gpl_future_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001920 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1921 sizeof(*mod->unused_syms);
1922 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1923 sizeof(*mod->unused_gpl_syms);
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001924 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1925 if (gplfuturecrcindex)
1926 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001928 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1929 if (unusedcrcindex)
1930 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1931 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1932 if (unusedgplcrcindex)
1933 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935#ifdef CONFIG_MODVERSIONS
Daniel Walker22a8bde2007-10-18 03:06:07 -07001936 if ((mod->num_syms && !crcindex) ||
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001937 (mod->num_gpl_syms && !gplcrcindex) ||
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001938 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1939 (mod->num_unused_syms && !unusedcrcindex) ||
1940 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 printk(KERN_WARNING "%s: No versions for exported symbols."
1942 " Tainting kernel.\n", mod->name);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001943 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001946 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
1947 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
1948 "__markers_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 /* Now do relocations. */
1951 for (i = 1; i < hdr->e_shnum; i++) {
1952 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1953 unsigned int info = sechdrs[i].sh_info;
1954
1955 /* Not a valid relocation section? */
1956 if (info >= hdr->e_shnum)
1957 continue;
1958
1959 /* Don't bother with non-allocated sections */
1960 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1961 continue;
1962
1963 if (sechdrs[i].sh_type == SHT_REL)
1964 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1965 else if (sechdrs[i].sh_type == SHT_RELA)
1966 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1967 mod);
1968 if (err < 0)
1969 goto cleanup;
1970 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001971#ifdef CONFIG_MARKERS
1972 mod->markers = (void *)sechdrs[markersindex].sh_addr;
1973 mod->num_markers =
1974 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
1975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001977 /* Find duplicate symbols */
1978 err = verify_export_symbols(mod);
1979
1980 if (err < 0)
1981 goto cleanup;
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* Set up and sort exception table */
1984 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1985 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1986 sort_extable(extable, extable + mod->num_exentries);
1987
1988 /* Finally, copy percpu area over. */
1989 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1990 sechdrs[pcpuindex].sh_size);
1991
1992 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1993
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001994#ifdef CONFIG_MARKERS
1995 if (!mod->taints)
1996 marker_update_probe_range(mod->markers,
1997 mod->markers + mod->num_markers, NULL, NULL);
1998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 err = module_finalize(hdr, sechdrs, mod);
2000 if (err < 0)
2001 goto cleanup;
2002
Thomas Koeller378bac82005-09-06 15:17:11 -07002003 /* flush the icache in correct context */
2004 old_fs = get_fs();
2005 set_fs(KERNEL_DS);
2006
2007 /*
2008 * Flush the instruction cache, since we've played with text.
2009 * Do it before processing of module parameters, so the module
2010 * can provide parameter accessor functions of its own.
2011 */
2012 if (mod->module_init)
2013 flush_icache_range((unsigned long)mod->module_init,
2014 (unsigned long)mod->module_init
2015 + mod->init_size);
2016 flush_icache_range((unsigned long)mod->module_core,
2017 (unsigned long)mod->module_core + mod->core_size);
2018
2019 set_fs(old_fs);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002022 if (obsparmindex)
2023 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2024 mod->name);
2025
2026 /* Size of section 0 is 0, so this works well if no params */
2027 err = parse_args(mod->name, mod->args,
2028 (struct kernel_param *)
2029 sechdrs[setupindex].sh_addr,
2030 sechdrs[setupindex].sh_size
2031 / sizeof(struct kernel_param),
2032 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (err < 0)
2034 goto arch_cleanup;
2035
Daniel Walker22a8bde2007-10-18 03:06:07 -07002036 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 (struct kernel_param *)
2038 sechdrs[setupindex].sh_addr,
2039 sechdrs[setupindex].sh_size
2040 / sizeof(struct kernel_param));
2041 if (err < 0)
2042 goto arch_cleanup;
2043 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002044 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Jan Beulich4552d5d2006-06-26 13:57:28 +02002046 /* Size of section 0 is 0, so this works well if no unwind info. */
2047 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002048 (void *)sechdrs[unwindex].sh_addr,
2049 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Get rid of temporary copy */
2052 vfree(hdr);
2053
2054 /* Done! */
2055 return mod;
2056
2057 arch_cleanup:
2058 module_arch_cleanup(mod);
2059 cleanup:
2060 module_unload_free(mod);
2061 module_free(mod, mod->module_init);
2062 free_core:
2063 module_free(mod, mod->module_core);
2064 free_percpu:
2065 if (percpu)
2066 percpu_modfree(percpu);
2067 free_mod:
2068 kfree(args);
2069 free_hdr:
2070 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002071 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 truncated:
2074 printk(KERN_ERR "Module len %lu truncated\n", len);
2075 err = -ENOEXEC;
2076 goto free_hdr;
2077}
2078
2079/*
2080 * link the module with the whole machine is stopped with interrupts off
2081 * - this defends against kallsyms not taking locks
2082 */
2083static int __link_module(void *_mod)
2084{
2085 struct module *mod = _mod;
2086 list_add(&mod->list, &modules);
2087 return 0;
2088}
2089
2090/* This is where the real work happens */
2091asmlinkage long
2092sys_init_module(void __user *umod,
2093 unsigned long len,
2094 const char __user *uargs)
2095{
2096 struct module *mod;
2097 int ret = 0;
2098
2099 /* Must have permission */
2100 if (!capable(CAP_SYS_MODULE))
2101 return -EPERM;
2102
2103 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002104 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return -EINTR;
2106
2107 /* Do all the hard work */
2108 mod = load_module(umod, len, uargs);
2109 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002110 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return PTR_ERR(mod);
2112 }
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* Now sew it into the lists. They won't access us, since
2115 strong_try_module_get() will fail. */
2116 stop_machine_run(__link_module, mod, NR_CPUS);
2117
2118 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002119 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Alan Sterne041c682006-03-27 01:16:30 -08002121 blocking_notifier_call_chain(&module_notify_list,
2122 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* Start the module */
2125 if (mod->init != NULL)
2126 ret = mod->init();
2127 if (ret < 0) {
2128 /* Init routine failed: abort. Try to protect us from
2129 buggy refcounters. */
2130 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002131 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002132 module_put(mod);
2133 mutex_lock(&module_mutex);
2134 free_module(mod);
2135 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return ret;
2137 }
2138
2139 /* Now it's a first class citizen! */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002140 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 mod->state = MODULE_STATE_LIVE;
2142 /* Drop initial reference. */
2143 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002144 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 module_free(mod, mod->module_init);
2146 mod->module_init = NULL;
2147 mod->init_size = 0;
2148 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002149 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 return 0;
2152}
2153
2154static inline int within(unsigned long addr, void *start, unsigned long size)
2155{
2156 return ((void *)addr >= start && (void *)addr < start + size);
2157}
2158
2159#ifdef CONFIG_KALLSYMS
2160/*
2161 * This ignores the intensely annoying "mapping symbols" found
2162 * in ARM ELF files: $a, $t and $d.
2163 */
2164static inline int is_arm_mapping_symbol(const char *str)
2165{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002166 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 && (str[2] == '\0' || str[2] == '.');
2168}
2169
2170static const char *get_ksymbol(struct module *mod,
2171 unsigned long addr,
2172 unsigned long *size,
2173 unsigned long *offset)
2174{
2175 unsigned int i, best = 0;
2176 unsigned long nextval;
2177
2178 /* At worse, next value is at end of module */
2179 if (within(addr, mod->module_init, mod->init_size))
2180 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002181 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2183
2184 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002185 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 for (i = 1; i < mod->num_symtab; i++) {
2187 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2188 continue;
2189
2190 /* We ignore unnamed symbols: they're uninformative
2191 * and inserted at a whim. */
2192 if (mod->symtab[i].st_value <= addr
2193 && mod->symtab[i].st_value > mod->symtab[best].st_value
2194 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2195 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2196 best = i;
2197 if (mod->symtab[i].st_value > addr
2198 && mod->symtab[i].st_value < nextval
2199 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2200 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2201 nextval = mod->symtab[i].st_value;
2202 }
2203
2204 if (!best)
2205 return NULL;
2206
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002207 if (size)
2208 *size = nextval - mod->symtab[best].st_value;
2209 if (offset)
2210 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return mod->strtab + mod->symtab[best].st_name;
2212}
2213
2214/* For kallsyms to ask for address resolution. NULL means not found.
2215 We don't lock, as this is used for oops resolution and races are a
2216 lesser concern. */
2217const char *module_address_lookup(unsigned long addr,
2218 unsigned long *size,
2219 unsigned long *offset,
2220 char **modname)
2221{
2222 struct module *mod;
2223
2224 list_for_each_entry(mod, &modules, list) {
2225 if (within(addr, mod->module_init, mod->init_size)
2226 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002227 if (modname)
2228 *modname = mod->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return get_ksymbol(mod, addr, size, offset);
2230 }
2231 }
2232 return NULL;
2233}
2234
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002235int lookup_module_symbol_name(unsigned long addr, char *symname)
2236{
2237 struct module *mod;
2238
2239 mutex_lock(&module_mutex);
2240 list_for_each_entry(mod, &modules, list) {
2241 if (within(addr, mod->module_init, mod->init_size) ||
2242 within(addr, mod->module_core, mod->core_size)) {
2243 const char *sym;
2244
2245 sym = get_ksymbol(mod, addr, NULL, NULL);
2246 if (!sym)
2247 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002248 strlcpy(symname, sym, KSYM_NAME_LEN);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002249 mutex_unlock(&module_mutex);
2250 return 0;
2251 }
2252 }
2253out:
2254 mutex_unlock(&module_mutex);
2255 return -ERANGE;
2256}
2257
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002258int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2259 unsigned long *offset, char *modname, char *name)
2260{
2261 struct module *mod;
2262
2263 mutex_lock(&module_mutex);
2264 list_for_each_entry(mod, &modules, list) {
2265 if (within(addr, mod->module_init, mod->init_size) ||
2266 within(addr, mod->module_core, mod->core_size)) {
2267 const char *sym;
2268
2269 sym = get_ksymbol(mod, addr, size, offset);
2270 if (!sym)
2271 goto out;
2272 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002273 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002274 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002275 strlcpy(name, sym, KSYM_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002276 mutex_unlock(&module_mutex);
2277 return 0;
2278 }
2279 }
2280out:
2281 mutex_unlock(&module_mutex);
2282 return -ERANGE;
2283}
2284
Alexey Dobriyanea078902007-05-08 00:28:39 -07002285int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2286 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 struct module *mod;
2289
Ashutosh Naik6389a382006-03-23 03:00:46 -08002290 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 list_for_each_entry(mod, &modules, list) {
2292 if (symnum < mod->num_symtab) {
2293 *value = mod->symtab[symnum].st_value;
2294 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002295 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002296 KSYM_NAME_LEN);
2297 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002298 *exported = is_exported(name, mod);
Ashutosh Naik6389a382006-03-23 03:00:46 -08002299 mutex_unlock(&module_mutex);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302 symnum -= mod->num_symtab;
2303 }
Ashutosh Naik6389a382006-03-23 03:00:46 -08002304 mutex_unlock(&module_mutex);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002305 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
2308static unsigned long mod_find_symname(struct module *mod, const char *name)
2309{
2310 unsigned int i;
2311
2312 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002313 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2314 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return mod->symtab[i].st_value;
2316 return 0;
2317}
2318
2319/* Look for this name: can be of form module:name. */
2320unsigned long module_kallsyms_lookup_name(const char *name)
2321{
2322 struct module *mod;
2323 char *colon;
2324 unsigned long ret = 0;
2325
2326 /* Don't lock: we're in enough trouble already. */
2327 if ((colon = strchr(name, ':')) != NULL) {
2328 *colon = '\0';
2329 if ((mod = find_module(name)) != NULL)
2330 ret = mod_find_symname(mod, colon+1);
2331 *colon = ':';
2332 } else {
2333 list_for_each_entry(mod, &modules, list)
2334 if ((ret = mod_find_symname(mod, name)) != 0)
2335 break;
2336 }
2337 return ret;
2338}
2339#endif /* CONFIG_KALLSYMS */
2340
2341/* Called by the /proc file system to return a list of modules. */
2342static void *m_start(struct seq_file *m, loff_t *pos)
2343{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002344 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002345 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2349{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002350 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
2353static void m_stop(struct seq_file *m, void *p)
2354{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002355 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002358static char *taint_flags(unsigned int taints, char *buf)
2359{
2360 int bx = 0;
2361
2362 if (taints) {
2363 buf[bx++] = '(';
2364 if (taints & TAINT_PROPRIETARY_MODULE)
2365 buf[bx++] = 'P';
2366 if (taints & TAINT_FORCED_MODULE)
2367 buf[bx++] = 'F';
2368 /*
2369 * TAINT_FORCED_RMMOD: could be added.
2370 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2371 * apply to modules.
2372 */
2373 buf[bx++] = ')';
2374 }
2375 buf[bx] = '\0';
2376
2377 return buf;
2378}
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380static int m_show(struct seq_file *m, void *p)
2381{
2382 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002383 char buf[8];
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 seq_printf(m, "%s %lu",
2386 mod->name, mod->init_size + mod->core_size);
2387 print_unload_info(m, mod);
2388
2389 /* Informative for users. */
2390 seq_printf(m, " %s",
2391 mod->state == MODULE_STATE_GOING ? "Unloading":
2392 mod->state == MODULE_STATE_COMING ? "Loading":
2393 "Live");
2394 /* Used by oprofile and other similar tools. */
2395 seq_printf(m, " 0x%p", mod->module_core);
2396
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002397 /* Taints info */
2398 if (mod->taints)
2399 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 seq_printf(m, "\n");
2402 return 0;
2403}
2404
2405/* Format: modulename size refcount deps address
2406
2407 Where refcount is a number or -, and deps is a comma-separated list
2408 of depends or -.
2409*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002410const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 .start = m_start,
2412 .next = m_next,
2413 .stop = m_stop,
2414 .show = m_show
2415};
2416
2417/* Given an address, look for it in the module exception tables. */
2418const struct exception_table_entry *search_module_extables(unsigned long addr)
2419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 const struct exception_table_entry *e = NULL;
2421 struct module *mod;
2422
Rusty Russell24da1cb2007-07-15 23:41:46 -07002423 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 list_for_each_entry(mod, &modules, list) {
2425 if (mod->num_exentries == 0)
2426 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 e = search_extable(mod->extable,
2429 mod->extable + mod->num_exentries - 1,
2430 addr);
2431 if (e)
2432 break;
2433 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002434 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002437 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return e;
2439}
2440
Ingo Molnar4d435f92006-07-03 00:24:24 -07002441/*
2442 * Is this a valid module address?
2443 */
2444int is_module_address(unsigned long addr)
2445{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002446 struct module *mod;
2447
Rusty Russell24da1cb2007-07-15 23:41:46 -07002448 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002449
2450 list_for_each_entry(mod, &modules, list) {
2451 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002452 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002453 return 1;
2454 }
2455 }
2456
Rusty Russell24da1cb2007-07-15 23:41:46 -07002457 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002458
2459 return 0;
2460}
2461
2462
Rusty Russell24da1cb2007-07-15 23:41:46 -07002463/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464struct module *__module_text_address(unsigned long addr)
2465{
2466 struct module *mod;
2467
2468 list_for_each_entry(mod, &modules, list)
2469 if (within(addr, mod->module_init, mod->init_text_size)
2470 || within(addr, mod->module_core, mod->core_text_size))
2471 return mod;
2472 return NULL;
2473}
2474
2475struct module *module_text_address(unsigned long addr)
2476{
2477 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Rusty Russell24da1cb2007-07-15 23:41:46 -07002479 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002481 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 return mod;
2484}
2485
2486/* Don't grab lock, we're oopsing. */
2487void print_modules(void)
2488{
2489 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002490 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 printk("Modules linked in:");
2493 list_for_each_entry(mod, &modules, list)
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002494 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 printk("\n");
2496}
2497
Randy Dunlapef665c12007-02-13 15:19:06 -08002498#ifdef CONFIG_SYSFS
Kay Sievers7f422e22006-12-20 10:39:33 +01002499static char *make_driver_name(struct device_driver *drv)
2500{
2501 char *driver_name;
2502
2503 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2504 GFP_KERNEL);
2505 if (!driver_name)
2506 return NULL;
2507
2508 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2509 return driver_name;
2510}
2511
Greg Kroah-Hartmanfe480a22007-01-15 11:50:02 -08002512static void module_create_drivers_dir(struct module_kobject *mk)
2513{
2514 if (!mk || mk->drivers_dir)
2515 return;
2516
2517 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520void module_add_driver(struct module *mod, struct device_driver *drv)
2521{
Kay Sievers7f422e22006-12-20 10:39:33 +01002522 char *driver_name;
Kay Sieverse17e0f52006-11-24 12:15:25 +01002523 int no_warn;
Kay Sieversf30c53a2007-01-15 20:22:02 +01002524 struct module_kobject *mk = NULL;
Kay Sieverse17e0f52006-11-24 12:15:25 +01002525
Kay Sieversf30c53a2007-01-15 20:22:02 +01002526 if (!drv)
2527 return;
2528
2529 if (mod)
2530 mk = &mod->mkobj;
2531 else if (drv->mod_name) {
2532 struct kobject *mkobj;
2533
2534 /* Lookup built-in module entry in /sys/modules */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07002535 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
Kay Sievers0c84ce22007-04-01 23:49:48 -07002536 if (mkobj) {
Kay Sieversf30c53a2007-01-15 20:22:02 +01002537 mk = container_of(mkobj, struct module_kobject, kobj);
Kay Sievers0c84ce22007-04-01 23:49:48 -07002538 /* remember our module structure */
2539 drv->mkobj = mk;
2540 /* kset_find_obj took a reference */
2541 kobject_put(mkobj);
2542 }
Kay Sieversf30c53a2007-01-15 20:22:02 +01002543 }
2544
2545 if (!mk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return;
2547
Kay Sieverse17e0f52006-11-24 12:15:25 +01002548 /* Don't check return codes; these calls are idempotent */
Kay Sieversf30c53a2007-01-15 20:22:02 +01002549 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
Kay Sievers7f422e22006-12-20 10:39:33 +01002550 driver_name = make_driver_name(drv);
2551 if (driver_name) {
Greg Kroah-Hartmanfe480a22007-01-15 11:50:02 -08002552 module_create_drivers_dir(mk);
Kay Sieversf30c53a2007-01-15 20:22:02 +01002553 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
Kay Sievers7f422e22006-12-20 10:39:33 +01002554 driver_name);
2555 kfree(driver_name);
2556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558EXPORT_SYMBOL(module_add_driver);
2559
2560void module_remove_driver(struct device_driver *drv)
2561{
Kay Sievers0c84ce22007-04-01 23:49:48 -07002562 struct module_kobject *mk = NULL;
Kay Sievers7f422e22006-12-20 10:39:33 +01002563 char *driver_name;
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (!drv)
2566 return;
Kay Sievers7f422e22006-12-20 10:39:33 +01002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 sysfs_remove_link(&drv->kobj, "module");
Kay Sievers0c84ce22007-04-01 23:49:48 -07002569
2570 if (drv->owner)
2571 mk = &drv->owner->mkobj;
2572 else if (drv->mkobj)
2573 mk = drv->mkobj;
2574 if (mk && mk->drivers_dir) {
Kay Sievers7f422e22006-12-20 10:39:33 +01002575 driver_name = make_driver_name(drv);
2576 if (driver_name) {
Kay Sievers0c84ce22007-04-01 23:49:48 -07002577 sysfs_remove_link(mk->drivers_dir, driver_name);
Kay Sievers7f422e22006-12-20 10:39:33 +01002578 kfree(driver_name);
2579 }
2580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582EXPORT_SYMBOL(module_remove_driver);
Randy Dunlapef665c12007-02-13 15:19:06 -08002583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585#ifdef CONFIG_MODVERSIONS
2586/* Generate the signature for struct module here, too, for modversions. */
2587void struct_module(struct module *mod) { return; }
2588EXPORT_SYMBOL(struct_module);
2589#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002590
2591#ifdef CONFIG_MARKERS
2592void module_update_markers(struct module *probe_module, int *refcount)
2593{
2594 struct module *mod;
2595
2596 mutex_lock(&module_mutex);
2597 list_for_each_entry(mod, &modules, list)
2598 if (!mod->taints)
2599 marker_update_probe_range(mod->markers,
2600 mod->markers + mod->num_markers,
2601 probe_module, refcount);
2602 mutex_unlock(&module_mutex);
2603}
2604#endif