blob: c0f1826e2d9e1cc3094060411f44b4178d9f38b5 [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>
Andi Kleend72b3752008-08-30 10:09:00 +020045#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020048#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080049#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040050#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040051#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#if 0
54#define DEBUGP printk
55#else
56#define DEBUGP(fmt , a...)
57#endif
58
59#ifndef ARCH_SHF_SMALL
60#define ARCH_SHF_SMALL 0
61#endif
62
63/* If this is set, the section belongs in the init part of the module */
64#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
65
Rusty Russell24da1cb2007-07-15 23:41:46 -070066/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020067 * (delete uses stop_machine/add uses RCU list operations). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080068static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static LIST_HEAD(modules);
70
Rusty Russellc9a3ba52008-01-29 17:13:18 -050071/* Waiting for a module to finish initializing? */
72static DECLARE_WAIT_QUEUE_HEAD(module_wq);
73
Alan Sterne041c682006-03-27 01:16:30 -080074static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Rusty Russell3a642e92008-07-22 19:24:28 -050076/* Bounds of module allocation, for speeding __module_text_address */
77static unsigned long module_addr_min = -1UL, module_addr_max = 0;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079int register_module_notifier(struct notifier_block * nb)
80{
Alan Sterne041c682006-03-27 01:16:30 -080081 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83EXPORT_SYMBOL(register_module_notifier);
84
85int unregister_module_notifier(struct notifier_block * nb)
86{
Alan Sterne041c682006-03-27 01:16:30 -080087 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89EXPORT_SYMBOL(unregister_module_notifier);
90
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080091/* We require a truly strong try_module_get(): 0 means failure due to
92 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static inline int strong_try_module_get(struct module *mod)
94{
95 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050096 return -EBUSY;
97 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -050099 else
100 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700103static inline void add_taint_module(struct module *mod, unsigned flag)
104{
105 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700106 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700107}
108
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200109/*
110 * A thread that wants to hold a reference to a module only while it
111 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113void __module_put_and_exit(struct module *mod, long code)
114{
115 module_put(mod);
116 do_exit(code);
117}
118EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* Find a module section: 0 means not found. */
121static unsigned int find_sec(Elf_Ehdr *hdr,
122 Elf_Shdr *sechdrs,
123 const char *secstrings,
124 const char *name)
125{
126 unsigned int i;
127
128 for (i = 1; i < hdr->e_shnum; i++)
129 /* Alloc bit cleared means "ignore it." */
130 if ((sechdrs[i].sh_flags & SHF_ALLOC)
131 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
132 return i;
133 return 0;
134}
135
Rusty Russell5e458cc2008-10-22 10:00:13 -0500136/* Find a module section, or NULL. */
137static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
138 const char *secstrings, const char *name)
139{
140 /* Section 0 has sh_addr 0. */
141 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
142}
143
144/* Find a module section, or NULL. Fill in number of "objects" in section. */
145static void *section_objs(Elf_Ehdr *hdr,
146 Elf_Shdr *sechdrs,
147 const char *secstrings,
148 const char *name,
149 size_t object_size,
150 unsigned int *num)
151{
152 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
153
154 /* Section 0 has sh_addr 0 and sh_size 0. */
155 *num = sechdrs[sec].sh_size / object_size;
156 return (void *)sechdrs[sec].sh_addr;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Provided by the linker */
160extern const struct kernel_symbol __start___ksymtab[];
161extern const struct kernel_symbol __stop___ksymtab[];
162extern const struct kernel_symbol __start___ksymtab_gpl[];
163extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800164extern const struct kernel_symbol __start___ksymtab_gpl_future[];
165extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700166extern const struct kernel_symbol __start___ksymtab_gpl_future[];
167extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168extern const unsigned long __start___kcrctab[];
169extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800170extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500171#ifdef CONFIG_UNUSED_SYMBOLS
172extern const struct kernel_symbol __start___ksymtab_unused[];
173extern const struct kernel_symbol __stop___ksymtab_unused[];
174extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
175extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700176extern const unsigned long __start___kcrctab_unused[];
177extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180#ifndef CONFIG_MODVERSIONS
181#define symversion(base, idx) NULL
182#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800183#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#endif
185
Rusty Russelldafd0942008-07-22 19:24:25 -0500186struct symsearch {
187 const struct kernel_symbol *start, *stop;
188 const unsigned long *crcs;
189 enum {
190 NOT_GPL_ONLY,
191 GPL_ONLY,
192 WILL_BE_GPL_ONLY,
193 } licence;
194 bool unused;
195};
196
197static bool each_symbol_in_section(const struct symsearch *arr,
198 unsigned int arrsize,
199 struct module *owner,
200 bool (*fn)(const struct symsearch *syms,
201 struct module *owner,
202 unsigned int symnum, void *data),
203 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100204{
Rusty Russelldafd0942008-07-22 19:24:25 -0500205 unsigned int i, j;
206
207 for (j = 0; j < arrsize; j++) {
208 for (i = 0; i < arr[j].stop - arr[j].start; i++)
209 if (fn(&arr[j], owner, i, data))
210 return true;
211 }
212
213 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100214}
215
Rusty Russelldafd0942008-07-22 19:24:25 -0500216/* Returns true as soon as fn returns true, otherwise false. */
217static bool each_symbol(bool (*fn)(const struct symsearch *arr,
218 struct module *owner,
219 unsigned int symnum, void *data),
220 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700221{
Rusty Russelldafd0942008-07-22 19:24:25 -0500222 struct module *mod;
223 const struct symsearch arr[] = {
224 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
225 NOT_GPL_ONLY, false },
226 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
227 __start___kcrctab_gpl,
228 GPL_ONLY, false },
229 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
230 __start___kcrctab_gpl_future,
231 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500232#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500233 { __start___ksymtab_unused, __stop___ksymtab_unused,
234 __start___kcrctab_unused,
235 NOT_GPL_ONLY, true },
236 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
237 __start___kcrctab_unused_gpl,
238 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500239#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500240 };
241
242 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
243 return true;
244
Andi Kleend72b3752008-08-30 10:09:00 +0200245 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500246 struct symsearch arr[] = {
247 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
248 NOT_GPL_ONLY, false },
249 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
250 mod->gpl_crcs,
251 GPL_ONLY, false },
252 { mod->gpl_future_syms,
253 mod->gpl_future_syms + mod->num_gpl_future_syms,
254 mod->gpl_future_crcs,
255 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500256#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500257 { mod->unused_syms,
258 mod->unused_syms + mod->num_unused_syms,
259 mod->unused_crcs,
260 NOT_GPL_ONLY, true },
261 { mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
263 mod->unused_gpl_crcs,
264 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500265#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500266 };
267
268 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
269 return true;
270 }
271 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700272}
273
Rusty Russelldafd0942008-07-22 19:24:25 -0500274struct find_symbol_arg {
275 /* Input */
276 const char *name;
277 bool gplok;
278 bool warn;
279
280 /* Output */
281 struct module *owner;
282 const unsigned long *crc;
283 unsigned long value;
284};
285
286static bool find_symbol_in_section(const struct symsearch *syms,
287 struct module *owner,
288 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500289{
Rusty Russelldafd0942008-07-22 19:24:25 -0500290 struct find_symbol_arg *fsa = data;
291
292 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
293 return false;
294
295 if (!fsa->gplok) {
296 if (syms->licence == GPL_ONLY)
297 return false;
298 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
299 printk(KERN_WARNING "Symbol %s is being used "
300 "by a non-GPL module, which will not "
301 "be allowed in the future\n", fsa->name);
302 printk(KERN_WARNING "Please see the file "
303 "Documentation/feature-removal-schedule.txt "
304 "in the kernel source tree for more details.\n");
305 }
306 }
307
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500308#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500309 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500310 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500311 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500312 printk(KERN_WARNING
313 "This symbol will go away in the future.\n");
314 printk(KERN_WARNING
315 "Please evalute if this is the right api to use and if "
316 "it really is, submit a report the linux kernel "
317 "mailinglist together with submitting your code for "
318 "inclusion.\n");
319 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500320#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500321
322 fsa->owner = owner;
323 fsa->crc = symversion(syms->crcs, symnum);
324 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500325 return true;
326}
327
Rusty Russellad9546c2008-05-01 21:14:59 -0500328/* Find a symbol, return value, (optional) crc and (optional) module
329 * which owns it */
330static unsigned long find_symbol(const char *name,
331 struct module **owner,
332 const unsigned long **crc,
333 bool gplok,
334 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Rusty Russelldafd0942008-07-22 19:24:25 -0500336 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Rusty Russelldafd0942008-07-22 19:24:25 -0500338 fsa.name = name;
339 fsa.gplok = gplok;
340 fsa.warn = warn;
341
342 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500343 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500344 *owner = fsa.owner;
345 if (crc)
346 *crc = fsa.crc;
347 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800351 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/* Search for module by name: must hold module_mutex. */
355static struct module *find_module(const char *name)
356{
357 struct module *mod;
358
359 list_for_each_entry(mod, &modules, list) {
360 if (strcmp(mod->name, name) == 0)
361 return mod;
362 }
363 return NULL;
364}
365
366#ifdef CONFIG_SMP
367/* Number of blocks used and allocated. */
368static unsigned int pcpu_num_used, pcpu_num_allocated;
369/* Size of each block. -ve means used. */
370static int *pcpu_size;
371
372static int split_block(unsigned int i, unsigned short size)
373{
374 /* Reallocation required? */
375 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700376 int *new;
377
378 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
379 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!new)
381 return 0;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 pcpu_size = new;
385 }
386
387 /* Insert a new subblock */
388 memmove(&pcpu_size[i+1], &pcpu_size[i],
389 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
390 pcpu_num_used++;
391
392 pcpu_size[i+1] -= size;
393 pcpu_size[i] = size;
394 return 1;
395}
396
397static inline unsigned int block_size(int val)
398{
399 if (val < 0)
400 return -val;
401 return val;
402}
403
Rusty Russell842bbaa2005-08-01 21:11:47 -0700404static void *percpu_modalloc(unsigned long size, unsigned long align,
405 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 unsigned long extra;
408 unsigned int i;
409 void *ptr;
410
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200411 if (align > PAGE_SIZE) {
412 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
413 name, align, PAGE_SIZE);
414 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 ptr = __per_cpu_start;
418 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
419 /* Extra for alignment requirement. */
420 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
421 BUG_ON(i == 0 && extra != 0);
422
423 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
424 continue;
425
426 /* Transfer extra to previous block. */
427 if (pcpu_size[i-1] < 0)
428 pcpu_size[i-1] -= extra;
429 else
430 pcpu_size[i-1] += extra;
431 pcpu_size[i] -= extra;
432 ptr += extra;
433
434 /* Split block if warranted */
435 if (pcpu_size[i] - size > sizeof(unsigned long))
436 if (!split_block(i, size))
437 return NULL;
438
439 /* Mark allocated */
440 pcpu_size[i] = -pcpu_size[i];
441 return ptr;
442 }
443
444 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
445 size);
446 return NULL;
447}
448
449static void percpu_modfree(void *freeme)
450{
451 unsigned int i;
452 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
453
454 /* First entry is core kernel percpu data. */
455 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
456 if (ptr == freeme) {
457 pcpu_size[i] = -pcpu_size[i];
458 goto free;
459 }
460 }
461 BUG();
462
463 free:
464 /* Merge with previous? */
465 if (pcpu_size[i-1] >= 0) {
466 pcpu_size[i-1] += pcpu_size[i];
467 pcpu_num_used--;
468 memmove(&pcpu_size[i], &pcpu_size[i+1],
469 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
470 i--;
471 }
472 /* Merge with next? */
473 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
474 pcpu_size[i] += pcpu_size[i+1];
475 pcpu_num_used--;
476 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
477 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
478 }
479}
480
481static unsigned int find_pcpusec(Elf_Ehdr *hdr,
482 Elf_Shdr *sechdrs,
483 const char *secstrings)
484{
485 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
486}
487
Mike Travisdd5af902008-01-30 13:33:32 +0100488static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
489{
490 int cpu;
491
492 for_each_possible_cpu(cpu)
493 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static int percpu_modinit(void)
497{
498 pcpu_num_used = 2;
499 pcpu_num_allocated = 2;
500 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
501 GFP_KERNEL);
502 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200503 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Free room. */
505 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
506 if (pcpu_size[1] < 0) {
507 printk(KERN_ERR "No per-cpu room for modules.\n");
508 pcpu_num_used = 1;
509 }
510
511 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700512}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513__initcall(percpu_modinit);
514#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700515static inline void *percpu_modalloc(unsigned long size, unsigned long align,
516 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 return NULL;
519}
520static inline void percpu_modfree(void *pcpuptr)
521{
522 BUG();
523}
524static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
525 Elf_Shdr *sechdrs,
526 const char *secstrings)
527{
528 return 0;
529}
530static inline void percpu_modcopy(void *pcpudst, const void *src,
531 unsigned long size)
532{
533 /* pcpusec should be 0, and size of that section should be 0. */
534 BUG_ON(size != 0);
535}
536#endif /* CONFIG_SMP */
537
Matt Domschc988d2b2005-06-23 22:05:15 -0700538#define MODINFO_ATTR(field) \
539static void setup_modinfo_##field(struct module *mod, const char *s) \
540{ \
541 mod->field = kstrdup(s, GFP_KERNEL); \
542} \
543static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
544 struct module *mod, char *buffer) \
545{ \
546 return sprintf(buffer, "%s\n", mod->field); \
547} \
548static int modinfo_##field##_exists(struct module *mod) \
549{ \
550 return mod->field != NULL; \
551} \
552static void free_modinfo_##field(struct module *mod) \
553{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700554 kfree(mod->field); \
555 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700556} \
557static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900558 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700559 .show = show_modinfo_##field, \
560 .setup = setup_modinfo_##field, \
561 .test = modinfo_##field##_exists, \
562 .free = free_modinfo_##field, \
563};
564
565MODINFO_ATTR(version);
566MODINFO_ATTR(srcversion);
567
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100568static char last_unloaded_module[MODULE_NAME_LEN+1];
569
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800570#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/* Init the unload section of the module. */
572static void module_unload_init(struct module *mod)
573{
574 unsigned int i;
575
576 INIT_LIST_HEAD(&mod->modules_which_use_me);
577 for (i = 0; i < NR_CPUS; i++)
578 local_set(&mod->ref[i].count, 0);
579 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700580 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Backwards compatibility macros put refcount during init. */
582 mod->waiter = current;
583}
584
585/* modules using other modules */
586struct module_use
587{
588 struct list_head list;
589 struct module *module_which_uses;
590};
591
592/* Does a already use b? */
593static int already_uses(struct module *a, struct module *b)
594{
595 struct module_use *use;
596
597 list_for_each_entry(use, &b->modules_which_use_me, list) {
598 if (use->module_which_uses == a) {
599 DEBUGP("%s uses %s!\n", a->name, b->name);
600 return 1;
601 }
602 }
603 DEBUGP("%s does not use %s!\n", a->name, b->name);
604 return 0;
605}
606
607/* Module a uses b */
608static int use_module(struct module *a, struct module *b)
609{
610 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500611 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (b == NULL || already_uses(a, b)) return 1;
614
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500615 /* If we're interrupted or time out, we fail. */
616 if (wait_event_interruptible_timeout(
617 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
618 30 * HZ) <= 0) {
619 printk("%s: gave up waiting for init of module %s.\n",
620 a->name, b->name);
621 return 0;
622 }
623
624 /* If strong_try_module_get() returned a different error, we fail. */
625 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627
628 DEBUGP("Allocating new usage for %s.\n", a->name);
629 use = kmalloc(sizeof(*use), GFP_ATOMIC);
630 if (!use) {
631 printk("%s: out of memory loading\n", a->name);
632 module_put(b);
633 return 0;
634 }
635
636 use->module_which_uses = a;
637 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100638 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 1;
640}
641
642/* Clear the unload stuff of the module. */
643static void module_unload_free(struct module *mod)
644{
645 struct module *i;
646
647 list_for_each_entry(i, &modules, list) {
648 struct module_use *use;
649
650 list_for_each_entry(use, &i->modules_which_use_me, list) {
651 if (use->module_which_uses == mod) {
652 DEBUGP("%s unusing %s\n", mod->name, i->name);
653 module_put(i);
654 list_del(&use->list);
655 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100656 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* There can be at most one match. */
658 break;
659 }
660 }
661 }
662}
663
664#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800665static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 int ret = (flags & O_TRUNC);
668 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800669 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return ret;
671}
672#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800673static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 return 0;
676}
677#endif /* CONFIG_MODULE_FORCE_UNLOAD */
678
679struct stopref
680{
681 struct module *mod;
682 int flags;
683 int *forced;
684};
685
686/* Whole machine is stopped with interrupts off when this runs. */
687static int __try_stop_module(void *_sref)
688{
689 struct stopref *sref = _sref;
690
Rusty Russellda39ba52008-07-22 19:24:25 -0500691 /* If it's not unused, quit unless we're forcing. */
692 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800693 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EWOULDBLOCK;
695 }
696
697 /* Mark it as dying. */
698 sref->mod->state = MODULE_STATE_GOING;
699 return 0;
700}
701
702static int try_stop_module(struct module *mod, int flags, int *forced)
703{
Rusty Russellda39ba52008-07-22 19:24:25 -0500704 if (flags & O_NONBLOCK) {
705 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500707 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500708 } else {
709 /* We don't need to stop the machine for this. */
710 mod->state = MODULE_STATE_GOING;
711 synchronize_sched();
712 return 0;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716unsigned int module_refcount(struct module *mod)
717{
718 unsigned int i, total = 0;
719
720 for (i = 0; i < NR_CPUS; i++)
721 total += local_read(&mod->ref[i].count);
722 return total;
723}
724EXPORT_SYMBOL(module_refcount);
725
726/* This exists whether we can unload or not */
727static void free_module(struct module *mod);
728
729static void wait_for_zero_refcount(struct module *mod)
730{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500731 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800732 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 for (;;) {
734 DEBUGP("Looking at refcount...\n");
735 set_current_state(TASK_UNINTERRUPTIBLE);
736 if (module_refcount(mod) == 0)
737 break;
738 schedule();
739 }
740 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800741 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800744asmlinkage long
745sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800748 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int ret, forced = 0;
750
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800751 if (!capable(CAP_SYS_MODULE))
752 return -EPERM;
753
754 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
755 return -EFAULT;
756 name[MODULE_NAME_LEN-1] = '\0';
757
Ashutosh Naik6389a382006-03-23 03:00:46 -0800758 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EINTR;
760
761 mod = find_module(name);
762 if (!mod) {
763 ret = -ENOENT;
764 goto out;
765 }
766
767 if (!list_empty(&mod->modules_which_use_me)) {
768 /* Other modules depend on us: get rid of them first. */
769 ret = -EWOULDBLOCK;
770 goto out;
771 }
772
773 /* Doing init or already dying? */
774 if (mod->state != MODULE_STATE_LIVE) {
775 /* FIXME: if (force), slam module count and wake up
776 waiter --RR */
777 DEBUGP("%s already dying\n", mod->name);
778 ret = -EBUSY;
779 goto out;
780 }
781
782 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700783 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800784 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!forced) {
786 /* This module can't be removed */
787 ret = -EBUSY;
788 goto out;
789 }
790 }
791
792 /* Set this up before setting mod->state */
793 mod->waiter = current;
794
795 /* Stop the machine so refcounts can't move and disable module. */
796 ret = try_stop_module(mod, flags, &forced);
797 if (ret != 0)
798 goto out;
799
800 /* Never wait if forced. */
801 if (!forced && module_refcount(mod) != 0)
802 wait_for_zero_refcount(mod);
803
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200804 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200806 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200808 blocking_notifier_call_chain(&module_notify_list,
809 MODULE_STATE_GOING, mod);
810 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100811 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500812 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400813 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 free_module(mod);
815
816 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800817 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return ret;
819}
820
821static void print_unload_info(struct seq_file *m, struct module *mod)
822{
823 struct module_use *use;
824 int printed_something = 0;
825
826 seq_printf(m, " %u ", module_refcount(mod));
827
828 /* Always include a trailing , so userspace can differentiate
829 between this and the old multi-field proc format. */
830 list_for_each_entry(use, &mod->modules_which_use_me, list) {
831 printed_something = 1;
832 seq_printf(m, "%s,", use->module_which_uses->name);
833 }
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (mod->init != NULL && mod->exit == NULL) {
836 printed_something = 1;
837 seq_printf(m, "[permanent],");
838 }
839
840 if (!printed_something)
841 seq_printf(m, "-");
842}
843
844void __symbol_put(const char *symbol)
845{
846 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Rusty Russell24da1cb2007-07-15 23:41:46 -0700848 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500849 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 BUG();
851 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700852 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854EXPORT_SYMBOL(__symbol_put);
855
856void symbol_put_addr(void *addr)
857{
Trent Piepho5e376612006-05-15 09:44:06 -0700858 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Trent Piepho5e376612006-05-15 09:44:06 -0700860 if (core_kernel_text((unsigned long)addr))
861 return;
862
863 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700865 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867EXPORT_SYMBOL_GPL(symbol_put_addr);
868
869static ssize_t show_refcnt(struct module_attribute *mattr,
870 struct module *mod, char *buffer)
871{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400872 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900876 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .show = show_refcnt,
878};
879
Al Virof6a57032006-10-18 01:47:25 -0400880void module_put(struct module *module)
881{
882 if (module) {
883 unsigned int cpu = get_cpu();
884 local_dec(&module->ref[cpu].count);
885 /* Maybe they're waiting for us to drop reference? */
886 if (unlikely(!module_is_live(module)))
887 wake_up_process(module->waiter);
888 put_cpu();
889 }
890}
891EXPORT_SYMBOL(module_put);
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#else /* !CONFIG_MODULE_UNLOAD */
894static void print_unload_info(struct seq_file *m, struct module *mod)
895{
896 /* We don't know the usage count, or what modules are using. */
897 seq_printf(m, " - -");
898}
899
900static inline void module_unload_free(struct module *mod)
901{
902}
903
904static inline int use_module(struct module *a, struct module *b)
905{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500906 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static inline void module_unload_init(struct module *mod)
910{
911}
912#endif /* CONFIG_MODULE_UNLOAD */
913
Kay Sievers1f717402006-11-24 12:15:25 +0100914static ssize_t show_initstate(struct module_attribute *mattr,
915 struct module *mod, char *buffer)
916{
917 const char *state = "unknown";
918
919 switch (mod->state) {
920 case MODULE_STATE_LIVE:
921 state = "live";
922 break;
923 case MODULE_STATE_COMING:
924 state = "coming";
925 break;
926 case MODULE_STATE_GOING:
927 state = "going";
928 break;
929 }
930 return sprintf(buffer, "%s\n", state);
931}
932
933static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900934 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100935 .show = show_initstate,
936};
937
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800938static struct module_attribute *modinfo_attrs[] = {
939 &modinfo_version,
940 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100941 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800942#ifdef CONFIG_MODULE_UNLOAD
943 &refcnt,
944#endif
945 NULL,
946};
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948static const char vermagic[] = VERMAGIC_STRING;
949
Linus Torvalds826e4502008-05-04 17:04:16 -0700950static int try_to_force_load(struct module *mod, const char *symname)
951{
952#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700953 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700954 printk("%s: no version for \"%s\" found: kernel tainted.\n",
955 mod->name, symname);
956 add_taint_module(mod, TAINT_FORCED_MODULE);
957 return 0;
958#else
959 return -ENOEXEC;
960#endif
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963#ifdef CONFIG_MODVERSIONS
964static int check_version(Elf_Shdr *sechdrs,
965 unsigned int versindex,
966 const char *symname,
967 struct module *mod,
968 const unsigned long *crc)
969{
970 unsigned int i, num_versions;
971 struct modversion_info *versions;
972
973 /* Exporting module didn't supply crcs? OK, we're already tainted. */
974 if (!crc)
975 return 1;
976
Rusty Russella5dd6972008-05-09 16:24:21 +1000977 /* No versions at all? modprobe --force does this. */
978 if (versindex == 0)
979 return try_to_force_load(mod, symname) == 0;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 versions = (void *) sechdrs[versindex].sh_addr;
982 num_versions = sechdrs[versindex].sh_size
983 / sizeof(struct modversion_info);
984
985 for (i = 0; i < num_versions; i++) {
986 if (strcmp(versions[i].name, symname) != 0)
987 continue;
988
989 if (versions[i].crc == *crc)
990 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 DEBUGP("Found checksum %lX vs module %lX\n",
992 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -0700993 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds826e4502008-05-04 17:04:16 -0700995
Rusty Russella5dd6972008-05-09 16:24:21 +1000996 printk(KERN_WARNING "%s: no symbol version for %s\n",
997 mod->name, symname);
998 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -0700999
1000bad_version:
1001 printk("%s: disagrees about version of symbol %s\n",
1002 mod->name, symname);
1003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1007 unsigned int versindex,
1008 struct module *mod)
1009{
1010 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Rusty Russellad9546c2008-05-01 21:14:59 -05001012 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001014 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Rusty Russell91e37a72008-05-09 16:25:28 +10001017/* First part is kernel version, which we ignore if module has crcs. */
1018static inline int same_magic(const char *amagic, const char *bmagic,
1019 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Rusty Russell91e37a72008-05-09 16:25:28 +10001021 if (has_crcs) {
1022 amagic += strcspn(amagic, " ");
1023 bmagic += strcspn(bmagic, " ");
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return strcmp(amagic, bmagic) == 0;
1026}
1027#else
1028static inline int check_version(Elf_Shdr *sechdrs,
1029 unsigned int versindex,
1030 const char *symname,
1031 struct module *mod,
1032 const unsigned long *crc)
1033{
1034 return 1;
1035}
1036
1037static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1038 unsigned int versindex,
1039 struct module *mod)
1040{
1041 return 1;
1042}
1043
Rusty Russell91e37a72008-05-09 16:25:28 +10001044static inline int same_magic(const char *amagic, const char *bmagic,
1045 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 return strcmp(amagic, bmagic) == 0;
1048}
1049#endif /* CONFIG_MODVERSIONS */
1050
1051/* Resolve a symbol for this module. I.e. if we find one, record usage.
1052 Must be holding module_mutex. */
1053static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1054 unsigned int versindex,
1055 const char *name,
1056 struct module *mod)
1057{
1058 struct module *owner;
1059 unsigned long ret;
1060 const unsigned long *crc;
1061
Rusty Russellad9546c2008-05-01 21:14:59 -05001062 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001063 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001064 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001065 /* use_module can fail due to OOM,
1066 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1068 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001069 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return ret;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/*
1075 * /sys/module/foo/sections stuff
1076 * J. Corbet <corbet@lwn.net>
1077 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001078#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001079struct module_sect_attr
1080{
1081 struct module_attribute mattr;
1082 char *name;
1083 unsigned long address;
1084};
1085
1086struct module_sect_attrs
1087{
1088 struct attribute_group grp;
1089 unsigned int nsections;
1090 struct module_sect_attr attrs[0];
1091};
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093static ssize_t module_sect_show(struct module_attribute *mattr,
1094 struct module *mod, char *buf)
1095{
1096 struct module_sect_attr *sattr =
1097 container_of(mattr, struct module_sect_attr, mattr);
1098 return sprintf(buf, "0x%lx\n", sattr->address);
1099}
1100
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001101static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1102{
Rusty Russella58730c2008-03-13 09:03:44 +00001103 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001104
1105 for (section = 0; section < sect_attrs->nsections; section++)
1106 kfree(sect_attrs->attrs[section].name);
1107 kfree(sect_attrs);
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110static void add_sect_attrs(struct module *mod, unsigned int nsect,
1111 char *secstrings, Elf_Shdr *sechdrs)
1112{
1113 unsigned int nloaded = 0, i, size[2];
1114 struct module_sect_attrs *sect_attrs;
1115 struct module_sect_attr *sattr;
1116 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* Count loaded sections and allocate structures */
1119 for (i = 0; i < nsect; i++)
1120 if (sechdrs[i].sh_flags & SHF_ALLOC)
1121 nloaded++;
1122 size[0] = ALIGN(sizeof(*sect_attrs)
1123 + nloaded * sizeof(sect_attrs->attrs[0]),
1124 sizeof(sect_attrs->grp.attrs[0]));
1125 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001126 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1127 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return;
1129
1130 /* Setup section attributes. */
1131 sect_attrs->grp.name = "sections";
1132 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1133
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001134 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 sattr = &sect_attrs->attrs[0];
1136 gattr = &sect_attrs->grp.attrs[0];
1137 for (i = 0; i < nsect; i++) {
1138 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1139 continue;
1140 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001141 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1142 GFP_KERNEL);
1143 if (sattr->name == NULL)
1144 goto out;
1145 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sattr->mattr.show = module_sect_show;
1147 sattr->mattr.store = NULL;
1148 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sattr->mattr.attr.mode = S_IRUGO;
1150 *(gattr++) = &(sattr++)->mattr.attr;
1151 }
1152 *gattr = NULL;
1153
1154 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1155 goto out;
1156
1157 mod->sect_attrs = sect_attrs;
1158 return;
1159 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001160 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163static void remove_sect_attrs(struct module *mod)
1164{
1165 if (mod->sect_attrs) {
1166 sysfs_remove_group(&mod->mkobj.kobj,
1167 &mod->sect_attrs->grp);
1168 /* We are positive that no one is using any sect attrs
1169 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001170 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 mod->sect_attrs = NULL;
1172 }
1173}
1174
Roland McGrath6d760132007-10-16 23:26:40 -07001175/*
1176 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1177 */
1178
1179struct module_notes_attrs {
1180 struct kobject *dir;
1181 unsigned int notes;
1182 struct bin_attribute attrs[0];
1183};
1184
1185static ssize_t module_notes_read(struct kobject *kobj,
1186 struct bin_attribute *bin_attr,
1187 char *buf, loff_t pos, size_t count)
1188{
1189 /*
1190 * The caller checked the pos and count against our size.
1191 */
1192 memcpy(buf, bin_attr->private + pos, count);
1193 return count;
1194}
1195
1196static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1197 unsigned int i)
1198{
1199 if (notes_attrs->dir) {
1200 while (i-- > 0)
1201 sysfs_remove_bin_file(notes_attrs->dir,
1202 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001203 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001204 }
1205 kfree(notes_attrs);
1206}
1207
1208static void add_notes_attrs(struct module *mod, unsigned int nsect,
1209 char *secstrings, Elf_Shdr *sechdrs)
1210{
1211 unsigned int notes, loaded, i;
1212 struct module_notes_attrs *notes_attrs;
1213 struct bin_attribute *nattr;
1214
1215 /* Count notes sections and allocate structures. */
1216 notes = 0;
1217 for (i = 0; i < nsect; i++)
1218 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1219 (sechdrs[i].sh_type == SHT_NOTE))
1220 ++notes;
1221
1222 if (notes == 0)
1223 return;
1224
1225 notes_attrs = kzalloc(sizeof(*notes_attrs)
1226 + notes * sizeof(notes_attrs->attrs[0]),
1227 GFP_KERNEL);
1228 if (notes_attrs == NULL)
1229 return;
1230
1231 notes_attrs->notes = notes;
1232 nattr = &notes_attrs->attrs[0];
1233 for (loaded = i = 0; i < nsect; ++i) {
1234 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1235 continue;
1236 if (sechdrs[i].sh_type == SHT_NOTE) {
1237 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1238 nattr->attr.mode = S_IRUGO;
1239 nattr->size = sechdrs[i].sh_size;
1240 nattr->private = (void *) sechdrs[i].sh_addr;
1241 nattr->read = module_notes_read;
1242 ++nattr;
1243 }
1244 ++loaded;
1245 }
1246
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001247 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001248 if (!notes_attrs->dir)
1249 goto out;
1250
1251 for (i = 0; i < notes; ++i)
1252 if (sysfs_create_bin_file(notes_attrs->dir,
1253 &notes_attrs->attrs[i]))
1254 goto out;
1255
1256 mod->notes_attrs = notes_attrs;
1257 return;
1258
1259 out:
1260 free_notes_attrs(notes_attrs, i);
1261}
1262
1263static void remove_notes_attrs(struct module *mod)
1264{
1265 if (mod->notes_attrs)
1266 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1272 char *sectstrings, Elf_Shdr *sechdrs)
1273{
1274}
1275
1276static inline void remove_sect_attrs(struct module *mod)
1277{
1278}
Roland McGrath6d760132007-10-16 23:26:40 -07001279
1280static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1281 char *sectstrings, Elf_Shdr *sechdrs)
1282{
1283}
1284
1285static inline void remove_notes_attrs(struct module *mod)
1286{
1287}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Randy Dunlapef665c12007-02-13 15:19:06 -08001290#ifdef CONFIG_SYSFS
1291int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001292{
1293 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001294 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001295 int error = 0;
1296 int i;
1297
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001298 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1299 (ARRAY_SIZE(modinfo_attrs) + 1)),
1300 GFP_KERNEL);
1301 if (!mod->modinfo_attrs)
1302 return -ENOMEM;
1303
1304 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001305 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1306 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001307 (attr->test && attr->test(mod))) {
1308 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001309 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1310 ++temp_attr;
1311 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001312 }
1313 return error;
1314}
1315
Randy Dunlapef665c12007-02-13 15:19:06 -08001316void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001317{
1318 struct module_attribute *attr;
1319 int i;
1320
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001321 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1322 /* pick a field to test for end of list */
1323 if (!attr->attr.name)
1324 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001325 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001326 if (attr->free)
1327 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001328 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001329 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Randy Dunlapef665c12007-02-13 15:19:06 -08001332int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001335 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001337 if (!module_sysfs_initialized) {
1338 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001339 mod->name);
1340 err = -EINVAL;
1341 goto out;
1342 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001343
1344 kobj = kset_find_obj(module_kset, mod->name);
1345 if (kobj) {
1346 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1347 kobject_put(kobj);
1348 err = -EINVAL;
1349 goto out;
1350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001353
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001354 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1355 mod->mkobj.kobj.kset = module_kset;
1356 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1357 "%s", mod->name);
1358 if (err)
1359 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001360
Kay Sievers97c146e2007-11-29 23:46:11 +01001361 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001362out:
1363 return err;
1364}
1365
Randy Dunlapef665c12007-02-13 15:19:06 -08001366int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001367 struct kernel_param *kparam,
1368 unsigned int num_params)
1369{
1370 int err;
1371
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001372 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001373 if (!mod->holders_dir) {
1374 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001375 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001376 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 err = module_param_sysfs_setup(mod, kparam, num_params);
1379 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001380 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Matt Domschc988d2b2005-06-23 22:05:15 -07001382 err = module_add_modinfo_attrs(mod);
1383 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001384 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001385
Kay Sieverse17e0f52006-11-24 12:15:25 +01001386 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388
Kay Sieverse17e0f52006-11-24 12:15:25 +01001389out_unreg_param:
1390 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001391out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001392 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001393out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001394 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return err;
1396}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001397
1398static void mod_sysfs_fini(struct module *mod)
1399{
1400 kobject_put(&mod->mkobj.kobj);
1401}
1402
1403#else /* CONFIG_SYSFS */
1404
1405static void mod_sysfs_fini(struct module *mod)
1406{
1407}
1408
1409#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411static void mod_kobject_remove(struct module *mod)
1412{
Matt Domschc988d2b2005-06-23 22:05:15 -07001413 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001415 kobject_put(mod->mkobj.drivers_dir);
1416 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001417 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420/*
1421 * unlink the module with the whole machine is stopped with interrupts off
1422 * - this defends against kallsyms not taking locks
1423 */
1424static int __unlink_module(void *_mod)
1425{
1426 struct module *mod = _mod;
1427 list_del(&mod->list);
1428 return 0;
1429}
1430
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001431/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static void free_module(struct module *mod)
1433{
1434 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001435 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001436 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 remove_sect_attrs(mod);
1438 mod_kobject_remove(mod);
1439
Jan Beulich4552d5d2006-06-26 13:57:28 +02001440 unwind_remove_table(mod->unwind_info, 0);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* Arch-specific cleanup. */
1443 module_arch_cleanup(mod);
1444
1445 /* Module unload stuff */
1446 module_unload_free(mod);
1447
Steven Rostedtfed19392008-08-14 22:47:19 -04001448 /* release any pointers to mcount in this module */
1449 ftrace_release(mod->module_core, mod->core_size);
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* This may be NULL, but that's OK */
1452 module_free(mod, mod->module_init);
1453 kfree(mod->args);
1454 if (mod->percpu)
1455 percpu_modfree(mod->percpu);
1456
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001457 /* Free lock-classes: */
1458 lockdep_free_key_range(mod->module_core, mod->core_size);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* Finally, free the core (containing the module structure) */
1461 module_free(mod, mod->module_core);
1462}
1463
1464void *__symbol_get(const char *symbol)
1465{
1466 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001467 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Rusty Russell24da1cb2007-07-15 23:41:46 -07001469 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001470 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001471 if (IS_ERR_VALUE(value))
1472 value = 0;
1473 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001475 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 return (void *)value;
1478}
1479EXPORT_SYMBOL_GPL(__symbol_get);
1480
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001481/*
1482 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001483 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001484 */
1485static int verify_export_symbols(struct module *mod)
1486{
Rusty Russellb2111042008-05-01 21:15:00 -05001487 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001488 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001489 const struct kernel_symbol *s;
1490 struct {
1491 const struct kernel_symbol *sym;
1492 unsigned int num;
1493 } arr[] = {
1494 { mod->syms, mod->num_syms },
1495 { mod->gpl_syms, mod->num_gpl_syms },
1496 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001497#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001498 { mod->unused_syms, mod->num_unused_syms },
1499 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001500#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001501 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001502
Rusty Russellb2111042008-05-01 21:15:00 -05001503 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1504 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1505 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1506 NULL, true, false))) {
1507 printk(KERN_ERR
1508 "%s: exports duplicate symbol %s"
1509 " (owned by %s)\n",
1510 mod->name, s->name, module_name(owner));
1511 return -ENOEXEC;
1512 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001513 }
Rusty Russellb2111042008-05-01 21:15:00 -05001514 }
1515 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001516}
1517
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001518/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519static int simplify_symbols(Elf_Shdr *sechdrs,
1520 unsigned int symindex,
1521 const char *strtab,
1522 unsigned int versindex,
1523 unsigned int pcpuindex,
1524 struct module *mod)
1525{
1526 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1527 unsigned long secbase;
1528 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1529 int ret = 0;
1530
1531 for (i = 1; i < n; i++) {
1532 switch (sym[i].st_shndx) {
1533 case SHN_COMMON:
1534 /* We compiled with -fno-common. These are not
1535 supposed to happen. */
1536 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1537 printk("%s: please compile with -fno-common\n",
1538 mod->name);
1539 ret = -ENOEXEC;
1540 break;
1541
1542 case SHN_ABS:
1543 /* Don't need to do anything */
1544 DEBUGP("Absolute symbol: 0x%08lx\n",
1545 (long)sym[i].st_value);
1546 break;
1547
1548 case SHN_UNDEF:
1549 sym[i].st_value
1550 = resolve_symbol(sechdrs, versindex,
1551 strtab + sym[i].st_name, mod);
1552
1553 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001554 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556 /* Ok if weak. */
1557 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1558 break;
1559
1560 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1561 mod->name, strtab + sym[i].st_name);
1562 ret = -ENOENT;
1563 break;
1564
1565 default:
1566 /* Divert to percpu allocation if a percpu var. */
1567 if (sym[i].st_shndx == pcpuindex)
1568 secbase = (unsigned long)mod->percpu;
1569 else
1570 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1571 sym[i].st_value += secbase;
1572 break;
1573 }
1574 }
1575
1576 return ret;
1577}
1578
1579/* Update size with this section: return offset. */
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05001580static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 long ret;
1583
1584 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1585 *size = ret + sechdr->sh_size;
1586 return ret;
1587}
1588
1589/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1590 might -- code, read-only data, read-write data, small data. Tally
1591 sizes, and place the offsets into sh_entsize fields: high bit means it
1592 belongs in init. */
1593static void layout_sections(struct module *mod,
1594 const Elf_Ehdr *hdr,
1595 Elf_Shdr *sechdrs,
1596 const char *secstrings)
1597{
1598 static unsigned long const masks[][2] = {
1599 /* NOTE: all executable code must be the first section
1600 * in this array; otherwise modify the text_size
1601 * finder in the two loops below */
1602 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1603 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1604 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1605 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1606 };
1607 unsigned int m, i;
1608
1609 for (i = 0; i < hdr->e_shnum; i++)
1610 sechdrs[i].sh_entsize = ~0UL;
1611
1612 DEBUGP("Core section allocation order:\n");
1613 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1614 for (i = 0; i < hdr->e_shnum; ++i) {
1615 Elf_Shdr *s = &sechdrs[i];
1616
1617 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1618 || (s->sh_flags & masks[m][1])
1619 || s->sh_entsize != ~0UL
1620 || strncmp(secstrings + s->sh_name,
1621 ".init", 5) == 0)
1622 continue;
1623 s->sh_entsize = get_offset(&mod->core_size, s);
1624 DEBUGP("\t%s\n", secstrings + s->sh_name);
1625 }
1626 if (m == 0)
1627 mod->core_text_size = mod->core_size;
1628 }
1629
1630 DEBUGP("Init section allocation order:\n");
1631 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1632 for (i = 0; i < hdr->e_shnum; ++i) {
1633 Elf_Shdr *s = &sechdrs[i];
1634
1635 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1636 || (s->sh_flags & masks[m][1])
1637 || s->sh_entsize != ~0UL
1638 || strncmp(secstrings + s->sh_name,
1639 ".init", 5) != 0)
1640 continue;
1641 s->sh_entsize = (get_offset(&mod->init_size, s)
1642 | INIT_OFFSET_MASK);
1643 DEBUGP("\t%s\n", secstrings + s->sh_name);
1644 }
1645 if (m == 0)
1646 mod->init_text_size = mod->init_size;
1647 }
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650static void set_license(struct module *mod, const char *license)
1651{
1652 if (!license)
1653 license = "unspecified";
1654
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001655 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001656 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001657 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001658 "kernel.\n", mod->name, license);
1659 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661}
1662
1663/* Parse tag=value strings from .modinfo section */
1664static char *next_string(char *string, unsigned long *secsize)
1665{
1666 /* Skip non-zero chars */
1667 while (string[0]) {
1668 string++;
1669 if ((*secsize)-- <= 1)
1670 return NULL;
1671 }
1672
1673 /* Skip any zero padding. */
1674 while (!string[0]) {
1675 string++;
1676 if ((*secsize)-- <= 1)
1677 return NULL;
1678 }
1679 return string;
1680}
1681
1682static char *get_modinfo(Elf_Shdr *sechdrs,
1683 unsigned int info,
1684 const char *tag)
1685{
1686 char *p;
1687 unsigned int taglen = strlen(tag);
1688 unsigned long size = sechdrs[info].sh_size;
1689
1690 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1691 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1692 return p + taglen + 1;
1693 }
1694 return NULL;
1695}
1696
Matt Domschc988d2b2005-06-23 22:05:15 -07001697static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1698 unsigned int infoindex)
1699{
1700 struct module_attribute *attr;
1701 int i;
1702
1703 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1704 if (attr->setup)
1705 attr->setup(mod,
1706 get_modinfo(sechdrs,
1707 infoindex,
1708 attr->attr.name));
1709 }
1710}
Matt Domschc988d2b2005-06-23 22:05:15 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001713
1714/* lookup symbol in given range of kernel_symbols */
1715static const struct kernel_symbol *lookup_symbol(const char *name,
1716 const struct kernel_symbol *start,
1717 const struct kernel_symbol *stop)
1718{
1719 const struct kernel_symbol *ks = start;
1720 for (; ks < stop; ks++)
1721 if (strcmp(ks->name, name) == 0)
1722 return ks;
1723 return NULL;
1724}
1725
Alexey Dobriyanea078902007-05-08 00:28:39 -07001726static int is_exported(const char *name, const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001728 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1729 return 1;
1730 else
Jesper Juhlf867d2a2006-06-25 05:47:09 -07001731 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 1;
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001733 else
1734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737/* As per nm */
1738static char elf_type(const Elf_Sym *sym,
1739 Elf_Shdr *sechdrs,
1740 const char *secstrings,
1741 struct module *mod)
1742{
1743 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1744 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1745 return 'v';
1746 else
1747 return 'w';
1748 }
1749 if (sym->st_shndx == SHN_UNDEF)
1750 return 'U';
1751 if (sym->st_shndx == SHN_ABS)
1752 return 'a';
1753 if (sym->st_shndx >= SHN_LORESERVE)
1754 return '?';
1755 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1756 return 't';
1757 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1758 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1759 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1760 return 'r';
1761 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1762 return 'g';
1763 else
1764 return 'd';
1765 }
1766 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1767 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1768 return 's';
1769 else
1770 return 'b';
1771 }
1772 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1773 ".debug", strlen(".debug")) == 0)
1774 return 'n';
1775 return '?';
1776}
1777
1778static void add_kallsyms(struct module *mod,
1779 Elf_Shdr *sechdrs,
1780 unsigned int symindex,
1781 unsigned int strindex,
1782 const char *secstrings)
1783{
1784 unsigned int i;
1785
1786 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1787 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1788 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1789
1790 /* Set types up while we still have access to sections. */
1791 for (i = 0; i < mod->num_symtab; i++)
1792 mod->symtab[i].st_info
1793 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1794}
1795#else
1796static inline void add_kallsyms(struct module *mod,
1797 Elf_Shdr *sechdrs,
1798 unsigned int symindex,
1799 unsigned int strindex,
1800 const char *secstrings)
1801{
1802}
1803#endif /* CONFIG_KALLSYMS */
1804
Rusty Russell5e458cc2008-10-22 10:00:13 -05001805static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1806{
Jason Baron346e15b2008-08-12 16:46:19 -04001807#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
Rusty Russell5e458cc2008-10-22 10:00:13 -05001808 unsigned int i;
Jason Baron346e15b2008-08-12 16:46:19 -04001809
Rusty Russell5e458cc2008-10-22 10:00:13 -05001810 for (i = 0; i < num; i++) {
1811 register_dynamic_debug_module(debug[i].modname,
1812 debug[i].type,
1813 debug[i].logical_modname,
1814 debug[i].flag_names,
1815 debug[i].hash, debug[i].hash2);
Jason Baron346e15b2008-08-12 16:46:19 -04001816 }
Jason Baron346e15b2008-08-12 16:46:19 -04001817#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001818}
Jason Baron346e15b2008-08-12 16:46:19 -04001819
Rusty Russell3a642e92008-07-22 19:24:28 -05001820static void *module_alloc_update_bounds(unsigned long size)
1821{
1822 void *ret = module_alloc(size);
1823
1824 if (ret) {
1825 /* Update module bounds. */
1826 if ((unsigned long)ret < module_addr_min)
1827 module_addr_min = (unsigned long)ret;
1828 if ((unsigned long)ret + size > module_addr_max)
1829 module_addr_max = (unsigned long)ret + size;
1830 }
1831 return ret;
1832}
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/* Allocate and load the module: note that size of section 0 is always
1835 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001836static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 unsigned long len,
1838 const char __user *uargs)
1839{
1840 Elf_Ehdr *hdr;
1841 Elf_Shdr *sechdrs;
1842 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001843 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001844 unsigned int i;
1845 unsigned int symindex = 0;
1846 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001847 unsigned int modindex, versindex, infoindex, pcpuindex;
Andrew Morton84860f92006-06-28 04:26:46 -07001848 unsigned int unwindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001849 unsigned int num_kp, num_mcount;
1850 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 struct module *mod;
1852 long err = 0;
1853 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001854 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001855 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1858 umod, len, uargs);
1859 if (len < sizeof(*hdr))
1860 return ERR_PTR(-ENOEXEC);
1861
1862 /* Suck in entire file: we'll want most of it. */
1863 /* vmalloc barfs on "unusual" numbers. Check here */
1864 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1865 return ERR_PTR(-ENOMEM);
1866 if (copy_from_user(hdr, umod, len) != 0) {
1867 err = -EFAULT;
1868 goto free_hdr;
1869 }
1870
1871 /* Sanity checks against insmoding binaries or wrong arch,
1872 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001873 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 || hdr->e_type != ET_REL
1875 || !elf_check_arch(hdr)
1876 || hdr->e_shentsize != sizeof(*sechdrs)) {
1877 err = -ENOEXEC;
1878 goto free_hdr;
1879 }
1880
1881 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1882 goto truncated;
1883
1884 /* Convenience variables */
1885 sechdrs = (void *)hdr + hdr->e_shoff;
1886 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1887 sechdrs[0].sh_addr = 0;
1888
1889 for (i = 1; i < hdr->e_shnum; i++) {
1890 if (sechdrs[i].sh_type != SHT_NOBITS
1891 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1892 goto truncated;
1893
1894 /* Mark all sections sh_addr with their address in the
1895 temporary image. */
1896 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1897
1898 /* Internal symbols and strings. */
1899 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1900 symindex = i;
1901 strindex = sechdrs[i].sh_link;
1902 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1903 }
1904#ifndef CONFIG_MODULE_UNLOAD
1905 /* Don't load .exit sections */
1906 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1907 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1908#endif
1909 }
1910
1911 modindex = find_sec(hdr, sechdrs, secstrings,
1912 ".gnu.linkonce.this_module");
1913 if (!modindex) {
1914 printk(KERN_WARNING "No module found in object\n");
1915 err = -ENOEXEC;
1916 goto free_hdr;
1917 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001918 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 mod = (void *)sechdrs[modindex].sh_addr;
1920
1921 if (symindex == 0) {
1922 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1923 mod->name);
1924 err = -ENOEXEC;
1925 goto free_hdr;
1926 }
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1929 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1930 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001931#ifdef ARCH_UNWIND_SECTION_NAME
1932 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1933#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Rusty Russellea01e792008-03-13 09:02:17 +00001935 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001937 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938#ifdef CONFIG_KALLSYMS
1939 /* Keep symbol and string tables for decoding later. */
1940 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1941 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1942#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001943 if (unwindex)
1944 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /* Check module struct version now, before we try to use module. */
1947 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1948 err = -ENOEXEC;
1949 goto free_hdr;
1950 }
1951
1952 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1953 /* This is allowed: modprobe --force will invalidate it. */
1954 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001955 err = try_to_force_load(mod, "magic");
1956 if (err)
1957 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001958 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1960 mod->name, modmagic, vermagic);
1961 err = -ENOEXEC;
1962 goto free_hdr;
1963 }
1964
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001965 staging = get_modinfo(sechdrs, infoindex, "staging");
1966 if (staging) {
1967 add_taint_module(mod, TAINT_CRAP);
1968 printk(KERN_WARNING "%s: module is from the staging directory,"
1969 " the quality is unknown, you have been warned.\n",
1970 mod->name);
1971 }
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001974 args = strndup_user(uargs, ~0UL >> 1);
1975 if (IS_ERR(args)) {
1976 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 goto free_hdr;
1978 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (find_module(mod->name)) {
1981 err = -EEXIST;
1982 goto free_mod;
1983 }
1984
1985 mod->state = MODULE_STATE_COMING;
1986
1987 /* Allow arches to frob section contents and sizes. */
1988 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1989 if (err < 0)
1990 goto free_mod;
1991
1992 if (pcpuindex) {
1993 /* We have a special allocation for this section. */
1994 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07001995 sechdrs[pcpuindex].sh_addralign,
1996 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (!percpu) {
1998 err = -ENOMEM;
1999 goto free_mod;
2000 }
2001 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2002 mod->percpu = percpu;
2003 }
2004
2005 /* Determine total sizes, and put offsets in sh_entsize. For now
2006 this is done generically; there doesn't appear to be any
2007 special cases for the architectures. */
2008 layout_sections(mod, hdr, sechdrs, secstrings);
2009
2010 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002011 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (!ptr) {
2013 err = -ENOMEM;
2014 goto free_percpu;
2015 }
2016 memset(ptr, 0, mod->core_size);
2017 mod->module_core = ptr;
2018
Rusty Russell3a642e92008-07-22 19:24:28 -05002019 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (!ptr && mod->init_size) {
2021 err = -ENOMEM;
2022 goto free_core;
2023 }
2024 memset(ptr, 0, mod->init_size);
2025 mod->module_init = ptr;
2026
2027 /* Transfer each section which specifies SHF_ALLOC */
2028 DEBUGP("final section addresses:\n");
2029 for (i = 0; i < hdr->e_shnum; i++) {
2030 void *dest;
2031
2032 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2033 continue;
2034
2035 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2036 dest = mod->module_init
2037 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2038 else
2039 dest = mod->module_core + sechdrs[i].sh_entsize;
2040
2041 if (sechdrs[i].sh_type != SHT_NOBITS)
2042 memcpy(dest, (void *)sechdrs[i].sh_addr,
2043 sechdrs[i].sh_size);
2044 /* Update sh_addr to point to copy in image. */
2045 sechdrs[i].sh_addr = (unsigned long)dest;
2046 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2047 }
2048 /* Module has been moved. */
2049 mod = (void *)sechdrs[modindex].sh_addr;
2050
2051 /* Now we've moved module, initialize linked lists, etc. */
2052 module_unload_init(mod);
2053
Kay Sievers97c146e2007-11-29 23:46:11 +01002054 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002055 err = mod_sysfs_init(mod);
2056 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002057 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* Set up license info based on the info section */
2060 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2061
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002062 /*
2063 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2064 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2065 * using GPL-only symbols it needs.
2066 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002067 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002068 add_taint(TAINT_PROPRIETARY_MODULE);
2069
2070 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002071 if (strcmp(mod->name, "driverloader") == 0)
2072 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002073
Matt Domschc988d2b2005-06-23 22:05:15 -07002074 /* Set up MODINFO_ATTR fields */
2075 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /* Fix up syms, so that st_value is a pointer to location. */
2078 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2079 mod);
2080 if (err < 0)
2081 goto cleanup;
2082
Rusty Russell5e458cc2008-10-22 10:00:13 -05002083 /* Now we've got everything in the final locations, we can
2084 * find optional sections. */
2085 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2086 &num_kp);
2087 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2088 sizeof(*mod->syms), &mod->num_syms);
2089 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2090 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2091 sizeof(*mod->gpl_syms),
2092 &mod->num_gpl_syms);
2093 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2094 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2095 "__ksymtab_gpl_future",
2096 sizeof(*mod->gpl_future_syms),
2097 &mod->num_gpl_future_syms);
2098 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2099 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002101#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002102 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2103 "__ksymtab_unused",
2104 sizeof(*mod->unused_syms),
2105 &mod->num_unused_syms);
2106 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2107 "__kcrctab_unused");
2108 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2109 "__ksymtab_unused_gpl",
2110 sizeof(*mod->unused_gpl_syms),
2111 &mod->num_unused_gpl_syms);
2112 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2113 "__kcrctab_unused_gpl");
2114#endif
2115
2116#ifdef CONFIG_MARKERS
2117 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2118 sizeof(*mod->markers), &mod->num_markers);
2119#endif
2120#ifdef CONFIG_TRACEPOINTS
2121 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2122 "__tracepoints",
2123 sizeof(*mod->tracepoints),
2124 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002125#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002128 if ((mod->num_syms && !mod->crcs)
2129 || (mod->num_gpl_syms && !mod->gpl_crcs)
2130 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002131#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002132 || (mod->num_unused_syms && !mod->unused_crcs)
2133 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002134#endif
2135 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002136 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2137 err = try_to_force_load(mod, "nocrc");
2138 if (err)
2139 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141#endif
2142
2143 /* Now do relocations. */
2144 for (i = 1; i < hdr->e_shnum; i++) {
2145 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2146 unsigned int info = sechdrs[i].sh_info;
2147
2148 /* Not a valid relocation section? */
2149 if (info >= hdr->e_shnum)
2150 continue;
2151
2152 /* Don't bother with non-allocated sections */
2153 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2154 continue;
2155
2156 if (sechdrs[i].sh_type == SHT_REL)
2157 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2158 else if (sechdrs[i].sh_type == SHT_RELA)
2159 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2160 mod);
2161 if (err < 0)
2162 goto cleanup;
2163 }
2164
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002165 /* Find duplicate symbols */
2166 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002167 if (err < 0)
2168 goto cleanup;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002171 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2172 sizeof(*mod->extable), &mod->num_exentries);
2173 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /* Finally, copy percpu area over. */
2176 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2177 sechdrs[pcpuindex].sh_size);
2178
2179 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2180
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002181 if (!mod->taints) {
Rusty Russell5e458cc2008-10-22 10:00:13 -05002182 struct mod_debug *debug;
2183 unsigned int num_debug;
2184
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002185#ifdef CONFIG_MARKERS
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002186 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002187 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002188#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002189 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2190 sizeof(*debug), &num_debug);
2191 dynamic_printk_setup(debug, num_debug);
2192
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002193#ifdef CONFIG_TRACEPOINTS
2194 tracepoint_update_probe_range(mod->tracepoints,
2195 mod->tracepoints + mod->num_tracepoints);
2196#endif
2197 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002198
Steven Rostedtfed19392008-08-14 22:47:19 -04002199 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002200 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2201 sizeof(*mseg), &num_mcount);
2202 ftrace_init_module(mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 err = module_finalize(hdr, sechdrs, mod);
2205 if (err < 0)
2206 goto cleanup;
2207
Thomas Koeller378bac82005-09-06 15:17:11 -07002208 /* flush the icache in correct context */
2209 old_fs = get_fs();
2210 set_fs(KERNEL_DS);
2211
2212 /*
2213 * Flush the instruction cache, since we've played with text.
2214 * Do it before processing of module parameters, so the module
2215 * can provide parameter accessor functions of its own.
2216 */
2217 if (mod->module_init)
2218 flush_icache_range((unsigned long)mod->module_init,
2219 (unsigned long)mod->module_init
2220 + mod->init_size);
2221 flush_icache_range((unsigned long)mod->module_core,
2222 (unsigned long)mod->module_core + mod->core_size);
2223
2224 set_fs(old_fs);
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002227 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002228 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2229 mod->name);
2230
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002231 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002232 * info during argument parsing. Noone should access us, since
2233 * strong_try_module_get() will fail.
2234 * lockdep/oops can run asynchronous, so use the RCU list insertion
2235 * function to insert in a way safe to concurrent readers.
2236 * The mutex protects against concurrent writers.
2237 */
2238 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002239
Rusty Russell5e458cc2008-10-22 10:00:13 -05002240 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002242 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Rusty Russell5e458cc2008-10-22 10:00:13 -05002244 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002246 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002248 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Jan Beulich4552d5d2006-06-26 13:57:28 +02002250 /* Size of section 0 is 0, so this works well if no unwind info. */
2251 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002252 (void *)sechdrs[unwindex].sh_addr,
2253 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* Get rid of temporary copy */
2256 vfree(hdr);
2257
2258 /* Done! */
2259 return mod;
2260
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002261 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002262 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 module_arch_cleanup(mod);
2264 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002265 kobject_del(&mod->mkobj.kobj);
2266 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002267 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002268 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 module_unload_free(mod);
2270 module_free(mod, mod->module_init);
2271 free_core:
2272 module_free(mod, mod->module_core);
2273 free_percpu:
2274 if (percpu)
2275 percpu_modfree(percpu);
2276 free_mod:
2277 kfree(args);
2278 free_hdr:
2279 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002280 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 truncated:
2283 printk(KERN_ERR "Module len %lu truncated\n", len);
2284 err = -ENOEXEC;
2285 goto free_hdr;
2286}
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288/* This is where the real work happens */
2289asmlinkage long
2290sys_init_module(void __user *umod,
2291 unsigned long len,
2292 const char __user *uargs)
2293{
2294 struct module *mod;
2295 int ret = 0;
2296
2297 /* Must have permission */
2298 if (!capable(CAP_SYS_MODULE))
2299 return -EPERM;
2300
2301 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002302 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return -EINTR;
2304
2305 /* Do all the hard work */
2306 mod = load_module(umod, len, uargs);
2307 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002308 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return PTR_ERR(mod);
2310 }
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002313 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Alan Sterne041c682006-03-27 01:16:30 -08002315 blocking_notifier_call_chain(&module_notify_list,
2316 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /* Start the module */
2319 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002320 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (ret < 0) {
2322 /* Init routine failed: abort. Try to protect us from
2323 buggy refcounters. */
2324 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002325 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002326 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002327 blocking_notifier_call_chain(&module_notify_list,
2328 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002329 mutex_lock(&module_mutex);
2330 free_module(mod);
2331 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002332 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return ret;
2334 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002335 if (ret > 0) {
2336 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2337 "it should follow 0/-E convention\n"
2338 KERN_WARNING "%s: loading module anyway...\n",
2339 __func__, mod->name, ret,
2340 __func__);
2341 dump_stack();
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Rusty Russell6c5db222008-03-10 11:43:52 -07002344 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002346 wake_up(&module_wq);
2347
2348 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* Drop initial reference. */
2350 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002351 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 module_free(mod, mod->module_init);
2353 mod->module_init = NULL;
2354 mod->init_size = 0;
2355 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002356 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 return 0;
2359}
2360
2361static inline int within(unsigned long addr, void *start, unsigned long size)
2362{
2363 return ((void *)addr >= start && (void *)addr < start + size);
2364}
2365
2366#ifdef CONFIG_KALLSYMS
2367/*
2368 * This ignores the intensely annoying "mapping symbols" found
2369 * in ARM ELF files: $a, $t and $d.
2370 */
2371static inline int is_arm_mapping_symbol(const char *str)
2372{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002373 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 && (str[2] == '\0' || str[2] == '.');
2375}
2376
2377static const char *get_ksymbol(struct module *mod,
2378 unsigned long addr,
2379 unsigned long *size,
2380 unsigned long *offset)
2381{
2382 unsigned int i, best = 0;
2383 unsigned long nextval;
2384
2385 /* At worse, next value is at end of module */
2386 if (within(addr, mod->module_init, mod->init_size))
2387 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002388 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2390
2391 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002392 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 for (i = 1; i < mod->num_symtab; i++) {
2394 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2395 continue;
2396
2397 /* We ignore unnamed symbols: they're uninformative
2398 * and inserted at a whim. */
2399 if (mod->symtab[i].st_value <= addr
2400 && mod->symtab[i].st_value > mod->symtab[best].st_value
2401 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2402 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2403 best = i;
2404 if (mod->symtab[i].st_value > addr
2405 && mod->symtab[i].st_value < nextval
2406 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2407 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2408 nextval = mod->symtab[i].st_value;
2409 }
2410
2411 if (!best)
2412 return NULL;
2413
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002414 if (size)
2415 *size = nextval - mod->symtab[best].st_value;
2416 if (offset)
2417 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return mod->strtab + mod->symtab[best].st_name;
2419}
2420
Rusty Russell6dd06c92008-01-29 17:13:22 -05002421/* For kallsyms to ask for address resolution. NULL means not found. Careful
2422 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002423const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002424 unsigned long *size,
2425 unsigned long *offset,
2426 char **modname,
2427 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002430 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Rusty Russellcb2a5202008-01-14 00:55:03 -08002432 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002433 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (within(addr, mod->module_init, mod->init_size)
2435 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002436 if (modname)
2437 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002438 ret = get_ksymbol(mod, addr, size, offset);
2439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002442 /* Make a copy in here where it's safe */
2443 if (ret) {
2444 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2445 ret = namebuf;
2446 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002447 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002451int lookup_module_symbol_name(unsigned long addr, char *symname)
2452{
2453 struct module *mod;
2454
Rusty Russellcb2a5202008-01-14 00:55:03 -08002455 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002456 list_for_each_entry_rcu(mod, &modules, list) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002457 if (within(addr, mod->module_init, mod->init_size) ||
2458 within(addr, mod->module_core, mod->core_size)) {
2459 const char *sym;
2460
2461 sym = get_ksymbol(mod, addr, NULL, NULL);
2462 if (!sym)
2463 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002464 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002465 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002466 return 0;
2467 }
2468 }
2469out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002470 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002471 return -ERANGE;
2472}
2473
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002474int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2475 unsigned long *offset, char *modname, char *name)
2476{
2477 struct module *mod;
2478
Rusty Russellcb2a5202008-01-14 00:55:03 -08002479 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002480 list_for_each_entry_rcu(mod, &modules, list) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002481 if (within(addr, mod->module_init, mod->init_size) ||
2482 within(addr, mod->module_core, mod->core_size)) {
2483 const char *sym;
2484
2485 sym = get_ksymbol(mod, addr, size, offset);
2486 if (!sym)
2487 goto out;
2488 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002489 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002490 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002491 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002492 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002493 return 0;
2494 }
2495 }
2496out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002497 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002498 return -ERANGE;
2499}
2500
Alexey Dobriyanea078902007-05-08 00:28:39 -07002501int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2502 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 struct module *mod;
2505
Rusty Russellcb2a5202008-01-14 00:55:03 -08002506 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002507 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (symnum < mod->num_symtab) {
2509 *value = mod->symtab[symnum].st_value;
2510 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002511 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002512 KSYM_NAME_LEN);
2513 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Alexey Dobriyanea078902007-05-08 00:28:39 -07002514 *exported = is_exported(name, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002515 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518 symnum -= mod->num_symtab;
2519 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002520 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002521 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
2524static unsigned long mod_find_symname(struct module *mod, const char *name)
2525{
2526 unsigned int i;
2527
2528 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002529 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2530 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return mod->symtab[i].st_value;
2532 return 0;
2533}
2534
2535/* Look for this name: can be of form module:name. */
2536unsigned long module_kallsyms_lookup_name(const char *name)
2537{
2538 struct module *mod;
2539 char *colon;
2540 unsigned long ret = 0;
2541
2542 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002543 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if ((colon = strchr(name, ':')) != NULL) {
2545 *colon = '\0';
2546 if ((mod = find_module(name)) != NULL)
2547 ret = mod_find_symname(mod, colon+1);
2548 *colon = ':';
2549 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002550 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if ((ret = mod_find_symname(mod, name)) != 0)
2552 break;
2553 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002554 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return ret;
2556}
2557#endif /* CONFIG_KALLSYMS */
2558
2559/* Called by the /proc file system to return a list of modules. */
2560static void *m_start(struct seq_file *m, loff_t *pos)
2561{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002562 mutex_lock(&module_mutex);
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002563 return seq_list_start(&modules, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2567{
Pavel Emelianov708f4b52007-07-15 23:39:54 -07002568 return seq_list_next(p, &modules, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
2571static void m_stop(struct seq_file *m, void *p)
2572{
Ashutosh Naik6389a382006-03-23 03:00:46 -08002573 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002576static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002577{
2578 int bx = 0;
2579
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002580 if (mod->taints ||
2581 mod->state == MODULE_STATE_GOING ||
2582 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002583 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002584 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002585 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002586 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002587 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002588 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002589 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002590 /*
2591 * TAINT_FORCED_RMMOD: could be added.
2592 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2593 * apply to modules.
2594 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002595
2596 /* Show a - for module-is-being-unloaded */
2597 if (mod->state == MODULE_STATE_GOING)
2598 buf[bx++] = '-';
2599 /* Show a + for module-is-being-loaded */
2600 if (mod->state == MODULE_STATE_COMING)
2601 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002602 buf[bx++] = ')';
2603 }
2604 buf[bx] = '\0';
2605
2606 return buf;
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609static int m_show(struct seq_file *m, void *p)
2610{
2611 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002612 char buf[8];
2613
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002614 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 mod->name, mod->init_size + mod->core_size);
2616 print_unload_info(m, mod);
2617
2618 /* Informative for users. */
2619 seq_printf(m, " %s",
2620 mod->state == MODULE_STATE_GOING ? "Unloading":
2621 mod->state == MODULE_STATE_COMING ? "Loading":
2622 "Live");
2623 /* Used by oprofile and other similar tools. */
2624 seq_printf(m, " 0x%p", mod->module_core);
2625
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002626 /* Taints info */
2627 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002628 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 seq_printf(m, "\n");
2631 return 0;
2632}
2633
2634/* Format: modulename size refcount deps address
2635
2636 Where refcount is a number or -, and deps is a comma-separated list
2637 of depends or -.
2638*/
Helge Deller15ad7cd2006-12-06 20:40:36 -08002639const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .start = m_start,
2641 .next = m_next,
2642 .stop = m_stop,
2643 .show = m_show
2644};
2645
2646/* Given an address, look for it in the module exception tables. */
2647const struct exception_table_entry *search_module_extables(unsigned long addr)
2648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 const struct exception_table_entry *e = NULL;
2650 struct module *mod;
2651
Rusty Russell24da1cb2007-07-15 23:41:46 -07002652 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002653 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (mod->num_exentries == 0)
2655 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 e = search_extable(mod->extable,
2658 mod->extable + mod->num_exentries - 1,
2659 addr);
2660 if (e)
2661 break;
2662 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002663 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002666 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return e;
2668}
2669
Ingo Molnar4d435f92006-07-03 00:24:24 -07002670/*
2671 * Is this a valid module address?
2672 */
2673int is_module_address(unsigned long addr)
2674{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002675 struct module *mod;
2676
Rusty Russell24da1cb2007-07-15 23:41:46 -07002677 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002678
Andi Kleend72b3752008-08-30 10:09:00 +02002679 list_for_each_entry_rcu(mod, &modules, list) {
Ingo Molnar4d435f92006-07-03 00:24:24 -07002680 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002681 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002682 return 1;
2683 }
2684 }
2685
Rusty Russell24da1cb2007-07-15 23:41:46 -07002686 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002687
2688 return 0;
2689}
2690
2691
Rusty Russell24da1cb2007-07-15 23:41:46 -07002692/* Is this a valid kernel address? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693struct module *__module_text_address(unsigned long addr)
2694{
2695 struct module *mod;
2696
Rusty Russell3a642e92008-07-22 19:24:28 -05002697 if (addr < module_addr_min || addr > module_addr_max)
2698 return NULL;
2699
Andi Kleend72b3752008-08-30 10:09:00 +02002700 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (within(addr, mod->module_init, mod->init_text_size)
2702 || within(addr, mod->module_core, mod->core_text_size))
2703 return mod;
2704 return NULL;
2705}
2706
2707struct module *module_text_address(unsigned long addr)
2708{
2709 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Rusty Russell24da1cb2007-07-15 23:41:46 -07002711 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002713 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 return mod;
2716}
2717
2718/* Don't grab lock, we're oopsing. */
2719void print_modules(void)
2720{
2721 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002722 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002725 /* Most callers should already have preempt disabled, but make sure */
2726 preempt_disable();
2727 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002728 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002729 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002730 if (last_unloaded_module[0])
2731 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 printk("\n");
2733}
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735#ifdef CONFIG_MODVERSIONS
2736/* Generate the signature for struct module here, too, for modversions. */
2737void struct_module(struct module *mod) { return; }
2738EXPORT_SYMBOL(struct_module);
2739#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002740
2741#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002742void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002743{
2744 struct module *mod;
2745
2746 mutex_lock(&module_mutex);
2747 list_for_each_entry(mod, &modules, list)
2748 if (!mod->taints)
2749 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002750 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002751 mutex_unlock(&module_mutex);
2752}
2753#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002754
2755#ifdef CONFIG_TRACEPOINTS
2756void module_update_tracepoints(void)
2757{
2758 struct module *mod;
2759
2760 mutex_lock(&module_mutex);
2761 list_for_each_entry(mod, &modules, list)
2762 if (!mod->taints)
2763 tracepoint_update_probe_range(mod->tracepoints,
2764 mod->tracepoints + mod->num_tracepoints);
2765 mutex_unlock(&module_mutex);
2766}
2767
2768/*
2769 * Returns 0 if current not found.
2770 * Returns 1 if current found.
2771 */
2772int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2773{
2774 struct module *iter_mod;
2775 int found = 0;
2776
2777 mutex_lock(&module_mutex);
2778 list_for_each_entry(iter_mod, &modules, list) {
2779 if (!iter_mod->taints) {
2780 /*
2781 * Sorted module list
2782 */
2783 if (iter_mod < iter->module)
2784 continue;
2785 else if (iter_mod > iter->module)
2786 iter->tracepoint = NULL;
2787 found = tracepoint_get_iter_range(&iter->tracepoint,
2788 iter_mod->tracepoints,
2789 iter_mod->tracepoints
2790 + iter_mod->num_tracepoints);
2791 if (found) {
2792 iter->module = iter_mod;
2793 break;
2794 }
2795 }
2796 }
2797 mutex_unlock(&module_mutex);
2798 return found;
2799}
2800#endif