blob: 52b3497b8748e9a6bdc14ecd135701521af4af07 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int percpu_modinit(void)
484{
485 pcpu_num_used = 2;
486 pcpu_num_allocated = 2;
487 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
488 GFP_KERNEL);
489 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200490 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Free room. */
492 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
493 if (pcpu_size[1] < 0) {
494 printk(KERN_ERR "No per-cpu room for modules.\n");
495 pcpu_num_used = 1;
496 }
497
498 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900501
502static unsigned int find_pcpusec(Elf_Ehdr *hdr,
503 Elf_Shdr *sechdrs,
504 const char *secstrings)
505{
506 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
507}
508
509static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
510{
511 int cpu;
512
513 for_each_possible_cpu(cpu)
514 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900518
Rusty Russell842bbaa2005-08-01 21:11:47 -0700519static inline void *percpu_modalloc(unsigned long size, unsigned long align,
520 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 return NULL;
523}
524static inline void percpu_modfree(void *pcpuptr)
525{
526 BUG();
527}
528static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
529 Elf_Shdr *sechdrs,
530 const char *secstrings)
531{
532 return 0;
533}
534static inline void percpu_modcopy(void *pcpudst, const void *src,
535 unsigned long size)
536{
537 /* pcpusec should be 0, and size of that section should be 0. */
538 BUG_ON(size != 0);
539}
Tejun Heo6b588c12009-02-20 16:29:07 +0900540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif /* CONFIG_SMP */
542
Matt Domschc988d2b2005-06-23 22:05:15 -0700543#define MODINFO_ATTR(field) \
544static void setup_modinfo_##field(struct module *mod, const char *s) \
545{ \
546 mod->field = kstrdup(s, GFP_KERNEL); \
547} \
548static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
549 struct module *mod, char *buffer) \
550{ \
551 return sprintf(buffer, "%s\n", mod->field); \
552} \
553static int modinfo_##field##_exists(struct module *mod) \
554{ \
555 return mod->field != NULL; \
556} \
557static void free_modinfo_##field(struct module *mod) \
558{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700559 kfree(mod->field); \
560 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700561} \
562static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900563 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700564 .show = show_modinfo_##field, \
565 .setup = setup_modinfo_##field, \
566 .test = modinfo_##field##_exists, \
567 .free = free_modinfo_##field, \
568};
569
570MODINFO_ATTR(version);
571MODINFO_ATTR(srcversion);
572
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100573static char last_unloaded_module[MODULE_NAME_LEN+1];
574
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800575#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/* Init the unload section of the module. */
577static void module_unload_init(struct module *mod)
578{
Eric Dumazet720eba32009-02-03 13:31:36 +1030579 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030582 for_each_possible_cpu(cpu)
583 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030585 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Backwards compatibility macros put refcount during init. */
587 mod->waiter = current;
588}
589
590/* modules using other modules */
591struct module_use
592{
593 struct list_head list;
594 struct module *module_which_uses;
595};
596
597/* Does a already use b? */
598static int already_uses(struct module *a, struct module *b)
599{
600 struct module_use *use;
601
602 list_for_each_entry(use, &b->modules_which_use_me, list) {
603 if (use->module_which_uses == a) {
604 DEBUGP("%s uses %s!\n", a->name, b->name);
605 return 1;
606 }
607 }
608 DEBUGP("%s does not use %s!\n", a->name, b->name);
609 return 0;
610}
611
612/* Module a uses b */
613static int use_module(struct module *a, struct module *b)
614{
615 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500616 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (b == NULL || already_uses(a, b)) return 1;
619
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500620 /* If we're interrupted or time out, we fail. */
621 if (wait_event_interruptible_timeout(
622 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
623 30 * HZ) <= 0) {
624 printk("%s: gave up waiting for init of module %s.\n",
625 a->name, b->name);
626 return 0;
627 }
628
629 /* If strong_try_module_get() returned a different error, we fail. */
630 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
632
633 DEBUGP("Allocating new usage for %s.\n", a->name);
634 use = kmalloc(sizeof(*use), GFP_ATOMIC);
635 if (!use) {
636 printk("%s: out of memory loading\n", a->name);
637 module_put(b);
638 return 0;
639 }
640
641 use->module_which_uses = a;
642 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100643 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 1;
645}
646
647/* Clear the unload stuff of the module. */
648static void module_unload_free(struct module *mod)
649{
650 struct module *i;
651
652 list_for_each_entry(i, &modules, list) {
653 struct module_use *use;
654
655 list_for_each_entry(use, &i->modules_which_use_me, list) {
656 if (use->module_which_uses == mod) {
657 DEBUGP("%s unusing %s\n", mod->name, i->name);
658 module_put(i);
659 list_del(&use->list);
660 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100661 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* There can be at most one match. */
663 break;
664 }
665 }
666 }
667}
668
669#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800670static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int ret = (flags & O_TRUNC);
673 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800674 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return ret;
676}
677#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800678static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 return 0;
681}
682#endif /* CONFIG_MODULE_FORCE_UNLOAD */
683
684struct stopref
685{
686 struct module *mod;
687 int flags;
688 int *forced;
689};
690
691/* Whole machine is stopped with interrupts off when this runs. */
692static int __try_stop_module(void *_sref)
693{
694 struct stopref *sref = _sref;
695
Rusty Russellda39ba52008-07-22 19:24:25 -0500696 /* If it's not unused, quit unless we're forcing. */
697 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800698 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EWOULDBLOCK;
700 }
701
702 /* Mark it as dying. */
703 sref->mod->state = MODULE_STATE_GOING;
704 return 0;
705}
706
707static int try_stop_module(struct module *mod, int flags, int *forced)
708{
Rusty Russellda39ba52008-07-22 19:24:25 -0500709 if (flags & O_NONBLOCK) {
710 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500712 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500713 } else {
714 /* We don't need to stop the machine for this. */
715 mod->state = MODULE_STATE_GOING;
716 synchronize_sched();
717 return 0;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721unsigned int module_refcount(struct module *mod)
722{
Eric Dumazet720eba32009-02-03 13:31:36 +1030723 unsigned int total = 0;
724 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Eric Dumazet720eba32009-02-03 13:31:36 +1030726 for_each_possible_cpu(cpu)
727 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return total;
729}
730EXPORT_SYMBOL(module_refcount);
731
732/* This exists whether we can unload or not */
733static void free_module(struct module *mod);
734
735static void wait_for_zero_refcount(struct module *mod)
736{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500737 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800738 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 for (;;) {
740 DEBUGP("Looking at refcount...\n");
741 set_current_state(TASK_UNINTERRUPTIBLE);
742 if (module_refcount(mod) == 0)
743 break;
744 schedule();
745 }
746 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800747 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100750SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
751 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800754 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 int ret, forced = 0;
756
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800757 if (!capable(CAP_SYS_MODULE))
758 return -EPERM;
759
760 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
761 return -EFAULT;
762 name[MODULE_NAME_LEN-1] = '\0';
763
Heiko Carstens9e018922008-12-22 12:36:31 +0100764 /* Create stop_machine threads since free_module relies on
765 * a non-failing stop_machine call. */
766 ret = stop_machine_create();
767 if (ret)
768 return ret;
769
770 if (mutex_lock_interruptible(&module_mutex) != 0) {
771 ret = -EINTR;
772 goto out_stop;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 mod = find_module(name);
776 if (!mod) {
777 ret = -ENOENT;
778 goto out;
779 }
780
781 if (!list_empty(&mod->modules_which_use_me)) {
782 /* Other modules depend on us: get rid of them first. */
783 ret = -EWOULDBLOCK;
784 goto out;
785 }
786
787 /* Doing init or already dying? */
788 if (mod->state != MODULE_STATE_LIVE) {
789 /* FIXME: if (force), slam module count and wake up
790 waiter --RR */
791 DEBUGP("%s already dying\n", mod->name);
792 ret = -EBUSY;
793 goto out;
794 }
795
796 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700797 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800798 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!forced) {
800 /* This module can't be removed */
801 ret = -EBUSY;
802 goto out;
803 }
804 }
805
806 /* Set this up before setting mod->state */
807 mod->waiter = current;
808
809 /* Stop the machine so refcounts can't move and disable module. */
810 ret = try_stop_module(mod, flags, &forced);
811 if (ret != 0)
812 goto out;
813
814 /* Never wait if forced. */
815 if (!forced && module_refcount(mod) != 0)
816 wait_for_zero_refcount(mod);
817
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200818 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200820 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200822 blocking_notifier_call_chain(&module_notify_list,
823 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800824 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200825 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100826 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500827 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Baron346e15b2008-08-12 16:46:19 -0400828 unregister_dynamic_debug_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 free_module(mod);
830
831 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800832 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100833out_stop:
834 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
836}
837
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800838static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct module_use *use;
841 int printed_something = 0;
842
843 seq_printf(m, " %u ", module_refcount(mod));
844
845 /* Always include a trailing , so userspace can differentiate
846 between this and the old multi-field proc format. */
847 list_for_each_entry(use, &mod->modules_which_use_me, list) {
848 printed_something = 1;
849 seq_printf(m, "%s,", use->module_which_uses->name);
850 }
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (mod->init != NULL && mod->exit == NULL) {
853 printed_something = 1;
854 seq_printf(m, "[permanent],");
855 }
856
857 if (!printed_something)
858 seq_printf(m, "-");
859}
860
861void __symbol_put(const char *symbol)
862{
863 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Rusty Russell24da1cb2007-07-15 23:41:46 -0700865 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -0500866 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 BUG();
868 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700869 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871EXPORT_SYMBOL(__symbol_put);
872
873void symbol_put_addr(void *addr)
874{
Trent Piepho5e376612006-05-15 09:44:06 -0700875 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Trent Piepho5e376612006-05-15 09:44:06 -0700877 if (core_kernel_text((unsigned long)addr))
878 return;
879
880 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700882 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884EXPORT_SYMBOL_GPL(symbol_put_addr);
885
886static ssize_t show_refcnt(struct module_attribute *mattr,
887 struct module *mod, char *buffer)
888{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400889 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900893 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .show = show_refcnt,
895};
896
Al Virof6a57032006-10-18 01:47:25 -0400897void module_put(struct module *module)
898{
899 if (module) {
900 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030901 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400902 /* Maybe they're waiting for us to drop reference? */
903 if (unlikely(!module_is_live(module)))
904 wake_up_process(module->waiter);
905 put_cpu();
906 }
907}
908EXPORT_SYMBOL(module_put);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800911static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 /* We don't know the usage count, or what modules are using. */
914 seq_printf(m, " - -");
915}
916
917static inline void module_unload_free(struct module *mod)
918{
919}
920
921static inline int use_module(struct module *a, struct module *b)
922{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500923 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926static inline void module_unload_init(struct module *mod)
927{
928}
929#endif /* CONFIG_MODULE_UNLOAD */
930
Kay Sievers1f717402006-11-24 12:15:25 +0100931static ssize_t show_initstate(struct module_attribute *mattr,
932 struct module *mod, char *buffer)
933{
934 const char *state = "unknown";
935
936 switch (mod->state) {
937 case MODULE_STATE_LIVE:
938 state = "live";
939 break;
940 case MODULE_STATE_COMING:
941 state = "coming";
942 break;
943 case MODULE_STATE_GOING:
944 state = "going";
945 break;
946 }
947 return sprintf(buffer, "%s\n", state);
948}
949
950static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900951 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100952 .show = show_initstate,
953};
954
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800955static struct module_attribute *modinfo_attrs[] = {
956 &modinfo_version,
957 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100958 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800959#ifdef CONFIG_MODULE_UNLOAD
960 &refcnt,
961#endif
962 NULL,
963};
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static const char vermagic[] = VERMAGIC_STRING;
966
Linus Torvalds826e4502008-05-04 17:04:16 -0700967static int try_to_force_load(struct module *mod, const char *symname)
968{
969#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700970 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700971 printk("%s: no version for \"%s\" found: kernel tainted.\n",
972 mod->name, symname);
973 add_taint_module(mod, TAINT_FORCED_MODULE);
974 return 0;
975#else
976 return -ENOEXEC;
977#endif
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#ifdef CONFIG_MODVERSIONS
981static int check_version(Elf_Shdr *sechdrs,
982 unsigned int versindex,
983 const char *symname,
984 struct module *mod,
985 const unsigned long *crc)
986{
987 unsigned int i, num_versions;
988 struct modversion_info *versions;
989
990 /* Exporting module didn't supply crcs? OK, we're already tainted. */
991 if (!crc)
992 return 1;
993
Rusty Russella5dd6972008-05-09 16:24:21 +1000994 /* No versions at all? modprobe --force does this. */
995 if (versindex == 0)
996 return try_to_force_load(mod, symname) == 0;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 versions = (void *) sechdrs[versindex].sh_addr;
999 num_versions = sechdrs[versindex].sh_size
1000 / sizeof(struct modversion_info);
1001
1002 for (i = 0; i < num_versions; i++) {
1003 if (strcmp(versions[i].name, symname) != 0)
1004 continue;
1005
1006 if (versions[i].crc == *crc)
1007 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 DEBUGP("Found checksum %lX vs module %lX\n",
1009 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001010 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001012
Rusty Russella5dd6972008-05-09 16:24:21 +10001013 printk(KERN_WARNING "%s: no symbol version for %s\n",
1014 mod->name, symname);
1015 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001016
1017bad_version:
1018 printk("%s: disagrees about version of symbol %s\n",
1019 mod->name, symname);
1020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1024 unsigned int versindex,
1025 struct module *mod)
1026{
1027 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Rusty Russellad9546c2008-05-01 21:14:59 -05001029 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001031 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Rusty Russell91e37a72008-05-09 16:25:28 +10001034/* First part is kernel version, which we ignore if module has crcs. */
1035static inline int same_magic(const char *amagic, const char *bmagic,
1036 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Rusty Russell91e37a72008-05-09 16:25:28 +10001038 if (has_crcs) {
1039 amagic += strcspn(amagic, " ");
1040 bmagic += strcspn(bmagic, " ");
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return strcmp(amagic, bmagic) == 0;
1043}
1044#else
1045static inline int check_version(Elf_Shdr *sechdrs,
1046 unsigned int versindex,
1047 const char *symname,
1048 struct module *mod,
1049 const unsigned long *crc)
1050{
1051 return 1;
1052}
1053
1054static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1055 unsigned int versindex,
1056 struct module *mod)
1057{
1058 return 1;
1059}
1060
Rusty Russell91e37a72008-05-09 16:25:28 +10001061static inline int same_magic(const char *amagic, const char *bmagic,
1062 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 return strcmp(amagic, bmagic) == 0;
1065}
1066#endif /* CONFIG_MODVERSIONS */
1067
1068/* Resolve a symbol for this module. I.e. if we find one, record usage.
1069 Must be holding module_mutex. */
1070static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1071 unsigned int versindex,
1072 const char *name,
1073 struct module *mod)
1074{
1075 struct module *owner;
1076 unsigned long ret;
1077 const unsigned long *crc;
1078
Rusty Russellad9546c2008-05-01 21:14:59 -05001079 ret = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001080 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Christoph Lameter88173502008-02-08 04:18:41 -08001081 if (!IS_ERR_VALUE(ret)) {
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001082 /* use_module can fail due to OOM,
1083 or module initialization or unloading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1085 !use_module(mod, owner))
Christoph Lameter88173502008-02-08 04:18:41 -08001086 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return ret;
1089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/*
1092 * /sys/module/foo/sections stuff
1093 * J. Corbet <corbet@lwn.net>
1094 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001095#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001096struct module_sect_attr
1097{
1098 struct module_attribute mattr;
1099 char *name;
1100 unsigned long address;
1101};
1102
1103struct module_sect_attrs
1104{
1105 struct attribute_group grp;
1106 unsigned int nsections;
1107 struct module_sect_attr attrs[0];
1108};
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110static ssize_t module_sect_show(struct module_attribute *mattr,
1111 struct module *mod, char *buf)
1112{
1113 struct module_sect_attr *sattr =
1114 container_of(mattr, struct module_sect_attr, mattr);
1115 return sprintf(buf, "0x%lx\n", sattr->address);
1116}
1117
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001118static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1119{
Rusty Russella58730c2008-03-13 09:03:44 +00001120 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001121
1122 for (section = 0; section < sect_attrs->nsections; section++)
1123 kfree(sect_attrs->attrs[section].name);
1124 kfree(sect_attrs);
1125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127static void add_sect_attrs(struct module *mod, unsigned int nsect,
1128 char *secstrings, Elf_Shdr *sechdrs)
1129{
1130 unsigned int nloaded = 0, i, size[2];
1131 struct module_sect_attrs *sect_attrs;
1132 struct module_sect_attr *sattr;
1133 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /* Count loaded sections and allocate structures */
1136 for (i = 0; i < nsect; i++)
1137 if (sechdrs[i].sh_flags & SHF_ALLOC)
1138 nloaded++;
1139 size[0] = ALIGN(sizeof(*sect_attrs)
1140 + nloaded * sizeof(sect_attrs->attrs[0]),
1141 sizeof(sect_attrs->grp.attrs[0]));
1142 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001143 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1144 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return;
1146
1147 /* Setup section attributes. */
1148 sect_attrs->grp.name = "sections";
1149 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1150
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001151 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 sattr = &sect_attrs->attrs[0];
1153 gattr = &sect_attrs->grp.attrs[0];
1154 for (i = 0; i < nsect; i++) {
1155 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1156 continue;
1157 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001158 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1159 GFP_KERNEL);
1160 if (sattr->name == NULL)
1161 goto out;
1162 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 sattr->mattr.show = module_sect_show;
1164 sattr->mattr.store = NULL;
1165 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 sattr->mattr.attr.mode = S_IRUGO;
1167 *(gattr++) = &(sattr++)->mattr.attr;
1168 }
1169 *gattr = NULL;
1170
1171 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1172 goto out;
1173
1174 mod->sect_attrs = sect_attrs;
1175 return;
1176 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001177 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180static void remove_sect_attrs(struct module *mod)
1181{
1182 if (mod->sect_attrs) {
1183 sysfs_remove_group(&mod->mkobj.kobj,
1184 &mod->sect_attrs->grp);
1185 /* We are positive that no one is using any sect attrs
1186 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001187 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 mod->sect_attrs = NULL;
1189 }
1190}
1191
Roland McGrath6d760132007-10-16 23:26:40 -07001192/*
1193 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1194 */
1195
1196struct module_notes_attrs {
1197 struct kobject *dir;
1198 unsigned int notes;
1199 struct bin_attribute attrs[0];
1200};
1201
1202static ssize_t module_notes_read(struct kobject *kobj,
1203 struct bin_attribute *bin_attr,
1204 char *buf, loff_t pos, size_t count)
1205{
1206 /*
1207 * The caller checked the pos and count against our size.
1208 */
1209 memcpy(buf, bin_attr->private + pos, count);
1210 return count;
1211}
1212
1213static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1214 unsigned int i)
1215{
1216 if (notes_attrs->dir) {
1217 while (i-- > 0)
1218 sysfs_remove_bin_file(notes_attrs->dir,
1219 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001220 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001221 }
1222 kfree(notes_attrs);
1223}
1224
1225static void add_notes_attrs(struct module *mod, unsigned int nsect,
1226 char *secstrings, Elf_Shdr *sechdrs)
1227{
1228 unsigned int notes, loaded, i;
1229 struct module_notes_attrs *notes_attrs;
1230 struct bin_attribute *nattr;
1231
1232 /* Count notes sections and allocate structures. */
1233 notes = 0;
1234 for (i = 0; i < nsect; i++)
1235 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1236 (sechdrs[i].sh_type == SHT_NOTE))
1237 ++notes;
1238
1239 if (notes == 0)
1240 return;
1241
1242 notes_attrs = kzalloc(sizeof(*notes_attrs)
1243 + notes * sizeof(notes_attrs->attrs[0]),
1244 GFP_KERNEL);
1245 if (notes_attrs == NULL)
1246 return;
1247
1248 notes_attrs->notes = notes;
1249 nattr = &notes_attrs->attrs[0];
1250 for (loaded = i = 0; i < nsect; ++i) {
1251 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1252 continue;
1253 if (sechdrs[i].sh_type == SHT_NOTE) {
1254 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1255 nattr->attr.mode = S_IRUGO;
1256 nattr->size = sechdrs[i].sh_size;
1257 nattr->private = (void *) sechdrs[i].sh_addr;
1258 nattr->read = module_notes_read;
1259 ++nattr;
1260 }
1261 ++loaded;
1262 }
1263
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001264 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001265 if (!notes_attrs->dir)
1266 goto out;
1267
1268 for (i = 0; i < notes; ++i)
1269 if (sysfs_create_bin_file(notes_attrs->dir,
1270 &notes_attrs->attrs[i]))
1271 goto out;
1272
1273 mod->notes_attrs = notes_attrs;
1274 return;
1275
1276 out:
1277 free_notes_attrs(notes_attrs, i);
1278}
1279
1280static void remove_notes_attrs(struct module *mod)
1281{
1282 if (mod->notes_attrs)
1283 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1289 char *sectstrings, Elf_Shdr *sechdrs)
1290{
1291}
1292
1293static inline void remove_sect_attrs(struct module *mod)
1294{
1295}
Roland McGrath6d760132007-10-16 23:26:40 -07001296
1297static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1298 char *sectstrings, Elf_Shdr *sechdrs)
1299{
1300}
1301
1302static inline void remove_notes_attrs(struct module *mod)
1303{
1304}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001305#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Randy Dunlapef665c12007-02-13 15:19:06 -08001307#ifdef CONFIG_SYSFS
1308int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001309{
1310 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001311 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001312 int error = 0;
1313 int i;
1314
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001315 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1316 (ARRAY_SIZE(modinfo_attrs) + 1)),
1317 GFP_KERNEL);
1318 if (!mod->modinfo_attrs)
1319 return -ENOMEM;
1320
1321 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001322 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1323 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001324 (attr->test && attr->test(mod))) {
1325 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001326 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1327 ++temp_attr;
1328 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001329 }
1330 return error;
1331}
1332
Randy Dunlapef665c12007-02-13 15:19:06 -08001333void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001334{
1335 struct module_attribute *attr;
1336 int i;
1337
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001338 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1339 /* pick a field to test for end of list */
1340 if (!attr->attr.name)
1341 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001342 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001343 if (attr->free)
1344 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001345 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001346 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Randy Dunlapef665c12007-02-13 15:19:06 -08001349int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001352 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001354 if (!module_sysfs_initialized) {
1355 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001356 mod->name);
1357 err = -EINVAL;
1358 goto out;
1359 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001360
1361 kobj = kset_find_obj(module_kset, mod->name);
1362 if (kobj) {
1363 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1364 kobject_put(kobj);
1365 err = -EINVAL;
1366 goto out;
1367 }
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001370
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001371 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1372 mod->mkobj.kobj.kset = module_kset;
1373 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1374 "%s", mod->name);
1375 if (err)
1376 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001377
Kay Sievers97c146e2007-11-29 23:46:11 +01001378 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001379out:
1380 return err;
1381}
1382
Randy Dunlapef665c12007-02-13 15:19:06 -08001383int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001384 struct kernel_param *kparam,
1385 unsigned int num_params)
1386{
1387 int err;
1388
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001389 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001390 if (!mod->holders_dir) {
1391 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001392 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001393 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 err = module_param_sysfs_setup(mod, kparam, num_params);
1396 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001397 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Matt Domschc988d2b2005-06-23 22:05:15 -07001399 err = module_add_modinfo_attrs(mod);
1400 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001401 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001402
Kay Sieverse17e0f52006-11-24 12:15:25 +01001403 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return 0;
1405
Kay Sieverse17e0f52006-11-24 12:15:25 +01001406out_unreg_param:
1407 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001408out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001409 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001410out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001411 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return err;
1413}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001414
1415static void mod_sysfs_fini(struct module *mod)
1416{
1417 kobject_put(&mod->mkobj.kobj);
1418}
1419
1420#else /* CONFIG_SYSFS */
1421
1422static void mod_sysfs_fini(struct module *mod)
1423{
1424}
1425
1426#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428static void mod_kobject_remove(struct module *mod)
1429{
Matt Domschc988d2b2005-06-23 22:05:15 -07001430 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001432 kobject_put(mod->mkobj.drivers_dir);
1433 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001434 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437/*
1438 * unlink the module with the whole machine is stopped with interrupts off
1439 * - this defends against kallsyms not taking locks
1440 */
1441static int __unlink_module(void *_mod)
1442{
1443 struct module *mod = _mod;
1444 list_del(&mod->list);
1445 return 0;
1446}
1447
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001448/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449static void free_module(struct module *mod)
1450{
1451 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001452 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001453 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 remove_sect_attrs(mod);
1455 mod_kobject_remove(mod);
1456
1457 /* Arch-specific cleanup. */
1458 module_arch_cleanup(mod);
1459
1460 /* Module unload stuff */
1461 module_unload_free(mod);
1462
Steven Rostedtfed19392008-08-14 22:47:19 -04001463 /* release any pointers to mcount in this module */
1464 ftrace_release(mod->module_core, mod->core_size);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* This may be NULL, but that's OK */
1467 module_free(mod, mod->module_init);
1468 kfree(mod->args);
1469 if (mod->percpu)
1470 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301471#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1472 if (mod->refptr)
1473 percpu_modfree(mod->refptr);
1474#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001475 /* Free lock-classes: */
1476 lockdep_free_key_range(mod->module_core, mod->core_size);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* Finally, free the core (containing the module structure) */
1479 module_free(mod, mod->module_core);
1480}
1481
1482void *__symbol_get(const char *symbol)
1483{
1484 struct module *owner;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001485 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Rusty Russell24da1cb2007-07-15 23:41:46 -07001487 preempt_disable();
Rusty Russellad9546c2008-05-01 21:14:59 -05001488 value = find_symbol(symbol, &owner, NULL, true, true);
Christoph Lameter88173502008-02-08 04:18:41 -08001489 if (IS_ERR_VALUE(value))
1490 value = 0;
1491 else if (strong_try_module_get(owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 value = 0;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001493 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 return (void *)value;
1496}
1497EXPORT_SYMBOL_GPL(__symbol_get);
1498
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001499/*
1500 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001501 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001502 */
1503static int verify_export_symbols(struct module *mod)
1504{
Rusty Russellb2111042008-05-01 21:15:00 -05001505 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001506 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001507 const struct kernel_symbol *s;
1508 struct {
1509 const struct kernel_symbol *sym;
1510 unsigned int num;
1511 } arr[] = {
1512 { mod->syms, mod->num_syms },
1513 { mod->gpl_syms, mod->num_gpl_syms },
1514 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001515#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001516 { mod->unused_syms, mod->num_unused_syms },
1517 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001518#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001519 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001520
Rusty Russellb2111042008-05-01 21:15:00 -05001521 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1522 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1523 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1524 NULL, true, false))) {
1525 printk(KERN_ERR
1526 "%s: exports duplicate symbol %s"
1527 " (owned by %s)\n",
1528 mod->name, s->name, module_name(owner));
1529 return -ENOEXEC;
1530 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001531 }
Rusty Russellb2111042008-05-01 21:15:00 -05001532 }
1533 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001534}
1535
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001536/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537static int simplify_symbols(Elf_Shdr *sechdrs,
1538 unsigned int symindex,
1539 const char *strtab,
1540 unsigned int versindex,
1541 unsigned int pcpuindex,
1542 struct module *mod)
1543{
1544 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1545 unsigned long secbase;
1546 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1547 int ret = 0;
1548
1549 for (i = 1; i < n; i++) {
1550 switch (sym[i].st_shndx) {
1551 case SHN_COMMON:
1552 /* We compiled with -fno-common. These are not
1553 supposed to happen. */
1554 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1555 printk("%s: please compile with -fno-common\n",
1556 mod->name);
1557 ret = -ENOEXEC;
1558 break;
1559
1560 case SHN_ABS:
1561 /* Don't need to do anything */
1562 DEBUGP("Absolute symbol: 0x%08lx\n",
1563 (long)sym[i].st_value);
1564 break;
1565
1566 case SHN_UNDEF:
1567 sym[i].st_value
1568 = resolve_symbol(sechdrs, versindex,
1569 strtab + sym[i].st_name, mod);
1570
1571 /* Ok if resolved. */
Christoph Lameter88173502008-02-08 04:18:41 -08001572 if (!IS_ERR_VALUE(sym[i].st_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 break;
1574 /* Ok if weak. */
1575 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1576 break;
1577
1578 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1579 mod->name, strtab + sym[i].st_name);
1580 ret = -ENOENT;
1581 break;
1582
1583 default:
1584 /* Divert to percpu allocation if a percpu var. */
1585 if (sym[i].st_shndx == pcpuindex)
1586 secbase = (unsigned long)mod->percpu;
1587 else
1588 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1589 sym[i].st_value += secbase;
1590 break;
1591 }
1592 }
1593
1594 return ret;
1595}
1596
Helge Deller088af9a2008-12-31 12:31:18 +01001597/* Additional bytes needed by arch in front of individual sections */
1598unsigned int __weak arch_mod_section_prepend(struct module *mod,
1599 unsigned int section)
1600{
1601 /* default implementation just returns zero */
1602 return 0;
1603}
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001606static long get_offset(struct module *mod, unsigned int *size,
1607 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 long ret;
1610
Helge Deller088af9a2008-12-31 12:31:18 +01001611 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1613 *size = ret + sechdr->sh_size;
1614 return ret;
1615}
1616
1617/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1618 might -- code, read-only data, read-write data, small data. Tally
1619 sizes, and place the offsets into sh_entsize fields: high bit means it
1620 belongs in init. */
1621static void layout_sections(struct module *mod,
1622 const Elf_Ehdr *hdr,
1623 Elf_Shdr *sechdrs,
1624 const char *secstrings)
1625{
1626 static unsigned long const masks[][2] = {
1627 /* NOTE: all executable code must be the first section
1628 * in this array; otherwise modify the text_size
1629 * finder in the two loops below */
1630 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1631 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1632 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1633 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1634 };
1635 unsigned int m, i;
1636
1637 for (i = 0; i < hdr->e_shnum; i++)
1638 sechdrs[i].sh_entsize = ~0UL;
1639
1640 DEBUGP("Core section allocation order:\n");
1641 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1642 for (i = 0; i < hdr->e_shnum; ++i) {
1643 Elf_Shdr *s = &sechdrs[i];
1644
1645 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1646 || (s->sh_flags & masks[m][1])
1647 || s->sh_entsize != ~0UL
1648 || strncmp(secstrings + s->sh_name,
1649 ".init", 5) == 0)
1650 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001651 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 DEBUGP("\t%s\n", secstrings + s->sh_name);
1653 }
1654 if (m == 0)
1655 mod->core_text_size = mod->core_size;
1656 }
1657
1658 DEBUGP("Init section allocation order:\n");
1659 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1660 for (i = 0; i < hdr->e_shnum; ++i) {
1661 Elf_Shdr *s = &sechdrs[i];
1662
1663 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1664 || (s->sh_flags & masks[m][1])
1665 || s->sh_entsize != ~0UL
1666 || strncmp(secstrings + s->sh_name,
1667 ".init", 5) != 0)
1668 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001669 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 | INIT_OFFSET_MASK);
1671 DEBUGP("\t%s\n", secstrings + s->sh_name);
1672 }
1673 if (m == 0)
1674 mod->init_text_size = mod->init_size;
1675 }
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678static void set_license(struct module *mod, const char *license)
1679{
1680 if (!license)
1681 license = "unspecified";
1682
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001683 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001684 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001685 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001686 "kernel.\n", mod->name, license);
1687 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689}
1690
1691/* Parse tag=value strings from .modinfo section */
1692static char *next_string(char *string, unsigned long *secsize)
1693{
1694 /* Skip non-zero chars */
1695 while (string[0]) {
1696 string++;
1697 if ((*secsize)-- <= 1)
1698 return NULL;
1699 }
1700
1701 /* Skip any zero padding. */
1702 while (!string[0]) {
1703 string++;
1704 if ((*secsize)-- <= 1)
1705 return NULL;
1706 }
1707 return string;
1708}
1709
1710static char *get_modinfo(Elf_Shdr *sechdrs,
1711 unsigned int info,
1712 const char *tag)
1713{
1714 char *p;
1715 unsigned int taglen = strlen(tag);
1716 unsigned long size = sechdrs[info].sh_size;
1717
1718 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1719 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1720 return p + taglen + 1;
1721 }
1722 return NULL;
1723}
1724
Matt Domschc988d2b2005-06-23 22:05:15 -07001725static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1726 unsigned int infoindex)
1727{
1728 struct module_attribute *attr;
1729 int i;
1730
1731 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1732 if (attr->setup)
1733 attr->setup(mod,
1734 get_modinfo(sechdrs,
1735 infoindex,
1736 attr->attr.name));
1737 }
1738}
Matt Domschc988d2b2005-06-23 22:05:15 -07001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001741
1742/* lookup symbol in given range of kernel_symbols */
1743static const struct kernel_symbol *lookup_symbol(const char *name,
1744 const struct kernel_symbol *start,
1745 const struct kernel_symbol *stop)
1746{
1747 const struct kernel_symbol *ks = start;
1748 for (; ks < stop; ks++)
1749 if (strcmp(ks->name, name) == 0)
1750 return ks;
1751 return NULL;
1752}
1753
Tim Abbottca4787b2009-01-05 08:40:10 -06001754static int is_exported(const char *name, unsigned long value,
1755 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Tim Abbottca4787b2009-01-05 08:40:10 -06001757 const struct kernel_symbol *ks;
1758 if (!mod)
1759 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001760 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001761 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1762 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/* As per nm */
1766static char elf_type(const Elf_Sym *sym,
1767 Elf_Shdr *sechdrs,
1768 const char *secstrings,
1769 struct module *mod)
1770{
1771 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1772 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1773 return 'v';
1774 else
1775 return 'w';
1776 }
1777 if (sym->st_shndx == SHN_UNDEF)
1778 return 'U';
1779 if (sym->st_shndx == SHN_ABS)
1780 return 'a';
1781 if (sym->st_shndx >= SHN_LORESERVE)
1782 return '?';
1783 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1784 return 't';
1785 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1786 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1787 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1788 return 'r';
1789 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1790 return 'g';
1791 else
1792 return 'd';
1793 }
1794 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1795 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1796 return 's';
1797 else
1798 return 'b';
1799 }
1800 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1801 ".debug", strlen(".debug")) == 0)
1802 return 'n';
1803 return '?';
1804}
1805
1806static void add_kallsyms(struct module *mod,
1807 Elf_Shdr *sechdrs,
1808 unsigned int symindex,
1809 unsigned int strindex,
1810 const char *secstrings)
1811{
1812 unsigned int i;
1813
1814 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1815 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1816 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1817
1818 /* Set types up while we still have access to sections. */
1819 for (i = 0; i < mod->num_symtab; i++)
1820 mod->symtab[i].st_info
1821 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1822}
1823#else
1824static inline void add_kallsyms(struct module *mod,
1825 Elf_Shdr *sechdrs,
1826 unsigned int symindex,
1827 unsigned int strindex,
1828 const char *secstrings)
1829{
1830}
1831#endif /* CONFIG_KALLSYMS */
1832
Rusty Russell5e458cc2008-10-22 10:00:13 -05001833static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1834{
Jason Baron346e15b2008-08-12 16:46:19 -04001835#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
Rusty Russell5e458cc2008-10-22 10:00:13 -05001836 unsigned int i;
Jason Baron346e15b2008-08-12 16:46:19 -04001837
Rusty Russell5e458cc2008-10-22 10:00:13 -05001838 for (i = 0; i < num; i++) {
1839 register_dynamic_debug_module(debug[i].modname,
1840 debug[i].type,
1841 debug[i].logical_modname,
1842 debug[i].flag_names,
1843 debug[i].hash, debug[i].hash2);
Jason Baron346e15b2008-08-12 16:46:19 -04001844 }
Jason Baron346e15b2008-08-12 16:46:19 -04001845#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001846}
Jason Baron346e15b2008-08-12 16:46:19 -04001847
Rusty Russell3a642e92008-07-22 19:24:28 -05001848static void *module_alloc_update_bounds(unsigned long size)
1849{
1850 void *ret = module_alloc(size);
1851
1852 if (ret) {
1853 /* Update module bounds. */
1854 if ((unsigned long)ret < module_addr_min)
1855 module_addr_min = (unsigned long)ret;
1856 if ((unsigned long)ret + size > module_addr_max)
1857 module_addr_max = (unsigned long)ret + size;
1858 }
1859 return ret;
1860}
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862/* Allocate and load the module: note that size of section 0 is always
1863 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001864static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 unsigned long len,
1866 const char __user *uargs)
1867{
1868 Elf_Ehdr *hdr;
1869 Elf_Shdr *sechdrs;
1870 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001871 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001872 unsigned int i;
1873 unsigned int symindex = 0;
1874 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001875 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001876 unsigned int num_kp, num_mcount;
1877 struct kernel_param *kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 struct module *mod;
1879 long err = 0;
1880 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001881 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001882 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1885 umod, len, uargs);
1886 if (len < sizeof(*hdr))
1887 return ERR_PTR(-ENOEXEC);
1888
1889 /* Suck in entire file: we'll want most of it. */
1890 /* vmalloc barfs on "unusual" numbers. Check here */
1891 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1892 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001893
1894 /* Create stop_machine threads since the error path relies on
1895 * a non-failing stop_machine call. */
1896 err = stop_machine_create();
1897 if (err)
1898 goto free_hdr;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (copy_from_user(hdr, umod, len) != 0) {
1901 err = -EFAULT;
1902 goto free_hdr;
1903 }
1904
1905 /* Sanity checks against insmoding binaries or wrong arch,
1906 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001907 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 || hdr->e_type != ET_REL
1909 || !elf_check_arch(hdr)
1910 || hdr->e_shentsize != sizeof(*sechdrs)) {
1911 err = -ENOEXEC;
1912 goto free_hdr;
1913 }
1914
1915 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1916 goto truncated;
1917
1918 /* Convenience variables */
1919 sechdrs = (void *)hdr + hdr->e_shoff;
1920 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1921 sechdrs[0].sh_addr = 0;
1922
1923 for (i = 1; i < hdr->e_shnum; i++) {
1924 if (sechdrs[i].sh_type != SHT_NOBITS
1925 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1926 goto truncated;
1927
1928 /* Mark all sections sh_addr with their address in the
1929 temporary image. */
1930 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1931
1932 /* Internal symbols and strings. */
1933 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1934 symindex = i;
1935 strindex = sechdrs[i].sh_link;
1936 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1937 }
1938#ifndef CONFIG_MODULE_UNLOAD
1939 /* Don't load .exit sections */
1940 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1941 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1942#endif
1943 }
1944
1945 modindex = find_sec(hdr, sechdrs, secstrings,
1946 ".gnu.linkonce.this_module");
1947 if (!modindex) {
1948 printk(KERN_WARNING "No module found in object\n");
1949 err = -ENOEXEC;
1950 goto free_hdr;
1951 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001952 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 mod = (void *)sechdrs[modindex].sh_addr;
1954
1955 if (symindex == 0) {
1956 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1957 mod->name);
1958 err = -ENOEXEC;
1959 goto free_hdr;
1960 }
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1963 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1964 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1965
Rusty Russellea01e792008-03-13 09:02:17 +00001966 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001968 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969#ifdef CONFIG_KALLSYMS
1970 /* Keep symbol and string tables for decoding later. */
1971 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1972 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1973#endif
1974
1975 /* Check module struct version now, before we try to use module. */
1976 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1977 err = -ENOEXEC;
1978 goto free_hdr;
1979 }
1980
1981 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1982 /* This is allowed: modprobe --force will invalidate it. */
1983 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07001984 err = try_to_force_load(mod, "magic");
1985 if (err)
1986 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10001987 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1989 mod->name, modmagic, vermagic);
1990 err = -ENOEXEC;
1991 goto free_hdr;
1992 }
1993
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001994 staging = get_modinfo(sechdrs, infoindex, "staging");
1995 if (staging) {
1996 add_taint_module(mod, TAINT_CRAP);
1997 printk(KERN_WARNING "%s: module is from the staging directory,"
1998 " the quality is unknown, you have been warned.\n",
1999 mod->name);
2000 }
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002003 args = strndup_user(uargs, ~0UL >> 1);
2004 if (IS_ERR(args)) {
2005 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto free_hdr;
2007 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (find_module(mod->name)) {
2010 err = -EEXIST;
2011 goto free_mod;
2012 }
2013
2014 mod->state = MODULE_STATE_COMING;
2015
2016 /* Allow arches to frob section contents and sizes. */
2017 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2018 if (err < 0)
2019 goto free_mod;
2020
Eric Dumazet720eba32009-02-03 13:31:36 +10302021#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2022 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2023 mod->name);
2024 if (!mod->refptr) {
2025 err = -ENOMEM;
2026 goto free_mod;
2027 }
2028#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (pcpuindex) {
2030 /* We have a special allocation for this section. */
2031 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002032 sechdrs[pcpuindex].sh_addralign,
2033 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!percpu) {
2035 err = -ENOMEM;
Eric Dumazet720eba32009-02-03 13:31:36 +10302036 goto free_percpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2039 mod->percpu = percpu;
2040 }
2041
2042 /* Determine total sizes, and put offsets in sh_entsize. For now
2043 this is done generically; there doesn't appear to be any
2044 special cases for the architectures. */
2045 layout_sections(mod, hdr, sechdrs, secstrings);
2046
2047 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002048 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (!ptr) {
2050 err = -ENOMEM;
2051 goto free_percpu;
2052 }
2053 memset(ptr, 0, mod->core_size);
2054 mod->module_core = ptr;
2055
Rusty Russell3a642e92008-07-22 19:24:28 -05002056 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (!ptr && mod->init_size) {
2058 err = -ENOMEM;
2059 goto free_core;
2060 }
2061 memset(ptr, 0, mod->init_size);
2062 mod->module_init = ptr;
2063
2064 /* Transfer each section which specifies SHF_ALLOC */
2065 DEBUGP("final section addresses:\n");
2066 for (i = 0; i < hdr->e_shnum; i++) {
2067 void *dest;
2068
2069 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2070 continue;
2071
2072 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2073 dest = mod->module_init
2074 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2075 else
2076 dest = mod->module_core + sechdrs[i].sh_entsize;
2077
2078 if (sechdrs[i].sh_type != SHT_NOBITS)
2079 memcpy(dest, (void *)sechdrs[i].sh_addr,
2080 sechdrs[i].sh_size);
2081 /* Update sh_addr to point to copy in image. */
2082 sechdrs[i].sh_addr = (unsigned long)dest;
2083 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2084 }
2085 /* Module has been moved. */
2086 mod = (void *)sechdrs[modindex].sh_addr;
2087
2088 /* Now we've moved module, initialize linked lists, etc. */
2089 module_unload_init(mod);
2090
Kay Sievers97c146e2007-11-29 23:46:11 +01002091 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002092 err = mod_sysfs_init(mod);
2093 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002094 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /* Set up license info based on the info section */
2097 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2098
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002099 /*
2100 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2101 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2102 * using GPL-only symbols it needs.
2103 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002104 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002105 add_taint(TAINT_PROPRIETARY_MODULE);
2106
2107 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002108 if (strcmp(mod->name, "driverloader") == 0)
2109 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002110
Matt Domschc988d2b2005-06-23 22:05:15 -07002111 /* Set up MODINFO_ATTR fields */
2112 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* Fix up syms, so that st_value is a pointer to location. */
2115 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2116 mod);
2117 if (err < 0)
2118 goto cleanup;
2119
Rusty Russell5e458cc2008-10-22 10:00:13 -05002120 /* Now we've got everything in the final locations, we can
2121 * find optional sections. */
2122 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2123 &num_kp);
2124 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2125 sizeof(*mod->syms), &mod->num_syms);
2126 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2127 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2128 sizeof(*mod->gpl_syms),
2129 &mod->num_gpl_syms);
2130 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2131 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2132 "__ksymtab_gpl_future",
2133 sizeof(*mod->gpl_future_syms),
2134 &mod->num_gpl_future_syms);
2135 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2136 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002138#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002139 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2140 "__ksymtab_unused",
2141 sizeof(*mod->unused_syms),
2142 &mod->num_unused_syms);
2143 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2144 "__kcrctab_unused");
2145 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2146 "__ksymtab_unused_gpl",
2147 sizeof(*mod->unused_gpl_syms),
2148 &mod->num_unused_gpl_syms);
2149 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2150 "__kcrctab_unused_gpl");
2151#endif
2152
2153#ifdef CONFIG_MARKERS
2154 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2155 sizeof(*mod->markers), &mod->num_markers);
2156#endif
2157#ifdef CONFIG_TRACEPOINTS
2158 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2159 "__tracepoints",
2160 sizeof(*mod->tracepoints),
2161 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002162#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002165 if ((mod->num_syms && !mod->crcs)
2166 || (mod->num_gpl_syms && !mod->gpl_crcs)
2167 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002168#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002169 || (mod->num_unused_syms && !mod->unused_crcs)
2170 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002171#endif
2172 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002173 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2174 err = try_to_force_load(mod, "nocrc");
2175 if (err)
2176 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178#endif
2179
2180 /* Now do relocations. */
2181 for (i = 1; i < hdr->e_shnum; i++) {
2182 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2183 unsigned int info = sechdrs[i].sh_info;
2184
2185 /* Not a valid relocation section? */
2186 if (info >= hdr->e_shnum)
2187 continue;
2188
2189 /* Don't bother with non-allocated sections */
2190 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2191 continue;
2192
2193 if (sechdrs[i].sh_type == SHT_REL)
2194 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2195 else if (sechdrs[i].sh_type == SHT_RELA)
2196 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2197 mod);
2198 if (err < 0)
2199 goto cleanup;
2200 }
2201
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002202 /* Find duplicate symbols */
2203 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002204 if (err < 0)
2205 goto cleanup;
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002208 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2209 sizeof(*mod->extable), &mod->num_exentries);
2210 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /* Finally, copy percpu area over. */
2213 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2214 sechdrs[pcpuindex].sh_size);
2215
2216 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2217
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002218 if (!mod->taints) {
Rusty Russell5e458cc2008-10-22 10:00:13 -05002219 struct mod_debug *debug;
2220 unsigned int num_debug;
2221
Rusty Russell5e458cc2008-10-22 10:00:13 -05002222 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2223 sizeof(*debug), &num_debug);
2224 dynamic_printk_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002225 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002226
Steven Rostedtfed19392008-08-14 22:47:19 -04002227 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002228 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2229 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002230 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 err = module_finalize(hdr, sechdrs, mod);
2233 if (err < 0)
2234 goto cleanup;
2235
Thomas Koeller378bac82005-09-06 15:17:11 -07002236 /* flush the icache in correct context */
2237 old_fs = get_fs();
2238 set_fs(KERNEL_DS);
2239
2240 /*
2241 * Flush the instruction cache, since we've played with text.
2242 * Do it before processing of module parameters, so the module
2243 * can provide parameter accessor functions of its own.
2244 */
2245 if (mod->module_init)
2246 flush_icache_range((unsigned long)mod->module_init,
2247 (unsigned long)mod->module_init
2248 + mod->init_size);
2249 flush_icache_range((unsigned long)mod->module_core,
2250 (unsigned long)mod->module_core + mod->core_size);
2251
2252 set_fs(old_fs);
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002255 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002256 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2257 mod->name);
2258
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002259 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002260 * info during argument parsing. Noone should access us, since
2261 * strong_try_module_get() will fail.
2262 * lockdep/oops can run asynchronous, so use the RCU list insertion
2263 * function to insert in a way safe to concurrent readers.
2264 * The mutex protects against concurrent writers.
2265 */
2266 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002267
Rusty Russell5e458cc2008-10-22 10:00:13 -05002268 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002270 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Rusty Russell5e458cc2008-10-22 10:00:13 -05002272 err = mod_sysfs_setup(mod, kp, num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002274 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002276 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /* Get rid of temporary copy */
2279 vfree(hdr);
2280
Heiko Carstens9e018922008-12-22 12:36:31 +01002281 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 /* Done! */
2283 return mod;
2284
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002285 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002286 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 module_arch_cleanup(mod);
2288 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002289 kobject_del(&mod->mkobj.kobj);
2290 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002291 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002292 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 module_unload_free(mod);
2294 module_free(mod, mod->module_init);
2295 free_core:
2296 module_free(mod, mod->module_core);
2297 free_percpu:
2298 if (percpu)
2299 percpu_modfree(percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10302300#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2301 percpu_modfree(mod->refptr);
2302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 free_mod:
2304 kfree(args);
2305 free_hdr:
2306 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002307 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002308 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 truncated:
2311 printk(KERN_ERR "Module len %lu truncated\n", len);
2312 err = -ENOEXEC;
2313 goto free_hdr;
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002317SYSCALL_DEFINE3(init_module, void __user *, umod,
2318 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 struct module *mod;
2321 int ret = 0;
2322
2323 /* Must have permission */
2324 if (!capable(CAP_SYS_MODULE))
2325 return -EPERM;
2326
2327 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002328 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return -EINTR;
2330
2331 /* Do all the hard work */
2332 mod = load_module(umod, len, uargs);
2333 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002334 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return PTR_ERR(mod);
2336 }
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002339 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Alan Sterne041c682006-03-27 01:16:30 -08002341 blocking_notifier_call_chain(&module_notify_list,
2342 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 /* Start the module */
2345 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002346 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (ret < 0) {
2348 /* Init routine failed: abort. Try to protect us from
2349 buggy refcounters. */
2350 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002351 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002352 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002353 blocking_notifier_call_chain(&module_notify_list,
2354 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002355 mutex_lock(&module_mutex);
2356 free_module(mod);
2357 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002358 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return ret;
2360 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002361 if (ret > 0) {
2362 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2363 "it should follow 0/-E convention\n"
2364 KERN_WARNING "%s: loading module anyway...\n",
2365 __func__, mod->name, ret,
2366 __func__);
2367 dump_stack();
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Rusty Russell6c5db222008-03-10 11:43:52 -07002370 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002372 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002373 blocking_notifier_call_chain(&module_notify_list,
2374 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002375
2376 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /* Drop initial reference. */
2378 module_put(mod);
2379 module_free(mod, mod->module_init);
2380 mod->module_init = NULL;
2381 mod->init_size = 0;
2382 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002383 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 return 0;
2386}
2387
2388static inline int within(unsigned long addr, void *start, unsigned long size)
2389{
2390 return ((void *)addr >= start && (void *)addr < start + size);
2391}
2392
2393#ifdef CONFIG_KALLSYMS
2394/*
2395 * This ignores the intensely annoying "mapping symbols" found
2396 * in ARM ELF files: $a, $t and $d.
2397 */
2398static inline int is_arm_mapping_symbol(const char *str)
2399{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002400 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 && (str[2] == '\0' || str[2] == '.');
2402}
2403
2404static const char *get_ksymbol(struct module *mod,
2405 unsigned long addr,
2406 unsigned long *size,
2407 unsigned long *offset)
2408{
2409 unsigned int i, best = 0;
2410 unsigned long nextval;
2411
2412 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002413 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002415 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2417
2418 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002419 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 for (i = 1; i < mod->num_symtab; i++) {
2421 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2422 continue;
2423
2424 /* We ignore unnamed symbols: they're uninformative
2425 * and inserted at a whim. */
2426 if (mod->symtab[i].st_value <= addr
2427 && mod->symtab[i].st_value > mod->symtab[best].st_value
2428 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2429 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2430 best = i;
2431 if (mod->symtab[i].st_value > addr
2432 && mod->symtab[i].st_value < nextval
2433 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2434 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2435 nextval = mod->symtab[i].st_value;
2436 }
2437
2438 if (!best)
2439 return NULL;
2440
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002441 if (size)
2442 *size = nextval - mod->symtab[best].st_value;
2443 if (offset)
2444 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return mod->strtab + mod->symtab[best].st_name;
2446}
2447
Rusty Russell6dd06c92008-01-29 17:13:22 -05002448/* For kallsyms to ask for address resolution. NULL means not found. Careful
2449 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002450const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002451 unsigned long *size,
2452 unsigned long *offset,
2453 char **modname,
2454 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002457 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Rusty Russellcb2a5202008-01-14 00:55:03 -08002459 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002460 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002461 if (within_module_init(addr, mod) ||
2462 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002463 if (modname)
2464 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002465 ret = get_ksymbol(mod, addr, size, offset);
2466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002469 /* Make a copy in here where it's safe */
2470 if (ret) {
2471 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2472 ret = namebuf;
2473 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002474 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002478int lookup_module_symbol_name(unsigned long addr, char *symname)
2479{
2480 struct module *mod;
2481
Rusty Russellcb2a5202008-01-14 00:55:03 -08002482 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002483 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002484 if (within_module_init(addr, mod) ||
2485 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002486 const char *sym;
2487
2488 sym = get_ksymbol(mod, addr, NULL, NULL);
2489 if (!sym)
2490 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002491 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002492 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002493 return 0;
2494 }
2495 }
2496out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002497 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002498 return -ERANGE;
2499}
2500
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002501int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2502 unsigned long *offset, char *modname, char *name)
2503{
2504 struct module *mod;
2505
Rusty Russellcb2a5202008-01-14 00:55:03 -08002506 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002507 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002508 if (within_module_init(addr, mod) ||
2509 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002510 const char *sym;
2511
2512 sym = get_ksymbol(mod, addr, size, offset);
2513 if (!sym)
2514 goto out;
2515 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002516 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002517 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002518 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002519 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002520 return 0;
2521 }
2522 }
2523out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002524 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002525 return -ERANGE;
2526}
2527
Alexey Dobriyanea078902007-05-08 00:28:39 -07002528int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2529 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 struct module *mod;
2532
Rusty Russellcb2a5202008-01-14 00:55:03 -08002533 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002534 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (symnum < mod->num_symtab) {
2536 *value = mod->symtab[symnum].st_value;
2537 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002538 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002539 KSYM_NAME_LEN);
2540 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002541 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002542 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
2545 symnum -= mod->num_symtab;
2546 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002547 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002548 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551static unsigned long mod_find_symname(struct module *mod, const char *name)
2552{
2553 unsigned int i;
2554
2555 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002556 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2557 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return mod->symtab[i].st_value;
2559 return 0;
2560}
2561
2562/* Look for this name: can be of form module:name. */
2563unsigned long module_kallsyms_lookup_name(const char *name)
2564{
2565 struct module *mod;
2566 char *colon;
2567 unsigned long ret = 0;
2568
2569 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002570 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if ((colon = strchr(name, ':')) != NULL) {
2572 *colon = '\0';
2573 if ((mod = find_module(name)) != NULL)
2574 ret = mod_find_symname(mod, colon+1);
2575 *colon = ':';
2576 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002577 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if ((ret = mod_find_symname(mod, name)) != 0)
2579 break;
2580 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002581 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return ret;
2583}
2584#endif /* CONFIG_KALLSYMS */
2585
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002586static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002587{
2588 int bx = 0;
2589
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002590 if (mod->taints ||
2591 mod->state == MODULE_STATE_GOING ||
2592 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002593 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002594 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002595 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002596 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002597 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002598 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002599 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002600 /*
2601 * TAINT_FORCED_RMMOD: could be added.
2602 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2603 * apply to modules.
2604 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002605
2606 /* Show a - for module-is-being-unloaded */
2607 if (mod->state == MODULE_STATE_GOING)
2608 buf[bx++] = '-';
2609 /* Show a + for module-is-being-loaded */
2610 if (mod->state == MODULE_STATE_COMING)
2611 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002612 buf[bx++] = ')';
2613 }
2614 buf[bx] = '\0';
2615
2616 return buf;
2617}
2618
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002619#ifdef CONFIG_PROC_FS
2620/* Called by the /proc file system to return a list of modules. */
2621static void *m_start(struct seq_file *m, loff_t *pos)
2622{
2623 mutex_lock(&module_mutex);
2624 return seq_list_start(&modules, *pos);
2625}
2626
2627static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2628{
2629 return seq_list_next(p, &modules, pos);
2630}
2631
2632static void m_stop(struct seq_file *m, void *p)
2633{
2634 mutex_unlock(&module_mutex);
2635}
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637static int m_show(struct seq_file *m, void *p)
2638{
2639 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002640 char buf[8];
2641
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002642 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 mod->name, mod->init_size + mod->core_size);
2644 print_unload_info(m, mod);
2645
2646 /* Informative for users. */
2647 seq_printf(m, " %s",
2648 mod->state == MODULE_STATE_GOING ? "Unloading":
2649 mod->state == MODULE_STATE_COMING ? "Loading":
2650 "Live");
2651 /* Used by oprofile and other similar tools. */
2652 seq_printf(m, " 0x%p", mod->module_core);
2653
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002654 /* Taints info */
2655 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002656 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 seq_printf(m, "\n");
2659 return 0;
2660}
2661
2662/* Format: modulename size refcount deps address
2663
2664 Where refcount is a number or -, and deps is a comma-separated list
2665 of depends or -.
2666*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002667static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 .start = m_start,
2669 .next = m_next,
2670 .stop = m_stop,
2671 .show = m_show
2672};
2673
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002674static int modules_open(struct inode *inode, struct file *file)
2675{
2676 return seq_open(file, &modules_op);
2677}
2678
2679static const struct file_operations proc_modules_operations = {
2680 .open = modules_open,
2681 .read = seq_read,
2682 .llseek = seq_lseek,
2683 .release = seq_release,
2684};
2685
2686static int __init proc_modules_init(void)
2687{
2688 proc_create("modules", 0, NULL, &proc_modules_operations);
2689 return 0;
2690}
2691module_init(proc_modules_init);
2692#endif
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694/* Given an address, look for it in the module exception tables. */
2695const struct exception_table_entry *search_module_extables(unsigned long addr)
2696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 const struct exception_table_entry *e = NULL;
2698 struct module *mod;
2699
Rusty Russell24da1cb2007-07-15 23:41:46 -07002700 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002701 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (mod->num_exentries == 0)
2703 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 e = search_extable(mod->extable,
2706 mod->extable + mod->num_exentries - 1,
2707 addr);
2708 if (e)
2709 break;
2710 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002711 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002714 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return e;
2716}
2717
Ingo Molnar4d435f92006-07-03 00:24:24 -07002718/*
2719 * Is this a valid module address?
2720 */
2721int is_module_address(unsigned long addr)
2722{
Ingo Molnar4d435f92006-07-03 00:24:24 -07002723 struct module *mod;
2724
Rusty Russell24da1cb2007-07-15 23:41:46 -07002725 preempt_disable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002726
Andi Kleend72b3752008-08-30 10:09:00 +02002727 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002728 if (within_module_core(addr, mod)) {
Rusty Russell24da1cb2007-07-15 23:41:46 -07002729 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002730 return 1;
2731 }
2732 }
2733
Rusty Russell24da1cb2007-07-15 23:41:46 -07002734 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002735
2736 return 0;
2737}
2738
2739
Rusty Russell24da1cb2007-07-15 23:41:46 -07002740/* Is this a valid kernel address? */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +01002741__notrace_funcgraph struct module *__module_text_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
2743 struct module *mod;
2744
Rusty Russell3a642e92008-07-22 19:24:28 -05002745 if (addr < module_addr_min || addr > module_addr_max)
2746 return NULL;
2747
Andi Kleend72b3752008-08-30 10:09:00 +02002748 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (within(addr, mod->module_init, mod->init_text_size)
2750 || within(addr, mod->module_core, mod->core_text_size))
2751 return mod;
2752 return NULL;
2753}
2754
2755struct module *module_text_address(unsigned long addr)
2756{
2757 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Rusty Russell24da1cb2007-07-15 23:41:46 -07002759 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002761 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 return mod;
2764}
2765
2766/* Don't grab lock, we're oopsing. */
2767void print_modules(void)
2768{
2769 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002770 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002773 /* Most callers should already have preempt disabled, but make sure */
2774 preempt_disable();
2775 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002776 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002777 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002778 if (last_unloaded_module[0])
2779 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 printk("\n");
2781}
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783#ifdef CONFIG_MODVERSIONS
2784/* Generate the signature for struct module here, too, for modversions. */
2785void struct_module(struct module *mod) { return; }
2786EXPORT_SYMBOL(struct_module);
2787#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002788
2789#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002790void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002791{
2792 struct module *mod;
2793
2794 mutex_lock(&module_mutex);
2795 list_for_each_entry(mod, &modules, list)
2796 if (!mod->taints)
2797 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002798 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002799 mutex_unlock(&module_mutex);
2800}
2801#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002802
2803#ifdef CONFIG_TRACEPOINTS
2804void module_update_tracepoints(void)
2805{
2806 struct module *mod;
2807
2808 mutex_lock(&module_mutex);
2809 list_for_each_entry(mod, &modules, list)
2810 if (!mod->taints)
2811 tracepoint_update_probe_range(mod->tracepoints,
2812 mod->tracepoints + mod->num_tracepoints);
2813 mutex_unlock(&module_mutex);
2814}
2815
2816/*
2817 * Returns 0 if current not found.
2818 * Returns 1 if current found.
2819 */
2820int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2821{
2822 struct module *iter_mod;
2823 int found = 0;
2824
2825 mutex_lock(&module_mutex);
2826 list_for_each_entry(iter_mod, &modules, list) {
2827 if (!iter_mod->taints) {
2828 /*
2829 * Sorted module list
2830 */
2831 if (iter_mod < iter->module)
2832 continue;
2833 else if (iter_mod > iter->module)
2834 iter->tracepoint = NULL;
2835 found = tracepoint_get_iter_range(&iter->tracepoint,
2836 iter_mod->tracepoints,
2837 iter_mod->tracepoints
2838 + iter_mod->num_tracepoints);
2839 if (found) {
2840 iter->module = iter_mod;
2841 break;
2842 }
2843 }
2844 }
2845 mutex_unlock(&module_mutex);
2846 return found;
2847}
2848#endif