blob: cb3887e770e21b2ca9244bef84ae19b1e9f2d3e5 [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>
Tejun Heofbf59bc2009-02-20 16:29:08 +090054#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#if 0
57#define DEBUGP printk
58#else
59#define DEBUGP(fmt , a...)
60#endif
61
62#ifndef ARCH_SHF_SMALL
63#define ARCH_SHF_SMALL 0
64#endif
65
66/* If this is set, the section belongs in the init part of the module */
67#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
68
Rusty Russell24da1cb2007-07-15 23:41:46 -070069/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020070 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050071DEFINE_MUTEX(module_mutex);
72EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static LIST_HEAD(modules);
74
Stephen Rothwell19e45292009-04-14 17:27:18 +100075/* Block module loading/unloading? */
76int modules_disabled = 0;
77
Rusty Russellc9a3ba52008-01-29 17:13:18 -050078/* Waiting for a module to finish initializing? */
79static DECLARE_WAIT_QUEUE_HEAD(module_wq);
80
Alan Sterne041c682006-03-27 01:16:30 -080081static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Rusty Russelle6104992009-03-31 13:05:31 -060083/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050084static unsigned long module_addr_min = -1UL, module_addr_max = 0;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086int register_module_notifier(struct notifier_block * nb)
87{
Alan Sterne041c682006-03-27 01:16:30 -080088 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90EXPORT_SYMBOL(register_module_notifier);
91
92int unregister_module_notifier(struct notifier_block * nb)
93{
Alan Sterne041c682006-03-27 01:16:30 -080094 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96EXPORT_SYMBOL(unregister_module_notifier);
97
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080098/* We require a truly strong try_module_get(): 0 means failure due to
99 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static inline int strong_try_module_get(struct module *mod)
101{
102 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500103 return -EBUSY;
104 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500106 else
107 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700110static inline void add_taint_module(struct module *mod, unsigned flag)
111{
112 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700113 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700114}
115
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200116/*
117 * A thread that wants to hold a reference to a module only while it
118 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
120void __module_put_and_exit(struct module *mod, long code)
121{
122 module_put(mod);
123 do_exit(code);
124}
125EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* Find a module section: 0 means not found. */
128static unsigned int find_sec(Elf_Ehdr *hdr,
129 Elf_Shdr *sechdrs,
130 const char *secstrings,
131 const char *name)
132{
133 unsigned int i;
134
135 for (i = 1; i < hdr->e_shnum; i++)
136 /* Alloc bit cleared means "ignore it." */
137 if ((sechdrs[i].sh_flags & SHF_ALLOC)
138 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
139 return i;
140 return 0;
141}
142
Rusty Russell5e458cc2008-10-22 10:00:13 -0500143/* Find a module section, or NULL. */
144static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
145 const char *secstrings, const char *name)
146{
147 /* Section 0 has sh_addr 0. */
148 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
149}
150
151/* Find a module section, or NULL. Fill in number of "objects" in section. */
152static void *section_objs(Elf_Ehdr *hdr,
153 Elf_Shdr *sechdrs,
154 const char *secstrings,
155 const char *name,
156 size_t object_size,
157 unsigned int *num)
158{
159 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
160
161 /* Section 0 has sh_addr 0 and sh_size 0. */
162 *num = sechdrs[sec].sh_size / object_size;
163 return (void *)sechdrs[sec].sh_addr;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Provided by the linker */
167extern const struct kernel_symbol __start___ksymtab[];
168extern const struct kernel_symbol __stop___ksymtab[];
169extern const struct kernel_symbol __start___ksymtab_gpl[];
170extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800171extern const struct kernel_symbol __start___ksymtab_gpl_future[];
172extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700173extern const struct kernel_symbol __start___ksymtab_gpl_future[];
174extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175extern const unsigned long __start___kcrctab[];
176extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800177extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500178#ifdef CONFIG_UNUSED_SYMBOLS
179extern const struct kernel_symbol __start___ksymtab_unused[];
180extern const struct kernel_symbol __stop___ksymtab_unused[];
181extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
182extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700183extern const unsigned long __start___kcrctab_unused[];
184extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187#ifndef CONFIG_MODVERSIONS
188#define symversion(base, idx) NULL
189#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800190#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
192
Rusty Russelldafd0942008-07-22 19:24:25 -0500193static bool each_symbol_in_section(const struct symsearch *arr,
194 unsigned int arrsize,
195 struct module *owner,
196 bool (*fn)(const struct symsearch *syms,
197 struct module *owner,
198 unsigned int symnum, void *data),
199 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100200{
Rusty Russelldafd0942008-07-22 19:24:25 -0500201 unsigned int i, j;
202
203 for (j = 0; j < arrsize; j++) {
204 for (i = 0; i < arr[j].stop - arr[j].start; i++)
205 if (fn(&arr[j], owner, i, data))
206 return true;
207 }
208
209 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100210}
211
Rusty Russelldafd0942008-07-22 19:24:25 -0500212/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500213bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
214 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700215{
Rusty Russelldafd0942008-07-22 19:24:25 -0500216 struct module *mod;
217 const struct symsearch arr[] = {
218 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
219 NOT_GPL_ONLY, false },
220 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
221 __start___kcrctab_gpl,
222 GPL_ONLY, false },
223 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
224 __start___kcrctab_gpl_future,
225 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500226#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500227 { __start___ksymtab_unused, __stop___ksymtab_unused,
228 __start___kcrctab_unused,
229 NOT_GPL_ONLY, true },
230 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
231 __start___kcrctab_unused_gpl,
232 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500233#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500234 };
235
236 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
237 return true;
238
Andi Kleend72b3752008-08-30 10:09:00 +0200239 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500240 struct symsearch arr[] = {
241 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
242 NOT_GPL_ONLY, false },
243 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
244 mod->gpl_crcs,
245 GPL_ONLY, false },
246 { mod->gpl_future_syms,
247 mod->gpl_future_syms + mod->num_gpl_future_syms,
248 mod->gpl_future_crcs,
249 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500250#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500251 { mod->unused_syms,
252 mod->unused_syms + mod->num_unused_syms,
253 mod->unused_crcs,
254 NOT_GPL_ONLY, true },
255 { mod->unused_gpl_syms,
256 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
257 mod->unused_gpl_crcs,
258 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500259#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500260 };
261
262 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
263 return true;
264 }
265 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700266}
Tim Abbottc6b37802008-12-05 19:03:59 -0500267EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700268
Rusty Russelldafd0942008-07-22 19:24:25 -0500269struct find_symbol_arg {
270 /* Input */
271 const char *name;
272 bool gplok;
273 bool warn;
274
275 /* Output */
276 struct module *owner;
277 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500278 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500279};
280
281static bool find_symbol_in_section(const struct symsearch *syms,
282 struct module *owner,
283 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500284{
Rusty Russelldafd0942008-07-22 19:24:25 -0500285 struct find_symbol_arg *fsa = data;
286
287 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
288 return false;
289
290 if (!fsa->gplok) {
291 if (syms->licence == GPL_ONLY)
292 return false;
293 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
294 printk(KERN_WARNING "Symbol %s is being used "
295 "by a non-GPL module, which will not "
296 "be allowed in the future\n", fsa->name);
297 printk(KERN_WARNING "Please see the file "
298 "Documentation/feature-removal-schedule.txt "
299 "in the kernel source tree for more details.\n");
300 }
301 }
302
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500303#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500304 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500305 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500306 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500307 printk(KERN_WARNING
308 "This symbol will go away in the future.\n");
309 printk(KERN_WARNING
310 "Please evalute if this is the right api to use and if "
311 "it really is, submit a report the linux kernel "
312 "mailinglist together with submitting your code for "
313 "inclusion.\n");
314 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500315#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500316
317 fsa->owner = owner;
318 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500319 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500320 return true;
321}
322
Tim Abbott414fd312008-12-05 19:03:56 -0500323/* Find a symbol and return it, along with, (optional) crc and
324 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500325const struct kernel_symbol *find_symbol(const char *name,
326 struct module **owner,
327 const unsigned long **crc,
328 bool gplok,
329 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Rusty Russelldafd0942008-07-22 19:24:25 -0500331 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Rusty Russelldafd0942008-07-22 19:24:25 -0500333 fsa.name = name;
334 fsa.gplok = gplok;
335 fsa.warn = warn;
336
337 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500338 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500339 *owner = fsa.owner;
340 if (crc)
341 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500342 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500346 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Tim Abbottc6b37802008-12-05 19:03:59 -0500348EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500351struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct module *mod;
354
355 list_for_each_entry(mod, &modules, list) {
356 if (strcmp(mod->name, name) == 0)
357 return mod;
358 }
359 return NULL;
360}
Tim Abbottc6b37802008-12-05 19:03:59 -0500361EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900364
365#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
366
367static void *percpu_modalloc(unsigned long size, unsigned long align,
368 const char *name)
369{
370 void *ptr;
371
372 if (align > PAGE_SIZE) {
373 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
374 name, align, PAGE_SIZE);
375 align = PAGE_SIZE;
376 }
377
Tejun Heoedcb4632009-03-06 14:33:59 +0900378 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900379 if (!ptr)
380 printk(KERN_WARNING
381 "Could not allocate %lu bytes percpu data\n", size);
382 return ptr;
383}
384
385static void percpu_modfree(void *freeme)
386{
387 free_percpu(freeme);
388}
389
390#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/* Number of blocks used and allocated. */
393static unsigned int pcpu_num_used, pcpu_num_allocated;
394/* Size of each block. -ve means used. */
395static int *pcpu_size;
396
397static int split_block(unsigned int i, unsigned short size)
398{
399 /* Reallocation required? */
400 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700401 int *new;
402
403 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
404 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!new)
406 return 0;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 pcpu_size = new;
410 }
411
412 /* Insert a new subblock */
413 memmove(&pcpu_size[i+1], &pcpu_size[i],
414 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
415 pcpu_num_used++;
416
417 pcpu_size[i+1] -= size;
418 pcpu_size[i] = size;
419 return 1;
420}
421
422static inline unsigned int block_size(int val)
423{
424 if (val < 0)
425 return -val;
426 return val;
427}
428
Rusty Russell842bbaa2005-08-01 21:11:47 -0700429static void *percpu_modalloc(unsigned long size, unsigned long align,
430 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 unsigned long extra;
433 unsigned int i;
434 void *ptr;
435
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200436 if (align > PAGE_SIZE) {
437 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
438 name, align, PAGE_SIZE);
439 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 ptr = __per_cpu_start;
443 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
444 /* Extra for alignment requirement. */
445 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
446 BUG_ON(i == 0 && extra != 0);
447
448 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
449 continue;
450
451 /* Transfer extra to previous block. */
452 if (pcpu_size[i-1] < 0)
453 pcpu_size[i-1] -= extra;
454 else
455 pcpu_size[i-1] += extra;
456 pcpu_size[i] -= extra;
457 ptr += extra;
458
459 /* Split block if warranted */
460 if (pcpu_size[i] - size > sizeof(unsigned long))
461 if (!split_block(i, size))
462 return NULL;
463
464 /* Mark allocated */
465 pcpu_size[i] = -pcpu_size[i];
466 return ptr;
467 }
468
469 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
470 size);
471 return NULL;
472}
473
474static void percpu_modfree(void *freeme)
475{
476 unsigned int i;
477 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
478
479 /* First entry is core kernel percpu data. */
480 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
481 if (ptr == freeme) {
482 pcpu_size[i] = -pcpu_size[i];
483 goto free;
484 }
485 }
486 BUG();
487
488 free:
489 /* Merge with previous? */
490 if (pcpu_size[i-1] >= 0) {
491 pcpu_size[i-1] += pcpu_size[i];
492 pcpu_num_used--;
493 memmove(&pcpu_size[i], &pcpu_size[i+1],
494 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
495 i--;
496 }
497 /* Merge with next? */
498 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
499 pcpu_size[i] += pcpu_size[i+1];
500 pcpu_num_used--;
501 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
502 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
503 }
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int percpu_modinit(void)
507{
508 pcpu_num_used = 2;
509 pcpu_num_allocated = 2;
510 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
511 GFP_KERNEL);
512 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d2007-05-02 19:27:11 +0200513 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Free room. */
515 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
516 if (pcpu_size[1] < 0) {
517 printk(KERN_ERR "No per-cpu room for modules.\n");
518 pcpu_num_used = 1;
519 }
520
521 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900524
Tejun Heofbf59bc2009-02-20 16:29:08 +0900525#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
526
Tejun Heo6b588c12009-02-20 16:29:07 +0900527static unsigned int find_pcpusec(Elf_Ehdr *hdr,
528 Elf_Shdr *sechdrs,
529 const char *secstrings)
530{
531 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
532}
533
534static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
535{
536 int cpu;
537
538 for_each_possible_cpu(cpu)
539 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900543
Rusty Russell842bbaa2005-08-01 21:11:47 -0700544static inline void *percpu_modalloc(unsigned long size, unsigned long align,
545 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 return NULL;
548}
549static inline void percpu_modfree(void *pcpuptr)
550{
551 BUG();
552}
553static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
554 Elf_Shdr *sechdrs,
555 const char *secstrings)
556{
557 return 0;
558}
559static inline void percpu_modcopy(void *pcpudst, const void *src,
560 unsigned long size)
561{
562 /* pcpusec should be 0, and size of that section should be 0. */
563 BUG_ON(size != 0);
564}
Tejun Heo6b588c12009-02-20 16:29:07 +0900565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif /* CONFIG_SMP */
567
Matt Domschc988d2b2005-06-23 22:05:15 -0700568#define MODINFO_ATTR(field) \
569static void setup_modinfo_##field(struct module *mod, const char *s) \
570{ \
571 mod->field = kstrdup(s, GFP_KERNEL); \
572} \
573static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
574 struct module *mod, char *buffer) \
575{ \
576 return sprintf(buffer, "%s\n", mod->field); \
577} \
578static int modinfo_##field##_exists(struct module *mod) \
579{ \
580 return mod->field != NULL; \
581} \
582static void free_modinfo_##field(struct module *mod) \
583{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700584 kfree(mod->field); \
585 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700586} \
587static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900588 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700589 .show = show_modinfo_##field, \
590 .setup = setup_modinfo_##field, \
591 .test = modinfo_##field##_exists, \
592 .free = free_modinfo_##field, \
593};
594
595MODINFO_ATTR(version);
596MODINFO_ATTR(srcversion);
597
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100598static char last_unloaded_module[MODULE_NAME_LEN+1];
599
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800600#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/* Init the unload section of the module. */
602static void module_unload_init(struct module *mod)
603{
Eric Dumazet720eba32009-02-03 13:31:36 +1030604 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030607 for_each_possible_cpu(cpu)
608 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030610 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Backwards compatibility macros put refcount during init. */
612 mod->waiter = current;
613}
614
615/* modules using other modules */
616struct module_use
617{
618 struct list_head list;
619 struct module *module_which_uses;
620};
621
622/* Does a already use b? */
623static int already_uses(struct module *a, struct module *b)
624{
625 struct module_use *use;
626
627 list_for_each_entry(use, &b->modules_which_use_me, list) {
628 if (use->module_which_uses == a) {
629 DEBUGP("%s uses %s!\n", a->name, b->name);
630 return 1;
631 }
632 }
633 DEBUGP("%s does not use %s!\n", a->name, b->name);
634 return 0;
635}
636
637/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500638int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500641 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (b == NULL || already_uses(a, b)) return 1;
644
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500645 /* If we're interrupted or time out, we fail. */
646 if (wait_event_interruptible_timeout(
647 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
648 30 * HZ) <= 0) {
649 printk("%s: gave up waiting for init of module %s.\n",
650 a->name, b->name);
651 return 0;
652 }
653
654 /* If strong_try_module_get() returned a different error, we fail. */
655 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657
658 DEBUGP("Allocating new usage for %s.\n", a->name);
659 use = kmalloc(sizeof(*use), GFP_ATOMIC);
660 if (!use) {
661 printk("%s: out of memory loading\n", a->name);
662 module_put(b);
663 return 0;
664 }
665
666 use->module_which_uses = a;
667 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100668 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 1;
670}
Tim Abbottc6b37802008-12-05 19:03:59 -0500671EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/* Clear the unload stuff of the module. */
674static void module_unload_free(struct module *mod)
675{
676 struct module *i;
677
678 list_for_each_entry(i, &modules, list) {
679 struct module_use *use;
680
681 list_for_each_entry(use, &i->modules_which_use_me, list) {
682 if (use->module_which_uses == mod) {
683 DEBUGP("%s unusing %s\n", mod->name, i->name);
684 module_put(i);
685 list_del(&use->list);
686 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100687 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* There can be at most one match. */
689 break;
690 }
691 }
692 }
693}
694
695#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800696static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int ret = (flags & O_TRUNC);
699 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800700 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return ret;
702}
703#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800704static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 return 0;
707}
708#endif /* CONFIG_MODULE_FORCE_UNLOAD */
709
710struct stopref
711{
712 struct module *mod;
713 int flags;
714 int *forced;
715};
716
717/* Whole machine is stopped with interrupts off when this runs. */
718static int __try_stop_module(void *_sref)
719{
720 struct stopref *sref = _sref;
721
Rusty Russellda39ba52008-07-22 19:24:25 -0500722 /* If it's not unused, quit unless we're forcing. */
723 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800724 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EWOULDBLOCK;
726 }
727
728 /* Mark it as dying. */
729 sref->mod->state = MODULE_STATE_GOING;
730 return 0;
731}
732
733static int try_stop_module(struct module *mod, int flags, int *forced)
734{
Rusty Russellda39ba52008-07-22 19:24:25 -0500735 if (flags & O_NONBLOCK) {
736 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500738 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500739 } else {
740 /* We don't need to stop the machine for this. */
741 mod->state = MODULE_STATE_GOING;
742 synchronize_sched();
743 return 0;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747unsigned int module_refcount(struct module *mod)
748{
Eric Dumazet720eba32009-02-03 13:31:36 +1030749 unsigned int total = 0;
750 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Eric Dumazet720eba32009-02-03 13:31:36 +1030752 for_each_possible_cpu(cpu)
753 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return total;
755}
756EXPORT_SYMBOL(module_refcount);
757
758/* This exists whether we can unload or not */
759static void free_module(struct module *mod);
760
761static void wait_for_zero_refcount(struct module *mod)
762{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500763 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800764 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 for (;;) {
766 DEBUGP("Looking at refcount...\n");
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 if (module_refcount(mod) == 0)
769 break;
770 schedule();
771 }
772 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800773 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100776SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
777 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800780 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int ret, forced = 0;
782
Kees Cook3d433212009-04-02 15:49:29 -0700783 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800784 return -EPERM;
785
786 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
787 return -EFAULT;
788 name[MODULE_NAME_LEN-1] = '\0';
789
Heiko Carstens9e018922008-12-22 12:36:31 +0100790 /* Create stop_machine threads since free_module relies on
791 * a non-failing stop_machine call. */
792 ret = stop_machine_create();
793 if (ret)
794 return ret;
795
796 if (mutex_lock_interruptible(&module_mutex) != 0) {
797 ret = -EINTR;
798 goto out_stop;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 mod = find_module(name);
802 if (!mod) {
803 ret = -ENOENT;
804 goto out;
805 }
806
807 if (!list_empty(&mod->modules_which_use_me)) {
808 /* Other modules depend on us: get rid of them first. */
809 ret = -EWOULDBLOCK;
810 goto out;
811 }
812
813 /* Doing init or already dying? */
814 if (mod->state != MODULE_STATE_LIVE) {
815 /* FIXME: if (force), slam module count and wake up
816 waiter --RR */
817 DEBUGP("%s already dying\n", mod->name);
818 ret = -EBUSY;
819 goto out;
820 }
821
822 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700823 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800824 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!forced) {
826 /* This module can't be removed */
827 ret = -EBUSY;
828 goto out;
829 }
830 }
831
832 /* Set this up before setting mod->state */
833 mod->waiter = current;
834
835 /* Stop the machine so refcounts can't move and disable module. */
836 ret = try_stop_module(mod, flags, &forced);
837 if (ret != 0)
838 goto out;
839
840 /* Never wait if forced. */
841 if (!forced && module_refcount(mod) != 0)
842 wait_for_zero_refcount(mod);
843
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200844 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200846 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200848 blocking_notifier_call_chain(&module_notify_list,
849 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800850 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200851 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100852 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500853 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500854 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 free_module(mod);
856
857 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800858 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100859out_stop:
860 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return ret;
862}
863
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800864static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct module_use *use;
867 int printed_something = 0;
868
869 seq_printf(m, " %u ", module_refcount(mod));
870
871 /* Always include a trailing , so userspace can differentiate
872 between this and the old multi-field proc format. */
873 list_for_each_entry(use, &mod->modules_which_use_me, list) {
874 printed_something = 1;
875 seq_printf(m, "%s,", use->module_which_uses->name);
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (mod->init != NULL && mod->exit == NULL) {
879 printed_something = 1;
880 seq_printf(m, "[permanent],");
881 }
882
883 if (!printed_something)
884 seq_printf(m, "-");
885}
886
887void __symbol_put(const char *symbol)
888{
889 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Rusty Russell24da1cb2007-07-15 23:41:46 -0700891 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500892 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 BUG();
894 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700895 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897EXPORT_SYMBOL(__symbol_put);
898
899void symbol_put_addr(void *addr)
900{
Trent Piepho5e376612006-05-15 09:44:06 -0700901 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Trent Piepho5e376612006-05-15 09:44:06 -0700903 if (core_kernel_text((unsigned long)addr))
904 return;
905
Rusty Russella6e6abd2009-03-31 13:05:31 -0600906 /* module_text_address is safe here: we're supposed to have reference
907 * to module from symbol_get, so it can't go away. */
908 modaddr = __module_text_address((unsigned long)addr);
909 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700910 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912EXPORT_SYMBOL_GPL(symbol_put_addr);
913
914static ssize_t show_refcnt(struct module_attribute *mattr,
915 struct module *mod, char *buffer)
916{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400917 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900921 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 .show = show_refcnt,
923};
924
Al Virof6a57032006-10-18 01:47:25 -0400925void module_put(struct module *module)
926{
927 if (module) {
928 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030929 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400930 /* Maybe they're waiting for us to drop reference? */
931 if (unlikely(!module_is_live(module)))
932 wake_up_process(module->waiter);
933 put_cpu();
934 }
935}
936EXPORT_SYMBOL(module_put);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800939static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 /* We don't know the usage count, or what modules are using. */
942 seq_printf(m, " - -");
943}
944
945static inline void module_unload_free(struct module *mod)
946{
947}
948
Tim Abbottc6b37802008-12-05 19:03:59 -0500949int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500951 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
Tim Abbottc6b37802008-12-05 19:03:59 -0500953EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955static inline void module_unload_init(struct module *mod)
956{
957}
958#endif /* CONFIG_MODULE_UNLOAD */
959
Kay Sievers1f717402006-11-24 12:15:25 +0100960static ssize_t show_initstate(struct module_attribute *mattr,
961 struct module *mod, char *buffer)
962{
963 const char *state = "unknown";
964
965 switch (mod->state) {
966 case MODULE_STATE_LIVE:
967 state = "live";
968 break;
969 case MODULE_STATE_COMING:
970 state = "coming";
971 break;
972 case MODULE_STATE_GOING:
973 state = "going";
974 break;
975 }
976 return sprintf(buffer, "%s\n", state);
977}
978
979static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900980 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100981 .show = show_initstate,
982};
983
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800984static struct module_attribute *modinfo_attrs[] = {
985 &modinfo_version,
986 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100987 &initstate,
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -0800988#ifdef CONFIG_MODULE_UNLOAD
989 &refcnt,
990#endif
991 NULL,
992};
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static const char vermagic[] = VERMAGIC_STRING;
995
Rusty Russellc6e665c2009-03-31 13:05:33 -0600996static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -0700997{
998#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700999 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001000 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1001 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001002 add_taint_module(mod, TAINT_FORCED_MODULE);
1003 return 0;
1004#else
1005 return -ENOEXEC;
1006#endif
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009#ifdef CONFIG_MODVERSIONS
1010static int check_version(Elf_Shdr *sechdrs,
1011 unsigned int versindex,
1012 const char *symname,
1013 struct module *mod,
1014 const unsigned long *crc)
1015{
1016 unsigned int i, num_versions;
1017 struct modversion_info *versions;
1018
1019 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1020 if (!crc)
1021 return 1;
1022
Rusty Russella5dd6972008-05-09 16:24:21 +10001023 /* No versions at all? modprobe --force does this. */
1024 if (versindex == 0)
1025 return try_to_force_load(mod, symname) == 0;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 versions = (void *) sechdrs[versindex].sh_addr;
1028 num_versions = sechdrs[versindex].sh_size
1029 / sizeof(struct modversion_info);
1030
1031 for (i = 0; i < num_versions; i++) {
1032 if (strcmp(versions[i].name, symname) != 0)
1033 continue;
1034
1035 if (versions[i].crc == *crc)
1036 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 DEBUGP("Found checksum %lX vs module %lX\n",
1038 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001039 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001041
Rusty Russella5dd6972008-05-09 16:24:21 +10001042 printk(KERN_WARNING "%s: no symbol version for %s\n",
1043 mod->name, symname);
1044 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001045
1046bad_version:
1047 printk("%s: disagrees about version of symbol %s\n",
1048 mod->name, symname);
1049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1053 unsigned int versindex,
1054 struct module *mod)
1055{
1056 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Rusty Russell8c8ef422009-03-31 13:05:34 -06001058 if (!find_symbol("module_layout", NULL, &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 BUG();
Rusty Russell8c8ef422009-03-31 13:05:34 -06001060 return check_version(sechdrs, versindex, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Rusty Russell91e37a72008-05-09 16:25:28 +10001063/* First part is kernel version, which we ignore if module has crcs. */
1064static inline int same_magic(const char *amagic, const char *bmagic,
1065 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Rusty Russell91e37a72008-05-09 16:25:28 +10001067 if (has_crcs) {
1068 amagic += strcspn(amagic, " ");
1069 bmagic += strcspn(bmagic, " ");
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return strcmp(amagic, bmagic) == 0;
1072}
1073#else
1074static inline int check_version(Elf_Shdr *sechdrs,
1075 unsigned int versindex,
1076 const char *symname,
1077 struct module *mod,
1078 const unsigned long *crc)
1079{
1080 return 1;
1081}
1082
1083static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1084 unsigned int versindex,
1085 struct module *mod)
1086{
1087 return 1;
1088}
1089
Rusty Russell91e37a72008-05-09 16:25:28 +10001090static inline int same_magic(const char *amagic, const char *bmagic,
1091 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 return strcmp(amagic, bmagic) == 0;
1094}
1095#endif /* CONFIG_MODVERSIONS */
1096
1097/* Resolve a symbol for this module. I.e. if we find one, record usage.
1098 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001099static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1100 unsigned int versindex,
1101 const char *name,
1102 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001105 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 const unsigned long *crc;
1107
Tim Abbott414fd312008-12-05 19:03:56 -05001108 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001109 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001110 /* use_module can fail due to OOM,
1111 or module initialization or unloading */
1112 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1114 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001115 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Tim Abbott414fd312008-12-05 19:03:56 -05001117 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/*
1121 * /sys/module/foo/sections stuff
1122 * J. Corbet <corbet@lwn.net>
1123 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001124#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001125struct module_sect_attr
1126{
1127 struct module_attribute mattr;
1128 char *name;
1129 unsigned long address;
1130};
1131
1132struct module_sect_attrs
1133{
1134 struct attribute_group grp;
1135 unsigned int nsections;
1136 struct module_sect_attr attrs[0];
1137};
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static ssize_t module_sect_show(struct module_attribute *mattr,
1140 struct module *mod, char *buf)
1141{
1142 struct module_sect_attr *sattr =
1143 container_of(mattr, struct module_sect_attr, mattr);
1144 return sprintf(buf, "0x%lx\n", sattr->address);
1145}
1146
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001147static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1148{
Rusty Russella58730c2008-03-13 09:03:44 +00001149 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001150
1151 for (section = 0; section < sect_attrs->nsections; section++)
1152 kfree(sect_attrs->attrs[section].name);
1153 kfree(sect_attrs);
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static void add_sect_attrs(struct module *mod, unsigned int nsect,
1157 char *secstrings, Elf_Shdr *sechdrs)
1158{
1159 unsigned int nloaded = 0, i, size[2];
1160 struct module_sect_attrs *sect_attrs;
1161 struct module_sect_attr *sattr;
1162 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* Count loaded sections and allocate structures */
1165 for (i = 0; i < nsect; i++)
1166 if (sechdrs[i].sh_flags & SHF_ALLOC)
1167 nloaded++;
1168 size[0] = ALIGN(sizeof(*sect_attrs)
1169 + nloaded * sizeof(sect_attrs->attrs[0]),
1170 sizeof(sect_attrs->grp.attrs[0]));
1171 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001172 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1173 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return;
1175
1176 /* Setup section attributes. */
1177 sect_attrs->grp.name = "sections";
1178 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1179
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001180 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 sattr = &sect_attrs->attrs[0];
1182 gattr = &sect_attrs->grp.attrs[0];
1183 for (i = 0; i < nsect; i++) {
1184 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1185 continue;
1186 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001187 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1188 GFP_KERNEL);
1189 if (sattr->name == NULL)
1190 goto out;
1191 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sattr->mattr.show = module_sect_show;
1193 sattr->mattr.store = NULL;
1194 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 sattr->mattr.attr.mode = S_IRUGO;
1196 *(gattr++) = &(sattr++)->mattr.attr;
1197 }
1198 *gattr = NULL;
1199
1200 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1201 goto out;
1202
1203 mod->sect_attrs = sect_attrs;
1204 return;
1205 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001206 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static void remove_sect_attrs(struct module *mod)
1210{
1211 if (mod->sect_attrs) {
1212 sysfs_remove_group(&mod->mkobj.kobj,
1213 &mod->sect_attrs->grp);
1214 /* We are positive that no one is using any sect attrs
1215 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001216 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 mod->sect_attrs = NULL;
1218 }
1219}
1220
Roland McGrath6d760132007-10-16 23:26:40 -07001221/*
1222 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1223 */
1224
1225struct module_notes_attrs {
1226 struct kobject *dir;
1227 unsigned int notes;
1228 struct bin_attribute attrs[0];
1229};
1230
1231static ssize_t module_notes_read(struct kobject *kobj,
1232 struct bin_attribute *bin_attr,
1233 char *buf, loff_t pos, size_t count)
1234{
1235 /*
1236 * The caller checked the pos and count against our size.
1237 */
1238 memcpy(buf, bin_attr->private + pos, count);
1239 return count;
1240}
1241
1242static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1243 unsigned int i)
1244{
1245 if (notes_attrs->dir) {
1246 while (i-- > 0)
1247 sysfs_remove_bin_file(notes_attrs->dir,
1248 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001249 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001250 }
1251 kfree(notes_attrs);
1252}
1253
1254static void add_notes_attrs(struct module *mod, unsigned int nsect,
1255 char *secstrings, Elf_Shdr *sechdrs)
1256{
1257 unsigned int notes, loaded, i;
1258 struct module_notes_attrs *notes_attrs;
1259 struct bin_attribute *nattr;
1260
1261 /* Count notes sections and allocate structures. */
1262 notes = 0;
1263 for (i = 0; i < nsect; i++)
1264 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1265 (sechdrs[i].sh_type == SHT_NOTE))
1266 ++notes;
1267
1268 if (notes == 0)
1269 return;
1270
1271 notes_attrs = kzalloc(sizeof(*notes_attrs)
1272 + notes * sizeof(notes_attrs->attrs[0]),
1273 GFP_KERNEL);
1274 if (notes_attrs == NULL)
1275 return;
1276
1277 notes_attrs->notes = notes;
1278 nattr = &notes_attrs->attrs[0];
1279 for (loaded = i = 0; i < nsect; ++i) {
1280 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1281 continue;
1282 if (sechdrs[i].sh_type == SHT_NOTE) {
1283 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1284 nattr->attr.mode = S_IRUGO;
1285 nattr->size = sechdrs[i].sh_size;
1286 nattr->private = (void *) sechdrs[i].sh_addr;
1287 nattr->read = module_notes_read;
1288 ++nattr;
1289 }
1290 ++loaded;
1291 }
1292
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001293 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001294 if (!notes_attrs->dir)
1295 goto out;
1296
1297 for (i = 0; i < notes; ++i)
1298 if (sysfs_create_bin_file(notes_attrs->dir,
1299 &notes_attrs->attrs[i]))
1300 goto out;
1301
1302 mod->notes_attrs = notes_attrs;
1303 return;
1304
1305 out:
1306 free_notes_attrs(notes_attrs, i);
1307}
1308
1309static void remove_notes_attrs(struct module *mod)
1310{
1311 if (mod->notes_attrs)
1312 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1318 char *sectstrings, Elf_Shdr *sechdrs)
1319{
1320}
1321
1322static inline void remove_sect_attrs(struct module *mod)
1323{
1324}
Roland McGrath6d760132007-10-16 23:26:40 -07001325
1326static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1327 char *sectstrings, Elf_Shdr *sechdrs)
1328{
1329}
1330
1331static inline void remove_notes_attrs(struct module *mod)
1332{
1333}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001334#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Randy Dunlapef665c12007-02-13 15:19:06 -08001336#ifdef CONFIG_SYSFS
1337int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001338{
1339 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001340 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001341 int error = 0;
1342 int i;
1343
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001344 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1345 (ARRAY_SIZE(modinfo_attrs) + 1)),
1346 GFP_KERNEL);
1347 if (!mod->modinfo_attrs)
1348 return -ENOMEM;
1349
1350 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001351 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1352 if (!attr->test ||
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001353 (attr->test && attr->test(mod))) {
1354 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001355 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1356 ++temp_attr;
1357 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001358 }
1359 return error;
1360}
1361
Randy Dunlapef665c12007-02-13 15:19:06 -08001362void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001363{
1364 struct module_attribute *attr;
1365 int i;
1366
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001367 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1368 /* pick a field to test for end of list */
1369 if (!attr->attr.name)
1370 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001371 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001372 if (attr->free)
1373 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001374 }
Greg Kroah-Hartman03e88ae2006-02-16 13:50:23 -08001375 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Randy Dunlapef665c12007-02-13 15:19:06 -08001378int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001381 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001383 if (!module_sysfs_initialized) {
1384 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001385 mod->name);
1386 err = -EINVAL;
1387 goto out;
1388 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001389
1390 kobj = kset_find_obj(module_kset, mod->name);
1391 if (kobj) {
1392 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1393 kobject_put(kobj);
1394 err = -EINVAL;
1395 goto out;
1396 }
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001399
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001400 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1401 mod->mkobj.kobj.kset = module_kset;
1402 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1403 "%s", mod->name);
1404 if (err)
1405 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001406
Kay Sievers97c146e2007-11-29 23:46:11 +01001407 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001408out:
1409 return err;
1410}
1411
Randy Dunlapef665c12007-02-13 15:19:06 -08001412int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001413 struct kernel_param *kparam,
1414 unsigned int num_params)
1415{
1416 int err;
1417
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001418 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001419 if (!mod->holders_dir) {
1420 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001421 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001422 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 err = module_param_sysfs_setup(mod, kparam, num_params);
1425 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001426 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Matt Domschc988d2b2005-06-23 22:05:15 -07001428 err = module_add_modinfo_attrs(mod);
1429 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001430 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001431
Kay Sieverse17e0f52006-11-24 12:15:25 +01001432 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434
Kay Sieverse17e0f52006-11-24 12:15:25 +01001435out_unreg_param:
1436 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001437out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001438 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001439out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001440 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return err;
1442}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001443
1444static void mod_sysfs_fini(struct module *mod)
1445{
1446 kobject_put(&mod->mkobj.kobj);
1447}
1448
1449#else /* CONFIG_SYSFS */
1450
1451static void mod_sysfs_fini(struct module *mod)
1452{
1453}
1454
1455#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457static void mod_kobject_remove(struct module *mod)
1458{
Matt Domschc988d2b2005-06-23 22:05:15 -07001459 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001461 kobject_put(mod->mkobj.drivers_dir);
1462 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001463 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466/*
1467 * unlink the module with the whole machine is stopped with interrupts off
1468 * - this defends against kallsyms not taking locks
1469 */
1470static int __unlink_module(void *_mod)
1471{
1472 struct module *mod = _mod;
1473 list_del(&mod->list);
1474 return 0;
1475}
1476
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001477/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static void free_module(struct module *mod)
1479{
1480 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001481 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001482 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 remove_sect_attrs(mod);
1484 mod_kobject_remove(mod);
1485
1486 /* Arch-specific cleanup. */
1487 module_arch_cleanup(mod);
1488
1489 /* Module unload stuff */
1490 module_unload_free(mod);
1491
Rusty Russelle180a6b2009-03-31 13:05:29 -06001492 /* Free any allocated parameters. */
1493 destroy_params(mod->kp, mod->num_kp);
1494
Steven Rostedtfed19392008-08-14 22:47:19 -04001495 /* release any pointers to mcount in this module */
1496 ftrace_release(mod->module_core, mod->core_size);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /* This may be NULL, but that's OK */
1499 module_free(mod, mod->module_init);
1500 kfree(mod->args);
1501 if (mod->percpu)
1502 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301503#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1504 if (mod->refptr)
1505 percpu_modfree(mod->refptr);
1506#endif
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001507 /* Free lock-classes: */
1508 lockdep_free_key_range(mod->module_core, mod->core_size);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* Finally, free the core (containing the module structure) */
1511 module_free(mod, mod->module_core);
1512}
1513
1514void *__symbol_get(const char *symbol)
1515{
1516 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001517 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Rusty Russell24da1cb2007-07-15 23:41:46 -07001519 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001520 sym = find_symbol(symbol, &owner, NULL, true, true);
1521 if (sym && strong_try_module_get(owner))
1522 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001523 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Tim Abbott414fd312008-12-05 19:03:56 -05001525 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527EXPORT_SYMBOL_GPL(__symbol_get);
1528
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001529/*
1530 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001531 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001532 */
1533static int verify_export_symbols(struct module *mod)
1534{
Rusty Russellb2111042008-05-01 21:15:00 -05001535 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001536 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001537 const struct kernel_symbol *s;
1538 struct {
1539 const struct kernel_symbol *sym;
1540 unsigned int num;
1541 } arr[] = {
1542 { mod->syms, mod->num_syms },
1543 { mod->gpl_syms, mod->num_gpl_syms },
1544 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001545#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001546 { mod->unused_syms, mod->num_unused_syms },
1547 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001548#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001549 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001550
Rusty Russellb2111042008-05-01 21:15:00 -05001551 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1552 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001553 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001554 printk(KERN_ERR
1555 "%s: exports duplicate symbol %s"
1556 " (owned by %s)\n",
1557 mod->name, s->name, module_name(owner));
1558 return -ENOEXEC;
1559 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001560 }
Rusty Russellb2111042008-05-01 21:15:00 -05001561 }
1562 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001563}
1564
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001565/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566static int simplify_symbols(Elf_Shdr *sechdrs,
1567 unsigned int symindex,
1568 const char *strtab,
1569 unsigned int versindex,
1570 unsigned int pcpuindex,
1571 struct module *mod)
1572{
1573 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1574 unsigned long secbase;
1575 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1576 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001577 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 for (i = 1; i < n; i++) {
1580 switch (sym[i].st_shndx) {
1581 case SHN_COMMON:
1582 /* We compiled with -fno-common. These are not
1583 supposed to happen. */
1584 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1585 printk("%s: please compile with -fno-common\n",
1586 mod->name);
1587 ret = -ENOEXEC;
1588 break;
1589
1590 case SHN_ABS:
1591 /* Don't need to do anything */
1592 DEBUGP("Absolute symbol: 0x%08lx\n",
1593 (long)sym[i].st_value);
1594 break;
1595
1596 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001597 ksym = resolve_symbol(sechdrs, versindex,
1598 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001600 if (ksym) {
1601 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001603 }
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /* Ok if weak. */
1606 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1607 break;
1608
1609 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1610 mod->name, strtab + sym[i].st_name);
1611 ret = -ENOENT;
1612 break;
1613
1614 default:
1615 /* Divert to percpu allocation if a percpu var. */
1616 if (sym[i].st_shndx == pcpuindex)
1617 secbase = (unsigned long)mod->percpu;
1618 else
1619 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1620 sym[i].st_value += secbase;
1621 break;
1622 }
1623 }
1624
1625 return ret;
1626}
1627
Helge Deller088af9a2008-12-31 12:31:18 +01001628/* Additional bytes needed by arch in front of individual sections */
1629unsigned int __weak arch_mod_section_prepend(struct module *mod,
1630 unsigned int section)
1631{
1632 /* default implementation just returns zero */
1633 return 0;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001637static long get_offset(struct module *mod, unsigned int *size,
1638 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 long ret;
1641
Helge Deller088af9a2008-12-31 12:31:18 +01001642 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1644 *size = ret + sechdr->sh_size;
1645 return ret;
1646}
1647
1648/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1649 might -- code, read-only data, read-write data, small data. Tally
1650 sizes, and place the offsets into sh_entsize fields: high bit means it
1651 belongs in init. */
1652static void layout_sections(struct module *mod,
1653 const Elf_Ehdr *hdr,
1654 Elf_Shdr *sechdrs,
1655 const char *secstrings)
1656{
1657 static unsigned long const masks[][2] = {
1658 /* NOTE: all executable code must be the first section
1659 * in this array; otherwise modify the text_size
1660 * finder in the two loops below */
1661 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1662 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1663 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1664 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1665 };
1666 unsigned int m, i;
1667
1668 for (i = 0; i < hdr->e_shnum; i++)
1669 sechdrs[i].sh_entsize = ~0UL;
1670
1671 DEBUGP("Core section allocation order:\n");
1672 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1673 for (i = 0; i < hdr->e_shnum; ++i) {
1674 Elf_Shdr *s = &sechdrs[i];
1675
1676 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1677 || (s->sh_flags & masks[m][1])
1678 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001679 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001681 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 DEBUGP("\t%s\n", secstrings + s->sh_name);
1683 }
1684 if (m == 0)
1685 mod->core_text_size = mod->core_size;
1686 }
1687
1688 DEBUGP("Init section allocation order:\n");
1689 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1690 for (i = 0; i < hdr->e_shnum; ++i) {
1691 Elf_Shdr *s = &sechdrs[i];
1692
1693 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1694 || (s->sh_flags & masks[m][1])
1695 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001696 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001698 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 | INIT_OFFSET_MASK);
1700 DEBUGP("\t%s\n", secstrings + s->sh_name);
1701 }
1702 if (m == 0)
1703 mod->init_text_size = mod->init_size;
1704 }
1705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707static void set_license(struct module *mod, const char *license)
1708{
1709 if (!license)
1710 license = "unspecified";
1711
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001712 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001713 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001714 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001715 "kernel.\n", mod->name, license);
1716 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718}
1719
1720/* Parse tag=value strings from .modinfo section */
1721static char *next_string(char *string, unsigned long *secsize)
1722{
1723 /* Skip non-zero chars */
1724 while (string[0]) {
1725 string++;
1726 if ((*secsize)-- <= 1)
1727 return NULL;
1728 }
1729
1730 /* Skip any zero padding. */
1731 while (!string[0]) {
1732 string++;
1733 if ((*secsize)-- <= 1)
1734 return NULL;
1735 }
1736 return string;
1737}
1738
1739static char *get_modinfo(Elf_Shdr *sechdrs,
1740 unsigned int info,
1741 const char *tag)
1742{
1743 char *p;
1744 unsigned int taglen = strlen(tag);
1745 unsigned long size = sechdrs[info].sh_size;
1746
1747 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1748 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1749 return p + taglen + 1;
1750 }
1751 return NULL;
1752}
1753
Matt Domschc988d2b2005-06-23 22:05:15 -07001754static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1755 unsigned int infoindex)
1756{
1757 struct module_attribute *attr;
1758 int i;
1759
1760 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1761 if (attr->setup)
1762 attr->setup(mod,
1763 get_modinfo(sechdrs,
1764 infoindex,
1765 attr->attr.name));
1766 }
1767}
Matt Domschc988d2b2005-06-23 22:05:15 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001770
1771/* lookup symbol in given range of kernel_symbols */
1772static const struct kernel_symbol *lookup_symbol(const char *name,
1773 const struct kernel_symbol *start,
1774 const struct kernel_symbol *stop)
1775{
1776 const struct kernel_symbol *ks = start;
1777 for (; ks < stop; ks++)
1778 if (strcmp(ks->name, name) == 0)
1779 return ks;
1780 return NULL;
1781}
1782
Tim Abbottca4787b2009-01-05 08:40:10 -06001783static int is_exported(const char *name, unsigned long value,
1784 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Tim Abbottca4787b2009-01-05 08:40:10 -06001786 const struct kernel_symbol *ks;
1787 if (!mod)
1788 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001789 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001790 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1791 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
1794/* As per nm */
1795static char elf_type(const Elf_Sym *sym,
1796 Elf_Shdr *sechdrs,
1797 const char *secstrings,
1798 struct module *mod)
1799{
1800 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1801 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1802 return 'v';
1803 else
1804 return 'w';
1805 }
1806 if (sym->st_shndx == SHN_UNDEF)
1807 return 'U';
1808 if (sym->st_shndx == SHN_ABS)
1809 return 'a';
1810 if (sym->st_shndx >= SHN_LORESERVE)
1811 return '?';
1812 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1813 return 't';
1814 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1815 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1816 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1817 return 'r';
1818 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1819 return 'g';
1820 else
1821 return 'd';
1822 }
1823 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1824 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1825 return 's';
1826 else
1827 return 'b';
1828 }
Rusty Russell49502672009-03-31 13:05:36 -06001829 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 'n';
1831 return '?';
1832}
1833
1834static void add_kallsyms(struct module *mod,
1835 Elf_Shdr *sechdrs,
1836 unsigned int symindex,
1837 unsigned int strindex,
1838 const char *secstrings)
1839{
1840 unsigned int i;
1841
1842 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1843 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1844 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1845
1846 /* Set types up while we still have access to sections. */
1847 for (i = 0; i < mod->num_symtab; i++)
1848 mod->symtab[i].st_info
1849 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1850}
1851#else
1852static inline void add_kallsyms(struct module *mod,
1853 Elf_Shdr *sechdrs,
1854 unsigned int symindex,
1855 unsigned int strindex,
1856 const char *secstrings)
1857{
1858}
1859#endif /* CONFIG_KALLSYMS */
1860
Jason Barone9d376f2009-02-05 11:51:38 -05001861static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001862{
Jason Barone9d376f2009-02-05 11:51:38 -05001863#ifdef CONFIG_DYNAMIC_DEBUG
1864 if (ddebug_add_module(debug, num, debug->modname))
1865 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1866 debug->modname);
1867#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001868}
Jason Baron346e15b2008-08-12 16:46:19 -04001869
Rusty Russell3a642e92008-07-22 19:24:28 -05001870static void *module_alloc_update_bounds(unsigned long size)
1871{
1872 void *ret = module_alloc(size);
1873
1874 if (ret) {
1875 /* Update module bounds. */
1876 if ((unsigned long)ret < module_addr_min)
1877 module_addr_min = (unsigned long)ret;
1878 if ((unsigned long)ret + size > module_addr_max)
1879 module_addr_max = (unsigned long)ret + size;
1880 }
1881 return ret;
1882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884/* Allocate and load the module: note that size of section 0 is always
1885 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001886static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 unsigned long len,
1888 const char __user *uargs)
1889{
1890 Elf_Ehdr *hdr;
1891 Elf_Shdr *sechdrs;
1892 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001893 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001894 unsigned int i;
1895 unsigned int symindex = 0;
1896 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001897 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russelle180a6b2009-03-31 13:05:29 -06001898 unsigned int num_mcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct module *mod;
1900 long err = 0;
1901 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001902 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001903 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1906 umod, len, uargs);
1907 if (len < sizeof(*hdr))
1908 return ERR_PTR(-ENOEXEC);
1909
1910 /* Suck in entire file: we'll want most of it. */
1911 /* vmalloc barfs on "unusual" numbers. Check here */
1912 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1913 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (copy_from_user(hdr, umod, len) != 0) {
1916 err = -EFAULT;
1917 goto free_hdr;
1918 }
1919
1920 /* Sanity checks against insmoding binaries or wrong arch,
1921 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001922 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 || hdr->e_type != ET_REL
1924 || !elf_check_arch(hdr)
1925 || hdr->e_shentsize != sizeof(*sechdrs)) {
1926 err = -ENOEXEC;
1927 goto free_hdr;
1928 }
1929
1930 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1931 goto truncated;
1932
1933 /* Convenience variables */
1934 sechdrs = (void *)hdr + hdr->e_shoff;
1935 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1936 sechdrs[0].sh_addr = 0;
1937
1938 for (i = 1; i < hdr->e_shnum; i++) {
1939 if (sechdrs[i].sh_type != SHT_NOBITS
1940 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1941 goto truncated;
1942
1943 /* Mark all sections sh_addr with their address in the
1944 temporary image. */
1945 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1946
1947 /* Internal symbols and strings. */
1948 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1949 symindex = i;
1950 strindex = sechdrs[i].sh_link;
1951 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1952 }
1953#ifndef CONFIG_MODULE_UNLOAD
1954 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06001955 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1957#endif
1958 }
1959
1960 modindex = find_sec(hdr, sechdrs, secstrings,
1961 ".gnu.linkonce.this_module");
1962 if (!modindex) {
1963 printk(KERN_WARNING "No module found in object\n");
1964 err = -ENOEXEC;
1965 goto free_hdr;
1966 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001967 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 mod = (void *)sechdrs[modindex].sh_addr;
1969
1970 if (symindex == 0) {
1971 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1972 mod->name);
1973 err = -ENOEXEC;
1974 goto free_hdr;
1975 }
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1978 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1979 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1980
Rusty Russellea01e792008-03-13 09:02:17 +00001981 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001983 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984#ifdef CONFIG_KALLSYMS
1985 /* Keep symbol and string tables for decoding later. */
1986 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1987 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1988#endif
1989
1990 /* Check module struct version now, before we try to use module. */
1991 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1992 err = -ENOEXEC;
1993 goto free_hdr;
1994 }
1995
1996 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1997 /* This is allowed: modprobe --force will invalidate it. */
1998 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06001999 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002000 if (err)
2001 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002002 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2004 mod->name, modmagic, vermagic);
2005 err = -ENOEXEC;
2006 goto free_hdr;
2007 }
2008
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002009 staging = get_modinfo(sechdrs, infoindex, "staging");
2010 if (staging) {
2011 add_taint_module(mod, TAINT_CRAP);
2012 printk(KERN_WARNING "%s: module is from the staging directory,"
2013 " the quality is unknown, you have been warned.\n",
2014 mod->name);
2015 }
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002018 args = strndup_user(uargs, ~0UL >> 1);
2019 if (IS_ERR(args)) {
2020 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto free_hdr;
2022 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (find_module(mod->name)) {
2025 err = -EEXIST;
2026 goto free_mod;
2027 }
2028
2029 mod->state = MODULE_STATE_COMING;
2030
2031 /* Allow arches to frob section contents and sizes. */
2032 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2033 if (err < 0)
2034 goto free_mod;
2035
2036 if (pcpuindex) {
2037 /* We have a special allocation for this section. */
2038 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002039 sechdrs[pcpuindex].sh_addralign,
2040 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (!percpu) {
2042 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002043 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2046 mod->percpu = percpu;
2047 }
2048
2049 /* Determine total sizes, and put offsets in sh_entsize. For now
2050 this is done generically; there doesn't appear to be any
2051 special cases for the architectures. */
2052 layout_sections(mod, hdr, sechdrs, secstrings);
2053
2054 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002055 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (!ptr) {
2057 err = -ENOMEM;
2058 goto free_percpu;
2059 }
2060 memset(ptr, 0, mod->core_size);
2061 mod->module_core = ptr;
2062
Rusty Russell3a642e92008-07-22 19:24:28 -05002063 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (!ptr && mod->init_size) {
2065 err = -ENOMEM;
2066 goto free_core;
2067 }
2068 memset(ptr, 0, mod->init_size);
2069 mod->module_init = ptr;
2070
2071 /* Transfer each section which specifies SHF_ALLOC */
2072 DEBUGP("final section addresses:\n");
2073 for (i = 0; i < hdr->e_shnum; i++) {
2074 void *dest;
2075
2076 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2077 continue;
2078
2079 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2080 dest = mod->module_init
2081 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2082 else
2083 dest = mod->module_core + sechdrs[i].sh_entsize;
2084
2085 if (sechdrs[i].sh_type != SHT_NOBITS)
2086 memcpy(dest, (void *)sechdrs[i].sh_addr,
2087 sechdrs[i].sh_size);
2088 /* Update sh_addr to point to copy in image. */
2089 sechdrs[i].sh_addr = (unsigned long)dest;
2090 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2091 }
2092 /* Module has been moved. */
2093 mod = (void *)sechdrs[modindex].sh_addr;
2094
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002095#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2096 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2097 mod->name);
2098 if (!mod->refptr) {
2099 err = -ENOMEM;
2100 goto free_init;
2101 }
2102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* Now we've moved module, initialize linked lists, etc. */
2104 module_unload_init(mod);
2105
Kay Sievers97c146e2007-11-29 23:46:11 +01002106 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002107 err = mod_sysfs_init(mod);
2108 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002109 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /* Set up license info based on the info section */
2112 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2113
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002114 /*
2115 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2116 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2117 * using GPL-only symbols it needs.
2118 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002119 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002120 add_taint(TAINT_PROPRIETARY_MODULE);
2121
2122 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002123 if (strcmp(mod->name, "driverloader") == 0)
2124 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d612006-01-08 01:03:41 -08002125
Matt Domschc988d2b2005-06-23 22:05:15 -07002126 /* Set up MODINFO_ATTR fields */
2127 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* Fix up syms, so that st_value is a pointer to location. */
2130 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2131 mod);
2132 if (err < 0)
2133 goto cleanup;
2134
Rusty Russell5e458cc2008-10-22 10:00:13 -05002135 /* Now we've got everything in the final locations, we can
2136 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002137 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2138 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002139 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2140 sizeof(*mod->syms), &mod->num_syms);
2141 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2142 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2143 sizeof(*mod->gpl_syms),
2144 &mod->num_gpl_syms);
2145 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2146 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2147 "__ksymtab_gpl_future",
2148 sizeof(*mod->gpl_future_syms),
2149 &mod->num_gpl_future_syms);
2150 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2151 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002153#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002154 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2155 "__ksymtab_unused",
2156 sizeof(*mod->unused_syms),
2157 &mod->num_unused_syms);
2158 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2159 "__kcrctab_unused");
2160 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2161 "__ksymtab_unused_gpl",
2162 sizeof(*mod->unused_gpl_syms),
2163 &mod->num_unused_gpl_syms);
2164 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2165 "__kcrctab_unused_gpl");
2166#endif
2167
2168#ifdef CONFIG_MARKERS
2169 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2170 sizeof(*mod->markers), &mod->num_markers);
2171#endif
2172#ifdef CONFIG_TRACEPOINTS
2173 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2174 "__tracepoints",
2175 sizeof(*mod->tracepoints),
2176 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002177#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002180 if ((mod->num_syms && !mod->crcs)
2181 || (mod->num_gpl_syms && !mod->gpl_crcs)
2182 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002183#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002184 || (mod->num_unused_syms && !mod->unused_crcs)
2185 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002186#endif
2187 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002188 err = try_to_force_load(mod,
2189 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002190 if (err)
2191 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193#endif
2194
2195 /* Now do relocations. */
2196 for (i = 1; i < hdr->e_shnum; i++) {
2197 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2198 unsigned int info = sechdrs[i].sh_info;
2199
2200 /* Not a valid relocation section? */
2201 if (info >= hdr->e_shnum)
2202 continue;
2203
2204 /* Don't bother with non-allocated sections */
2205 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2206 continue;
2207
2208 if (sechdrs[i].sh_type == SHT_REL)
2209 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2210 else if (sechdrs[i].sh_type == SHT_RELA)
2211 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2212 mod);
2213 if (err < 0)
2214 goto cleanup;
2215 }
2216
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002217 /* Find duplicate symbols */
2218 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002219 if (err < 0)
2220 goto cleanup;
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002223 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2224 sizeof(*mod->extable), &mod->num_exentries);
2225 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 /* Finally, copy percpu area over. */
2228 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2229 sechdrs[pcpuindex].sh_size);
2230
2231 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2232
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002233 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002234 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002235 unsigned int num_debug;
2236
Rusty Russell5e458cc2008-10-22 10:00:13 -05002237 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2238 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002239 if (debug)
2240 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002241 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002242
Steven Rostedtfed19392008-08-14 22:47:19 -04002243 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002244 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2245 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002246 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 err = module_finalize(hdr, sechdrs, mod);
2249 if (err < 0)
2250 goto cleanup;
2251
Thomas Koeller378bac82005-09-06 15:17:11 -07002252 /* flush the icache in correct context */
2253 old_fs = get_fs();
2254 set_fs(KERNEL_DS);
2255
2256 /*
2257 * Flush the instruction cache, since we've played with text.
2258 * Do it before processing of module parameters, so the module
2259 * can provide parameter accessor functions of its own.
2260 */
2261 if (mod->module_init)
2262 flush_icache_range((unsigned long)mod->module_init,
2263 (unsigned long)mod->module_init
2264 + mod->init_size);
2265 flush_icache_range((unsigned long)mod->module_core,
2266 (unsigned long)mod->module_core + mod->core_size);
2267
2268 set_fs(old_fs);
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002271 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002272 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2273 mod->name);
2274
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002275 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002276 * info during argument parsing. Noone should access us, since
2277 * strong_try_module_get() will fail.
2278 * lockdep/oops can run asynchronous, so use the RCU list insertion
2279 * function to insert in a way safe to concurrent readers.
2280 * The mutex protects against concurrent writers.
2281 */
2282 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002283
Rusty Russelle180a6b2009-03-31 13:05:29 -06002284 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002286 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Rusty Russelle180a6b2009-03-31 13:05:29 -06002288 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002290 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002292 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* Get rid of temporary copy */
2295 vfree(hdr);
2296
2297 /* Done! */
2298 return mod;
2299
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002300 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002301 /* Unlink carefully: kallsyms could be walking list. */
2302 list_del_rcu(&mod->list);
2303 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 module_arch_cleanup(mod);
2305 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002306 kobject_del(&mod->mkobj.kobj);
2307 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002308 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002309 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302311#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002312 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302313 percpu_modfree(mod->refptr);
2314#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002315 module_free(mod, mod->module_init);
2316 free_core:
2317 module_free(mod, mod->module_core);
2318 /* mod will be freed with core. Don't access it beyond this line! */
2319 free_percpu:
2320 if (percpu)
2321 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 free_mod:
2323 kfree(args);
2324 free_hdr:
2325 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002326 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 truncated:
2329 printk(KERN_ERR "Module len %lu truncated\n", len);
2330 err = -ENOEXEC;
2331 goto free_hdr;
2332}
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002335SYSCALL_DEFINE3(init_module, void __user *, umod,
2336 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 struct module *mod;
2339 int ret = 0;
2340
2341 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002342 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return -EPERM;
2344
2345 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002346 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return -EINTR;
2348
2349 /* Do all the hard work */
2350 mod = load_module(umod, len, uargs);
2351 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002352 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return PTR_ERR(mod);
2354 }
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002357 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Alan Sterne041c682006-03-27 01:16:30 -08002359 blocking_notifier_call_chain(&module_notify_list,
2360 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 /* Start the module */
2363 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002364 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (ret < 0) {
2366 /* Init routine failed: abort. Try to protect us from
2367 buggy refcounters. */
2368 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002369 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002370 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002371 blocking_notifier_call_chain(&module_notify_list,
2372 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002373 mutex_lock(&module_mutex);
2374 free_module(mod);
2375 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002376 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return ret;
2378 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002379 if (ret > 0) {
2380 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2381 "it should follow 0/-E convention\n"
2382 KERN_WARNING "%s: loading module anyway...\n",
2383 __func__, mod->name, ret,
2384 __func__);
2385 dump_stack();
2386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Rusty Russell6c5db222008-03-10 11:43:52 -07002388 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002390 wake_up(&module_wq);
Masami Hiramatsu0deddf42009-01-06 14:41:54 -08002391 blocking_notifier_call_chain(&module_notify_list,
2392 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002393
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002394 /* We need to finish all async code before the module init sequence is done */
2395 async_synchronize_full();
2396
Rusty Russell6c5db222008-03-10 11:43:52 -07002397 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /* Drop initial reference. */
2399 module_put(mod);
2400 module_free(mod, mod->module_init);
2401 mod->module_init = NULL;
2402 mod->init_size = 0;
2403 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002404 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 return 0;
2407}
2408
2409static inline int within(unsigned long addr, void *start, unsigned long size)
2410{
2411 return ((void *)addr >= start && (void *)addr < start + size);
2412}
2413
2414#ifdef CONFIG_KALLSYMS
2415/*
2416 * This ignores the intensely annoying "mapping symbols" found
2417 * in ARM ELF files: $a, $t and $d.
2418 */
2419static inline int is_arm_mapping_symbol(const char *str)
2420{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002421 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 && (str[2] == '\0' || str[2] == '.');
2423}
2424
2425static const char *get_ksymbol(struct module *mod,
2426 unsigned long addr,
2427 unsigned long *size,
2428 unsigned long *offset)
2429{
2430 unsigned int i, best = 0;
2431 unsigned long nextval;
2432
2433 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002434 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002436 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2438
2439 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002440 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 for (i = 1; i < mod->num_symtab; i++) {
2442 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2443 continue;
2444
2445 /* We ignore unnamed symbols: they're uninformative
2446 * and inserted at a whim. */
2447 if (mod->symtab[i].st_value <= addr
2448 && mod->symtab[i].st_value > mod->symtab[best].st_value
2449 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2450 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2451 best = i;
2452 if (mod->symtab[i].st_value > addr
2453 && mod->symtab[i].st_value < nextval
2454 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2455 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2456 nextval = mod->symtab[i].st_value;
2457 }
2458
2459 if (!best)
2460 return NULL;
2461
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002462 if (size)
2463 *size = nextval - mod->symtab[best].st_value;
2464 if (offset)
2465 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return mod->strtab + mod->symtab[best].st_name;
2467}
2468
Rusty Russell6dd06c92008-01-29 17:13:22 -05002469/* For kallsyms to ask for address resolution. NULL means not found. Careful
2470 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002471const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002472 unsigned long *size,
2473 unsigned long *offset,
2474 char **modname,
2475 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002478 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Rusty Russellcb2a5202008-01-14 00:55:03 -08002480 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002481 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002482 if (within_module_init(addr, mod) ||
2483 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002484 if (modname)
2485 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002486 ret = get_ksymbol(mod, addr, size, offset);
2487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002490 /* Make a copy in here where it's safe */
2491 if (ret) {
2492 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2493 ret = namebuf;
2494 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002495 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002499int lookup_module_symbol_name(unsigned long addr, char *symname)
2500{
2501 struct module *mod;
2502
Rusty Russellcb2a5202008-01-14 00:55:03 -08002503 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002504 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002505 if (within_module_init(addr, mod) ||
2506 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002507 const char *sym;
2508
2509 sym = get_ksymbol(mod, addr, NULL, NULL);
2510 if (!sym)
2511 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002512 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002513 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002514 return 0;
2515 }
2516 }
2517out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002518 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002519 return -ERANGE;
2520}
2521
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002522int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2523 unsigned long *offset, char *modname, char *name)
2524{
2525 struct module *mod;
2526
Rusty Russellcb2a5202008-01-14 00:55:03 -08002527 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002528 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002529 if (within_module_init(addr, mod) ||
2530 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002531 const char *sym;
2532
2533 sym = get_ksymbol(mod, addr, size, offset);
2534 if (!sym)
2535 goto out;
2536 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002537 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002538 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002539 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002540 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002541 return 0;
2542 }
2543 }
2544out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002545 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002546 return -ERANGE;
2547}
2548
Alexey Dobriyanea078902007-05-08 00:28:39 -07002549int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2550 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 struct module *mod;
2553
Rusty Russellcb2a5202008-01-14 00:55:03 -08002554 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002555 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (symnum < mod->num_symtab) {
2557 *value = mod->symtab[symnum].st_value;
2558 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002559 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002560 KSYM_NAME_LEN);
2561 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002562 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002563 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566 symnum -= mod->num_symtab;
2567 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002568 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002569 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572static unsigned long mod_find_symname(struct module *mod, const char *name)
2573{
2574 unsigned int i;
2575
2576 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002577 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2578 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return mod->symtab[i].st_value;
2580 return 0;
2581}
2582
2583/* Look for this name: can be of form module:name. */
2584unsigned long module_kallsyms_lookup_name(const char *name)
2585{
2586 struct module *mod;
2587 char *colon;
2588 unsigned long ret = 0;
2589
2590 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002591 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if ((colon = strchr(name, ':')) != NULL) {
2593 *colon = '\0';
2594 if ((mod = find_module(name)) != NULL)
2595 ret = mod_find_symname(mod, colon+1);
2596 *colon = ':';
2597 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002598 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if ((ret = mod_find_symname(mod, name)) != 0)
2600 break;
2601 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002602 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return ret;
2604}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002605
2606int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2607 struct module *, unsigned long),
2608 void *data)
2609{
2610 struct module *mod;
2611 unsigned int i;
2612 int ret;
2613
2614 list_for_each_entry(mod, &modules, list) {
2615 for (i = 0; i < mod->num_symtab; i++) {
2616 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2617 mod, mod->symtab[i].st_value);
2618 if (ret != 0)
2619 return ret;
2620 }
2621 }
2622 return 0;
2623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624#endif /* CONFIG_KALLSYMS */
2625
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002626static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002627{
2628 int bx = 0;
2629
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002630 if (mod->taints ||
2631 mod->state == MODULE_STATE_GOING ||
2632 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002633 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002634 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002635 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002636 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002637 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002638 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002639 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002640 /*
2641 * TAINT_FORCED_RMMOD: could be added.
2642 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2643 * apply to modules.
2644 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002645
2646 /* Show a - for module-is-being-unloaded */
2647 if (mod->state == MODULE_STATE_GOING)
2648 buf[bx++] = '-';
2649 /* Show a + for module-is-being-loaded */
2650 if (mod->state == MODULE_STATE_COMING)
2651 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002652 buf[bx++] = ')';
2653 }
2654 buf[bx] = '\0';
2655
2656 return buf;
2657}
2658
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002659#ifdef CONFIG_PROC_FS
2660/* Called by the /proc file system to return a list of modules. */
2661static void *m_start(struct seq_file *m, loff_t *pos)
2662{
2663 mutex_lock(&module_mutex);
2664 return seq_list_start(&modules, *pos);
2665}
2666
2667static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2668{
2669 return seq_list_next(p, &modules, pos);
2670}
2671
2672static void m_stop(struct seq_file *m, void *p)
2673{
2674 mutex_unlock(&module_mutex);
2675}
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677static int m_show(struct seq_file *m, void *p)
2678{
2679 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002680 char buf[8];
2681
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002682 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 mod->name, mod->init_size + mod->core_size);
2684 print_unload_info(m, mod);
2685
2686 /* Informative for users. */
2687 seq_printf(m, " %s",
2688 mod->state == MODULE_STATE_GOING ? "Unloading":
2689 mod->state == MODULE_STATE_COMING ? "Loading":
2690 "Live");
2691 /* Used by oprofile and other similar tools. */
2692 seq_printf(m, " 0x%p", mod->module_core);
2693
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002694 /* Taints info */
2695 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002696 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 seq_printf(m, "\n");
2699 return 0;
2700}
2701
2702/* Format: modulename size refcount deps address
2703
2704 Where refcount is a number or -, and deps is a comma-separated list
2705 of depends or -.
2706*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002707static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 .start = m_start,
2709 .next = m_next,
2710 .stop = m_stop,
2711 .show = m_show
2712};
2713
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002714static int modules_open(struct inode *inode, struct file *file)
2715{
2716 return seq_open(file, &modules_op);
2717}
2718
2719static const struct file_operations proc_modules_operations = {
2720 .open = modules_open,
2721 .read = seq_read,
2722 .llseek = seq_lseek,
2723 .release = seq_release,
2724};
2725
2726static int __init proc_modules_init(void)
2727{
2728 proc_create("modules", 0, NULL, &proc_modules_operations);
2729 return 0;
2730}
2731module_init(proc_modules_init);
2732#endif
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734/* Given an address, look for it in the module exception tables. */
2735const struct exception_table_entry *search_module_extables(unsigned long addr)
2736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 const struct exception_table_entry *e = NULL;
2738 struct module *mod;
2739
Rusty Russell24da1cb2007-07-15 23:41:46 -07002740 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002741 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (mod->num_exentries == 0)
2743 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 e = search_extable(mod->extable,
2746 mod->extable + mod->num_exentries - 1,
2747 addr);
2748 if (e)
2749 break;
2750 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002751 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002754 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return e;
2756}
2757
Ingo Molnar4d435f92006-07-03 00:24:24 -07002758/*
Rusty Russelle6104992009-03-31 13:05:31 -06002759 * is_module_address - is this address inside a module?
2760 * @addr: the address to check.
2761 *
2762 * See is_module_text_address() if you simply want to see if the address
2763 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002764 */
Rusty Russelle6104992009-03-31 13:05:31 -06002765bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002766{
Rusty Russelle6104992009-03-31 13:05:31 -06002767 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002768
Rusty Russell24da1cb2007-07-15 23:41:46 -07002769 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002770 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002771 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002772
Rusty Russelle6104992009-03-31 13:05:31 -06002773 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002774}
2775
Rusty Russelle6104992009-03-31 13:05:31 -06002776/*
2777 * __module_address - get the module which contains an address.
2778 * @addr: the address.
2779 *
2780 * Must be called with preempt disabled or module mutex held so that
2781 * module doesn't get freed during this.
2782 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002783struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
2785 struct module *mod;
2786
Rusty Russell3a642e92008-07-22 19:24:28 -05002787 if (addr < module_addr_min || addr > module_addr_max)
2788 return NULL;
2789
Andi Kleend72b3752008-08-30 10:09:00 +02002790 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002791 if (within_module_core(addr, mod)
2792 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return mod;
2794 return NULL;
2795}
Tim Abbottc6b37802008-12-05 19:03:59 -05002796EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Rusty Russelle6104992009-03-31 13:05:31 -06002798/*
2799 * is_module_text_address - is this address inside module code?
2800 * @addr: the address to check.
2801 *
2802 * See is_module_address() if you simply want to see if the address is
2803 * anywhere in a module. See kernel_text_address() for testing if an
2804 * address corresponds to kernel or module code.
2805 */
2806bool is_module_text_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Rusty Russelle6104992009-03-31 13:05:31 -06002808 bool ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Rusty Russell24da1cb2007-07-15 23:41:46 -07002810 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002811 ret = __module_text_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002812 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Rusty Russelle6104992009-03-31 13:05:31 -06002814 return ret;
2815}
2816
2817/*
2818 * __module_text_address - get the module whose code contains an address.
2819 * @addr: the address.
2820 *
2821 * Must be called with preempt disabled or module mutex held so that
2822 * module doesn't get freed during this.
2823 */
2824struct module *__module_text_address(unsigned long addr)
2825{
2826 struct module *mod = __module_address(addr);
2827 if (mod) {
2828 /* Make sure it's within the text section. */
2829 if (!within(addr, mod->module_init, mod->init_text_size)
2830 && !within(addr, mod->module_core, mod->core_text_size))
2831 mod = NULL;
2832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return mod;
2834}
Tim Abbottc6b37802008-12-05 19:03:59 -05002835EXPORT_SYMBOL_GPL(__module_text_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837/* Don't grab lock, we're oopsing. */
2838void print_modules(void)
2839{
2840 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002841 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002844 /* Most callers should already have preempt disabled, but make sure */
2845 preempt_disable();
2846 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002847 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002848 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002849 if (last_unloaded_module[0])
2850 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 printk("\n");
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06002855/* Generate the signature for all relevant module structures here.
2856 * If these change, we don't want to try to parse the module. */
2857void module_layout(struct module *mod,
2858 struct modversion_info *ver,
2859 struct kernel_param *kp,
2860 struct kernel_symbol *ks,
2861 struct marker *marker,
2862 struct tracepoint *tp)
2863{
2864}
2865EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002867
2868#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002869void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002870{
2871 struct module *mod;
2872
2873 mutex_lock(&module_mutex);
2874 list_for_each_entry(mod, &modules, list)
2875 if (!mod->taints)
2876 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002877 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002878 mutex_unlock(&module_mutex);
2879}
2880#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002881
2882#ifdef CONFIG_TRACEPOINTS
2883void module_update_tracepoints(void)
2884{
2885 struct module *mod;
2886
2887 mutex_lock(&module_mutex);
2888 list_for_each_entry(mod, &modules, list)
2889 if (!mod->taints)
2890 tracepoint_update_probe_range(mod->tracepoints,
2891 mod->tracepoints + mod->num_tracepoints);
2892 mutex_unlock(&module_mutex);
2893}
2894
2895/*
2896 * Returns 0 if current not found.
2897 * Returns 1 if current found.
2898 */
2899int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2900{
2901 struct module *iter_mod;
2902 int found = 0;
2903
2904 mutex_lock(&module_mutex);
2905 list_for_each_entry(iter_mod, &modules, list) {
2906 if (!iter_mod->taints) {
2907 /*
2908 * Sorted module list
2909 */
2910 if (iter_mod < iter->module)
2911 continue;
2912 else if (iter_mod > iter->module)
2913 iter->tracepoint = NULL;
2914 found = tracepoint_get_iter_range(&iter->tracepoint,
2915 iter_mod->tracepoints,
2916 iter_mod->tracepoints
2917 + iter_mod->num_tracepoints);
2918 if (found) {
2919 iter->module = iter_mod;
2920 break;
2921 }
2922 }
2923 }
2924 mutex_unlock(&module_mutex);
2925 return found;
2926}
2927#endif