blob: 0d8d21ee792c4e5812694162d241b7975ffd99bf [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>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040049#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040050#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#if 0
53#define DEBUGP printk
54#else
55#define DEBUGP(fmt , a...)
56#endif
57
58#ifndef ARCH_SHF_SMALL
59#define ARCH_SHF_SMALL 0
60#endif
61
62/* If this is set, the section belongs in the init part of the module */
63#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
64
Rusty Russell24da1cb2007-07-15 23:41:46 -070065/* List of modules, protected by module_mutex or preempt_disable
66 * (add/delete uses stop_machine). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080067static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static LIST_HEAD(modules);
69
Rusty Russellc9a3ba52008-01-29 17:13:18 -050070/* Waiting for a module to finish initializing? */
71static DECLARE_WAIT_QUEUE_HEAD(module_wq);
72
Alan Sterne041c682006-03-27 01:16:30 -080073static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Rusty Russell3a642e92008-07-22 19:24:28 -050075/* Bounds of module allocation, for speeding __module_text_address */
76static unsigned long module_addr_min = -1UL, module_addr_max = 0;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int register_module_notifier(struct notifier_block * nb)
79{
Alan Sterne041c682006-03-27 01:16:30 -080080 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82EXPORT_SYMBOL(register_module_notifier);
83
84int unregister_module_notifier(struct notifier_block * nb)
85{
Alan Sterne041c682006-03-27 01:16:30 -080086 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88EXPORT_SYMBOL(unregister_module_notifier);
89
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080090/* We require a truly strong try_module_get(): 0 means failure due to
91 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static inline int strong_try_module_get(struct module *mod)
93{
94 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050095 return -EBUSY;
96 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -050098 else
99 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700102static inline void add_taint_module(struct module *mod, unsigned flag)
103{
104 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700105 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700106}
107
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200108/*
109 * A thread that wants to hold a reference to a module only while it
110 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112void __module_put_and_exit(struct module *mod, long code)
113{
114 module_put(mod);
115 do_exit(code);
116}
117EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Find a module section: 0 means not found. */
120static unsigned int find_sec(Elf_Ehdr *hdr,
121 Elf_Shdr *sechdrs,
122 const char *secstrings,
123 const char *name)
124{
125 unsigned int i;
126
127 for (i = 1; i < hdr->e_shnum; i++)
128 /* Alloc bit cleared means "ignore it." */
129 if ((sechdrs[i].sh_flags & SHF_ALLOC)
130 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
131 return i;
132 return 0;
133}
134
135/* Provided by the linker */
136extern const struct kernel_symbol __start___ksymtab[];
137extern const struct kernel_symbol __stop___ksymtab[];
138extern const struct kernel_symbol __start___ksymtab_gpl[];
139extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800140extern const struct kernel_symbol __start___ksymtab_gpl_future[];
141extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700142extern const struct kernel_symbol __start___ksymtab_gpl_future[];
143extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144extern const unsigned long __start___kcrctab[];
145extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800146extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500147#ifdef CONFIG_UNUSED_SYMBOLS
148extern const struct kernel_symbol __start___ksymtab_unused[];
149extern const struct kernel_symbol __stop___ksymtab_unused[];
150extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
151extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700152extern const unsigned long __start___kcrctab_unused[];
153extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#ifndef CONFIG_MODVERSIONS
157#define symversion(base, idx) NULL
158#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800159#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
161
Rusty Russelldafd0942008-07-22 19:24:25 -0500162struct symsearch {
163 const struct kernel_symbol *start, *stop;
164 const unsigned long *crcs;
165 enum {
166 NOT_GPL_ONLY,
167 GPL_ONLY,
168 WILL_BE_GPL_ONLY,
169 } licence;
170 bool unused;
171};
172
173static bool each_symbol_in_section(const struct symsearch *arr,
174 unsigned int arrsize,
175 struct module *owner,
176 bool (*fn)(const struct symsearch *syms,
177 struct module *owner,
178 unsigned int symnum, void *data),
179 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100180{
Rusty Russelldafd0942008-07-22 19:24:25 -0500181 unsigned int i, j;
182
183 for (j = 0; j < arrsize; j++) {
184 for (i = 0; i < arr[j].stop - arr[j].start; i++)
185 if (fn(&arr[j], owner, i, data))
186 return true;
187 }
188
189 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100190}
191
Rusty Russelldafd0942008-07-22 19:24:25 -0500192/* Returns true as soon as fn returns true, otherwise false. */
193static bool each_symbol(bool (*fn)(const struct symsearch *arr,
194 struct module *owner,
195 unsigned int symnum, void *data),
196 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700197{
Rusty Russelldafd0942008-07-22 19:24:25 -0500198 struct module *mod;
199 const struct symsearch arr[] = {
200 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
201 NOT_GPL_ONLY, false },
202 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
203 __start___kcrctab_gpl,
204 GPL_ONLY, false },
205 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
206 __start___kcrctab_gpl_future,
207 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500208#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500209 { __start___ksymtab_unused, __stop___ksymtab_unused,
210 __start___kcrctab_unused,
211 NOT_GPL_ONLY, true },
212 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
213 __start___kcrctab_unused_gpl,
214 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500215#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500216 };
217
218 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
219 return true;
220
221 list_for_each_entry(mod, &modules, list) {
222 struct symsearch arr[] = {
223 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
224 NOT_GPL_ONLY, false },
225 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
226 mod->gpl_crcs,
227 GPL_ONLY, false },
228 { mod->gpl_future_syms,
229 mod->gpl_future_syms + mod->num_gpl_future_syms,
230 mod->gpl_future_crcs,
231 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500232#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500233 { mod->unused_syms,
234 mod->unused_syms + mod->num_unused_syms,
235 mod->unused_crcs,
236 NOT_GPL_ONLY, true },
237 { mod->unused_gpl_syms,
238 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
239 mod->unused_gpl_crcs,
240 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500241#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 };
243
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
245 return true;
246 }
247 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700248}
249
Rusty Russelldafd0942008-07-22 19:24:25 -0500250struct find_symbol_arg {
251 /* Input */
252 const char *name;
253 bool gplok;
254 bool warn;
255
256 /* Output */
257 struct module *owner;
258 const unsigned long *crc;
259 unsigned long value;
260};
261
262static bool find_symbol_in_section(const struct symsearch *syms,
263 struct module *owner,
264 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500265{
Rusty Russelldafd0942008-07-22 19:24:25 -0500266 struct find_symbol_arg *fsa = data;
267
268 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
269 return false;
270
271 if (!fsa->gplok) {
272 if (syms->licence == GPL_ONLY)
273 return false;
274 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", fsa->name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more details.\n");
281 }
282 }
283
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500284#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500285 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500286 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500287 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500288 printk(KERN_WARNING
289 "This symbol will go away in the future.\n");
290 printk(KERN_WARNING
291 "Please evalute if this is the right api to use and if "
292 "it really is, submit a report the linux kernel "
293 "mailinglist together with submitting your code for "
294 "inclusion.\n");
295 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500296#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500297
298 fsa->owner = owner;
299 fsa->crc = symversion(syms->crcs, symnum);
300 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500301 return true;
302}
303
Rusty Russellad9546c2008-05-01 21:14:59 -0500304/* Find a symbol, return value, (optional) crc and (optional) module
305 * which owns it */
306static unsigned long find_symbol(const char *name,
307 struct module **owner,
308 const unsigned long **crc,
309 bool gplok,
310 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 fsa.name = name;
315 fsa.gplok = gplok;
316 fsa.warn = warn;
317
318 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500319 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500320 *owner = fsa.owner;
321 if (crc)
322 *crc = fsa.crc;
323 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800327 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/* Search for module by name: must hold module_mutex. */
331static struct module *find_module(const char *name)
332{
333 struct module *mod;
334
335 list_for_each_entry(mod, &modules, list) {
336 if (strcmp(mod->name, name) == 0)
337 return mod;
338 }
339 return NULL;
340}
341
342#ifdef CONFIG_SMP
343/* Number of blocks used and allocated. */
344static unsigned int pcpu_num_used, pcpu_num_allocated;
345/* Size of each block. -ve means used. */
346static int *pcpu_size;
347
348static int split_block(unsigned int i, unsigned short size)
349{
350 /* Reallocation required? */
351 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700352 int *new;
353
354 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
355 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!new)
357 return 0;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 pcpu_size = new;
361 }
362
363 /* Insert a new subblock */
364 memmove(&pcpu_size[i+1], &pcpu_size[i],
365 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
366 pcpu_num_used++;
367
368 pcpu_size[i+1] -= size;
369 pcpu_size[i] = size;
370 return 1;
371}
372
373static inline unsigned int block_size(int val)
374{
375 if (val < 0)
376 return -val;
377 return val;
378}
379
Rusty Russell842bbaa2005-08-01 21:11:47 -0700380static void *percpu_modalloc(unsigned long size, unsigned long align,
381 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 unsigned long extra;
384 unsigned int i;
385 void *ptr;
386
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200387 if (align > PAGE_SIZE) {
388 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
389 name, align, PAGE_SIZE);
390 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 ptr = __per_cpu_start;
394 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
395 /* Extra for alignment requirement. */
396 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
397 BUG_ON(i == 0 && extra != 0);
398
399 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
400 continue;
401
402 /* Transfer extra to previous block. */
403 if (pcpu_size[i-1] < 0)
404 pcpu_size[i-1] -= extra;
405 else
406 pcpu_size[i-1] += extra;
407 pcpu_size[i] -= extra;
408 ptr += extra;
409
410 /* Split block if warranted */
411 if (pcpu_size[i] - size > sizeof(unsigned long))
412 if (!split_block(i, size))
413 return NULL;
414
415 /* Mark allocated */
416 pcpu_size[i] = -pcpu_size[i];
417 return ptr;
418 }
419
420 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
421 size);
422 return NULL;
423}
424
425static void percpu_modfree(void *freeme)
426{
427 unsigned int i;
428 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
429
430 /* First entry is core kernel percpu data. */
431 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
432 if (ptr == freeme) {
433 pcpu_size[i] = -pcpu_size[i];
434 goto free;
435 }
436 }
437 BUG();
438
439 free:
440 /* Merge with previous? */
441 if (pcpu_size[i-1] >= 0) {
442 pcpu_size[i-1] += pcpu_size[i];
443 pcpu_num_used--;
444 memmove(&pcpu_size[i], &pcpu_size[i+1],
445 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
446 i--;
447 }
448 /* Merge with next? */
449 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
450 pcpu_size[i] += pcpu_size[i+1];
451 pcpu_num_used--;
452 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
453 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
454 }
455}
456
457static unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 Elf_Shdr *sechdrs,
459 const char *secstrings)
460{
461 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
462}
463
Mike Travisdd5af902008-01-30 13:33:32 +0100464static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
465{
466 int cpu;
467
468 for_each_possible_cpu(cpu)
469 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int percpu_modinit(void)
473{
474 pcpu_num_used = 2;
475 pcpu_num_allocated = 2;
476 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
477 GFP_KERNEL);
478 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200479 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Free room. */
481 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
482 if (pcpu_size[1] < 0) {
483 printk(KERN_ERR "No per-cpu room for modules.\n");
484 pcpu_num_used = 1;
485 }
486
487 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489__initcall(percpu_modinit);
490#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700491static inline void *percpu_modalloc(unsigned long size, unsigned long align,
492 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 return NULL;
495}
496static inline void percpu_modfree(void *pcpuptr)
497{
498 BUG();
499}
500static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
501 Elf_Shdr *sechdrs,
502 const char *secstrings)
503{
504 return 0;
505}
506static inline void percpu_modcopy(void *pcpudst, const void *src,
507 unsigned long size)
508{
509 /* pcpusec should be 0, and size of that section should be 0. */
510 BUG_ON(size != 0);
511}
512#endif /* CONFIG_SMP */
513
Matt Domschc988d2b2005-06-23 22:05:15 -0700514#define MODINFO_ATTR(field) \
515static void setup_modinfo_##field(struct module *mod, const char *s) \
516{ \
517 mod->field = kstrdup(s, GFP_KERNEL); \
518} \
519static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
520 struct module *mod, char *buffer) \
521{ \
522 return sprintf(buffer, "%s\n", mod->field); \
523} \
524static int modinfo_##field##_exists(struct module *mod) \
525{ \
526 return mod->field != NULL; \
527} \
528static void free_modinfo_##field(struct module *mod) \
529{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700530 kfree(mod->field); \
531 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700532} \
533static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900534 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700535 .show = show_modinfo_##field, \
536 .setup = setup_modinfo_##field, \
537 .test = modinfo_##field##_exists, \
538 .free = free_modinfo_##field, \
539};
540
541MODINFO_ATTR(version);
542MODINFO_ATTR(srcversion);
543
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100544static char last_unloaded_module[MODULE_NAME_LEN+1];
545
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800546#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/* Init the unload section of the module. */
548static void module_unload_init(struct module *mod)
549{
550 unsigned int i;
551
552 INIT_LIST_HEAD(&mod->modules_which_use_me);
553 for (i = 0; i < NR_CPUS; i++)
554 local_set(&mod->ref[i].count, 0);
555 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700556 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Backwards compatibility macros put refcount during init. */
558 mod->waiter = current;
559}
560
561/* modules using other modules */
562struct module_use
563{
564 struct list_head list;
565 struct module *module_which_uses;
566};
567
568/* Does a already use b? */
569static int already_uses(struct module *a, struct module *b)
570{
571 struct module_use *use;
572
573 list_for_each_entry(use, &b->modules_which_use_me, list) {
574 if (use->module_which_uses == a) {
575 DEBUGP("%s uses %s!\n", a->name, b->name);
576 return 1;
577 }
578 }
579 DEBUGP("%s does not use %s!\n", a->name, b->name);
580 return 0;
581}
582
583/* Module a uses b */
584static int use_module(struct module *a, struct module *b)
585{
586 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500587 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (b == NULL || already_uses(a, b)) return 1;
590
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500591 /* If we're interrupted or time out, we fail. */
592 if (wait_event_interruptible_timeout(
593 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
594 30 * HZ) <= 0) {
595 printk("%s: gave up waiting for init of module %s.\n",
596 a->name, b->name);
597 return 0;
598 }
599
600 /* If strong_try_module_get() returned a different error, we fail. */
601 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603
604 DEBUGP("Allocating new usage for %s.\n", a->name);
605 use = kmalloc(sizeof(*use), GFP_ATOMIC);
606 if (!use) {
607 printk("%s: out of memory loading\n", a->name);
608 module_put(b);
609 return 0;
610 }
611
612 use->module_which_uses = a;
613 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100614 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 1;
616}
617
618/* Clear the unload stuff of the module. */
619static void module_unload_free(struct module *mod)
620{
621 struct module *i;
622
623 list_for_each_entry(i, &modules, list) {
624 struct module_use *use;
625
626 list_for_each_entry(use, &i->modules_which_use_me, list) {
627 if (use->module_which_uses == mod) {
628 DEBUGP("%s unusing %s\n", mod->name, i->name);
629 module_put(i);
630 list_del(&use->list);
631 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100632 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* There can be at most one match. */
634 break;
635 }
636 }
637 }
638}
639
640#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800641static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int ret = (flags & O_TRUNC);
644 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800645 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return ret;
647}
648#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800649static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 return 0;
652}
653#endif /* CONFIG_MODULE_FORCE_UNLOAD */
654
655struct stopref
656{
657 struct module *mod;
658 int flags;
659 int *forced;
660};
661
662/* Whole machine is stopped with interrupts off when this runs. */
663static int __try_stop_module(void *_sref)
664{
665 struct stopref *sref = _sref;
666
Rusty Russellda39ba52008-07-22 19:24:25 -0500667 /* If it's not unused, quit unless we're forcing. */
668 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800669 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EWOULDBLOCK;
671 }
672
673 /* Mark it as dying. */
674 sref->mod->state = MODULE_STATE_GOING;
675 return 0;
676}
677
678static int try_stop_module(struct module *mod, int flags, int *forced)
679{
Rusty Russellda39ba52008-07-22 19:24:25 -0500680 if (flags & O_NONBLOCK) {
681 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500683 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500684 } else {
685 /* We don't need to stop the machine for this. */
686 mod->state = MODULE_STATE_GOING;
687 synchronize_sched();
688 return 0;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692unsigned int module_refcount(struct module *mod)
693{
694 unsigned int i, total = 0;
695
696 for (i = 0; i < NR_CPUS; i++)
697 total += local_read(&mod->ref[i].count);
698 return total;
699}
700EXPORT_SYMBOL(module_refcount);
701
702/* This exists whether we can unload or not */
703static void free_module(struct module *mod);
704
705static void wait_for_zero_refcount(struct module *mod)
706{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500707 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800708 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 for (;;) {
710 DEBUGP("Looking at refcount...\n");
711 set_current_state(TASK_UNINTERRUPTIBLE);
712 if (module_refcount(mod) == 0)
713 break;
714 schedule();
715 }
716 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800717 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800720asmlinkage long
721sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800724 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int ret, forced = 0;
726
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800727 if (!capable(CAP_SYS_MODULE))
728 return -EPERM;
729
730 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
731 return -EFAULT;
732 name[MODULE_NAME_LEN-1] = '\0';
733
Ashutosh Naik6389a382006-03-23 03:00:46 -0800734 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EINTR;
736
737 mod = find_module(name);
738 if (!mod) {
739 ret = -ENOENT;
740 goto out;
741 }
742
743 if (!list_empty(&mod->modules_which_use_me)) {
744 /* Other modules depend on us: get rid of them first. */
745 ret = -EWOULDBLOCK;
746 goto out;
747 }
748
749 /* Doing init or already dying? */
750 if (mod->state != MODULE_STATE_LIVE) {
751 /* FIXME: if (force), slam module count and wake up
752 waiter --RR */
753 DEBUGP("%s already dying\n", mod->name);
754 ret = -EBUSY;
755 goto out;
756 }
757
758 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700759 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800760 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!forced) {
762 /* This module can't be removed */
763 ret = -EBUSY;
764 goto out;
765 }
766 }
767
768 /* Set this up before setting mod->state */
769 mod->waiter = current;
770
771 /* Stop the machine so refcounts can't move and disable module. */
772 ret = try_stop_module(mod, flags, &forced);
773 if (ret != 0)
774 goto out;
775
776 /* Never wait if forced. */
777 if (!forced && module_refcount(mod) != 0)
778 wait_for_zero_refcount(mod);
779
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200780 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200782 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200784 blocking_notifier_call_chain(&module_notify_list,
785 MODULE_STATE_GOING, mod);
786 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100787 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500788 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400789 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 free_module(mod);
791
792 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800793 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return ret;
795}
796
797static void print_unload_info(struct seq_file *m, struct module *mod)
798{
799 struct module_use *use;
800 int printed_something = 0;
801
802 seq_printf(m, " %u ", module_refcount(mod));
803
804 /* Always include a trailing , so userspace can differentiate
805 between this and the old multi-field proc format. */
806 list_for_each_entry(use, &mod->modules_which_use_me, list) {
807 printed_something = 1;
808 seq_printf(m, "%s,", use->module_which_uses->name);
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (mod->init != NULL && mod->exit == NULL) {
812 printed_something = 1;
813 seq_printf(m, "[permanent],");
814 }
815
816 if (!printed_something)
817 seq_printf(m, "-");
818}
819
820void __symbol_put(const char *symbol)
821{
822 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Rusty Russell24da1cb2007-07-15 23:41:46 -0700824 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500825 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 BUG();
827 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700828 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830EXPORT_SYMBOL(__symbol_put);
831
832void symbol_put_addr(void *addr)
833{
Trent Piepho5e376612006-05-15 09:44:06 -0700834 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Trent Piepho5e376612006-05-15 09:44:06 -0700836 if (core_kernel_text((unsigned long)addr))
837 return;
838
839 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700841 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843EXPORT_SYMBOL_GPL(symbol_put_addr);
844
845static ssize_t show_refcnt(struct module_attribute *mattr,
846 struct module *mod, char *buffer)
847{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400848 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900852 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 .show = show_refcnt,
854};
855
Al Virof6a57032006-10-18 01:47:25 -0400856void module_put(struct module *module)
857{
858 if (module) {
859 unsigned int cpu = get_cpu();
860 local_dec(&module->ref[cpu].count);
861 /* Maybe they're waiting for us to drop reference? */
862 if (unlikely(!module_is_live(module)))
863 wake_up_process(module->waiter);
864 put_cpu();
865 }
866}
867EXPORT_SYMBOL(module_put);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#else /* !CONFIG_MODULE_UNLOAD */
870static void print_unload_info(struct seq_file *m, struct module *mod)
871{
872 /* We don't know the usage count, or what modules are using. */
873 seq_printf(m, " - -");
874}
875
876static inline void module_unload_free(struct module *mod)
877{
878}
879
880static inline int use_module(struct module *a, struct module *b)
881{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500882 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static inline void module_unload_init(struct module *mod)
886{
887}
888#endif /* CONFIG_MODULE_UNLOAD */
889
Kay Sievers1f717402006-11-24 12:15:25 +0100890static ssize_t show_initstate(struct module_attribute *mattr,
891 struct module *mod, char *buffer)
892{
893 const char *state = "unknown";
894
895 switch (mod->state) {
896 case MODULE_STATE_LIVE:
897 state = "live";
898 break;
899 case MODULE_STATE_COMING:
900 state = "coming";
901 break;
902 case MODULE_STATE_GOING:
903 state = "going";
904 break;
905 }
906 return sprintf(buffer, "%s\n", state);
907}
908
909static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900910 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100911 .show = show_initstate,
912};
913
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800914static struct module_attribute *modinfo_attrs[] = {
915 &modinfo_version,
916 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100917 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800918#ifdef CONFIG_MODULE_UNLOAD
919 &refcnt,
920#endif
921 NULL,
922};
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924static const char vermagic[] = VERMAGIC_STRING;
925
Linus Torvalds826e4502008-05-04 17:04:16 -0700926static int try_to_force_load(struct module *mod, const char *symname)
927{
928#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700929 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700930 printk("%s: no version for \"%s\" found: kernel tainted.\n",
931 mod->name, symname);
932 add_taint_module(mod, TAINT_FORCED_MODULE);
933 return 0;
934#else
935 return -ENOEXEC;
936#endif
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939#ifdef CONFIG_MODVERSIONS
940static int check_version(Elf_Shdr *sechdrs,
941 unsigned int versindex,
942 const char *symname,
943 struct module *mod,
944 const unsigned long *crc)
945{
946 unsigned int i, num_versions;
947 struct modversion_info *versions;
948
949 /* Exporting module didn't supply crcs? OK, we're already tainted. */
950 if (!crc)
951 return 1;
952
Rusty Russella5dd6972008-05-09 16:24:21 +1000953 /* No versions at all? modprobe --force does this. */
954 if (versindex == 0)
955 return try_to_force_load(mod, symname) == 0;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 versions = (void *) sechdrs[versindex].sh_addr;
958 num_versions = sechdrs[versindex].sh_size
959 / sizeof(struct modversion_info);
960
961 for (i = 0; i < num_versions; i++) {
962 if (strcmp(versions[i].name, symname) != 0)
963 continue;
964
965 if (versions[i].crc == *crc)
966 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 DEBUGP("Found checksum %lX vs module %lX\n",
968 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700969 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700971
Rusty Russella5dd6972008-05-09 16:24:21 +1000972 printk(KERN_WARNING "%s: no symbol version for %s\n",
973 mod->name, symname);
974 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700975
976bad_version:
977 printk("%s: disagrees about version of symbol %s\n",
978 mod->name, symname);
979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982static inline int check_modstruct_version(Elf_Shdr *sechdrs,
983 unsigned int versindex,
984 struct module *mod)
985{
986 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Rusty Russellad9546c2008-05-01 21:14:59 -0500988 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -0500990 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Rusty Russell91e37a72008-05-09 16:25:28 +1000993/* First part is kernel version, which we ignore if module has crcs. */
994static inline int same_magic(const char *amagic, const char *bmagic,
995 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Rusty Russell91e37a72008-05-09 16:25:28 +1000997 if (has_crcs) {
998 amagic += strcspn(amagic, " ");
999 bmagic += strcspn(bmagic, " ");
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return strcmp(amagic, bmagic) == 0;
1002}
1003#else
1004static inline int check_version(Elf_Shdr *sechdrs,
1005 unsigned int versindex,
1006 const char *symname,
1007 struct module *mod,
1008 const unsigned long *crc)
1009{
1010 return 1;
1011}
1012
1013static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1014 unsigned int versindex,
1015 struct module *mod)
1016{
1017 return 1;
1018}
1019
Rusty Russell91e37a72008-05-09 16:25:28 +10001020static inline int same_magic(const char *amagic, const char *bmagic,
1021 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 return strcmp(amagic, bmagic) == 0;
1024}
1025#endif /* CONFIG_MODVERSIONS */
1026
1027/* Resolve a symbol for this module. I.e. if we find one, record usage.
1028 Must be holding module_mutex. */
1029static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1030 unsigned int versindex,
1031 const char *name,
1032 struct module *mod)
1033{
1034 struct module *owner;
1035 unsigned long ret;
1036 const unsigned long *crc;
1037
Rusty Russellad9546c2008-05-01 21:14:59 -05001038 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001039 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001040 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001041 /* use_module can fail due to OOM,
1042 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1044 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001045 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return ret;
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*
1051 * /sys/module/foo/sections stuff
1052 * J. Corbet <corbet@lwn.net>
1053 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001054#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001055struct module_sect_attr
1056{
1057 struct module_attribute mattr;
1058 char *name;
1059 unsigned long address;
1060};
1061
1062struct module_sect_attrs
1063{
1064 struct attribute_group grp;
1065 unsigned int nsections;
1066 struct module_sect_attr attrs[0];
1067};
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069static ssize_t module_sect_show(struct module_attribute *mattr,
1070 struct module *mod, char *buf)
1071{
1072 struct module_sect_attr *sattr =
1073 container_of(mattr, struct module_sect_attr, mattr);
1074 return sprintf(buf, "0x%lx\n", sattr->address);
1075}
1076
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001077static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1078{
Rusty Russella58730c2008-03-13 09:03:44 +00001079 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001080
1081 for (section = 0; section < sect_attrs->nsections; section++)
1082 kfree(sect_attrs->attrs[section].name);
1083 kfree(sect_attrs);
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static void add_sect_attrs(struct module *mod, unsigned int nsect,
1087 char *secstrings, Elf_Shdr *sechdrs)
1088{
1089 unsigned int nloaded = 0, i, size[2];
1090 struct module_sect_attrs *sect_attrs;
1091 struct module_sect_attr *sattr;
1092 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* Count loaded sections and allocate structures */
1095 for (i = 0; i < nsect; i++)
1096 if (sechdrs[i].sh_flags & SHF_ALLOC)
1097 nloaded++;
1098 size[0] = ALIGN(sizeof(*sect_attrs)
1099 + nloaded * sizeof(sect_attrs->attrs[0]),
1100 sizeof(sect_attrs->grp.attrs[0]));
1101 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001102 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1103 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return;
1105
1106 /* Setup section attributes. */
1107 sect_attrs->grp.name = "sections";
1108 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1109
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001110 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 sattr = &sect_attrs->attrs[0];
1112 gattr = &sect_attrs->grp.attrs[0];
1113 for (i = 0; i < nsect; i++) {
1114 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1115 continue;
1116 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001117 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1118 GFP_KERNEL);
1119 if (sattr->name == NULL)
1120 goto out;
1121 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 sattr->mattr.show = module_sect_show;
1123 sattr->mattr.store = NULL;
1124 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 sattr->mattr.attr.mode = S_IRUGO;
1126 *(gattr++) = &(sattr++)->mattr.attr;
1127 }
1128 *gattr = NULL;
1129
1130 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1131 goto out;
1132
1133 mod->sect_attrs = sect_attrs;
1134 return;
1135 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001136 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139static void remove_sect_attrs(struct module *mod)
1140{
1141 if (mod->sect_attrs) {
1142 sysfs_remove_group(&mod->mkobj.kobj,
1143 &mod->sect_attrs->grp);
1144 /* We are positive that no one is using any sect attrs
1145 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001146 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 mod->sect_attrs = NULL;
1148 }
1149}
1150
Roland McGrath6d760132007-10-16 23:26:40 -07001151/*
1152 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1153 */
1154
1155struct module_notes_attrs {
1156 struct kobject *dir;
1157 unsigned int notes;
1158 struct bin_attribute attrs[0];
1159};
1160
1161static ssize_t module_notes_read(struct kobject *kobj,
1162 struct bin_attribute *bin_attr,
1163 char *buf, loff_t pos, size_t count)
1164{
1165 /*
1166 * The caller checked the pos and count against our size.
1167 */
1168 memcpy(buf, bin_attr->private + pos, count);
1169 return count;
1170}
1171
1172static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1173 unsigned int i)
1174{
1175 if (notes_attrs->dir) {
1176 while (i-- > 0)
1177 sysfs_remove_bin_file(notes_attrs->dir,
1178 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001179 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001180 }
1181 kfree(notes_attrs);
1182}
1183
1184static void add_notes_attrs(struct module *mod, unsigned int nsect,
1185 char *secstrings, Elf_Shdr *sechdrs)
1186{
1187 unsigned int notes, loaded, i;
1188 struct module_notes_attrs *notes_attrs;
1189 struct bin_attribute *nattr;
1190
1191 /* Count notes sections and allocate structures. */
1192 notes = 0;
1193 for (i = 0; i < nsect; i++)
1194 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1195 (sechdrs[i].sh_type == SHT_NOTE))
1196 ++notes;
1197
1198 if (notes == 0)
1199 return;
1200
1201 notes_attrs = kzalloc(sizeof(*notes_attrs)
1202 + notes * sizeof(notes_attrs->attrs[0]),
1203 GFP_KERNEL);
1204 if (notes_attrs == NULL)
1205 return;
1206
1207 notes_attrs->notes = notes;
1208 nattr = &notes_attrs->attrs[0];
1209 for (loaded = i = 0; i < nsect; ++i) {
1210 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1211 continue;
1212 if (sechdrs[i].sh_type == SHT_NOTE) {
1213 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1214 nattr->attr.mode = S_IRUGO;
1215 nattr->size = sechdrs[i].sh_size;
1216 nattr->private = (void *) sechdrs[i].sh_addr;
1217 nattr->read = module_notes_read;
1218 ++nattr;
1219 }
1220 ++loaded;
1221 }
1222
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001223 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001224 if (!notes_attrs->dir)
1225 goto out;
1226
1227 for (i = 0; i < notes; ++i)
1228 if (sysfs_create_bin_file(notes_attrs->dir,
1229 &notes_attrs->attrs[i]))
1230 goto out;
1231
1232 mod->notes_attrs = notes_attrs;
1233 return;
1234
1235 out:
1236 free_notes_attrs(notes_attrs, i);
1237}
1238
1239static void remove_notes_attrs(struct module *mod)
1240{
1241 if (mod->notes_attrs)
1242 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1248 char *sectstrings, Elf_Shdr *sechdrs)
1249{
1250}
1251
1252static inline void remove_sect_attrs(struct module *mod)
1253{
1254}
Roland McGrath6d760132007-10-16 23:26:40 -07001255
1256static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1257 char *sectstrings, Elf_Shdr *sechdrs)
1258{
1259}
1260
1261static inline void remove_notes_attrs(struct module *mod)
1262{
1263}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Randy Dunlapef665c12007-02-13 15:19:06 -08001266#ifdef CONFIG_SYSFS
1267int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001268{
1269 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001270 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001271 int error = 0;
1272 int i;
1273
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001274 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1275 (ARRAY_SIZE(modinfo_attrs) + 1)),
1276 GFP_KERNEL);
1277 if (!mod->modinfo_attrs)
1278 return -ENOMEM;
1279
1280 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001281 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1282 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001283 (attr->test && attr->test(mod))) {
1284 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001285 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1286 ++temp_attr;
1287 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001288 }
1289 return error;
1290}
1291
Randy Dunlapef665c12007-02-13 15:19:06 -08001292void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001293{
1294 struct module_attribute *attr;
1295 int i;
1296
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001297 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1298 /* pick a field to test for end of list */
1299 if (!attr->attr.name)
1300 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001301 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001302 if (attr->free)
1303 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001304 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001305 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Randy Dunlapef665c12007-02-13 15:19:06 -08001308int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001311 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001313 if (!module_sysfs_initialized) {
1314 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001315 mod->name);
1316 err = -EINVAL;
1317 goto out;
1318 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001319
1320 kobj = kset_find_obj(module_kset, mod->name);
1321 if (kobj) {
1322 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1323 kobject_put(kobj);
1324 err = -EINVAL;
1325 goto out;
1326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001329
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001330 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1331 mod->mkobj.kobj.kset = module_kset;
1332 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1333 "%s", mod->name);
1334 if (err)
1335 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001336
Kay Sievers97c146e2007-11-29 23:46:11 +01001337 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001338out:
1339 return err;
1340}
1341
Randy Dunlapef665c12007-02-13 15:19:06 -08001342int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001343 struct kernel_param *kparam,
1344 unsigned int num_params)
1345{
1346 int err;
1347
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001348 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001349 if (!mod->holders_dir) {
1350 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001351 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001352 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 err = module_param_sysfs_setup(mod, kparam, num_params);
1355 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001356 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Matt Domschc988d2b2005-06-23 22:05:15 -07001358 err = module_add_modinfo_attrs(mod);
1359 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001360 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001361
Kay Sieverse17e0f52006-11-24 12:15:25 +01001362 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364
Kay Sieverse17e0f52006-11-24 12:15:25 +01001365out_unreg_param:
1366 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001367out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001368 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001369out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001370 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return err;
1372}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001373
1374static void mod_sysfs_fini(struct module *mod)
1375{
1376 kobject_put(&mod->mkobj.kobj);
1377}
1378
1379#else /* CONFIG_SYSFS */
1380
1381static void mod_sysfs_fini(struct module *mod)
1382{
1383}
1384
1385#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387static void mod_kobject_remove(struct module *mod)
1388{
Matt Domschc988d2b2005-06-23 22:05:15 -07001389 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001391 kobject_put(mod->mkobj.drivers_dir);
1392 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001393 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
1396/*
Rusty Russellbb9d3d52008-01-29 17:13:21 -05001397 * link the module with the whole machine is stopped with interrupts off
1398 * - this defends against kallsyms not taking locks
1399 */
1400static int __link_module(void *_mod)
1401{
1402 struct module *mod = _mod;
1403 list_add(&mod->list, &modules);
1404 return 0;
1405}
1406
1407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * unlink the module with the whole machine is stopped with interrupts off
1409 * - this defends against kallsyms not taking locks
1410 */
1411static int __unlink_module(void *_mod)
1412{
1413 struct module *mod = _mod;
1414 list_del(&mod->list);
1415 return 0;
1416}
1417
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001418/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419static void free_module(struct module *mod)
1420{
1421 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001422 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001423 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 remove_sect_attrs(mod);
1425 mod_kobject_remove(mod);
1426
Jan Beulich4552d5d2006-06-26 13:57:28 +02001427 unwind_remove_table(mod->unwind_info, 0);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Arch-specific cleanup. */
1430 module_arch_cleanup(mod);
1431
1432 /* Module unload stuff */
1433 module_unload_free(mod);
1434
Steven Rostedtfed19392008-08-14 22:47:19 -04001435 /* release any pointers to mcount in this module */
1436 ftrace_release(mod->module_core, mod->core_size);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /* This may be NULL, but that's OK */
1439 module_free(mod, mod->module_init);
1440 kfree(mod->args);
1441 if (mod->percpu)
1442 percpu_modfree(mod->percpu);
1443
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001444 /* Free lock-classes: */
1445 lockdep_free_key_range(mod->module_core, mod->core_size);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Finally, free the core (containing the module structure) */
1448 module_free(mod, mod->module_core);
1449}
1450
1451void *__symbol_get(const char *symbol)
1452{
1453 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001454 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Rusty Russell24da1cb2007-07-15 23:41:46 -07001456 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001457 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001458 if (IS_ERR_VALUE(value))
1459 value = 0;
1460 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001462 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 return (void *)value;
1465}
1466EXPORT_SYMBOL_GPL(__symbol_get);
1467
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001468/*
1469 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001470 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001471 */
1472static int verify_export_symbols(struct module *mod)
1473{
Rusty Russellb2111042008-05-01 21:15:00 -05001474 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001475 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001476 const struct kernel_symbol *s;
1477 struct {
1478 const struct kernel_symbol *sym;
1479 unsigned int num;
1480 } arr[] = {
1481 { mod->syms, mod->num_syms },
1482 { mod->gpl_syms, mod->num_gpl_syms },
1483 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001484#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001485 { mod->unused_syms, mod->num_unused_syms },
1486 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001487#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001488 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001489
Rusty Russellb2111042008-05-01 21:15:00 -05001490 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1491 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1492 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1493 NULL, true, false))) {
1494 printk(KERN_ERR
1495 "%s: exports duplicate symbol %s"
1496 " (owned by %s)\n",
1497 mod->name, s->name, module_name(owner));
1498 return -ENOEXEC;
1499 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001500 }
Rusty Russellb2111042008-05-01 21:15:00 -05001501 }
1502 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001503}
1504
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001505/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506static int simplify_symbols(Elf_Shdr *sechdrs,
1507 unsigned int symindex,
1508 const char *strtab,
1509 unsigned int versindex,
1510 unsigned int pcpuindex,
1511 struct module *mod)
1512{
1513 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1514 unsigned long secbase;
1515 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1516 int ret = 0;
1517
1518 for (i = 1; i < n; i++) {
1519 switch (sym[i].st_shndx) {
1520 case SHN_COMMON:
1521 /* We compiled with -fno-common. These are not
1522 supposed to happen. */
1523 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1524 printk("%s: please compile with -fno-common\n",
1525 mod->name);
1526 ret = -ENOEXEC;
1527 break;
1528
1529 case SHN_ABS:
1530 /* Don't need to do anything */
1531 DEBUGP("Absolute symbol: 0x%08lx\n",
1532 (long)sym[i].st_value);
1533 break;
1534
1535 case SHN_UNDEF:
1536 sym[i].st_value
1537 = resolve_symbol(sechdrs, versindex,
1538 strtab + sym[i].st_name, mod);
1539
1540 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001541 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 /* Ok if weak. */
1544 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1545 break;
1546
1547 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1548 mod->name, strtab + sym[i].st_name);
1549 ret = -ENOENT;
1550 break;
1551
1552 default:
1553 /* Divert to percpu allocation if a percpu var. */
1554 if (sym[i].st_shndx == pcpuindex)
1555 secbase = (unsigned long)mod->percpu;
1556 else
1557 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1558 sym[i].st_value += secbase;
1559 break;
1560 }
1561 }
1562
1563 return ret;
1564}
1565
1566/* Update size with this section: return offset. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05001567static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 long ret;
1570
1571 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1572 *size = ret + sechdr->sh_size;
1573 return ret;
1574}
1575
1576/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1577 might -- code, read-only data, read-write data, small data. Tally
1578 sizes, and place the offsets into sh_entsize fields: high bit means it
1579 belongs in init. */
1580static void layout_sections(struct module *mod,
1581 const Elf_Ehdr *hdr,
1582 Elf_Shdr *sechdrs,
1583 const char *secstrings)
1584{
1585 static unsigned long const masks[][2] = {
1586 /* NOTE: all executable code must be the first section
1587 * in this array; otherwise modify the text_size
1588 * finder in the two loops below */
1589 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1590 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1591 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1592 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1593 };
1594 unsigned int m, i;
1595
1596 for (i = 0; i < hdr->e_shnum; i++)
1597 sechdrs[i].sh_entsize = ~0UL;
1598
1599 DEBUGP("Core section allocation order:\n");
1600 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1601 for (i = 0; i < hdr->e_shnum; ++i) {
1602 Elf_Shdr *s = &sechdrs[i];
1603
1604 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1605 || (s->sh_flags & masks[m][1])
1606 || s->sh_entsize != ~0UL
1607 || strncmp(secstrings + s->sh_name,
1608 ".init", 5) == 0)
1609 continue;
1610 s->sh_entsize = get_offset(&mod->core_size, s);
1611 DEBUGP("\t%s\n", secstrings + s->sh_name);
1612 }
1613 if (m == 0)
1614 mod->core_text_size = mod->core_size;
1615 }
1616
1617 DEBUGP("Init section allocation order:\n");
1618 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1619 for (i = 0; i < hdr->e_shnum; ++i) {
1620 Elf_Shdr *s = &sechdrs[i];
1621
1622 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1623 || (s->sh_flags & masks[m][1])
1624 || s->sh_entsize != ~0UL
1625 || strncmp(secstrings + s->sh_name,
1626 ".init", 5) != 0)
1627 continue;
1628 s->sh_entsize = (get_offset(&mod->init_size, s)
1629 | INIT_OFFSET_MASK);
1630 DEBUGP("\t%s\n", secstrings + s->sh_name);
1631 }
1632 if (m == 0)
1633 mod->init_text_size = mod->init_size;
1634 }
1635}
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637static void set_license(struct module *mod, const char *license)
1638{
1639 if (!license)
1640 license = "unspecified";
1641
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001642 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001643 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001644 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001645 "kernel.\n", mod->name, license);
1646 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648}
1649
1650/* Parse tag=value strings from .modinfo section */
1651static char *next_string(char *string, unsigned long *secsize)
1652{
1653 /* Skip non-zero chars */
1654 while (string[0]) {
1655 string++;
1656 if ((*secsize)-- <= 1)
1657 return NULL;
1658 }
1659
1660 /* Skip any zero padding. */
1661 while (!string[0]) {
1662 string++;
1663 if ((*secsize)-- <= 1)
1664 return NULL;
1665 }
1666 return string;
1667}
1668
1669static char *get_modinfo(Elf_Shdr *sechdrs,
1670 unsigned int info,
1671 const char *tag)
1672{
1673 char *p;
1674 unsigned int taglen = strlen(tag);
1675 unsigned long size = sechdrs[info].sh_size;
1676
1677 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1678 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1679 return p + taglen + 1;
1680 }
1681 return NULL;
1682}
1683
Matt Domschc988d2b2005-06-23 22:05:15 -07001684static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1685 unsigned int infoindex)
1686{
1687 struct module_attribute *attr;
1688 int i;
1689
1690 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1691 if (attr->setup)
1692 attr->setup(mod,
1693 get_modinfo(sechdrs,
1694 infoindex,
1695 attr->attr.name));
1696 }
1697}
Matt Domschc988d2b2005-06-23 22:05:15 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001700
1701/* lookup symbol in given range of kernel_symbols */
1702static const struct kernel_symbol *lookup_symbol(const char *name,
1703 const struct kernel_symbol *start,
1704 const struct kernel_symbol *stop)
1705{
1706 const struct kernel_symbol *ks = start;
1707 for (; ks < stop; ks++)
1708 if (strcmp(ks->name, name) == 0)
1709 return ks;
1710 return NULL;
1711}
1712
Alexey Dobriyanea078902007-05-08 00:28:39 -07001713static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001715 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1716 return 1;
1717 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001718 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001720 else
1721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724/* As per nm */
1725static char elf_type(const Elf_Sym *sym,
1726 Elf_Shdr *sechdrs,
1727 const char *secstrings,
1728 struct module *mod)
1729{
1730 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1731 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1732 return 'v';
1733 else
1734 return 'w';
1735 }
1736 if (sym->st_shndx == SHN_UNDEF)
1737 return 'U';
1738 if (sym->st_shndx == SHN_ABS)
1739 return 'a';
1740 if (sym->st_shndx >= SHN_LORESERVE)
1741 return '?';
1742 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1743 return 't';
1744 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1745 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1746 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1747 return 'r';
1748 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1749 return 'g';
1750 else
1751 return 'd';
1752 }
1753 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1754 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1755 return 's';
1756 else
1757 return 'b';
1758 }
1759 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1760 ".debug", strlen(".debug")) == 0)
1761 return 'n';
1762 return '?';
1763}
1764
1765static void add_kallsyms(struct module *mod,
1766 Elf_Shdr *sechdrs,
1767 unsigned int symindex,
1768 unsigned int strindex,
1769 const char *secstrings)
1770{
1771 unsigned int i;
1772
1773 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1774 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1775 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1776
1777 /* Set types up while we still have access to sections. */
1778 for (i = 0; i < mod->num_symtab; i++)
1779 mod->symtab[i].st_info
1780 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1781}
1782#else
1783static inline void add_kallsyms(struct module *mod,
1784 Elf_Shdr *sechdrs,
1785 unsigned int symindex,
1786 unsigned int strindex,
1787 const char *secstrings)
1788{
1789}
1790#endif /* CONFIG_KALLSYMS */
1791
Jason Baron346e15b2008-08-12 16:46:19 -04001792#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1793static void dynamic_printk_setup(Elf_Shdr *sechdrs, unsigned int verboseindex)
1794{
1795 struct mod_debug *debug_info;
1796 unsigned long pos, end;
1797 unsigned int num_verbose;
1798
1799 pos = sechdrs[verboseindex].sh_addr;
1800 num_verbose = sechdrs[verboseindex].sh_size /
1801 sizeof(struct mod_debug);
1802 end = pos + (num_verbose * sizeof(struct mod_debug));
1803
1804 for (; pos < end; pos += sizeof(struct mod_debug)) {
1805 debug_info = (struct mod_debug *)pos;
1806 register_dynamic_debug_module(debug_info->modname,
1807 debug_info->type, debug_info->logical_modname,
1808 debug_info->flag_names, debug_info->hash,
1809 debug_info->hash2);
1810 }
1811}
1812#else
1813static inline void dynamic_printk_setup(Elf_Shdr *sechdrs,
1814 unsigned int verboseindex)
1815{
1816}
1817#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1818
Rusty Russell3a642e92008-07-22 19:24:28 -05001819static void *module_alloc_update_bounds(unsigned long size)
1820{
1821 void *ret = module_alloc(size);
1822
1823 if (ret) {
1824 /* Update module bounds. */
1825 if ((unsigned long)ret < module_addr_min)
1826 module_addr_min = (unsigned long)ret;
1827 if ((unsigned long)ret + size > module_addr_max)
1828 module_addr_max = (unsigned long)ret + size;
1829 }
1830 return ret;
1831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833/* Allocate and load the module: note that size of section 0 is always
1834 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001835static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 unsigned long len,
1837 const char __user *uargs)
1838{
1839 Elf_Ehdr *hdr;
1840 Elf_Shdr *sechdrs;
1841 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001842 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001843 unsigned int i;
1844 unsigned int symindex = 0;
1845 unsigned int strindex = 0;
1846 unsigned int setupindex;
1847 unsigned int exindex;
1848 unsigned int exportindex;
1849 unsigned int modindex;
1850 unsigned int obsparmindex;
1851 unsigned int infoindex;
1852 unsigned int gplindex;
1853 unsigned int crcindex;
1854 unsigned int gplcrcindex;
1855 unsigned int versindex;
1856 unsigned int pcpuindex;
1857 unsigned int gplfutureindex;
1858 unsigned int gplfuturecrcindex;
1859 unsigned int unwindex = 0;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001860#ifdef CONFIG_UNUSED_SYMBOLS
Andrew Morton84860f92006-06-28 04:26:46 -07001861 unsigned int unusedindex;
1862 unsigned int unusedcrcindex;
1863 unsigned int unusedgplindex;
1864 unsigned int unusedgplcrcindex;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001865#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07001866 unsigned int markersindex;
1867 unsigned int markersstringsindex;
Jason Baron346e15b2008-08-12 16:46:19 -04001868 unsigned int verboseindex;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04001869 unsigned int tracepointsindex;
1870 unsigned int tracepointsstringsindex;
Steven Rostedt90d595f2008-08-14 15:45:09 -04001871 unsigned int mcountindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct module *mod;
1873 long err = 0;
1874 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Steven Rostedtfed19392008-08-14 22:47:19 -04001875 void *mseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct exception_table_entry *extable;
Thomas Koeller378bac82005-09-06 15:17:11 -07001877 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1880 umod, len, uargs);
1881 if (len < sizeof(*hdr))
1882 return ERR_PTR(-ENOEXEC);
1883
1884 /* Suck in entire file: we'll want most of it. */
1885 /* vmalloc barfs on "unusual" numbers. Check here */
1886 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1887 return ERR_PTR(-ENOMEM);
1888 if (copy_from_user(hdr, umod, len) != 0) {
1889 err = -EFAULT;
1890 goto free_hdr;
1891 }
1892
1893 /* Sanity checks against insmoding binaries or wrong arch,
1894 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001895 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 || hdr->e_type != ET_REL
1897 || !elf_check_arch(hdr)
1898 || hdr->e_shentsize != sizeof(*sechdrs)) {
1899 err = -ENOEXEC;
1900 goto free_hdr;
1901 }
1902
1903 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1904 goto truncated;
1905
1906 /* Convenience variables */
1907 sechdrs = (void *)hdr + hdr->e_shoff;
1908 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1909 sechdrs[0].sh_addr = 0;
1910
1911 for (i = 1; i < hdr->e_shnum; i++) {
1912 if (sechdrs[i].sh_type != SHT_NOBITS
1913 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1914 goto truncated;
1915
1916 /* Mark all sections sh_addr with their address in the
1917 temporary image. */
1918 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1919
1920 /* Internal symbols and strings. */
1921 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1922 symindex = i;
1923 strindex = sechdrs[i].sh_link;
1924 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1925 }
1926#ifndef CONFIG_MODULE_UNLOAD
1927 /* Don't load .exit sections */
1928 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1929 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1930#endif
1931 }
1932
1933 modindex = find_sec(hdr, sechdrs, secstrings,
1934 ".gnu.linkonce.this_module");
1935 if (!modindex) {
1936 printk(KERN_WARNING "No module found in object\n");
1937 err = -ENOEXEC;
1938 goto free_hdr;
1939 }
1940 mod = (void *)sechdrs[modindex].sh_addr;
1941
1942 if (symindex == 0) {
1943 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1944 mod->name);
1945 err = -ENOEXEC;
1946 goto free_hdr;
1947 }
1948
1949 /* Optional sections */
1950 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1951 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001952 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1954 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08001955 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001956#ifdef CONFIG_UNUSED_SYMBOLS
1957 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1958 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001959 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1960 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1963 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1964 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1965 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1966 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1967 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001968#ifdef ARCH_UNWIND_SECTION_NAME
1969 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1970#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Rusty Russellea01e792008-03-13 09:02:17 +00001972 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001974 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975#ifdef CONFIG_KALLSYMS
1976 /* Keep symbol and string tables for decoding later. */
1977 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1978 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1979#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001980 if (unwindex)
1981 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 /* Check module struct version now, before we try to use module. */
1984 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1985 err = -ENOEXEC;
1986 goto free_hdr;
1987 }
1988
1989 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1990 /* This is allowed: modprobe --force will invalidate it. */
1991 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001992 err = try_to_force_load(mod, "magic");
1993 if (err)
1994 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001995 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1997 mod->name, modmagic, vermagic);
1998 err = -ENOEXEC;
1999 goto free_hdr;
2000 }
2001
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002002 staging = get_modinfo(sechdrs, infoindex, "staging");
2003 if (staging) {
2004 add_taint_module(mod, TAINT_CRAP);
2005 printk(KERN_WARNING "%s: module is from the staging directory,"
2006 " the quality is unknown, you have been warned.\n",
2007 mod->name);
2008 }
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002011 args = strndup_user(uargs, ~0UL >> 1);
2012 if (IS_ERR(args)) {
2013 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 goto free_hdr;
2015 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (find_module(mod->name)) {
2018 err = -EEXIST;
2019 goto free_mod;
2020 }
2021
2022 mod->state = MODULE_STATE_COMING;
2023
2024 /* Allow arches to frob section contents and sizes. */
2025 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2026 if (err < 0)
2027 goto free_mod;
2028
2029 if (pcpuindex) {
2030 /* We have a special allocation for this section. */
2031 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002032 sechdrs[pcpuindex].sh_addralign,
2033 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!percpu) {
2035 err = -ENOMEM;
2036 goto free_mod;
2037 }
2038 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2039 mod->percpu = percpu;
2040 }
2041
2042 /* Determine total sizes, and put offsets in sh_entsize. For now
2043 this is done generically; there doesn't appear to be any
2044 special cases for the architectures. */
2045 layout_sections(mod, hdr, sechdrs, secstrings);
2046
2047 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002048 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (!ptr) {
2050 err = -ENOMEM;
2051 goto free_percpu;
2052 }
2053 memset(ptr, 0, mod->core_size);
2054 mod->module_core = ptr;
2055
Rusty Russell3a642e92008-07-22 19:24:28 -05002056 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (!ptr && mod->init_size) {
2058 err = -ENOMEM;
2059 goto free_core;
2060 }
2061 memset(ptr, 0, mod->init_size);
2062 mod->module_init = ptr;
2063
2064 /* Transfer each section which specifies SHF_ALLOC */
2065 DEBUGP("final section addresses:\n");
2066 for (i = 0; i < hdr->e_shnum; i++) {
2067 void *dest;
2068
2069 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2070 continue;
2071
2072 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2073 dest = mod->module_init
2074 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2075 else
2076 dest = mod->module_core + sechdrs[i].sh_entsize;
2077
2078 if (sechdrs[i].sh_type != SHT_NOBITS)
2079 memcpy(dest, (void *)sechdrs[i].sh_addr,
2080 sechdrs[i].sh_size);
2081 /* Update sh_addr to point to copy in image. */
2082 sechdrs[i].sh_addr = (unsigned long)dest;
2083 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2084 }
2085 /* Module has been moved. */
2086 mod = (void *)sechdrs[modindex].sh_addr;
2087
2088 /* Now we've moved module, initialize linked lists, etc. */
2089 module_unload_init(mod);
2090
Kay Sievers97c146e2007-11-29 23:46:11 +01002091 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002092 err = mod_sysfs_init(mod);
2093 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002094 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* Set up license info based on the info section */
2097 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2098
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002099 /*
2100 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2101 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2102 * using GPL-only symbols it needs.
2103 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002104 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002105 add_taint(TAINT_PROPRIETARY_MODULE);
2106
2107 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002108 if (strcmp(mod->name, "driverloader") == 0)
2109 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002110
Matt Domschc988d2b2005-06-23 22:05:15 -07002111 /* Set up MODINFO_ATTR fields */
2112 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* Fix up syms, so that st_value is a pointer to location. */
2115 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2116 mod);
2117 if (err < 0)
2118 goto cleanup;
2119
2120 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2121 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2122 mod->syms = (void *)sechdrs[exportindex].sh_addr;
2123 if (crcindex)
2124 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2125 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2126 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2127 if (gplcrcindex)
2128 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -08002129 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2130 sizeof(*mod->gpl_future_syms);
2131 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2132 if (gplfuturecrcindex)
2133 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002135#ifdef CONFIG_UNUSED_SYMBOLS
2136 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2137 sizeof(*mod->unused_syms);
2138 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2139 sizeof(*mod->unused_gpl_syms);
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002140 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2141 if (unusedcrcindex)
2142 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2143 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2144 if (unusedgplcrcindex)
Rusty Russell4e2d9242008-05-01 21:15:00 -05002145 mod->unused_gpl_crcs
2146 = (void *)sechdrs[unusedgplcrcindex].sh_addr;
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002147#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149#ifdef CONFIG_MODVERSIONS
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002150 if ((mod->num_syms && !crcindex)
2151 || (mod->num_gpl_syms && !gplcrcindex)
2152 || (mod->num_gpl_future_syms && !gplfuturecrcindex)
2153#ifdef CONFIG_UNUSED_SYMBOLS
2154 || (mod->num_unused_syms && !unusedcrcindex)
2155 || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
2156#endif
2157 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002158 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2159 err = try_to_force_load(mod, "nocrc");
2160 if (err)
2161 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002164 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2165 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2166 "__markers_strings");
Jason Baron346e15b2008-08-12 16:46:19 -04002167 verboseindex = find_sec(hdr, sechdrs, secstrings, "__verbose");
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002168 tracepointsindex = find_sec(hdr, sechdrs, secstrings, "__tracepoints");
2169 tracepointsstringsindex = find_sec(hdr, sechdrs, secstrings,
2170 "__tracepoints_strings");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Steven Rostedt90d595f2008-08-14 15:45:09 -04002172 mcountindex = find_sec(hdr, sechdrs, secstrings,
2173 "__mcount_loc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /* Now do relocations. */
2176 for (i = 1; i < hdr->e_shnum; i++) {
2177 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2178 unsigned int info = sechdrs[i].sh_info;
2179
2180 /* Not a valid relocation section? */
2181 if (info >= hdr->e_shnum)
2182 continue;
2183
2184 /* Don't bother with non-allocated sections */
2185 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2186 continue;
2187
2188 if (sechdrs[i].sh_type == SHT_REL)
2189 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2190 else if (sechdrs[i].sh_type == SHT_RELA)
2191 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2192 mod);
2193 if (err < 0)
2194 goto cleanup;
2195 }
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002196#ifdef CONFIG_MARKERS
2197 mod->markers = (void *)sechdrs[markersindex].sh_addr;
2198 mod->num_markers =
2199 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2200#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002201#ifdef CONFIG_TRACEPOINTS
2202 mod->tracepoints = (void *)sechdrs[tracepointsindex].sh_addr;
2203 mod->num_tracepoints =
2204 sechdrs[tracepointsindex].sh_size / sizeof(*mod->tracepoints);
2205#endif
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002208 /* Find duplicate symbols */
2209 err = verify_export_symbols(mod);
2210
2211 if (err < 0)
2212 goto cleanup;
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* Set up and sort exception table */
2215 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2216 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2217 sort_extable(extable, extable + mod->num_exentries);
2218
2219 /* Finally, copy percpu area over. */
2220 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2221 sechdrs[pcpuindex].sh_size);
2222
2223 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2224
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002225 if (!mod->taints) {
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002226#ifdef CONFIG_MARKERS
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002227 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002228 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002229#endif
Jason Baron346e15b2008-08-12 16:46:19 -04002230 dynamic_printk_setup(sechdrs, verboseindex);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002231#ifdef CONFIG_TRACEPOINTS
2232 tracepoint_update_probe_range(mod->tracepoints,
2233 mod->tracepoints + mod->num_tracepoints);
2234#endif
2235 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002236
Steven Rostedtfed19392008-08-14 22:47:19 -04002237 /* sechdrs[0].sh_size is always zero */
2238 mseg = (void *)sechdrs[mcountindex].sh_addr;
2239 ftrace_init_module(mseg, mseg + sechdrs[mcountindex].sh_size);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 err = module_finalize(hdr, sechdrs, mod);
2242 if (err < 0)
2243 goto cleanup;
2244
Thomas Koeller378bac82005-09-06 15:17:11 -07002245 /* flush the icache in correct context */
2246 old_fs = get_fs();
2247 set_fs(KERNEL_DS);
2248
2249 /*
2250 * Flush the instruction cache, since we've played with text.
2251 * Do it before processing of module parameters, so the module
2252 * can provide parameter accessor functions of its own.
2253 */
2254 if (mod->module_init)
2255 flush_icache_range((unsigned long)mod->module_init,
2256 (unsigned long)mod->module_init
2257 + mod->init_size);
2258 flush_icache_range((unsigned long)mod->module_core,
2259 (unsigned long)mod->module_core + mod->core_size);
2260
2261 set_fs(old_fs);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 mod->args = args;
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002264 if (obsparmindex)
2265 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2266 mod->name);
2267
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002268 /* Now sew it into the lists so we can get lockdep and oops
2269 * info during argument parsing. Noone should access us, since
2270 * strong_try_module_get() will fail. */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002271 stop_machine(__link_module, mod, NULL);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002272
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002273 /* Size of section 0 is 0, so this works well if no params */
2274 err = parse_args(mod->name, mod->args,
2275 (struct kernel_param *)
2276 sechdrs[setupindex].sh_addr,
2277 sechdrs[setupindex].sh_size
2278 / sizeof(struct kernel_param),
2279 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002281 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Daniel Walker22a8bde2007-10-18 03:06:07 -07002283 err = mod_sysfs_setup(mod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 (struct kernel_param *)
2285 sechdrs[setupindex].sh_addr,
2286 sechdrs[setupindex].sh_size
2287 / sizeof(struct kernel_param));
2288 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002289 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002291 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Jan Beulich4552d5d2006-06-26 13:57:28 +02002293 /* Size of section 0 is 0, so this works well if no unwind info. */
2294 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002295 (void *)sechdrs[unwindex].sh_addr,
2296 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /* Get rid of temporary copy */
2299 vfree(hdr);
2300
2301 /* Done! */
2302 return mod;
2303
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002304 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002305 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 module_arch_cleanup(mod);
2307 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002308 kobject_del(&mod->mkobj.kobj);
2309 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002310 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002311 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 module_unload_free(mod);
2313 module_free(mod, mod->module_init);
2314 free_core:
2315 module_free(mod, mod->module_core);
2316 free_percpu:
2317 if (percpu)
2318 percpu_modfree(percpu);
2319 free_mod:
2320 kfree(args);
2321 free_hdr:
2322 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002323 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 truncated:
2326 printk(KERN_ERR "Module len %lu truncated\n", len);
2327 err = -ENOEXEC;
2328 goto free_hdr;
2329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331/* This is where the real work happens */
2332asmlinkage long
2333sys_init_module(void __user *umod,
2334 unsigned long len,
2335 const char __user *uargs)
2336{
2337 struct module *mod;
2338 int ret = 0;
2339
2340 /* Must have permission */
2341 if (!capable(CAP_SYS_MODULE))
2342 return -EPERM;
2343
2344 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002345 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -EINTR;
2347
2348 /* Do all the hard work */
2349 mod = load_module(umod, len, uargs);
2350 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002351 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return PTR_ERR(mod);
2353 }
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002356 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Alan Sterne041c682006-03-27 01:16:30 -08002358 blocking_notifier_call_chain(&module_notify_list,
2359 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 /* Start the module */
2362 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002363 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (ret < 0) {
2365 /* Init routine failed: abort. Try to protect us from
2366 buggy refcounters. */
2367 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002368 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002369 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002370 blocking_notifier_call_chain(&module_notify_list,
2371 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002372 mutex_lock(&module_mutex);
2373 free_module(mod);
2374 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002375 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return ret;
2377 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002378 if (ret > 0) {
2379 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2380 "it should follow 0/-E convention\n"
2381 KERN_WARNING "%s: loading module anyway...\n",
2382 __func__, mod->name, ret,
2383 __func__);
2384 dump_stack();
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Rusty Russell6c5db222008-03-10 11:43:52 -07002387 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002389 wake_up(&module_wq);
2390
2391 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /* Drop initial reference. */
2393 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002394 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 module_free(mod, mod->module_init);
2396 mod->module_init = NULL;
2397 mod->init_size = 0;
2398 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002399 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 return 0;
2402}
2403
2404static inline int within(unsigned long addr, void *start, unsigned long size)
2405{
2406 return ((void *)addr >= start && (void *)addr < start + size);
2407}
2408
2409#ifdef CONFIG_KALLSYMS
2410/*
2411 * This ignores the intensely annoying "mapping symbols" found
2412 * in ARM ELF files: $a, $t and $d.
2413 */
2414static inline int is_arm_mapping_symbol(const char *str)
2415{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002416 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 && (str[2] == '\0' || str[2] == '.');
2418}
2419
2420static const char *get_ksymbol(struct module *mod,
2421 unsigned long addr,
2422 unsigned long *size,
2423 unsigned long *offset)
2424{
2425 unsigned int i, best = 0;
2426 unsigned long nextval;
2427
2428 /* At worse, next value is at end of module */
2429 if (within(addr, mod->module_init, mod->init_size))
2430 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002431 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2433
2434 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002435 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 for (i = 1; i < mod->num_symtab; i++) {
2437 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2438 continue;
2439
2440 /* We ignore unnamed symbols: they're uninformative
2441 * and inserted at a whim. */
2442 if (mod->symtab[i].st_value <= addr
2443 && mod->symtab[i].st_value > mod->symtab[best].st_value
2444 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2445 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2446 best = i;
2447 if (mod->symtab[i].st_value > addr
2448 && mod->symtab[i].st_value < nextval
2449 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2450 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2451 nextval = mod->symtab[i].st_value;
2452 }
2453
2454 if (!best)
2455 return NULL;
2456
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002457 if (size)
2458 *size = nextval - mod->symtab[best].st_value;
2459 if (offset)
2460 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return mod->strtab + mod->symtab[best].st_name;
2462}
2463
Rusty Russell6dd06c92008-01-29 17:13:22 -05002464/* For kallsyms to ask for address resolution. NULL means not found. Careful
2465 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002466const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002467 unsigned long *size,
2468 unsigned long *offset,
2469 char **modname,
2470 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
2472 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002473 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Rusty Russellcb2a5202008-01-14 00:55:03 -08002475 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 list_for_each_entry(mod, &modules, list) {
2477 if (within(addr, mod->module_init, mod->init_size)
2478 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002479 if (modname)
2480 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002481 ret = get_ksymbol(mod, addr, size, offset);
2482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002485 /* Make a copy in here where it's safe */
2486 if (ret) {
2487 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2488 ret = namebuf;
2489 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002490 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002491 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002494int lookup_module_symbol_name(unsigned long addr, char *symname)
2495{
2496 struct module *mod;
2497
Rusty Russellcb2a5202008-01-14 00:55:03 -08002498 preempt_disable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002499 list_for_each_entry(mod, &modules, list) {
2500 if (within(addr, mod->module_init, mod->init_size) ||
2501 within(addr, mod->module_core, mod->core_size)) {
2502 const char *sym;
2503
2504 sym = get_ksymbol(mod, addr, NULL, NULL);
2505 if (!sym)
2506 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002507 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002508 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002509 return 0;
2510 }
2511 }
2512out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002513 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002514 return -ERANGE;
2515}
2516
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002517int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2518 unsigned long *offset, char *modname, char *name)
2519{
2520 struct module *mod;
2521
Rusty Russellcb2a5202008-01-14 00:55:03 -08002522 preempt_disable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002523 list_for_each_entry(mod, &modules, list) {
2524 if (within(addr, mod->module_init, mod->init_size) ||
2525 within(addr, mod->module_core, mod->core_size)) {
2526 const char *sym;
2527
2528 sym = get_ksymbol(mod, addr, size, offset);
2529 if (!sym)
2530 goto out;
2531 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002532 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002533 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002534 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002535 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002536 return 0;
2537 }
2538 }
2539out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002540 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002541 return -ERANGE;
2542}
2543
Alexey Dobriyanea078902007-05-08 00:28:39 -07002544int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2545 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 struct module *mod;
2548
Rusty Russellcb2a5202008-01-14 00:55:03 -08002549 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 list_for_each_entry(mod, &modules, list) {
2551 if (symnum < mod->num_symtab) {
2552 *value = mod->symtab[symnum].st_value;
2553 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002554 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002555 KSYM_NAME_LEN);
2556 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002557 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002558 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 symnum -= mod->num_symtab;
2562 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002563 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002564 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
2567static unsigned long mod_find_symname(struct module *mod, const char *name)
2568{
2569 unsigned int i;
2570
2571 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002572 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2573 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return mod->symtab[i].st_value;
2575 return 0;
2576}
2577
2578/* Look for this name: can be of form module:name. */
2579unsigned long module_kallsyms_lookup_name(const char *name)
2580{
2581 struct module *mod;
2582 char *colon;
2583 unsigned long ret = 0;
2584
2585 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002586 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if ((colon = strchr(name, ':')) != NULL) {
2588 *colon = '\0';
2589 if ((mod = find_module(name)) != NULL)
2590 ret = mod_find_symname(mod, colon+1);
2591 *colon = ':';
2592 } else {
2593 list_for_each_entry(mod, &modules, list)
2594 if ((ret = mod_find_symname(mod, name)) != 0)
2595 break;
2596 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002597 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return ret;
2599}
2600#endif /* CONFIG_KALLSYMS */
2601
2602/* Called by the /proc file system to return a list of modules. */
2603static void *m_start(struct seq_file *m, loff_t *pos)
2604{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002605 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002606 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
2609static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2610{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002611 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
2614static void m_stop(struct seq_file *m, void *p)
2615{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002616 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002619static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002620{
2621 int bx = 0;
2622
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002623 if (mod->taints ||
2624 mod->state == MODULE_STATE_GOING ||
2625 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002626 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002627 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002628 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002629 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002630 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002631 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002632 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002633 /*
2634 * TAINT_FORCED_RMMOD: could be added.
2635 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2636 * apply to modules.
2637 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002638
2639 /* Show a - for module-is-being-unloaded */
2640 if (mod->state == MODULE_STATE_GOING)
2641 buf[bx++] = '-';
2642 /* Show a + for module-is-being-loaded */
2643 if (mod->state == MODULE_STATE_COMING)
2644 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002645 buf[bx++] = ')';
2646 }
2647 buf[bx] = '\0';
2648
2649 return buf;
2650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652static int m_show(struct seq_file *m, void *p)
2653{
2654 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002655 char buf[8];
2656
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002657 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 mod->name, mod->init_size + mod->core_size);
2659 print_unload_info(m, mod);
2660
2661 /* Informative for users. */
2662 seq_printf(m, " %s",
2663 mod->state == MODULE_STATE_GOING ? "Unloading":
2664 mod->state == MODULE_STATE_COMING ? "Loading":
2665 "Live");
2666 /* Used by oprofile and other similar tools. */
2667 seq_printf(m, " 0x%p", mod->module_core);
2668
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002669 /* Taints info */
2670 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002671 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 seq_printf(m, "\n");
2674 return 0;
2675}
2676
2677/* Format: modulename size refcount deps address
2678
2679 Where refcount is a number or -, and deps is a comma-separated list
2680 of depends or -.
2681*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002682const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .start = m_start,
2684 .next = m_next,
2685 .stop = m_stop,
2686 .show = m_show
2687};
2688
2689/* Given an address, look for it in the module exception tables. */
2690const struct exception_table_entry *search_module_extables(unsigned long addr)
2691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 const struct exception_table_entry *e = NULL;
2693 struct module *mod;
2694
Rusty Russell24da1cb2007-07-15 23:41:46 -07002695 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 list_for_each_entry(mod, &modules, list) {
2697 if (mod->num_exentries == 0)
2698 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 e = search_extable(mod->extable,
2701 mod->extable + mod->num_exentries - 1,
2702 addr);
2703 if (e)
2704 break;
2705 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002706 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002709 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return e;
2711}
2712
Ingo Molnar4d435f92006-07-03 00:24:24 -07002713/*
2714 * Is this a valid module address?
2715 */
2716int is_module_address(unsigned long addr)
2717{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002718 struct module *mod;
2719
Rusty Russell24da1cb2007-07-15 23:41:46 -07002720 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002721
2722 list_for_each_entry(mod, &modules, list) {
2723 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002724 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002725 return 1;
2726 }
2727 }
2728
Rusty Russell24da1cb2007-07-15 23:41:46 -07002729 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002730
2731 return 0;
2732}
2733
2734
Rusty Russell24da1cb2007-07-15 23:41:46 -07002735/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736struct module *__module_text_address(unsigned long addr)
2737{
2738 struct module *mod;
2739
Rusty Russell3a642e92008-07-22 19:24:28 -05002740 if (addr < module_addr_min || addr > module_addr_max)
2741 return NULL;
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 list_for_each_entry(mod, &modules, list)
2744 if (within(addr, mod->module_init, mod->init_text_size)
2745 || within(addr, mod->module_core, mod->core_text_size))
2746 return mod;
2747 return NULL;
2748}
2749
2750struct module *module_text_address(unsigned long addr)
2751{
2752 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Rusty Russell24da1cb2007-07-15 23:41:46 -07002754 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002756 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 return mod;
2759}
2760
2761/* Don't grab lock, we're oopsing. */
2762void print_modules(void)
2763{
2764 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002765 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 printk("Modules linked in:");
2768 list_for_each_entry(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002769 printk(" %s%s", mod->name, module_flags(mod, buf));
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002770 if (last_unloaded_module[0])
2771 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 printk("\n");
2773}
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775#ifdef CONFIG_MODVERSIONS
2776/* Generate the signature for struct module here, too, for modversions. */
2777void struct_module(struct module *mod) { return; }
2778EXPORT_SYMBOL(struct_module);
2779#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002780
2781#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002782void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002783{
2784 struct module *mod;
2785
2786 mutex_lock(&module_mutex);
2787 list_for_each_entry(mod, &modules, list)
2788 if (!mod->taints)
2789 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002790 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002791 mutex_unlock(&module_mutex);
2792}
2793#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002794
2795#ifdef CONFIG_TRACEPOINTS
2796void module_update_tracepoints(void)
2797{
2798 struct module *mod;
2799
2800 mutex_lock(&module_mutex);
2801 list_for_each_entry(mod, &modules, list)
2802 if (!mod->taints)
2803 tracepoint_update_probe_range(mod->tracepoints,
2804 mod->tracepoints + mod->num_tracepoints);
2805 mutex_unlock(&module_mutex);
2806}
2807
2808/*
2809 * Returns 0 if current not found.
2810 * Returns 1 if current found.
2811 */
2812int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2813{
2814 struct module *iter_mod;
2815 int found = 0;
2816
2817 mutex_lock(&module_mutex);
2818 list_for_each_entry(iter_mod, &modules, list) {
2819 if (!iter_mod->taints) {
2820 /*
2821 * Sorted module list
2822 */
2823 if (iter_mod < iter->module)
2824 continue;
2825 else if (iter_mod > iter->module)
2826 iter->tracepoint = NULL;
2827 found = tracepoint_get_iter_range(&iter->tracepoint,
2828 iter_mod->tracepoints,
2829 iter_mod->tracepoints
2830 + iter_mod->num_tracepoints);
2831 if (found) {
2832 iter->module = iter_mod;
2833 break;
2834 }
2835 }
2836 }
2837 mutex_unlock(&module_mutex);
2838 return found;
2839}
2840#endif