blob: 3202c9950073ebc05fb232a625da27d2ec35eee8 [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
84/* We require a truly strong try_module_get() */
85static inline int strong_try_module_get(struct module *mod)
86{
87 if (mod && mod->state == MODULE_STATE_COMING)
88 return 0;
89 return try_module_get(mod);
90}
91
Florin Malitafa3ba2e82006-10-11 01:21:48 -070092static inline void add_taint_module(struct module *mod, unsigned flag)
93{
94 add_taint(flag);
95 mod->taints |= flag;
96}
97
Robert P. J. Day02a3e592007-05-09 07:26:28 +020098/*
99 * A thread that wants to hold a reference to a module only while it
100 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102void __module_put_and_exit(struct module *mod, long code)
103{
104 module_put(mod);
105 do_exit(code);
106}
107EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Find a module section: 0 means not found. */
110static unsigned int find_sec(Elf_Ehdr *hdr,
111 Elf_Shdr *sechdrs,
112 const char *secstrings,
113 const char *name)
114{
115 unsigned int i;
116
117 for (i = 1; i < hdr->e_shnum; i++)
118 /* Alloc bit cleared means "ignore it." */
119 if ((sechdrs[i].sh_flags & SHF_ALLOC)
120 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
121 return i;
122 return 0;
123}
124
125/* Provided by the linker */
126extern const struct kernel_symbol __start___ksymtab[];
127extern const struct kernel_symbol __stop___ksymtab[];
128extern const struct kernel_symbol __start___ksymtab_gpl[];
129extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800130extern const struct kernel_symbol __start___ksymtab_gpl_future[];
131extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700132extern const struct kernel_symbol __start___ksymtab_unused[];
133extern const struct kernel_symbol __stop___ksymtab_unused[];
134extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
135extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
136extern const struct kernel_symbol __start___ksymtab_gpl_future[];
137extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138extern const unsigned long __start___kcrctab[];
139extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800140extern const unsigned long __start___kcrctab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700141extern const unsigned long __start___kcrctab_unused[];
142extern const unsigned long __start___kcrctab_unused_gpl[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#ifndef CONFIG_MODVERSIONS
145#define symversion(base, idx) NULL
146#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800147#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
149
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100150/* lookup symbol in given range of kernel_symbols */
151static const struct kernel_symbol *lookup_symbol(const char *name,
152 const struct kernel_symbol *start,
153 const struct kernel_symbol *stop)
154{
155 const struct kernel_symbol *ks = start;
156 for (; ks < stop; ks++)
157 if (strcmp(ks->name, name) == 0)
158 return ks;
159 return NULL;
160}
161
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700162static void printk_unused_warning(const char *name)
163{
164 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
165 "however this module is using it.\n", name);
166 printk(KERN_WARNING "This symbol will go away in the future.\n");
167 printk(KERN_WARNING "Please evalute if this is the right api to use, "
168 "and if it really is, submit a report the linux kernel "
169 "mailinglist together with submitting your code for "
170 "inclusion.\n");
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* Find a symbol, return value, crc and module which owns it */
174static unsigned long __find_symbol(const char *name,
175 struct module **owner,
176 const unsigned long **crc,
177 int gplok)
178{
179 struct module *mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100180 const struct kernel_symbol *ks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Daniel Walker22a8bde2007-10-18 03:06:07 -0700182 /* Core kernel first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *owner = NULL;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100184 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
185 if (ks) {
186 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
187 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100190 ks = lookup_symbol(name, __start___ksymtab_gpl,
191 __stop___ksymtab_gpl);
192 if (ks) {
193 *crc = symversion(__start___kcrctab_gpl,
194 (ks - __start___ksymtab_gpl));
195 return ks->value;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800198 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
199 __stop___ksymtab_gpl_future);
200 if (ks) {
201 if (!gplok) {
202 printk(KERN_WARNING "Symbol %s is being used "
203 "by a non-GPL module, which will not "
204 "be allowed in the future\n", name);
205 printk(KERN_WARNING "Please see the file "
206 "Documentation/feature-removal-schedule.txt "
207 "in the kernel source tree for more "
208 "details.\n");
209 }
210 *crc = symversion(__start___kcrctab_gpl_future,
211 (ks - __start___ksymtab_gpl_future));
212 return ks->value;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700215 ks = lookup_symbol(name, __start___ksymtab_unused,
216 __stop___ksymtab_unused);
217 if (ks) {
218 printk_unused_warning(name);
219 *crc = symversion(__start___kcrctab_unused,
220 (ks - __start___ksymtab_unused));
221 return ks->value;
222 }
223
224 if (gplok)
225 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
226 __stop___ksymtab_unused_gpl);
227 if (ks) {
228 printk_unused_warning(name);
229 *crc = symversion(__start___kcrctab_unused_gpl,
230 (ks - __start___ksymtab_unused_gpl));
231 return ks->value;
232 }
233
Daniel Walker22a8bde2007-10-18 03:06:07 -0700234 /* Now try modules. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 list_for_each_entry(mod, &modules, list) {
236 *owner = mod;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100237 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
238 if (ks) {
239 *crc = symversion(mod->crcs, (ks - mod->syms));
240 return ks->value;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (gplok) {
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100244 ks = lookup_symbol(name, mod->gpl_syms,
245 mod->gpl_syms + mod->num_gpl_syms);
246 if (ks) {
247 *crc = symversion(mod->gpl_crcs,
248 (ks - mod->gpl_syms));
249 return ks->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 }
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700252 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
253 if (ks) {
254 printk_unused_warning(name);
255 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
256 return ks->value;
257 }
258
259 if (gplok) {
260 ks = lookup_symbol(name, mod->unused_gpl_syms,
261 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
262 if (ks) {
263 printk_unused_warning(name);
264 *crc = symversion(mod->unused_gpl_crcs,
265 (ks - mod->unused_gpl_syms));
266 return ks->value;
267 }
268 }
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800269 ks = lookup_symbol(name, mod->gpl_future_syms,
270 (mod->gpl_future_syms +
271 mod->num_gpl_future_syms));
272 if (ks) {
273 if (!gplok) {
274 printk(KERN_WARNING "Symbol %s is being used "
275 "by a non-GPL module, which will not "
276 "be allowed in the future\n", name);
277 printk(KERN_WARNING "Please see the file "
278 "Documentation/feature-removal-schedule.txt "
279 "in the kernel source tree for more "
280 "details.\n");
281 }
282 *crc = symversion(mod->gpl_future_crcs,
283 (ks - mod->gpl_future_syms));
284 return ks->value;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 DEBUGP("Failed to find symbol %s\n", name);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/* Search for module by name: must hold module_mutex. */
292static struct module *find_module(const char *name)
293{
294 struct module *mod;
295
296 list_for_each_entry(mod, &modules, list) {
297 if (strcmp(mod->name, name) == 0)
298 return mod;
299 }
300 return NULL;
301}
302
303#ifdef CONFIG_SMP
304/* Number of blocks used and allocated. */
305static unsigned int pcpu_num_used, pcpu_num_allocated;
306/* Size of each block. -ve means used. */
307static int *pcpu_size;
308
309static int split_block(unsigned int i, unsigned short size)
310{
311 /* Reallocation required? */
312 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700313 int *new;
314
315 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
316 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!new)
318 return 0;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pcpu_size = new;
322 }
323
324 /* Insert a new subblock */
325 memmove(&pcpu_size[i+1], &pcpu_size[i],
326 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
327 pcpu_num_used++;
328
329 pcpu_size[i+1] -= size;
330 pcpu_size[i] = size;
331 return 1;
332}
333
334static inline unsigned int block_size(int val)
335{
336 if (val < 0)
337 return -val;
338 return val;
339}
340
341/* Created by linker magic */
342extern char __per_cpu_start[], __per_cpu_end[];
343
Rusty Russell842bbaa2005-08-01 21:11:47 -0700344static void *percpu_modalloc(unsigned long size, unsigned long align,
345 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 unsigned long extra;
348 unsigned int i;
349 void *ptr;
350
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200351 if (align > PAGE_SIZE) {
352 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
353 name, align, PAGE_SIZE);
354 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 ptr = __per_cpu_start;
358 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
359 /* Extra for alignment requirement. */
360 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
361 BUG_ON(i == 0 && extra != 0);
362
363 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
364 continue;
365
366 /* Transfer extra to previous block. */
367 if (pcpu_size[i-1] < 0)
368 pcpu_size[i-1] -= extra;
369 else
370 pcpu_size[i-1] += extra;
371 pcpu_size[i] -= extra;
372 ptr += extra;
373
374 /* Split block if warranted */
375 if (pcpu_size[i] - size > sizeof(unsigned long))
376 if (!split_block(i, size))
377 return NULL;
378
379 /* Mark allocated */
380 pcpu_size[i] = -pcpu_size[i];
381 return ptr;
382 }
383
384 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
385 size);
386 return NULL;
387}
388
389static void percpu_modfree(void *freeme)
390{
391 unsigned int i;
392 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
393
394 /* First entry is core kernel percpu data. */
395 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
396 if (ptr == freeme) {
397 pcpu_size[i] = -pcpu_size[i];
398 goto free;
399 }
400 }
401 BUG();
402
403 free:
404 /* Merge with previous? */
405 if (pcpu_size[i-1] >= 0) {
406 pcpu_size[i-1] += pcpu_size[i];
407 pcpu_num_used--;
408 memmove(&pcpu_size[i], &pcpu_size[i+1],
409 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
410 i--;
411 }
412 /* Merge with next? */
413 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
414 pcpu_size[i] += pcpu_size[i+1];
415 pcpu_num_used--;
416 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
417 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
418 }
419}
420
421static unsigned int find_pcpusec(Elf_Ehdr *hdr,
422 Elf_Shdr *sechdrs,
423 const char *secstrings)
424{
425 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
426}
427
428static int percpu_modinit(void)
429{
430 pcpu_num_used = 2;
431 pcpu_num_allocated = 2;
432 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
433 GFP_KERNEL);
434 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200435 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Free room. */
437 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
438 if (pcpu_size[1] < 0) {
439 printk(KERN_ERR "No per-cpu room for modules.\n");
440 pcpu_num_used = 1;
441 }
442
443 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445__initcall(percpu_modinit);
446#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700447static inline void *percpu_modalloc(unsigned long size, unsigned long align,
448 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 return NULL;
451}
452static inline void percpu_modfree(void *pcpuptr)
453{
454 BUG();
455}
456static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
457 Elf_Shdr *sechdrs,
458 const char *secstrings)
459{
460 return 0;
461}
462static inline void percpu_modcopy(void *pcpudst, const void *src,
463 unsigned long size)
464{
465 /* pcpusec should be 0, and size of that section should be 0. */
466 BUG_ON(size != 0);
467}
468#endif /* CONFIG_SMP */
469
Matt Domschc988d2b2005-06-23 22:05:15 -0700470#define MODINFO_ATTR(field) \
471static void setup_modinfo_##field(struct module *mod, const char *s) \
472{ \
473 mod->field = kstrdup(s, GFP_KERNEL); \
474} \
475static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
476 struct module *mod, char *buffer) \
477{ \
478 return sprintf(buffer, "%s\n", mod->field); \
479} \
480static int modinfo_##field##_exists(struct module *mod) \
481{ \
482 return mod->field != NULL; \
483} \
484static void free_modinfo_##field(struct module *mod) \
485{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700486 kfree(mod->field); \
487 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700488} \
489static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900490 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700491 .show = show_modinfo_##field, \
492 .setup = setup_modinfo_##field, \
493 .test = modinfo_##field##_exists, \
494 .free = free_modinfo_##field, \
495};
496
497MODINFO_ATTR(version);
498MODINFO_ATTR(srcversion);
499
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800500#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* Init the unload section of the module. */
502static void module_unload_init(struct module *mod)
503{
504 unsigned int i;
505
506 INIT_LIST_HEAD(&mod->modules_which_use_me);
507 for (i = 0; i < NR_CPUS; i++)
508 local_set(&mod->ref[i].count, 0);
509 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700510 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Backwards compatibility macros put refcount during init. */
512 mod->waiter = current;
513}
514
515/* modules using other modules */
516struct module_use
517{
518 struct list_head list;
519 struct module *module_which_uses;
520};
521
522/* Does a already use b? */
523static int already_uses(struct module *a, struct module *b)
524{
525 struct module_use *use;
526
527 list_for_each_entry(use, &b->modules_which_use_me, list) {
528 if (use->module_which_uses == a) {
529 DEBUGP("%s uses %s!\n", a->name, b->name);
530 return 1;
531 }
532 }
533 DEBUGP("%s does not use %s!\n", a->name, b->name);
534 return 0;
535}
536
537/* Module a uses b */
538static int use_module(struct module *a, struct module *b)
539{
540 struct module_use *use;
Kay Sievers270a6c42007-01-18 13:26:15 +0100541 int no_warn;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (b == NULL || already_uses(a, b)) return 1;
544
545 if (!strong_try_module_get(b))
546 return 0;
547
548 DEBUGP("Allocating new usage for %s.\n", a->name);
549 use = kmalloc(sizeof(*use), GFP_ATOMIC);
550 if (!use) {
551 printk("%s: out of memory loading\n", a->name);
552 module_put(b);
553 return 0;
554 }
555
556 use->module_which_uses = a;
557 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100558 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 1;
560}
561
562/* Clear the unload stuff of the module. */
563static void module_unload_free(struct module *mod)
564{
565 struct module *i;
566
567 list_for_each_entry(i, &modules, list) {
568 struct module_use *use;
569
570 list_for_each_entry(use, &i->modules_which_use_me, list) {
571 if (use->module_which_uses == mod) {
572 DEBUGP("%s unusing %s\n", mod->name, i->name);
573 module_put(i);
574 list_del(&use->list);
575 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100576 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* There can be at most one match. */
578 break;
579 }
580 }
581 }
582}
583
584#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800585static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 int ret = (flags & O_TRUNC);
588 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800589 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return ret;
591}
592#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800593static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 return 0;
596}
597#endif /* CONFIG_MODULE_FORCE_UNLOAD */
598
599struct stopref
600{
601 struct module *mod;
602 int flags;
603 int *forced;
604};
605
606/* Whole machine is stopped with interrupts off when this runs. */
607static int __try_stop_module(void *_sref)
608{
609 struct stopref *sref = _sref;
610
611 /* If it's not unused, quit unless we are told to block. */
612 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800613 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EWOULDBLOCK;
615 }
616
617 /* Mark it as dying. */
618 sref->mod->state = MODULE_STATE_GOING;
619 return 0;
620}
621
622static int try_stop_module(struct module *mod, int flags, int *forced)
623{
624 struct stopref sref = { mod, flags, forced };
625
626 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
627}
628
629unsigned int module_refcount(struct module *mod)
630{
631 unsigned int i, total = 0;
632
633 for (i = 0; i < NR_CPUS; i++)
634 total += local_read(&mod->ref[i].count);
635 return total;
636}
637EXPORT_SYMBOL(module_refcount);
638
639/* This exists whether we can unload or not */
640static void free_module(struct module *mod);
641
642static void wait_for_zero_refcount(struct module *mod)
643{
644 /* Since we might sleep for some time, drop the semaphore first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800645 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 for (;;) {
647 DEBUGP("Looking at refcount...\n");
648 set_current_state(TASK_UNINTERRUPTIBLE);
649 if (module_refcount(mod) == 0)
650 break;
651 schedule();
652 }
653 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800654 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800657asmlinkage long
658sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800661 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int ret, forced = 0;
663
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800664 if (!capable(CAP_SYS_MODULE))
665 return -EPERM;
666
667 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
668 return -EFAULT;
669 name[MODULE_NAME_LEN-1] = '\0';
670
Ashutosh Naik6389a382006-03-23 03:00:46 -0800671 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -EINTR;
673
674 mod = find_module(name);
675 if (!mod) {
676 ret = -ENOENT;
677 goto out;
678 }
679
680 if (!list_empty(&mod->modules_which_use_me)) {
681 /* Other modules depend on us: get rid of them first. */
682 ret = -EWOULDBLOCK;
683 goto out;
684 }
685
686 /* Doing init or already dying? */
687 if (mod->state != MODULE_STATE_LIVE) {
688 /* FIXME: if (force), slam module count and wake up
689 waiter --RR */
690 DEBUGP("%s already dying\n", mod->name);
691 ret = -EBUSY;
692 goto out;
693 }
694
695 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700696 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800697 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (!forced) {
699 /* This module can't be removed */
700 ret = -EBUSY;
701 goto out;
702 }
703 }
704
705 /* Set this up before setting mod->state */
706 mod->waiter = current;
707
708 /* Stop the machine so refcounts can't move and disable module. */
709 ret = try_stop_module(mod, flags, &forced);
710 if (ret != 0)
711 goto out;
712
713 /* Never wait if forced. */
714 if (!forced && module_refcount(mod) != 0)
715 wait_for_zero_refcount(mod);
716
717 /* Final destruction now noone is using it. */
718 if (mod->exit != NULL) {
Ashutosh Naik6389a382006-03-23 03:00:46 -0800719 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 mod->exit();
Ashutosh Naik6389a382006-03-23 03:00:46 -0800721 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 free_module(mod);
724
725 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800726 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return ret;
728}
729
730static void print_unload_info(struct seq_file *m, struct module *mod)
731{
732 struct module_use *use;
733 int printed_something = 0;
734
735 seq_printf(m, " %u ", module_refcount(mod));
736
737 /* Always include a trailing , so userspace can differentiate
738 between this and the old multi-field proc format. */
739 list_for_each_entry(use, &mod->modules_which_use_me, list) {
740 printed_something = 1;
741 seq_printf(m, "%s,", use->module_which_uses->name);
742 }
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (mod->init != NULL && mod->exit == NULL) {
745 printed_something = 1;
746 seq_printf(m, "[permanent],");
747 }
748
749 if (!printed_something)
750 seq_printf(m, "-");
751}
752
753void __symbol_put(const char *symbol)
754{
755 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 const unsigned long *crc;
757
Rusty Russell24da1cb2007-07-15 23:41:46 -0700758 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!__find_symbol(symbol, &owner, &crc, 1))
760 BUG();
761 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700762 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764EXPORT_SYMBOL(__symbol_put);
765
766void symbol_put_addr(void *addr)
767{
Trent Piepho5e376612006-05-15 09:44:06 -0700768 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Trent Piepho5e376612006-05-15 09:44:06 -0700770 if (core_kernel_text((unsigned long)addr))
771 return;
772
773 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700775 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777EXPORT_SYMBOL_GPL(symbol_put_addr);
778
779static ssize_t show_refcnt(struct module_attribute *mattr,
780 struct module *mod, char *buffer)
781{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400782 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900786 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 .show = show_refcnt,
788};
789
Al Virof6a57032006-10-18 01:47:25 -0400790void module_put(struct module *module)
791{
792 if (module) {
793 unsigned int cpu = get_cpu();
794 local_dec(&module->ref[cpu].count);
795 /* Maybe they're waiting for us to drop reference? */
796 if (unlikely(!module_is_live(module)))
797 wake_up_process(module->waiter);
798 put_cpu();
799 }
800}
801EXPORT_SYMBOL(module_put);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#else /* !CONFIG_MODULE_UNLOAD */
804static void print_unload_info(struct seq_file *m, struct module *mod)
805{
806 /* We don't know the usage count, or what modules are using. */
807 seq_printf(m, " - -");
808}
809
810static inline void module_unload_free(struct module *mod)
811{
812}
813
814static inline int use_module(struct module *a, struct module *b)
815{
816 return strong_try_module_get(b);
817}
818
819static inline void module_unload_init(struct module *mod)
820{
821}
822#endif /* CONFIG_MODULE_UNLOAD */
823
Kay Sievers1f717402006-11-24 12:15:25 +0100824static ssize_t show_initstate(struct module_attribute *mattr,
825 struct module *mod, char *buffer)
826{
827 const char *state = "unknown";
828
829 switch (mod->state) {
830 case MODULE_STATE_LIVE:
831 state = "live";
832 break;
833 case MODULE_STATE_COMING:
834 state = "coming";
835 break;
836 case MODULE_STATE_GOING:
837 state = "going";
838 break;
839 }
840 return sprintf(buffer, "%s\n", state);
841}
842
843static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900844 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100845 .show = show_initstate,
846};
847
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800848static struct module_attribute *modinfo_attrs[] = {
849 &modinfo_version,
850 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100851 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800852#ifdef CONFIG_MODULE_UNLOAD
853 &refcnt,
854#endif
855 NULL,
856};
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static const char vermagic[] = VERMAGIC_STRING;
859
860#ifdef CONFIG_MODVERSIONS
861static int check_version(Elf_Shdr *sechdrs,
862 unsigned int versindex,
863 const char *symname,
864 struct module *mod,
865 const unsigned long *crc)
866{
867 unsigned int i, num_versions;
868 struct modversion_info *versions;
869
870 /* Exporting module didn't supply crcs? OK, we're already tainted. */
871 if (!crc)
872 return 1;
873
874 versions = (void *) sechdrs[versindex].sh_addr;
875 num_versions = sechdrs[versindex].sh_size
876 / sizeof(struct modversion_info);
877
878 for (i = 0; i < num_versions; i++) {
879 if (strcmp(versions[i].name, symname) != 0)
880 continue;
881
882 if (versions[i].crc == *crc)
883 return 1;
884 printk("%s: disagrees about version of symbol %s\n",
885 mod->name, symname);
886 DEBUGP("Found checksum %lX vs module %lX\n",
887 *crc, versions[i].crc);
888 return 0;
889 }
890 /* Not in module's version table. OK, but that taints the kernel. */
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700891 if (!(tainted & TAINT_FORCED_MODULE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 printk("%s: no version for \"%s\" found: kernel tainted.\n",
893 mod->name, symname);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700894 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 1;
896}
897
898static inline int check_modstruct_version(Elf_Shdr *sechdrs,
899 unsigned int versindex,
900 struct module *mod)
901{
902 const unsigned long *crc;
903 struct module *owner;
904
905 if (!__find_symbol("struct_module", &owner, &crc, 1))
906 BUG();
907 return check_version(sechdrs, versindex, "struct_module", mod,
908 crc);
909}
910
911/* First part is kernel version, which we ignore. */
912static inline int same_magic(const char *amagic, const char *bmagic)
913{
914 amagic += strcspn(amagic, " ");
915 bmagic += strcspn(bmagic, " ");
916 return strcmp(amagic, bmagic) == 0;
917}
918#else
919static inline int check_version(Elf_Shdr *sechdrs,
920 unsigned int versindex,
921 const char *symname,
922 struct module *mod,
923 const unsigned long *crc)
924{
925 return 1;
926}
927
928static inline int check_modstruct_version(Elf_Shdr *sechdrs,
929 unsigned int versindex,
930 struct module *mod)
931{
932 return 1;
933}
934
935static inline int same_magic(const char *amagic, const char *bmagic)
936{
937 return strcmp(amagic, bmagic) == 0;
938}
939#endif /* CONFIG_MODVERSIONS */
940
941/* Resolve a symbol for this module. I.e. if we find one, record usage.
942 Must be holding module_mutex. */
943static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
944 unsigned int versindex,
945 const char *name,
946 struct module *mod)
947{
948 struct module *owner;
949 unsigned long ret;
950 const unsigned long *crc;
951
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700952 ret = __find_symbol(name, &owner, &crc,
953 !(mod->taints & TAINT_PROPRIETARY_MODULE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (ret) {
955 /* use_module can fail due to OOM, or module unloading */
956 if (!check_version(sechdrs, versindex, name, mod, crc) ||
957 !use_module(mod, owner))
958 ret = 0;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return ret;
961}
962
963
964/*
965 * /sys/module/foo/sections stuff
966 * J. Corbet <corbet@lwn.net>
967 */
968#ifdef CONFIG_KALLSYMS
969static ssize_t module_sect_show(struct module_attribute *mattr,
970 struct module *mod, char *buf)
971{
972 struct module_sect_attr *sattr =
973 container_of(mattr, struct module_sect_attr, mattr);
974 return sprintf(buf, "0x%lx\n", sattr->address);
975}
976
Ian S. Nelson04b1db92006-09-29 02:01:31 -0700977static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
978{
979 int section;
980
981 for (section = 0; section < sect_attrs->nsections; section++)
982 kfree(sect_attrs->attrs[section].name);
983 kfree(sect_attrs);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static void add_sect_attrs(struct module *mod, unsigned int nsect,
987 char *secstrings, Elf_Shdr *sechdrs)
988{
989 unsigned int nloaded = 0, i, size[2];
990 struct module_sect_attrs *sect_attrs;
991 struct module_sect_attr *sattr;
992 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Count loaded sections and allocate structures */
995 for (i = 0; i < nsect; i++)
996 if (sechdrs[i].sh_flags & SHF_ALLOC)
997 nloaded++;
998 size[0] = ALIGN(sizeof(*sect_attrs)
999 + nloaded * sizeof(sect_attrs->attrs[0]),
1000 sizeof(sect_attrs->grp.attrs[0]));
1001 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001002 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1003 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005
1006 /* Setup section attributes. */
1007 sect_attrs->grp.name = "sections";
1008 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1009
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001010 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sattr = &sect_attrs->attrs[0];
1012 gattr = &sect_attrs->grp.attrs[0];
1013 for (i = 0; i < nsect; i++) {
1014 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1015 continue;
1016 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001017 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1018 GFP_KERNEL);
1019 if (sattr->name == NULL)
1020 goto out;
1021 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 sattr->mattr.show = module_sect_show;
1023 sattr->mattr.store = NULL;
1024 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sattr->mattr.attr.mode = S_IRUGO;
1026 *(gattr++) = &(sattr++)->mattr.attr;
1027 }
1028 *gattr = NULL;
1029
1030 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1031 goto out;
1032
1033 mod->sect_attrs = sect_attrs;
1034 return;
1035 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001036 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static void remove_sect_attrs(struct module *mod)
1040{
1041 if (mod->sect_attrs) {
1042 sysfs_remove_group(&mod->mkobj.kobj,
1043 &mod->sect_attrs->grp);
1044 /* We are positive that no one is using any sect attrs
1045 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001046 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 mod->sect_attrs = NULL;
1048 }
1049}
1050
Roland McGrath6d760132007-10-16 23:26:40 -07001051/*
1052 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1053 */
1054
1055struct module_notes_attrs {
1056 struct kobject *dir;
1057 unsigned int notes;
1058 struct bin_attribute attrs[0];
1059};
1060
1061static ssize_t module_notes_read(struct kobject *kobj,
1062 struct bin_attribute *bin_attr,
1063 char *buf, loff_t pos, size_t count)
1064{
1065 /*
1066 * The caller checked the pos and count against our size.
1067 */
1068 memcpy(buf, bin_attr->private + pos, count);
1069 return count;
1070}
1071
1072static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1073 unsigned int i)
1074{
1075 if (notes_attrs->dir) {
1076 while (i-- > 0)
1077 sysfs_remove_bin_file(notes_attrs->dir,
1078 &notes_attrs->attrs[i]);
1079 kobject_del(notes_attrs->dir);
1080 }
1081 kfree(notes_attrs);
1082}
1083
1084static void add_notes_attrs(struct module *mod, unsigned int nsect,
1085 char *secstrings, Elf_Shdr *sechdrs)
1086{
1087 unsigned int notes, loaded, i;
1088 struct module_notes_attrs *notes_attrs;
1089 struct bin_attribute *nattr;
1090
1091 /* Count notes sections and allocate structures. */
1092 notes = 0;
1093 for (i = 0; i < nsect; i++)
1094 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1095 (sechdrs[i].sh_type == SHT_NOTE))
1096 ++notes;
1097
1098 if (notes == 0)
1099 return;
1100
1101 notes_attrs = kzalloc(sizeof(*notes_attrs)
1102 + notes * sizeof(notes_attrs->attrs[0]),
1103 GFP_KERNEL);
1104 if (notes_attrs == NULL)
1105 return;
1106
1107 notes_attrs->notes = notes;
1108 nattr = &notes_attrs->attrs[0];
1109 for (loaded = i = 0; i < nsect; ++i) {
1110 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1111 continue;
1112 if (sechdrs[i].sh_type == SHT_NOTE) {
1113 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1114 nattr->attr.mode = S_IRUGO;
1115 nattr->size = sechdrs[i].sh_size;
1116 nattr->private = (void *) sechdrs[i].sh_addr;
1117 nattr->read = module_notes_read;
1118 ++nattr;
1119 }
1120 ++loaded;
1121 }
1122
1123 notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes");
1124 if (!notes_attrs->dir)
1125 goto out;
1126
1127 for (i = 0; i < notes; ++i)
1128 if (sysfs_create_bin_file(notes_attrs->dir,
1129 &notes_attrs->attrs[i]))
1130 goto out;
1131
1132 mod->notes_attrs = notes_attrs;
1133 return;
1134
1135 out:
1136 free_notes_attrs(notes_attrs, i);
1137}
1138
1139static void remove_notes_attrs(struct module *mod)
1140{
1141 if (mod->notes_attrs)
1142 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1148 char *sectstrings, Elf_Shdr *sechdrs)
1149{
1150}
1151
1152static inline void remove_sect_attrs(struct module *mod)
1153{
1154}
Roland McGrath6d760132007-10-16 23:26:40 -07001155
1156static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1157 char *sectstrings, Elf_Shdr *sechdrs)
1158{
1159}
1160
1161static inline void remove_notes_attrs(struct module *mod)
1162{
1163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164#endif /* CONFIG_KALLSYMS */
1165
Randy Dunlapef665c12007-02-13 15:19:06 -08001166#ifdef CONFIG_SYSFS
1167int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001168{
1169 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001170 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001171 int error = 0;
1172 int i;
1173
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001174 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1175 (ARRAY_SIZE(modinfo_attrs) + 1)),
1176 GFP_KERNEL);
1177 if (!mod->modinfo_attrs)
1178 return -ENOMEM;
1179
1180 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001181 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1182 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001183 (attr->test && attr->test(mod))) {
1184 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001185 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1186 ++temp_attr;
1187 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001188 }
1189 return error;
1190}
1191
Randy Dunlapef665c12007-02-13 15:19:06 -08001192void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001193{
1194 struct module_attribute *attr;
1195 int i;
1196
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001197 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1198 /* pick a field to test for end of list */
1199 if (!attr->attr.name)
1200 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001201 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001202 if (attr->free)
1203 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001204 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001205 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001206}
Randy Dunlapef665c12007-02-13 15:19:06 -08001207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Randy Dunlapef665c12007-02-13 15:19:06 -08001209#ifdef CONFIG_SYSFS
1210int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 int err;
1213
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001214 if (!module_sysfs_initialized) {
1215 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001216 mod->name);
1217 err = -EINVAL;
1218 goto out;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1221 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1222 if (err)
1223 goto out;
1224 kobj_set_kset_s(&mod->mkobj, module_subsys);
1225 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001226
Kay Sieverse17e0f52006-11-24 12:15:25 +01001227 kobject_init(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001228
1229out:
1230 return err;
1231}
1232
Randy Dunlapef665c12007-02-13 15:19:06 -08001233int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001234 struct kernel_param *kparam,
1235 unsigned int num_params)
1236{
1237 int err;
1238
1239 /* delay uevent until full sysfs population */
Kay Sieverse17e0f52006-11-24 12:15:25 +01001240 err = kobject_add(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (err)
1242 goto out;
1243
Kay Sievers270a6c42007-01-18 13:26:15 +01001244 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
Akinobu Mita240936e2007-04-26 00:12:09 -07001245 if (!mod->holders_dir) {
1246 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001247 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001248 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 err = module_param_sysfs_setup(mod, kparam, num_params);
1251 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001252 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Matt Domschc988d2b2005-06-23 22:05:15 -07001254 err = module_add_modinfo_attrs(mod);
1255 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001256 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001257
Kay Sieverse17e0f52006-11-24 12:15:25 +01001258 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260
Kay Sieverse17e0f52006-11-24 12:15:25 +01001261out_unreg_param:
1262 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001263out_unreg_holders:
1264 kobject_unregister(mod->holders_dir);
1265out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001266 kobject_del(&mod->mkobj.kobj);
1267 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268out:
1269 return err;
1270}
Randy Dunlapef665c12007-02-13 15:19:06 -08001271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273static void mod_kobject_remove(struct module *mod)
1274{
Matt Domschc988d2b2005-06-23 22:05:15 -07001275 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 module_param_sysfs_remove(mod);
Mariusz Kozlowskib92be9f2007-02-14 21:03:39 +01001277 kobject_unregister(mod->mkobj.drivers_dir);
1278 kobject_unregister(mod->holders_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 kobject_unregister(&mod->mkobj.kobj);
1280}
1281
1282/*
1283 * unlink the module with the whole machine is stopped with interrupts off
1284 * - this defends against kallsyms not taking locks
1285 */
1286static int __unlink_module(void *_mod)
1287{
1288 struct module *mod = _mod;
1289 list_del(&mod->list);
1290 return 0;
1291}
1292
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001293/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static void free_module(struct module *mod)
1295{
1296 /* Delete from various lists */
1297 stop_machine_run(__unlink_module, mod, NR_CPUS);
Roland McGrath6d760132007-10-16 23:26:40 -07001298 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 remove_sect_attrs(mod);
1300 mod_kobject_remove(mod);
1301
Jan Beulich4552d5d2006-06-26 13:57:28 +02001302 unwind_remove_table(mod->unwind_info, 0);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* Arch-specific cleanup. */
1305 module_arch_cleanup(mod);
1306
1307 /* Module unload stuff */
1308 module_unload_free(mod);
1309
1310 /* This may be NULL, but that's OK */
1311 module_free(mod, mod->module_init);
1312 kfree(mod->args);
1313 if (mod->percpu)
1314 percpu_modfree(mod->percpu);
1315
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001316 /* Free lock-classes: */
1317 lockdep_free_key_range(mod->module_core, mod->core_size);
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* Finally, free the core (containing the module structure) */
1320 module_free(mod, mod->module_core);
1321}
1322
1323void *__symbol_get(const char *symbol)
1324{
1325 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001326 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 const unsigned long *crc;
1328
Rusty Russell24da1cb2007-07-15 23:41:46 -07001329 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 value = __find_symbol(symbol, &owner, &crc, 1);
1331 if (value && !strong_try_module_get(owner))
1332 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001333 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 return (void *)value;
1336}
1337EXPORT_SYMBOL_GPL(__symbol_get);
1338
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001339/*
1340 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001341 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001342 */
1343static int verify_export_symbols(struct module *mod)
1344{
1345 const char *name = NULL;
1346 unsigned long i, ret = 0;
1347 struct module *owner;
1348 const unsigned long *crc;
1349
1350 for (i = 0; i < mod->num_syms; i++)
Daniel Walker22a8bde2007-10-18 03:06:07 -07001351 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001352 name = mod->syms[i].name;
1353 ret = -ENOEXEC;
1354 goto dup;
1355 }
1356
1357 for (i = 0; i < mod->num_gpl_syms; i++)
Daniel Walker22a8bde2007-10-18 03:06:07 -07001358 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001359 name = mod->gpl_syms[i].name;
1360 ret = -ENOEXEC;
1361 goto dup;
1362 }
1363
1364dup:
1365 if (ret)
1366 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1367 mod->name, name, module_name(owner));
1368
1369 return ret;
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/* Change all symbols so that sh_value encodes the pointer directly. */
1373static int simplify_symbols(Elf_Shdr *sechdrs,
1374 unsigned int symindex,
1375 const char *strtab,
1376 unsigned int versindex,
1377 unsigned int pcpuindex,
1378 struct module *mod)
1379{
1380 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1381 unsigned long secbase;
1382 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1383 int ret = 0;
1384
1385 for (i = 1; i < n; i++) {
1386 switch (sym[i].st_shndx) {
1387 case SHN_COMMON:
1388 /* We compiled with -fno-common. These are not
1389 supposed to happen. */
1390 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1391 printk("%s: please compile with -fno-common\n",
1392 mod->name);
1393 ret = -ENOEXEC;
1394 break;
1395
1396 case SHN_ABS:
1397 /* Don't need to do anything */
1398 DEBUGP("Absolute symbol: 0x%08lx\n",
1399 (long)sym[i].st_value);
1400 break;
1401
1402 case SHN_UNDEF:
1403 sym[i].st_value
1404 = resolve_symbol(sechdrs, versindex,
1405 strtab + sym[i].st_name, mod);
1406
1407 /* Ok if resolved. */
1408 if (sym[i].st_value != 0)
1409 break;
1410 /* Ok if weak. */
1411 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1412 break;
1413
1414 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1415 mod->name, strtab + sym[i].st_name);
1416 ret = -ENOENT;
1417 break;
1418
1419 default:
1420 /* Divert to percpu allocation if a percpu var. */
1421 if (sym[i].st_shndx == pcpuindex)
1422 secbase = (unsigned long)mod->percpu;
1423 else
1424 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1425 sym[i].st_value += secbase;
1426 break;
1427 }
1428 }
1429
1430 return ret;
1431}
1432
1433/* Update size with this section: return offset. */
1434static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1435{
1436 long ret;
1437
1438 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1439 *size = ret + sechdr->sh_size;
1440 return ret;
1441}
1442
1443/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1444 might -- code, read-only data, read-write data, small data. Tally
1445 sizes, and place the offsets into sh_entsize fields: high bit means it
1446 belongs in init. */
1447static void layout_sections(struct module *mod,
1448 const Elf_Ehdr *hdr,
1449 Elf_Shdr *sechdrs,
1450 const char *secstrings)
1451{
1452 static unsigned long const masks[][2] = {
1453 /* NOTE: all executable code must be the first section
1454 * in this array; otherwise modify the text_size
1455 * finder in the two loops below */
1456 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1457 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1458 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1459 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1460 };
1461 unsigned int m, i;
1462
1463 for (i = 0; i < hdr->e_shnum; i++)
1464 sechdrs[i].sh_entsize = ~0UL;
1465
1466 DEBUGP("Core section allocation order:\n");
1467 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1468 for (i = 0; i < hdr->e_shnum; ++i) {
1469 Elf_Shdr *s = &sechdrs[i];
1470
1471 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1472 || (s->sh_flags & masks[m][1])
1473 || s->sh_entsize != ~0UL
1474 || strncmp(secstrings + s->sh_name,
1475 ".init", 5) == 0)
1476 continue;
1477 s->sh_entsize = get_offset(&mod->core_size, s);
1478 DEBUGP("\t%s\n", secstrings + s->sh_name);
1479 }
1480 if (m == 0)
1481 mod->core_text_size = mod->core_size;
1482 }
1483
1484 DEBUGP("Init section allocation order:\n");
1485 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1486 for (i = 0; i < hdr->e_shnum; ++i) {
1487 Elf_Shdr *s = &sechdrs[i];
1488
1489 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1490 || (s->sh_flags & masks[m][1])
1491 || s->sh_entsize != ~0UL
1492 || strncmp(secstrings + s->sh_name,
1493 ".init", 5) != 0)
1494 continue;
1495 s->sh_entsize = (get_offset(&mod->init_size, s)
1496 | INIT_OFFSET_MASK);
1497 DEBUGP("\t%s\n", secstrings + s->sh_name);
1498 }
1499 if (m == 0)
1500 mod->init_text_size = mod->init_size;
1501 }
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504static void set_license(struct module *mod, const char *license)
1505{
1506 if (!license)
1507 license = "unspecified";
1508
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001509 if (!license_is_gpl_compatible(license)) {
1510 if (!(tainted & TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001511 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001512 "kernel.\n", mod->name, license);
1513 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515}
1516
1517/* Parse tag=value strings from .modinfo section */
1518static char *next_string(char *string, unsigned long *secsize)
1519{
1520 /* Skip non-zero chars */
1521 while (string[0]) {
1522 string++;
1523 if ((*secsize)-- <= 1)
1524 return NULL;
1525 }
1526
1527 /* Skip any zero padding. */
1528 while (!string[0]) {
1529 string++;
1530 if ((*secsize)-- <= 1)
1531 return NULL;
1532 }
1533 return string;
1534}
1535
1536static char *get_modinfo(Elf_Shdr *sechdrs,
1537 unsigned int info,
1538 const char *tag)
1539{
1540 char *p;
1541 unsigned int taglen = strlen(tag);
1542 unsigned long size = sechdrs[info].sh_size;
1543
1544 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1545 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1546 return p + taglen + 1;
1547 }
1548 return NULL;
1549}
1550
Matt Domschc988d2b2005-06-23 22:05:15 -07001551static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1552 unsigned int infoindex)
1553{
1554 struct module_attribute *attr;
1555 int i;
1556
1557 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1558 if (attr->setup)
1559 attr->setup(mod,
1560 get_modinfo(sechdrs,
1561 infoindex,
1562 attr->attr.name));
1563 }
1564}
Matt Domschc988d2b2005-06-23 22:05:15 -07001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566#ifdef CONFIG_KALLSYMS
Alexey Dobriyanea078902007-05-08 00:28:39 -07001567static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001569 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1570 return 1;
1571 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001572 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001574 else
1575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578/* As per nm */
1579static char elf_type(const Elf_Sym *sym,
1580 Elf_Shdr *sechdrs,
1581 const char *secstrings,
1582 struct module *mod)
1583{
1584 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1585 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1586 return 'v';
1587 else
1588 return 'w';
1589 }
1590 if (sym->st_shndx == SHN_UNDEF)
1591 return 'U';
1592 if (sym->st_shndx == SHN_ABS)
1593 return 'a';
1594 if (sym->st_shndx >= SHN_LORESERVE)
1595 return '?';
1596 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1597 return 't';
1598 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1599 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1600 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1601 return 'r';
1602 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1603 return 'g';
1604 else
1605 return 'd';
1606 }
1607 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1608 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1609 return 's';
1610 else
1611 return 'b';
1612 }
1613 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1614 ".debug", strlen(".debug")) == 0)
1615 return 'n';
1616 return '?';
1617}
1618
1619static void add_kallsyms(struct module *mod,
1620 Elf_Shdr *sechdrs,
1621 unsigned int symindex,
1622 unsigned int strindex,
1623 const char *secstrings)
1624{
1625 unsigned int i;
1626
1627 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1628 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1629 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1630
1631 /* Set types up while we still have access to sections. */
1632 for (i = 0; i < mod->num_symtab; i++)
1633 mod->symtab[i].st_info
1634 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1635}
1636#else
1637static inline void add_kallsyms(struct module *mod,
1638 Elf_Shdr *sechdrs,
1639 unsigned int symindex,
1640 unsigned int strindex,
1641 const char *secstrings)
1642{
1643}
1644#endif /* CONFIG_KALLSYMS */
1645
1646/* Allocate and load the module: note that size of section 0 is always
1647 zero, and we rely on this for optional sections. */
1648static struct module *load_module(void __user *umod,
1649 unsigned long len,
1650 const char __user *uargs)
1651{
1652 Elf_Ehdr *hdr;
1653 Elf_Shdr *sechdrs;
1654 char *secstrings, *args, *modmagic, *strtab = NULL;
Andrew Morton84860f92006-06-28 04:26:46 -07001655 unsigned int i;
1656 unsigned int symindex = 0;
1657 unsigned int strindex = 0;
1658 unsigned int setupindex;
1659 unsigned int exindex;
1660 unsigned int exportindex;
1661 unsigned int modindex;
1662 unsigned int obsparmindex;
1663 unsigned int infoindex;
1664 unsigned int gplindex;
1665 unsigned int crcindex;
1666 unsigned int gplcrcindex;
1667 unsigned int versindex;
1668 unsigned int pcpuindex;
1669 unsigned int gplfutureindex;
1670 unsigned int gplfuturecrcindex;
1671 unsigned int unwindex = 0;
1672 unsigned int unusedindex;
1673 unsigned int unusedcrcindex;
1674 unsigned int unusedgplindex;
1675 unsigned int unusedgplcrcindex;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001676 unsigned int markersindex;
1677 unsigned int markersstringsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct module *mod;
1679 long err = 0;
1680 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1681 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001682 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1685 umod, len, uargs);
1686 if (len < sizeof(*hdr))
1687 return ERR_PTR(-ENOEXEC);
1688
1689 /* Suck in entire file: we'll want most of it. */
1690 /* vmalloc barfs on "unusual" numbers. Check here */
1691 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1692 return ERR_PTR(-ENOMEM);
1693 if (copy_from_user(hdr, umod, len) != 0) {
1694 err = -EFAULT;
1695 goto free_hdr;
1696 }
1697
1698 /* Sanity checks against insmoding binaries or wrong arch,
1699 weird elf version */
1700 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1701 || hdr->e_type != ET_REL
1702 || !elf_check_arch(hdr)
1703 || hdr->e_shentsize != sizeof(*sechdrs)) {
1704 err = -ENOEXEC;
1705 goto free_hdr;
1706 }
1707
1708 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1709 goto truncated;
1710
1711 /* Convenience variables */
1712 sechdrs = (void *)hdr + hdr->e_shoff;
1713 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1714 sechdrs[0].sh_addr = 0;
1715
1716 for (i = 1; i < hdr->e_shnum; i++) {
1717 if (sechdrs[i].sh_type != SHT_NOBITS
1718 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1719 goto truncated;
1720
1721 /* Mark all sections sh_addr with their address in the
1722 temporary image. */
1723 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1724
1725 /* Internal symbols and strings. */
1726 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1727 symindex = i;
1728 strindex = sechdrs[i].sh_link;
1729 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1730 }
1731#ifndef CONFIG_MODULE_UNLOAD
1732 /* Don't load .exit sections */
1733 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1734 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1735#endif
1736 }
1737
1738 modindex = find_sec(hdr, sechdrs, secstrings,
1739 ".gnu.linkonce.this_module");
1740 if (!modindex) {
1741 printk(KERN_WARNING "No module found in object\n");
1742 err = -ENOEXEC;
1743 goto free_hdr;
1744 }
1745 mod = (void *)sechdrs[modindex].sh_addr;
1746
1747 if (symindex == 0) {
1748 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1749 mod->name);
1750 err = -ENOEXEC;
1751 goto free_hdr;
1752 }
1753
1754 /* Optional sections */
1755 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1756 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001757 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001758 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1759 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1761 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001762 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001763 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1764 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1766 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1767 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1768 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1769 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1770 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001771#ifdef ARCH_UNWIND_SECTION_NAME
1772 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* Don't keep modinfo section */
1776 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1777#ifdef CONFIG_KALLSYMS
1778 /* Keep symbol and string tables for decoding later. */
1779 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1780 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1781#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001782 if (unwindex)
1783 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 /* Check module struct version now, before we try to use module. */
1786 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1787 err = -ENOEXEC;
1788 goto free_hdr;
1789 }
1790
1791 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1792 /* This is allowed: modprobe --force will invalidate it. */
1793 if (!modmagic) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001794 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1796 mod->name);
1797 } else if (!same_magic(modmagic, vermagic)) {
1798 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1799 mod->name, modmagic, vermagic);
1800 err = -ENOEXEC;
1801 goto free_hdr;
1802 }
1803
1804 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001805 args = strndup_user(uargs, ~0UL >> 1);
1806 if (IS_ERR(args)) {
1807 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto free_hdr;
1809 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (find_module(mod->name)) {
1812 err = -EEXIST;
1813 goto free_mod;
1814 }
1815
1816 mod->state = MODULE_STATE_COMING;
1817
1818 /* Allow arches to frob section contents and sizes. */
1819 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1820 if (err < 0)
1821 goto free_mod;
1822
1823 if (pcpuindex) {
1824 /* We have a special allocation for this section. */
1825 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001826 sechdrs[pcpuindex].sh_addralign,
1827 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (!percpu) {
1829 err = -ENOMEM;
1830 goto free_mod;
1831 }
1832 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1833 mod->percpu = percpu;
1834 }
1835
1836 /* Determine total sizes, and put offsets in sh_entsize. For now
1837 this is done generically; there doesn't appear to be any
1838 special cases for the architectures. */
1839 layout_sections(mod, hdr, sechdrs, secstrings);
1840
1841 /* Do the allocs. */
1842 ptr = module_alloc(mod->core_size);
1843 if (!ptr) {
1844 err = -ENOMEM;
1845 goto free_percpu;
1846 }
1847 memset(ptr, 0, mod->core_size);
1848 mod->module_core = ptr;
1849
1850 ptr = module_alloc(mod->init_size);
1851 if (!ptr && mod->init_size) {
1852 err = -ENOMEM;
1853 goto free_core;
1854 }
1855 memset(ptr, 0, mod->init_size);
1856 mod->module_init = ptr;
1857
1858 /* Transfer each section which specifies SHF_ALLOC */
1859 DEBUGP("final section addresses:\n");
1860 for (i = 0; i < hdr->e_shnum; i++) {
1861 void *dest;
1862
1863 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1864 continue;
1865
1866 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1867 dest = mod->module_init
1868 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1869 else
1870 dest = mod->module_core + sechdrs[i].sh_entsize;
1871
1872 if (sechdrs[i].sh_type != SHT_NOBITS)
1873 memcpy(dest, (void *)sechdrs[i].sh_addr,
1874 sechdrs[i].sh_size);
1875 /* Update sh_addr to point to copy in image. */
1876 sechdrs[i].sh_addr = (unsigned long)dest;
1877 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1878 }
1879 /* Module has been moved. */
1880 mod = (void *)sechdrs[modindex].sh_addr;
1881
1882 /* Now we've moved module, initialize linked lists, etc. */
1883 module_unload_init(mod);
1884
Kay Sievers270a6c42007-01-18 13:26:15 +01001885 /* Initialize kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07001886 err = mod_sysfs_init(mod);
1887 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001888 goto cleanup;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* Set up license info based on the info section */
1891 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1892
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001893 if (strcmp(mod->name, "ndiswrapper") == 0)
Randy Dunlap0e2d57f2006-10-29 22:46:34 -08001894 add_taint(TAINT_PROPRIETARY_MODULE);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001895 if (strcmp(mod->name, "driverloader") == 0)
1896 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d612006-01-08 01:03:41 -08001897
Matt Domschc988d2b2005-06-23 22:05:15 -07001898 /* Set up MODINFO_ATTR fields */
1899 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Fix up syms, so that st_value is a pointer to location. */
1902 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1903 mod);
1904 if (err < 0)
1905 goto cleanup;
1906
1907 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1908 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1909 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1910 if (crcindex)
1911 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1912 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1913 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1914 if (gplcrcindex)
1915 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001916 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1917 sizeof(*mod->gpl_future_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001918 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1919 sizeof(*mod->unused_syms);
1920 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1921 sizeof(*mod->unused_gpl_syms);
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001922 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1923 if (gplfuturecrcindex)
1924 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001926 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1927 if (unusedcrcindex)
1928 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1929 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1930 if (unusedgplcrcindex)
1931 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933#ifdef CONFIG_MODVERSIONS
Daniel Walker22a8bde2007-10-18 03:06:07 -07001934 if ((mod->num_syms && !crcindex) ||
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001935 (mod->num_gpl_syms && !gplcrcindex) ||
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001936 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1937 (mod->num_unused_syms && !unusedcrcindex) ||
1938 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 printk(KERN_WARNING "%s: No versions for exported symbols."
1940 " Tainting kernel.\n", mod->name);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001941 add_taint_module(mod, TAINT_FORCED_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001944 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
1945 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
1946 "__markers_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /* Now do relocations. */
1949 for (i = 1; i < hdr->e_shnum; i++) {
1950 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1951 unsigned int info = sechdrs[i].sh_info;
1952
1953 /* Not a valid relocation section? */
1954 if (info >= hdr->e_shnum)
1955 continue;
1956
1957 /* Don't bother with non-allocated sections */
1958 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1959 continue;
1960
1961 if (sechdrs[i].sh_type == SHT_REL)
1962 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1963 else if (sechdrs[i].sh_type == SHT_RELA)
1964 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1965 mod);
1966 if (err < 0)
1967 goto cleanup;
1968 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001969#ifdef CONFIG_MARKERS
1970 mod->markers = (void *)sechdrs[markersindex].sh_addr;
1971 mod->num_markers =
1972 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
1973#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001975 /* Find duplicate symbols */
1976 err = verify_export_symbols(mod);
1977
1978 if (err < 0)
1979 goto cleanup;
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* Set up and sort exception table */
1982 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1983 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1984 sort_extable(extable, extable + mod->num_exentries);
1985
1986 /* Finally, copy percpu area over. */
1987 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1988 sechdrs[pcpuindex].sh_size);
1989
1990 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1991
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001992#ifdef CONFIG_MARKERS
1993 if (!mod->taints)
1994 marker_update_probe_range(mod->markers,
1995 mod->markers + mod->num_markers, NULL, NULL);
1996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 err = module_finalize(hdr, sechdrs, mod);
1998 if (err < 0)
1999 goto cleanup;
2000
Thomas Koeller378bac82005-09-06 15:17:11 -07002001 /* flush the icache in correct context */
2002 old_fs = get_fs();
2003 set_fs(KERNEL_DS);
2004
2005 /*
2006 * Flush the instruction cache, since we've played with text.
2007 * Do it before processing of module parameters, so the module
2008 * can provide parameter accessor functions of its own.
2009 */
2010 if (mod->module_init)
2011 flush_icache_range((unsigned long)mod->module_init,
2012 (unsigned long)mod->module_init
2013 + mod->init_size);
2014 flush_icache_range((unsigned long)mod->module_core,
2015 (unsigned long)mod->module_core + mod->core_size);
2016
2017 set_fs(old_fs);
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002020 if (obsparmindex)
2021 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2022 mod->name);
2023
2024 /* Size of section 0 is 0, so this works well if no params */
2025 err = parse_args(mod->name, mod->args,
2026 (struct kernel_param *)
2027 sechdrs[setupindex].sh_addr,
2028 sechdrs[setupindex].sh_size
2029 / sizeof(struct kernel_param),
2030 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (err < 0)
2032 goto arch_cleanup;
2033
Daniel Walker22a8bde2007-10-18 03:06:07 -07002034 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 (struct kernel_param *)
2036 sechdrs[setupindex].sh_addr,
2037 sechdrs[setupindex].sh_size
2038 / sizeof(struct kernel_param));
2039 if (err < 0)
2040 goto arch_cleanup;
2041 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002042 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Jan Beulich4552d5d2006-06-26 13:57:28 +02002044 /* Size of section 0 is 0, so this works well if no unwind info. */
2045 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002046 (void *)sechdrs[unwindex].sh_addr,
2047 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* Get rid of temporary copy */
2050 vfree(hdr);
2051
2052 /* Done! */
2053 return mod;
2054
2055 arch_cleanup:
2056 module_arch_cleanup(mod);
2057 cleanup:
2058 module_unload_free(mod);
2059 module_free(mod, mod->module_init);
2060 free_core:
2061 module_free(mod, mod->module_core);
2062 free_percpu:
2063 if (percpu)
2064 percpu_modfree(percpu);
2065 free_mod:
2066 kfree(args);
2067 free_hdr:
2068 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002069 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 truncated:
2072 printk(KERN_ERR "Module len %lu truncated\n", len);
2073 err = -ENOEXEC;
2074 goto free_hdr;
2075}
2076
2077/*
2078 * link the module with the whole machine is stopped with interrupts off
2079 * - this defends against kallsyms not taking locks
2080 */
2081static int __link_module(void *_mod)
2082{
2083 struct module *mod = _mod;
2084 list_add(&mod->list, &modules);
2085 return 0;
2086}
2087
2088/* This is where the real work happens */
2089asmlinkage long
2090sys_init_module(void __user *umod,
2091 unsigned long len,
2092 const char __user *uargs)
2093{
2094 struct module *mod;
2095 int ret = 0;
2096
2097 /* Must have permission */
2098 if (!capable(CAP_SYS_MODULE))
2099 return -EPERM;
2100
2101 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002102 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return -EINTR;
2104
2105 /* Do all the hard work */
2106 mod = load_module(umod, len, uargs);
2107 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002108 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return PTR_ERR(mod);
2110 }
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /* Now sew it into the lists. They won't access us, since
2113 strong_try_module_get() will fail. */
2114 stop_machine_run(__link_module, mod, NR_CPUS);
2115
2116 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002117 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Alan Sterne041c682006-03-27 01:16:30 -08002119 blocking_notifier_call_chain(&module_notify_list,
2120 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* Start the module */
2123 if (mod->init != NULL)
2124 ret = mod->init();
2125 if (ret < 0) {
2126 /* Init routine failed: abort. Try to protect us from
2127 buggy refcounters. */
2128 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002129 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002130 module_put(mod);
2131 mutex_lock(&module_mutex);
2132 free_module(mod);
2133 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return ret;
2135 }
2136
2137 /* Now it's a first class citizen! */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002138 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 mod->state = MODULE_STATE_LIVE;
2140 /* Drop initial reference. */
2141 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002142 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 module_free(mod, mod->module_init);
2144 mod->module_init = NULL;
2145 mod->init_size = 0;
2146 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002147 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 return 0;
2150}
2151
2152static inline int within(unsigned long addr, void *start, unsigned long size)
2153{
2154 return ((void *)addr >= start && (void *)addr < start + size);
2155}
2156
2157#ifdef CONFIG_KALLSYMS
2158/*
2159 * This ignores the intensely annoying "mapping symbols" found
2160 * in ARM ELF files: $a, $t and $d.
2161 */
2162static inline int is_arm_mapping_symbol(const char *str)
2163{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002164 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 && (str[2] == '\0' || str[2] == '.');
2166}
2167
2168static const char *get_ksymbol(struct module *mod,
2169 unsigned long addr,
2170 unsigned long *size,
2171 unsigned long *offset)
2172{
2173 unsigned int i, best = 0;
2174 unsigned long nextval;
2175
2176 /* At worse, next value is at end of module */
2177 if (within(addr, mod->module_init, mod->init_size))
2178 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002179 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2181
2182 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002183 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 for (i = 1; i < mod->num_symtab; i++) {
2185 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2186 continue;
2187
2188 /* We ignore unnamed symbols: they're uninformative
2189 * and inserted at a whim. */
2190 if (mod->symtab[i].st_value <= addr
2191 && mod->symtab[i].st_value > mod->symtab[best].st_value
2192 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2193 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2194 best = i;
2195 if (mod->symtab[i].st_value > addr
2196 && mod->symtab[i].st_value < nextval
2197 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2198 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2199 nextval = mod->symtab[i].st_value;
2200 }
2201
2202 if (!best)
2203 return NULL;
2204
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002205 if (size)
2206 *size = nextval - mod->symtab[best].st_value;
2207 if (offset)
2208 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return mod->strtab + mod->symtab[best].st_name;
2210}
2211
2212/* For kallsyms to ask for address resolution. NULL means not found.
2213 We don't lock, as this is used for oops resolution and races are a
2214 lesser concern. */
2215const char *module_address_lookup(unsigned long addr,
2216 unsigned long *size,
2217 unsigned long *offset,
2218 char **modname)
2219{
2220 struct module *mod;
2221
2222 list_for_each_entry(mod, &modules, list) {
2223 if (within(addr, mod->module_init, mod->init_size)
2224 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002225 if (modname)
2226 *modname = mod->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return get_ksymbol(mod, addr, size, offset);
2228 }
2229 }
2230 return NULL;
2231}
2232
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002233int lookup_module_symbol_name(unsigned long addr, char *symname)
2234{
2235 struct module *mod;
2236
2237 mutex_lock(&module_mutex);
2238 list_for_each_entry(mod, &modules, list) {
2239 if (within(addr, mod->module_init, mod->init_size) ||
2240 within(addr, mod->module_core, mod->core_size)) {
2241 const char *sym;
2242
2243 sym = get_ksymbol(mod, addr, NULL, NULL);
2244 if (!sym)
2245 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002246 strlcpy(symname, sym, KSYM_NAME_LEN);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002247 mutex_unlock(&module_mutex);
2248 return 0;
2249 }
2250 }
2251out:
2252 mutex_unlock(&module_mutex);
2253 return -ERANGE;
2254}
2255
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002256int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2257 unsigned long *offset, char *modname, char *name)
2258{
2259 struct module *mod;
2260
2261 mutex_lock(&module_mutex);
2262 list_for_each_entry(mod, &modules, list) {
2263 if (within(addr, mod->module_init, mod->init_size) ||
2264 within(addr, mod->module_core, mod->core_size)) {
2265 const char *sym;
2266
2267 sym = get_ksymbol(mod, addr, size, offset);
2268 if (!sym)
2269 goto out;
2270 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002271 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002272 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002273 strlcpy(name, sym, KSYM_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002274 mutex_unlock(&module_mutex);
2275 return 0;
2276 }
2277 }
2278out:
2279 mutex_unlock(&module_mutex);
2280 return -ERANGE;
2281}
2282
Alexey Dobriyanea078902007-05-08 00:28:39 -07002283int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2284 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 struct module *mod;
2287
Ashutosh Naik6389a382006-03-23 03:00:46 -08002288 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 list_for_each_entry(mod, &modules, list) {
2290 if (symnum < mod->num_symtab) {
2291 *value = mod->symtab[symnum].st_value;
2292 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002293 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002294 KSYM_NAME_LEN);
2295 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002296 *exported = is_exported(name, mod);
Ashutosh Naik6389a382006-03-23 03:00:46 -08002297 mutex_unlock(&module_mutex);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 symnum -= mod->num_symtab;
2301 }
Ashutosh Naik6389a382006-03-23 03:00:46 -08002302 mutex_unlock(&module_mutex);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002303 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306static unsigned long mod_find_symname(struct module *mod, const char *name)
2307{
2308 unsigned int i;
2309
2310 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002311 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2312 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return mod->symtab[i].st_value;
2314 return 0;
2315}
2316
2317/* Look for this name: can be of form module:name. */
2318unsigned long module_kallsyms_lookup_name(const char *name)
2319{
2320 struct module *mod;
2321 char *colon;
2322 unsigned long ret = 0;
2323
2324 /* Don't lock: we're in enough trouble already. */
2325 if ((colon = strchr(name, ':')) != NULL) {
2326 *colon = '\0';
2327 if ((mod = find_module(name)) != NULL)
2328 ret = mod_find_symname(mod, colon+1);
2329 *colon = ':';
2330 } else {
2331 list_for_each_entry(mod, &modules, list)
2332 if ((ret = mod_find_symname(mod, name)) != 0)
2333 break;
2334 }
2335 return ret;
2336}
2337#endif /* CONFIG_KALLSYMS */
2338
2339/* Called by the /proc file system to return a list of modules. */
2340static void *m_start(struct seq_file *m, loff_t *pos)
2341{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002342 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002343 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
2346static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2347{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002348 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
2351static void m_stop(struct seq_file *m, void *p)
2352{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002353 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002356static char *taint_flags(unsigned int taints, char *buf)
2357{
2358 int bx = 0;
2359
2360 if (taints) {
2361 buf[bx++] = '(';
2362 if (taints & TAINT_PROPRIETARY_MODULE)
2363 buf[bx++] = 'P';
2364 if (taints & TAINT_FORCED_MODULE)
2365 buf[bx++] = 'F';
2366 /*
2367 * TAINT_FORCED_RMMOD: could be added.
2368 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2369 * apply to modules.
2370 */
2371 buf[bx++] = ')';
2372 }
2373 buf[bx] = '\0';
2374
2375 return buf;
2376}
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378static int m_show(struct seq_file *m, void *p)
2379{
2380 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002381 char buf[8];
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 seq_printf(m, "%s %lu",
2384 mod->name, mod->init_size + mod->core_size);
2385 print_unload_info(m, mod);
2386
2387 /* Informative for users. */
2388 seq_printf(m, " %s",
2389 mod->state == MODULE_STATE_GOING ? "Unloading":
2390 mod->state == MODULE_STATE_COMING ? "Loading":
2391 "Live");
2392 /* Used by oprofile and other similar tools. */
2393 seq_printf(m, " 0x%p", mod->module_core);
2394
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002395 /* Taints info */
2396 if (mod->taints)
2397 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 seq_printf(m, "\n");
2400 return 0;
2401}
2402
2403/* Format: modulename size refcount deps address
2404
2405 Where refcount is a number or -, and deps is a comma-separated list
2406 of depends or -.
2407*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002408const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 .start = m_start,
2410 .next = m_next,
2411 .stop = m_stop,
2412 .show = m_show
2413};
2414
2415/* Given an address, look for it in the module exception tables. */
2416const struct exception_table_entry *search_module_extables(unsigned long addr)
2417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 const struct exception_table_entry *e = NULL;
2419 struct module *mod;
2420
Rusty Russell24da1cb2007-07-15 23:41:46 -07002421 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 list_for_each_entry(mod, &modules, list) {
2423 if (mod->num_exentries == 0)
2424 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 e = search_extable(mod->extable,
2427 mod->extable + mod->num_exentries - 1,
2428 addr);
2429 if (e)
2430 break;
2431 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002432 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002435 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return e;
2437}
2438
Ingo Molnar4d435f92006-07-03 00:24:24 -07002439/*
2440 * Is this a valid module address?
2441 */
2442int is_module_address(unsigned long addr)
2443{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002444 struct module *mod;
2445
Rusty Russell24da1cb2007-07-15 23:41:46 -07002446 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002447
2448 list_for_each_entry(mod, &modules, list) {
2449 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002450 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002451 return 1;
2452 }
2453 }
2454
Rusty Russell24da1cb2007-07-15 23:41:46 -07002455 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002456
2457 return 0;
2458}
2459
2460
Rusty Russell24da1cb2007-07-15 23:41:46 -07002461/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462struct module *__module_text_address(unsigned long addr)
2463{
2464 struct module *mod;
2465
2466 list_for_each_entry(mod, &modules, list)
2467 if (within(addr, mod->module_init, mod->init_text_size)
2468 || within(addr, mod->module_core, mod->core_text_size))
2469 return mod;
2470 return NULL;
2471}
2472
2473struct module *module_text_address(unsigned long addr)
2474{
2475 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Rusty Russell24da1cb2007-07-15 23:41:46 -07002477 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002479 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 return mod;
2482}
2483
2484/* Don't grab lock, we're oopsing. */
2485void print_modules(void)
2486{
2487 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002488 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 printk("Modules linked in:");
2491 list_for_each_entry(mod, &modules, list)
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002492 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 printk("\n");
2494}
2495
Randy Dunlapef665c12007-02-13 15:19:06 -08002496#ifdef CONFIG_SYSFS
Kay Sievers7f422e22006-12-20 10:39:33 +01002497static char *make_driver_name(struct device_driver *drv)
2498{
2499 char *driver_name;
2500
2501 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2502 GFP_KERNEL);
2503 if (!driver_name)
2504 return NULL;
2505
2506 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2507 return driver_name;
2508}
2509
Greg Kroah-Hartmanfe480a22007-01-15 11:50:02 -08002510static void module_create_drivers_dir(struct module_kobject *mk)
2511{
2512 if (!mk || mk->drivers_dir)
2513 return;
2514
2515 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2516}
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518void module_add_driver(struct module *mod, struct device_driver *drv)
2519{
Kay Sievers7f422e22006-12-20 10:39:33 +01002520 char *driver_name;
Kay Sieverse17e0f52006-11-24 12:15:25 +01002521 int no_warn;
Kay Sieversf30c53a2007-01-15 20:22:02 +01002522 struct module_kobject *mk = NULL;
Kay Sieverse17e0f52006-11-24 12:15:25 +01002523
Kay Sieversf30c53a2007-01-15 20:22:02 +01002524 if (!drv)
2525 return;
2526
2527 if (mod)
2528 mk = &mod->mkobj;
2529 else if (drv->mod_name) {
2530 struct kobject *mkobj;
2531
2532 /* Lookup built-in module entry in /sys/modules */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07002533 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
Kay Sievers0c84ce22007-04-01 23:49:48 -07002534 if (mkobj) {
Kay Sieversf30c53a2007-01-15 20:22:02 +01002535 mk = container_of(mkobj, struct module_kobject, kobj);
Kay Sievers0c84ce22007-04-01 23:49:48 -07002536 /* remember our module structure */
2537 drv->mkobj = mk;
2538 /* kset_find_obj took a reference */
2539 kobject_put(mkobj);
2540 }
Kay Sieversf30c53a2007-01-15 20:22:02 +01002541 }
2542
2543 if (!mk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return;
2545
Kay Sieverse17e0f52006-11-24 12:15:25 +01002546 /* Don't check return codes; these calls are idempotent */
Kay Sieversf30c53a2007-01-15 20:22:02 +01002547 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
Kay Sievers7f422e22006-12-20 10:39:33 +01002548 driver_name = make_driver_name(drv);
2549 if (driver_name) {
Greg Kroah-Hartmanfe480a22007-01-15 11:50:02 -08002550 module_create_drivers_dir(mk);
Kay Sieversf30c53a2007-01-15 20:22:02 +01002551 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
Kay Sievers7f422e22006-12-20 10:39:33 +01002552 driver_name);
2553 kfree(driver_name);
2554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556EXPORT_SYMBOL(module_add_driver);
2557
2558void module_remove_driver(struct device_driver *drv)
2559{
Kay Sievers0c84ce22007-04-01 23:49:48 -07002560 struct module_kobject *mk = NULL;
Kay Sievers7f422e22006-12-20 10:39:33 +01002561 char *driver_name;
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (!drv)
2564 return;
Kay Sievers7f422e22006-12-20 10:39:33 +01002565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 sysfs_remove_link(&drv->kobj, "module");
Kay Sievers0c84ce22007-04-01 23:49:48 -07002567
2568 if (drv->owner)
2569 mk = &drv->owner->mkobj;
2570 else if (drv->mkobj)
2571 mk = drv->mkobj;
2572 if (mk && mk->drivers_dir) {
Kay Sievers7f422e22006-12-20 10:39:33 +01002573 driver_name = make_driver_name(drv);
2574 if (driver_name) {
Kay Sievers0c84ce22007-04-01 23:49:48 -07002575 sysfs_remove_link(mk->drivers_dir, driver_name);
Kay Sievers7f422e22006-12-20 10:39:33 +01002576 kfree(driver_name);
2577 }
2578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580EXPORT_SYMBOL(module_remove_driver);
Randy Dunlapef665c12007-02-13 15:19:06 -08002581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583#ifdef CONFIG_MODVERSIONS
2584/* Generate the signature for struct module here, too, for modversions. */
2585void struct_module(struct module *mod) { return; }
2586EXPORT_SYMBOL(struct_module);
2587#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002588
2589#ifdef CONFIG_MARKERS
2590void module_update_markers(struct module *probe_module, int *refcount)
2591{
2592 struct module *mod;
2593
2594 mutex_lock(&module_mutex);
2595 list_for_each_entry(mod, &modules, list)
2596 if (!mod->taints)
2597 marker_update_probe_range(mod->markers,
2598 mod->markers + mod->num_markers,
2599 probe_module, refcount);
2600 mutex_unlock(&module_mutex);
2601}
2602#endif