blob: 496dcb57b608e0e6c60ebc5a1be002761bdf66a2 [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>
Andi Kleend72b3752008-08-30 10:09:00 +020046#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020049#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080050#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040051#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040052#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#if 0
55#define DEBUGP printk
56#else
57#define DEBUGP(fmt , a...)
58#endif
59
60#ifndef ARCH_SHF_SMALL
61#define ARCH_SHF_SMALL 0
62#endif
63
64/* If this is set, the section belongs in the init part of the module */
65#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
66
Rusty Russell24da1cb2007-07-15 23:41:46 -070067/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020068 * (delete uses stop_machine/add uses RCU list operations). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080069static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static LIST_HEAD(modules);
71
Rusty Russellc9a3ba52008-01-29 17:13:18 -050072/* Waiting for a module to finish initializing? */
73static DECLARE_WAIT_QUEUE_HEAD(module_wq);
74
Alan Sterne041c682006-03-27 01:16:30 -080075static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Rusty Russell3a642e92008-07-22 19:24:28 -050077/* Bounds of module allocation, for speeding __module_text_address */
78static unsigned long module_addr_min = -1UL, module_addr_max = 0;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080int register_module_notifier(struct notifier_block * nb)
81{
Alan Sterne041c682006-03-27 01:16:30 -080082 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84EXPORT_SYMBOL(register_module_notifier);
85
86int unregister_module_notifier(struct notifier_block * nb)
87{
Alan Sterne041c682006-03-27 01:16:30 -080088 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90EXPORT_SYMBOL(unregister_module_notifier);
91
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080092/* We require a truly strong try_module_get(): 0 means failure due to
93 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static inline int strong_try_module_get(struct module *mod)
95{
96 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050097 return -EBUSY;
98 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500100 else
101 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700104static inline void add_taint_module(struct module *mod, unsigned flag)
105{
106 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700107 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700108}
109
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200110/*
111 * A thread that wants to hold a reference to a module only while it
112 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114void __module_put_and_exit(struct module *mod, long code)
115{
116 module_put(mod);
117 do_exit(code);
118}
119EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* Find a module section: 0 means not found. */
122static unsigned int find_sec(Elf_Ehdr *hdr,
123 Elf_Shdr *sechdrs,
124 const char *secstrings,
125 const char *name)
126{
127 unsigned int i;
128
129 for (i = 1; i < hdr->e_shnum; i++)
130 /* Alloc bit cleared means "ignore it." */
131 if ((sechdrs[i].sh_flags & SHF_ALLOC)
132 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
133 return i;
134 return 0;
135}
136
Rusty Russell5e458cc2008-10-22 10:00:13 -0500137/* Find a module section, or NULL. */
138static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
139 const char *secstrings, const char *name)
140{
141 /* Section 0 has sh_addr 0. */
142 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
143}
144
145/* Find a module section, or NULL. Fill in number of "objects" in section. */
146static void *section_objs(Elf_Ehdr *hdr,
147 Elf_Shdr *sechdrs,
148 const char *secstrings,
149 const char *name,
150 size_t object_size,
151 unsigned int *num)
152{
153 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
154
155 /* Section 0 has sh_addr 0 and sh_size 0. */
156 *num = sechdrs[sec].sh_size / object_size;
157 return (void *)sechdrs[sec].sh_addr;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Provided by the linker */
161extern const struct kernel_symbol __start___ksymtab[];
162extern const struct kernel_symbol __stop___ksymtab[];
163extern const struct kernel_symbol __start___ksymtab_gpl[];
164extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800165extern const struct kernel_symbol __start___ksymtab_gpl_future[];
166extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700167extern const struct kernel_symbol __start___ksymtab_gpl_future[];
168extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169extern const unsigned long __start___kcrctab[];
170extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800171extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500172#ifdef CONFIG_UNUSED_SYMBOLS
173extern const struct kernel_symbol __start___ksymtab_unused[];
174extern const struct kernel_symbol __stop___ksymtab_unused[];
175extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
176extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700177extern const unsigned long __start___kcrctab_unused[];
178extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500179#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181#ifndef CONFIG_MODVERSIONS
182#define symversion(base, idx) NULL
183#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800184#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
186
Rusty Russelldafd0942008-07-22 19:24:25 -0500187struct symsearch {
188 const struct kernel_symbol *start, *stop;
189 const unsigned long *crcs;
190 enum {
191 NOT_GPL_ONLY,
192 GPL_ONLY,
193 WILL_BE_GPL_ONLY,
194 } licence;
195 bool unused;
196};
197
198static bool each_symbol_in_section(const struct symsearch *arr,
199 unsigned int arrsize,
200 struct module *owner,
201 bool (*fn)(const struct symsearch *syms,
202 struct module *owner,
203 unsigned int symnum, void *data),
204 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100205{
Rusty Russelldafd0942008-07-22 19:24:25 -0500206 unsigned int i, j;
207
208 for (j = 0; j < arrsize; j++) {
209 for (i = 0; i < arr[j].stop - arr[j].start; i++)
210 if (fn(&arr[j], owner, i, data))
211 return true;
212 }
213
214 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100215}
216
Rusty Russelldafd0942008-07-22 19:24:25 -0500217/* Returns true as soon as fn returns true, otherwise false. */
218static bool each_symbol(bool (*fn)(const struct symsearch *arr,
219 struct module *owner,
220 unsigned int symnum, void *data),
221 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700222{
Rusty Russelldafd0942008-07-22 19:24:25 -0500223 struct module *mod;
224 const struct symsearch arr[] = {
225 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
226 NOT_GPL_ONLY, false },
227 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
228 __start___kcrctab_gpl,
229 GPL_ONLY, false },
230 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
231 __start___kcrctab_gpl_future,
232 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500233#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500234 { __start___ksymtab_unused, __stop___ksymtab_unused,
235 __start___kcrctab_unused,
236 NOT_GPL_ONLY, true },
237 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
238 __start___kcrctab_unused_gpl,
239 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500240#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500241 };
242
243 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
244 return true;
245
Andi Kleend72b3752008-08-30 10:09:00 +0200246 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500247 struct symsearch arr[] = {
248 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
249 NOT_GPL_ONLY, false },
250 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
251 mod->gpl_crcs,
252 GPL_ONLY, false },
253 { mod->gpl_future_syms,
254 mod->gpl_future_syms + mod->num_gpl_future_syms,
255 mod->gpl_future_crcs,
256 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500257#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500258 { mod->unused_syms,
259 mod->unused_syms + mod->num_unused_syms,
260 mod->unused_crcs,
261 NOT_GPL_ONLY, true },
262 { mod->unused_gpl_syms,
263 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
264 mod->unused_gpl_crcs,
265 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500266#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500267 };
268
269 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
270 return true;
271 }
272 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700273}
274
Rusty Russelldafd0942008-07-22 19:24:25 -0500275struct find_symbol_arg {
276 /* Input */
277 const char *name;
278 bool gplok;
279 bool warn;
280
281 /* Output */
282 struct module *owner;
283 const unsigned long *crc;
284 unsigned long value;
285};
286
287static bool find_symbol_in_section(const struct symsearch *syms,
288 struct module *owner,
289 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500290{
Rusty Russelldafd0942008-07-22 19:24:25 -0500291 struct find_symbol_arg *fsa = data;
292
293 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
294 return false;
295
296 if (!fsa->gplok) {
297 if (syms->licence == GPL_ONLY)
298 return false;
299 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
300 printk(KERN_WARNING "Symbol %s is being used "
301 "by a non-GPL module, which will not "
302 "be allowed in the future\n", fsa->name);
303 printk(KERN_WARNING "Please see the file "
304 "Documentation/feature-removal-schedule.txt "
305 "in the kernel source tree for more details.\n");
306 }
307 }
308
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500309#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500310 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500311 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500313 printk(KERN_WARNING
314 "This symbol will go away in the future.\n");
315 printk(KERN_WARNING
316 "Please evalute if this is the right api to use and if "
317 "it really is, submit a report the linux kernel "
318 "mailinglist together with submitting your code for "
319 "inclusion.\n");
320 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500321#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500322
323 fsa->owner = owner;
324 fsa->crc = symversion(syms->crcs, symnum);
325 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500326 return true;
327}
328
Rusty Russellad9546c2008-05-01 21:14:59 -0500329/* Find a symbol, return value, (optional) crc and (optional) module
330 * which owns it */
331static unsigned long find_symbol(const char *name,
332 struct module **owner,
333 const unsigned long **crc,
334 bool gplok,
335 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Rusty Russelldafd0942008-07-22 19:24:25 -0500337 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Rusty Russelldafd0942008-07-22 19:24:25 -0500339 fsa.name = name;
340 fsa.gplok = gplok;
341 fsa.warn = warn;
342
343 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500344 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500345 *owner = fsa.owner;
346 if (crc)
347 *crc = fsa.crc;
348 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800352 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* Search for module by name: must hold module_mutex. */
356static struct module *find_module(const char *name)
357{
358 struct module *mod;
359
360 list_for_each_entry(mod, &modules, list) {
361 if (strcmp(mod->name, name) == 0)
362 return mod;
363 }
364 return NULL;
365}
366
367#ifdef CONFIG_SMP
368/* Number of blocks used and allocated. */
369static unsigned int pcpu_num_used, pcpu_num_allocated;
370/* Size of each block. -ve means used. */
371static int *pcpu_size;
372
373static int split_block(unsigned int i, unsigned short size)
374{
375 /* Reallocation required? */
376 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700377 int *new;
378
379 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
380 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!new)
382 return 0;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pcpu_size = new;
386 }
387
388 /* Insert a new subblock */
389 memmove(&pcpu_size[i+1], &pcpu_size[i],
390 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
391 pcpu_num_used++;
392
393 pcpu_size[i+1] -= size;
394 pcpu_size[i] = size;
395 return 1;
396}
397
398static inline unsigned int block_size(int val)
399{
400 if (val < 0)
401 return -val;
402 return val;
403}
404
Rusty Russell842bbaa2005-08-01 21:11:47 -0700405static void *percpu_modalloc(unsigned long size, unsigned long align,
406 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 unsigned long extra;
409 unsigned int i;
410 void *ptr;
411
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200412 if (align > PAGE_SIZE) {
413 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
414 name, align, PAGE_SIZE);
415 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 ptr = __per_cpu_start;
419 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
420 /* Extra for alignment requirement. */
421 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
422 BUG_ON(i == 0 && extra != 0);
423
424 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
425 continue;
426
427 /* Transfer extra to previous block. */
428 if (pcpu_size[i-1] < 0)
429 pcpu_size[i-1] -= extra;
430 else
431 pcpu_size[i-1] += extra;
432 pcpu_size[i] -= extra;
433 ptr += extra;
434
435 /* Split block if warranted */
436 if (pcpu_size[i] - size > sizeof(unsigned long))
437 if (!split_block(i, size))
438 return NULL;
439
440 /* Mark allocated */
441 pcpu_size[i] = -pcpu_size[i];
442 return ptr;
443 }
444
445 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
446 size);
447 return NULL;
448}
449
450static void percpu_modfree(void *freeme)
451{
452 unsigned int i;
453 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
454
455 /* First entry is core kernel percpu data. */
456 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
457 if (ptr == freeme) {
458 pcpu_size[i] = -pcpu_size[i];
459 goto free;
460 }
461 }
462 BUG();
463
464 free:
465 /* Merge with previous? */
466 if (pcpu_size[i-1] >= 0) {
467 pcpu_size[i-1] += pcpu_size[i];
468 pcpu_num_used--;
469 memmove(&pcpu_size[i], &pcpu_size[i+1],
470 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
471 i--;
472 }
473 /* Merge with next? */
474 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
475 pcpu_size[i] += pcpu_size[i+1];
476 pcpu_num_used--;
477 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
478 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
479 }
480}
481
482static unsigned int find_pcpusec(Elf_Ehdr *hdr,
483 Elf_Shdr *sechdrs,
484 const char *secstrings)
485{
486 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
487}
488
Mike Travisdd5af902008-01-30 13:33:32 +0100489static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
490{
491 int cpu;
492
493 for_each_possible_cpu(cpu)
494 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497static int percpu_modinit(void)
498{
499 pcpu_num_used = 2;
500 pcpu_num_allocated = 2;
501 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
502 GFP_KERNEL);
503 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200504 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* Free room. */
506 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
507 if (pcpu_size[1] < 0) {
508 printk(KERN_ERR "No per-cpu room for modules.\n");
509 pcpu_num_used = 1;
510 }
511
512 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514__initcall(percpu_modinit);
515#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700516static inline void *percpu_modalloc(unsigned long size, unsigned long align,
517 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 return NULL;
520}
521static inline void percpu_modfree(void *pcpuptr)
522{
523 BUG();
524}
525static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
526 Elf_Shdr *sechdrs,
527 const char *secstrings)
528{
529 return 0;
530}
531static inline void percpu_modcopy(void *pcpudst, const void *src,
532 unsigned long size)
533{
534 /* pcpusec should be 0, and size of that section should be 0. */
535 BUG_ON(size != 0);
536}
537#endif /* CONFIG_SMP */
538
Matt Domschc988d2b2005-06-23 22:05:15 -0700539#define MODINFO_ATTR(field) \
540static void setup_modinfo_##field(struct module *mod, const char *s) \
541{ \
542 mod->field = kstrdup(s, GFP_KERNEL); \
543} \
544static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
545 struct module *mod, char *buffer) \
546{ \
547 return sprintf(buffer, "%s\n", mod->field); \
548} \
549static int modinfo_##field##_exists(struct module *mod) \
550{ \
551 return mod->field != NULL; \
552} \
553static void free_modinfo_##field(struct module *mod) \
554{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700555 kfree(mod->field); \
556 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700557} \
558static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900559 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700560 .show = show_modinfo_##field, \
561 .setup = setup_modinfo_##field, \
562 .test = modinfo_##field##_exists, \
563 .free = free_modinfo_##field, \
564};
565
566MODINFO_ATTR(version);
567MODINFO_ATTR(srcversion);
568
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100569static char last_unloaded_module[MODULE_NAME_LEN+1];
570
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800571#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* Init the unload section of the module. */
573static void module_unload_init(struct module *mod)
574{
575 unsigned int i;
576
577 INIT_LIST_HEAD(&mod->modules_which_use_me);
578 for (i = 0; i < NR_CPUS; i++)
579 local_set(&mod->ref[i].count, 0);
580 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700581 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Backwards compatibility macros put refcount during init. */
583 mod->waiter = current;
584}
585
586/* modules using other modules */
587struct module_use
588{
589 struct list_head list;
590 struct module *module_which_uses;
591};
592
593/* Does a already use b? */
594static int already_uses(struct module *a, struct module *b)
595{
596 struct module_use *use;
597
598 list_for_each_entry(use, &b->modules_which_use_me, list) {
599 if (use->module_which_uses == a) {
600 DEBUGP("%s uses %s!\n", a->name, b->name);
601 return 1;
602 }
603 }
604 DEBUGP("%s does not use %s!\n", a->name, b->name);
605 return 0;
606}
607
608/* Module a uses b */
609static int use_module(struct module *a, struct module *b)
610{
611 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500612 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (b == NULL || already_uses(a, b)) return 1;
615
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500616 /* If we're interrupted or time out, we fail. */
617 if (wait_event_interruptible_timeout(
618 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
619 30 * HZ) <= 0) {
620 printk("%s: gave up waiting for init of module %s.\n",
621 a->name, b->name);
622 return 0;
623 }
624
625 /* If strong_try_module_get() returned a different error, we fail. */
626 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628
629 DEBUGP("Allocating new usage for %s.\n", a->name);
630 use = kmalloc(sizeof(*use), GFP_ATOMIC);
631 if (!use) {
632 printk("%s: out of memory loading\n", a->name);
633 module_put(b);
634 return 0;
635 }
636
637 use->module_which_uses = a;
638 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100639 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 1;
641}
642
643/* Clear the unload stuff of the module. */
644static void module_unload_free(struct module *mod)
645{
646 struct module *i;
647
648 list_for_each_entry(i, &modules, list) {
649 struct module_use *use;
650
651 list_for_each_entry(use, &i->modules_which_use_me, list) {
652 if (use->module_which_uses == mod) {
653 DEBUGP("%s unusing %s\n", mod->name, i->name);
654 module_put(i);
655 list_del(&use->list);
656 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100657 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* There can be at most one match. */
659 break;
660 }
661 }
662 }
663}
664
665#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800666static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int ret = (flags & O_TRUNC);
669 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800670 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return ret;
672}
673#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800674static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 return 0;
677}
678#endif /* CONFIG_MODULE_FORCE_UNLOAD */
679
680struct stopref
681{
682 struct module *mod;
683 int flags;
684 int *forced;
685};
686
687/* Whole machine is stopped with interrupts off when this runs. */
688static int __try_stop_module(void *_sref)
689{
690 struct stopref *sref = _sref;
691
Rusty Russellda39ba52008-07-22 19:24:25 -0500692 /* If it's not unused, quit unless we're forcing. */
693 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800694 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EWOULDBLOCK;
696 }
697
698 /* Mark it as dying. */
699 sref->mod->state = MODULE_STATE_GOING;
700 return 0;
701}
702
703static int try_stop_module(struct module *mod, int flags, int *forced)
704{
Rusty Russellda39ba52008-07-22 19:24:25 -0500705 if (flags & O_NONBLOCK) {
706 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500708 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500709 } else {
710 /* We don't need to stop the machine for this. */
711 mod->state = MODULE_STATE_GOING;
712 synchronize_sched();
713 return 0;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717unsigned int module_refcount(struct module *mod)
718{
719 unsigned int i, total = 0;
720
721 for (i = 0; i < NR_CPUS; i++)
722 total += local_read(&mod->ref[i].count);
723 return total;
724}
725EXPORT_SYMBOL(module_refcount);
726
727/* This exists whether we can unload or not */
728static void free_module(struct module *mod);
729
730static void wait_for_zero_refcount(struct module *mod)
731{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500732 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800733 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 for (;;) {
735 DEBUGP("Looking at refcount...\n");
736 set_current_state(TASK_UNINTERRUPTIBLE);
737 if (module_refcount(mod) == 0)
738 break;
739 schedule();
740 }
741 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800742 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800745asmlinkage long
746sys_delete_module(const char __user *name_user, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800749 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int ret, forced = 0;
751
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800752 if (!capable(CAP_SYS_MODULE))
753 return -EPERM;
754
755 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
756 return -EFAULT;
757 name[MODULE_NAME_LEN-1] = '\0';
758
Heiko Carstens9e018922008-12-22 12:36:31 +0100759 /* Create stop_machine threads since free_module relies on
760 * a non-failing stop_machine call. */
761 ret = stop_machine_create();
762 if (ret)
763 return ret;
764
765 if (mutex_lock_interruptible(&module_mutex) != 0) {
766 ret = -EINTR;
767 goto out_stop;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 mod = find_module(name);
771 if (!mod) {
772 ret = -ENOENT;
773 goto out;
774 }
775
776 if (!list_empty(&mod->modules_which_use_me)) {
777 /* Other modules depend on us: get rid of them first. */
778 ret = -EWOULDBLOCK;
779 goto out;
780 }
781
782 /* Doing init or already dying? */
783 if (mod->state != MODULE_STATE_LIVE) {
784 /* FIXME: if (force), slam module count and wake up
785 waiter --RR */
786 DEBUGP("%s already dying\n", mod->name);
787 ret = -EBUSY;
788 goto out;
789 }
790
791 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700792 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800793 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!forced) {
795 /* This module can't be removed */
796 ret = -EBUSY;
797 goto out;
798 }
799 }
800
801 /* Set this up before setting mod->state */
802 mod->waiter = current;
803
804 /* Stop the machine so refcounts can't move and disable module. */
805 ret = try_stop_module(mod, flags, &forced);
806 if (ret != 0)
807 goto out;
808
809 /* Never wait if forced. */
810 if (!forced && module_refcount(mod) != 0)
811 wait_for_zero_refcount(mod);
812
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200813 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200815 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200817 blocking_notifier_call_chain(&module_notify_list,
818 MODULE_STATE_GOING, mod);
819 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100820 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500821 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400822 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 free_module(mod);
824
825 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800826 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100827out_stop:
828 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return ret;
830}
831
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800832static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct module_use *use;
835 int printed_something = 0;
836
837 seq_printf(m, " %u ", module_refcount(mod));
838
839 /* Always include a trailing , so userspace can differentiate
840 between this and the old multi-field proc format. */
841 list_for_each_entry(use, &mod->modules_which_use_me, list) {
842 printed_something = 1;
843 seq_printf(m, "%s,", use->module_which_uses->name);
844 }
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (mod->init != NULL && mod->exit == NULL) {
847 printed_something = 1;
848 seq_printf(m, "[permanent],");
849 }
850
851 if (!printed_something)
852 seq_printf(m, "-");
853}
854
855void __symbol_put(const char *symbol)
856{
857 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Rusty Russell24da1cb2007-07-15 23:41:46 -0700859 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500860 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 BUG();
862 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700863 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865EXPORT_SYMBOL(__symbol_put);
866
867void symbol_put_addr(void *addr)
868{
Trent Piepho5e376612006-05-15 09:44:06 -0700869 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Trent Piepho5e376612006-05-15 09:44:06 -0700871 if (core_kernel_text((unsigned long)addr))
872 return;
873
874 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700876 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878EXPORT_SYMBOL_GPL(symbol_put_addr);
879
880static ssize_t show_refcnt(struct module_attribute *mattr,
881 struct module *mod, char *buffer)
882{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400883 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900887 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 .show = show_refcnt,
889};
890
Al Virof6a57032006-10-18 01:47:25 -0400891void module_put(struct module *module)
892{
893 if (module) {
894 unsigned int cpu = get_cpu();
895 local_dec(&module->ref[cpu].count);
896 /* Maybe they're waiting for us to drop reference? */
897 if (unlikely(!module_is_live(module)))
898 wake_up_process(module->waiter);
899 put_cpu();
900 }
901}
902EXPORT_SYMBOL(module_put);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800905static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 /* We don't know the usage count, or what modules are using. */
908 seq_printf(m, " - -");
909}
910
911static inline void module_unload_free(struct module *mod)
912{
913}
914
915static inline int use_module(struct module *a, struct module *b)
916{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500917 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920static inline void module_unload_init(struct module *mod)
921{
922}
923#endif /* CONFIG_MODULE_UNLOAD */
924
Kay Sievers1f717402006-11-24 12:15:25 +0100925static ssize_t show_initstate(struct module_attribute *mattr,
926 struct module *mod, char *buffer)
927{
928 const char *state = "unknown";
929
930 switch (mod->state) {
931 case MODULE_STATE_LIVE:
932 state = "live";
933 break;
934 case MODULE_STATE_COMING:
935 state = "coming";
936 break;
937 case MODULE_STATE_GOING:
938 state = "going";
939 break;
940 }
941 return sprintf(buffer, "%s\n", state);
942}
943
944static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900945 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100946 .show = show_initstate,
947};
948
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800949static struct module_attribute *modinfo_attrs[] = {
950 &modinfo_version,
951 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100952 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800953#ifdef CONFIG_MODULE_UNLOAD
954 &refcnt,
955#endif
956 NULL,
957};
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959static const char vermagic[] = VERMAGIC_STRING;
960
Linus Torvalds826e4502008-05-04 17:04:16 -0700961static int try_to_force_load(struct module *mod, const char *symname)
962{
963#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700964 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700965 printk("%s: no version for \"%s\" found: kernel tainted.\n",
966 mod->name, symname);
967 add_taint_module(mod, TAINT_FORCED_MODULE);
968 return 0;
969#else
970 return -ENOEXEC;
971#endif
972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#ifdef CONFIG_MODVERSIONS
975static int check_version(Elf_Shdr *sechdrs,
976 unsigned int versindex,
977 const char *symname,
978 struct module *mod,
979 const unsigned long *crc)
980{
981 unsigned int i, num_versions;
982 struct modversion_info *versions;
983
984 /* Exporting module didn't supply crcs? OK, we're already tainted. */
985 if (!crc)
986 return 1;
987
Rusty Russella5dd6972008-05-09 16:24:21 +1000988 /* No versions at all? modprobe --force does this. */
989 if (versindex == 0)
990 return try_to_force_load(mod, symname) == 0;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 versions = (void *) sechdrs[versindex].sh_addr;
993 num_versions = sechdrs[versindex].sh_size
994 / sizeof(struct modversion_info);
995
996 for (i = 0; i < num_versions; i++) {
997 if (strcmp(versions[i].name, symname) != 0)
998 continue;
999
1000 if (versions[i].crc == *crc)
1001 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 DEBUGP("Found checksum %lX vs module %lX\n",
1003 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001004 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001006
Rusty Russella5dd6972008-05-09 16:24:21 +10001007 printk(KERN_WARNING "%s: no symbol version for %s\n",
1008 mod->name, symname);
1009 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001010
1011bad_version:
1012 printk("%s: disagrees about version of symbol %s\n",
1013 mod->name, symname);
1014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1018 unsigned int versindex,
1019 struct module *mod)
1020{
1021 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Rusty Russellad9546c2008-05-01 21:14:59 -05001023 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001025 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Rusty Russell91e37a72008-05-09 16:25:28 +10001028/* First part is kernel version, which we ignore if module has crcs. */
1029static inline int same_magic(const char *amagic, const char *bmagic,
1030 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Rusty Russell91e37a72008-05-09 16:25:28 +10001032 if (has_crcs) {
1033 amagic += strcspn(amagic, " ");
1034 bmagic += strcspn(bmagic, " ");
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return strcmp(amagic, bmagic) == 0;
1037}
1038#else
1039static inline int check_version(Elf_Shdr *sechdrs,
1040 unsigned int versindex,
1041 const char *symname,
1042 struct module *mod,
1043 const unsigned long *crc)
1044{
1045 return 1;
1046}
1047
1048static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1049 unsigned int versindex,
1050 struct module *mod)
1051{
1052 return 1;
1053}
1054
Rusty Russell91e37a72008-05-09 16:25:28 +10001055static inline int same_magic(const char *amagic, const char *bmagic,
1056 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 return strcmp(amagic, bmagic) == 0;
1059}
1060#endif /* CONFIG_MODVERSIONS */
1061
1062/* Resolve a symbol for this module. I.e. if we find one, record usage.
1063 Must be holding module_mutex. */
1064static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1065 unsigned int versindex,
1066 const char *name,
1067 struct module *mod)
1068{
1069 struct module *owner;
1070 unsigned long ret;
1071 const unsigned long *crc;
1072
Rusty Russellad9546c2008-05-01 21:14:59 -05001073 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001074 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001075 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001076 /* use_module can fail due to OOM,
1077 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1079 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001080 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return ret;
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/*
1086 * /sys/module/foo/sections stuff
1087 * J. Corbet <corbet@lwn.net>
1088 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001089#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001090struct module_sect_attr
1091{
1092 struct module_attribute mattr;
1093 char *name;
1094 unsigned long address;
1095};
1096
1097struct module_sect_attrs
1098{
1099 struct attribute_group grp;
1100 unsigned int nsections;
1101 struct module_sect_attr attrs[0];
1102};
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104static ssize_t module_sect_show(struct module_attribute *mattr,
1105 struct module *mod, char *buf)
1106{
1107 struct module_sect_attr *sattr =
1108 container_of(mattr, struct module_sect_attr, mattr);
1109 return sprintf(buf, "0x%lx\n", sattr->address);
1110}
1111
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001112static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1113{
Rusty Russella58730c2008-03-13 09:03:44 +00001114 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001115
1116 for (section = 0; section < sect_attrs->nsections; section++)
1117 kfree(sect_attrs->attrs[section].name);
1118 kfree(sect_attrs);
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static void add_sect_attrs(struct module *mod, unsigned int nsect,
1122 char *secstrings, Elf_Shdr *sechdrs)
1123{
1124 unsigned int nloaded = 0, i, size[2];
1125 struct module_sect_attrs *sect_attrs;
1126 struct module_sect_attr *sattr;
1127 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Count loaded sections and allocate structures */
1130 for (i = 0; i < nsect; i++)
1131 if (sechdrs[i].sh_flags & SHF_ALLOC)
1132 nloaded++;
1133 size[0] = ALIGN(sizeof(*sect_attrs)
1134 + nloaded * sizeof(sect_attrs->attrs[0]),
1135 sizeof(sect_attrs->grp.attrs[0]));
1136 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001137 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1138 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return;
1140
1141 /* Setup section attributes. */
1142 sect_attrs->grp.name = "sections";
1143 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1144
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001145 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sattr = &sect_attrs->attrs[0];
1147 gattr = &sect_attrs->grp.attrs[0];
1148 for (i = 0; i < nsect; i++) {
1149 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1150 continue;
1151 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001152 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1153 GFP_KERNEL);
1154 if (sattr->name == NULL)
1155 goto out;
1156 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 sattr->mattr.show = module_sect_show;
1158 sattr->mattr.store = NULL;
1159 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 sattr->mattr.attr.mode = S_IRUGO;
1161 *(gattr++) = &(sattr++)->mattr.attr;
1162 }
1163 *gattr = NULL;
1164
1165 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1166 goto out;
1167
1168 mod->sect_attrs = sect_attrs;
1169 return;
1170 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001171 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static void remove_sect_attrs(struct module *mod)
1175{
1176 if (mod->sect_attrs) {
1177 sysfs_remove_group(&mod->mkobj.kobj,
1178 &mod->sect_attrs->grp);
1179 /* We are positive that no one is using any sect attrs
1180 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001181 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 mod->sect_attrs = NULL;
1183 }
1184}
1185
Roland McGrath6d760132007-10-16 23:26:40 -07001186/*
1187 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1188 */
1189
1190struct module_notes_attrs {
1191 struct kobject *dir;
1192 unsigned int notes;
1193 struct bin_attribute attrs[0];
1194};
1195
1196static ssize_t module_notes_read(struct kobject *kobj,
1197 struct bin_attribute *bin_attr,
1198 char *buf, loff_t pos, size_t count)
1199{
1200 /*
1201 * The caller checked the pos and count against our size.
1202 */
1203 memcpy(buf, bin_attr->private + pos, count);
1204 return count;
1205}
1206
1207static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1208 unsigned int i)
1209{
1210 if (notes_attrs->dir) {
1211 while (i-- > 0)
1212 sysfs_remove_bin_file(notes_attrs->dir,
1213 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001214 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001215 }
1216 kfree(notes_attrs);
1217}
1218
1219static void add_notes_attrs(struct module *mod, unsigned int nsect,
1220 char *secstrings, Elf_Shdr *sechdrs)
1221{
1222 unsigned int notes, loaded, i;
1223 struct module_notes_attrs *notes_attrs;
1224 struct bin_attribute *nattr;
1225
1226 /* Count notes sections and allocate structures. */
1227 notes = 0;
1228 for (i = 0; i < nsect; i++)
1229 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1230 (sechdrs[i].sh_type == SHT_NOTE))
1231 ++notes;
1232
1233 if (notes == 0)
1234 return;
1235
1236 notes_attrs = kzalloc(sizeof(*notes_attrs)
1237 + notes * sizeof(notes_attrs->attrs[0]),
1238 GFP_KERNEL);
1239 if (notes_attrs == NULL)
1240 return;
1241
1242 notes_attrs->notes = notes;
1243 nattr = &notes_attrs->attrs[0];
1244 for (loaded = i = 0; i < nsect; ++i) {
1245 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1246 continue;
1247 if (sechdrs[i].sh_type == SHT_NOTE) {
1248 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1249 nattr->attr.mode = S_IRUGO;
1250 nattr->size = sechdrs[i].sh_size;
1251 nattr->private = (void *) sechdrs[i].sh_addr;
1252 nattr->read = module_notes_read;
1253 ++nattr;
1254 }
1255 ++loaded;
1256 }
1257
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001258 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001259 if (!notes_attrs->dir)
1260 goto out;
1261
1262 for (i = 0; i < notes; ++i)
1263 if (sysfs_create_bin_file(notes_attrs->dir,
1264 &notes_attrs->attrs[i]))
1265 goto out;
1266
1267 mod->notes_attrs = notes_attrs;
1268 return;
1269
1270 out:
1271 free_notes_attrs(notes_attrs, i);
1272}
1273
1274static void remove_notes_attrs(struct module *mod)
1275{
1276 if (mod->notes_attrs)
1277 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1283 char *sectstrings, Elf_Shdr *sechdrs)
1284{
1285}
1286
1287static inline void remove_sect_attrs(struct module *mod)
1288{
1289}
Roland McGrath6d760132007-10-16 23:26:40 -07001290
1291static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1292 char *sectstrings, Elf_Shdr *sechdrs)
1293{
1294}
1295
1296static inline void remove_notes_attrs(struct module *mod)
1297{
1298}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Randy Dunlapef665c12007-02-13 15:19:06 -08001301#ifdef CONFIG_SYSFS
1302int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001303{
1304 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001305 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001306 int error = 0;
1307 int i;
1308
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001309 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1310 (ARRAY_SIZE(modinfo_attrs) + 1)),
1311 GFP_KERNEL);
1312 if (!mod->modinfo_attrs)
1313 return -ENOMEM;
1314
1315 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001316 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1317 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001318 (attr->test && attr->test(mod))) {
1319 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001320 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1321 ++temp_attr;
1322 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001323 }
1324 return error;
1325}
1326
Randy Dunlapef665c12007-02-13 15:19:06 -08001327void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001328{
1329 struct module_attribute *attr;
1330 int i;
1331
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001332 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1333 /* pick a field to test for end of list */
1334 if (!attr->attr.name)
1335 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001336 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001337 if (attr->free)
1338 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001339 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001340 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001341}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Randy Dunlapef665c12007-02-13 15:19:06 -08001343int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001346 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001348 if (!module_sysfs_initialized) {
1349 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001350 mod->name);
1351 err = -EINVAL;
1352 goto out;
1353 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001354
1355 kobj = kset_find_obj(module_kset, mod->name);
1356 if (kobj) {
1357 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1358 kobject_put(kobj);
1359 err = -EINVAL;
1360 goto out;
1361 }
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001364
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001365 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1366 mod->mkobj.kobj.kset = module_kset;
1367 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1368 "%s", mod->name);
1369 if (err)
1370 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001371
Kay Sievers97c146e2007-11-29 23:46:11 +01001372 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001373out:
1374 return err;
1375}
1376
Randy Dunlapef665c12007-02-13 15:19:06 -08001377int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001378 struct kernel_param *kparam,
1379 unsigned int num_params)
1380{
1381 int err;
1382
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001383 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001384 if (!mod->holders_dir) {
1385 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001386 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001387 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 err = module_param_sysfs_setup(mod, kparam, num_params);
1390 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001391 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Matt Domschc988d2b2005-06-23 22:05:15 -07001393 err = module_add_modinfo_attrs(mod);
1394 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001395 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001396
Kay Sieverse17e0f52006-11-24 12:15:25 +01001397 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399
Kay Sieverse17e0f52006-11-24 12:15:25 +01001400out_unreg_param:
1401 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001402out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001403 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001404out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001405 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return err;
1407}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001408
1409static void mod_sysfs_fini(struct module *mod)
1410{
1411 kobject_put(&mod->mkobj.kobj);
1412}
1413
1414#else /* CONFIG_SYSFS */
1415
1416static void mod_sysfs_fini(struct module *mod)
1417{
1418}
1419
1420#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422static void mod_kobject_remove(struct module *mod)
1423{
Matt Domschc988d2b2005-06-23 22:05:15 -07001424 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001426 kobject_put(mod->mkobj.drivers_dir);
1427 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001428 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431/*
1432 * unlink the module with the whole machine is stopped with interrupts off
1433 * - this defends against kallsyms not taking locks
1434 */
1435static int __unlink_module(void *_mod)
1436{
1437 struct module *mod = _mod;
1438 list_del(&mod->list);
1439 return 0;
1440}
1441
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001442/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443static void free_module(struct module *mod)
1444{
1445 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001446 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001447 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 remove_sect_attrs(mod);
1449 mod_kobject_remove(mod);
1450
1451 /* Arch-specific cleanup. */
1452 module_arch_cleanup(mod);
1453
1454 /* Module unload stuff */
1455 module_unload_free(mod);
1456
Steven Rostedtfed19392008-08-14 22:47:19 -04001457 /* release any pointers to mcount in this module */
1458 ftrace_release(mod->module_core, mod->core_size);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* This may be NULL, but that's OK */
1461 module_free(mod, mod->module_init);
1462 kfree(mod->args);
1463 if (mod->percpu)
1464 percpu_modfree(mod->percpu);
1465
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001466 /* Free lock-classes: */
1467 lockdep_free_key_range(mod->module_core, mod->core_size);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* Finally, free the core (containing the module structure) */
1470 module_free(mod, mod->module_core);
1471}
1472
1473void *__symbol_get(const char *symbol)
1474{
1475 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001476 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Rusty Russell24da1cb2007-07-15 23:41:46 -07001478 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001479 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001480 if (IS_ERR_VALUE(value))
1481 value = 0;
1482 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001484 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 return (void *)value;
1487}
1488EXPORT_SYMBOL_GPL(__symbol_get);
1489
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001490/*
1491 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001492 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001493 */
1494static int verify_export_symbols(struct module *mod)
1495{
Rusty Russellb2111042008-05-01 21:15:00 -05001496 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001497 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001498 const struct kernel_symbol *s;
1499 struct {
1500 const struct kernel_symbol *sym;
1501 unsigned int num;
1502 } arr[] = {
1503 { mod->syms, mod->num_syms },
1504 { mod->gpl_syms, mod->num_gpl_syms },
1505 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001506#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001507 { mod->unused_syms, mod->num_unused_syms },
1508 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001509#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001510 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001511
Rusty Russellb2111042008-05-01 21:15:00 -05001512 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1513 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1514 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1515 NULL, true, false))) {
1516 printk(KERN_ERR
1517 "%s: exports duplicate symbol %s"
1518 " (owned by %s)\n",
1519 mod->name, s->name, module_name(owner));
1520 return -ENOEXEC;
1521 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001522 }
Rusty Russellb2111042008-05-01 21:15:00 -05001523 }
1524 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001525}
1526
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001527/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528static int simplify_symbols(Elf_Shdr *sechdrs,
1529 unsigned int symindex,
1530 const char *strtab,
1531 unsigned int versindex,
1532 unsigned int pcpuindex,
1533 struct module *mod)
1534{
1535 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1536 unsigned long secbase;
1537 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1538 int ret = 0;
1539
1540 for (i = 1; i < n; i++) {
1541 switch (sym[i].st_shndx) {
1542 case SHN_COMMON:
1543 /* We compiled with -fno-common. These are not
1544 supposed to happen. */
1545 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1546 printk("%s: please compile with -fno-common\n",
1547 mod->name);
1548 ret = -ENOEXEC;
1549 break;
1550
1551 case SHN_ABS:
1552 /* Don't need to do anything */
1553 DEBUGP("Absolute symbol: 0x%08lx\n",
1554 (long)sym[i].st_value);
1555 break;
1556
1557 case SHN_UNDEF:
1558 sym[i].st_value
1559 = resolve_symbol(sechdrs, versindex,
1560 strtab + sym[i].st_name, mod);
1561
1562 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001563 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565 /* Ok if weak. */
1566 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1567 break;
1568
1569 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1570 mod->name, strtab + sym[i].st_name);
1571 ret = -ENOENT;
1572 break;
1573
1574 default:
1575 /* Divert to percpu allocation if a percpu var. */
1576 if (sym[i].st_shndx == pcpuindex)
1577 secbase = (unsigned long)mod->percpu;
1578 else
1579 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1580 sym[i].st_value += secbase;
1581 break;
1582 }
1583 }
1584
1585 return ret;
1586}
1587
Helge Deller088af9a2008-12-31 12:31:18 +01001588/* Additional bytes needed by arch in front of individual sections */
1589unsigned int __weak arch_mod_section_prepend(struct module *mod,
1590 unsigned int section)
1591{
1592 /* default implementation just returns zero */
1593 return 0;
1594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001597static long get_offset(struct module *mod, unsigned int *size,
1598 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 long ret;
1601
Helge Deller088af9a2008-12-31 12:31:18 +01001602 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1604 *size = ret + sechdr->sh_size;
1605 return ret;
1606}
1607
1608/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1609 might -- code, read-only data, read-write data, small data. Tally
1610 sizes, and place the offsets into sh_entsize fields: high bit means it
1611 belongs in init. */
1612static void layout_sections(struct module *mod,
1613 const Elf_Ehdr *hdr,
1614 Elf_Shdr *sechdrs,
1615 const char *secstrings)
1616{
1617 static unsigned long const masks[][2] = {
1618 /* NOTE: all executable code must be the first section
1619 * in this array; otherwise modify the text_size
1620 * finder in the two loops below */
1621 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1622 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1623 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1624 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1625 };
1626 unsigned int m, i;
1627
1628 for (i = 0; i < hdr->e_shnum; i++)
1629 sechdrs[i].sh_entsize = ~0UL;
1630
1631 DEBUGP("Core section allocation order:\n");
1632 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1633 for (i = 0; i < hdr->e_shnum; ++i) {
1634 Elf_Shdr *s = &sechdrs[i];
1635
1636 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1637 || (s->sh_flags & masks[m][1])
1638 || s->sh_entsize != ~0UL
1639 || strncmp(secstrings + s->sh_name,
1640 ".init", 5) == 0)
1641 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001642 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 DEBUGP("\t%s\n", secstrings + s->sh_name);
1644 }
1645 if (m == 0)
1646 mod->core_text_size = mod->core_size;
1647 }
1648
1649 DEBUGP("Init section allocation order:\n");
1650 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1651 for (i = 0; i < hdr->e_shnum; ++i) {
1652 Elf_Shdr *s = &sechdrs[i];
1653
1654 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1655 || (s->sh_flags & masks[m][1])
1656 || s->sh_entsize != ~0UL
1657 || strncmp(secstrings + s->sh_name,
1658 ".init", 5) != 0)
1659 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001660 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 | INIT_OFFSET_MASK);
1662 DEBUGP("\t%s\n", secstrings + s->sh_name);
1663 }
1664 if (m == 0)
1665 mod->init_text_size = mod->init_size;
1666 }
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669static void set_license(struct module *mod, const char *license)
1670{
1671 if (!license)
1672 license = "unspecified";
1673
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001674 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001675 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001676 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001677 "kernel.\n", mod->name, license);
1678 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680}
1681
1682/* Parse tag=value strings from .modinfo section */
1683static char *next_string(char *string, unsigned long *secsize)
1684{
1685 /* Skip non-zero chars */
1686 while (string[0]) {
1687 string++;
1688 if ((*secsize)-- <= 1)
1689 return NULL;
1690 }
1691
1692 /* Skip any zero padding. */
1693 while (!string[0]) {
1694 string++;
1695 if ((*secsize)-- <= 1)
1696 return NULL;
1697 }
1698 return string;
1699}
1700
1701static char *get_modinfo(Elf_Shdr *sechdrs,
1702 unsigned int info,
1703 const char *tag)
1704{
1705 char *p;
1706 unsigned int taglen = strlen(tag);
1707 unsigned long size = sechdrs[info].sh_size;
1708
1709 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1710 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1711 return p + taglen + 1;
1712 }
1713 return NULL;
1714}
1715
Matt Domschc988d2b2005-06-23 22:05:15 -07001716static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1717 unsigned int infoindex)
1718{
1719 struct module_attribute *attr;
1720 int i;
1721
1722 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1723 if (attr->setup)
1724 attr->setup(mod,
1725 get_modinfo(sechdrs,
1726 infoindex,
1727 attr->attr.name));
1728 }
1729}
Matt Domschc988d2b2005-06-23 22:05:15 -07001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001732
1733/* lookup symbol in given range of kernel_symbols */
1734static const struct kernel_symbol *lookup_symbol(const char *name,
1735 const struct kernel_symbol *start,
1736 const struct kernel_symbol *stop)
1737{
1738 const struct kernel_symbol *ks = start;
1739 for (; ks < stop; ks++)
1740 if (strcmp(ks->name, name) == 0)
1741 return ks;
1742 return NULL;
1743}
1744
Tim Abbottca4787b2009-01-05 08:40:10 -06001745static int is_exported(const char *name, unsigned long value,
1746 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Tim Abbottca4787b2009-01-05 08:40:10 -06001748 const struct kernel_symbol *ks;
1749 if (!mod)
1750 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001751 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001752 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1753 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756/* As per nm */
1757static char elf_type(const Elf_Sym *sym,
1758 Elf_Shdr *sechdrs,
1759 const char *secstrings,
1760 struct module *mod)
1761{
1762 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1763 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1764 return 'v';
1765 else
1766 return 'w';
1767 }
1768 if (sym->st_shndx == SHN_UNDEF)
1769 return 'U';
1770 if (sym->st_shndx == SHN_ABS)
1771 return 'a';
1772 if (sym->st_shndx >= SHN_LORESERVE)
1773 return '?';
1774 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1775 return 't';
1776 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1777 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1778 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1779 return 'r';
1780 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1781 return 'g';
1782 else
1783 return 'd';
1784 }
1785 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1786 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1787 return 's';
1788 else
1789 return 'b';
1790 }
1791 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1792 ".debug", strlen(".debug")) == 0)
1793 return 'n';
1794 return '?';
1795}
1796
1797static void add_kallsyms(struct module *mod,
1798 Elf_Shdr *sechdrs,
1799 unsigned int symindex,
1800 unsigned int strindex,
1801 const char *secstrings)
1802{
1803 unsigned int i;
1804
1805 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1806 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1807 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1808
1809 /* Set types up while we still have access to sections. */
1810 for (i = 0; i < mod->num_symtab; i++)
1811 mod->symtab[i].st_info
1812 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1813}
1814#else
1815static inline void add_kallsyms(struct module *mod,
1816 Elf_Shdr *sechdrs,
1817 unsigned int symindex,
1818 unsigned int strindex,
1819 const char *secstrings)
1820{
1821}
1822#endif /* CONFIG_KALLSYMS */
1823
Rusty Russell5e458cc2008-10-22 10:00:13 -05001824static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1825{
Jason Baron346e15b2008-08-12 16:46:19 -04001826#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
Rusty Russell5e458cc2008-10-22 10:00:13 -05001827 unsigned int i;
Jason Baron346e15b2008-08-12 16:46:19 -04001828
Rusty Russell5e458cc2008-10-22 10:00:13 -05001829 for (i = 0; i < num; i++) {
1830 register_dynamic_debug_module(debug[i].modname,
1831 debug[i].type,
1832 debug[i].logical_modname,
1833 debug[i].flag_names,
1834 debug[i].hash, debug[i].hash2);
Jason Baron346e15b2008-08-12 16:46:19 -04001835 }
Jason Baron346e15b2008-08-12 16:46:19 -04001836#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001837}
Jason Baron346e15b2008-08-12 16:46:19 -04001838
Rusty Russell3a642e92008-07-22 19:24:28 -05001839static void *module_alloc_update_bounds(unsigned long size)
1840{
1841 void *ret = module_alloc(size);
1842
1843 if (ret) {
1844 /* Update module bounds. */
1845 if ((unsigned long)ret < module_addr_min)
1846 module_addr_min = (unsigned long)ret;
1847 if ((unsigned long)ret + size > module_addr_max)
1848 module_addr_max = (unsigned long)ret + size;
1849 }
1850 return ret;
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853/* Allocate and load the module: note that size of section 0 is always
1854 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001855static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 unsigned long len,
1857 const char __user *uargs)
1858{
1859 Elf_Ehdr *hdr;
1860 Elf_Shdr *sechdrs;
1861 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001862 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001863 unsigned int i;
1864 unsigned int symindex = 0;
1865 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001866 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001867 unsigned int num_kp, num_mcount;
1868 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct module *mod;
1870 long err = 0;
1871 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001872 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001873 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1876 umod, len, uargs);
1877 if (len < sizeof(*hdr))
1878 return ERR_PTR(-ENOEXEC);
1879
1880 /* Suck in entire file: we'll want most of it. */
1881 /* vmalloc barfs on "unusual" numbers. Check here */
1882 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1883 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001884
1885 /* Create stop_machine threads since the error path relies on
1886 * a non-failing stop_machine call. */
1887 err = stop_machine_create();
1888 if (err)
1889 goto free_hdr;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (copy_from_user(hdr, umod, len) != 0) {
1892 err = -EFAULT;
1893 goto free_hdr;
1894 }
1895
1896 /* Sanity checks against insmoding binaries or wrong arch,
1897 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001898 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 || hdr->e_type != ET_REL
1900 || !elf_check_arch(hdr)
1901 || hdr->e_shentsize != sizeof(*sechdrs)) {
1902 err = -ENOEXEC;
1903 goto free_hdr;
1904 }
1905
1906 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1907 goto truncated;
1908
1909 /* Convenience variables */
1910 sechdrs = (void *)hdr + hdr->e_shoff;
1911 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1912 sechdrs[0].sh_addr = 0;
1913
1914 for (i = 1; i < hdr->e_shnum; i++) {
1915 if (sechdrs[i].sh_type != SHT_NOBITS
1916 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1917 goto truncated;
1918
1919 /* Mark all sections sh_addr with their address in the
1920 temporary image. */
1921 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1922
1923 /* Internal symbols and strings. */
1924 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1925 symindex = i;
1926 strindex = sechdrs[i].sh_link;
1927 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1928 }
1929#ifndef CONFIG_MODULE_UNLOAD
1930 /* Don't load .exit sections */
1931 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1932 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1933#endif
1934 }
1935
1936 modindex = find_sec(hdr, sechdrs, secstrings,
1937 ".gnu.linkonce.this_module");
1938 if (!modindex) {
1939 printk(KERN_WARNING "No module found in object\n");
1940 err = -ENOEXEC;
1941 goto free_hdr;
1942 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001943 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 mod = (void *)sechdrs[modindex].sh_addr;
1945
1946 if (symindex == 0) {
1947 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1948 mod->name);
1949 err = -ENOEXEC;
1950 goto free_hdr;
1951 }
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1954 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1955 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1956
Rusty Russellea01e792008-03-13 09:02:17 +00001957 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001959 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960#ifdef CONFIG_KALLSYMS
1961 /* Keep symbol and string tables for decoding later. */
1962 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1963 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1964#endif
1965
1966 /* Check module struct version now, before we try to use module. */
1967 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1968 err = -ENOEXEC;
1969 goto free_hdr;
1970 }
1971
1972 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1973 /* This is allowed: modprobe --force will invalidate it. */
1974 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001975 err = try_to_force_load(mod, "magic");
1976 if (err)
1977 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001978 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1980 mod->name, modmagic, vermagic);
1981 err = -ENOEXEC;
1982 goto free_hdr;
1983 }
1984
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001985 staging = get_modinfo(sechdrs, infoindex, "staging");
1986 if (staging) {
1987 add_taint_module(mod, TAINT_CRAP);
1988 printk(KERN_WARNING "%s: module is from the staging directory,"
1989 " the quality is unknown, you have been warned.\n",
1990 mod->name);
1991 }
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001994 args = strndup_user(uargs, ~0UL >> 1);
1995 if (IS_ERR(args)) {
1996 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto free_hdr;
1998 }
Andrew Morton8e08b752006-02-07 12:58:45 -08001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (find_module(mod->name)) {
2001 err = -EEXIST;
2002 goto free_mod;
2003 }
2004
2005 mod->state = MODULE_STATE_COMING;
2006
2007 /* Allow arches to frob section contents and sizes. */
2008 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2009 if (err < 0)
2010 goto free_mod;
2011
2012 if (pcpuindex) {
2013 /* We have a special allocation for this section. */
2014 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002015 sechdrs[pcpuindex].sh_addralign,
2016 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (!percpu) {
2018 err = -ENOMEM;
2019 goto free_mod;
2020 }
2021 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2022 mod->percpu = percpu;
2023 }
2024
2025 /* Determine total sizes, and put offsets in sh_entsize. For now
2026 this is done generically; there doesn't appear to be any
2027 special cases for the architectures. */
2028 layout_sections(mod, hdr, sechdrs, secstrings);
2029
2030 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002031 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!ptr) {
2033 err = -ENOMEM;
2034 goto free_percpu;
2035 }
2036 memset(ptr, 0, mod->core_size);
2037 mod->module_core = ptr;
2038
Rusty Russell3a642e92008-07-22 19:24:28 -05002039 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (!ptr && mod->init_size) {
2041 err = -ENOMEM;
2042 goto free_core;
2043 }
2044 memset(ptr, 0, mod->init_size);
2045 mod->module_init = ptr;
2046
2047 /* Transfer each section which specifies SHF_ALLOC */
2048 DEBUGP("final section addresses:\n");
2049 for (i = 0; i < hdr->e_shnum; i++) {
2050 void *dest;
2051
2052 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2053 continue;
2054
2055 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2056 dest = mod->module_init
2057 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2058 else
2059 dest = mod->module_core + sechdrs[i].sh_entsize;
2060
2061 if (sechdrs[i].sh_type != SHT_NOBITS)
2062 memcpy(dest, (void *)sechdrs[i].sh_addr,
2063 sechdrs[i].sh_size);
2064 /* Update sh_addr to point to copy in image. */
2065 sechdrs[i].sh_addr = (unsigned long)dest;
2066 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2067 }
2068 /* Module has been moved. */
2069 mod = (void *)sechdrs[modindex].sh_addr;
2070
2071 /* Now we've moved module, initialize linked lists, etc. */
2072 module_unload_init(mod);
2073
Kay Sievers97c146e2007-11-29 23:46:11 +01002074 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002075 err = mod_sysfs_init(mod);
2076 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002077 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /* Set up license info based on the info section */
2080 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2081
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002082 /*
2083 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2084 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2085 * using GPL-only symbols it needs.
2086 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002087 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002088 add_taint(TAINT_PROPRIETARY_MODULE);
2089
2090 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002091 if (strcmp(mod->name, "driverloader") == 0)
2092 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002093
Matt Domschc988d2b2005-06-23 22:05:15 -07002094 /* Set up MODINFO_ATTR fields */
2095 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /* Fix up syms, so that st_value is a pointer to location. */
2098 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2099 mod);
2100 if (err < 0)
2101 goto cleanup;
2102
Rusty Russell5e458cc2008-10-22 10:00:13 -05002103 /* Now we've got everything in the final locations, we can
2104 * find optional sections. */
2105 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2106 &num_kp);
2107 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2108 sizeof(*mod->syms), &mod->num_syms);
2109 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2110 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2111 sizeof(*mod->gpl_syms),
2112 &mod->num_gpl_syms);
2113 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2114 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2115 "__ksymtab_gpl_future",
2116 sizeof(*mod->gpl_future_syms),
2117 &mod->num_gpl_future_syms);
2118 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2119 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002121#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002122 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2123 "__ksymtab_unused",
2124 sizeof(*mod->unused_syms),
2125 &mod->num_unused_syms);
2126 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2127 "__kcrctab_unused");
2128 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2129 "__ksymtab_unused_gpl",
2130 sizeof(*mod->unused_gpl_syms),
2131 &mod->num_unused_gpl_syms);
2132 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2133 "__kcrctab_unused_gpl");
2134#endif
2135
2136#ifdef CONFIG_MARKERS
2137 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2138 sizeof(*mod->markers), &mod->num_markers);
2139#endif
2140#ifdef CONFIG_TRACEPOINTS
2141 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2142 "__tracepoints",
2143 sizeof(*mod->tracepoints),
2144 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002145#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002148 if ((mod->num_syms && !mod->crcs)
2149 || (mod->num_gpl_syms && !mod->gpl_crcs)
2150 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002151#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002152 || (mod->num_unused_syms && !mod->unused_crcs)
2153 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002154#endif
2155 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002156 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2157 err = try_to_force_load(mod, "nocrc");
2158 if (err)
2159 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161#endif
2162
2163 /* Now do relocations. */
2164 for (i = 1; i < hdr->e_shnum; i++) {
2165 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2166 unsigned int info = sechdrs[i].sh_info;
2167
2168 /* Not a valid relocation section? */
2169 if (info >= hdr->e_shnum)
2170 continue;
2171
2172 /* Don't bother with non-allocated sections */
2173 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2174 continue;
2175
2176 if (sechdrs[i].sh_type == SHT_REL)
2177 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2178 else if (sechdrs[i].sh_type == SHT_RELA)
2179 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2180 mod);
2181 if (err < 0)
2182 goto cleanup;
2183 }
2184
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002185 /* Find duplicate symbols */
2186 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002187 if (err < 0)
2188 goto cleanup;
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002191 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2192 sizeof(*mod->extable), &mod->num_exentries);
2193 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* Finally, copy percpu area over. */
2196 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2197 sechdrs[pcpuindex].sh_size);
2198
2199 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2200
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002201 if (!mod->taints) {
Rusty Russell5e458cc2008-10-22 10:00:13 -05002202 struct mod_debug *debug;
2203 unsigned int num_debug;
2204
Rusty Russell5e458cc2008-10-22 10:00:13 -05002205 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2206 sizeof(*debug), &num_debug);
2207 dynamic_printk_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002208 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002209
Steven Rostedtfed19392008-08-14 22:47:19 -04002210 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002211 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2212 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002213 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 err = module_finalize(hdr, sechdrs, mod);
2216 if (err < 0)
2217 goto cleanup;
2218
Thomas Koeller378bac82005-09-06 15:17:11 -07002219 /* flush the icache in correct context */
2220 old_fs = get_fs();
2221 set_fs(KERNEL_DS);
2222
2223 /*
2224 * Flush the instruction cache, since we've played with text.
2225 * Do it before processing of module parameters, so the module
2226 * can provide parameter accessor functions of its own.
2227 */
2228 if (mod->module_init)
2229 flush_icache_range((unsigned long)mod->module_init,
2230 (unsigned long)mod->module_init
2231 + mod->init_size);
2232 flush_icache_range((unsigned long)mod->module_core,
2233 (unsigned long)mod->module_core + mod->core_size);
2234
2235 set_fs(old_fs);
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002238 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002239 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2240 mod->name);
2241
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002242 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002243 * info during argument parsing. Noone should access us, since
2244 * strong_try_module_get() will fail.
2245 * lockdep/oops can run asynchronous, so use the RCU list insertion
2246 * function to insert in a way safe to concurrent readers.
2247 * The mutex protects against concurrent writers.
2248 */
2249 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002250
Rusty Russell5e458cc2008-10-22 10:00:13 -05002251 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002253 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Rusty Russell5e458cc2008-10-22 10:00:13 -05002255 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002257 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002259 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Get rid of temporary copy */
2262 vfree(hdr);
2263
Heiko Carstens9e018922008-12-22 12:36:31 +01002264 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /* Done! */
2266 return mod;
2267
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002268 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002269 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 module_arch_cleanup(mod);
2271 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002272 kobject_del(&mod->mkobj.kobj);
2273 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002274 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002275 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 module_unload_free(mod);
2277 module_free(mod, mod->module_init);
2278 free_core:
2279 module_free(mod, mod->module_core);
2280 free_percpu:
2281 if (percpu)
2282 percpu_modfree(percpu);
2283 free_mod:
2284 kfree(args);
2285 free_hdr:
2286 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002287 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002288 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 truncated:
2291 printk(KERN_ERR "Module len %lu truncated\n", len);
2292 err = -ENOEXEC;
2293 goto free_hdr;
2294}
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296/* This is where the real work happens */
2297asmlinkage long
2298sys_init_module(void __user *umod,
2299 unsigned long len,
2300 const char __user *uargs)
2301{
2302 struct module *mod;
2303 int ret = 0;
2304
2305 /* Must have permission */
2306 if (!capable(CAP_SYS_MODULE))
2307 return -EPERM;
2308
2309 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002310 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return -EINTR;
2312
2313 /* Do all the hard work */
2314 mod = load_module(umod, len, uargs);
2315 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002316 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return PTR_ERR(mod);
2318 }
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002321 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Alan Sterne041c682006-03-27 01:16:30 -08002323 blocking_notifier_call_chain(&module_notify_list,
2324 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 /* Start the module */
2327 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002328 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (ret < 0) {
2330 /* Init routine failed: abort. Try to protect us from
2331 buggy refcounters. */
2332 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002333 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002334 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002335 blocking_notifier_call_chain(&module_notify_list,
2336 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002337 mutex_lock(&module_mutex);
2338 free_module(mod);
2339 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002340 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return ret;
2342 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002343 if (ret > 0) {
2344 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2345 "it should follow 0/-E convention\n"
2346 KERN_WARNING "%s: loading module anyway...\n",
2347 __func__, mod->name, ret,
2348 __func__);
2349 dump_stack();
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Rusty Russell6c5db222008-03-10 11:43:52 -07002352 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002354 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002355 blocking_notifier_call_chain(&module_notify_list,
2356 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002357
2358 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* Drop initial reference. */
2360 module_put(mod);
2361 module_free(mod, mod->module_init);
2362 mod->module_init = NULL;
2363 mod->init_size = 0;
2364 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002365 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 return 0;
2368}
2369
2370static inline int within(unsigned long addr, void *start, unsigned long size)
2371{
2372 return ((void *)addr >= start && (void *)addr < start + size);
2373}
2374
2375#ifdef CONFIG_KALLSYMS
2376/*
2377 * This ignores the intensely annoying "mapping symbols" found
2378 * in ARM ELF files: $a, $t and $d.
2379 */
2380static inline int is_arm_mapping_symbol(const char *str)
2381{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002382 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 && (str[2] == '\0' || str[2] == '.');
2384}
2385
2386static const char *get_ksymbol(struct module *mod,
2387 unsigned long addr,
2388 unsigned long *size,
2389 unsigned long *offset)
2390{
2391 unsigned int i, best = 0;
2392 unsigned long nextval;
2393
2394 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002395 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002397 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2399
2400 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002401 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 for (i = 1; i < mod->num_symtab; i++) {
2403 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2404 continue;
2405
2406 /* We ignore unnamed symbols: they're uninformative
2407 * and inserted at a whim. */
2408 if (mod->symtab[i].st_value <= addr
2409 && mod->symtab[i].st_value > mod->symtab[best].st_value
2410 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2411 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2412 best = i;
2413 if (mod->symtab[i].st_value > addr
2414 && mod->symtab[i].st_value < nextval
2415 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2416 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2417 nextval = mod->symtab[i].st_value;
2418 }
2419
2420 if (!best)
2421 return NULL;
2422
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002423 if (size)
2424 *size = nextval - mod->symtab[best].st_value;
2425 if (offset)
2426 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return mod->strtab + mod->symtab[best].st_name;
2428}
2429
Rusty Russell6dd06c92008-01-29 17:13:22 -05002430/* For kallsyms to ask for address resolution. NULL means not found. Careful
2431 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002432const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002433 unsigned long *size,
2434 unsigned long *offset,
2435 char **modname,
2436 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002439 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Rusty Russellcb2a5202008-01-14 00:55:03 -08002441 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002442 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002443 if (within_module_init(addr, mod) ||
2444 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002445 if (modname)
2446 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002447 ret = get_ksymbol(mod, addr, size, offset);
2448 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002451 /* Make a copy in here where it's safe */
2452 if (ret) {
2453 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2454 ret = namebuf;
2455 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002456 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002460int lookup_module_symbol_name(unsigned long addr, char *symname)
2461{
2462 struct module *mod;
2463
Rusty Russellcb2a5202008-01-14 00:55:03 -08002464 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002465 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002466 if (within_module_init(addr, mod) ||
2467 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002468 const char *sym;
2469
2470 sym = get_ksymbol(mod, addr, NULL, NULL);
2471 if (!sym)
2472 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002473 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002474 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002475 return 0;
2476 }
2477 }
2478out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002479 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002480 return -ERANGE;
2481}
2482
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002483int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2484 unsigned long *offset, char *modname, char *name)
2485{
2486 struct module *mod;
2487
Rusty Russellcb2a5202008-01-14 00:55:03 -08002488 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002489 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002490 if (within_module_init(addr, mod) ||
2491 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002492 const char *sym;
2493
2494 sym = get_ksymbol(mod, addr, size, offset);
2495 if (!sym)
2496 goto out;
2497 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002498 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002499 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002500 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002501 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002502 return 0;
2503 }
2504 }
2505out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002506 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002507 return -ERANGE;
2508}
2509
Alexey Dobriyanea078902007-05-08 00:28:39 -07002510int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2511 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct module *mod;
2514
Rusty Russellcb2a5202008-01-14 00:55:03 -08002515 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002516 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (symnum < mod->num_symtab) {
2518 *value = mod->symtab[symnum].st_value;
2519 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002520 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002521 KSYM_NAME_LEN);
2522 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002523 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002524 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527 symnum -= mod->num_symtab;
2528 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002529 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002530 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533static unsigned long mod_find_symname(struct module *mod, const char *name)
2534{
2535 unsigned int i;
2536
2537 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002538 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2539 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return mod->symtab[i].st_value;
2541 return 0;
2542}
2543
2544/* Look for this name: can be of form module:name. */
2545unsigned long module_kallsyms_lookup_name(const char *name)
2546{
2547 struct module *mod;
2548 char *colon;
2549 unsigned long ret = 0;
2550
2551 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002552 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if ((colon = strchr(name, ':')) != NULL) {
2554 *colon = '\0';
2555 if ((mod = find_module(name)) != NULL)
2556 ret = mod_find_symname(mod, colon+1);
2557 *colon = ':';
2558 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002559 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if ((ret = mod_find_symname(mod, name)) != 0)
2561 break;
2562 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002563 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 return ret;
2565}
2566#endif /* CONFIG_KALLSYMS */
2567
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002568static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002569{
2570 int bx = 0;
2571
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002572 if (mod->taints ||
2573 mod->state == MODULE_STATE_GOING ||
2574 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002575 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002576 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002577 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002578 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002579 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002580 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002581 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002582 /*
2583 * TAINT_FORCED_RMMOD: could be added.
2584 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2585 * apply to modules.
2586 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002587
2588 /* Show a - for module-is-being-unloaded */
2589 if (mod->state == MODULE_STATE_GOING)
2590 buf[bx++] = '-';
2591 /* Show a + for module-is-being-loaded */
2592 if (mod->state == MODULE_STATE_COMING)
2593 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002594 buf[bx++] = ')';
2595 }
2596 buf[bx] = '\0';
2597
2598 return buf;
2599}
2600
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002601#ifdef CONFIG_PROC_FS
2602/* Called by the /proc file system to return a list of modules. */
2603static void *m_start(struct seq_file *m, loff_t *pos)
2604{
2605 mutex_lock(&module_mutex);
2606 return seq_list_start(&modules, *pos);
2607}
2608
2609static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2610{
2611 return seq_list_next(p, &modules, pos);
2612}
2613
2614static void m_stop(struct seq_file *m, void *p)
2615{
2616 mutex_unlock(&module_mutex);
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619static int m_show(struct seq_file *m, void *p)
2620{
2621 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002622 char buf[8];
2623
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002624 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 mod->name, mod->init_size + mod->core_size);
2626 print_unload_info(m, mod);
2627
2628 /* Informative for users. */
2629 seq_printf(m, " %s",
2630 mod->state == MODULE_STATE_GOING ? "Unloading":
2631 mod->state == MODULE_STATE_COMING ? "Loading":
2632 "Live");
2633 /* Used by oprofile and other similar tools. */
2634 seq_printf(m, " 0x%p", mod->module_core);
2635
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002636 /* Taints info */
2637 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002638 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 seq_printf(m, "\n");
2641 return 0;
2642}
2643
2644/* Format: modulename size refcount deps address
2645
2646 Where refcount is a number or -, and deps is a comma-separated list
2647 of depends or -.
2648*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002649static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .start = m_start,
2651 .next = m_next,
2652 .stop = m_stop,
2653 .show = m_show
2654};
2655
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002656static int modules_open(struct inode *inode, struct file *file)
2657{
2658 return seq_open(file, &modules_op);
2659}
2660
2661static const struct file_operations proc_modules_operations = {
2662 .open = modules_open,
2663 .read = seq_read,
2664 .llseek = seq_lseek,
2665 .release = seq_release,
2666};
2667
2668static int __init proc_modules_init(void)
2669{
2670 proc_create("modules", 0, NULL, &proc_modules_operations);
2671 return 0;
2672}
2673module_init(proc_modules_init);
2674#endif
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676/* Given an address, look for it in the module exception tables. */
2677const struct exception_table_entry *search_module_extables(unsigned long addr)
2678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 const struct exception_table_entry *e = NULL;
2680 struct module *mod;
2681
Rusty Russell24da1cb2007-07-15 23:41:46 -07002682 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002683 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (mod->num_exentries == 0)
2685 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 e = search_extable(mod->extable,
2688 mod->extable + mod->num_exentries - 1,
2689 addr);
2690 if (e)
2691 break;
2692 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002693 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002696 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return e;
2698}
2699
Ingo Molnar4d435f92006-07-03 00:24:24 -07002700/*
2701 * Is this a valid module address?
2702 */
2703int is_module_address(unsigned long addr)
2704{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002705 struct module *mod;
2706
Rusty Russell24da1cb2007-07-15 23:41:46 -07002707 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002708
Andi Kleend72b3752008-08-30 10:09:00 +02002709 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002710 if (within_module_core(addr, mod)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002711 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002712 return 1;
2713 }
2714 }
2715
Rusty Russell24da1cb2007-07-15 23:41:46 -07002716 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002717
2718 return 0;
2719}
2720
2721
Rusty Russell24da1cb2007-07-15 23:41:46 -07002722/* Is this a valid kernel address? */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +01002723__notrace_funcgraph struct module *__module_text_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 struct module *mod;
2726
Rusty Russell3a642e92008-07-22 19:24:28 -05002727 if (addr < module_addr_min || addr > module_addr_max)
2728 return NULL;
2729
Andi Kleend72b3752008-08-30 10:09:00 +02002730 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (within(addr, mod->module_init, mod->init_text_size)
2732 || within(addr, mod->module_core, mod->core_text_size))
2733 return mod;
2734 return NULL;
2735}
2736
2737struct module *module_text_address(unsigned long addr)
2738{
2739 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Rusty Russell24da1cb2007-07-15 23:41:46 -07002741 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002743 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 return mod;
2746}
2747
2748/* Don't grab lock, we're oopsing. */
2749void print_modules(void)
2750{
2751 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002752 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002755 /* Most callers should already have preempt disabled, but make sure */
2756 preempt_disable();
2757 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002758 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002759 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002760 if (last_unloaded_module[0])
2761 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 printk("\n");
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765#ifdef CONFIG_MODVERSIONS
2766/* Generate the signature for struct module here, too, for modversions. */
2767void struct_module(struct module *mod) { return; }
2768EXPORT_SYMBOL(struct_module);
2769#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002770
2771#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002772void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002773{
2774 struct module *mod;
2775
2776 mutex_lock(&module_mutex);
2777 list_for_each_entry(mod, &modules, list)
2778 if (!mod->taints)
2779 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002780 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002781 mutex_unlock(&module_mutex);
2782}
2783#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002784
2785#ifdef CONFIG_TRACEPOINTS
2786void module_update_tracepoints(void)
2787{
2788 struct module *mod;
2789
2790 mutex_lock(&module_mutex);
2791 list_for_each_entry(mod, &modules, list)
2792 if (!mod->taints)
2793 tracepoint_update_probe_range(mod->tracepoints,
2794 mod->tracepoints + mod->num_tracepoints);
2795 mutex_unlock(&module_mutex);
2796}
2797
2798/*
2799 * Returns 0 if current not found.
2800 * Returns 1 if current found.
2801 */
2802int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2803{
2804 struct module *iter_mod;
2805 int found = 0;
2806
2807 mutex_lock(&module_mutex);
2808 list_for_each_entry(iter_mod, &modules, list) {
2809 if (!iter_mod->taints) {
2810 /*
2811 * Sorted module list
2812 */
2813 if (iter_mod < iter->module)
2814 continue;
2815 else if (iter_mod > iter->module)
2816 iter->tracepoint = NULL;
2817 found = tracepoint_get_iter_range(&iter->tracepoint,
2818 iter_mod->tracepoints,
2819 iter_mod->tracepoints
2820 + iter_mod->num_tracepoints);
2821 if (found) {
2822 iter->module = iter_mod;
2823 break;
2824 }
2825 }
2826 }
2827 mutex_unlock(&module_mutex);
2828 return found;
2829}
2830#endif