blob: c51c089c666ef9bd66013d0628f37e551b3e173b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020047#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080048#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#if 0
51#define DEBUGP printk
52#else
53#define DEBUGP(fmt , a...)
54#endif
55
56#ifndef ARCH_SHF_SMALL
57#define ARCH_SHF_SMALL 0
58#endif
59
60/* If this is set, the section belongs in the init part of the module */
61#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
62
Rusty Russell24da1cb2007-07-15 23:41:46 -070063/* List of modules, protected by module_mutex or preempt_disable
64 * (add/delete uses stop_machine). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080065static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static LIST_HEAD(modules);
67
Rusty Russellc9a3ba52008-01-29 17:13:18 -050068/* Waiting for a module to finish initializing? */
69static DECLARE_WAIT_QUEUE_HEAD(module_wq);
70
Alan Sterne041c682006-03-27 01:16:30 -080071static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73int register_module_notifier(struct notifier_block * nb)
74{
Alan Sterne041c682006-03-27 01:16:30 -080075 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77EXPORT_SYMBOL(register_module_notifier);
78
79int unregister_module_notifier(struct notifier_block * nb)
80{
Alan Sterne041c682006-03-27 01:16:30 -080081 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83EXPORT_SYMBOL(unregister_module_notifier);
84
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080085/* We require a truly strong try_module_get(): 0 means failure due to
86 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static inline int strong_try_module_get(struct module *mod)
88{
89 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050090 return -EBUSY;
91 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -050093 else
94 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Florin Malitafa3ba2e82006-10-11 01:21:48 -070097static inline void add_taint_module(struct module *mod, unsigned flag)
98{
99 add_taint(flag);
100 mod->taints |= flag;
101}
102
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200103/*
104 * A thread that wants to hold a reference to a module only while it
105 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107void __module_put_and_exit(struct module *mod, long code)
108{
109 module_put(mod);
110 do_exit(code);
111}
112EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Find a module section: 0 means not found. */
115static unsigned int find_sec(Elf_Ehdr *hdr,
116 Elf_Shdr *sechdrs,
117 const char *secstrings,
118 const char *name)
119{
120 unsigned int i;
121
122 for (i = 1; i < hdr->e_shnum; i++)
123 /* Alloc bit cleared means "ignore it." */
124 if ((sechdrs[i].sh_flags & SHF_ALLOC)
125 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
126 return i;
127 return 0;
128}
129
130/* Provided by the linker */
131extern const struct kernel_symbol __start___ksymtab[];
132extern const struct kernel_symbol __stop___ksymtab[];
133extern const struct kernel_symbol __start___ksymtab_gpl[];
134extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800135extern const struct kernel_symbol __start___ksymtab_gpl_future[];
136extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700137extern const struct kernel_symbol __start___ksymtab_unused[];
138extern const struct kernel_symbol __stop___ksymtab_unused[];
139extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
140extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
141extern const struct kernel_symbol __start___ksymtab_gpl_future[];
142extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143extern const unsigned long __start___kcrctab[];
144extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800145extern const unsigned long __start___kcrctab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700146extern const unsigned long __start___kcrctab_unused[];
147extern const unsigned long __start___kcrctab_unused_gpl[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149#ifndef CONFIG_MODVERSIONS
150#define symversion(base, idx) NULL
151#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800152#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif
154
Rusty Russelldafd0942008-07-22 19:24:25 -0500155struct symsearch {
156 const struct kernel_symbol *start, *stop;
157 const unsigned long *crcs;
158 enum {
159 NOT_GPL_ONLY,
160 GPL_ONLY,
161 WILL_BE_GPL_ONLY,
162 } licence;
163 bool unused;
164};
165
166static bool each_symbol_in_section(const struct symsearch *arr,
167 unsigned int arrsize,
168 struct module *owner,
169 bool (*fn)(const struct symsearch *syms,
170 struct module *owner,
171 unsigned int symnum, void *data),
172 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100173{
Rusty Russelldafd0942008-07-22 19:24:25 -0500174 unsigned int i, j;
175
176 for (j = 0; j < arrsize; j++) {
177 for (i = 0; i < arr[j].stop - arr[j].start; i++)
178 if (fn(&arr[j], owner, i, data))
179 return true;
180 }
181
182 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100183}
184
Rusty Russelldafd0942008-07-22 19:24:25 -0500185/* Returns true as soon as fn returns true, otherwise false. */
186static bool each_symbol(bool (*fn)(const struct symsearch *arr,
187 struct module *owner,
188 unsigned int symnum, void *data),
189 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700190{
Rusty Russelldafd0942008-07-22 19:24:25 -0500191 struct module *mod;
192 const struct symsearch arr[] = {
193 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
194 NOT_GPL_ONLY, false },
195 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
196 __start___kcrctab_gpl,
197 GPL_ONLY, false },
198 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
199 __start___kcrctab_gpl_future,
200 WILL_BE_GPL_ONLY, false },
201 { __start___ksymtab_unused, __stop___ksymtab_unused,
202 __start___kcrctab_unused,
203 NOT_GPL_ONLY, true },
204 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
205 __start___kcrctab_unused_gpl,
206 GPL_ONLY, true },
207 };
208
209 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
210 return true;
211
212 list_for_each_entry(mod, &modules, list) {
213 struct symsearch arr[] = {
214 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
215 NOT_GPL_ONLY, false },
216 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
217 mod->gpl_crcs,
218 GPL_ONLY, false },
219 { mod->gpl_future_syms,
220 mod->gpl_future_syms + mod->num_gpl_future_syms,
221 mod->gpl_future_crcs,
222 WILL_BE_GPL_ONLY, false },
223 { mod->unused_syms,
224 mod->unused_syms + mod->num_unused_syms,
225 mod->unused_crcs,
226 NOT_GPL_ONLY, true },
227 { mod->unused_gpl_syms,
228 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
229 mod->unused_gpl_crcs,
230 GPL_ONLY, true },
231 };
232
233 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
234 return true;
235 }
236 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700237}
238
Rusty Russelldafd0942008-07-22 19:24:25 -0500239struct find_symbol_arg {
240 /* Input */
241 const char *name;
242 bool gplok;
243 bool warn;
244
245 /* Output */
246 struct module *owner;
247 const unsigned long *crc;
248 unsigned long value;
249};
250
251static bool find_symbol_in_section(const struct symsearch *syms,
252 struct module *owner,
253 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500254{
Rusty Russelldafd0942008-07-22 19:24:25 -0500255 struct find_symbol_arg *fsa = data;
256
257 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
258 return false;
259
260 if (!fsa->gplok) {
261 if (syms->licence == GPL_ONLY)
262 return false;
263 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
264 printk(KERN_WARNING "Symbol %s is being used "
265 "by a non-GPL module, which will not "
266 "be allowed in the future\n", fsa->name);
267 printk(KERN_WARNING "Please see the file "
268 "Documentation/feature-removal-schedule.txt "
269 "in the kernel source tree for more details.\n");
270 }
271 }
272
273 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500274 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500275 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500276 printk(KERN_WARNING
277 "This symbol will go away in the future.\n");
278 printk(KERN_WARNING
279 "Please evalute if this is the right api to use and if "
280 "it really is, submit a report the linux kernel "
281 "mailinglist together with submitting your code for "
282 "inclusion.\n");
283 }
Rusty Russelldafd0942008-07-22 19:24:25 -0500284
285 fsa->owner = owner;
286 fsa->crc = symversion(syms->crcs, symnum);
287 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500288 return true;
289}
290
Rusty Russellad9546c2008-05-01 21:14:59 -0500291/* Find a symbol, return value, (optional) crc and (optional) module
292 * which owns it */
293static unsigned long find_symbol(const char *name,
294 struct module **owner,
295 const unsigned long **crc,
296 bool gplok,
297 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Rusty Russelldafd0942008-07-22 19:24:25 -0500299 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Rusty Russelldafd0942008-07-22 19:24:25 -0500301 fsa.name = name;
302 fsa.gplok = gplok;
303 fsa.warn = warn;
304
305 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500306 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500307 *owner = fsa.owner;
308 if (crc)
309 *crc = fsa.crc;
310 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800314 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Rusty Russelldafd0942008-07-22 19:24:25 -0500317/* lookup symbol in given range of kernel_symbols */
318static const struct kernel_symbol *lookup_symbol(const char *name,
319 const struct kernel_symbol *start,
320 const struct kernel_symbol *stop)
321{
322 const struct kernel_symbol *ks = start;
323 for (; ks < stop; ks++)
324 if (strcmp(ks->name, name) == 0)
325 return ks;
326 return NULL;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* Search for module by name: must hold module_mutex. */
330static struct module *find_module(const char *name)
331{
332 struct module *mod;
333
334 list_for_each_entry(mod, &modules, list) {
335 if (strcmp(mod->name, name) == 0)
336 return mod;
337 }
338 return NULL;
339}
340
341#ifdef CONFIG_SMP
342/* Number of blocks used and allocated. */
343static unsigned int pcpu_num_used, pcpu_num_allocated;
344/* Size of each block. -ve means used. */
345static int *pcpu_size;
346
347static int split_block(unsigned int i, unsigned short size)
348{
349 /* Reallocation required? */
350 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700351 int *new;
352
353 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
354 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!new)
356 return 0;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pcpu_size = new;
360 }
361
362 /* Insert a new subblock */
363 memmove(&pcpu_size[i+1], &pcpu_size[i],
364 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
365 pcpu_num_used++;
366
367 pcpu_size[i+1] -= size;
368 pcpu_size[i] = size;
369 return 1;
370}
371
372static inline unsigned int block_size(int val)
373{
374 if (val < 0)
375 return -val;
376 return val;
377}
378
Rusty Russell842bbaa2005-08-01 21:11:47 -0700379static void *percpu_modalloc(unsigned long size, unsigned long align,
380 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 unsigned long extra;
383 unsigned int i;
384 void *ptr;
385
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200386 if (align > PAGE_SIZE) {
387 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
388 name, align, PAGE_SIZE);
389 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 ptr = __per_cpu_start;
393 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
394 /* Extra for alignment requirement. */
395 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
396 BUG_ON(i == 0 && extra != 0);
397
398 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
399 continue;
400
401 /* Transfer extra to previous block. */
402 if (pcpu_size[i-1] < 0)
403 pcpu_size[i-1] -= extra;
404 else
405 pcpu_size[i-1] += extra;
406 pcpu_size[i] -= extra;
407 ptr += extra;
408
409 /* Split block if warranted */
410 if (pcpu_size[i] - size > sizeof(unsigned long))
411 if (!split_block(i, size))
412 return NULL;
413
414 /* Mark allocated */
415 pcpu_size[i] = -pcpu_size[i];
416 return ptr;
417 }
418
419 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
420 size);
421 return NULL;
422}
423
424static void percpu_modfree(void *freeme)
425{
426 unsigned int i;
427 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
428
429 /* First entry is core kernel percpu data. */
430 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
431 if (ptr == freeme) {
432 pcpu_size[i] = -pcpu_size[i];
433 goto free;
434 }
435 }
436 BUG();
437
438 free:
439 /* Merge with previous? */
440 if (pcpu_size[i-1] >= 0) {
441 pcpu_size[i-1] += pcpu_size[i];
442 pcpu_num_used--;
443 memmove(&pcpu_size[i], &pcpu_size[i+1],
444 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
445 i--;
446 }
447 /* Merge with next? */
448 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
449 pcpu_size[i] += pcpu_size[i+1];
450 pcpu_num_used--;
451 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
452 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
453 }
454}
455
456static unsigned int find_pcpusec(Elf_Ehdr *hdr,
457 Elf_Shdr *sechdrs,
458 const char *secstrings)
459{
460 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
461}
462
Mike Travisdd5af902008-01-30 13:33:32 +0100463static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
464{
465 int cpu;
466
467 for_each_possible_cpu(cpu)
468 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static int percpu_modinit(void)
472{
473 pcpu_num_used = 2;
474 pcpu_num_allocated = 2;
475 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
476 GFP_KERNEL);
477 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200478 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* Free room. */
480 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
481 if (pcpu_size[1] < 0) {
482 printk(KERN_ERR "No per-cpu room for modules.\n");
483 pcpu_num_used = 1;
484 }
485
486 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488__initcall(percpu_modinit);
489#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700490static inline void *percpu_modalloc(unsigned long size, unsigned long align,
491 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 return NULL;
494}
495static inline void percpu_modfree(void *pcpuptr)
496{
497 BUG();
498}
499static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
500 Elf_Shdr *sechdrs,
501 const char *secstrings)
502{
503 return 0;
504}
505static inline void percpu_modcopy(void *pcpudst, const void *src,
506 unsigned long size)
507{
508 /* pcpusec should be 0, and size of that section should be 0. */
509 BUG_ON(size != 0);
510}
511#endif /* CONFIG_SMP */
512
Matt Domschc988d2b2005-06-23 22:05:15 -0700513#define MODINFO_ATTR(field) \
514static void setup_modinfo_##field(struct module *mod, const char *s) \
515{ \
516 mod->field = kstrdup(s, GFP_KERNEL); \
517} \
518static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
519 struct module *mod, char *buffer) \
520{ \
521 return sprintf(buffer, "%s\n", mod->field); \
522} \
523static int modinfo_##field##_exists(struct module *mod) \
524{ \
525 return mod->field != NULL; \
526} \
527static void free_modinfo_##field(struct module *mod) \
528{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700529 kfree(mod->field); \
530 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700531} \
532static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900533 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700534 .show = show_modinfo_##field, \
535 .setup = setup_modinfo_##field, \
536 .test = modinfo_##field##_exists, \
537 .free = free_modinfo_##field, \
538};
539
540MODINFO_ATTR(version);
541MODINFO_ATTR(srcversion);
542
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100543static char last_unloaded_module[MODULE_NAME_LEN+1];
544
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800545#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/* Init the unload section of the module. */
547static void module_unload_init(struct module *mod)
548{
549 unsigned int i;
550
551 INIT_LIST_HEAD(&mod->modules_which_use_me);
552 for (i = 0; i < NR_CPUS; i++)
553 local_set(&mod->ref[i].count, 0);
554 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700555 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* Backwards compatibility macros put refcount during init. */
557 mod->waiter = current;
558}
559
560/* modules using other modules */
561struct module_use
562{
563 struct list_head list;
564 struct module *module_which_uses;
565};
566
567/* Does a already use b? */
568static int already_uses(struct module *a, struct module *b)
569{
570 struct module_use *use;
571
572 list_for_each_entry(use, &b->modules_which_use_me, list) {
573 if (use->module_which_uses == a) {
574 DEBUGP("%s uses %s!\n", a->name, b->name);
575 return 1;
576 }
577 }
578 DEBUGP("%s does not use %s!\n", a->name, b->name);
579 return 0;
580}
581
582/* Module a uses b */
583static int use_module(struct module *a, struct module *b)
584{
585 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500586 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (b == NULL || already_uses(a, b)) return 1;
589
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500590 /* If we're interrupted or time out, we fail. */
591 if (wait_event_interruptible_timeout(
592 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
593 30 * HZ) <= 0) {
594 printk("%s: gave up waiting for init of module %s.\n",
595 a->name, b->name);
596 return 0;
597 }
598
599 /* If strong_try_module_get() returned a different error, we fail. */
600 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602
603 DEBUGP("Allocating new usage for %s.\n", a->name);
604 use = kmalloc(sizeof(*use), GFP_ATOMIC);
605 if (!use) {
606 printk("%s: out of memory loading\n", a->name);
607 module_put(b);
608 return 0;
609 }
610
611 use->module_which_uses = a;
612 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100613 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 1;
615}
616
617/* Clear the unload stuff of the module. */
618static void module_unload_free(struct module *mod)
619{
620 struct module *i;
621
622 list_for_each_entry(i, &modules, list) {
623 struct module_use *use;
624
625 list_for_each_entry(use, &i->modules_which_use_me, list) {
626 if (use->module_which_uses == mod) {
627 DEBUGP("%s unusing %s\n", mod->name, i->name);
628 module_put(i);
629 list_del(&use->list);
630 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100631 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* There can be at most one match. */
633 break;
634 }
635 }
636 }
637}
638
639#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800640static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 int ret = (flags & O_TRUNC);
643 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800644 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return ret;
646}
647#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800648static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 return 0;
651}
652#endif /* CONFIG_MODULE_FORCE_UNLOAD */
653
654struct stopref
655{
656 struct module *mod;
657 int flags;
658 int *forced;
659};
660
661/* Whole machine is stopped with interrupts off when this runs. */
662static int __try_stop_module(void *_sref)
663{
664 struct stopref *sref = _sref;
665
Rusty Russellda39ba52008-07-22 19:24:25 -0500666 /* If it's not unused, quit unless we're forcing. */
667 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800668 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EWOULDBLOCK;
670 }
671
672 /* Mark it as dying. */
673 sref->mod->state = MODULE_STATE_GOING;
674 return 0;
675}
676
677static int try_stop_module(struct module *mod, int flags, int *forced)
678{
Rusty Russellda39ba52008-07-22 19:24:25 -0500679 if (flags & O_NONBLOCK) {
680 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Rusty Russellda39ba52008-07-22 19:24:25 -0500682 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
683 } else {
684 /* We don't need to stop the machine for this. */
685 mod->state = MODULE_STATE_GOING;
686 synchronize_sched();
687 return 0;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691unsigned int module_refcount(struct module *mod)
692{
693 unsigned int i, total = 0;
694
695 for (i = 0; i < NR_CPUS; i++)
696 total += local_read(&mod->ref[i].count);
697 return total;
698}
699EXPORT_SYMBOL(module_refcount);
700
701/* This exists whether we can unload or not */
702static void free_module(struct module *mod);
703
704static void wait_for_zero_refcount(struct module *mod)
705{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500706 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800707 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 for (;;) {
709 DEBUGP("Looking at refcount...\n");
710 set_current_state(TASK_UNINTERRUPTIBLE);
711 if (module_refcount(mod) == 0)
712 break;
713 schedule();
714 }
715 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800716 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800719asmlinkage long
720sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800723 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int ret, forced = 0;
725
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800726 if (!capable(CAP_SYS_MODULE))
727 return -EPERM;
728
729 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
730 return -EFAULT;
731 name[MODULE_NAME_LEN-1] = '\0';
732
Ashutosh Naik6389a382006-03-23 03:00:46 -0800733 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -EINTR;
735
736 mod = find_module(name);
737 if (!mod) {
738 ret = -ENOENT;
739 goto out;
740 }
741
742 if (!list_empty(&mod->modules_which_use_me)) {
743 /* Other modules depend on us: get rid of them first. */
744 ret = -EWOULDBLOCK;
745 goto out;
746 }
747
748 /* Doing init or already dying? */
749 if (mod->state != MODULE_STATE_LIVE) {
750 /* FIXME: if (force), slam module count and wake up
751 waiter --RR */
752 DEBUGP("%s already dying\n", mod->name);
753 ret = -EBUSY;
754 goto out;
755 }
756
757 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700758 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800759 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!forced) {
761 /* This module can't be removed */
762 ret = -EBUSY;
763 goto out;
764 }
765 }
766
767 /* Set this up before setting mod->state */
768 mod->waiter = current;
769
770 /* Stop the machine so refcounts can't move and disable module. */
771 ret = try_stop_module(mod, flags, &forced);
772 if (ret != 0)
773 goto out;
774
775 /* Never wait if forced. */
776 if (!forced && module_refcount(mod) != 0)
777 wait_for_zero_refcount(mod);
778
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200779 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200781 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200783 blocking_notifier_call_chain(&module_notify_list,
784 MODULE_STATE_GOING, mod);
785 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100786 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500787 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 free_module(mod);
789
790 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800791 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return ret;
793}
794
795static void print_unload_info(struct seq_file *m, struct module *mod)
796{
797 struct module_use *use;
798 int printed_something = 0;
799
800 seq_printf(m, " %u ", module_refcount(mod));
801
802 /* Always include a trailing , so userspace can differentiate
803 between this and the old multi-field proc format. */
804 list_for_each_entry(use, &mod->modules_which_use_me, list) {
805 printed_something = 1;
806 seq_printf(m, "%s,", use->module_which_uses->name);
807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (mod->init != NULL && mod->exit == NULL) {
810 printed_something = 1;
811 seq_printf(m, "[permanent],");
812 }
813
814 if (!printed_something)
815 seq_printf(m, "-");
816}
817
818void __symbol_put(const char *symbol)
819{
820 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Rusty Russell24da1cb2007-07-15 23:41:46 -0700822 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500823 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 BUG();
825 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700826 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828EXPORT_SYMBOL(__symbol_put);
829
830void symbol_put_addr(void *addr)
831{
Trent Piepho5e376612006-05-15 09:44:06 -0700832 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Trent Piepho5e376612006-05-15 09:44:06 -0700834 if (core_kernel_text((unsigned long)addr))
835 return;
836
837 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700839 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841EXPORT_SYMBOL_GPL(symbol_put_addr);
842
843static ssize_t show_refcnt(struct module_attribute *mattr,
844 struct module *mod, char *buffer)
845{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400846 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900850 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 .show = show_refcnt,
852};
853
Al Virof6a57032006-10-18 01:47:25 -0400854void module_put(struct module *module)
855{
856 if (module) {
857 unsigned int cpu = get_cpu();
858 local_dec(&module->ref[cpu].count);
859 /* Maybe they're waiting for us to drop reference? */
860 if (unlikely(!module_is_live(module)))
861 wake_up_process(module->waiter);
862 put_cpu();
863 }
864}
865EXPORT_SYMBOL(module_put);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867#else /* !CONFIG_MODULE_UNLOAD */
868static void print_unload_info(struct seq_file *m, struct module *mod)
869{
870 /* We don't know the usage count, or what modules are using. */
871 seq_printf(m, " - -");
872}
873
874static inline void module_unload_free(struct module *mod)
875{
876}
877
878static inline int use_module(struct module *a, struct module *b)
879{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500880 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static inline void module_unload_init(struct module *mod)
884{
885}
886#endif /* CONFIG_MODULE_UNLOAD */
887
Kay Sievers1f717402006-11-24 12:15:25 +0100888static ssize_t show_initstate(struct module_attribute *mattr,
889 struct module *mod, char *buffer)
890{
891 const char *state = "unknown";
892
893 switch (mod->state) {
894 case MODULE_STATE_LIVE:
895 state = "live";
896 break;
897 case MODULE_STATE_COMING:
898 state = "coming";
899 break;
900 case MODULE_STATE_GOING:
901 state = "going";
902 break;
903 }
904 return sprintf(buffer, "%s\n", state);
905}
906
907static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900908 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100909 .show = show_initstate,
910};
911
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800912static struct module_attribute *modinfo_attrs[] = {
913 &modinfo_version,
914 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100915 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800916#ifdef CONFIG_MODULE_UNLOAD
917 &refcnt,
918#endif
919 NULL,
920};
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922static const char vermagic[] = VERMAGIC_STRING;
923
Linus Torvalds826e4502008-05-04 17:04:16 -0700924static int try_to_force_load(struct module *mod, const char *symname)
925{
926#ifdef CONFIG_MODULE_FORCE_LOAD
927 if (!(tainted & TAINT_FORCED_MODULE))
928 printk("%s: no version for \"%s\" found: kernel tainted.\n",
929 mod->name, symname);
930 add_taint_module(mod, TAINT_FORCED_MODULE);
931 return 0;
932#else
933 return -ENOEXEC;
934#endif
935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937#ifdef CONFIG_MODVERSIONS
938static int check_version(Elf_Shdr *sechdrs,
939 unsigned int versindex,
940 const char *symname,
941 struct module *mod,
942 const unsigned long *crc)
943{
944 unsigned int i, num_versions;
945 struct modversion_info *versions;
946
947 /* Exporting module didn't supply crcs? OK, we're already tainted. */
948 if (!crc)
949 return 1;
950
Rusty Russella5dd6972008-05-09 16:24:21 +1000951 /* No versions at all? modprobe --force does this. */
952 if (versindex == 0)
953 return try_to_force_load(mod, symname) == 0;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 versions = (void *) sechdrs[versindex].sh_addr;
956 num_versions = sechdrs[versindex].sh_size
957 / sizeof(struct modversion_info);
958
959 for (i = 0; i < num_versions; i++) {
960 if (strcmp(versions[i].name, symname) != 0)
961 continue;
962
963 if (versions[i].crc == *crc)
964 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 DEBUGP("Found checksum %lX vs module %lX\n",
966 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700967 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700969
Rusty Russella5dd6972008-05-09 16:24:21 +1000970 printk(KERN_WARNING "%s: no symbol version for %s\n",
971 mod->name, symname);
972 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700973
974bad_version:
975 printk("%s: disagrees about version of symbol %s\n",
976 mod->name, symname);
977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980static inline int check_modstruct_version(Elf_Shdr *sechdrs,
981 unsigned int versindex,
982 struct module *mod)
983{
984 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Rusty Russellad9546c2008-05-01 21:14:59 -0500986 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -0500988 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Rusty Russell91e37a72008-05-09 16:25:28 +1000991/* First part is kernel version, which we ignore if module has crcs. */
992static inline int same_magic(const char *amagic, const char *bmagic,
993 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Rusty Russell91e37a72008-05-09 16:25:28 +1000995 if (has_crcs) {
996 amagic += strcspn(amagic, " ");
997 bmagic += strcspn(bmagic, " ");
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return strcmp(amagic, bmagic) == 0;
1000}
1001#else
1002static inline int check_version(Elf_Shdr *sechdrs,
1003 unsigned int versindex,
1004 const char *symname,
1005 struct module *mod,
1006 const unsigned long *crc)
1007{
1008 return 1;
1009}
1010
1011static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1012 unsigned int versindex,
1013 struct module *mod)
1014{
1015 return 1;
1016}
1017
Rusty Russell91e37a72008-05-09 16:25:28 +10001018static inline int same_magic(const char *amagic, const char *bmagic,
1019 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 return strcmp(amagic, bmagic) == 0;
1022}
1023#endif /* CONFIG_MODVERSIONS */
1024
1025/* Resolve a symbol for this module. I.e. if we find one, record usage.
1026 Must be holding module_mutex. */
1027static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1028 unsigned int versindex,
1029 const char *name,
1030 struct module *mod)
1031{
1032 struct module *owner;
1033 unsigned long ret;
1034 const unsigned long *crc;
1035
Rusty Russellad9546c2008-05-01 21:14:59 -05001036 ret = find_symbol(name, &owner, &crc,
1037 !(mod->taints & TAINT_PROPRIETARY_MODULE), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001038 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001039 /* use_module can fail due to OOM,
1040 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1042 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001043 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return ret;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * /sys/module/foo/sections stuff
1050 * J. Corbet <corbet@lwn.net>
1051 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001052#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001053struct module_sect_attr
1054{
1055 struct module_attribute mattr;
1056 char *name;
1057 unsigned long address;
1058};
1059
1060struct module_sect_attrs
1061{
1062 struct attribute_group grp;
1063 unsigned int nsections;
1064 struct module_sect_attr attrs[0];
1065};
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static ssize_t module_sect_show(struct module_attribute *mattr,
1068 struct module *mod, char *buf)
1069{
1070 struct module_sect_attr *sattr =
1071 container_of(mattr, struct module_sect_attr, mattr);
1072 return sprintf(buf, "0x%lx\n", sattr->address);
1073}
1074
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001075static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1076{
Rusty Russella58730c2008-03-13 09:03:44 +00001077 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001078
1079 for (section = 0; section < sect_attrs->nsections; section++)
1080 kfree(sect_attrs->attrs[section].name);
1081 kfree(sect_attrs);
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084static void add_sect_attrs(struct module *mod, unsigned int nsect,
1085 char *secstrings, Elf_Shdr *sechdrs)
1086{
1087 unsigned int nloaded = 0, i, size[2];
1088 struct module_sect_attrs *sect_attrs;
1089 struct module_sect_attr *sattr;
1090 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* Count loaded sections and allocate structures */
1093 for (i = 0; i < nsect; i++)
1094 if (sechdrs[i].sh_flags & SHF_ALLOC)
1095 nloaded++;
1096 size[0] = ALIGN(sizeof(*sect_attrs)
1097 + nloaded * sizeof(sect_attrs->attrs[0]),
1098 sizeof(sect_attrs->grp.attrs[0]));
1099 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001100 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1101 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return;
1103
1104 /* Setup section attributes. */
1105 sect_attrs->grp.name = "sections";
1106 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1107
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001108 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 sattr = &sect_attrs->attrs[0];
1110 gattr = &sect_attrs->grp.attrs[0];
1111 for (i = 0; i < nsect; i++) {
1112 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1113 continue;
1114 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001115 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1116 GFP_KERNEL);
1117 if (sattr->name == NULL)
1118 goto out;
1119 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 sattr->mattr.show = module_sect_show;
1121 sattr->mattr.store = NULL;
1122 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 sattr->mattr.attr.mode = S_IRUGO;
1124 *(gattr++) = &(sattr++)->mattr.attr;
1125 }
1126 *gattr = NULL;
1127
1128 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1129 goto out;
1130
1131 mod->sect_attrs = sect_attrs;
1132 return;
1133 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001134 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137static void remove_sect_attrs(struct module *mod)
1138{
1139 if (mod->sect_attrs) {
1140 sysfs_remove_group(&mod->mkobj.kobj,
1141 &mod->sect_attrs->grp);
1142 /* We are positive that no one is using any sect attrs
1143 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001144 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 mod->sect_attrs = NULL;
1146 }
1147}
1148
Roland McGrath6d760132007-10-16 23:26:40 -07001149/*
1150 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1151 */
1152
1153struct module_notes_attrs {
1154 struct kobject *dir;
1155 unsigned int notes;
1156 struct bin_attribute attrs[0];
1157};
1158
1159static ssize_t module_notes_read(struct kobject *kobj,
1160 struct bin_attribute *bin_attr,
1161 char *buf, loff_t pos, size_t count)
1162{
1163 /*
1164 * The caller checked the pos and count against our size.
1165 */
1166 memcpy(buf, bin_attr->private + pos, count);
1167 return count;
1168}
1169
1170static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1171 unsigned int i)
1172{
1173 if (notes_attrs->dir) {
1174 while (i-- > 0)
1175 sysfs_remove_bin_file(notes_attrs->dir,
1176 &notes_attrs->attrs[i]);
1177 kobject_del(notes_attrs->dir);
1178 }
1179 kfree(notes_attrs);
1180}
1181
1182static void add_notes_attrs(struct module *mod, unsigned int nsect,
1183 char *secstrings, Elf_Shdr *sechdrs)
1184{
1185 unsigned int notes, loaded, i;
1186 struct module_notes_attrs *notes_attrs;
1187 struct bin_attribute *nattr;
1188
1189 /* Count notes sections and allocate structures. */
1190 notes = 0;
1191 for (i = 0; i < nsect; i++)
1192 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1193 (sechdrs[i].sh_type == SHT_NOTE))
1194 ++notes;
1195
1196 if (notes == 0)
1197 return;
1198
1199 notes_attrs = kzalloc(sizeof(*notes_attrs)
1200 + notes * sizeof(notes_attrs->attrs[0]),
1201 GFP_KERNEL);
1202 if (notes_attrs == NULL)
1203 return;
1204
1205 notes_attrs->notes = notes;
1206 nattr = &notes_attrs->attrs[0];
1207 for (loaded = i = 0; i < nsect; ++i) {
1208 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1209 continue;
1210 if (sechdrs[i].sh_type == SHT_NOTE) {
1211 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1212 nattr->attr.mode = S_IRUGO;
1213 nattr->size = sechdrs[i].sh_size;
1214 nattr->private = (void *) sechdrs[i].sh_addr;
1215 nattr->read = module_notes_read;
1216 ++nattr;
1217 }
1218 ++loaded;
1219 }
1220
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001221 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001222 if (!notes_attrs->dir)
1223 goto out;
1224
1225 for (i = 0; i < notes; ++i)
1226 if (sysfs_create_bin_file(notes_attrs->dir,
1227 &notes_attrs->attrs[i]))
1228 goto out;
1229
1230 mod->notes_attrs = notes_attrs;
1231 return;
1232
1233 out:
1234 free_notes_attrs(notes_attrs, i);
1235}
1236
1237static void remove_notes_attrs(struct module *mod)
1238{
1239 if (mod->notes_attrs)
1240 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1246 char *sectstrings, Elf_Shdr *sechdrs)
1247{
1248}
1249
1250static inline void remove_sect_attrs(struct module *mod)
1251{
1252}
Roland McGrath6d760132007-10-16 23:26:40 -07001253
1254static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1255 char *sectstrings, Elf_Shdr *sechdrs)
1256{
1257}
1258
1259static inline void remove_notes_attrs(struct module *mod)
1260{
1261}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Randy Dunlapef665c12007-02-13 15:19:06 -08001264#ifdef CONFIG_SYSFS
1265int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001266{
1267 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001268 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001269 int error = 0;
1270 int i;
1271
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001272 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1273 (ARRAY_SIZE(modinfo_attrs) + 1)),
1274 GFP_KERNEL);
1275 if (!mod->modinfo_attrs)
1276 return -ENOMEM;
1277
1278 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001279 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1280 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001281 (attr->test && attr->test(mod))) {
1282 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001283 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1284 ++temp_attr;
1285 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001286 }
1287 return error;
1288}
1289
Randy Dunlapef665c12007-02-13 15:19:06 -08001290void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001291{
1292 struct module_attribute *attr;
1293 int i;
1294
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001295 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1296 /* pick a field to test for end of list */
1297 if (!attr->attr.name)
1298 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001299 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001300 if (attr->free)
1301 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001302 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001303 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Randy Dunlapef665c12007-02-13 15:19:06 -08001306int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001309 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001311 if (!module_sysfs_initialized) {
1312 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001313 mod->name);
1314 err = -EINVAL;
1315 goto out;
1316 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001317
1318 kobj = kset_find_obj(module_kset, mod->name);
1319 if (kobj) {
1320 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1321 kobject_put(kobj);
1322 err = -EINVAL;
1323 goto out;
1324 }
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001327
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001328 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1329 mod->mkobj.kobj.kset = module_kset;
1330 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1331 "%s", mod->name);
1332 if (err)
1333 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001334
Kay Sievers97c146e2007-11-29 23:46:11 +01001335 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001336out:
1337 return err;
1338}
1339
Randy Dunlapef665c12007-02-13 15:19:06 -08001340int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001341 struct kernel_param *kparam,
1342 unsigned int num_params)
1343{
1344 int err;
1345
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001346 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001347 if (!mod->holders_dir) {
1348 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001349 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001350 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 err = module_param_sysfs_setup(mod, kparam, num_params);
1353 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001354 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Matt Domschc988d2b2005-06-23 22:05:15 -07001356 err = module_add_modinfo_attrs(mod);
1357 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001358 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001359
Kay Sieverse17e0f52006-11-24 12:15:25 +01001360 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
1362
Kay Sieverse17e0f52006-11-24 12:15:25 +01001363out_unreg_param:
1364 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001365out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001366 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001367out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001368 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return err;
1370}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001371
1372static void mod_sysfs_fini(struct module *mod)
1373{
1374 kobject_put(&mod->mkobj.kobj);
1375}
1376
1377#else /* CONFIG_SYSFS */
1378
1379static void mod_sysfs_fini(struct module *mod)
1380{
1381}
1382
1383#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385static void mod_kobject_remove(struct module *mod)
1386{
Matt Domschc988d2b2005-06-23 22:05:15 -07001387 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001389 kobject_put(mod->mkobj.drivers_dir);
1390 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001391 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394/*
Rusty Russellbb9d3d52008-01-29 17:13:21 -05001395 * link the module with the whole machine is stopped with interrupts off
1396 * - this defends against kallsyms not taking locks
1397 */
1398static int __link_module(void *_mod)
1399{
1400 struct module *mod = _mod;
1401 list_add(&mod->list, &modules);
1402 return 0;
1403}
1404
1405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 * unlink the module with the whole machine is stopped with interrupts off
1407 * - this defends against kallsyms not taking locks
1408 */
1409static int __unlink_module(void *_mod)
1410{
1411 struct module *mod = _mod;
1412 list_del(&mod->list);
1413 return 0;
1414}
1415
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001416/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417static void free_module(struct module *mod)
1418{
1419 /* Delete from various lists */
1420 stop_machine_run(__unlink_module, mod, NR_CPUS);
Roland McGrath6d760132007-10-16 23:26:40 -07001421 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 remove_sect_attrs(mod);
1423 mod_kobject_remove(mod);
1424
Jan Beulich4552d5d2006-06-26 13:57:28 +02001425 unwind_remove_table(mod->unwind_info, 0);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* Arch-specific cleanup. */
1428 module_arch_cleanup(mod);
1429
1430 /* Module unload stuff */
1431 module_unload_free(mod);
1432
1433 /* This may be NULL, but that's OK */
1434 module_free(mod, mod->module_init);
1435 kfree(mod->args);
1436 if (mod->percpu)
1437 percpu_modfree(mod->percpu);
1438
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001439 /* Free lock-classes: */
1440 lockdep_free_key_range(mod->module_core, mod->core_size);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* Finally, free the core (containing the module structure) */
1443 module_free(mod, mod->module_core);
1444}
1445
1446void *__symbol_get(const char *symbol)
1447{
1448 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001449 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Rusty Russell24da1cb2007-07-15 23:41:46 -07001451 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001452 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001453 if (IS_ERR_VALUE(value))
1454 value = 0;
1455 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001457 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 return (void *)value;
1460}
1461EXPORT_SYMBOL_GPL(__symbol_get);
1462
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001463/*
1464 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001465 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001466 */
1467static int verify_export_symbols(struct module *mod)
1468{
Rusty Russellb2111042008-05-01 21:15:00 -05001469 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001470 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001471 const struct kernel_symbol *s;
1472 struct {
1473 const struct kernel_symbol *sym;
1474 unsigned int num;
1475 } arr[] = {
1476 { mod->syms, mod->num_syms },
1477 { mod->gpl_syms, mod->num_gpl_syms },
1478 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1479 { mod->unused_syms, mod->num_unused_syms },
1480 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1481 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001482
Rusty Russellb2111042008-05-01 21:15:00 -05001483 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1484 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1485 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1486 NULL, true, false))) {
1487 printk(KERN_ERR
1488 "%s: exports duplicate symbol %s"
1489 " (owned by %s)\n",
1490 mod->name, s->name, module_name(owner));
1491 return -ENOEXEC;
1492 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001493 }
Rusty Russellb2111042008-05-01 21:15:00 -05001494 }
1495 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001496}
1497
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001498/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499static int simplify_symbols(Elf_Shdr *sechdrs,
1500 unsigned int symindex,
1501 const char *strtab,
1502 unsigned int versindex,
1503 unsigned int pcpuindex,
1504 struct module *mod)
1505{
1506 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1507 unsigned long secbase;
1508 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1509 int ret = 0;
1510
1511 for (i = 1; i < n; i++) {
1512 switch (sym[i].st_shndx) {
1513 case SHN_COMMON:
1514 /* We compiled with -fno-common. These are not
1515 supposed to happen. */
1516 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1517 printk("%s: please compile with -fno-common\n",
1518 mod->name);
1519 ret = -ENOEXEC;
1520 break;
1521
1522 case SHN_ABS:
1523 /* Don't need to do anything */
1524 DEBUGP("Absolute symbol: 0x%08lx\n",
1525 (long)sym[i].st_value);
1526 break;
1527
1528 case SHN_UNDEF:
1529 sym[i].st_value
1530 = resolve_symbol(sechdrs, versindex,
1531 strtab + sym[i].st_name, mod);
1532
1533 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001534 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536 /* Ok if weak. */
1537 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1538 break;
1539
1540 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1541 mod->name, strtab + sym[i].st_name);
1542 ret = -ENOENT;
1543 break;
1544
1545 default:
1546 /* Divert to percpu allocation if a percpu var. */
1547 if (sym[i].st_shndx == pcpuindex)
1548 secbase = (unsigned long)mod->percpu;
1549 else
1550 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1551 sym[i].st_value += secbase;
1552 break;
1553 }
1554 }
1555
1556 return ret;
1557}
1558
1559/* Update size with this section: return offset. */
1560static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1561{
1562 long ret;
1563
1564 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1565 *size = ret + sechdr->sh_size;
1566 return ret;
1567}
1568
1569/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1570 might -- code, read-only data, read-write data, small data. Tally
1571 sizes, and place the offsets into sh_entsize fields: high bit means it
1572 belongs in init. */
1573static void layout_sections(struct module *mod,
1574 const Elf_Ehdr *hdr,
1575 Elf_Shdr *sechdrs,
1576 const char *secstrings)
1577{
1578 static unsigned long const masks[][2] = {
1579 /* NOTE: all executable code must be the first section
1580 * in this array; otherwise modify the text_size
1581 * finder in the two loops below */
1582 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1583 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1584 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1585 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1586 };
1587 unsigned int m, i;
1588
1589 for (i = 0; i < hdr->e_shnum; i++)
1590 sechdrs[i].sh_entsize = ~0UL;
1591
1592 DEBUGP("Core section allocation order:\n");
1593 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1594 for (i = 0; i < hdr->e_shnum; ++i) {
1595 Elf_Shdr *s = &sechdrs[i];
1596
1597 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1598 || (s->sh_flags & masks[m][1])
1599 || s->sh_entsize != ~0UL
1600 || strncmp(secstrings + s->sh_name,
1601 ".init", 5) == 0)
1602 continue;
1603 s->sh_entsize = get_offset(&mod->core_size, s);
1604 DEBUGP("\t%s\n", secstrings + s->sh_name);
1605 }
1606 if (m == 0)
1607 mod->core_text_size = mod->core_size;
1608 }
1609
1610 DEBUGP("Init section allocation order:\n");
1611 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1612 for (i = 0; i < hdr->e_shnum; ++i) {
1613 Elf_Shdr *s = &sechdrs[i];
1614
1615 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1616 || (s->sh_flags & masks[m][1])
1617 || s->sh_entsize != ~0UL
1618 || strncmp(secstrings + s->sh_name,
1619 ".init", 5) != 0)
1620 continue;
1621 s->sh_entsize = (get_offset(&mod->init_size, s)
1622 | INIT_OFFSET_MASK);
1623 DEBUGP("\t%s\n", secstrings + s->sh_name);
1624 }
1625 if (m == 0)
1626 mod->init_text_size = mod->init_size;
1627 }
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630static void set_license(struct module *mod, const char *license)
1631{
1632 if (!license)
1633 license = "unspecified";
1634
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001635 if (!license_is_gpl_compatible(license)) {
1636 if (!(tainted & TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001637 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001638 "kernel.\n", mod->name, license);
1639 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641}
1642
1643/* Parse tag=value strings from .modinfo section */
1644static char *next_string(char *string, unsigned long *secsize)
1645{
1646 /* Skip non-zero chars */
1647 while (string[0]) {
1648 string++;
1649 if ((*secsize)-- <= 1)
1650 return NULL;
1651 }
1652
1653 /* Skip any zero padding. */
1654 while (!string[0]) {
1655 string++;
1656 if ((*secsize)-- <= 1)
1657 return NULL;
1658 }
1659 return string;
1660}
1661
1662static char *get_modinfo(Elf_Shdr *sechdrs,
1663 unsigned int info,
1664 const char *tag)
1665{
1666 char *p;
1667 unsigned int taglen = strlen(tag);
1668 unsigned long size = sechdrs[info].sh_size;
1669
1670 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1671 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1672 return p + taglen + 1;
1673 }
1674 return NULL;
1675}
1676
Matt Domschc988d2b2005-06-23 22:05:15 -07001677static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1678 unsigned int infoindex)
1679{
1680 struct module_attribute *attr;
1681 int i;
1682
1683 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1684 if (attr->setup)
1685 attr->setup(mod,
1686 get_modinfo(sechdrs,
1687 infoindex,
1688 attr->attr.name));
1689 }
1690}
Matt Domschc988d2b2005-06-23 22:05:15 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#ifdef CONFIG_KALLSYMS
Alexey Dobriyanea078902007-05-08 00:28:39 -07001693static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001695 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1696 return 1;
1697 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001698 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001700 else
1701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704/* As per nm */
1705static char elf_type(const Elf_Sym *sym,
1706 Elf_Shdr *sechdrs,
1707 const char *secstrings,
1708 struct module *mod)
1709{
1710 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1711 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1712 return 'v';
1713 else
1714 return 'w';
1715 }
1716 if (sym->st_shndx == SHN_UNDEF)
1717 return 'U';
1718 if (sym->st_shndx == SHN_ABS)
1719 return 'a';
1720 if (sym->st_shndx >= SHN_LORESERVE)
1721 return '?';
1722 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1723 return 't';
1724 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1725 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1726 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1727 return 'r';
1728 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1729 return 'g';
1730 else
1731 return 'd';
1732 }
1733 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1734 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1735 return 's';
1736 else
1737 return 'b';
1738 }
1739 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1740 ".debug", strlen(".debug")) == 0)
1741 return 'n';
1742 return '?';
1743}
1744
1745static void add_kallsyms(struct module *mod,
1746 Elf_Shdr *sechdrs,
1747 unsigned int symindex,
1748 unsigned int strindex,
1749 const char *secstrings)
1750{
1751 unsigned int i;
1752
1753 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1754 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1755 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1756
1757 /* Set types up while we still have access to sections. */
1758 for (i = 0; i < mod->num_symtab; i++)
1759 mod->symtab[i].st_info
1760 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1761}
1762#else
1763static inline void add_kallsyms(struct module *mod,
1764 Elf_Shdr *sechdrs,
1765 unsigned int symindex,
1766 unsigned int strindex,
1767 const char *secstrings)
1768{
1769}
1770#endif /* CONFIG_KALLSYMS */
1771
1772/* Allocate and load the module: note that size of section 0 is always
1773 zero, and we rely on this for optional sections. */
1774static struct module *load_module(void __user *umod,
1775 unsigned long len,
1776 const char __user *uargs)
1777{
1778 Elf_Ehdr *hdr;
1779 Elf_Shdr *sechdrs;
1780 char *secstrings, *args, *modmagic, *strtab = NULL;
Andrew Morton84860f92006-06-28 04:26:46 -07001781 unsigned int i;
1782 unsigned int symindex = 0;
1783 unsigned int strindex = 0;
1784 unsigned int setupindex;
1785 unsigned int exindex;
1786 unsigned int exportindex;
1787 unsigned int modindex;
1788 unsigned int obsparmindex;
1789 unsigned int infoindex;
1790 unsigned int gplindex;
1791 unsigned int crcindex;
1792 unsigned int gplcrcindex;
1793 unsigned int versindex;
1794 unsigned int pcpuindex;
1795 unsigned int gplfutureindex;
1796 unsigned int gplfuturecrcindex;
1797 unsigned int unwindex = 0;
1798 unsigned int unusedindex;
1799 unsigned int unusedcrcindex;
1800 unsigned int unusedgplindex;
1801 unsigned int unusedgplcrcindex;
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001802 unsigned int markersindex;
1803 unsigned int markersstringsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 struct module *mod;
1805 long err = 0;
1806 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1807 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001808 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1811 umod, len, uargs);
1812 if (len < sizeof(*hdr))
1813 return ERR_PTR(-ENOEXEC);
1814
1815 /* Suck in entire file: we'll want most of it. */
1816 /* vmalloc barfs on "unusual" numbers. Check here */
1817 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1818 return ERR_PTR(-ENOMEM);
1819 if (copy_from_user(hdr, umod, len) != 0) {
1820 err = -EFAULT;
1821 goto free_hdr;
1822 }
1823
1824 /* Sanity checks against insmoding binaries or wrong arch,
1825 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001826 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 || hdr->e_type != ET_REL
1828 || !elf_check_arch(hdr)
1829 || hdr->e_shentsize != sizeof(*sechdrs)) {
1830 err = -ENOEXEC;
1831 goto free_hdr;
1832 }
1833
1834 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1835 goto truncated;
1836
1837 /* Convenience variables */
1838 sechdrs = (void *)hdr + hdr->e_shoff;
1839 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1840 sechdrs[0].sh_addr = 0;
1841
1842 for (i = 1; i < hdr->e_shnum; i++) {
1843 if (sechdrs[i].sh_type != SHT_NOBITS
1844 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1845 goto truncated;
1846
1847 /* Mark all sections sh_addr with their address in the
1848 temporary image. */
1849 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1850
1851 /* Internal symbols and strings. */
1852 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1853 symindex = i;
1854 strindex = sechdrs[i].sh_link;
1855 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1856 }
1857#ifndef CONFIG_MODULE_UNLOAD
1858 /* Don't load .exit sections */
1859 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1860 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1861#endif
1862 }
1863
1864 modindex = find_sec(hdr, sechdrs, secstrings,
1865 ".gnu.linkonce.this_module");
1866 if (!modindex) {
1867 printk(KERN_WARNING "No module found in object\n");
1868 err = -ENOEXEC;
1869 goto free_hdr;
1870 }
1871 mod = (void *)sechdrs[modindex].sh_addr;
1872
1873 if (symindex == 0) {
1874 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1875 mod->name);
1876 err = -ENOEXEC;
1877 goto free_hdr;
1878 }
1879
1880 /* Optional sections */
1881 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1882 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001883 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001884 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1885 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1887 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001888 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001889 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1890 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1892 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1893 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1894 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1895 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1896 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001897#ifdef ARCH_UNWIND_SECTION_NAME
1898 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Rusty Russellea01e792008-03-13 09:02:17 +00001901 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001903 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904#ifdef CONFIG_KALLSYMS
1905 /* Keep symbol and string tables for decoding later. */
1906 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1907 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1908#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001909 if (unwindex)
1910 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* Check module struct version now, before we try to use module. */
1913 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1914 err = -ENOEXEC;
1915 goto free_hdr;
1916 }
1917
1918 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1919 /* This is allowed: modprobe --force will invalidate it. */
1920 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001921 err = try_to_force_load(mod, "magic");
1922 if (err)
1923 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001924 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1926 mod->name, modmagic, vermagic);
1927 err = -ENOEXEC;
1928 goto free_hdr;
1929 }
1930
1931 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001932 args = strndup_user(uargs, ~0UL >> 1);
1933 if (IS_ERR(args)) {
1934 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto free_hdr;
1936 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (find_module(mod->name)) {
1939 err = -EEXIST;
1940 goto free_mod;
1941 }
1942
1943 mod->state = MODULE_STATE_COMING;
1944
1945 /* Allow arches to frob section contents and sizes. */
1946 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1947 if (err < 0)
1948 goto free_mod;
1949
1950 if (pcpuindex) {
1951 /* We have a special allocation for this section. */
1952 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001953 sechdrs[pcpuindex].sh_addralign,
1954 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (!percpu) {
1956 err = -ENOMEM;
1957 goto free_mod;
1958 }
1959 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1960 mod->percpu = percpu;
1961 }
1962
1963 /* Determine total sizes, and put offsets in sh_entsize. For now
1964 this is done generically; there doesn't appear to be any
1965 special cases for the architectures. */
1966 layout_sections(mod, hdr, sechdrs, secstrings);
1967
1968 /* Do the allocs. */
1969 ptr = module_alloc(mod->core_size);
1970 if (!ptr) {
1971 err = -ENOMEM;
1972 goto free_percpu;
1973 }
1974 memset(ptr, 0, mod->core_size);
1975 mod->module_core = ptr;
1976
1977 ptr = module_alloc(mod->init_size);
1978 if (!ptr && mod->init_size) {
1979 err = -ENOMEM;
1980 goto free_core;
1981 }
1982 memset(ptr, 0, mod->init_size);
1983 mod->module_init = ptr;
1984
1985 /* Transfer each section which specifies SHF_ALLOC */
1986 DEBUGP("final section addresses:\n");
1987 for (i = 0; i < hdr->e_shnum; i++) {
1988 void *dest;
1989
1990 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1991 continue;
1992
1993 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1994 dest = mod->module_init
1995 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1996 else
1997 dest = mod->module_core + sechdrs[i].sh_entsize;
1998
1999 if (sechdrs[i].sh_type != SHT_NOBITS)
2000 memcpy(dest, (void *)sechdrs[i].sh_addr,
2001 sechdrs[i].sh_size);
2002 /* Update sh_addr to point to copy in image. */
2003 sechdrs[i].sh_addr = (unsigned long)dest;
2004 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2005 }
2006 /* Module has been moved. */
2007 mod = (void *)sechdrs[modindex].sh_addr;
2008
2009 /* Now we've moved module, initialize linked lists, etc. */
2010 module_unload_init(mod);
2011
Kay Sievers97c146e2007-11-29 23:46:11 +01002012 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002013 err = mod_sysfs_init(mod);
2014 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002015 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /* Set up license info based on the info section */
2018 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2019
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002020 /*
2021 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2022 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2023 * using GPL-only symbols it needs.
2024 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002025 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002026 add_taint(TAINT_PROPRIETARY_MODULE);
2027
2028 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002029 if (strcmp(mod->name, "driverloader") == 0)
2030 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002031
Matt Domschc988d2b2005-06-23 22:05:15 -07002032 /* Set up MODINFO_ATTR fields */
2033 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* Fix up syms, so that st_value is a pointer to location. */
2036 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2037 mod);
2038 if (err < 0)
2039 goto cleanup;
2040
2041 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2042 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2043 mod->syms = (void *)sechdrs[exportindex].sh_addr;
2044 if (crcindex)
2045 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2046 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2047 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2048 if (gplcrcindex)
2049 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002050 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2051 sizeof(*mod->gpl_future_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002052 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2053 sizeof(*mod->unused_syms);
2054 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2055 sizeof(*mod->unused_gpl_syms);
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002056 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2057 if (gplfuturecrcindex)
2058 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002060 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2061 if (unusedcrcindex)
2062 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2063 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2064 if (unusedgplcrcindex)
Rusty Russell4e2d9242008-05-01 21:15:00 -05002065 mod->unused_gpl_crcs
2066 = (void *)sechdrs[unusedgplcrcindex].sh_addr;
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068#ifdef CONFIG_MODVERSIONS
Daniel Walker22a8bde2007-10-18 03:06:07 -07002069 if ((mod->num_syms && !crcindex) ||
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002070 (mod->num_gpl_syms && !gplcrcindex) ||
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002071 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
2072 (mod->num_unused_syms && !unusedcrcindex) ||
2073 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002074 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2075 err = try_to_force_load(mod, "nocrc");
2076 if (err)
2077 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002080 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2081 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2082 "__markers_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* Now do relocations. */
2085 for (i = 1; i < hdr->e_shnum; i++) {
2086 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2087 unsigned int info = sechdrs[i].sh_info;
2088
2089 /* Not a valid relocation section? */
2090 if (info >= hdr->e_shnum)
2091 continue;
2092
2093 /* Don't bother with non-allocated sections */
2094 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2095 continue;
2096
2097 if (sechdrs[i].sh_type == SHT_REL)
2098 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2099 else if (sechdrs[i].sh_type == SHT_RELA)
2100 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2101 mod);
2102 if (err < 0)
2103 goto cleanup;
2104 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002105#ifdef CONFIG_MARKERS
2106 mod->markers = (void *)sechdrs[markersindex].sh_addr;
2107 mod->num_markers =
2108 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002111 /* Find duplicate symbols */
2112 err = verify_export_symbols(mod);
2113
2114 if (err < 0)
2115 goto cleanup;
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* Set up and sort exception table */
2118 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2119 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2120 sort_extable(extable, extable + mod->num_exentries);
2121
2122 /* Finally, copy percpu area over. */
2123 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2124 sechdrs[pcpuindex].sh_size);
2125
2126 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2127
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002128#ifdef CONFIG_MARKERS
2129 if (!mod->taints)
2130 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002131 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 err = module_finalize(hdr, sechdrs, mod);
2134 if (err < 0)
2135 goto cleanup;
2136
Thomas Koeller378bac82005-09-06 15:17:11 -07002137 /* flush the icache in correct context */
2138 old_fs = get_fs();
2139 set_fs(KERNEL_DS);
2140
2141 /*
2142 * Flush the instruction cache, since we've played with text.
2143 * Do it before processing of module parameters, so the module
2144 * can provide parameter accessor functions of its own.
2145 */
2146 if (mod->module_init)
2147 flush_icache_range((unsigned long)mod->module_init,
2148 (unsigned long)mod->module_init
2149 + mod->init_size);
2150 flush_icache_range((unsigned long)mod->module_core,
2151 (unsigned long)mod->module_core + mod->core_size);
2152
2153 set_fs(old_fs);
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002156 if (obsparmindex)
2157 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2158 mod->name);
2159
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002160 /* Now sew it into the lists so we can get lockdep and oops
2161 * info during argument parsing. Noone should access us, since
2162 * strong_try_module_get() will fail. */
2163 stop_machine_run(__link_module, mod, NR_CPUS);
2164
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002165 /* Size of section 0 is 0, so this works well if no params */
2166 err = parse_args(mod->name, mod->args,
2167 (struct kernel_param *)
2168 sechdrs[setupindex].sh_addr,
2169 sechdrs[setupindex].sh_size
2170 / sizeof(struct kernel_param),
2171 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002173 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Daniel Walker22a8bde2007-10-18 03:06:07 -07002175 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 (struct kernel_param *)
2177 sechdrs[setupindex].sh_addr,
2178 sechdrs[setupindex].sh_size
2179 / sizeof(struct kernel_param));
2180 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002181 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002183 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Jan Beulich4552d5d2006-06-26 13:57:28 +02002185 /* Size of section 0 is 0, so this works well if no unwind info. */
2186 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002187 (void *)sechdrs[unwindex].sh_addr,
2188 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* Get rid of temporary copy */
2191 vfree(hdr);
2192
2193 /* Done! */
2194 return mod;
2195
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002196 unlink:
2197 stop_machine_run(__unlink_module, mod, NR_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 module_arch_cleanup(mod);
2199 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002200 kobject_del(&mod->mkobj.kobj);
2201 kobject_put(&mod->mkobj.kobj);
2202 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 module_unload_free(mod);
2204 module_free(mod, mod->module_init);
2205 free_core:
2206 module_free(mod, mod->module_core);
2207 free_percpu:
2208 if (percpu)
2209 percpu_modfree(percpu);
2210 free_mod:
2211 kfree(args);
2212 free_hdr:
2213 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002214 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 truncated:
2217 printk(KERN_ERR "Module len %lu truncated\n", len);
2218 err = -ENOEXEC;
2219 goto free_hdr;
2220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222/* This is where the real work happens */
2223asmlinkage long
2224sys_init_module(void __user *umod,
2225 unsigned long len,
2226 const char __user *uargs)
2227{
2228 struct module *mod;
2229 int ret = 0;
2230
2231 /* Must have permission */
2232 if (!capable(CAP_SYS_MODULE))
2233 return -EPERM;
2234
2235 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002236 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return -EINTR;
2238
2239 /* Do all the hard work */
2240 mod = load_module(umod, len, uargs);
2241 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002242 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return PTR_ERR(mod);
2244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002247 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Alan Sterne041c682006-03-27 01:16:30 -08002249 blocking_notifier_call_chain(&module_notify_list,
2250 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 /* Start the module */
2253 if (mod->init != NULL)
2254 ret = mod->init();
2255 if (ret < 0) {
2256 /* Init routine failed: abort. Try to protect us from
2257 buggy refcounters. */
2258 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002259 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002260 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002261 blocking_notifier_call_chain(&module_notify_list,
2262 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002263 mutex_lock(&module_mutex);
2264 free_module(mod);
2265 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002266 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return ret;
2268 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002269 if (ret > 0) {
2270 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2271 "it should follow 0/-E convention\n"
2272 KERN_WARNING "%s: loading module anyway...\n",
2273 __func__, mod->name, ret,
2274 __func__);
2275 dump_stack();
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Rusty Russell6c5db222008-03-10 11:43:52 -07002278 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002280 wake_up(&module_wq);
2281
2282 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* Drop initial reference. */
2284 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002285 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 module_free(mod, mod->module_init);
2287 mod->module_init = NULL;
2288 mod->init_size = 0;
2289 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002290 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 return 0;
2293}
2294
2295static inline int within(unsigned long addr, void *start, unsigned long size)
2296{
2297 return ((void *)addr >= start && (void *)addr < start + size);
2298}
2299
2300#ifdef CONFIG_KALLSYMS
2301/*
2302 * This ignores the intensely annoying "mapping symbols" found
2303 * in ARM ELF files: $a, $t and $d.
2304 */
2305static inline int is_arm_mapping_symbol(const char *str)
2306{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002307 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 && (str[2] == '\0' || str[2] == '.');
2309}
2310
2311static const char *get_ksymbol(struct module *mod,
2312 unsigned long addr,
2313 unsigned long *size,
2314 unsigned long *offset)
2315{
2316 unsigned int i, best = 0;
2317 unsigned long nextval;
2318
2319 /* At worse, next value is at end of module */
2320 if (within(addr, mod->module_init, mod->init_size))
2321 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002322 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2324
2325 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002326 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 for (i = 1; i < mod->num_symtab; i++) {
2328 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2329 continue;
2330
2331 /* We ignore unnamed symbols: they're uninformative
2332 * and inserted at a whim. */
2333 if (mod->symtab[i].st_value <= addr
2334 && mod->symtab[i].st_value > mod->symtab[best].st_value
2335 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2336 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2337 best = i;
2338 if (mod->symtab[i].st_value > addr
2339 && mod->symtab[i].st_value < nextval
2340 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2341 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2342 nextval = mod->symtab[i].st_value;
2343 }
2344
2345 if (!best)
2346 return NULL;
2347
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002348 if (size)
2349 *size = nextval - mod->symtab[best].st_value;
2350 if (offset)
2351 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return mod->strtab + mod->symtab[best].st_name;
2353}
2354
Rusty Russell6dd06c92008-01-29 17:13:22 -05002355/* For kallsyms to ask for address resolution. NULL means not found. Careful
2356 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002357const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002358 unsigned long *size,
2359 unsigned long *offset,
2360 char **modname,
2361 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002364 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Rusty Russellcb2a5202008-01-14 00:55:03 -08002366 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 list_for_each_entry(mod, &modules, list) {
2368 if (within(addr, mod->module_init, mod->init_size)
2369 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002370 if (modname)
2371 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002372 ret = get_ksymbol(mod, addr, size, offset);
2373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002376 /* Make a copy in here where it's safe */
2377 if (ret) {
2378 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2379 ret = namebuf;
2380 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002381 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002385int lookup_module_symbol_name(unsigned long addr, char *symname)
2386{
2387 struct module *mod;
2388
Rusty Russellcb2a5202008-01-14 00:55:03 -08002389 preempt_disable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002390 list_for_each_entry(mod, &modules, list) {
2391 if (within(addr, mod->module_init, mod->init_size) ||
2392 within(addr, mod->module_core, mod->core_size)) {
2393 const char *sym;
2394
2395 sym = get_ksymbol(mod, addr, NULL, NULL);
2396 if (!sym)
2397 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002398 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002399 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002400 return 0;
2401 }
2402 }
2403out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002404 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002405 return -ERANGE;
2406}
2407
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002408int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2409 unsigned long *offset, char *modname, char *name)
2410{
2411 struct module *mod;
2412
Rusty Russellcb2a5202008-01-14 00:55:03 -08002413 preempt_disable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002414 list_for_each_entry(mod, &modules, list) {
2415 if (within(addr, mod->module_init, mod->init_size) ||
2416 within(addr, mod->module_core, mod->core_size)) {
2417 const char *sym;
2418
2419 sym = get_ksymbol(mod, addr, size, offset);
2420 if (!sym)
2421 goto out;
2422 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002423 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002424 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002425 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002426 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002427 return 0;
2428 }
2429 }
2430out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002431 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002432 return -ERANGE;
2433}
2434
Alexey Dobriyanea078902007-05-08 00:28:39 -07002435int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2436 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 struct module *mod;
2439
Rusty Russellcb2a5202008-01-14 00:55:03 -08002440 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 list_for_each_entry(mod, &modules, list) {
2442 if (symnum < mod->num_symtab) {
2443 *value = mod->symtab[symnum].st_value;
2444 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002445 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002446 KSYM_NAME_LEN);
2447 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002448 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002449 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452 symnum -= mod->num_symtab;
2453 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002454 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002455 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
2458static unsigned long mod_find_symname(struct module *mod, const char *name)
2459{
2460 unsigned int i;
2461
2462 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002463 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2464 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return mod->symtab[i].st_value;
2466 return 0;
2467}
2468
2469/* Look for this name: can be of form module:name. */
2470unsigned long module_kallsyms_lookup_name(const char *name)
2471{
2472 struct module *mod;
2473 char *colon;
2474 unsigned long ret = 0;
2475
2476 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002477 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if ((colon = strchr(name, ':')) != NULL) {
2479 *colon = '\0';
2480 if ((mod = find_module(name)) != NULL)
2481 ret = mod_find_symname(mod, colon+1);
2482 *colon = ':';
2483 } else {
2484 list_for_each_entry(mod, &modules, list)
2485 if ((ret = mod_find_symname(mod, name)) != 0)
2486 break;
2487 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002488 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return ret;
2490}
2491#endif /* CONFIG_KALLSYMS */
2492
2493/* Called by the /proc file system to return a list of modules. */
2494static void *m_start(struct seq_file *m, loff_t *pos)
2495{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002496 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002497 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
2500static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2501{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002502 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505static void m_stop(struct seq_file *m, void *p)
2506{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002507 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002510static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002511{
2512 int bx = 0;
2513
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002514 if (mod->taints ||
2515 mod->state == MODULE_STATE_GOING ||
2516 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002517 buf[bx++] = '(';
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002518 if (mod->taints & TAINT_PROPRIETARY_MODULE)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002519 buf[bx++] = 'P';
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002520 if (mod->taints & TAINT_FORCED_MODULE)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002521 buf[bx++] = 'F';
2522 /*
2523 * TAINT_FORCED_RMMOD: could be added.
2524 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2525 * apply to modules.
2526 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002527
2528 /* Show a - for module-is-being-unloaded */
2529 if (mod->state == MODULE_STATE_GOING)
2530 buf[bx++] = '-';
2531 /* Show a + for module-is-being-loaded */
2532 if (mod->state == MODULE_STATE_COMING)
2533 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002534 buf[bx++] = ')';
2535 }
2536 buf[bx] = '\0';
2537
2538 return buf;
2539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541static int m_show(struct seq_file *m, void *p)
2542{
2543 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002544 char buf[8];
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 seq_printf(m, "%s %lu",
2547 mod->name, mod->init_size + mod->core_size);
2548 print_unload_info(m, mod);
2549
2550 /* Informative for users. */
2551 seq_printf(m, " %s",
2552 mod->state == MODULE_STATE_GOING ? "Unloading":
2553 mod->state == MODULE_STATE_COMING ? "Loading":
2554 "Live");
2555 /* Used by oprofile and other similar tools. */
2556 seq_printf(m, " 0x%p", mod->module_core);
2557
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002558 /* Taints info */
2559 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002560 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 seq_printf(m, "\n");
2563 return 0;
2564}
2565
2566/* Format: modulename size refcount deps address
2567
2568 Where refcount is a number or -, and deps is a comma-separated list
2569 of depends or -.
2570*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002571const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 .start = m_start,
2573 .next = m_next,
2574 .stop = m_stop,
2575 .show = m_show
2576};
2577
2578/* Given an address, look for it in the module exception tables. */
2579const struct exception_table_entry *search_module_extables(unsigned long addr)
2580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 const struct exception_table_entry *e = NULL;
2582 struct module *mod;
2583
Rusty Russell24da1cb2007-07-15 23:41:46 -07002584 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 list_for_each_entry(mod, &modules, list) {
2586 if (mod->num_exentries == 0)
2587 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 e = search_extable(mod->extable,
2590 mod->extable + mod->num_exentries - 1,
2591 addr);
2592 if (e)
2593 break;
2594 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002595 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002598 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return e;
2600}
2601
Ingo Molnar4d435f92006-07-03 00:24:24 -07002602/*
2603 * Is this a valid module address?
2604 */
2605int is_module_address(unsigned long addr)
2606{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002607 struct module *mod;
2608
Rusty Russell24da1cb2007-07-15 23:41:46 -07002609 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002610
2611 list_for_each_entry(mod, &modules, list) {
2612 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002613 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002614 return 1;
2615 }
2616 }
2617
Rusty Russell24da1cb2007-07-15 23:41:46 -07002618 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002619
2620 return 0;
2621}
2622
2623
Rusty Russell24da1cb2007-07-15 23:41:46 -07002624/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625struct module *__module_text_address(unsigned long addr)
2626{
2627 struct module *mod;
2628
2629 list_for_each_entry(mod, &modules, list)
2630 if (within(addr, mod->module_init, mod->init_text_size)
2631 || within(addr, mod->module_core, mod->core_text_size))
2632 return mod;
2633 return NULL;
2634}
2635
2636struct module *module_text_address(unsigned long addr)
2637{
2638 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Rusty Russell24da1cb2007-07-15 23:41:46 -07002640 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002642 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 return mod;
2645}
2646
2647/* Don't grab lock, we're oopsing. */
2648void print_modules(void)
2649{
2650 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002651 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 printk("Modules linked in:");
2654 list_for_each_entry(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002655 printk(" %s%s", mod->name, module_flags(mod, buf));
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002656 if (last_unloaded_module[0])
2657 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 printk("\n");
2659}
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661#ifdef CONFIG_MODVERSIONS
2662/* Generate the signature for struct module here, too, for modversions. */
2663void struct_module(struct module *mod) { return; }
2664EXPORT_SYMBOL(struct_module);
2665#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002666
2667#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002668void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002669{
2670 struct module *mod;
2671
2672 mutex_lock(&module_mutex);
2673 list_for_each_entry(mod, &modules, list)
2674 if (!mod->taints)
2675 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002676 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002677 mutex_unlock(&module_mutex);
2678}
2679#endif