blob: e8b51d41dd72cf8170d3404b14dff167978e324a [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>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080053#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#if 0
56#define DEBUGP printk
57#else
58#define DEBUGP(fmt , a...)
59#endif
60
61#ifndef ARCH_SHF_SMALL
62#define ARCH_SHF_SMALL 0
63#endif
64
65/* If this is set, the section belongs in the init part of the module */
66#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
67
Rusty Russell24da1cb2007-07-15 23:41:46 -070068/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020069 * (delete uses stop_machine/add uses RCU list operations). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080070static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static LIST_HEAD(modules);
72
Rusty Russellc9a3ba52008-01-29 17:13:18 -050073/* Waiting for a module to finish initializing? */
74static DECLARE_WAIT_QUEUE_HEAD(module_wq);
75
Alan Sterne041c682006-03-27 01:16:30 -080076static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Rusty Russell3a642e92008-07-22 19:24:28 -050078/* Bounds of module allocation, for speeding __module_text_address */
79static unsigned long module_addr_min = -1UL, module_addr_max = 0;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int register_module_notifier(struct notifier_block * nb)
82{
Alan Sterne041c682006-03-27 01:16:30 -080083 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85EXPORT_SYMBOL(register_module_notifier);
86
87int unregister_module_notifier(struct notifier_block * nb)
88{
Alan Sterne041c682006-03-27 01:16:30 -080089 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91EXPORT_SYMBOL(unregister_module_notifier);
92
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080093/* We require a truly strong try_module_get(): 0 means failure due to
94 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static inline int strong_try_module_get(struct module *mod)
96{
97 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050098 return -EBUSY;
99 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500101 else
102 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700105static inline void add_taint_module(struct module *mod, unsigned flag)
106{
107 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700108 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700109}
110
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200111/*
112 * A thread that wants to hold a reference to a module only while it
113 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115void __module_put_and_exit(struct module *mod, long code)
116{
117 module_put(mod);
118 do_exit(code);
119}
120EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Find a module section: 0 means not found. */
123static unsigned int find_sec(Elf_Ehdr *hdr,
124 Elf_Shdr *sechdrs,
125 const char *secstrings,
126 const char *name)
127{
128 unsigned int i;
129
130 for (i = 1; i < hdr->e_shnum; i++)
131 /* Alloc bit cleared means "ignore it." */
132 if ((sechdrs[i].sh_flags & SHF_ALLOC)
133 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
134 return i;
135 return 0;
136}
137
Rusty Russell5e458cc2008-10-22 10:00:13 -0500138/* Find a module section, or NULL. */
139static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
140 const char *secstrings, const char *name)
141{
142 /* Section 0 has sh_addr 0. */
143 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
144}
145
146/* Find a module section, or NULL. Fill in number of "objects" in section. */
147static void *section_objs(Elf_Ehdr *hdr,
148 Elf_Shdr *sechdrs,
149 const char *secstrings,
150 const char *name,
151 size_t object_size,
152 unsigned int *num)
153{
154 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
155
156 /* Section 0 has sh_addr 0 and sh_size 0. */
157 *num = sechdrs[sec].sh_size / object_size;
158 return (void *)sechdrs[sec].sh_addr;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* Provided by the linker */
162extern const struct kernel_symbol __start___ksymtab[];
163extern const struct kernel_symbol __stop___ksymtab[];
164extern const struct kernel_symbol __start___ksymtab_gpl[];
165extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800166extern const struct kernel_symbol __start___ksymtab_gpl_future[];
167extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700168extern const struct kernel_symbol __start___ksymtab_gpl_future[];
169extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170extern const unsigned long __start___kcrctab[];
171extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800172extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500173#ifdef CONFIG_UNUSED_SYMBOLS
174extern const struct kernel_symbol __start___ksymtab_unused[];
175extern const struct kernel_symbol __stop___ksymtab_unused[];
176extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
177extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700178extern const unsigned long __start___kcrctab_unused[];
179extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182#ifndef CONFIG_MODVERSIONS
183#define symversion(base, idx) NULL
184#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800185#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
187
Rusty Russelldafd0942008-07-22 19:24:25 -0500188struct symsearch {
189 const struct kernel_symbol *start, *stop;
190 const unsigned long *crcs;
191 enum {
192 NOT_GPL_ONLY,
193 GPL_ONLY,
194 WILL_BE_GPL_ONLY,
195 } licence;
196 bool unused;
197};
198
199static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize,
201 struct module *owner,
202 bool (*fn)(const struct symsearch *syms,
203 struct module *owner,
204 unsigned int symnum, void *data),
205 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100206{
Rusty Russelldafd0942008-07-22 19:24:25 -0500207 unsigned int i, j;
208
209 for (j = 0; j < arrsize; j++) {
210 for (i = 0; i < arr[j].stop - arr[j].start; i++)
211 if (fn(&arr[j], owner, i, data))
212 return true;
213 }
214
215 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100216}
217
Rusty Russelldafd0942008-07-22 19:24:25 -0500218/* Returns true as soon as fn returns true, otherwise false. */
219static bool each_symbol(bool (*fn)(const struct symsearch *arr,
220 struct module *owner,
221 unsigned int symnum, void *data),
222 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700223{
Rusty Russelldafd0942008-07-22 19:24:25 -0500224 struct module *mod;
225 const struct symsearch arr[] = {
226 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
227 NOT_GPL_ONLY, false },
228 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
229 __start___kcrctab_gpl,
230 GPL_ONLY, false },
231 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
232 __start___kcrctab_gpl_future,
233 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500234#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500235 { __start___ksymtab_unused, __stop___ksymtab_unused,
236 __start___kcrctab_unused,
237 NOT_GPL_ONLY, true },
238 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
239 __start___kcrctab_unused_gpl,
240 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500241#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500242 };
243
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
245 return true;
246
Andi Kleend72b3752008-08-30 10:09:00 +0200247 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500248 struct symsearch arr[] = {
249 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
250 NOT_GPL_ONLY, false },
251 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
252 mod->gpl_crcs,
253 GPL_ONLY, false },
254 { mod->gpl_future_syms,
255 mod->gpl_future_syms + mod->num_gpl_future_syms,
256 mod->gpl_future_crcs,
257 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500258#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500259 { mod->unused_syms,
260 mod->unused_syms + mod->num_unused_syms,
261 mod->unused_crcs,
262 NOT_GPL_ONLY, true },
263 { mod->unused_gpl_syms,
264 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
265 mod->unused_gpl_crcs,
266 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500267#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500268 };
269
270 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
271 return true;
272 }
273 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700274}
275
Rusty Russelldafd0942008-07-22 19:24:25 -0500276struct find_symbol_arg {
277 /* Input */
278 const char *name;
279 bool gplok;
280 bool warn;
281
282 /* Output */
283 struct module *owner;
284 const unsigned long *crc;
285 unsigned long value;
286};
287
288static bool find_symbol_in_section(const struct symsearch *syms,
289 struct module *owner,
290 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500291{
Rusty Russelldafd0942008-07-22 19:24:25 -0500292 struct find_symbol_arg *fsa = data;
293
294 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
295 return false;
296
297 if (!fsa->gplok) {
298 if (syms->licence == GPL_ONLY)
299 return false;
300 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
301 printk(KERN_WARNING "Symbol %s is being used "
302 "by a non-GPL module, which will not "
303 "be allowed in the future\n", fsa->name);
304 printk(KERN_WARNING "Please see the file "
305 "Documentation/feature-removal-schedule.txt "
306 "in the kernel source tree for more details.\n");
307 }
308 }
309
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500310#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500311 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500312 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500313 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500314 printk(KERN_WARNING
315 "This symbol will go away in the future.\n");
316 printk(KERN_WARNING
317 "Please evalute if this is the right api to use and if "
318 "it really is, submit a report the linux kernel "
319 "mailinglist together with submitting your code for "
320 "inclusion.\n");
321 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500322#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500323
324 fsa->owner = owner;
325 fsa->crc = symversion(syms->crcs, symnum);
326 fsa->value = syms->start[symnum].value;
Rusty Russellad9546c2008-05-01 21:14:59 -0500327 return true;
328}
329
Rusty Russellad9546c2008-05-01 21:14:59 -0500330/* Find a symbol, return value, (optional) crc and (optional) module
331 * which owns it */
332static unsigned long find_symbol(const char *name,
333 struct module **owner,
334 const unsigned long **crc,
335 bool gplok,
336 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Rusty Russelldafd0942008-07-22 19:24:25 -0500338 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Rusty Russelldafd0942008-07-22 19:24:25 -0500340 fsa.name = name;
341 fsa.gplok = gplok;
342 fsa.warn = warn;
343
344 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500345 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500346 *owner = fsa.owner;
347 if (crc)
348 *crc = fsa.crc;
349 return fsa.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 DEBUGP("Failed to find symbol %s\n", name);
Christoph Lameter88173502008-02-08 04:18:41 -0800353 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* Search for module by name: must hold module_mutex. */
357static struct module *find_module(const char *name)
358{
359 struct module *mod;
360
361 list_for_each_entry(mod, &modules, list) {
362 if (strcmp(mod->name, name) == 0)
363 return mod;
364 }
365 return NULL;
366}
367
368#ifdef CONFIG_SMP
369/* Number of blocks used and allocated. */
370static unsigned int pcpu_num_used, pcpu_num_allocated;
371/* Size of each block. -ve means used. */
372static int *pcpu_size;
373
374static int split_block(unsigned int i, unsigned short size)
375{
376 /* Reallocation required? */
377 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700378 int *new;
379
380 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
381 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!new)
383 return 0;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pcpu_size = new;
387 }
388
389 /* Insert a new subblock */
390 memmove(&pcpu_size[i+1], &pcpu_size[i],
391 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
392 pcpu_num_used++;
393
394 pcpu_size[i+1] -= size;
395 pcpu_size[i] = size;
396 return 1;
397}
398
399static inline unsigned int block_size(int val)
400{
401 if (val < 0)
402 return -val;
403 return val;
404}
405
Rusty Russell842bbaa2005-08-01 21:11:47 -0700406static void *percpu_modalloc(unsigned long size, unsigned long align,
407 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 unsigned long extra;
410 unsigned int i;
411 void *ptr;
412
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200413 if (align > PAGE_SIZE) {
414 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
415 name, align, PAGE_SIZE);
416 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 ptr = __per_cpu_start;
420 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
421 /* Extra for alignment requirement. */
422 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
423 BUG_ON(i == 0 && extra != 0);
424
425 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
426 continue;
427
428 /* Transfer extra to previous block. */
429 if (pcpu_size[i-1] < 0)
430 pcpu_size[i-1] -= extra;
431 else
432 pcpu_size[i-1] += extra;
433 pcpu_size[i] -= extra;
434 ptr += extra;
435
436 /* Split block if warranted */
437 if (pcpu_size[i] - size > sizeof(unsigned long))
438 if (!split_block(i, size))
439 return NULL;
440
441 /* Mark allocated */
442 pcpu_size[i] = -pcpu_size[i];
443 return ptr;
444 }
445
446 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
447 size);
448 return NULL;
449}
450
451static void percpu_modfree(void *freeme)
452{
453 unsigned int i;
454 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
455
456 /* First entry is core kernel percpu data. */
457 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
458 if (ptr == freeme) {
459 pcpu_size[i] = -pcpu_size[i];
460 goto free;
461 }
462 }
463 BUG();
464
465 free:
466 /* Merge with previous? */
467 if (pcpu_size[i-1] >= 0) {
468 pcpu_size[i-1] += pcpu_size[i];
469 pcpu_num_used--;
470 memmove(&pcpu_size[i], &pcpu_size[i+1],
471 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
472 i--;
473 }
474 /* Merge with next? */
475 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
476 pcpu_size[i] += pcpu_size[i+1];
477 pcpu_num_used--;
478 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
479 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
480 }
481}
482
483static unsigned int find_pcpusec(Elf_Ehdr *hdr,
484 Elf_Shdr *sechdrs,
485 const char *secstrings)
486{
487 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
488}
489
Mike Travisdd5af902008-01-30 13:33:32 +0100490static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
491{
492 int cpu;
493
494 for_each_possible_cpu(cpu)
495 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int percpu_modinit(void)
499{
500 pcpu_num_used = 2;
501 pcpu_num_allocated = 2;
502 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
503 GFP_KERNEL);
504 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200505 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Free room. */
507 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
508 if (pcpu_size[1] < 0) {
509 printk(KERN_ERR "No per-cpu room for modules.\n");
510 pcpu_num_used = 1;
511 }
512
513 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515__initcall(percpu_modinit);
516#else /* ... !CONFIG_SMP */
Rusty Russell842bbaa2005-08-01 21:11:47 -0700517static inline void *percpu_modalloc(unsigned long size, unsigned long align,
518 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 return NULL;
521}
522static inline void percpu_modfree(void *pcpuptr)
523{
524 BUG();
525}
526static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
527 Elf_Shdr *sechdrs,
528 const char *secstrings)
529{
530 return 0;
531}
532static inline void percpu_modcopy(void *pcpudst, const void *src,
533 unsigned long size)
534{
535 /* pcpusec should be 0, and size of that section should be 0. */
536 BUG_ON(size != 0);
537}
538#endif /* CONFIG_SMP */
539
Matt Domschc988d2b2005-06-23 22:05:15 -0700540#define MODINFO_ATTR(field) \
541static void setup_modinfo_##field(struct module *mod, const char *s) \
542{ \
543 mod->field = kstrdup(s, GFP_KERNEL); \
544} \
545static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
546 struct module *mod, char *buffer) \
547{ \
548 return sprintf(buffer, "%s\n", mod->field); \
549} \
550static int modinfo_##field##_exists(struct module *mod) \
551{ \
552 return mod->field != NULL; \
553} \
554static void free_modinfo_##field(struct module *mod) \
555{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700556 kfree(mod->field); \
557 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700558} \
559static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900560 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700561 .show = show_modinfo_##field, \
562 .setup = setup_modinfo_##field, \
563 .test = modinfo_##field##_exists, \
564 .free = free_modinfo_##field, \
565};
566
567MODINFO_ATTR(version);
568MODINFO_ATTR(srcversion);
569
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100570static char last_unloaded_module[MODULE_NAME_LEN+1];
571
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800572#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* Init the unload section of the module. */
574static void module_unload_init(struct module *mod)
575{
576 unsigned int i;
577
578 INIT_LIST_HEAD(&mod->modules_which_use_me);
579 for (i = 0; i < NR_CPUS; i++)
580 local_set(&mod->ref[i].count, 0);
581 /* Hold reference count during initialization. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700582 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Backwards compatibility macros put refcount during init. */
584 mod->waiter = current;
585}
586
587/* modules using other modules */
588struct module_use
589{
590 struct list_head list;
591 struct module *module_which_uses;
592};
593
594/* Does a already use b? */
595static int already_uses(struct module *a, struct module *b)
596{
597 struct module_use *use;
598
599 list_for_each_entry(use, &b->modules_which_use_me, list) {
600 if (use->module_which_uses == a) {
601 DEBUGP("%s uses %s!\n", a->name, b->name);
602 return 1;
603 }
604 }
605 DEBUGP("%s does not use %s!\n", a->name, b->name);
606 return 0;
607}
608
609/* Module a uses b */
610static int use_module(struct module *a, struct module *b)
611{
612 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500613 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (b == NULL || already_uses(a, b)) return 1;
616
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500617 /* If we're interrupted or time out, we fail. */
618 if (wait_event_interruptible_timeout(
619 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
620 30 * HZ) <= 0) {
621 printk("%s: gave up waiting for init of module %s.\n",
622 a->name, b->name);
623 return 0;
624 }
625
626 /* If strong_try_module_get() returned a different error, we fail. */
627 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629
630 DEBUGP("Allocating new usage for %s.\n", a->name);
631 use = kmalloc(sizeof(*use), GFP_ATOMIC);
632 if (!use) {
633 printk("%s: out of memory loading\n", a->name);
634 module_put(b);
635 return 0;
636 }
637
638 use->module_which_uses = a;
639 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100640 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 1;
642}
643
644/* Clear the unload stuff of the module. */
645static void module_unload_free(struct module *mod)
646{
647 struct module *i;
648
649 list_for_each_entry(i, &modules, list) {
650 struct module_use *use;
651
652 list_for_each_entry(use, &i->modules_which_use_me, list) {
653 if (use->module_which_uses == mod) {
654 DEBUGP("%s unusing %s\n", mod->name, i->name);
655 module_put(i);
656 list_del(&use->list);
657 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100658 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* There can be at most one match. */
660 break;
661 }
662 }
663 }
664}
665
666#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800667static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 int ret = (flags & O_TRUNC);
670 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800671 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return ret;
673}
674#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800675static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 return 0;
678}
679#endif /* CONFIG_MODULE_FORCE_UNLOAD */
680
681struct stopref
682{
683 struct module *mod;
684 int flags;
685 int *forced;
686};
687
688/* Whole machine is stopped with interrupts off when this runs. */
689static int __try_stop_module(void *_sref)
690{
691 struct stopref *sref = _sref;
692
Rusty Russellda39ba52008-07-22 19:24:25 -0500693 /* If it's not unused, quit unless we're forcing. */
694 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800695 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EWOULDBLOCK;
697 }
698
699 /* Mark it as dying. */
700 sref->mod->state = MODULE_STATE_GOING;
701 return 0;
702}
703
704static int try_stop_module(struct module *mod, int flags, int *forced)
705{
Rusty Russellda39ba52008-07-22 19:24:25 -0500706 if (flags & O_NONBLOCK) {
707 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500709 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500710 } else {
711 /* We don't need to stop the machine for this. */
712 mod->state = MODULE_STATE_GOING;
713 synchronize_sched();
714 return 0;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718unsigned int module_refcount(struct module *mod)
719{
720 unsigned int i, total = 0;
721
722 for (i = 0; i < NR_CPUS; i++)
723 total += local_read(&mod->ref[i].count);
724 return total;
725}
726EXPORT_SYMBOL(module_refcount);
727
728/* This exists whether we can unload or not */
729static void free_module(struct module *mod);
730
731static void wait_for_zero_refcount(struct module *mod)
732{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500733 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800734 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 for (;;) {
736 DEBUGP("Looking at refcount...\n");
737 set_current_state(TASK_UNINTERRUPTIBLE);
738 if (module_refcount(mod) == 0)
739 break;
740 schedule();
741 }
742 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800743 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100746SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
747 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800750 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int ret, forced = 0;
752
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800753 if (!capable(CAP_SYS_MODULE))
754 return -EPERM;
755
756 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
757 return -EFAULT;
758 name[MODULE_NAME_LEN-1] = '\0';
759
Heiko Carstens9e018922008-12-22 12:36:31 +0100760 /* Create stop_machine threads since free_module relies on
761 * a non-failing stop_machine call. */
762 ret = stop_machine_create();
763 if (ret)
764 return ret;
765
766 if (mutex_lock_interruptible(&module_mutex) != 0) {
767 ret = -EINTR;
768 goto out_stop;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 mod = find_module(name);
772 if (!mod) {
773 ret = -ENOENT;
774 goto out;
775 }
776
777 if (!list_empty(&mod->modules_which_use_me)) {
778 /* Other modules depend on us: get rid of them first. */
779 ret = -EWOULDBLOCK;
780 goto out;
781 }
782
783 /* Doing init or already dying? */
784 if (mod->state != MODULE_STATE_LIVE) {
785 /* FIXME: if (force), slam module count and wake up
786 waiter --RR */
787 DEBUGP("%s already dying\n", mod->name);
788 ret = -EBUSY;
789 goto out;
790 }
791
792 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700793 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800794 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!forced) {
796 /* This module can't be removed */
797 ret = -EBUSY;
798 goto out;
799 }
800 }
801
802 /* Set this up before setting mod->state */
803 mod->waiter = current;
804
805 /* Stop the machine so refcounts can't move and disable module. */
806 ret = try_stop_module(mod, flags, &forced);
807 if (ret != 0)
808 goto out;
809
810 /* Never wait if forced. */
811 if (!forced && module_refcount(mod) != 0)
812 wait_for_zero_refcount(mod);
813
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200814 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200816 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200818 blocking_notifier_call_chain(&module_notify_list,
819 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800820 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200821 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100822 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500823 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400824 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 free_module(mod);
826
827 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800828 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100829out_stop:
830 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return ret;
832}
833
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800834static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 struct module_use *use;
837 int printed_something = 0;
838
839 seq_printf(m, " %u ", module_refcount(mod));
840
841 /* Always include a trailing , so userspace can differentiate
842 between this and the old multi-field proc format. */
843 list_for_each_entry(use, &mod->modules_which_use_me, list) {
844 printed_something = 1;
845 seq_printf(m, "%s,", use->module_which_uses->name);
846 }
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (mod->init != NULL && mod->exit == NULL) {
849 printed_something = 1;
850 seq_printf(m, "[permanent],");
851 }
852
853 if (!printed_something)
854 seq_printf(m, "-");
855}
856
857void __symbol_put(const char *symbol)
858{
859 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Rusty Russell24da1cb2007-07-15 23:41:46 -0700861 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500862 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 BUG();
864 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700865 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867EXPORT_SYMBOL(__symbol_put);
868
869void symbol_put_addr(void *addr)
870{
Trent Piepho5e376612006-05-15 09:44:06 -0700871 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Trent Piepho5e376612006-05-15 09:44:06 -0700873 if (core_kernel_text((unsigned long)addr))
874 return;
875
876 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700878 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880EXPORT_SYMBOL_GPL(symbol_put_addr);
881
882static ssize_t show_refcnt(struct module_attribute *mattr,
883 struct module *mod, char *buffer)
884{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400885 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900889 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 .show = show_refcnt,
891};
892
Al Virof6a57032006-10-18 01:47:25 -0400893void module_put(struct module *module)
894{
895 if (module) {
896 unsigned int cpu = get_cpu();
897 local_dec(&module->ref[cpu].count);
898 /* Maybe they're waiting for us to drop reference? */
899 if (unlikely(!module_is_live(module)))
900 wake_up_process(module->waiter);
901 put_cpu();
902 }
903}
904EXPORT_SYMBOL(module_put);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800907static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 /* We don't know the usage count, or what modules are using. */
910 seq_printf(m, " - -");
911}
912
913static inline void module_unload_free(struct module *mod)
914{
915}
916
917static inline int use_module(struct module *a, struct module *b)
918{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500919 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922static inline void module_unload_init(struct module *mod)
923{
924}
925#endif /* CONFIG_MODULE_UNLOAD */
926
Kay Sievers1f717402006-11-24 12:15:25 +0100927static ssize_t show_initstate(struct module_attribute *mattr,
928 struct module *mod, char *buffer)
929{
930 const char *state = "unknown";
931
932 switch (mod->state) {
933 case MODULE_STATE_LIVE:
934 state = "live";
935 break;
936 case MODULE_STATE_COMING:
937 state = "coming";
938 break;
939 case MODULE_STATE_GOING:
940 state = "going";
941 break;
942 }
943 return sprintf(buffer, "%s\n", state);
944}
945
946static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900947 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100948 .show = show_initstate,
949};
950
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800951static struct module_attribute *modinfo_attrs[] = {
952 &modinfo_version,
953 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100954 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800955#ifdef CONFIG_MODULE_UNLOAD
956 &refcnt,
957#endif
958 NULL,
959};
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961static const char vermagic[] = VERMAGIC_STRING;
962
Linus Torvalds826e4502008-05-04 17:04:16 -0700963static int try_to_force_load(struct module *mod, const char *symname)
964{
965#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700966 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700967 printk("%s: no version for \"%s\" found: kernel tainted.\n",
968 mod->name, symname);
969 add_taint_module(mod, TAINT_FORCED_MODULE);
970 return 0;
971#else
972 return -ENOEXEC;
973#endif
974}
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976#ifdef CONFIG_MODVERSIONS
977static int check_version(Elf_Shdr *sechdrs,
978 unsigned int versindex,
979 const char *symname,
980 struct module *mod,
981 const unsigned long *crc)
982{
983 unsigned int i, num_versions;
984 struct modversion_info *versions;
985
986 /* Exporting module didn't supply crcs? OK, we're already tainted. */
987 if (!crc)
988 return 1;
989
Rusty Russella5dd6972008-05-09 16:24:21 +1000990 /* No versions at all? modprobe --force does this. */
991 if (versindex == 0)
992 return try_to_force_load(mod, symname) == 0;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 versions = (void *) sechdrs[versindex].sh_addr;
995 num_versions = sechdrs[versindex].sh_size
996 / sizeof(struct modversion_info);
997
998 for (i = 0; i < num_versions; i++) {
999 if (strcmp(versions[i].name, symname) != 0)
1000 continue;
1001
1002 if (versions[i].crc == *crc)
1003 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 DEBUGP("Found checksum %lX vs module %lX\n",
1005 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001006 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001008
Rusty Russella5dd6972008-05-09 16:24:21 +10001009 printk(KERN_WARNING "%s: no symbol version for %s\n",
1010 mod->name, symname);
1011 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001012
1013bad_version:
1014 printk("%s: disagrees about version of symbol %s\n",
1015 mod->name, symname);
1016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1020 unsigned int versindex,
1021 struct module *mod)
1022{
1023 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Rusty Russellad9546c2008-05-01 21:14:59 -05001025 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001027 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Rusty Russell91e37a72008-05-09 16:25:28 +10001030/* First part is kernel version, which we ignore if module has crcs. */
1031static inline int same_magic(const char *amagic, const char *bmagic,
1032 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Rusty Russell91e37a72008-05-09 16:25:28 +10001034 if (has_crcs) {
1035 amagic += strcspn(amagic, " ");
1036 bmagic += strcspn(bmagic, " ");
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return strcmp(amagic, bmagic) == 0;
1039}
1040#else
1041static inline int check_version(Elf_Shdr *sechdrs,
1042 unsigned int versindex,
1043 const char *symname,
1044 struct module *mod,
1045 const unsigned long *crc)
1046{
1047 return 1;
1048}
1049
1050static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1051 unsigned int versindex,
1052 struct module *mod)
1053{
1054 return 1;
1055}
1056
Rusty Russell91e37a72008-05-09 16:25:28 +10001057static inline int same_magic(const char *amagic, const char *bmagic,
1058 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 return strcmp(amagic, bmagic) == 0;
1061}
1062#endif /* CONFIG_MODVERSIONS */
1063
1064/* Resolve a symbol for this module. I.e. if we find one, record usage.
1065 Must be holding module_mutex. */
1066static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1067 unsigned int versindex,
1068 const char *name,
1069 struct module *mod)
1070{
1071 struct module *owner;
1072 unsigned long ret;
1073 const unsigned long *crc;
1074
Rusty Russellad9546c2008-05-01 21:14:59 -05001075 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001076 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001077 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001078 /* use_module can fail due to OOM,
1079 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1081 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001082 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return ret;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
1088 * /sys/module/foo/sections stuff
1089 * J. Corbet <corbet@lwn.net>
1090 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001091#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001092struct module_sect_attr
1093{
1094 struct module_attribute mattr;
1095 char *name;
1096 unsigned long address;
1097};
1098
1099struct module_sect_attrs
1100{
1101 struct attribute_group grp;
1102 unsigned int nsections;
1103 struct module_sect_attr attrs[0];
1104};
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static ssize_t module_sect_show(struct module_attribute *mattr,
1107 struct module *mod, char *buf)
1108{
1109 struct module_sect_attr *sattr =
1110 container_of(mattr, struct module_sect_attr, mattr);
1111 return sprintf(buf, "0x%lx\n", sattr->address);
1112}
1113
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001114static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1115{
Rusty Russella58730c2008-03-13 09:03:44 +00001116 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001117
1118 for (section = 0; section < sect_attrs->nsections; section++)
1119 kfree(sect_attrs->attrs[section].name);
1120 kfree(sect_attrs);
1121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123static void add_sect_attrs(struct module *mod, unsigned int nsect,
1124 char *secstrings, Elf_Shdr *sechdrs)
1125{
1126 unsigned int nloaded = 0, i, size[2];
1127 struct module_sect_attrs *sect_attrs;
1128 struct module_sect_attr *sattr;
1129 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* Count loaded sections and allocate structures */
1132 for (i = 0; i < nsect; i++)
1133 if (sechdrs[i].sh_flags & SHF_ALLOC)
1134 nloaded++;
1135 size[0] = ALIGN(sizeof(*sect_attrs)
1136 + nloaded * sizeof(sect_attrs->attrs[0]),
1137 sizeof(sect_attrs->grp.attrs[0]));
1138 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001139 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1140 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return;
1142
1143 /* Setup section attributes. */
1144 sect_attrs->grp.name = "sections";
1145 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1146
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001147 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sattr = &sect_attrs->attrs[0];
1149 gattr = &sect_attrs->grp.attrs[0];
1150 for (i = 0; i < nsect; i++) {
1151 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1152 continue;
1153 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001154 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1155 GFP_KERNEL);
1156 if (sattr->name == NULL)
1157 goto out;
1158 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 sattr->mattr.show = module_sect_show;
1160 sattr->mattr.store = NULL;
1161 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 sattr->mattr.attr.mode = S_IRUGO;
1163 *(gattr++) = &(sattr++)->mattr.attr;
1164 }
1165 *gattr = NULL;
1166
1167 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1168 goto out;
1169
1170 mod->sect_attrs = sect_attrs;
1171 return;
1172 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001173 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176static void remove_sect_attrs(struct module *mod)
1177{
1178 if (mod->sect_attrs) {
1179 sysfs_remove_group(&mod->mkobj.kobj,
1180 &mod->sect_attrs->grp);
1181 /* We are positive that no one is using any sect attrs
1182 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001183 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mod->sect_attrs = NULL;
1185 }
1186}
1187
Roland McGrath6d760132007-10-16 23:26:40 -07001188/*
1189 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1190 */
1191
1192struct module_notes_attrs {
1193 struct kobject *dir;
1194 unsigned int notes;
1195 struct bin_attribute attrs[0];
1196};
1197
1198static ssize_t module_notes_read(struct kobject *kobj,
1199 struct bin_attribute *bin_attr,
1200 char *buf, loff_t pos, size_t count)
1201{
1202 /*
1203 * The caller checked the pos and count against our size.
1204 */
1205 memcpy(buf, bin_attr->private + pos, count);
1206 return count;
1207}
1208
1209static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1210 unsigned int i)
1211{
1212 if (notes_attrs->dir) {
1213 while (i-- > 0)
1214 sysfs_remove_bin_file(notes_attrs->dir,
1215 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001216 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001217 }
1218 kfree(notes_attrs);
1219}
1220
1221static void add_notes_attrs(struct module *mod, unsigned int nsect,
1222 char *secstrings, Elf_Shdr *sechdrs)
1223{
1224 unsigned int notes, loaded, i;
1225 struct module_notes_attrs *notes_attrs;
1226 struct bin_attribute *nattr;
1227
1228 /* Count notes sections and allocate structures. */
1229 notes = 0;
1230 for (i = 0; i < nsect; i++)
1231 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1232 (sechdrs[i].sh_type == SHT_NOTE))
1233 ++notes;
1234
1235 if (notes == 0)
1236 return;
1237
1238 notes_attrs = kzalloc(sizeof(*notes_attrs)
1239 + notes * sizeof(notes_attrs->attrs[0]),
1240 GFP_KERNEL);
1241 if (notes_attrs == NULL)
1242 return;
1243
1244 notes_attrs->notes = notes;
1245 nattr = &notes_attrs->attrs[0];
1246 for (loaded = i = 0; i < nsect; ++i) {
1247 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1248 continue;
1249 if (sechdrs[i].sh_type == SHT_NOTE) {
1250 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1251 nattr->attr.mode = S_IRUGO;
1252 nattr->size = sechdrs[i].sh_size;
1253 nattr->private = (void *) sechdrs[i].sh_addr;
1254 nattr->read = module_notes_read;
1255 ++nattr;
1256 }
1257 ++loaded;
1258 }
1259
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001260 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001261 if (!notes_attrs->dir)
1262 goto out;
1263
1264 for (i = 0; i < notes; ++i)
1265 if (sysfs_create_bin_file(notes_attrs->dir,
1266 &notes_attrs->attrs[i]))
1267 goto out;
1268
1269 mod->notes_attrs = notes_attrs;
1270 return;
1271
1272 out:
1273 free_notes_attrs(notes_attrs, i);
1274}
1275
1276static void remove_notes_attrs(struct module *mod)
1277{
1278 if (mod->notes_attrs)
1279 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1285 char *sectstrings, Elf_Shdr *sechdrs)
1286{
1287}
1288
1289static inline void remove_sect_attrs(struct module *mod)
1290{
1291}
Roland McGrath6d760132007-10-16 23:26:40 -07001292
1293static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1294 char *sectstrings, Elf_Shdr *sechdrs)
1295{
1296}
1297
1298static inline void remove_notes_attrs(struct module *mod)
1299{
1300}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Randy Dunlapef665c12007-02-13 15:19:06 -08001303#ifdef CONFIG_SYSFS
1304int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001305{
1306 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001307 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001308 int error = 0;
1309 int i;
1310
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001311 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1312 (ARRAY_SIZE(modinfo_attrs) + 1)),
1313 GFP_KERNEL);
1314 if (!mod->modinfo_attrs)
1315 return -ENOMEM;
1316
1317 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001318 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1319 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001320 (attr->test && attr->test(mod))) {
1321 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001322 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1323 ++temp_attr;
1324 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001325 }
1326 return error;
1327}
1328
Randy Dunlapef665c12007-02-13 15:19:06 -08001329void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001330{
1331 struct module_attribute *attr;
1332 int i;
1333
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001334 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1335 /* pick a field to test for end of list */
1336 if (!attr->attr.name)
1337 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001338 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001339 if (attr->free)
1340 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001341 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001342 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Randy Dunlapef665c12007-02-13 15:19:06 -08001345int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001348 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001350 if (!module_sysfs_initialized) {
1351 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001352 mod->name);
1353 err = -EINVAL;
1354 goto out;
1355 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001356
1357 kobj = kset_find_obj(module_kset, mod->name);
1358 if (kobj) {
1359 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1360 kobject_put(kobj);
1361 err = -EINVAL;
1362 goto out;
1363 }
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001366
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001367 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1368 mod->mkobj.kobj.kset = module_kset;
1369 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1370 "%s", mod->name);
1371 if (err)
1372 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001373
Kay Sievers97c146e2007-11-29 23:46:11 +01001374 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001375out:
1376 return err;
1377}
1378
Randy Dunlapef665c12007-02-13 15:19:06 -08001379int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001380 struct kernel_param *kparam,
1381 unsigned int num_params)
1382{
1383 int err;
1384
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001385 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001386 if (!mod->holders_dir) {
1387 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001388 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001389 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 err = module_param_sysfs_setup(mod, kparam, num_params);
1392 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001393 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Matt Domschc988d2b2005-06-23 22:05:15 -07001395 err = module_add_modinfo_attrs(mod);
1396 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001397 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001398
Kay Sieverse17e0f52006-11-24 12:15:25 +01001399 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return 0;
1401
Kay Sieverse17e0f52006-11-24 12:15:25 +01001402out_unreg_param:
1403 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001404out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001405 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001406out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001407 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return err;
1409}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001410
1411static void mod_sysfs_fini(struct module *mod)
1412{
1413 kobject_put(&mod->mkobj.kobj);
1414}
1415
1416#else /* CONFIG_SYSFS */
1417
1418static void mod_sysfs_fini(struct module *mod)
1419{
1420}
1421
1422#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424static void mod_kobject_remove(struct module *mod)
1425{
Matt Domschc988d2b2005-06-23 22:05:15 -07001426 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001428 kobject_put(mod->mkobj.drivers_dir);
1429 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001430 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433/*
1434 * unlink the module with the whole machine is stopped with interrupts off
1435 * - this defends against kallsyms not taking locks
1436 */
1437static int __unlink_module(void *_mod)
1438{
1439 struct module *mod = _mod;
1440 list_del(&mod->list);
1441 return 0;
1442}
1443
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001444/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445static void free_module(struct module *mod)
1446{
1447 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001448 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001449 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 remove_sect_attrs(mod);
1451 mod_kobject_remove(mod);
1452
1453 /* Arch-specific cleanup. */
1454 module_arch_cleanup(mod);
1455
1456 /* Module unload stuff */
1457 module_unload_free(mod);
1458
Steven Rostedtfed19392008-08-14 22:47:19 -04001459 /* release any pointers to mcount in this module */
1460 ftrace_release(mod->module_core, mod->core_size);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* This may be NULL, but that's OK */
1463 module_free(mod, mod->module_init);
1464 kfree(mod->args);
1465 if (mod->percpu)
1466 percpu_modfree(mod->percpu);
1467
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001468 /* Free lock-classes: */
1469 lockdep_free_key_range(mod->module_core, mod->core_size);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* Finally, free the core (containing the module structure) */
1472 module_free(mod, mod->module_core);
1473}
1474
1475void *__symbol_get(const char *symbol)
1476{
1477 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001478 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Rusty Russell24da1cb2007-07-15 23:41:46 -07001480 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001481 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001482 if (IS_ERR_VALUE(value))
1483 value = 0;
1484 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001486 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 return (void *)value;
1489}
1490EXPORT_SYMBOL_GPL(__symbol_get);
1491
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001492/*
1493 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001494 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001495 */
1496static int verify_export_symbols(struct module *mod)
1497{
Rusty Russellb2111042008-05-01 21:15:00 -05001498 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001499 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001500 const struct kernel_symbol *s;
1501 struct {
1502 const struct kernel_symbol *sym;
1503 unsigned int num;
1504 } arr[] = {
1505 { mod->syms, mod->num_syms },
1506 { mod->gpl_syms, mod->num_gpl_syms },
1507 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001508#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001509 { mod->unused_syms, mod->num_unused_syms },
1510 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001511#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001512 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001513
Rusty Russellb2111042008-05-01 21:15:00 -05001514 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1515 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1516 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1517 NULL, true, false))) {
1518 printk(KERN_ERR
1519 "%s: exports duplicate symbol %s"
1520 " (owned by %s)\n",
1521 mod->name, s->name, module_name(owner));
1522 return -ENOEXEC;
1523 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001524 }
Rusty Russellb2111042008-05-01 21:15:00 -05001525 }
1526 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001527}
1528
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001529/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530static int simplify_symbols(Elf_Shdr *sechdrs,
1531 unsigned int symindex,
1532 const char *strtab,
1533 unsigned int versindex,
1534 unsigned int pcpuindex,
1535 struct module *mod)
1536{
1537 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1538 unsigned long secbase;
1539 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1540 int ret = 0;
1541
1542 for (i = 1; i < n; i++) {
1543 switch (sym[i].st_shndx) {
1544 case SHN_COMMON:
1545 /* We compiled with -fno-common. These are not
1546 supposed to happen. */
1547 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1548 printk("%s: please compile with -fno-common\n",
1549 mod->name);
1550 ret = -ENOEXEC;
1551 break;
1552
1553 case SHN_ABS:
1554 /* Don't need to do anything */
1555 DEBUGP("Absolute symbol: 0x%08lx\n",
1556 (long)sym[i].st_value);
1557 break;
1558
1559 case SHN_UNDEF:
1560 sym[i].st_value
1561 = resolve_symbol(sechdrs, versindex,
1562 strtab + sym[i].st_name, mod);
1563
1564 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001565 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567 /* Ok if weak. */
1568 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1569 break;
1570
1571 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1572 mod->name, strtab + sym[i].st_name);
1573 ret = -ENOENT;
1574 break;
1575
1576 default:
1577 /* Divert to percpu allocation if a percpu var. */
1578 if (sym[i].st_shndx == pcpuindex)
1579 secbase = (unsigned long)mod->percpu;
1580 else
1581 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1582 sym[i].st_value += secbase;
1583 break;
1584 }
1585 }
1586
1587 return ret;
1588}
1589
Helge Deller088af9a2008-12-31 12:31:18 +01001590/* Additional bytes needed by arch in front of individual sections */
1591unsigned int __weak arch_mod_section_prepend(struct module *mod,
1592 unsigned int section)
1593{
1594 /* default implementation just returns zero */
1595 return 0;
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001599static long get_offset(struct module *mod, unsigned int *size,
1600 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 long ret;
1603
Helge Deller088af9a2008-12-31 12:31:18 +01001604 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1606 *size = ret + sechdr->sh_size;
1607 return ret;
1608}
1609
1610/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1611 might -- code, read-only data, read-write data, small data. Tally
1612 sizes, and place the offsets into sh_entsize fields: high bit means it
1613 belongs in init. */
1614static void layout_sections(struct module *mod,
1615 const Elf_Ehdr *hdr,
1616 Elf_Shdr *sechdrs,
1617 const char *secstrings)
1618{
1619 static unsigned long const masks[][2] = {
1620 /* NOTE: all executable code must be the first section
1621 * in this array; otherwise modify the text_size
1622 * finder in the two loops below */
1623 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1624 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1625 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1626 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1627 };
1628 unsigned int m, i;
1629
1630 for (i = 0; i < hdr->e_shnum; i++)
1631 sechdrs[i].sh_entsize = ~0UL;
1632
1633 DEBUGP("Core section allocation order:\n");
1634 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1635 for (i = 0; i < hdr->e_shnum; ++i) {
1636 Elf_Shdr *s = &sechdrs[i];
1637
1638 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1639 || (s->sh_flags & masks[m][1])
1640 || s->sh_entsize != ~0UL
1641 || strncmp(secstrings + s->sh_name,
1642 ".init", 5) == 0)
1643 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001644 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 DEBUGP("\t%s\n", secstrings + s->sh_name);
1646 }
1647 if (m == 0)
1648 mod->core_text_size = mod->core_size;
1649 }
1650
1651 DEBUGP("Init section allocation order:\n");
1652 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1653 for (i = 0; i < hdr->e_shnum; ++i) {
1654 Elf_Shdr *s = &sechdrs[i];
1655
1656 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1657 || (s->sh_flags & masks[m][1])
1658 || s->sh_entsize != ~0UL
1659 || strncmp(secstrings + s->sh_name,
1660 ".init", 5) != 0)
1661 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001662 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 | INIT_OFFSET_MASK);
1664 DEBUGP("\t%s\n", secstrings + s->sh_name);
1665 }
1666 if (m == 0)
1667 mod->init_text_size = mod->init_size;
1668 }
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671static void set_license(struct module *mod, const char *license)
1672{
1673 if (!license)
1674 license = "unspecified";
1675
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001676 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001677 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001678 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001679 "kernel.\n", mod->name, license);
1680 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682}
1683
1684/* Parse tag=value strings from .modinfo section */
1685static char *next_string(char *string, unsigned long *secsize)
1686{
1687 /* Skip non-zero chars */
1688 while (string[0]) {
1689 string++;
1690 if ((*secsize)-- <= 1)
1691 return NULL;
1692 }
1693
1694 /* Skip any zero padding. */
1695 while (!string[0]) {
1696 string++;
1697 if ((*secsize)-- <= 1)
1698 return NULL;
1699 }
1700 return string;
1701}
1702
1703static char *get_modinfo(Elf_Shdr *sechdrs,
1704 unsigned int info,
1705 const char *tag)
1706{
1707 char *p;
1708 unsigned int taglen = strlen(tag);
1709 unsigned long size = sechdrs[info].sh_size;
1710
1711 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1712 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1713 return p + taglen + 1;
1714 }
1715 return NULL;
1716}
1717
Matt Domschc988d2b2005-06-23 22:05:15 -07001718static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1719 unsigned int infoindex)
1720{
1721 struct module_attribute *attr;
1722 int i;
1723
1724 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1725 if (attr->setup)
1726 attr->setup(mod,
1727 get_modinfo(sechdrs,
1728 infoindex,
1729 attr->attr.name));
1730 }
1731}
Matt Domschc988d2b2005-06-23 22:05:15 -07001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001734
1735/* lookup symbol in given range of kernel_symbols */
1736static const struct kernel_symbol *lookup_symbol(const char *name,
1737 const struct kernel_symbol *start,
1738 const struct kernel_symbol *stop)
1739{
1740 const struct kernel_symbol *ks = start;
1741 for (; ks < stop; ks++)
1742 if (strcmp(ks->name, name) == 0)
1743 return ks;
1744 return NULL;
1745}
1746
Tim Abbottca4787b2009-01-05 08:40:10 -06001747static int is_exported(const char *name, unsigned long value,
1748 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Tim Abbottca4787b2009-01-05 08:40:10 -06001750 const struct kernel_symbol *ks;
1751 if (!mod)
1752 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001753 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001754 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1755 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/* As per nm */
1759static char elf_type(const Elf_Sym *sym,
1760 Elf_Shdr *sechdrs,
1761 const char *secstrings,
1762 struct module *mod)
1763{
1764 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1765 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1766 return 'v';
1767 else
1768 return 'w';
1769 }
1770 if (sym->st_shndx == SHN_UNDEF)
1771 return 'U';
1772 if (sym->st_shndx == SHN_ABS)
1773 return 'a';
1774 if (sym->st_shndx >= SHN_LORESERVE)
1775 return '?';
1776 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1777 return 't';
1778 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1779 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1780 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1781 return 'r';
1782 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1783 return 'g';
1784 else
1785 return 'd';
1786 }
1787 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1788 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1789 return 's';
1790 else
1791 return 'b';
1792 }
1793 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1794 ".debug", strlen(".debug")) == 0)
1795 return 'n';
1796 return '?';
1797}
1798
1799static void add_kallsyms(struct module *mod,
1800 Elf_Shdr *sechdrs,
1801 unsigned int symindex,
1802 unsigned int strindex,
1803 const char *secstrings)
1804{
1805 unsigned int i;
1806
1807 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1808 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1809 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1810
1811 /* Set types up while we still have access to sections. */
1812 for (i = 0; i < mod->num_symtab; i++)
1813 mod->symtab[i].st_info
1814 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1815}
1816#else
1817static inline void add_kallsyms(struct module *mod,
1818 Elf_Shdr *sechdrs,
1819 unsigned int symindex,
1820 unsigned int strindex,
1821 const char *secstrings)
1822{
1823}
1824#endif /* CONFIG_KALLSYMS */
1825
Rusty Russell5e458cc2008-10-22 10:00:13 -05001826static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1827{
Jason Baron346e15b2008-08-12 16:46:19 -04001828#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
Rusty Russell5e458cc2008-10-22 10:00:13 -05001829 unsigned int i;
Jason Baron346e15b2008-08-12 16:46:19 -04001830
Rusty Russell5e458cc2008-10-22 10:00:13 -05001831 for (i = 0; i < num; i++) {
1832 register_dynamic_debug_module(debug[i].modname,
1833 debug[i].type,
1834 debug[i].logical_modname,
1835 debug[i].flag_names,
1836 debug[i].hash, debug[i].hash2);
Jason Baron346e15b2008-08-12 16:46:19 -04001837 }
Jason Baron346e15b2008-08-12 16:46:19 -04001838#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001839}
Jason Baron346e15b2008-08-12 16:46:19 -04001840
Rusty Russell3a642e92008-07-22 19:24:28 -05001841static void *module_alloc_update_bounds(unsigned long size)
1842{
1843 void *ret = module_alloc(size);
1844
1845 if (ret) {
1846 /* Update module bounds. */
1847 if ((unsigned long)ret < module_addr_min)
1848 module_addr_min = (unsigned long)ret;
1849 if ((unsigned long)ret + size > module_addr_max)
1850 module_addr_max = (unsigned long)ret + size;
1851 }
1852 return ret;
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/* Allocate and load the module: note that size of section 0 is always
1856 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001857static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 unsigned long len,
1859 const char __user *uargs)
1860{
1861 Elf_Ehdr *hdr;
1862 Elf_Shdr *sechdrs;
1863 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001864 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001865 unsigned int i;
1866 unsigned int symindex = 0;
1867 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001868 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001869 unsigned int num_kp, num_mcount;
1870 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct module *mod;
1872 long err = 0;
1873 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001874 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001875 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1878 umod, len, uargs);
1879 if (len < sizeof(*hdr))
1880 return ERR_PTR(-ENOEXEC);
1881
1882 /* Suck in entire file: we'll want most of it. */
1883 /* vmalloc barfs on "unusual" numbers. Check here */
1884 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1885 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001886
1887 /* Create stop_machine threads since the error path relies on
1888 * a non-failing stop_machine call. */
1889 err = stop_machine_create();
1890 if (err)
1891 goto free_hdr;
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (copy_from_user(hdr, umod, len) != 0) {
1894 err = -EFAULT;
1895 goto free_hdr;
1896 }
1897
1898 /* Sanity checks against insmoding binaries or wrong arch,
1899 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001900 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 || hdr->e_type != ET_REL
1902 || !elf_check_arch(hdr)
1903 || hdr->e_shentsize != sizeof(*sechdrs)) {
1904 err = -ENOEXEC;
1905 goto free_hdr;
1906 }
1907
1908 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1909 goto truncated;
1910
1911 /* Convenience variables */
1912 sechdrs = (void *)hdr + hdr->e_shoff;
1913 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1914 sechdrs[0].sh_addr = 0;
1915
1916 for (i = 1; i < hdr->e_shnum; i++) {
1917 if (sechdrs[i].sh_type != SHT_NOBITS
1918 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1919 goto truncated;
1920
1921 /* Mark all sections sh_addr with their address in the
1922 temporary image. */
1923 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1924
1925 /* Internal symbols and strings. */
1926 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1927 symindex = i;
1928 strindex = sechdrs[i].sh_link;
1929 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1930 }
1931#ifndef CONFIG_MODULE_UNLOAD
1932 /* Don't load .exit sections */
1933 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1934 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1935#endif
1936 }
1937
1938 modindex = find_sec(hdr, sechdrs, secstrings,
1939 ".gnu.linkonce.this_module");
1940 if (!modindex) {
1941 printk(KERN_WARNING "No module found in object\n");
1942 err = -ENOEXEC;
1943 goto free_hdr;
1944 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001945 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 mod = (void *)sechdrs[modindex].sh_addr;
1947
1948 if (symindex == 0) {
1949 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1950 mod->name);
1951 err = -ENOEXEC;
1952 goto free_hdr;
1953 }
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1956 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1957 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1958
Rusty Russellea01e792008-03-13 09:02:17 +00001959 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001961 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962#ifdef CONFIG_KALLSYMS
1963 /* Keep symbol and string tables for decoding later. */
1964 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1965 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1966#endif
1967
1968 /* Check module struct version now, before we try to use module. */
1969 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1970 err = -ENOEXEC;
1971 goto free_hdr;
1972 }
1973
1974 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1975 /* This is allowed: modprobe --force will invalidate it. */
1976 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001977 err = try_to_force_load(mod, "magic");
1978 if (err)
1979 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001980 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1982 mod->name, modmagic, vermagic);
1983 err = -ENOEXEC;
1984 goto free_hdr;
1985 }
1986
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001987 staging = get_modinfo(sechdrs, infoindex, "staging");
1988 if (staging) {
1989 add_taint_module(mod, TAINT_CRAP);
1990 printk(KERN_WARNING "%s: module is from the staging directory,"
1991 " the quality is unknown, you have been warned.\n",
1992 mod->name);
1993 }
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08001996 args = strndup_user(uargs, ~0UL >> 1);
1997 if (IS_ERR(args)) {
1998 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 goto free_hdr;
2000 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (find_module(mod->name)) {
2003 err = -EEXIST;
2004 goto free_mod;
2005 }
2006
2007 mod->state = MODULE_STATE_COMING;
2008
2009 /* Allow arches to frob section contents and sizes. */
2010 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2011 if (err < 0)
2012 goto free_mod;
2013
2014 if (pcpuindex) {
2015 /* We have a special allocation for this section. */
2016 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002017 sechdrs[pcpuindex].sh_addralign,
2018 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (!percpu) {
2020 err = -ENOMEM;
2021 goto free_mod;
2022 }
2023 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2024 mod->percpu = percpu;
2025 }
2026
2027 /* Determine total sizes, and put offsets in sh_entsize. For now
2028 this is done generically; there doesn't appear to be any
2029 special cases for the architectures. */
2030 layout_sections(mod, hdr, sechdrs, secstrings);
2031
2032 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002033 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!ptr) {
2035 err = -ENOMEM;
2036 goto free_percpu;
2037 }
2038 memset(ptr, 0, mod->core_size);
2039 mod->module_core = ptr;
2040
Rusty Russell3a642e92008-07-22 19:24:28 -05002041 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (!ptr && mod->init_size) {
2043 err = -ENOMEM;
2044 goto free_core;
2045 }
2046 memset(ptr, 0, mod->init_size);
2047 mod->module_init = ptr;
2048
2049 /* Transfer each section which specifies SHF_ALLOC */
2050 DEBUGP("final section addresses:\n");
2051 for (i = 0; i < hdr->e_shnum; i++) {
2052 void *dest;
2053
2054 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2055 continue;
2056
2057 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2058 dest = mod->module_init
2059 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2060 else
2061 dest = mod->module_core + sechdrs[i].sh_entsize;
2062
2063 if (sechdrs[i].sh_type != SHT_NOBITS)
2064 memcpy(dest, (void *)sechdrs[i].sh_addr,
2065 sechdrs[i].sh_size);
2066 /* Update sh_addr to point to copy in image. */
2067 sechdrs[i].sh_addr = (unsigned long)dest;
2068 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2069 }
2070 /* Module has been moved. */
2071 mod = (void *)sechdrs[modindex].sh_addr;
2072
2073 /* Now we've moved module, initialize linked lists, etc. */
2074 module_unload_init(mod);
2075
Kay Sievers97c146e2007-11-29 23:46:11 +01002076 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002077 err = mod_sysfs_init(mod);
2078 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002079 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* Set up license info based on the info section */
2082 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2083
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002084 /*
2085 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2086 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2087 * using GPL-only symbols it needs.
2088 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002089 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002090 add_taint(TAINT_PROPRIETARY_MODULE);
2091
2092 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002093 if (strcmp(mod->name, "driverloader") == 0)
2094 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002095
Matt Domschc988d2b2005-06-23 22:05:15 -07002096 /* Set up MODINFO_ATTR fields */
2097 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Fix up syms, so that st_value is a pointer to location. */
2100 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2101 mod);
2102 if (err < 0)
2103 goto cleanup;
2104
Rusty Russell5e458cc2008-10-22 10:00:13 -05002105 /* Now we've got everything in the final locations, we can
2106 * find optional sections. */
2107 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2108 &num_kp);
2109 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2110 sizeof(*mod->syms), &mod->num_syms);
2111 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2112 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2113 sizeof(*mod->gpl_syms),
2114 &mod->num_gpl_syms);
2115 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2116 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2117 "__ksymtab_gpl_future",
2118 sizeof(*mod->gpl_future_syms),
2119 &mod->num_gpl_future_syms);
2120 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2121 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002123#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002124 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2125 "__ksymtab_unused",
2126 sizeof(*mod->unused_syms),
2127 &mod->num_unused_syms);
2128 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2129 "__kcrctab_unused");
2130 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2131 "__ksymtab_unused_gpl",
2132 sizeof(*mod->unused_gpl_syms),
2133 &mod->num_unused_gpl_syms);
2134 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2135 "__kcrctab_unused_gpl");
2136#endif
2137
2138#ifdef CONFIG_MARKERS
2139 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2140 sizeof(*mod->markers), &mod->num_markers);
2141#endif
2142#ifdef CONFIG_TRACEPOINTS
2143 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2144 "__tracepoints",
2145 sizeof(*mod->tracepoints),
2146 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002147#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002150 if ((mod->num_syms && !mod->crcs)
2151 || (mod->num_gpl_syms && !mod->gpl_crcs)
2152 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002153#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002154 || (mod->num_unused_syms && !mod->unused_crcs)
2155 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002156#endif
2157 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002158 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2159 err = try_to_force_load(mod, "nocrc");
2160 if (err)
2161 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163#endif
2164
2165 /* Now do relocations. */
2166 for (i = 1; i < hdr->e_shnum; i++) {
2167 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2168 unsigned int info = sechdrs[i].sh_info;
2169
2170 /* Not a valid relocation section? */
2171 if (info >= hdr->e_shnum)
2172 continue;
2173
2174 /* Don't bother with non-allocated sections */
2175 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2176 continue;
2177
2178 if (sechdrs[i].sh_type == SHT_REL)
2179 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2180 else if (sechdrs[i].sh_type == SHT_RELA)
2181 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2182 mod);
2183 if (err < 0)
2184 goto cleanup;
2185 }
2186
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002187 /* Find duplicate symbols */
2188 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002189 if (err < 0)
2190 goto cleanup;
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002193 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2194 sizeof(*mod->extable), &mod->num_exentries);
2195 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 /* Finally, copy percpu area over. */
2198 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2199 sechdrs[pcpuindex].sh_size);
2200
2201 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2202
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002203 if (!mod->taints) {
Rusty Russell5e458cc2008-10-22 10:00:13 -05002204 struct mod_debug *debug;
2205 unsigned int num_debug;
2206
Rusty Russell5e458cc2008-10-22 10:00:13 -05002207 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2208 sizeof(*debug), &num_debug);
2209 dynamic_printk_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002210 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002211
Steven Rostedtfed19392008-08-14 22:47:19 -04002212 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002213 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2214 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002215 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 err = module_finalize(hdr, sechdrs, mod);
2218 if (err < 0)
2219 goto cleanup;
2220
Thomas Koeller378bac82005-09-06 15:17:11 -07002221 /* flush the icache in correct context */
2222 old_fs = get_fs();
2223 set_fs(KERNEL_DS);
2224
2225 /*
2226 * Flush the instruction cache, since we've played with text.
2227 * Do it before processing of module parameters, so the module
2228 * can provide parameter accessor functions of its own.
2229 */
2230 if (mod->module_init)
2231 flush_icache_range((unsigned long)mod->module_init,
2232 (unsigned long)mod->module_init
2233 + mod->init_size);
2234 flush_icache_range((unsigned long)mod->module_core,
2235 (unsigned long)mod->module_core + mod->core_size);
2236
2237 set_fs(old_fs);
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002240 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002241 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2242 mod->name);
2243
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002244 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002245 * info during argument parsing. Noone should access us, since
2246 * strong_try_module_get() will fail.
2247 * lockdep/oops can run asynchronous, so use the RCU list insertion
2248 * function to insert in a way safe to concurrent readers.
2249 * The mutex protects against concurrent writers.
2250 */
2251 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002252
Rusty Russell5e458cc2008-10-22 10:00:13 -05002253 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002255 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Rusty Russell5e458cc2008-10-22 10:00:13 -05002257 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002259 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002261 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 /* Get rid of temporary copy */
2264 vfree(hdr);
2265
Heiko Carstens9e018922008-12-22 12:36:31 +01002266 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* Done! */
2268 return mod;
2269
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002270 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002271 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 module_arch_cleanup(mod);
2273 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002274 kobject_del(&mod->mkobj.kobj);
2275 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002276 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002277 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 module_unload_free(mod);
2279 module_free(mod, mod->module_init);
2280 free_core:
2281 module_free(mod, mod->module_core);
2282 free_percpu:
2283 if (percpu)
2284 percpu_modfree(percpu);
2285 free_mod:
2286 kfree(args);
2287 free_hdr:
2288 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002289 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002290 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 truncated:
2293 printk(KERN_ERR "Module len %lu truncated\n", len);
2294 err = -ENOEXEC;
2295 goto free_hdr;
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002299SYSCALL_DEFINE3(init_module, void __user *, umod,
2300 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
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