blob: 3d256681ab64364f5dcf25d782d5e00f40f12b14 [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
Rusty Russell5e458cc2008-10-22 10:00:13 -0500135/* Find a module section, or NULL. */
136static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
137 const char *secstrings, const char *name)
138{
139 /* Section 0 has sh_addr 0. */
140 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
141}
142
143/* Find a module section, or NULL. Fill in number of "objects" in section. */
144static void *section_objs(Elf_Ehdr *hdr,
145 Elf_Shdr *sechdrs,
146 const char *secstrings,
147 const char *name,
148 size_t object_size,
149 unsigned int *num)
150{
151 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
152
153 /* Section 0 has sh_addr 0 and sh_size 0. */
154 *num = sechdrs[sec].sh_size / object_size;
155 return (void *)sechdrs[sec].sh_addr;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Provided by the linker */
159extern const struct kernel_symbol __start___ksymtab[];
160extern const struct kernel_symbol __stop___ksymtab[];
161extern const struct kernel_symbol __start___ksymtab_gpl[];
162extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800163extern const struct kernel_symbol __start___ksymtab_gpl_future[];
164extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700165extern const struct kernel_symbol __start___ksymtab_gpl_future[];
166extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167extern const unsigned long __start___kcrctab[];
168extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800169extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500170#ifdef CONFIG_UNUSED_SYMBOLS
171extern const struct kernel_symbol __start___ksymtab_unused[];
172extern const struct kernel_symbol __stop___ksymtab_unused[];
173extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
174extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700175extern const unsigned long __start___kcrctab_unused[];
176extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179#ifndef CONFIG_MODVERSIONS
180#define symversion(base, idx) NULL
181#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800182#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif
184
Rusty Russelldafd0942008-07-22 19:24:25 -0500185struct symsearch {
186 const struct kernel_symbol *start, *stop;
187 const unsigned long *crcs;
188 enum {
189 NOT_GPL_ONLY,
190 GPL_ONLY,
191 WILL_BE_GPL_ONLY,
192 } licence;
193 bool unused;
194};
195
196static bool each_symbol_in_section(const struct symsearch *arr,
197 unsigned int arrsize,
198 struct module *owner,
199 bool (*fn)(const struct symsearch *syms,
200 struct module *owner,
201 unsigned int symnum, void *data),
202 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100203{
Rusty Russelldafd0942008-07-22 19:24:25 -0500204 unsigned int i, j;
205
206 for (j = 0; j < arrsize; j++) {
207 for (i = 0; i < arr[j].stop - arr[j].start; i++)
208 if (fn(&arr[j], owner, i, data))
209 return true;
210 }
211
212 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100213}
214
Rusty Russelldafd0942008-07-22 19:24:25 -0500215/* Returns true as soon as fn returns true, otherwise false. */
216static bool each_symbol(bool (*fn)(const struct symsearch *arr,
217 struct module *owner,
218 unsigned int symnum, void *data),
219 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700220{
Rusty Russelldafd0942008-07-22 19:24:25 -0500221 struct module *mod;
222 const struct symsearch arr[] = {
223 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
224 NOT_GPL_ONLY, false },
225 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
226 __start___kcrctab_gpl,
227 GPL_ONLY, false },
228 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
229 __start___kcrctab_gpl_future,
230 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500231#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500232 { __start___ksymtab_unused, __stop___ksymtab_unused,
233 __start___kcrctab_unused,
234 NOT_GPL_ONLY, true },
235 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
236 __start___kcrctab_unused_gpl,
237 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500238#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500239 };
240
241 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
242 return true;
243
244 list_for_each_entry(mod, &modules, list) {
245 struct symsearch arr[] = {
246 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
247 NOT_GPL_ONLY, false },
248 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
249 mod->gpl_crcs,
250 GPL_ONLY, false },
251 { mod->gpl_future_syms,
252 mod->gpl_future_syms + mod->num_gpl_future_syms,
253 mod->gpl_future_crcs,
254 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500255#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500256 { mod->unused_syms,
257 mod->unused_syms + mod->num_unused_syms,
258 mod->unused_crcs,
259 NOT_GPL_ONLY, true },
260 { mod->unused_gpl_syms,
261 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
262 mod->unused_gpl_crcs,
263 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500264#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500265 };
266
267 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
268 return true;
269 }
270 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700271}
272
Rusty Russelldafd0942008-07-22 19:24:25 -0500273struct find_symbol_arg {
274 /* Input */
275 const char *name;
276 bool gplok;
277 bool warn;
278
279 /* Output */
280 struct module *owner;
281 const unsigned long *crc;
282 unsigned long value;
283};
284
285static bool find_symbol_in_section(const struct symsearch *syms,
286 struct module *owner,
287 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500288{
Rusty Russelldafd0942008-07-22 19:24:25 -0500289 struct find_symbol_arg *fsa = data;
290
291 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
292 return false;
293
294 if (!fsa->gplok) {
295 if (syms->licence == GPL_ONLY)
296 return false;
297 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
298 printk(KERN_WARNING "Symbol %s is being used "
299 "by a non-GPL module, which will not "
300 "be allowed in the future\n", fsa->name);
301 printk(KERN_WARNING "Please see the file "
302 "Documentation/feature-removal-schedule.txt "
303 "in the kernel source tree for more details.\n");
304 }
305 }
306
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500307#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500308 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500309 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500310 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500311 printk(KERN_WARNING
312 "This symbol will go away in the future.\n");
313 printk(KERN_WARNING
314 "Please evalute if this is the right api to use and if "
315 "it really is, submit a report the linux kernel "
316 "mailinglist together with submitting your code for "
317 "inclusion.\n");
318 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500319#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500320
321 fsa->owner = owner;
322 fsa->crc = symversion(syms->crcs, symnum);
323 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500324 return true;
325}
326
Rusty Russellad9546c2008-05-01 21:14:59 -0500327/* Find a symbol, return value, (optional) crc and (optional) module
328 * which owns it */
329static unsigned long find_symbol(const char *name,
330 struct module **owner,
331 const unsigned long **crc,
332 bool gplok,
333 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Rusty Russelldafd0942008-07-22 19:24:25 -0500335 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Rusty Russelldafd0942008-07-22 19:24:25 -0500337 fsa.name = name;
338 fsa.gplok = gplok;
339 fsa.warn = warn;
340
341 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500342 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500343 *owner = fsa.owner;
344 if (crc)
345 *crc = fsa.crc;
346 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800350 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/* Search for module by name: must hold module_mutex. */
354static struct module *find_module(const char *name)
355{
356 struct module *mod;
357
358 list_for_each_entry(mod, &modules, list) {
359 if (strcmp(mod->name, name) == 0)
360 return mod;
361 }
362 return NULL;
363}
364
365#ifdef CONFIG_SMP
366/* Number of blocks used and allocated. */
367static unsigned int pcpu_num_used, pcpu_num_allocated;
368/* Size of each block. -ve means used. */
369static int *pcpu_size;
370
371static int split_block(unsigned int i, unsigned short size)
372{
373 /* Reallocation required? */
374 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700375 int *new;
376
377 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
378 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!new)
380 return 0;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pcpu_size = new;
384 }
385
386 /* Insert a new subblock */
387 memmove(&pcpu_size[i+1], &pcpu_size[i],
388 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
389 pcpu_num_used++;
390
391 pcpu_size[i+1] -= size;
392 pcpu_size[i] = size;
393 return 1;
394}
395
396static inline unsigned int block_size(int val)
397{
398 if (val < 0)
399 return -val;
400 return val;
401}
402
Rusty Russell842bbaa2005-08-01 21:11:47 -0700403static void *percpu_modalloc(unsigned long size, unsigned long align,
404 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 unsigned long extra;
407 unsigned int i;
408 void *ptr;
409
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200410 if (align > PAGE_SIZE) {
411 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
412 name, align, PAGE_SIZE);
413 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 ptr = __per_cpu_start;
417 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
418 /* Extra for alignment requirement. */
419 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
420 BUG_ON(i == 0 && extra != 0);
421
422 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
423 continue;
424
425 /* Transfer extra to previous block. */
426 if (pcpu_size[i-1] < 0)
427 pcpu_size[i-1] -= extra;
428 else
429 pcpu_size[i-1] += extra;
430 pcpu_size[i] -= extra;
431 ptr += extra;
432
433 /* Split block if warranted */
434 if (pcpu_size[i] - size > sizeof(unsigned long))
435 if (!split_block(i, size))
436 return NULL;
437
438 /* Mark allocated */
439 pcpu_size[i] = -pcpu_size[i];
440 return ptr;
441 }
442
443 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
444 size);
445 return NULL;
446}
447
448static void percpu_modfree(void *freeme)
449{
450 unsigned int i;
451 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
452
453 /* First entry is core kernel percpu data. */
454 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
455 if (ptr == freeme) {
456 pcpu_size[i] = -pcpu_size[i];
457 goto free;
458 }
459 }
460 BUG();
461
462 free:
463 /* Merge with previous? */
464 if (pcpu_size[i-1] >= 0) {
465 pcpu_size[i-1] += pcpu_size[i];
466 pcpu_num_used--;
467 memmove(&pcpu_size[i], &pcpu_size[i+1],
468 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
469 i--;
470 }
471 /* Merge with next? */
472 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
473 pcpu_size[i] += pcpu_size[i+1];
474 pcpu_num_used--;
475 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
476 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
477 }
478}
479
480static unsigned int find_pcpusec(Elf_Ehdr *hdr,
481 Elf_Shdr *sechdrs,
482 const char *secstrings)
483{
484 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
485}
486
Mike Travisdd5af902008-01-30 13:33:32 +0100487static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
488{
489 int cpu;
490
491 for_each_possible_cpu(cpu)
492 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int percpu_modinit(void)
496{
497 pcpu_num_used = 2;
498 pcpu_num_allocated = 2;
499 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
500 GFP_KERNEL);
501 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200502 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Free room. */
504 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
505 if (pcpu_size[1] < 0) {
506 printk(KERN_ERR "No per-cpu room for modules.\n");
507 pcpu_num_used = 1;
508 }
509
510 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700511}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512__initcall(percpu_modinit);
513#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700514static inline void *percpu_modalloc(unsigned long size, unsigned long align,
515 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 return NULL;
518}
519static inline void percpu_modfree(void *pcpuptr)
520{
521 BUG();
522}
523static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
524 Elf_Shdr *sechdrs,
525 const char *secstrings)
526{
527 return 0;
528}
529static inline void percpu_modcopy(void *pcpudst, const void *src,
530 unsigned long size)
531{
532 /* pcpusec should be 0, and size of that section should be 0. */
533 BUG_ON(size != 0);
534}
535#endif /* CONFIG_SMP */
536
Matt Domschc988d2b2005-06-23 22:05:15 -0700537#define MODINFO_ATTR(field) \
538static void setup_modinfo_##field(struct module *mod, const char *s) \
539{ \
540 mod->field = kstrdup(s, GFP_KERNEL); \
541} \
542static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
543 struct module *mod, char *buffer) \
544{ \
545 return sprintf(buffer, "%s\n", mod->field); \
546} \
547static int modinfo_##field##_exists(struct module *mod) \
548{ \
549 return mod->field != NULL; \
550} \
551static void free_modinfo_##field(struct module *mod) \
552{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700553 kfree(mod->field); \
554 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700555} \
556static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900557 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700558 .show = show_modinfo_##field, \
559 .setup = setup_modinfo_##field, \
560 .test = modinfo_##field##_exists, \
561 .free = free_modinfo_##field, \
562};
563
564MODINFO_ATTR(version);
565MODINFO_ATTR(srcversion);
566
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100567static char last_unloaded_module[MODULE_NAME_LEN+1];
568
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800569#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/* Init the unload section of the module. */
571static void module_unload_init(struct module *mod)
572{
573 unsigned int i;
574
575 INIT_LIST_HEAD(&mod->modules_which_use_me);
576 for (i = 0; i < NR_CPUS; i++)
577 local_set(&mod->ref[i].count, 0);
578 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700579 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Backwards compatibility macros put refcount during init. */
581 mod->waiter = current;
582}
583
584/* modules using other modules */
585struct module_use
586{
587 struct list_head list;
588 struct module *module_which_uses;
589};
590
591/* Does a already use b? */
592static int already_uses(struct module *a, struct module *b)
593{
594 struct module_use *use;
595
596 list_for_each_entry(use, &b->modules_which_use_me, list) {
597 if (use->module_which_uses == a) {
598 DEBUGP("%s uses %s!\n", a->name, b->name);
599 return 1;
600 }
601 }
602 DEBUGP("%s does not use %s!\n", a->name, b->name);
603 return 0;
604}
605
606/* Module a uses b */
607static int use_module(struct module *a, struct module *b)
608{
609 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500610 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (b == NULL || already_uses(a, b)) return 1;
613
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500614 /* If we're interrupted or time out, we fail. */
615 if (wait_event_interruptible_timeout(
616 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
617 30 * HZ) <= 0) {
618 printk("%s: gave up waiting for init of module %s.\n",
619 a->name, b->name);
620 return 0;
621 }
622
623 /* If strong_try_module_get() returned a different error, we fail. */
624 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626
627 DEBUGP("Allocating new usage for %s.\n", a->name);
628 use = kmalloc(sizeof(*use), GFP_ATOMIC);
629 if (!use) {
630 printk("%s: out of memory loading\n", a->name);
631 module_put(b);
632 return 0;
633 }
634
635 use->module_which_uses = a;
636 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100637 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 1;
639}
640
641/* Clear the unload stuff of the module. */
642static void module_unload_free(struct module *mod)
643{
644 struct module *i;
645
646 list_for_each_entry(i, &modules, list) {
647 struct module_use *use;
648
649 list_for_each_entry(use, &i->modules_which_use_me, list) {
650 if (use->module_which_uses == mod) {
651 DEBUGP("%s unusing %s\n", mod->name, i->name);
652 module_put(i);
653 list_del(&use->list);
654 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100655 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* There can be at most one match. */
657 break;
658 }
659 }
660 }
661}
662
663#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800664static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int ret = (flags & O_TRUNC);
667 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800668 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return ret;
670}
671#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800672static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 return 0;
675}
676#endif /* CONFIG_MODULE_FORCE_UNLOAD */
677
678struct stopref
679{
680 struct module *mod;
681 int flags;
682 int *forced;
683};
684
685/* Whole machine is stopped with interrupts off when this runs. */
686static int __try_stop_module(void *_sref)
687{
688 struct stopref *sref = _sref;
689
Rusty Russellda39ba52008-07-22 19:24:25 -0500690 /* If it's not unused, quit unless we're forcing. */
691 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800692 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EWOULDBLOCK;
694 }
695
696 /* Mark it as dying. */
697 sref->mod->state = MODULE_STATE_GOING;
698 return 0;
699}
700
701static int try_stop_module(struct module *mod, int flags, int *forced)
702{
Rusty Russellda39ba52008-07-22 19:24:25 -0500703 if (flags & O_NONBLOCK) {
704 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500706 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500707 } else {
708 /* We don't need to stop the machine for this. */
709 mod->state = MODULE_STATE_GOING;
710 synchronize_sched();
711 return 0;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715unsigned int module_refcount(struct module *mod)
716{
717 unsigned int i, total = 0;
718
719 for (i = 0; i < NR_CPUS; i++)
720 total += local_read(&mod->ref[i].count);
721 return total;
722}
723EXPORT_SYMBOL(module_refcount);
724
725/* This exists whether we can unload or not */
726static void free_module(struct module *mod);
727
728static void wait_for_zero_refcount(struct module *mod)
729{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500730 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800731 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 for (;;) {
733 DEBUGP("Looking at refcount...\n");
734 set_current_state(TASK_UNINTERRUPTIBLE);
735 if (module_refcount(mod) == 0)
736 break;
737 schedule();
738 }
739 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800740 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800743asmlinkage long
744sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800747 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int ret, forced = 0;
749
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800750 if (!capable(CAP_SYS_MODULE))
751 return -EPERM;
752
753 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
754 return -EFAULT;
755 name[MODULE_NAME_LEN-1] = '\0';
756
Ashutosh Naik6389a382006-03-23 03:00:46 -0800757 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return -EINTR;
759
760 mod = find_module(name);
761 if (!mod) {
762 ret = -ENOENT;
763 goto out;
764 }
765
766 if (!list_empty(&mod->modules_which_use_me)) {
767 /* Other modules depend on us: get rid of them first. */
768 ret = -EWOULDBLOCK;
769 goto out;
770 }
771
772 /* Doing init or already dying? */
773 if (mod->state != MODULE_STATE_LIVE) {
774 /* FIXME: if (force), slam module count and wake up
775 waiter --RR */
776 DEBUGP("%s already dying\n", mod->name);
777 ret = -EBUSY;
778 goto out;
779 }
780
781 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700782 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800783 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!forced) {
785 /* This module can't be removed */
786 ret = -EBUSY;
787 goto out;
788 }
789 }
790
791 /* Set this up before setting mod->state */
792 mod->waiter = current;
793
794 /* Stop the machine so refcounts can't move and disable module. */
795 ret = try_stop_module(mod, flags, &forced);
796 if (ret != 0)
797 goto out;
798
799 /* Never wait if forced. */
800 if (!forced && module_refcount(mod) != 0)
801 wait_for_zero_refcount(mod);
802
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200803 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200805 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200807 blocking_notifier_call_chain(&module_notify_list,
808 MODULE_STATE_GOING, mod);
809 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100810 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500811 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400812 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 free_module(mod);
814
815 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800816 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return ret;
818}
819
820static void print_unload_info(struct seq_file *m, struct module *mod)
821{
822 struct module_use *use;
823 int printed_something = 0;
824
825 seq_printf(m, " %u ", module_refcount(mod));
826
827 /* Always include a trailing , so userspace can differentiate
828 between this and the old multi-field proc format. */
829 list_for_each_entry(use, &mod->modules_which_use_me, list) {
830 printed_something = 1;
831 seq_printf(m, "%s,", use->module_which_uses->name);
832 }
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (mod->init != NULL && mod->exit == NULL) {
835 printed_something = 1;
836 seq_printf(m, "[permanent],");
837 }
838
839 if (!printed_something)
840 seq_printf(m, "-");
841}
842
843void __symbol_put(const char *symbol)
844{
845 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Rusty Russell24da1cb2007-07-15 23:41:46 -0700847 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500848 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 BUG();
850 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700851 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853EXPORT_SYMBOL(__symbol_put);
854
855void symbol_put_addr(void *addr)
856{
Trent Piepho5e376612006-05-15 09:44:06 -0700857 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Trent Piepho5e376612006-05-15 09:44:06 -0700859 if (core_kernel_text((unsigned long)addr))
860 return;
861
862 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700864 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866EXPORT_SYMBOL_GPL(symbol_put_addr);
867
868static ssize_t show_refcnt(struct module_attribute *mattr,
869 struct module *mod, char *buffer)
870{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400871 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900875 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 .show = show_refcnt,
877};
878
Al Virof6a57032006-10-18 01:47:25 -0400879void module_put(struct module *module)
880{
881 if (module) {
882 unsigned int cpu = get_cpu();
883 local_dec(&module->ref[cpu].count);
884 /* Maybe they're waiting for us to drop reference? */
885 if (unlikely(!module_is_live(module)))
886 wake_up_process(module->waiter);
887 put_cpu();
888 }
889}
890EXPORT_SYMBOL(module_put);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#else /* !CONFIG_MODULE_UNLOAD */
893static void print_unload_info(struct seq_file *m, struct module *mod)
894{
895 /* We don't know the usage count, or what modules are using. */
896 seq_printf(m, " - -");
897}
898
899static inline void module_unload_free(struct module *mod)
900{
901}
902
903static inline int use_module(struct module *a, struct module *b)
904{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500905 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908static inline void module_unload_init(struct module *mod)
909{
910}
911#endif /* CONFIG_MODULE_UNLOAD */
912
Kay Sievers1f717402006-11-24 12:15:25 +0100913static ssize_t show_initstate(struct module_attribute *mattr,
914 struct module *mod, char *buffer)
915{
916 const char *state = "unknown";
917
918 switch (mod->state) {
919 case MODULE_STATE_LIVE:
920 state = "live";
921 break;
922 case MODULE_STATE_COMING:
923 state = "coming";
924 break;
925 case MODULE_STATE_GOING:
926 state = "going";
927 break;
928 }
929 return sprintf(buffer, "%s\n", state);
930}
931
932static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900933 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100934 .show = show_initstate,
935};
936
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800937static struct module_attribute *modinfo_attrs[] = {
938 &modinfo_version,
939 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100940 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800941#ifdef CONFIG_MODULE_UNLOAD
942 &refcnt,
943#endif
944 NULL,
945};
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static const char vermagic[] = VERMAGIC_STRING;
948
Linus Torvalds826e4502008-05-04 17:04:16 -0700949static int try_to_force_load(struct module *mod, const char *symname)
950{
951#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700952 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700953 printk("%s: no version for \"%s\" found: kernel tainted.\n",
954 mod->name, symname);
955 add_taint_module(mod, TAINT_FORCED_MODULE);
956 return 0;
957#else
958 return -ENOEXEC;
959#endif
960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962#ifdef CONFIG_MODVERSIONS
963static int check_version(Elf_Shdr *sechdrs,
964 unsigned int versindex,
965 const char *symname,
966 struct module *mod,
967 const unsigned long *crc)
968{
969 unsigned int i, num_versions;
970 struct modversion_info *versions;
971
972 /* Exporting module didn't supply crcs? OK, we're already tainted. */
973 if (!crc)
974 return 1;
975
Rusty Russella5dd6972008-05-09 16:24:21 +1000976 /* No versions at all? modprobe --force does this. */
977 if (versindex == 0)
978 return try_to_force_load(mod, symname) == 0;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 versions = (void *) sechdrs[versindex].sh_addr;
981 num_versions = sechdrs[versindex].sh_size
982 / sizeof(struct modversion_info);
983
984 for (i = 0; i < num_versions; i++) {
985 if (strcmp(versions[i].name, symname) != 0)
986 continue;
987
988 if (versions[i].crc == *crc)
989 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 DEBUGP("Found checksum %lX vs module %lX\n",
991 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700992 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700994
Rusty Russella5dd6972008-05-09 16:24:21 +1000995 printk(KERN_WARNING "%s: no symbol version for %s\n",
996 mod->name, symname);
997 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700998
999bad_version:
1000 printk("%s: disagrees about version of symbol %s\n",
1001 mod->name, symname);
1002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1006 unsigned int versindex,
1007 struct module *mod)
1008{
1009 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Rusty Russellad9546c2008-05-01 21:14:59 -05001011 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001013 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Rusty Russell91e37a72008-05-09 16:25:28 +10001016/* First part is kernel version, which we ignore if module has crcs. */
1017static inline int same_magic(const char *amagic, const char *bmagic,
1018 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Rusty Russell91e37a72008-05-09 16:25:28 +10001020 if (has_crcs) {
1021 amagic += strcspn(amagic, " ");
1022 bmagic += strcspn(bmagic, " ");
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return strcmp(amagic, bmagic) == 0;
1025}
1026#else
1027static inline int check_version(Elf_Shdr *sechdrs,
1028 unsigned int versindex,
1029 const char *symname,
1030 struct module *mod,
1031 const unsigned long *crc)
1032{
1033 return 1;
1034}
1035
1036static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1037 unsigned int versindex,
1038 struct module *mod)
1039{
1040 return 1;
1041}
1042
Rusty Russell91e37a72008-05-09 16:25:28 +10001043static inline int same_magic(const char *amagic, const char *bmagic,
1044 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 return strcmp(amagic, bmagic) == 0;
1047}
1048#endif /* CONFIG_MODVERSIONS */
1049
1050/* Resolve a symbol for this module. I.e. if we find one, record usage.
1051 Must be holding module_mutex. */
1052static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1053 unsigned int versindex,
1054 const char *name,
1055 struct module *mod)
1056{
1057 struct module *owner;
1058 unsigned long ret;
1059 const unsigned long *crc;
1060
Rusty Russellad9546c2008-05-01 21:14:59 -05001061 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001062 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001063 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001064 /* use_module can fail due to OOM,
1065 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1067 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001068 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return ret;
1071}
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073/*
1074 * /sys/module/foo/sections stuff
1075 * J. Corbet <corbet@lwn.net>
1076 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001077#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001078struct module_sect_attr
1079{
1080 struct module_attribute mattr;
1081 char *name;
1082 unsigned long address;
1083};
1084
1085struct module_sect_attrs
1086{
1087 struct attribute_group grp;
1088 unsigned int nsections;
1089 struct module_sect_attr attrs[0];
1090};
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static ssize_t module_sect_show(struct module_attribute *mattr,
1093 struct module *mod, char *buf)
1094{
1095 struct module_sect_attr *sattr =
1096 container_of(mattr, struct module_sect_attr, mattr);
1097 return sprintf(buf, "0x%lx\n", sattr->address);
1098}
1099
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001100static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1101{
Rusty Russella58730c2008-03-13 09:03:44 +00001102 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001103
1104 for (section = 0; section < sect_attrs->nsections; section++)
1105 kfree(sect_attrs->attrs[section].name);
1106 kfree(sect_attrs);
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109static void add_sect_attrs(struct module *mod, unsigned int nsect,
1110 char *secstrings, Elf_Shdr *sechdrs)
1111{
1112 unsigned int nloaded = 0, i, size[2];
1113 struct module_sect_attrs *sect_attrs;
1114 struct module_sect_attr *sattr;
1115 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Count loaded sections and allocate structures */
1118 for (i = 0; i < nsect; i++)
1119 if (sechdrs[i].sh_flags & SHF_ALLOC)
1120 nloaded++;
1121 size[0] = ALIGN(sizeof(*sect_attrs)
1122 + nloaded * sizeof(sect_attrs->attrs[0]),
1123 sizeof(sect_attrs->grp.attrs[0]));
1124 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001125 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1126 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128
1129 /* Setup section attributes. */
1130 sect_attrs->grp.name = "sections";
1131 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1132
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001133 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 sattr = &sect_attrs->attrs[0];
1135 gattr = &sect_attrs->grp.attrs[0];
1136 for (i = 0; i < nsect; i++) {
1137 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1138 continue;
1139 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001140 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1141 GFP_KERNEL);
1142 if (sattr->name == NULL)
1143 goto out;
1144 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sattr->mattr.show = module_sect_show;
1146 sattr->mattr.store = NULL;
1147 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sattr->mattr.attr.mode = S_IRUGO;
1149 *(gattr++) = &(sattr++)->mattr.attr;
1150 }
1151 *gattr = NULL;
1152
1153 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1154 goto out;
1155
1156 mod->sect_attrs = sect_attrs;
1157 return;
1158 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001159 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static void remove_sect_attrs(struct module *mod)
1163{
1164 if (mod->sect_attrs) {
1165 sysfs_remove_group(&mod->mkobj.kobj,
1166 &mod->sect_attrs->grp);
1167 /* We are positive that no one is using any sect attrs
1168 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001169 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 mod->sect_attrs = NULL;
1171 }
1172}
1173
Roland McGrath6d760132007-10-16 23:26:40 -07001174/*
1175 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1176 */
1177
1178struct module_notes_attrs {
1179 struct kobject *dir;
1180 unsigned int notes;
1181 struct bin_attribute attrs[0];
1182};
1183
1184static ssize_t module_notes_read(struct kobject *kobj,
1185 struct bin_attribute *bin_attr,
1186 char *buf, loff_t pos, size_t count)
1187{
1188 /*
1189 * The caller checked the pos and count against our size.
1190 */
1191 memcpy(buf, bin_attr->private + pos, count);
1192 return count;
1193}
1194
1195static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1196 unsigned int i)
1197{
1198 if (notes_attrs->dir) {
1199 while (i-- > 0)
1200 sysfs_remove_bin_file(notes_attrs->dir,
1201 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001202 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001203 }
1204 kfree(notes_attrs);
1205}
1206
1207static void add_notes_attrs(struct module *mod, unsigned int nsect,
1208 char *secstrings, Elf_Shdr *sechdrs)
1209{
1210 unsigned int notes, loaded, i;
1211 struct module_notes_attrs *notes_attrs;
1212 struct bin_attribute *nattr;
1213
1214 /* Count notes sections and allocate structures. */
1215 notes = 0;
1216 for (i = 0; i < nsect; i++)
1217 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1218 (sechdrs[i].sh_type == SHT_NOTE))
1219 ++notes;
1220
1221 if (notes == 0)
1222 return;
1223
1224 notes_attrs = kzalloc(sizeof(*notes_attrs)
1225 + notes * sizeof(notes_attrs->attrs[0]),
1226 GFP_KERNEL);
1227 if (notes_attrs == NULL)
1228 return;
1229
1230 notes_attrs->notes = notes;
1231 nattr = &notes_attrs->attrs[0];
1232 for (loaded = i = 0; i < nsect; ++i) {
1233 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1234 continue;
1235 if (sechdrs[i].sh_type == SHT_NOTE) {
1236 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1237 nattr->attr.mode = S_IRUGO;
1238 nattr->size = sechdrs[i].sh_size;
1239 nattr->private = (void *) sechdrs[i].sh_addr;
1240 nattr->read = module_notes_read;
1241 ++nattr;
1242 }
1243 ++loaded;
1244 }
1245
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001246 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001247 if (!notes_attrs->dir)
1248 goto out;
1249
1250 for (i = 0; i < notes; ++i)
1251 if (sysfs_create_bin_file(notes_attrs->dir,
1252 &notes_attrs->attrs[i]))
1253 goto out;
1254
1255 mod->notes_attrs = notes_attrs;
1256 return;
1257
1258 out:
1259 free_notes_attrs(notes_attrs, i);
1260}
1261
1262static void remove_notes_attrs(struct module *mod)
1263{
1264 if (mod->notes_attrs)
1265 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1271 char *sectstrings, Elf_Shdr *sechdrs)
1272{
1273}
1274
1275static inline void remove_sect_attrs(struct module *mod)
1276{
1277}
Roland McGrath6d760132007-10-16 23:26:40 -07001278
1279static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1280 char *sectstrings, Elf_Shdr *sechdrs)
1281{
1282}
1283
1284static inline void remove_notes_attrs(struct module *mod)
1285{
1286}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Randy Dunlapef665c12007-02-13 15:19:06 -08001289#ifdef CONFIG_SYSFS
1290int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001291{
1292 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001293 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001294 int error = 0;
1295 int i;
1296
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001297 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1298 (ARRAY_SIZE(modinfo_attrs) + 1)),
1299 GFP_KERNEL);
1300 if (!mod->modinfo_attrs)
1301 return -ENOMEM;
1302
1303 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001304 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1305 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001306 (attr->test && attr->test(mod))) {
1307 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001308 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1309 ++temp_attr;
1310 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001311 }
1312 return error;
1313}
1314
Randy Dunlapef665c12007-02-13 15:19:06 -08001315void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001316{
1317 struct module_attribute *attr;
1318 int i;
1319
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001320 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1321 /* pick a field to test for end of list */
1322 if (!attr->attr.name)
1323 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001324 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001325 if (attr->free)
1326 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001327 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001328 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Randy Dunlapef665c12007-02-13 15:19:06 -08001331int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001334 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001336 if (!module_sysfs_initialized) {
1337 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001338 mod->name);
1339 err = -EINVAL;
1340 goto out;
1341 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001342
1343 kobj = kset_find_obj(module_kset, mod->name);
1344 if (kobj) {
1345 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1346 kobject_put(kobj);
1347 err = -EINVAL;
1348 goto out;
1349 }
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001352
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001353 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1354 mod->mkobj.kobj.kset = module_kset;
1355 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1356 "%s", mod->name);
1357 if (err)
1358 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001359
Kay Sievers97c146e2007-11-29 23:46:11 +01001360 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001361out:
1362 return err;
1363}
1364
Randy Dunlapef665c12007-02-13 15:19:06 -08001365int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001366 struct kernel_param *kparam,
1367 unsigned int num_params)
1368{
1369 int err;
1370
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001371 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001372 if (!mod->holders_dir) {
1373 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001374 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001375 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 err = module_param_sysfs_setup(mod, kparam, num_params);
1378 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001379 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Matt Domschc988d2b2005-06-23 22:05:15 -07001381 err = module_add_modinfo_attrs(mod);
1382 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001383 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001384
Kay Sieverse17e0f52006-11-24 12:15:25 +01001385 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return 0;
1387
Kay Sieverse17e0f52006-11-24 12:15:25 +01001388out_unreg_param:
1389 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001390out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001391 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001392out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001393 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return err;
1395}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001396
1397static void mod_sysfs_fini(struct module *mod)
1398{
1399 kobject_put(&mod->mkobj.kobj);
1400}
1401
1402#else /* CONFIG_SYSFS */
1403
1404static void mod_sysfs_fini(struct module *mod)
1405{
1406}
1407
1408#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410static void mod_kobject_remove(struct module *mod)
1411{
Matt Domschc988d2b2005-06-23 22:05:15 -07001412 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001414 kobject_put(mod->mkobj.drivers_dir);
1415 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001416 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/*
Rusty Russellbb9d3d52008-01-29 17:13:21 -05001420 * link the module with the whole machine is stopped with interrupts off
1421 * - this defends against kallsyms not taking locks
1422 */
1423static int __link_module(void *_mod)
1424{
1425 struct module *mod = _mod;
1426 list_add(&mod->list, &modules);
1427 return 0;
1428}
1429
1430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 * unlink the module with the whole machine is stopped with interrupts off
1432 * - this defends against kallsyms not taking locks
1433 */
1434static int __unlink_module(void *_mod)
1435{
1436 struct module *mod = _mod;
1437 list_del(&mod->list);
1438 return 0;
1439}
1440
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001441/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442static void free_module(struct module *mod)
1443{
1444 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001445 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001446 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 remove_sect_attrs(mod);
1448 mod_kobject_remove(mod);
1449
Jan Beulich4552d5d2006-06-26 13:57:28 +02001450 unwind_remove_table(mod->unwind_info, 0);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* Arch-specific cleanup. */
1453 module_arch_cleanup(mod);
1454
1455 /* Module unload stuff */
1456 module_unload_free(mod);
1457
Steven Rostedtfed19392008-08-14 22:47:19 -04001458 /* release any pointers to mcount in this module */
1459 ftrace_release(mod->module_core, mod->core_size);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* This may be NULL, but that's OK */
1462 module_free(mod, mod->module_init);
1463 kfree(mod->args);
1464 if (mod->percpu)
1465 percpu_modfree(mod->percpu);
1466
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001467 /* Free lock-classes: */
1468 lockdep_free_key_range(mod->module_core, mod->core_size);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Finally, free the core (containing the module structure) */
1471 module_free(mod, mod->module_core);
1472}
1473
1474void *__symbol_get(const char *symbol)
1475{
1476 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001477 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Rusty Russell24da1cb2007-07-15 23:41:46 -07001479 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001480 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001481 if (IS_ERR_VALUE(value))
1482 value = 0;
1483 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001485 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 return (void *)value;
1488}
1489EXPORT_SYMBOL_GPL(__symbol_get);
1490
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001491/*
1492 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001493 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001494 */
1495static int verify_export_symbols(struct module *mod)
1496{
Rusty Russellb2111042008-05-01 21:15:00 -05001497 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001498 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001499 const struct kernel_symbol *s;
1500 struct {
1501 const struct kernel_symbol *sym;
1502 unsigned int num;
1503 } arr[] = {
1504 { mod->syms, mod->num_syms },
1505 { mod->gpl_syms, mod->num_gpl_syms },
1506 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001507#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001508 { mod->unused_syms, mod->num_unused_syms },
1509 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001510#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001511 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001512
Rusty Russellb2111042008-05-01 21:15:00 -05001513 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1514 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1515 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1516 NULL, true, false))) {
1517 printk(KERN_ERR
1518 "%s: exports duplicate symbol %s"
1519 " (owned by %s)\n",
1520 mod->name, s->name, module_name(owner));
1521 return -ENOEXEC;
1522 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001523 }
Rusty Russellb2111042008-05-01 21:15:00 -05001524 }
1525 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001526}
1527
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001528/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529static int simplify_symbols(Elf_Shdr *sechdrs,
1530 unsigned int symindex,
1531 const char *strtab,
1532 unsigned int versindex,
1533 unsigned int pcpuindex,
1534 struct module *mod)
1535{
1536 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1537 unsigned long secbase;
1538 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1539 int ret = 0;
1540
1541 for (i = 1; i < n; i++) {
1542 switch (sym[i].st_shndx) {
1543 case SHN_COMMON:
1544 /* We compiled with -fno-common. These are not
1545 supposed to happen. */
1546 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1547 printk("%s: please compile with -fno-common\n",
1548 mod->name);
1549 ret = -ENOEXEC;
1550 break;
1551
1552 case SHN_ABS:
1553 /* Don't need to do anything */
1554 DEBUGP("Absolute symbol: 0x%08lx\n",
1555 (long)sym[i].st_value);
1556 break;
1557
1558 case SHN_UNDEF:
1559 sym[i].st_value
1560 = resolve_symbol(sechdrs, versindex,
1561 strtab + sym[i].st_name, mod);
1562
1563 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001564 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 /* Ok if weak. */
1567 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1568 break;
1569
1570 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1571 mod->name, strtab + sym[i].st_name);
1572 ret = -ENOENT;
1573 break;
1574
1575 default:
1576 /* Divert to percpu allocation if a percpu var. */
1577 if (sym[i].st_shndx == pcpuindex)
1578 secbase = (unsigned long)mod->percpu;
1579 else
1580 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1581 sym[i].st_value += secbase;
1582 break;
1583 }
1584 }
1585
1586 return ret;
1587}
1588
1589/* Update size with this section: return offset. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05001590static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 long ret;
1593
1594 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1595 *size = ret + sechdr->sh_size;
1596 return ret;
1597}
1598
1599/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1600 might -- code, read-only data, read-write data, small data. Tally
1601 sizes, and place the offsets into sh_entsize fields: high bit means it
1602 belongs in init. */
1603static void layout_sections(struct module *mod,
1604 const Elf_Ehdr *hdr,
1605 Elf_Shdr *sechdrs,
1606 const char *secstrings)
1607{
1608 static unsigned long const masks[][2] = {
1609 /* NOTE: all executable code must be the first section
1610 * in this array; otherwise modify the text_size
1611 * finder in the two loops below */
1612 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1613 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1614 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1615 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1616 };
1617 unsigned int m, i;
1618
1619 for (i = 0; i < hdr->e_shnum; i++)
1620 sechdrs[i].sh_entsize = ~0UL;
1621
1622 DEBUGP("Core section allocation order:\n");
1623 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1624 for (i = 0; i < hdr->e_shnum; ++i) {
1625 Elf_Shdr *s = &sechdrs[i];
1626
1627 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1628 || (s->sh_flags & masks[m][1])
1629 || s->sh_entsize != ~0UL
1630 || strncmp(secstrings + s->sh_name,
1631 ".init", 5) == 0)
1632 continue;
1633 s->sh_entsize = get_offset(&mod->core_size, s);
1634 DEBUGP("\t%s\n", secstrings + s->sh_name);
1635 }
1636 if (m == 0)
1637 mod->core_text_size = mod->core_size;
1638 }
1639
1640 DEBUGP("Init section allocation order:\n");
1641 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1642 for (i = 0; i < hdr->e_shnum; ++i) {
1643 Elf_Shdr *s = &sechdrs[i];
1644
1645 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1646 || (s->sh_flags & masks[m][1])
1647 || s->sh_entsize != ~0UL
1648 || strncmp(secstrings + s->sh_name,
1649 ".init", 5) != 0)
1650 continue;
1651 s->sh_entsize = (get_offset(&mod->init_size, s)
1652 | INIT_OFFSET_MASK);
1653 DEBUGP("\t%s\n", secstrings + s->sh_name);
1654 }
1655 if (m == 0)
1656 mod->init_text_size = mod->init_size;
1657 }
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static void set_license(struct module *mod, const char *license)
1661{
1662 if (!license)
1663 license = "unspecified";
1664
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001665 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001666 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001667 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001668 "kernel.\n", mod->name, license);
1669 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671}
1672
1673/* Parse tag=value strings from .modinfo section */
1674static char *next_string(char *string, unsigned long *secsize)
1675{
1676 /* Skip non-zero chars */
1677 while (string[0]) {
1678 string++;
1679 if ((*secsize)-- <= 1)
1680 return NULL;
1681 }
1682
1683 /* Skip any zero padding. */
1684 while (!string[0]) {
1685 string++;
1686 if ((*secsize)-- <= 1)
1687 return NULL;
1688 }
1689 return string;
1690}
1691
1692static char *get_modinfo(Elf_Shdr *sechdrs,
1693 unsigned int info,
1694 const char *tag)
1695{
1696 char *p;
1697 unsigned int taglen = strlen(tag);
1698 unsigned long size = sechdrs[info].sh_size;
1699
1700 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1701 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1702 return p + taglen + 1;
1703 }
1704 return NULL;
1705}
1706
Matt Domschc988d2b2005-06-23 22:05:15 -07001707static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1708 unsigned int infoindex)
1709{
1710 struct module_attribute *attr;
1711 int i;
1712
1713 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1714 if (attr->setup)
1715 attr->setup(mod,
1716 get_modinfo(sechdrs,
1717 infoindex,
1718 attr->attr.name));
1719 }
1720}
Matt Domschc988d2b2005-06-23 22:05:15 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001723
1724/* lookup symbol in given range of kernel_symbols */
1725static const struct kernel_symbol *lookup_symbol(const char *name,
1726 const struct kernel_symbol *start,
1727 const struct kernel_symbol *stop)
1728{
1729 const struct kernel_symbol *ks = start;
1730 for (; ks < stop; ks++)
1731 if (strcmp(ks->name, name) == 0)
1732 return ks;
1733 return NULL;
1734}
1735
Alexey Dobriyanea078902007-05-08 00:28:39 -07001736static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001738 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1739 return 1;
1740 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001741 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001743 else
1744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747/* As per nm */
1748static char elf_type(const Elf_Sym *sym,
1749 Elf_Shdr *sechdrs,
1750 const char *secstrings,
1751 struct module *mod)
1752{
1753 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1754 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1755 return 'v';
1756 else
1757 return 'w';
1758 }
1759 if (sym->st_shndx == SHN_UNDEF)
1760 return 'U';
1761 if (sym->st_shndx == SHN_ABS)
1762 return 'a';
1763 if (sym->st_shndx >= SHN_LORESERVE)
1764 return '?';
1765 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1766 return 't';
1767 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1768 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1769 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1770 return 'r';
1771 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1772 return 'g';
1773 else
1774 return 'd';
1775 }
1776 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1777 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1778 return 's';
1779 else
1780 return 'b';
1781 }
1782 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1783 ".debug", strlen(".debug")) == 0)
1784 return 'n';
1785 return '?';
1786}
1787
1788static void add_kallsyms(struct module *mod,
1789 Elf_Shdr *sechdrs,
1790 unsigned int symindex,
1791 unsigned int strindex,
1792 const char *secstrings)
1793{
1794 unsigned int i;
1795
1796 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1797 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1798 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1799
1800 /* Set types up while we still have access to sections. */
1801 for (i = 0; i < mod->num_symtab; i++)
1802 mod->symtab[i].st_info
1803 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1804}
1805#else
1806static inline void add_kallsyms(struct module *mod,
1807 Elf_Shdr *sechdrs,
1808 unsigned int symindex,
1809 unsigned int strindex,
1810 const char *secstrings)
1811{
1812}
1813#endif /* CONFIG_KALLSYMS */
1814
Rusty Russell5e458cc2008-10-22 10:00:13 -05001815static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1816{
Jason Baron346e15b2008-08-12 16:46:19 -04001817#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
Rusty Russell5e458cc2008-10-22 10:00:13 -05001818 unsigned int i;
Jason Baron346e15b2008-08-12 16:46:19 -04001819
Rusty Russell5e458cc2008-10-22 10:00:13 -05001820 for (i = 0; i < num; i++) {
1821 register_dynamic_debug_module(debug[i].modname,
1822 debug[i].type,
1823 debug[i].logical_modname,
1824 debug[i].flag_names,
1825 debug[i].hash, debug[i].hash2);
Jason Baron346e15b2008-08-12 16:46:19 -04001826 }
Jason Baron346e15b2008-08-12 16:46:19 -04001827#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001828}
Jason Baron346e15b2008-08-12 16:46:19 -04001829
Rusty Russell3a642e92008-07-22 19:24:28 -05001830static void *module_alloc_update_bounds(unsigned long size)
1831{
1832 void *ret = module_alloc(size);
1833
1834 if (ret) {
1835 /* Update module bounds. */
1836 if ((unsigned long)ret < module_addr_min)
1837 module_addr_min = (unsigned long)ret;
1838 if ((unsigned long)ret + size > module_addr_max)
1839 module_addr_max = (unsigned long)ret + size;
1840 }
1841 return ret;
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/* Allocate and load the module: note that size of section 0 is always
1845 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001846static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 unsigned long len,
1848 const char __user *uargs)
1849{
1850 Elf_Ehdr *hdr;
1851 Elf_Shdr *sechdrs;
1852 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001853 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001854 unsigned int i;
1855 unsigned int symindex = 0;
1856 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001857 unsigned int modindex, versindex, infoindex, pcpuindex;
Andrew Morton84860f92006-06-28 04:26:46 -07001858 unsigned int unwindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001859 unsigned int num_kp, num_mcount;
1860 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct module *mod;
1862 long err = 0;
1863 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001864 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001865 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1868 umod, len, uargs);
1869 if (len < sizeof(*hdr))
1870 return ERR_PTR(-ENOEXEC);
1871
1872 /* Suck in entire file: we'll want most of it. */
1873 /* vmalloc barfs on "unusual" numbers. Check here */
1874 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1875 return ERR_PTR(-ENOMEM);
1876 if (copy_from_user(hdr, umod, len) != 0) {
1877 err = -EFAULT;
1878 goto free_hdr;
1879 }
1880
1881 /* Sanity checks against insmoding binaries or wrong arch,
1882 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001883 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 || hdr->e_type != ET_REL
1885 || !elf_check_arch(hdr)
1886 || hdr->e_shentsize != sizeof(*sechdrs)) {
1887 err = -ENOEXEC;
1888 goto free_hdr;
1889 }
1890
1891 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1892 goto truncated;
1893
1894 /* Convenience variables */
1895 sechdrs = (void *)hdr + hdr->e_shoff;
1896 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1897 sechdrs[0].sh_addr = 0;
1898
1899 for (i = 1; i < hdr->e_shnum; i++) {
1900 if (sechdrs[i].sh_type != SHT_NOBITS
1901 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1902 goto truncated;
1903
1904 /* Mark all sections sh_addr with their address in the
1905 temporary image. */
1906 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1907
1908 /* Internal symbols and strings. */
1909 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1910 symindex = i;
1911 strindex = sechdrs[i].sh_link;
1912 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1913 }
1914#ifndef CONFIG_MODULE_UNLOAD
1915 /* Don't load .exit sections */
1916 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1917 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1918#endif
1919 }
1920
1921 modindex = find_sec(hdr, sechdrs, secstrings,
1922 ".gnu.linkonce.this_module");
1923 if (!modindex) {
1924 printk(KERN_WARNING "No module found in object\n");
1925 err = -ENOEXEC;
1926 goto free_hdr;
1927 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001928 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 mod = (void *)sechdrs[modindex].sh_addr;
1930
1931 if (symindex == 0) {
1932 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1933 mod->name);
1934 err = -ENOEXEC;
1935 goto free_hdr;
1936 }
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1939 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1940 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001941#ifdef ARCH_UNWIND_SECTION_NAME
1942 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1943#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Rusty Russellea01e792008-03-13 09:02:17 +00001945 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001947 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948#ifdef CONFIG_KALLSYMS
1949 /* Keep symbol and string tables for decoding later. */
1950 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1951 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1952#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001953 if (unwindex)
1954 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* Check module struct version now, before we try to use module. */
1957 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1958 err = -ENOEXEC;
1959 goto free_hdr;
1960 }
1961
1962 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1963 /* This is allowed: modprobe --force will invalidate it. */
1964 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001965 err = try_to_force_load(mod, "magic");
1966 if (err)
1967 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001968 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1970 mod->name, modmagic, vermagic);
1971 err = -ENOEXEC;
1972 goto free_hdr;
1973 }
1974
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001975 staging = get_modinfo(sechdrs, infoindex, "staging");
1976 if (staging) {
1977 add_taint_module(mod, TAINT_CRAP);
1978 printk(KERN_WARNING "%s: module is from the staging directory,"
1979 " the quality is unknown, you have been warned.\n",
1980 mod->name);
1981 }
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001984 args = strndup_user(uargs, ~0UL >> 1);
1985 if (IS_ERR(args)) {
1986 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto free_hdr;
1988 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (find_module(mod->name)) {
1991 err = -EEXIST;
1992 goto free_mod;
1993 }
1994
1995 mod->state = MODULE_STATE_COMING;
1996
1997 /* Allow arches to frob section contents and sizes. */
1998 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1999 if (err < 0)
2000 goto free_mod;
2001
2002 if (pcpuindex) {
2003 /* We have a special allocation for this section. */
2004 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002005 sechdrs[pcpuindex].sh_addralign,
2006 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!percpu) {
2008 err = -ENOMEM;
2009 goto free_mod;
2010 }
2011 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2012 mod->percpu = percpu;
2013 }
2014
2015 /* Determine total sizes, and put offsets in sh_entsize. For now
2016 this is done generically; there doesn't appear to be any
2017 special cases for the architectures. */
2018 layout_sections(mod, hdr, sechdrs, secstrings);
2019
2020 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002021 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (!ptr) {
2023 err = -ENOMEM;
2024 goto free_percpu;
2025 }
2026 memset(ptr, 0, mod->core_size);
2027 mod->module_core = ptr;
2028
Rusty Russell3a642e92008-07-22 19:24:28 -05002029 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (!ptr && mod->init_size) {
2031 err = -ENOMEM;
2032 goto free_core;
2033 }
2034 memset(ptr, 0, mod->init_size);
2035 mod->module_init = ptr;
2036
2037 /* Transfer each section which specifies SHF_ALLOC */
2038 DEBUGP("final section addresses:\n");
2039 for (i = 0; i < hdr->e_shnum; i++) {
2040 void *dest;
2041
2042 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2043 continue;
2044
2045 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2046 dest = mod->module_init
2047 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2048 else
2049 dest = mod->module_core + sechdrs[i].sh_entsize;
2050
2051 if (sechdrs[i].sh_type != SHT_NOBITS)
2052 memcpy(dest, (void *)sechdrs[i].sh_addr,
2053 sechdrs[i].sh_size);
2054 /* Update sh_addr to point to copy in image. */
2055 sechdrs[i].sh_addr = (unsigned long)dest;
2056 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2057 }
2058 /* Module has been moved. */
2059 mod = (void *)sechdrs[modindex].sh_addr;
2060
2061 /* Now we've moved module, initialize linked lists, etc. */
2062 module_unload_init(mod);
2063
Kay Sievers97c146e2007-11-29 23:46:11 +01002064 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002065 err = mod_sysfs_init(mod);
2066 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002067 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /* Set up license info based on the info section */
2070 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2071
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002072 /*
2073 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2074 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2075 * using GPL-only symbols it needs.
2076 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002077 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002078 add_taint(TAINT_PROPRIETARY_MODULE);
2079
2080 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002081 if (strcmp(mod->name, "driverloader") == 0)
2082 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002083
Matt Domschc988d2b2005-06-23 22:05:15 -07002084 /* Set up MODINFO_ATTR fields */
2085 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /* Fix up syms, so that st_value is a pointer to location. */
2088 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2089 mod);
2090 if (err < 0)
2091 goto cleanup;
2092
Rusty Russell5e458cc2008-10-22 10:00:13 -05002093 /* Now we've got everything in the final locations, we can
2094 * find optional sections. */
2095 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2096 &num_kp);
2097 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2098 sizeof(*mod->syms), &mod->num_syms);
2099 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2100 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2101 sizeof(*mod->gpl_syms),
2102 &mod->num_gpl_syms);
2103 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2104 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2105 "__ksymtab_gpl_future",
2106 sizeof(*mod->gpl_future_syms),
2107 &mod->num_gpl_future_syms);
2108 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2109 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002111#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002112 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2113 "__ksymtab_unused",
2114 sizeof(*mod->unused_syms),
2115 &mod->num_unused_syms);
2116 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2117 "__kcrctab_unused");
2118 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2119 "__ksymtab_unused_gpl",
2120 sizeof(*mod->unused_gpl_syms),
2121 &mod->num_unused_gpl_syms);
2122 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2123 "__kcrctab_unused_gpl");
2124#endif
2125
2126#ifdef CONFIG_MARKERS
2127 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2128 sizeof(*mod->markers), &mod->num_markers);
2129#endif
2130#ifdef CONFIG_TRACEPOINTS
2131 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2132 "__tracepoints",
2133 sizeof(*mod->tracepoints),
2134 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002135#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002138 if ((mod->num_syms && !mod->crcs)
2139 || (mod->num_gpl_syms && !mod->gpl_crcs)
2140 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002141#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002142 || (mod->num_unused_syms && !mod->unused_crcs)
2143 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002144#endif
2145 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002146 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2147 err = try_to_force_load(mod, "nocrc");
2148 if (err)
2149 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151#endif
2152
2153 /* Now do relocations. */
2154 for (i = 1; i < hdr->e_shnum; i++) {
2155 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2156 unsigned int info = sechdrs[i].sh_info;
2157
2158 /* Not a valid relocation section? */
2159 if (info >= hdr->e_shnum)
2160 continue;
2161
2162 /* Don't bother with non-allocated sections */
2163 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2164 continue;
2165
2166 if (sechdrs[i].sh_type == SHT_REL)
2167 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2168 else if (sechdrs[i].sh_type == SHT_RELA)
2169 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2170 mod);
2171 if (err < 0)
2172 goto cleanup;
2173 }
2174
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002175 /* Find duplicate symbols */
2176 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002177 if (err < 0)
2178 goto cleanup;
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002181 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2182 sizeof(*mod->extable), &mod->num_exentries);
2183 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 /* Finally, copy percpu area over. */
2186 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2187 sechdrs[pcpuindex].sh_size);
2188
2189 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2190
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002191 if (!mod->taints) {
Rusty Russell5e458cc2008-10-22 10:00:13 -05002192 struct mod_debug *debug;
2193 unsigned int num_debug;
2194
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002195#ifdef CONFIG_MARKERS
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002196 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002197 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002198#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002199 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2200 sizeof(*debug), &num_debug);
2201 dynamic_printk_setup(debug, num_debug);
2202
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002203#ifdef CONFIG_TRACEPOINTS
2204 tracepoint_update_probe_range(mod->tracepoints,
2205 mod->tracepoints + mod->num_tracepoints);
2206#endif
2207 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002208
Steven Rostedtfed19392008-08-14 22:47:19 -04002209 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002210 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2211 sizeof(*mseg), &num_mcount);
2212 ftrace_init_module(mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 err = module_finalize(hdr, sechdrs, mod);
2215 if (err < 0)
2216 goto cleanup;
2217
Thomas Koeller378bac82005-09-06 15:17:11 -07002218 /* flush the icache in correct context */
2219 old_fs = get_fs();
2220 set_fs(KERNEL_DS);
2221
2222 /*
2223 * Flush the instruction cache, since we've played with text.
2224 * Do it before processing of module parameters, so the module
2225 * can provide parameter accessor functions of its own.
2226 */
2227 if (mod->module_init)
2228 flush_icache_range((unsigned long)mod->module_init,
2229 (unsigned long)mod->module_init
2230 + mod->init_size);
2231 flush_icache_range((unsigned long)mod->module_core,
2232 (unsigned long)mod->module_core + mod->core_size);
2233
2234 set_fs(old_fs);
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002237 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002238 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2239 mod->name);
2240
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002241 /* Now sew it into the lists so we can get lockdep and oops
2242 * info during argument parsing. Noone should access us, since
2243 * strong_try_module_get() will fail. */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002244 stop_machine(__link_module, mod, NULL);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002245
Rusty Russell5e458cc2008-10-22 10:00:13 -05002246 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002248 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Rusty Russell5e458cc2008-10-22 10:00:13 -05002250 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002252 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002254 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Jan Beulich4552d5d2006-06-26 13:57:28 +02002256 /* Size of section 0 is 0, so this works well if no unwind info. */
2257 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002258 (void *)sechdrs[unwindex].sh_addr,
2259 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 /* Get rid of temporary copy */
2262 vfree(hdr);
2263
2264 /* Done! */
2265 return mod;
2266
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002267 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002268 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 module_arch_cleanup(mod);
2270 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002271 kobject_del(&mod->mkobj.kobj);
2272 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002273 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002274 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 module_unload_free(mod);
2276 module_free(mod, mod->module_init);
2277 free_core:
2278 module_free(mod, mod->module_core);
2279 free_percpu:
2280 if (percpu)
2281 percpu_modfree(percpu);
2282 free_mod:
2283 kfree(args);
2284 free_hdr:
2285 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002286 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 truncated:
2289 printk(KERN_ERR "Module len %lu truncated\n", len);
2290 err = -ENOEXEC;
2291 goto free_hdr;
2292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294/* This is where the real work happens */
2295asmlinkage long
2296sys_init_module(void __user *umod,
2297 unsigned long len,
2298 const char __user *uargs)
2299{
2300 struct module *mod;
2301 int ret = 0;
2302
2303 /* Must have permission */
2304 if (!capable(CAP_SYS_MODULE))
2305 return -EPERM;
2306
2307 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002308 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return -EINTR;
2310
2311 /* Do all the hard work */
2312 mod = load_module(umod, len, uargs);
2313 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002314 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return PTR_ERR(mod);
2316 }
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002319 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Alan Sterne041c682006-03-27 01:16:30 -08002321 blocking_notifier_call_chain(&module_notify_list,
2322 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 /* Start the module */
2325 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002326 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (ret < 0) {
2328 /* Init routine failed: abort. Try to protect us from
2329 buggy refcounters. */
2330 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002331 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002332 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002333 blocking_notifier_call_chain(&module_notify_list,
2334 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002335 mutex_lock(&module_mutex);
2336 free_module(mod);
2337 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002338 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return ret;
2340 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002341 if (ret > 0) {
2342 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2343 "it should follow 0/-E convention\n"
2344 KERN_WARNING "%s: loading module anyway...\n",
2345 __func__, mod->name, ret,
2346 __func__);
2347 dump_stack();
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Rusty Russell6c5db222008-03-10 11:43:52 -07002350 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002352 wake_up(&module_wq);
2353
2354 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* Drop initial reference. */
2356 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002357 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 module_free(mod, mod->module_init);
2359 mod->module_init = NULL;
2360 mod->init_size = 0;
2361 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002362 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 return 0;
2365}
2366
2367static inline int within(unsigned long addr, void *start, unsigned long size)
2368{
2369 return ((void *)addr >= start && (void *)addr < start + size);
2370}
2371
2372#ifdef CONFIG_KALLSYMS
2373/*
2374 * This ignores the intensely annoying "mapping symbols" found
2375 * in ARM ELF files: $a, $t and $d.
2376 */
2377static inline int is_arm_mapping_symbol(const char *str)
2378{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002379 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 && (str[2] == '\0' || str[2] == '.');
2381}
2382
2383static const char *get_ksymbol(struct module *mod,
2384 unsigned long addr,
2385 unsigned long *size,
2386 unsigned long *offset)
2387{
2388 unsigned int i, best = 0;
2389 unsigned long nextval;
2390
2391 /* At worse, next value is at end of module */
2392 if (within(addr, mod->module_init, mod->init_size))
2393 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002394 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2396
2397 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002398 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 for (i = 1; i < mod->num_symtab; i++) {
2400 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2401 continue;
2402
2403 /* We ignore unnamed symbols: they're uninformative
2404 * and inserted at a whim. */
2405 if (mod->symtab[i].st_value <= addr
2406 && mod->symtab[i].st_value > mod->symtab[best].st_value
2407 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2408 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2409 best = i;
2410 if (mod->symtab[i].st_value > addr
2411 && mod->symtab[i].st_value < nextval
2412 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2413 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2414 nextval = mod->symtab[i].st_value;
2415 }
2416
2417 if (!best)
2418 return NULL;
2419
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002420 if (size)
2421 *size = nextval - mod->symtab[best].st_value;
2422 if (offset)
2423 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return mod->strtab + mod->symtab[best].st_name;
2425}
2426
Rusty Russell6dd06c92008-01-29 17:13:22 -05002427/* For kallsyms to ask for address resolution. NULL means not found. Careful
2428 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002429const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002430 unsigned long *size,
2431 unsigned long *offset,
2432 char **modname,
2433 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002436 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Rusty Russellcb2a5202008-01-14 00:55:03 -08002438 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 list_for_each_entry(mod, &modules, list) {
2440 if (within(addr, mod->module_init, mod->init_size)
2441 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002442 if (modname)
2443 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002444 ret = get_ksymbol(mod, addr, size, offset);
2445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002448 /* Make a copy in here where it's safe */
2449 if (ret) {
2450 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2451 ret = namebuf;
2452 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002453 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002454 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002457int lookup_module_symbol_name(unsigned long addr, char *symname)
2458{
2459 struct module *mod;
2460
Rusty Russellcb2a5202008-01-14 00:55:03 -08002461 preempt_disable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002462 list_for_each_entry(mod, &modules, list) {
2463 if (within(addr, mod->module_init, mod->init_size) ||
2464 within(addr, mod->module_core, mod->core_size)) {
2465 const char *sym;
2466
2467 sym = get_ksymbol(mod, addr, NULL, NULL);
2468 if (!sym)
2469 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002470 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002471 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002472 return 0;
2473 }
2474 }
2475out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002476 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002477 return -ERANGE;
2478}
2479
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002480int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2481 unsigned long *offset, char *modname, char *name)
2482{
2483 struct module *mod;
2484
Rusty Russellcb2a5202008-01-14 00:55:03 -08002485 preempt_disable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002486 list_for_each_entry(mod, &modules, list) {
2487 if (within(addr, mod->module_init, mod->init_size) ||
2488 within(addr, mod->module_core, mod->core_size)) {
2489 const char *sym;
2490
2491 sym = get_ksymbol(mod, addr, size, offset);
2492 if (!sym)
2493 goto out;
2494 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002495 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002496 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002497 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002498 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002499 return 0;
2500 }
2501 }
2502out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002503 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002504 return -ERANGE;
2505}
2506
Alexey Dobriyanea078902007-05-08 00:28:39 -07002507int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2508 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct module *mod;
2511
Rusty Russellcb2a5202008-01-14 00:55:03 -08002512 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 list_for_each_entry(mod, &modules, list) {
2514 if (symnum < mod->num_symtab) {
2515 *value = mod->symtab[symnum].st_value;
2516 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002517 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002518 KSYM_NAME_LEN);
2519 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002520 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002521 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524 symnum -= mod->num_symtab;
2525 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002526 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002527 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
2530static unsigned long mod_find_symname(struct module *mod, const char *name)
2531{
2532 unsigned int i;
2533
2534 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002535 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2536 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return mod->symtab[i].st_value;
2538 return 0;
2539}
2540
2541/* Look for this name: can be of form module:name. */
2542unsigned long module_kallsyms_lookup_name(const char *name)
2543{
2544 struct module *mod;
2545 char *colon;
2546 unsigned long ret = 0;
2547
2548 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002549 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if ((colon = strchr(name, ':')) != NULL) {
2551 *colon = '\0';
2552 if ((mod = find_module(name)) != NULL)
2553 ret = mod_find_symname(mod, colon+1);
2554 *colon = ':';
2555 } else {
2556 list_for_each_entry(mod, &modules, list)
2557 if ((ret = mod_find_symname(mod, name)) != 0)
2558 break;
2559 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002560 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return ret;
2562}
2563#endif /* CONFIG_KALLSYMS */
2564
2565/* Called by the /proc file system to return a list of modules. */
2566static void *m_start(struct seq_file *m, loff_t *pos)
2567{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002568 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002569 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2573{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002574 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
2577static void m_stop(struct seq_file *m, void *p)
2578{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002579 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002582static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002583{
2584 int bx = 0;
2585
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002586 if (mod->taints ||
2587 mod->state == MODULE_STATE_GOING ||
2588 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002589 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002590 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002591 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002592 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002593 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002594 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002595 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002596 /*
2597 * TAINT_FORCED_RMMOD: could be added.
2598 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2599 * apply to modules.
2600 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002601
2602 /* Show a - for module-is-being-unloaded */
2603 if (mod->state == MODULE_STATE_GOING)
2604 buf[bx++] = '-';
2605 /* Show a + for module-is-being-loaded */
2606 if (mod->state == MODULE_STATE_COMING)
2607 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002608 buf[bx++] = ')';
2609 }
2610 buf[bx] = '\0';
2611
2612 return buf;
2613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615static int m_show(struct seq_file *m, void *p)
2616{
2617 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002618 char buf[8];
2619
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002620 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 mod->name, mod->init_size + mod->core_size);
2622 print_unload_info(m, mod);
2623
2624 /* Informative for users. */
2625 seq_printf(m, " %s",
2626 mod->state == MODULE_STATE_GOING ? "Unloading":
2627 mod->state == MODULE_STATE_COMING ? "Loading":
2628 "Live");
2629 /* Used by oprofile and other similar tools. */
2630 seq_printf(m, " 0x%p", mod->module_core);
2631
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002632 /* Taints info */
2633 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002634 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 seq_printf(m, "\n");
2637 return 0;
2638}
2639
2640/* Format: modulename size refcount deps address
2641
2642 Where refcount is a number or -, and deps is a comma-separated list
2643 of depends or -.
2644*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002645const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .start = m_start,
2647 .next = m_next,
2648 .stop = m_stop,
2649 .show = m_show
2650};
2651
2652/* Given an address, look for it in the module exception tables. */
2653const struct exception_table_entry *search_module_extables(unsigned long addr)
2654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 const struct exception_table_entry *e = NULL;
2656 struct module *mod;
2657
Rusty Russell24da1cb2007-07-15 23:41:46 -07002658 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 list_for_each_entry(mod, &modules, list) {
2660 if (mod->num_exentries == 0)
2661 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 e = search_extable(mod->extable,
2664 mod->extable + mod->num_exentries - 1,
2665 addr);
2666 if (e)
2667 break;
2668 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002669 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002672 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return e;
2674}
2675
Ingo Molnar4d435f92006-07-03 00:24:24 -07002676/*
2677 * Is this a valid module address?
2678 */
2679int is_module_address(unsigned long addr)
2680{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002681 struct module *mod;
2682
Rusty Russell24da1cb2007-07-15 23:41:46 -07002683 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002684
2685 list_for_each_entry(mod, &modules, list) {
2686 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002687 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002688 return 1;
2689 }
2690 }
2691
Rusty Russell24da1cb2007-07-15 23:41:46 -07002692 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002693
2694 return 0;
2695}
2696
2697
Rusty Russell24da1cb2007-07-15 23:41:46 -07002698/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699struct module *__module_text_address(unsigned long addr)
2700{
2701 struct module *mod;
2702
Rusty Russell3a642e92008-07-22 19:24:28 -05002703 if (addr < module_addr_min || addr > module_addr_max)
2704 return NULL;
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 list_for_each_entry(mod, &modules, list)
2707 if (within(addr, mod->module_init, mod->init_text_size)
2708 || within(addr, mod->module_core, mod->core_text_size))
2709 return mod;
2710 return NULL;
2711}
2712
2713struct module *module_text_address(unsigned long addr)
2714{
2715 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Rusty Russell24da1cb2007-07-15 23:41:46 -07002717 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002719 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 return mod;
2722}
2723
2724/* Don't grab lock, we're oopsing. */
2725void print_modules(void)
2726{
2727 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002728 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 printk("Modules linked in:");
2731 list_for_each_entry(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002732 printk(" %s%s", mod->name, module_flags(mod, buf));
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002733 if (last_unloaded_module[0])
2734 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 printk("\n");
2736}
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738#ifdef CONFIG_MODVERSIONS
2739/* Generate the signature for struct module here, too, for modversions. */
2740void struct_module(struct module *mod) { return; }
2741EXPORT_SYMBOL(struct_module);
2742#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002743
2744#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002745void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002746{
2747 struct module *mod;
2748
2749 mutex_lock(&module_mutex);
2750 list_for_each_entry(mod, &modules, list)
2751 if (!mod->taints)
2752 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002753 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002754 mutex_unlock(&module_mutex);
2755}
2756#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002757
2758#ifdef CONFIG_TRACEPOINTS
2759void module_update_tracepoints(void)
2760{
2761 struct module *mod;
2762
2763 mutex_lock(&module_mutex);
2764 list_for_each_entry(mod, &modules, list)
2765 if (!mod->taints)
2766 tracepoint_update_probe_range(mod->tracepoints,
2767 mod->tracepoints + mod->num_tracepoints);
2768 mutex_unlock(&module_mutex);
2769}
2770
2771/*
2772 * Returns 0 if current not found.
2773 * Returns 1 if current found.
2774 */
2775int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2776{
2777 struct module *iter_mod;
2778 int found = 0;
2779
2780 mutex_lock(&module_mutex);
2781 list_for_each_entry(iter_mod, &modules, list) {
2782 if (!iter_mod->taints) {
2783 /*
2784 * Sorted module list
2785 */
2786 if (iter_mod < iter->module)
2787 continue;
2788 else if (iter_mod > iter->module)
2789 iter->tracepoint = NULL;
2790 found = tracepoint_get_iter_range(&iter->tracepoint,
2791 iter_mod->tracepoints,
2792 iter_mod->tracepoints
2793 + iter_mod->num_tracepoints);
2794 if (found) {
2795 iter->module = iter_mod;
2796 break;
2797 }
2798 }
2799 }
2800 mutex_unlock(&module_mutex);
2801 return found;
2802}
2803#endif