blob: f47cce910f25aab3d346f680e6af35d599dd1da7 [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>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040023#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070024#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070025#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040029#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/seq_file.h>
31#include <linux/syscalls.h>
32#include <linux/fcntl.h>
33#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080034#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/cpu.h>
36#include <linux/moduleparam.h>
37#include <linux/errno.h>
38#include <linux/err.h>
39#include <linux/vermagic.h>
40#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040041#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/stop_machine.h>
43#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070044#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080045#include <linux/mutex.h>
Jan Beulich4552d5d2006-06-26 13:57:28 +020046#include <linux/unwind.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020050#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080051#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040052#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040053#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#if 0
56#define DEBUGP printk
57#else
58#define DEBUGP(fmt , a...)
59#endif
60
61#ifndef ARCH_SHF_SMALL
62#define ARCH_SHF_SMALL 0
63#endif
64
65/* If this is set, the section belongs in the init part of the module */
66#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
67
Rusty Russell24da1cb2007-07-15 23:41:46 -070068/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020069 * (delete uses stop_machine/add uses RCU list operations). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080070static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static LIST_HEAD(modules);
72
Rusty Russellc9a3ba52008-01-29 17:13:18 -050073/* Waiting for a module to finish initializing? */
74static DECLARE_WAIT_QUEUE_HEAD(module_wq);
75
Alan Sterne041c682006-03-27 01:16:30 -080076static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Rusty Russell3a642e92008-07-22 19:24:28 -050078/* Bounds of module allocation, for speeding __module_text_address */
79static unsigned long module_addr_min = -1UL, module_addr_max = 0;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int register_module_notifier(struct notifier_block * nb)
82{
Alan Sterne041c682006-03-27 01:16:30 -080083 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85EXPORT_SYMBOL(register_module_notifier);
86
87int unregister_module_notifier(struct notifier_block * nb)
88{
Alan Sterne041c682006-03-27 01:16:30 -080089 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91EXPORT_SYMBOL(unregister_module_notifier);
92
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080093/* We require a truly strong try_module_get(): 0 means failure due to
94 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static inline int strong_try_module_get(struct module *mod)
96{
97 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050098 return -EBUSY;
99 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500101 else
102 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700105static inline void add_taint_module(struct module *mod, unsigned flag)
106{
107 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700108 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700109}
110
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200111/*
112 * A thread that wants to hold a reference to a module only while it
113 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115void __module_put_and_exit(struct module *mod, long code)
116{
117 module_put(mod);
118 do_exit(code);
119}
120EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Find a module section: 0 means not found. */
123static unsigned int find_sec(Elf_Ehdr *hdr,
124 Elf_Shdr *sechdrs,
125 const char *secstrings,
126 const char *name)
127{
128 unsigned int i;
129
130 for (i = 1; i < hdr->e_shnum; i++)
131 /* Alloc bit cleared means "ignore it." */
132 if ((sechdrs[i].sh_flags & SHF_ALLOC)
133 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
134 return i;
135 return 0;
136}
137
Rusty Russell5e458cc2008-10-22 10:00:13 -0500138/* Find a module section, or NULL. */
139static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
140 const char *secstrings, const char *name)
141{
142 /* Section 0 has sh_addr 0. */
143 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
144}
145
146/* Find a module section, or NULL. Fill in number of "objects" in section. */
147static void *section_objs(Elf_Ehdr *hdr,
148 Elf_Shdr *sechdrs,
149 const char *secstrings,
150 const char *name,
151 size_t object_size,
152 unsigned int *num)
153{
154 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
155
156 /* Section 0 has sh_addr 0 and sh_size 0. */
157 *num = sechdrs[sec].sh_size / object_size;
158 return (void *)sechdrs[sec].sh_addr;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* Provided by the linker */
162extern const struct kernel_symbol __start___ksymtab[];
163extern const struct kernel_symbol __stop___ksymtab[];
164extern const struct kernel_symbol __start___ksymtab_gpl[];
165extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800166extern const struct kernel_symbol __start___ksymtab_gpl_future[];
167extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700168extern const struct kernel_symbol __start___ksymtab_gpl_future[];
169extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170extern const unsigned long __start___kcrctab[];
171extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800172extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500173#ifdef CONFIG_UNUSED_SYMBOLS
174extern const struct kernel_symbol __start___ksymtab_unused[];
175extern const struct kernel_symbol __stop___ksymtab_unused[];
176extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
177extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700178extern const unsigned long __start___kcrctab_unused[];
179extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182#ifndef CONFIG_MODVERSIONS
183#define symversion(base, idx) NULL
184#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800185#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
187
Rusty Russelldafd0942008-07-22 19:24:25 -0500188struct symsearch {
189 const struct kernel_symbol *start, *stop;
190 const unsigned long *crcs;
191 enum {
192 NOT_GPL_ONLY,
193 GPL_ONLY,
194 WILL_BE_GPL_ONLY,
195 } licence;
196 bool unused;
197};
198
199static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize,
201 struct module *owner,
202 bool (*fn)(const struct symsearch *syms,
203 struct module *owner,
204 unsigned int symnum, void *data),
205 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100206{
Rusty Russelldafd0942008-07-22 19:24:25 -0500207 unsigned int i, j;
208
209 for (j = 0; j < arrsize; j++) {
210 for (i = 0; i < arr[j].stop - arr[j].start; i++)
211 if (fn(&arr[j], owner, i, data))
212 return true;
213 }
214
215 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100216}
217
Rusty Russelldafd0942008-07-22 19:24:25 -0500218/* Returns true as soon as fn returns true, otherwise false. */
219static bool each_symbol(bool (*fn)(const struct symsearch *arr,
220 struct module *owner,
221 unsigned int symnum, void *data),
222 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700223{
Rusty Russelldafd0942008-07-22 19:24:25 -0500224 struct module *mod;
225 const struct symsearch arr[] = {
226 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
227 NOT_GPL_ONLY, false },
228 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
229 __start___kcrctab_gpl,
230 GPL_ONLY, false },
231 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
232 __start___kcrctab_gpl_future,
233 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500234#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500235 { __start___ksymtab_unused, __stop___ksymtab_unused,
236 __start___kcrctab_unused,
237 NOT_GPL_ONLY, true },
238 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
239 __start___kcrctab_unused_gpl,
240 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500241#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 };
243
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
245 return true;
246
Andi Kleend72b3752008-08-30 10:09:00 +0200247 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500248 struct symsearch arr[] = {
249 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
250 NOT_GPL_ONLY, false },
251 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
252 mod->gpl_crcs,
253 GPL_ONLY, false },
254 { mod->gpl_future_syms,
255 mod->gpl_future_syms + mod->num_gpl_future_syms,
256 mod->gpl_future_crcs,
257 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500258#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500259 { mod->unused_syms,
260 mod->unused_syms + mod->num_unused_syms,
261 mod->unused_crcs,
262 NOT_GPL_ONLY, true },
263 { mod->unused_gpl_syms,
264 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
265 mod->unused_gpl_crcs,
266 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500267#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500268 };
269
270 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
271 return true;
272 }
273 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700274}
275
Rusty Russelldafd0942008-07-22 19:24:25 -0500276struct find_symbol_arg {
277 /* Input */
278 const char *name;
279 bool gplok;
280 bool warn;
281
282 /* Output */
283 struct module *owner;
284 const unsigned long *crc;
285 unsigned long value;
286};
287
288static bool find_symbol_in_section(const struct symsearch *syms,
289 struct module *owner,
290 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500291{
Rusty Russelldafd0942008-07-22 19:24:25 -0500292 struct find_symbol_arg *fsa = data;
293
294 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
295 return false;
296
297 if (!fsa->gplok) {
298 if (syms->licence == GPL_ONLY)
299 return false;
300 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
301 printk(KERN_WARNING "Symbol %s is being used "
302 "by a non-GPL module, which will not "
303 "be allowed in the future\n", fsa->name);
304 printk(KERN_WARNING "Please see the file "
305 "Documentation/feature-removal-schedule.txt "
306 "in the kernel source tree for more details.\n");
307 }
308 }
309
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500310#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500311 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500312 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500313 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500314 printk(KERN_WARNING
315 "This symbol will go away in the future.\n");
316 printk(KERN_WARNING
317 "Please evalute if this is the right api to use and if "
318 "it really is, submit a report the linux kernel "
319 "mailinglist together with submitting your code for "
320 "inclusion.\n");
321 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500322#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500323
324 fsa->owner = owner;
325 fsa->crc = symversion(syms->crcs, symnum);
326 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500327 return true;
328}
329
Rusty Russellad9546c2008-05-01 21:14:59 -0500330/* Find a symbol, return value, (optional) crc and (optional) module
331 * which owns it */
332static unsigned long find_symbol(const char *name,
333 struct module **owner,
334 const unsigned long **crc,
335 bool gplok,
336 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Rusty Russelldafd0942008-07-22 19:24:25 -0500338 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Rusty Russelldafd0942008-07-22 19:24:25 -0500340 fsa.name = name;
341 fsa.gplok = gplok;
342 fsa.warn = warn;
343
344 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500345 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500346 *owner = fsa.owner;
347 if (crc)
348 *crc = fsa.crc;
349 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800353 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* Search for module by name: must hold module_mutex. */
357static struct module *find_module(const char *name)
358{
359 struct module *mod;
360
361 list_for_each_entry(mod, &modules, list) {
362 if (strcmp(mod->name, name) == 0)
363 return mod;
364 }
365 return NULL;
366}
367
368#ifdef CONFIG_SMP
369/* Number of blocks used and allocated. */
370static unsigned int pcpu_num_used, pcpu_num_allocated;
371/* Size of each block. -ve means used. */
372static int *pcpu_size;
373
374static int split_block(unsigned int i, unsigned short size)
375{
376 /* Reallocation required? */
377 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700378 int *new;
379
380 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
381 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!new)
383 return 0;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pcpu_size = new;
387 }
388
389 /* Insert a new subblock */
390 memmove(&pcpu_size[i+1], &pcpu_size[i],
391 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
392 pcpu_num_used++;
393
394 pcpu_size[i+1] -= size;
395 pcpu_size[i] = size;
396 return 1;
397}
398
399static inline unsigned int block_size(int val)
400{
401 if (val < 0)
402 return -val;
403 return val;
404}
405
Rusty Russell842bbaa2005-08-01 21:11:47 -0700406static void *percpu_modalloc(unsigned long size, unsigned long align,
407 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 unsigned long extra;
410 unsigned int i;
411 void *ptr;
412
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200413 if (align > PAGE_SIZE) {
414 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
415 name, align, PAGE_SIZE);
416 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 ptr = __per_cpu_start;
420 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
421 /* Extra for alignment requirement. */
422 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
423 BUG_ON(i == 0 && extra != 0);
424
425 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
426 continue;
427
428 /* Transfer extra to previous block. */
429 if (pcpu_size[i-1] < 0)
430 pcpu_size[i-1] -= extra;
431 else
432 pcpu_size[i-1] += extra;
433 pcpu_size[i] -= extra;
434 ptr += extra;
435
436 /* Split block if warranted */
437 if (pcpu_size[i] - size > sizeof(unsigned long))
438 if (!split_block(i, size))
439 return NULL;
440
441 /* Mark allocated */
442 pcpu_size[i] = -pcpu_size[i];
443 return ptr;
444 }
445
446 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
447 size);
448 return NULL;
449}
450
451static void percpu_modfree(void *freeme)
452{
453 unsigned int i;
454 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
455
456 /* First entry is core kernel percpu data. */
457 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
458 if (ptr == freeme) {
459 pcpu_size[i] = -pcpu_size[i];
460 goto free;
461 }
462 }
463 BUG();
464
465 free:
466 /* Merge with previous? */
467 if (pcpu_size[i-1] >= 0) {
468 pcpu_size[i-1] += pcpu_size[i];
469 pcpu_num_used--;
470 memmove(&pcpu_size[i], &pcpu_size[i+1],
471 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
472 i--;
473 }
474 /* Merge with next? */
475 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
476 pcpu_size[i] += pcpu_size[i+1];
477 pcpu_num_used--;
478 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
479 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
480 }
481}
482
483static unsigned int find_pcpusec(Elf_Ehdr *hdr,
484 Elf_Shdr *sechdrs,
485 const char *secstrings)
486{
487 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
488}
489
Mike Travisdd5af902008-01-30 13:33:32 +0100490static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
491{
492 int cpu;
493
494 for_each_possible_cpu(cpu)
495 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int percpu_modinit(void)
499{
500 pcpu_num_used = 2;
501 pcpu_num_allocated = 2;
502 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
503 GFP_KERNEL);
504 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +0200505 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Free room. */
507 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
508 if (pcpu_size[1] < 0) {
509 printk(KERN_ERR "No per-cpu room for modules.\n");
510 pcpu_num_used = 1;
511 }
512
513 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515__initcall(percpu_modinit);
516#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700517static inline void *percpu_modalloc(unsigned long size, unsigned long align,
518 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 return NULL;
521}
522static inline void percpu_modfree(void *pcpuptr)
523{
524 BUG();
525}
526static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
527 Elf_Shdr *sechdrs,
528 const char *secstrings)
529{
530 return 0;
531}
532static inline void percpu_modcopy(void *pcpudst, const void *src,
533 unsigned long size)
534{
535 /* pcpusec should be 0, and size of that section should be 0. */
536 BUG_ON(size != 0);
537}
538#endif /* CONFIG_SMP */
539
Matt Domschc988d2b2005-06-23 22:05:15 -0700540#define MODINFO_ATTR(field) \
541static void setup_modinfo_##field(struct module *mod, const char *s) \
542{ \
543 mod->field = kstrdup(s, GFP_KERNEL); \
544} \
545static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
546 struct module *mod, char *buffer) \
547{ \
548 return sprintf(buffer, "%s\n", mod->field); \
549} \
550static int modinfo_##field##_exists(struct module *mod) \
551{ \
552 return mod->field != NULL; \
553} \
554static void free_modinfo_##field(struct module *mod) \
555{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700556 kfree(mod->field); \
557 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700558} \
559static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900560 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700561 .show = show_modinfo_##field, \
562 .setup = setup_modinfo_##field, \
563 .test = modinfo_##field##_exists, \
564 .free = free_modinfo_##field, \
565};
566
567MODINFO_ATTR(version);
568MODINFO_ATTR(srcversion);
569
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100570static char last_unloaded_module[MODULE_NAME_LEN+1];
571
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800572#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* Init the unload section of the module. */
574static void module_unload_init(struct module *mod)
575{
576 unsigned int i;
577
578 INIT_LIST_HEAD(&mod->modules_which_use_me);
579 for (i = 0; i < NR_CPUS; i++)
580 local_set(&mod->ref[i].count, 0);
581 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700582 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Backwards compatibility macros put refcount during init. */
584 mod->waiter = current;
585}
586
587/* modules using other modules */
588struct module_use
589{
590 struct list_head list;
591 struct module *module_which_uses;
592};
593
594/* Does a already use b? */
595static int already_uses(struct module *a, struct module *b)
596{
597 struct module_use *use;
598
599 list_for_each_entry(use, &b->modules_which_use_me, list) {
600 if (use->module_which_uses == a) {
601 DEBUGP("%s uses %s!\n", a->name, b->name);
602 return 1;
603 }
604 }
605 DEBUGP("%s does not use %s!\n", a->name, b->name);
606 return 0;
607}
608
609/* Module a uses b */
610static int use_module(struct module *a, struct module *b)
611{
612 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500613 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (b == NULL || already_uses(a, b)) return 1;
616
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500617 /* If we're interrupted or time out, we fail. */
618 if (wait_event_interruptible_timeout(
619 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
620 30 * HZ) <= 0) {
621 printk("%s: gave up waiting for init of module %s.\n",
622 a->name, b->name);
623 return 0;
624 }
625
626 /* If strong_try_module_get() returned a different error, we fail. */
627 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629
630 DEBUGP("Allocating new usage for %s.\n", a->name);
631 use = kmalloc(sizeof(*use), GFP_ATOMIC);
632 if (!use) {
633 printk("%s: out of memory loading\n", a->name);
634 module_put(b);
635 return 0;
636 }
637
638 use->module_which_uses = a;
639 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100640 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 1;
642}
643
644/* Clear the unload stuff of the module. */
645static void module_unload_free(struct module *mod)
646{
647 struct module *i;
648
649 list_for_each_entry(i, &modules, list) {
650 struct module_use *use;
651
652 list_for_each_entry(use, &i->modules_which_use_me, list) {
653 if (use->module_which_uses == mod) {
654 DEBUGP("%s unusing %s\n", mod->name, i->name);
655 module_put(i);
656 list_del(&use->list);
657 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100658 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* There can be at most one match. */
660 break;
661 }
662 }
663 }
664}
665
666#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800667static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 int ret = (flags & O_TRUNC);
670 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800671 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return ret;
673}
674#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800675static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 return 0;
678}
679#endif /* CONFIG_MODULE_FORCE_UNLOAD */
680
681struct stopref
682{
683 struct module *mod;
684 int flags;
685 int *forced;
686};
687
688/* Whole machine is stopped with interrupts off when this runs. */
689static int __try_stop_module(void *_sref)
690{
691 struct stopref *sref = _sref;
692
Rusty Russellda39ba52008-07-22 19:24:25 -0500693 /* If it's not unused, quit unless we're forcing. */
694 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800695 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EWOULDBLOCK;
697 }
698
699 /* Mark it as dying. */
700 sref->mod->state = MODULE_STATE_GOING;
701 return 0;
702}
703
704static int try_stop_module(struct module *mod, int flags, int *forced)
705{
Rusty Russellda39ba52008-07-22 19:24:25 -0500706 if (flags & O_NONBLOCK) {
707 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500709 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500710 } else {
711 /* We don't need to stop the machine for this. */
712 mod->state = MODULE_STATE_GOING;
713 synchronize_sched();
714 return 0;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718unsigned int module_refcount(struct module *mod)
719{
720 unsigned int i, total = 0;
721
722 for (i = 0; i < NR_CPUS; i++)
723 total += local_read(&mod->ref[i].count);
724 return total;
725}
726EXPORT_SYMBOL(module_refcount);
727
728/* This exists whether we can unload or not */
729static void free_module(struct module *mod);
730
731static void wait_for_zero_refcount(struct module *mod)
732{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500733 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800734 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 for (;;) {
736 DEBUGP("Looking at refcount...\n");
737 set_current_state(TASK_UNINTERRUPTIBLE);
738 if (module_refcount(mod) == 0)
739 break;
740 schedule();
741 }
742 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800743 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800746asmlinkage long
747sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800750 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int ret, forced = 0;
752
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800753 if (!capable(CAP_SYS_MODULE))
754 return -EPERM;
755
756 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
757 return -EFAULT;
758 name[MODULE_NAME_LEN-1] = '\0';
759
Heiko Carstens9e018922008-12-22 12:36:31 +0100760 /* Create stop_machine threads since free_module relies on
761 * a non-failing stop_machine call. */
762 ret = stop_machine_create();
763 if (ret)
764 return ret;
765
766 if (mutex_lock_interruptible(&module_mutex) != 0) {
767 ret = -EINTR;
768 goto out_stop;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 mod = find_module(name);
772 if (!mod) {
773 ret = -ENOENT;
774 goto out;
775 }
776
777 if (!list_empty(&mod->modules_which_use_me)) {
778 /* Other modules depend on us: get rid of them first. */
779 ret = -EWOULDBLOCK;
780 goto out;
781 }
782
783 /* Doing init or already dying? */
784 if (mod->state != MODULE_STATE_LIVE) {
785 /* FIXME: if (force), slam module count and wake up
786 waiter --RR */
787 DEBUGP("%s already dying\n", mod->name);
788 ret = -EBUSY;
789 goto out;
790 }
791
792 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700793 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800794 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!forced) {
796 /* This module can't be removed */
797 ret = -EBUSY;
798 goto out;
799 }
800 }
801
802 /* Set this up before setting mod->state */
803 mod->waiter = current;
804
805 /* Stop the machine so refcounts can't move and disable module. */
806 ret = try_stop_module(mod, flags, &forced);
807 if (ret != 0)
808 goto out;
809
810 /* Never wait if forced. */
811 if (!forced && module_refcount(mod) != 0)
812 wait_for_zero_refcount(mod);
813
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200814 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200816 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200818 blocking_notifier_call_chain(&module_notify_list,
819 MODULE_STATE_GOING, mod);
820 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100821 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500822 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400823 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 free_module(mod);
825
826 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800827 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100828out_stop:
829 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return ret;
831}
832
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800833static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct module_use *use;
836 int printed_something = 0;
837
838 seq_printf(m, " %u ", module_refcount(mod));
839
840 /* Always include a trailing , so userspace can differentiate
841 between this and the old multi-field proc format. */
842 list_for_each_entry(use, &mod->modules_which_use_me, list) {
843 printed_something = 1;
844 seq_printf(m, "%s,", use->module_which_uses->name);
845 }
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (mod->init != NULL && mod->exit == NULL) {
848 printed_something = 1;
849 seq_printf(m, "[permanent],");
850 }
851
852 if (!printed_something)
853 seq_printf(m, "-");
854}
855
856void __symbol_put(const char *symbol)
857{
858 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Rusty Russell24da1cb2007-07-15 23:41:46 -0700860 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500861 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 BUG();
863 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700864 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866EXPORT_SYMBOL(__symbol_put);
867
868void symbol_put_addr(void *addr)
869{
Trent Piepho5e376612006-05-15 09:44:06 -0700870 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Trent Piepho5e376612006-05-15 09:44:06 -0700872 if (core_kernel_text((unsigned long)addr))
873 return;
874
875 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700877 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879EXPORT_SYMBOL_GPL(symbol_put_addr);
880
881static ssize_t show_refcnt(struct module_attribute *mattr,
882 struct module *mod, char *buffer)
883{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400884 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900888 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 .show = show_refcnt,
890};
891
Al Virof6a57032006-10-18 01:47:25 -0400892void module_put(struct module *module)
893{
894 if (module) {
895 unsigned int cpu = get_cpu();
896 local_dec(&module->ref[cpu].count);
897 /* Maybe they're waiting for us to drop reference? */
898 if (unlikely(!module_is_live(module)))
899 wake_up_process(module->waiter);
900 put_cpu();
901 }
902}
903EXPORT_SYMBOL(module_put);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800906static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 /* We don't know the usage count, or what modules are using. */
909 seq_printf(m, " - -");
910}
911
912static inline void module_unload_free(struct module *mod)
913{
914}
915
916static inline int use_module(struct module *a, struct module *b)
917{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500918 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921static inline void module_unload_init(struct module *mod)
922{
923}
924#endif /* CONFIG_MODULE_UNLOAD */
925
Kay Sievers1f717402006-11-24 12:15:25 +0100926static ssize_t show_initstate(struct module_attribute *mattr,
927 struct module *mod, char *buffer)
928{
929 const char *state = "unknown";
930
931 switch (mod->state) {
932 case MODULE_STATE_LIVE:
933 state = "live";
934 break;
935 case MODULE_STATE_COMING:
936 state = "coming";
937 break;
938 case MODULE_STATE_GOING:
939 state = "going";
940 break;
941 }
942 return sprintf(buffer, "%s\n", state);
943}
944
945static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900946 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100947 .show = show_initstate,
948};
949
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800950static struct module_attribute *modinfo_attrs[] = {
951 &modinfo_version,
952 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100953 &initstate,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800954#ifdef CONFIG_MODULE_UNLOAD
955 &refcnt,
956#endif
957 NULL,
958};
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960static const char vermagic[] = VERMAGIC_STRING;
961
Linus Torvalds826e4502008-05-04 17:04:16 -0700962static int try_to_force_load(struct module *mod, const char *symname)
963{
964#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700965 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700966 printk("%s: no version for \"%s\" found: kernel tainted.\n",
967 mod->name, symname);
968 add_taint_module(mod, TAINT_FORCED_MODULE);
969 return 0;
970#else
971 return -ENOEXEC;
972#endif
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifdef CONFIG_MODVERSIONS
976static int check_version(Elf_Shdr *sechdrs,
977 unsigned int versindex,
978 const char *symname,
979 struct module *mod,
980 const unsigned long *crc)
981{
982 unsigned int i, num_versions;
983 struct modversion_info *versions;
984
985 /* Exporting module didn't supply crcs? OK, we're already tainted. */
986 if (!crc)
987 return 1;
988
Rusty Russella5dd6972008-05-09 16:24:21 +1000989 /* No versions at all? modprobe --force does this. */
990 if (versindex == 0)
991 return try_to_force_load(mod, symname) == 0;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 versions = (void *) sechdrs[versindex].sh_addr;
994 num_versions = sechdrs[versindex].sh_size
995 / sizeof(struct modversion_info);
996
997 for (i = 0; i < num_versions; i++) {
998 if (strcmp(versions[i].name, symname) != 0)
999 continue;
1000
1001 if (versions[i].crc == *crc)
1002 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 DEBUGP("Found checksum %lX vs module %lX\n",
1004 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001005 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001007
Rusty Russella5dd6972008-05-09 16:24:21 +10001008 printk(KERN_WARNING "%s: no symbol version for %s\n",
1009 mod->name, symname);
1010 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001011
1012bad_version:
1013 printk("%s: disagrees about version of symbol %s\n",
1014 mod->name, symname);
1015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1019 unsigned int versindex,
1020 struct module *mod)
1021{
1022 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Rusty Russellad9546c2008-05-01 21:14:59 -05001024 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001026 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Rusty Russell91e37a72008-05-09 16:25:28 +10001029/* First part is kernel version, which we ignore if module has crcs. */
1030static inline int same_magic(const char *amagic, const char *bmagic,
1031 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Rusty Russell91e37a72008-05-09 16:25:28 +10001033 if (has_crcs) {
1034 amagic += strcspn(amagic, " ");
1035 bmagic += strcspn(bmagic, " ");
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return strcmp(amagic, bmagic) == 0;
1038}
1039#else
1040static inline int check_version(Elf_Shdr *sechdrs,
1041 unsigned int versindex,
1042 const char *symname,
1043 struct module *mod,
1044 const unsigned long *crc)
1045{
1046 return 1;
1047}
1048
1049static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1050 unsigned int versindex,
1051 struct module *mod)
1052{
1053 return 1;
1054}
1055
Rusty Russell91e37a72008-05-09 16:25:28 +10001056static inline int same_magic(const char *amagic, const char *bmagic,
1057 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 return strcmp(amagic, bmagic) == 0;
1060}
1061#endif /* CONFIG_MODVERSIONS */
1062
1063/* Resolve a symbol for this module. I.e. if we find one, record usage.
1064 Must be holding module_mutex. */
1065static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1066 unsigned int versindex,
1067 const char *name,
1068 struct module *mod)
1069{
1070 struct module *owner;
1071 unsigned long ret;
1072 const unsigned long *crc;
1073
Rusty Russellad9546c2008-05-01 21:14:59 -05001074 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001075 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001076 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001077 /* use_module can fail due to OOM,
1078 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1080 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001081 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return ret;
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/*
1087 * /sys/module/foo/sections stuff
1088 * J. Corbet <corbet@lwn.net>
1089 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001090#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001091struct module_sect_attr
1092{
1093 struct module_attribute mattr;
1094 char *name;
1095 unsigned long address;
1096};
1097
1098struct module_sect_attrs
1099{
1100 struct attribute_group grp;
1101 unsigned int nsections;
1102 struct module_sect_attr attrs[0];
1103};
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105static ssize_t module_sect_show(struct module_attribute *mattr,
1106 struct module *mod, char *buf)
1107{
1108 struct module_sect_attr *sattr =
1109 container_of(mattr, struct module_sect_attr, mattr);
1110 return sprintf(buf, "0x%lx\n", sattr->address);
1111}
1112
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001113static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1114{
Rusty Russella58730c2008-03-13 09:03:44 +00001115 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001116
1117 for (section = 0; section < sect_attrs->nsections; section++)
1118 kfree(sect_attrs->attrs[section].name);
1119 kfree(sect_attrs);
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static void add_sect_attrs(struct module *mod, unsigned int nsect,
1123 char *secstrings, Elf_Shdr *sechdrs)
1124{
1125 unsigned int nloaded = 0, i, size[2];
1126 struct module_sect_attrs *sect_attrs;
1127 struct module_sect_attr *sattr;
1128 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* Count loaded sections and allocate structures */
1131 for (i = 0; i < nsect; i++)
1132 if (sechdrs[i].sh_flags & SHF_ALLOC)
1133 nloaded++;
1134 size[0] = ALIGN(sizeof(*sect_attrs)
1135 + nloaded * sizeof(sect_attrs->attrs[0]),
1136 sizeof(sect_attrs->grp.attrs[0]));
1137 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001138 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1139 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return;
1141
1142 /* Setup section attributes. */
1143 sect_attrs->grp.name = "sections";
1144 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1145
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001146 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 sattr = &sect_attrs->attrs[0];
1148 gattr = &sect_attrs->grp.attrs[0];
1149 for (i = 0; i < nsect; i++) {
1150 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1151 continue;
1152 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001153 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1154 GFP_KERNEL);
1155 if (sattr->name == NULL)
1156 goto out;
1157 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 sattr->mattr.show = module_sect_show;
1159 sattr->mattr.store = NULL;
1160 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 sattr->mattr.attr.mode = S_IRUGO;
1162 *(gattr++) = &(sattr++)->mattr.attr;
1163 }
1164 *gattr = NULL;
1165
1166 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1167 goto out;
1168
1169 mod->sect_attrs = sect_attrs;
1170 return;
1171 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001172 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175static void remove_sect_attrs(struct module *mod)
1176{
1177 if (mod->sect_attrs) {
1178 sysfs_remove_group(&mod->mkobj.kobj,
1179 &mod->sect_attrs->grp);
1180 /* We are positive that no one is using any sect attrs
1181 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001182 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 mod->sect_attrs = NULL;
1184 }
1185}
1186
Roland McGrath6d760132007-10-16 23:26:40 -07001187/*
1188 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1189 */
1190
1191struct module_notes_attrs {
1192 struct kobject *dir;
1193 unsigned int notes;
1194 struct bin_attribute attrs[0];
1195};
1196
1197static ssize_t module_notes_read(struct kobject *kobj,
1198 struct bin_attribute *bin_attr,
1199 char *buf, loff_t pos, size_t count)
1200{
1201 /*
1202 * The caller checked the pos and count against our size.
1203 */
1204 memcpy(buf, bin_attr->private + pos, count);
1205 return count;
1206}
1207
1208static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1209 unsigned int i)
1210{
1211 if (notes_attrs->dir) {
1212 while (i-- > 0)
1213 sysfs_remove_bin_file(notes_attrs->dir,
1214 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001215 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001216 }
1217 kfree(notes_attrs);
1218}
1219
1220static void add_notes_attrs(struct module *mod, unsigned int nsect,
1221 char *secstrings, Elf_Shdr *sechdrs)
1222{
1223 unsigned int notes, loaded, i;
1224 struct module_notes_attrs *notes_attrs;
1225 struct bin_attribute *nattr;
1226
1227 /* Count notes sections and allocate structures. */
1228 notes = 0;
1229 for (i = 0; i < nsect; i++)
1230 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1231 (sechdrs[i].sh_type == SHT_NOTE))
1232 ++notes;
1233
1234 if (notes == 0)
1235 return;
1236
1237 notes_attrs = kzalloc(sizeof(*notes_attrs)
1238 + notes * sizeof(notes_attrs->attrs[0]),
1239 GFP_KERNEL);
1240 if (notes_attrs == NULL)
1241 return;
1242
1243 notes_attrs->notes = notes;
1244 nattr = &notes_attrs->attrs[0];
1245 for (loaded = i = 0; i < nsect; ++i) {
1246 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1247 continue;
1248 if (sechdrs[i].sh_type == SHT_NOTE) {
1249 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1250 nattr->attr.mode = S_IRUGO;
1251 nattr->size = sechdrs[i].sh_size;
1252 nattr->private = (void *) sechdrs[i].sh_addr;
1253 nattr->read = module_notes_read;
1254 ++nattr;
1255 }
1256 ++loaded;
1257 }
1258
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001259 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001260 if (!notes_attrs->dir)
1261 goto out;
1262
1263 for (i = 0; i < notes; ++i)
1264 if (sysfs_create_bin_file(notes_attrs->dir,
1265 &notes_attrs->attrs[i]))
1266 goto out;
1267
1268 mod->notes_attrs = notes_attrs;
1269 return;
1270
1271 out:
1272 free_notes_attrs(notes_attrs, i);
1273}
1274
1275static void remove_notes_attrs(struct module *mod)
1276{
1277 if (mod->notes_attrs)
1278 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1284 char *sectstrings, Elf_Shdr *sechdrs)
1285{
1286}
1287
1288static inline void remove_sect_attrs(struct module *mod)
1289{
1290}
Roland McGrath6d760132007-10-16 23:26:40 -07001291
1292static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1293 char *sectstrings, Elf_Shdr *sechdrs)
1294{
1295}
1296
1297static inline void remove_notes_attrs(struct module *mod)
1298{
1299}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Randy Dunlapef665c12007-02-13 15:19:06 -08001302#ifdef CONFIG_SYSFS
1303int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001304{
1305 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001306 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001307 int error = 0;
1308 int i;
1309
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001310 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1311 (ARRAY_SIZE(modinfo_attrs) + 1)),
1312 GFP_KERNEL);
1313 if (!mod->modinfo_attrs)
1314 return -ENOMEM;
1315
1316 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001317 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1318 if (!attr->test ||
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001319 (attr->test && attr->test(mod))) {
1320 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001321 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1322 ++temp_attr;
1323 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001324 }
1325 return error;
1326}
1327
Randy Dunlapef665c12007-02-13 15:19:06 -08001328void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001329{
1330 struct module_attribute *attr;
1331 int i;
1332
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001333 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1334 /* pick a field to test for end of list */
1335 if (!attr->attr.name)
1336 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001337 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001338 if (attr->free)
1339 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001340 }
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001341 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Randy Dunlapef665c12007-02-13 15:19:06 -08001344int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001347 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001349 if (!module_sysfs_initialized) {
1350 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001351 mod->name);
1352 err = -EINVAL;
1353 goto out;
1354 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001355
1356 kobj = kset_find_obj(module_kset, mod->name);
1357 if (kobj) {
1358 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1359 kobject_put(kobj);
1360 err = -EINVAL;
1361 goto out;
1362 }
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001365
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001366 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1367 mod->mkobj.kobj.kset = module_kset;
1368 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1369 "%s", mod->name);
1370 if (err)
1371 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001372
Kay Sievers97c146e2007-11-29 23:46:11 +01001373 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001374out:
1375 return err;
1376}
1377
Randy Dunlapef665c12007-02-13 15:19:06 -08001378int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001379 struct kernel_param *kparam,
1380 unsigned int num_params)
1381{
1382 int err;
1383
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001384 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001385 if (!mod->holders_dir) {
1386 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001387 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001388 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 err = module_param_sysfs_setup(mod, kparam, num_params);
1391 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001392 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Matt Domschc988d2b2005-06-23 22:05:15 -07001394 err = module_add_modinfo_attrs(mod);
1395 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001396 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001397
Kay Sieverse17e0f52006-11-24 12:15:25 +01001398 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
1400
Kay Sieverse17e0f52006-11-24 12:15:25 +01001401out_unreg_param:
1402 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001403out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001404 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001405out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001406 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return err;
1408}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001409
1410static void mod_sysfs_fini(struct module *mod)
1411{
1412 kobject_put(&mod->mkobj.kobj);
1413}
1414
1415#else /* CONFIG_SYSFS */
1416
1417static void mod_sysfs_fini(struct module *mod)
1418{
1419}
1420
1421#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423static void mod_kobject_remove(struct module *mod)
1424{
Matt Domschc988d2b2005-06-23 22:05:15 -07001425 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001427 kobject_put(mod->mkobj.drivers_dir);
1428 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001429 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432/*
1433 * unlink the module with the whole machine is stopped with interrupts off
1434 * - this defends against kallsyms not taking locks
1435 */
1436static int __unlink_module(void *_mod)
1437{
1438 struct module *mod = _mod;
1439 list_del(&mod->list);
1440 return 0;
1441}
1442
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001443/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static void free_module(struct module *mod)
1445{
1446 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001447 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001448 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 remove_sect_attrs(mod);
1450 mod_kobject_remove(mod);
1451
Jan Beulich4552d5d2006-06-26 13:57:28 +02001452 unwind_remove_table(mod->unwind_info, 0);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* Arch-specific cleanup. */
1455 module_arch_cleanup(mod);
1456
1457 /* Module unload stuff */
1458 module_unload_free(mod);
1459
Steven Rostedtfed19392008-08-14 22:47:19 -04001460 /* release any pointers to mcount in this module */
1461 ftrace_release(mod->module_core, mod->core_size);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* This may be NULL, but that's OK */
1464 module_free(mod, mod->module_init);
1465 kfree(mod->args);
1466 if (mod->percpu)
1467 percpu_modfree(mod->percpu);
1468
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001469 /* Free lock-classes: */
1470 lockdep_free_key_range(mod->module_core, mod->core_size);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Finally, free the core (containing the module structure) */
1473 module_free(mod, mod->module_core);
1474}
1475
1476void *__symbol_get(const char *symbol)
1477{
1478 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001479 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Rusty Russell24da1cb2007-07-15 23:41:46 -07001481 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001482 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001483 if (IS_ERR_VALUE(value))
1484 value = 0;
1485 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001487 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 return (void *)value;
1490}
1491EXPORT_SYMBOL_GPL(__symbol_get);
1492
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001493/*
1494 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001495 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001496 */
1497static int verify_export_symbols(struct module *mod)
1498{
Rusty Russellb2111042008-05-01 21:15:00 -05001499 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001500 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001501 const struct kernel_symbol *s;
1502 struct {
1503 const struct kernel_symbol *sym;
1504 unsigned int num;
1505 } arr[] = {
1506 { mod->syms, mod->num_syms },
1507 { mod->gpl_syms, mod->num_gpl_syms },
1508 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001509#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001510 { mod->unused_syms, mod->num_unused_syms },
1511 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001512#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001513 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001514
Rusty Russellb2111042008-05-01 21:15:00 -05001515 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1516 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1517 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1518 NULL, true, false))) {
1519 printk(KERN_ERR
1520 "%s: exports duplicate symbol %s"
1521 " (owned by %s)\n",
1522 mod->name, s->name, module_name(owner));
1523 return -ENOEXEC;
1524 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001525 }
Rusty Russellb2111042008-05-01 21:15:00 -05001526 }
1527 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001528}
1529
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001530/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static int simplify_symbols(Elf_Shdr *sechdrs,
1532 unsigned int symindex,
1533 const char *strtab,
1534 unsigned int versindex,
1535 unsigned int pcpuindex,
1536 struct module *mod)
1537{
1538 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1539 unsigned long secbase;
1540 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1541 int ret = 0;
1542
1543 for (i = 1; i < n; i++) {
1544 switch (sym[i].st_shndx) {
1545 case SHN_COMMON:
1546 /* We compiled with -fno-common. These are not
1547 supposed to happen. */
1548 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1549 printk("%s: please compile with -fno-common\n",
1550 mod->name);
1551 ret = -ENOEXEC;
1552 break;
1553
1554 case SHN_ABS:
1555 /* Don't need to do anything */
1556 DEBUGP("Absolute symbol: 0x%08lx\n",
1557 (long)sym[i].st_value);
1558 break;
1559
1560 case SHN_UNDEF:
1561 sym[i].st_value
1562 = resolve_symbol(sechdrs, versindex,
1563 strtab + sym[i].st_name, mod);
1564
1565 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001566 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 break;
1568 /* Ok if weak. */
1569 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1570 break;
1571
1572 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1573 mod->name, strtab + sym[i].st_name);
1574 ret = -ENOENT;
1575 break;
1576
1577 default:
1578 /* Divert to percpu allocation if a percpu var. */
1579 if (sym[i].st_shndx == pcpuindex)
1580 secbase = (unsigned long)mod->percpu;
1581 else
1582 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1583 sym[i].st_value += secbase;
1584 break;
1585 }
1586 }
1587
1588 return ret;
1589}
1590
Helge Deller088af9a2008-12-31 12:31:18 +01001591/* Additional bytes needed by arch in front of individual sections */
1592unsigned int __weak arch_mod_section_prepend(struct module *mod,
1593 unsigned int section)
1594{
1595 /* default implementation just returns zero */
1596 return 0;
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001600static long get_offset(struct module *mod, unsigned int *size,
1601 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 long ret;
1604
Helge Deller088af9a2008-12-31 12:31:18 +01001605 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1607 *size = ret + sechdr->sh_size;
1608 return ret;
1609}
1610
1611/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1612 might -- code, read-only data, read-write data, small data. Tally
1613 sizes, and place the offsets into sh_entsize fields: high bit means it
1614 belongs in init. */
1615static void layout_sections(struct module *mod,
1616 const Elf_Ehdr *hdr,
1617 Elf_Shdr *sechdrs,
1618 const char *secstrings)
1619{
1620 static unsigned long const masks[][2] = {
1621 /* NOTE: all executable code must be the first section
1622 * in this array; otherwise modify the text_size
1623 * finder in the two loops below */
1624 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1625 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1626 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1627 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1628 };
1629 unsigned int m, i;
1630
1631 for (i = 0; i < hdr->e_shnum; i++)
1632 sechdrs[i].sh_entsize = ~0UL;
1633
1634 DEBUGP("Core section allocation order:\n");
1635 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1636 for (i = 0; i < hdr->e_shnum; ++i) {
1637 Elf_Shdr *s = &sechdrs[i];
1638
1639 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1640 || (s->sh_flags & masks[m][1])
1641 || s->sh_entsize != ~0UL
1642 || strncmp(secstrings + s->sh_name,
1643 ".init", 5) == 0)
1644 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001645 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 DEBUGP("\t%s\n", secstrings + s->sh_name);
1647 }
1648 if (m == 0)
1649 mod->core_text_size = mod->core_size;
1650 }
1651
1652 DEBUGP("Init section allocation order:\n");
1653 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1654 for (i = 0; i < hdr->e_shnum; ++i) {
1655 Elf_Shdr *s = &sechdrs[i];
1656
1657 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1658 || (s->sh_flags & masks[m][1])
1659 || s->sh_entsize != ~0UL
1660 || strncmp(secstrings + s->sh_name,
1661 ".init", 5) != 0)
1662 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001663 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 | INIT_OFFSET_MASK);
1665 DEBUGP("\t%s\n", secstrings + s->sh_name);
1666 }
1667 if (m == 0)
1668 mod->init_text_size = mod->init_size;
1669 }
1670}
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672static void set_license(struct module *mod, const char *license)
1673{
1674 if (!license)
1675 license = "unspecified";
1676
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001677 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001678 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001679 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001680 "kernel.\n", mod->name, license);
1681 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683}
1684
1685/* Parse tag=value strings from .modinfo section */
1686static char *next_string(char *string, unsigned long *secsize)
1687{
1688 /* Skip non-zero chars */
1689 while (string[0]) {
1690 string++;
1691 if ((*secsize)-- <= 1)
1692 return NULL;
1693 }
1694
1695 /* Skip any zero padding. */
1696 while (!string[0]) {
1697 string++;
1698 if ((*secsize)-- <= 1)
1699 return NULL;
1700 }
1701 return string;
1702}
1703
1704static char *get_modinfo(Elf_Shdr *sechdrs,
1705 unsigned int info,
1706 const char *tag)
1707{
1708 char *p;
1709 unsigned int taglen = strlen(tag);
1710 unsigned long size = sechdrs[info].sh_size;
1711
1712 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1713 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1714 return p + taglen + 1;
1715 }
1716 return NULL;
1717}
1718
Matt Domschc988d2b2005-06-23 22:05:15 -07001719static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1720 unsigned int infoindex)
1721{
1722 struct module_attribute *attr;
1723 int i;
1724
1725 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1726 if (attr->setup)
1727 attr->setup(mod,
1728 get_modinfo(sechdrs,
1729 infoindex,
1730 attr->attr.name));
1731 }
1732}
Matt Domschc988d2b2005-06-23 22:05:15 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001735
1736/* lookup symbol in given range of kernel_symbols */
1737static const struct kernel_symbol *lookup_symbol(const char *name,
1738 const struct kernel_symbol *start,
1739 const struct kernel_symbol *stop)
1740{
1741 const struct kernel_symbol *ks = start;
1742 for (; ks < stop; ks++)
1743 if (strcmp(ks->name, name) == 0)
1744 return ks;
1745 return NULL;
1746}
1747
Tim Abbottca4787b2009-01-05 08:40:10 -06001748static int is_exported(const char *name, unsigned long value,
1749 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Tim Abbottca4787b2009-01-05 08:40:10 -06001751 const struct kernel_symbol *ks;
1752 if (!mod)
1753 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001754 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001755 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1756 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
1759/* As per nm */
1760static char elf_type(const Elf_Sym *sym,
1761 Elf_Shdr *sechdrs,
1762 const char *secstrings,
1763 struct module *mod)
1764{
1765 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1766 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1767 return 'v';
1768 else
1769 return 'w';
1770 }
1771 if (sym->st_shndx == SHN_UNDEF)
1772 return 'U';
1773 if (sym->st_shndx == SHN_ABS)
1774 return 'a';
1775 if (sym->st_shndx >= SHN_LORESERVE)
1776 return '?';
1777 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1778 return 't';
1779 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1780 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1781 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1782 return 'r';
1783 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1784 return 'g';
1785 else
1786 return 'd';
1787 }
1788 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1789 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1790 return 's';
1791 else
1792 return 'b';
1793 }
1794 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1795 ".debug", strlen(".debug")) == 0)
1796 return 'n';
1797 return '?';
1798}
1799
1800static void add_kallsyms(struct module *mod,
1801 Elf_Shdr *sechdrs,
1802 unsigned int symindex,
1803 unsigned int strindex,
1804 const char *secstrings)
1805{
1806 unsigned int i;
1807
1808 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1809 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1810 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1811
1812 /* Set types up while we still have access to sections. */
1813 for (i = 0; i < mod->num_symtab; i++)
1814 mod->symtab[i].st_info
1815 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1816}
1817#else
1818static inline void add_kallsyms(struct module *mod,
1819 Elf_Shdr *sechdrs,
1820 unsigned int symindex,
1821 unsigned int strindex,
1822 const char *secstrings)
1823{
1824}
1825#endif /* CONFIG_KALLSYMS */
1826
Rusty Russell5e458cc2008-10-22 10:00:13 -05001827static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1828{
Jason Baron346e15b2008-08-12 16:46:19 -04001829#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
Rusty Russell5e458cc2008-10-22 10:00:13 -05001830 unsigned int i;
Jason Baron346e15b2008-08-12 16:46:19 -04001831
Rusty Russell5e458cc2008-10-22 10:00:13 -05001832 for (i = 0; i < num; i++) {
1833 register_dynamic_debug_module(debug[i].modname,
1834 debug[i].type,
1835 debug[i].logical_modname,
1836 debug[i].flag_names,
1837 debug[i].hash, debug[i].hash2);
Jason Baron346e15b2008-08-12 16:46:19 -04001838 }
Jason Baron346e15b2008-08-12 16:46:19 -04001839#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001840}
Jason Baron346e15b2008-08-12 16:46:19 -04001841
Rusty Russell3a642e92008-07-22 19:24:28 -05001842static void *module_alloc_update_bounds(unsigned long size)
1843{
1844 void *ret = module_alloc(size);
1845
1846 if (ret) {
1847 /* Update module bounds. */
1848 if ((unsigned long)ret < module_addr_min)
1849 module_addr_min = (unsigned long)ret;
1850 if ((unsigned long)ret + size > module_addr_max)
1851 module_addr_max = (unsigned long)ret + size;
1852 }
1853 return ret;
1854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/* Allocate and load the module: note that size of section 0 is always
1857 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001858static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned long len,
1860 const char __user *uargs)
1861{
1862 Elf_Ehdr *hdr;
1863 Elf_Shdr *sechdrs;
1864 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001865 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001866 unsigned int i;
1867 unsigned int symindex = 0;
1868 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001869 unsigned int modindex, versindex, infoindex, pcpuindex;
Andrew Morton84860f92006-06-28 04:26:46 -07001870 unsigned int unwindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001871 unsigned int num_kp, num_mcount;
1872 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct module *mod;
1874 long err = 0;
1875 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001876 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001877 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1880 umod, len, uargs);
1881 if (len < sizeof(*hdr))
1882 return ERR_PTR(-ENOEXEC);
1883
1884 /* Suck in entire file: we'll want most of it. */
1885 /* vmalloc barfs on "unusual" numbers. Check here */
1886 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1887 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001888
1889 /* Create stop_machine threads since the error path relies on
1890 * a non-failing stop_machine call. */
1891 err = stop_machine_create();
1892 if (err)
1893 goto free_hdr;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (copy_from_user(hdr, umod, len) != 0) {
1896 err = -EFAULT;
1897 goto free_hdr;
1898 }
1899
1900 /* Sanity checks against insmoding binaries or wrong arch,
1901 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001902 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 || hdr->e_type != ET_REL
1904 || !elf_check_arch(hdr)
1905 || hdr->e_shentsize != sizeof(*sechdrs)) {
1906 err = -ENOEXEC;
1907 goto free_hdr;
1908 }
1909
1910 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1911 goto truncated;
1912
1913 /* Convenience variables */
1914 sechdrs = (void *)hdr + hdr->e_shoff;
1915 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1916 sechdrs[0].sh_addr = 0;
1917
1918 for (i = 1; i < hdr->e_shnum; i++) {
1919 if (sechdrs[i].sh_type != SHT_NOBITS
1920 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1921 goto truncated;
1922
1923 /* Mark all sections sh_addr with their address in the
1924 temporary image. */
1925 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1926
1927 /* Internal symbols and strings. */
1928 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1929 symindex = i;
1930 strindex = sechdrs[i].sh_link;
1931 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1932 }
1933#ifndef CONFIG_MODULE_UNLOAD
1934 /* Don't load .exit sections */
1935 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1936 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1937#endif
1938 }
1939
1940 modindex = find_sec(hdr, sechdrs, secstrings,
1941 ".gnu.linkonce.this_module");
1942 if (!modindex) {
1943 printk(KERN_WARNING "No module found in object\n");
1944 err = -ENOEXEC;
1945 goto free_hdr;
1946 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001947 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 mod = (void *)sechdrs[modindex].sh_addr;
1949
1950 if (symindex == 0) {
1951 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1952 mod->name);
1953 err = -ENOEXEC;
1954 goto free_hdr;
1955 }
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1958 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1959 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
Jan Beulich4552d5d2006-06-26 13:57:28 +02001960#ifdef ARCH_UNWIND_SECTION_NAME
1961 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Rusty Russellea01e792008-03-13 09:02:17 +00001964 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001966 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967#ifdef CONFIG_KALLSYMS
1968 /* Keep symbol and string tables for decoding later. */
1969 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1970 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1971#endif
Jan Beulich4552d5d2006-06-26 13:57:28 +02001972 if (unwindex)
1973 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Check module struct version now, before we try to use module. */
1976 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1977 err = -ENOEXEC;
1978 goto free_hdr;
1979 }
1980
1981 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1982 /* This is allowed: modprobe --force will invalidate it. */
1983 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001984 err = try_to_force_load(mod, "magic");
1985 if (err)
1986 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001987 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1989 mod->name, modmagic, vermagic);
1990 err = -ENOEXEC;
1991 goto free_hdr;
1992 }
1993
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001994 staging = get_modinfo(sechdrs, infoindex, "staging");
1995 if (staging) {
1996 add_taint_module(mod, TAINT_CRAP);
1997 printk(KERN_WARNING "%s: module is from the staging directory,"
1998 " the quality is unknown, you have been warned.\n",
1999 mod->name);
2000 }
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002003 args = strndup_user(uargs, ~0UL >> 1);
2004 if (IS_ERR(args)) {
2005 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto free_hdr;
2007 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (find_module(mod->name)) {
2010 err = -EEXIST;
2011 goto free_mod;
2012 }
2013
2014 mod->state = MODULE_STATE_COMING;
2015
2016 /* Allow arches to frob section contents and sizes. */
2017 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2018 if (err < 0)
2019 goto free_mod;
2020
2021 if (pcpuindex) {
2022 /* We have a special allocation for this section. */
2023 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002024 sechdrs[pcpuindex].sh_addralign,
2025 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (!percpu) {
2027 err = -ENOMEM;
2028 goto free_mod;
2029 }
2030 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2031 mod->percpu = percpu;
2032 }
2033
2034 /* Determine total sizes, and put offsets in sh_entsize. For now
2035 this is done generically; there doesn't appear to be any
2036 special cases for the architectures. */
2037 layout_sections(mod, hdr, sechdrs, secstrings);
2038
2039 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002040 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (!ptr) {
2042 err = -ENOMEM;
2043 goto free_percpu;
2044 }
2045 memset(ptr, 0, mod->core_size);
2046 mod->module_core = ptr;
2047
Rusty Russell3a642e92008-07-22 19:24:28 -05002048 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (!ptr && mod->init_size) {
2050 err = -ENOMEM;
2051 goto free_core;
2052 }
2053 memset(ptr, 0, mod->init_size);
2054 mod->module_init = ptr;
2055
2056 /* Transfer each section which specifies SHF_ALLOC */
2057 DEBUGP("final section addresses:\n");
2058 for (i = 0; i < hdr->e_shnum; i++) {
2059 void *dest;
2060
2061 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2062 continue;
2063
2064 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2065 dest = mod->module_init
2066 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2067 else
2068 dest = mod->module_core + sechdrs[i].sh_entsize;
2069
2070 if (sechdrs[i].sh_type != SHT_NOBITS)
2071 memcpy(dest, (void *)sechdrs[i].sh_addr,
2072 sechdrs[i].sh_size);
2073 /* Update sh_addr to point to copy in image. */
2074 sechdrs[i].sh_addr = (unsigned long)dest;
2075 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2076 }
2077 /* Module has been moved. */
2078 mod = (void *)sechdrs[modindex].sh_addr;
2079
2080 /* Now we've moved module, initialize linked lists, etc. */
2081 module_unload_init(mod);
2082
Kay Sievers97c146e2007-11-29 23:46:11 +01002083 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002084 err = mod_sysfs_init(mod);
2085 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002086 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* Set up license info based on the info section */
2089 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2090
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002091 /*
2092 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2093 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2094 * using GPL-only symbols it needs.
2095 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002096 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002097 add_taint(TAINT_PROPRIETARY_MODULE);
2098
2099 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002100 if (strcmp(mod->name, "driverloader") == 0)
2101 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d612006-01-08 01:03:41 -08002102
Matt Domschc988d2b2005-06-23 22:05:15 -07002103 /* Set up MODINFO_ATTR fields */
2104 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* Fix up syms, so that st_value is a pointer to location. */
2107 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2108 mod);
2109 if (err < 0)
2110 goto cleanup;
2111
Rusty Russell5e458cc2008-10-22 10:00:13 -05002112 /* Now we've got everything in the final locations, we can
2113 * find optional sections. */
2114 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2115 &num_kp);
2116 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2117 sizeof(*mod->syms), &mod->num_syms);
2118 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2119 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2120 sizeof(*mod->gpl_syms),
2121 &mod->num_gpl_syms);
2122 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2123 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2124 "__ksymtab_gpl_future",
2125 sizeof(*mod->gpl_future_syms),
2126 &mod->num_gpl_future_syms);
2127 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2128 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002130#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002131 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2132 "__ksymtab_unused",
2133 sizeof(*mod->unused_syms),
2134 &mod->num_unused_syms);
2135 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2136 "__kcrctab_unused");
2137 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2138 "__ksymtab_unused_gpl",
2139 sizeof(*mod->unused_gpl_syms),
2140 &mod->num_unused_gpl_syms);
2141 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2142 "__kcrctab_unused_gpl");
2143#endif
2144
2145#ifdef CONFIG_MARKERS
2146 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2147 sizeof(*mod->markers), &mod->num_markers);
2148#endif
2149#ifdef CONFIG_TRACEPOINTS
2150 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2151 "__tracepoints",
2152 sizeof(*mod->tracepoints),
2153 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002154#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002157 if ((mod->num_syms && !mod->crcs)
2158 || (mod->num_gpl_syms && !mod->gpl_crcs)
2159 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002160#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002161 || (mod->num_unused_syms && !mod->unused_crcs)
2162 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002163#endif
2164 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002165 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2166 err = try_to_force_load(mod, "nocrc");
2167 if (err)
2168 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170#endif
2171
2172 /* Now do relocations. */
2173 for (i = 1; i < hdr->e_shnum; i++) {
2174 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2175 unsigned int info = sechdrs[i].sh_info;
2176
2177 /* Not a valid relocation section? */
2178 if (info >= hdr->e_shnum)
2179 continue;
2180
2181 /* Don't bother with non-allocated sections */
2182 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2183 continue;
2184
2185 if (sechdrs[i].sh_type == SHT_REL)
2186 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2187 else if (sechdrs[i].sh_type == SHT_RELA)
2188 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2189 mod);
2190 if (err < 0)
2191 goto cleanup;
2192 }
2193
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002194 /* Find duplicate symbols */
2195 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002196 if (err < 0)
2197 goto cleanup;
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002200 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2201 sizeof(*mod->extable), &mod->num_exentries);
2202 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* Finally, copy percpu area over. */
2205 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2206 sechdrs[pcpuindex].sh_size);
2207
2208 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2209
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002210 if (!mod->taints) {
Rusty Russell5e458cc2008-10-22 10:00:13 -05002211 struct mod_debug *debug;
2212 unsigned int num_debug;
2213
Rusty Russell5e458cc2008-10-22 10:00:13 -05002214 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2215 sizeof(*debug), &num_debug);
2216 dynamic_printk_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002217 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002218
Steven Rostedtfed19392008-08-14 22:47:19 -04002219 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002220 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2221 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002222 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 err = module_finalize(hdr, sechdrs, mod);
2225 if (err < 0)
2226 goto cleanup;
2227
Thomas Koeller378bac82005-09-06 15:17:11 -07002228 /* flush the icache in correct context */
2229 old_fs = get_fs();
2230 set_fs(KERNEL_DS);
2231
2232 /*
2233 * Flush the instruction cache, since we've played with text.
2234 * Do it before processing of module parameters, so the module
2235 * can provide parameter accessor functions of its own.
2236 */
2237 if (mod->module_init)
2238 flush_icache_range((unsigned long)mod->module_init,
2239 (unsigned long)mod->module_init
2240 + mod->init_size);
2241 flush_icache_range((unsigned long)mod->module_core,
2242 (unsigned long)mod->module_core + mod->core_size);
2243
2244 set_fs(old_fs);
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002247 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002248 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2249 mod->name);
2250
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002251 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002252 * info during argument parsing. Noone should access us, since
2253 * strong_try_module_get() will fail.
2254 * lockdep/oops can run asynchronous, so use the RCU list insertion
2255 * function to insert in a way safe to concurrent readers.
2256 * The mutex protects against concurrent writers.
2257 */
2258 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002259
Rusty Russell5e458cc2008-10-22 10:00:13 -05002260 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002262 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Rusty Russell5e458cc2008-10-22 10:00:13 -05002264 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002266 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002268 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Jan Beulich4552d5d2006-06-26 13:57:28 +02002270 /* Size of section 0 is 0, so this works well if no unwind info. */
2271 mod->unwind_info = unwind_add_table(mod,
Daniel Walker22a8bde2007-10-18 03:06:07 -07002272 (void *)sechdrs[unwindex].sh_addr,
2273 sechdrs[unwindex].sh_size);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* Get rid of temporary copy */
2276 vfree(hdr);
2277
Heiko Carstens9e018922008-12-22 12:36:31 +01002278 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* Done! */
2280 return mod;
2281
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002282 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002283 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 module_arch_cleanup(mod);
2285 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002286 kobject_del(&mod->mkobj.kobj);
2287 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002288 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002289 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 module_unload_free(mod);
2291 module_free(mod, mod->module_init);
2292 free_core:
2293 module_free(mod, mod->module_core);
2294 free_percpu:
2295 if (percpu)
2296 percpu_modfree(percpu);
2297 free_mod:
2298 kfree(args);
2299 free_hdr:
2300 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002301 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002302 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 truncated:
2305 printk(KERN_ERR "Module len %lu truncated\n", len);
2306 err = -ENOEXEC;
2307 goto free_hdr;
2308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/* This is where the real work happens */
2311asmlinkage long
2312sys_init_module(void __user *umod,
2313 unsigned long len,
2314 const char __user *uargs)
2315{
2316 struct module *mod;
2317 int ret = 0;
2318
2319 /* Must have permission */
2320 if (!capable(CAP_SYS_MODULE))
2321 return -EPERM;
2322
2323 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002324 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return -EINTR;
2326
2327 /* Do all the hard work */
2328 mod = load_module(umod, len, uargs);
2329 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002330 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return PTR_ERR(mod);
2332 }
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002335 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Alan Sterne041c682006-03-27 01:16:30 -08002337 blocking_notifier_call_chain(&module_notify_list,
2338 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 /* Start the module */
2341 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002342 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (ret < 0) {
2344 /* Init routine failed: abort. Try to protect us from
2345 buggy refcounters. */
2346 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002347 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002348 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002349 blocking_notifier_call_chain(&module_notify_list,
2350 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002351 mutex_lock(&module_mutex);
2352 free_module(mod);
2353 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002354 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return ret;
2356 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002357 if (ret > 0) {
2358 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2359 "it should follow 0/-E convention\n"
2360 KERN_WARNING "%s: loading module anyway...\n",
2361 __func__, mod->name, ret,
2362 __func__);
2363 dump_stack();
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Rusty Russell6c5db222008-03-10 11:43:52 -07002366 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002368 wake_up(&module_wq);
2369
2370 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /* Drop initial reference. */
2372 module_put(mod);
Jan Beulich4552d5d2006-06-26 13:57:28 +02002373 unwind_remove_table(mod->unwind_info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 module_free(mod, mod->module_init);
2375 mod->module_init = NULL;
2376 mod->init_size = 0;
2377 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002378 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 return 0;
2381}
2382
2383static inline int within(unsigned long addr, void *start, unsigned long size)
2384{
2385 return ((void *)addr >= start && (void *)addr < start + size);
2386}
2387
2388#ifdef CONFIG_KALLSYMS
2389/*
2390 * This ignores the intensely annoying "mapping symbols" found
2391 * in ARM ELF files: $a, $t and $d.
2392 */
2393static inline int is_arm_mapping_symbol(const char *str)
2394{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002395 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 && (str[2] == '\0' || str[2] == '.');
2397}
2398
2399static const char *get_ksymbol(struct module *mod,
2400 unsigned long addr,
2401 unsigned long *size,
2402 unsigned long *offset)
2403{
2404 unsigned int i, best = 0;
2405 unsigned long nextval;
2406
2407 /* At worse, next value is at end of module */
2408 if (within(addr, mod->module_init, mod->init_size))
2409 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002410 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2412
2413 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002414 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 for (i = 1; i < mod->num_symtab; i++) {
2416 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2417 continue;
2418
2419 /* We ignore unnamed symbols: they're uninformative
2420 * and inserted at a whim. */
2421 if (mod->symtab[i].st_value <= addr
2422 && mod->symtab[i].st_value > mod->symtab[best].st_value
2423 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2424 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2425 best = i;
2426 if (mod->symtab[i].st_value > addr
2427 && mod->symtab[i].st_value < nextval
2428 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2429 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2430 nextval = mod->symtab[i].st_value;
2431 }
2432
2433 if (!best)
2434 return NULL;
2435
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002436 if (size)
2437 *size = nextval - mod->symtab[best].st_value;
2438 if (offset)
2439 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return mod->strtab + mod->symtab[best].st_name;
2441}
2442
Rusty Russell6dd06c92008-01-29 17:13:22 -05002443/* For kallsyms to ask for address resolution. NULL means not found. Careful
2444 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002445const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002446 unsigned long *size,
2447 unsigned long *offset,
2448 char **modname,
2449 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002452 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Rusty Russellcb2a5202008-01-14 00:55:03 -08002454 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002455 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (within(addr, mod->module_init, mod->init_size)
2457 || within(addr, mod->module_core, mod->core_size)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002458 if (modname)
2459 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002460 ret = get_ksymbol(mod, addr, size, offset);
2461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002464 /* Make a copy in here where it's safe */
2465 if (ret) {
2466 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2467 ret = namebuf;
2468 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002469 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002473int lookup_module_symbol_name(unsigned long addr, char *symname)
2474{
2475 struct module *mod;
2476
Rusty Russellcb2a5202008-01-14 00:55:03 -08002477 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002478 list_for_each_entry_rcu(mod, &modules, list) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002479 if (within(addr, mod->module_init, mod->init_size) ||
2480 within(addr, mod->module_core, mod->core_size)) {
2481 const char *sym;
2482
2483 sym = get_ksymbol(mod, addr, NULL, NULL);
2484 if (!sym)
2485 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002486 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002487 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002488 return 0;
2489 }
2490 }
2491out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002492 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002493 return -ERANGE;
2494}
2495
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002496int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2497 unsigned long *offset, char *modname, char *name)
2498{
2499 struct module *mod;
2500
Rusty Russellcb2a5202008-01-14 00:55:03 -08002501 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002502 list_for_each_entry_rcu(mod, &modules, list) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002503 if (within(addr, mod->module_init, mod->init_size) ||
2504 within(addr, mod->module_core, mod->core_size)) {
2505 const char *sym;
2506
2507 sym = get_ksymbol(mod, addr, size, offset);
2508 if (!sym)
2509 goto out;
2510 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002511 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002512 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002513 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002514 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002515 return 0;
2516 }
2517 }
2518out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002519 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002520 return -ERANGE;
2521}
2522
Alexey Dobriyanea078902007-05-08 00:28:39 -07002523int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2524 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct module *mod;
2527
Rusty Russellcb2a5202008-01-14 00:55:03 -08002528 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002529 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (symnum < mod->num_symtab) {
2531 *value = mod->symtab[symnum].st_value;
2532 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002533 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002534 KSYM_NAME_LEN);
2535 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002536 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002537 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540 symnum -= mod->num_symtab;
2541 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002542 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002543 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
2546static unsigned long mod_find_symname(struct module *mod, const char *name)
2547{
2548 unsigned int i;
2549
2550 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002551 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2552 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return mod->symtab[i].st_value;
2554 return 0;
2555}
2556
2557/* Look for this name: can be of form module:name. */
2558unsigned long module_kallsyms_lookup_name(const char *name)
2559{
2560 struct module *mod;
2561 char *colon;
2562 unsigned long ret = 0;
2563
2564 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002565 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if ((colon = strchr(name, ':')) != NULL) {
2567 *colon = '\0';
2568 if ((mod = find_module(name)) != NULL)
2569 ret = mod_find_symname(mod, colon+1);
2570 *colon = ':';
2571 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002572 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if ((ret = mod_find_symname(mod, name)) != 0)
2574 break;
2575 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002576 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return ret;
2578}
2579#endif /* CONFIG_KALLSYMS */
2580
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002581static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002582{
2583 int bx = 0;
2584
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002585 if (mod->taints ||
2586 mod->state == MODULE_STATE_GOING ||
2587 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002588 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002589 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002590 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002591 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002592 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002593 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002594 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002595 /*
2596 * TAINT_FORCED_RMMOD: could be added.
2597 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2598 * apply to modules.
2599 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002600
2601 /* Show a - for module-is-being-unloaded */
2602 if (mod->state == MODULE_STATE_GOING)
2603 buf[bx++] = '-';
2604 /* Show a + for module-is-being-loaded */
2605 if (mod->state == MODULE_STATE_COMING)
2606 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002607 buf[bx++] = ')';
2608 }
2609 buf[bx] = '\0';
2610
2611 return buf;
2612}
2613
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002614#ifdef CONFIG_PROC_FS
2615/* Called by the /proc file system to return a list of modules. */
2616static void *m_start(struct seq_file *m, loff_t *pos)
2617{
2618 mutex_lock(&module_mutex);
2619 return seq_list_start(&modules, *pos);
2620}
2621
2622static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2623{
2624 return seq_list_next(p, &modules, pos);
2625}
2626
2627static void m_stop(struct seq_file *m, void *p)
2628{
2629 mutex_unlock(&module_mutex);
2630}
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632static int m_show(struct seq_file *m, void *p)
2633{
2634 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002635 char buf[8];
2636
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002637 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 mod->name, mod->init_size + mod->core_size);
2639 print_unload_info(m, mod);
2640
2641 /* Informative for users. */
2642 seq_printf(m, " %s",
2643 mod->state == MODULE_STATE_GOING ? "Unloading":
2644 mod->state == MODULE_STATE_COMING ? "Loading":
2645 "Live");
2646 /* Used by oprofile and other similar tools. */
2647 seq_printf(m, " 0x%p", mod->module_core);
2648
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002649 /* Taints info */
2650 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002651 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 seq_printf(m, "\n");
2654 return 0;
2655}
2656
2657/* Format: modulename size refcount deps address
2658
2659 Where refcount is a number or -, and deps is a comma-separated list
2660 of depends or -.
2661*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002662static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .start = m_start,
2664 .next = m_next,
2665 .stop = m_stop,
2666 .show = m_show
2667};
2668
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002669static int modules_open(struct inode *inode, struct file *file)
2670{
2671 return seq_open(file, &modules_op);
2672}
2673
2674static const struct file_operations proc_modules_operations = {
2675 .open = modules_open,
2676 .read = seq_read,
2677 .llseek = seq_lseek,
2678 .release = seq_release,
2679};
2680
2681static int __init proc_modules_init(void)
2682{
2683 proc_create("modules", 0, NULL, &proc_modules_operations);
2684 return 0;
2685}
2686module_init(proc_modules_init);
2687#endif
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689/* Given an address, look for it in the module exception tables. */
2690const struct exception_table_entry *search_module_extables(unsigned long addr)
2691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 const struct exception_table_entry *e = NULL;
2693 struct module *mod;
2694
Rusty Russell24da1cb2007-07-15 23:41:46 -07002695 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002696 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (mod->num_exentries == 0)
2698 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 e = search_extable(mod->extable,
2701 mod->extable + mod->num_exentries - 1,
2702 addr);
2703 if (e)
2704 break;
2705 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002706 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002709 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return e;
2711}
2712
Ingo Molnar4d435f92006-07-03 00:24:24 -07002713/*
2714 * Is this a valid module address?
2715 */
2716int is_module_address(unsigned long addr)
2717{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002718 struct module *mod;
2719
Rusty Russell24da1cb2007-07-15 23:41:46 -07002720 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002721
Andi Kleend72b3752008-08-30 10:09:00 +02002722 list_for_each_entry_rcu(mod, &modules, list) {
Ingo Molnar4d435f92006-07-03 00:24:24 -07002723 if (within(addr, mod->module_core, mod->core_size)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002724 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002725 return 1;
2726 }
2727 }
2728
Rusty Russell24da1cb2007-07-15 23:41:46 -07002729 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002730
2731 return 0;
2732}
2733
2734
Rusty Russell24da1cb2007-07-15 23:41:46 -07002735/* Is this a valid kernel address? */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +01002736__notrace_funcgraph struct module *__module_text_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
2738 struct module *mod;
2739
Rusty Russell3a642e92008-07-22 19:24:28 -05002740 if (addr < module_addr_min || addr > module_addr_max)
2741 return NULL;
2742
Andi Kleend72b3752008-08-30 10:09:00 +02002743 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (within(addr, mod->module_init, mod->init_text_size)
2745 || within(addr, mod->module_core, mod->core_text_size))
2746 return mod;
2747 return NULL;
2748}
2749
2750struct module *module_text_address(unsigned long addr)
2751{
2752 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Rusty Russell24da1cb2007-07-15 23:41:46 -07002754 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002756 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 return mod;
2759}
2760
2761/* Don't grab lock, we're oopsing. */
2762void print_modules(void)
2763{
2764 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002765 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002768 /* Most callers should already have preempt disabled, but make sure */
2769 preempt_disable();
2770 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002771 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002772 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002773 if (last_unloaded_module[0])
2774 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 printk("\n");
2776}
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778#ifdef CONFIG_MODVERSIONS
2779/* Generate the signature for struct module here, too, for modversions. */
2780void struct_module(struct module *mod) { return; }
2781EXPORT_SYMBOL(struct_module);
2782#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002783
2784#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002785void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002786{
2787 struct module *mod;
2788
2789 mutex_lock(&module_mutex);
2790 list_for_each_entry(mod, &modules, list)
2791 if (!mod->taints)
2792 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002793 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002794 mutex_unlock(&module_mutex);
2795}
2796#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002797
2798#ifdef CONFIG_TRACEPOINTS
2799void module_update_tracepoints(void)
2800{
2801 struct module *mod;
2802
2803 mutex_lock(&module_mutex);
2804 list_for_each_entry(mod, &modules, list)
2805 if (!mod->taints)
2806 tracepoint_update_probe_range(mod->tracepoints,
2807 mod->tracepoints + mod->num_tracepoints);
2808 mutex_unlock(&module_mutex);
2809}
2810
2811/*
2812 * Returns 0 if current not found.
2813 * Returns 1 if current found.
2814 */
2815int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2816{
2817 struct module *iter_mod;
2818 int found = 0;
2819
2820 mutex_lock(&module_mutex);
2821 list_for_each_entry(iter_mod, &modules, list) {
2822 if (!iter_mod->taints) {
2823 /*
2824 * Sorted module list
2825 */
2826 if (iter_mod < iter->module)
2827 continue;
2828 else if (iter_mod > iter->module)
2829 iter->tracepoint = NULL;
2830 found = tracepoint_get_iter_range(&iter->tracepoint,
2831 iter_mod->tracepoints,
2832 iter_mod->tracepoints
2833 + iter_mod->num_tracepoints);
2834 if (found) {
2835 iter->module = iter_mod;
2836 break;
2837 }
2838 }
2839 }
2840 mutex_unlock(&module_mutex);
2841 return found;
2842}
2843#endif