blob: bd15a94f91c10f2394cbf0fad307911716a761dd [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). */
Ashutosh Naik6389a382006-03-23 03:00:46 -080071static DEFINE_MUTEX(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static LIST_HEAD(modules);
73
Rusty Russellc9a3ba52008-01-29 17:13:18 -050074/* Waiting for a module to finish initializing? */
75static DECLARE_WAIT_QUEUE_HEAD(module_wq);
76
Alan Sterne041c682006-03-27 01:16:30 -080077static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Rusty Russelle6104992009-03-31 13:05:31 -060079/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050080static unsigned long module_addr_min = -1UL, module_addr_max = 0;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082int register_module_notifier(struct notifier_block * nb)
83{
Alan Sterne041c682006-03-27 01:16:30 -080084 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86EXPORT_SYMBOL(register_module_notifier);
87
88int unregister_module_notifier(struct notifier_block * nb)
89{
Alan Sterne041c682006-03-27 01:16:30 -080090 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92EXPORT_SYMBOL(unregister_module_notifier);
93
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080094/* We require a truly strong try_module_get(): 0 means failure due to
95 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static inline int strong_try_module_get(struct module *mod)
97{
98 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -050099 return -EBUSY;
100 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500102 else
103 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700106static inline void add_taint_module(struct module *mod, unsigned flag)
107{
108 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700109 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700110}
111
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200112/*
113 * A thread that wants to hold a reference to a module only while it
114 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116void __module_put_and_exit(struct module *mod, long code)
117{
118 module_put(mod);
119 do_exit(code);
120}
121EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Find a module section: 0 means not found. */
124static unsigned int find_sec(Elf_Ehdr *hdr,
125 Elf_Shdr *sechdrs,
126 const char *secstrings,
127 const char *name)
128{
129 unsigned int i;
130
131 for (i = 1; i < hdr->e_shnum; i++)
132 /* Alloc bit cleared means "ignore it." */
133 if ((sechdrs[i].sh_flags & SHF_ALLOC)
134 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
135 return i;
136 return 0;
137}
138
Rusty Russell5e458cc2008-10-22 10:00:13 -0500139/* Find a module section, or NULL. */
140static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
141 const char *secstrings, const char *name)
142{
143 /* Section 0 has sh_addr 0. */
144 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
145}
146
147/* Find a module section, or NULL. Fill in number of "objects" in section. */
148static void *section_objs(Elf_Ehdr *hdr,
149 Elf_Shdr *sechdrs,
150 const char *secstrings,
151 const char *name,
152 size_t object_size,
153 unsigned int *num)
154{
155 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
156
157 /* Section 0 has sh_addr 0 and sh_size 0. */
158 *num = sechdrs[sec].sh_size / object_size;
159 return (void *)sechdrs[sec].sh_addr;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Provided by the linker */
163extern const struct kernel_symbol __start___ksymtab[];
164extern const struct kernel_symbol __stop___ksymtab[];
165extern const struct kernel_symbol __start___ksymtab_gpl[];
166extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800167extern const struct kernel_symbol __start___ksymtab_gpl_future[];
168extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700169extern const struct kernel_symbol __start___ksymtab_gpl_future[];
170extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171extern const unsigned long __start___kcrctab[];
172extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800173extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500174#ifdef CONFIG_UNUSED_SYMBOLS
175extern const struct kernel_symbol __start___ksymtab_unused[];
176extern const struct kernel_symbol __stop___ksymtab_unused[];
177extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
178extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700179extern const unsigned long __start___kcrctab_unused[];
180extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183#ifndef CONFIG_MODVERSIONS
184#define symversion(base, idx) NULL
185#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800186#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
188
Rusty Russelldafd0942008-07-22 19:24:25 -0500189struct symsearch {
190 const struct kernel_symbol *start, *stop;
191 const unsigned long *crcs;
192 enum {
193 NOT_GPL_ONLY,
194 GPL_ONLY,
195 WILL_BE_GPL_ONLY,
196 } licence;
197 bool unused;
198};
199
200static bool each_symbol_in_section(const struct symsearch *arr,
201 unsigned int arrsize,
202 struct module *owner,
203 bool (*fn)(const struct symsearch *syms,
204 struct module *owner,
205 unsigned int symnum, void *data),
206 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100207{
Rusty Russelldafd0942008-07-22 19:24:25 -0500208 unsigned int i, j;
209
210 for (j = 0; j < arrsize; j++) {
211 for (i = 0; i < arr[j].stop - arr[j].start; i++)
212 if (fn(&arr[j], owner, i, data))
213 return true;
214 }
215
216 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100217}
218
Rusty Russelldafd0942008-07-22 19:24:25 -0500219/* Returns true as soon as fn returns true, otherwise false. */
220static bool each_symbol(bool (*fn)(const struct symsearch *arr,
221 struct module *owner,
222 unsigned int symnum, void *data),
223 void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700224{
Rusty Russelldafd0942008-07-22 19:24:25 -0500225 struct module *mod;
226 const struct symsearch arr[] = {
227 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
228 NOT_GPL_ONLY, false },
229 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
230 __start___kcrctab_gpl,
231 GPL_ONLY, false },
232 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
233 __start___kcrctab_gpl_future,
234 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500235#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500236 { __start___ksymtab_unused, __stop___ksymtab_unused,
237 __start___kcrctab_unused,
238 NOT_GPL_ONLY, true },
239 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
240 __start___kcrctab_unused_gpl,
241 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500242#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500243 };
244
245 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
246 return true;
247
Andi Kleend72b3752008-08-30 10:09:00 +0200248 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500249 struct symsearch arr[] = {
250 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
251 NOT_GPL_ONLY, false },
252 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
253 mod->gpl_crcs,
254 GPL_ONLY, false },
255 { mod->gpl_future_syms,
256 mod->gpl_future_syms + mod->num_gpl_future_syms,
257 mod->gpl_future_crcs,
258 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500259#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500260 { mod->unused_syms,
261 mod->unused_syms + mod->num_unused_syms,
262 mod->unused_crcs,
263 NOT_GPL_ONLY, true },
264 { mod->unused_gpl_syms,
265 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
266 mod->unused_gpl_crcs,
267 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500268#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500269 };
270
271 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
272 return true;
273 }
274 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700275}
276
Rusty Russelldafd0942008-07-22 19:24:25 -0500277struct find_symbol_arg {
278 /* Input */
279 const char *name;
280 bool gplok;
281 bool warn;
282
283 /* Output */
284 struct module *owner;
285 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500286 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500287};
288
289static bool find_symbol_in_section(const struct symsearch *syms,
290 struct module *owner,
291 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500292{
Rusty Russelldafd0942008-07-22 19:24:25 -0500293 struct find_symbol_arg *fsa = data;
294
295 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
296 return false;
297
298 if (!fsa->gplok) {
299 if (syms->licence == GPL_ONLY)
300 return false;
301 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
302 printk(KERN_WARNING "Symbol %s is being used "
303 "by a non-GPL module, which will not "
304 "be allowed in the future\n", fsa->name);
305 printk(KERN_WARNING "Please see the file "
306 "Documentation/feature-removal-schedule.txt "
307 "in the kernel source tree for more details.\n");
308 }
309 }
310
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500311#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500312 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500313 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500314 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500315 printk(KERN_WARNING
316 "This symbol will go away in the future.\n");
317 printk(KERN_WARNING
318 "Please evalute if this is the right api to use and if "
319 "it really is, submit a report the linux kernel "
320 "mailinglist together with submitting your code for "
321 "inclusion.\n");
322 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500323#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500324
325 fsa->owner = owner;
326 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500327 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500328 return true;
329}
330
Tim Abbott414fd312008-12-05 19:03:56 -0500331/* Find a symbol and return it, along with, (optional) crc and
332 * (optional) module which owns it */
333static const struct kernel_symbol *find_symbol(const char *name,
334 struct module **owner,
335 const unsigned long **crc,
336 bool gplok,
337 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Rusty Russelldafd0942008-07-22 19:24:25 -0500339 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Rusty Russelldafd0942008-07-22 19:24:25 -0500341 fsa.name = name;
342 fsa.gplok = gplok;
343 fsa.warn = warn;
344
345 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500346 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500347 *owner = fsa.owner;
348 if (crc)
349 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500350 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500354 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* Search for module by name: must hold module_mutex. */
358static struct module *find_module(const char *name)
359{
360 struct module *mod;
361
362 list_for_each_entry(mod, &modules, list) {
363 if (strcmp(mod->name, name) == 0)
364 return mod;
365 }
366 return NULL;
367}
368
369#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900370
371#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
372
373static void *percpu_modalloc(unsigned long size, unsigned long align,
374 const char *name)
375{
376 void *ptr;
377
378 if (align > PAGE_SIZE) {
379 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
380 name, align, PAGE_SIZE);
381 align = PAGE_SIZE;
382 }
383
Tejun Heoedcb4632009-03-06 14:33:59 +0900384 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900385 if (!ptr)
386 printk(KERN_WARNING
387 "Could not allocate %lu bytes percpu data\n", size);
388 return ptr;
389}
390
391static void percpu_modfree(void *freeme)
392{
393 free_percpu(freeme);
394}
395
396#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/* Number of blocks used and allocated. */
399static unsigned int pcpu_num_used, pcpu_num_allocated;
400/* Size of each block. -ve means used. */
401static int *pcpu_size;
402
403static int split_block(unsigned int i, unsigned short size)
404{
405 /* Reallocation required? */
406 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700407 int *new;
408
409 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
410 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!new)
412 return 0;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 pcpu_size = new;
416 }
417
418 /* Insert a new subblock */
419 memmove(&pcpu_size[i+1], &pcpu_size[i],
420 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
421 pcpu_num_used++;
422
423 pcpu_size[i+1] -= size;
424 pcpu_size[i] = size;
425 return 1;
426}
427
428static inline unsigned int block_size(int val)
429{
430 if (val < 0)
431 return -val;
432 return val;
433}
434
Rusty Russell842bbaa2005-08-01 21:11:47 -0700435static void *percpu_modalloc(unsigned long size, unsigned long align,
436 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 unsigned long extra;
439 unsigned int i;
440 void *ptr;
441
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200442 if (align > PAGE_SIZE) {
443 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
444 name, align, PAGE_SIZE);
445 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 ptr = __per_cpu_start;
449 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
450 /* Extra for alignment requirement. */
451 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
452 BUG_ON(i == 0 && extra != 0);
453
454 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
455 continue;
456
457 /* Transfer extra to previous block. */
458 if (pcpu_size[i-1] < 0)
459 pcpu_size[i-1] -= extra;
460 else
461 pcpu_size[i-1] += extra;
462 pcpu_size[i] -= extra;
463 ptr += extra;
464
465 /* Split block if warranted */
466 if (pcpu_size[i] - size > sizeof(unsigned long))
467 if (!split_block(i, size))
468 return NULL;
469
470 /* Mark allocated */
471 pcpu_size[i] = -pcpu_size[i];
472 return ptr;
473 }
474
475 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
476 size);
477 return NULL;
478}
479
480static void percpu_modfree(void *freeme)
481{
482 unsigned int i;
483 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
484
485 /* First entry is core kernel percpu data. */
486 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
487 if (ptr == freeme) {
488 pcpu_size[i] = -pcpu_size[i];
489 goto free;
490 }
491 }
492 BUG();
493
494 free:
495 /* Merge with previous? */
496 if (pcpu_size[i-1] >= 0) {
497 pcpu_size[i-1] += pcpu_size[i];
498 pcpu_num_used--;
499 memmove(&pcpu_size[i], &pcpu_size[i+1],
500 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
501 i--;
502 }
503 /* Merge with next? */
504 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
505 pcpu_size[i] += pcpu_size[i+1];
506 pcpu_num_used--;
507 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
508 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
509 }
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static int percpu_modinit(void)
513{
514 pcpu_num_used = 2;
515 pcpu_num_allocated = 2;
516 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
517 GFP_KERNEL);
518 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200519 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Free room. */
521 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
522 if (pcpu_size[1] < 0) {
523 printk(KERN_ERR "No per-cpu room for modules.\n");
524 pcpu_num_used = 1;
525 }
526
527 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900530
Tejun Heofbf59bc2009-02-20 16:29:08 +0900531#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
532
Tejun Heo6b588c12009-02-20 16:29:07 +0900533static unsigned int find_pcpusec(Elf_Ehdr *hdr,
534 Elf_Shdr *sechdrs,
535 const char *secstrings)
536{
537 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
538}
539
540static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
541{
542 int cpu;
543
544 for_each_possible_cpu(cpu)
545 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900549
Rusty Russell842bbaa2005-08-01 21:11:47 -0700550static inline void *percpu_modalloc(unsigned long size, unsigned long align,
551 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 return NULL;
554}
555static inline void percpu_modfree(void *pcpuptr)
556{
557 BUG();
558}
559static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
560 Elf_Shdr *sechdrs,
561 const char *secstrings)
562{
563 return 0;
564}
565static inline void percpu_modcopy(void *pcpudst, const void *src,
566 unsigned long size)
567{
568 /* pcpusec should be 0, and size of that section should be 0. */
569 BUG_ON(size != 0);
570}
Tejun Heo6b588c12009-02-20 16:29:07 +0900571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#endif /* CONFIG_SMP */
573
Matt Domschc988d2b2005-06-23 22:05:15 -0700574#define MODINFO_ATTR(field) \
575static void setup_modinfo_##field(struct module *mod, const char *s) \
576{ \
577 mod->field = kstrdup(s, GFP_KERNEL); \
578} \
579static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
580 struct module *mod, char *buffer) \
581{ \
582 return sprintf(buffer, "%s\n", mod->field); \
583} \
584static int modinfo_##field##_exists(struct module *mod) \
585{ \
586 return mod->field != NULL; \
587} \
588static void free_modinfo_##field(struct module *mod) \
589{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700590 kfree(mod->field); \
591 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700592} \
593static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900594 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700595 .show = show_modinfo_##field, \
596 .setup = setup_modinfo_##field, \
597 .test = modinfo_##field##_exists, \
598 .free = free_modinfo_##field, \
599};
600
601MODINFO_ATTR(version);
602MODINFO_ATTR(srcversion);
603
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100604static char last_unloaded_module[MODULE_NAME_LEN+1];
605
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800606#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/* Init the unload section of the module. */
608static void module_unload_init(struct module *mod)
609{
Eric Dumazet720eba32009-02-03 13:31:36 +1030610 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030613 for_each_possible_cpu(cpu)
614 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030616 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Backwards compatibility macros put refcount during init. */
618 mod->waiter = current;
619}
620
621/* modules using other modules */
622struct module_use
623{
624 struct list_head list;
625 struct module *module_which_uses;
626};
627
628/* Does a already use b? */
629static int already_uses(struct module *a, struct module *b)
630{
631 struct module_use *use;
632
633 list_for_each_entry(use, &b->modules_which_use_me, list) {
634 if (use->module_which_uses == a) {
635 DEBUGP("%s uses %s!\n", a->name, b->name);
636 return 1;
637 }
638 }
639 DEBUGP("%s does not use %s!\n", a->name, b->name);
640 return 0;
641}
642
643/* Module a uses b */
644static int use_module(struct module *a, struct module *b)
645{
646 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500647 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (b == NULL || already_uses(a, b)) return 1;
650
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500651 /* If we're interrupted or time out, we fail. */
652 if (wait_event_interruptible_timeout(
653 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
654 30 * HZ) <= 0) {
655 printk("%s: gave up waiting for init of module %s.\n",
656 a->name, b->name);
657 return 0;
658 }
659
660 /* If strong_try_module_get() returned a different error, we fail. */
661 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663
664 DEBUGP("Allocating new usage for %s.\n", a->name);
665 use = kmalloc(sizeof(*use), GFP_ATOMIC);
666 if (!use) {
667 printk("%s: out of memory loading\n", a->name);
668 module_put(b);
669 return 0;
670 }
671
672 use->module_which_uses = a;
673 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100674 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 1;
676}
677
678/* Clear the unload stuff of the module. */
679static void module_unload_free(struct module *mod)
680{
681 struct module *i;
682
683 list_for_each_entry(i, &modules, list) {
684 struct module_use *use;
685
686 list_for_each_entry(use, &i->modules_which_use_me, list) {
687 if (use->module_which_uses == mod) {
688 DEBUGP("%s unusing %s\n", mod->name, i->name);
689 module_put(i);
690 list_del(&use->list);
691 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100692 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* There can be at most one match. */
694 break;
695 }
696 }
697 }
698}
699
700#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800701static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 int ret = (flags & O_TRUNC);
704 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800705 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return ret;
707}
708#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800709static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 return 0;
712}
713#endif /* CONFIG_MODULE_FORCE_UNLOAD */
714
715struct stopref
716{
717 struct module *mod;
718 int flags;
719 int *forced;
720};
721
722/* Whole machine is stopped with interrupts off when this runs. */
723static int __try_stop_module(void *_sref)
724{
725 struct stopref *sref = _sref;
726
Rusty Russellda39ba52008-07-22 19:24:25 -0500727 /* If it's not unused, quit unless we're forcing. */
728 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800729 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EWOULDBLOCK;
731 }
732
733 /* Mark it as dying. */
734 sref->mod->state = MODULE_STATE_GOING;
735 return 0;
736}
737
738static int try_stop_module(struct module *mod, int flags, int *forced)
739{
Rusty Russellda39ba52008-07-22 19:24:25 -0500740 if (flags & O_NONBLOCK) {
741 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500743 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500744 } else {
745 /* We don't need to stop the machine for this. */
746 mod->state = MODULE_STATE_GOING;
747 synchronize_sched();
748 return 0;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752unsigned int module_refcount(struct module *mod)
753{
Eric Dumazet720eba32009-02-03 13:31:36 +1030754 unsigned int total = 0;
755 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Eric Dumazet720eba32009-02-03 13:31:36 +1030757 for_each_possible_cpu(cpu)
758 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return total;
760}
761EXPORT_SYMBOL(module_refcount);
762
763/* This exists whether we can unload or not */
764static void free_module(struct module *mod);
765
766static void wait_for_zero_refcount(struct module *mod)
767{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500768 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800769 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 for (;;) {
771 DEBUGP("Looking at refcount...\n");
772 set_current_state(TASK_UNINTERRUPTIBLE);
773 if (module_refcount(mod) == 0)
774 break;
775 schedule();
776 }
777 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800778 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100781SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
782 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800785 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int ret, forced = 0;
787
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800788 if (!capable(CAP_SYS_MODULE))
789 return -EPERM;
790
791 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
792 return -EFAULT;
793 name[MODULE_NAME_LEN-1] = '\0';
794
Heiko Carstens9e018922008-12-22 12:36:31 +0100795 /* Create stop_machine threads since free_module relies on
796 * a non-failing stop_machine call. */
797 ret = stop_machine_create();
798 if (ret)
799 return ret;
800
801 if (mutex_lock_interruptible(&module_mutex) != 0) {
802 ret = -EINTR;
803 goto out_stop;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 mod = find_module(name);
807 if (!mod) {
808 ret = -ENOENT;
809 goto out;
810 }
811
812 if (!list_empty(&mod->modules_which_use_me)) {
813 /* Other modules depend on us: get rid of them first. */
814 ret = -EWOULDBLOCK;
815 goto out;
816 }
817
818 /* Doing init or already dying? */
819 if (mod->state != MODULE_STATE_LIVE) {
820 /* FIXME: if (force), slam module count and wake up
821 waiter --RR */
822 DEBUGP("%s already dying\n", mod->name);
823 ret = -EBUSY;
824 goto out;
825 }
826
827 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700828 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800829 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!forced) {
831 /* This module can't be removed */
832 ret = -EBUSY;
833 goto out;
834 }
835 }
836
837 /* Set this up before setting mod->state */
838 mod->waiter = current;
839
840 /* Stop the machine so refcounts can't move and disable module. */
841 ret = try_stop_module(mod, flags, &forced);
842 if (ret != 0)
843 goto out;
844
845 /* Never wait if forced. */
846 if (!forced && module_refcount(mod) != 0)
847 wait_for_zero_refcount(mod);
848
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200849 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200851 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200853 blocking_notifier_call_chain(&module_notify_list,
854 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800855 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200856 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100857 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500858 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500859 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 free_module(mod);
861
862 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800863 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100864out_stop:
865 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return ret;
867}
868
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800869static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct module_use *use;
872 int printed_something = 0;
873
874 seq_printf(m, " %u ", module_refcount(mod));
875
876 /* Always include a trailing , so userspace can differentiate
877 between this and the old multi-field proc format. */
878 list_for_each_entry(use, &mod->modules_which_use_me, list) {
879 printed_something = 1;
880 seq_printf(m, "%s,", use->module_which_uses->name);
881 }
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (mod->init != NULL && mod->exit == NULL) {
884 printed_something = 1;
885 seq_printf(m, "[permanent],");
886 }
887
888 if (!printed_something)
889 seq_printf(m, "-");
890}
891
892void __symbol_put(const char *symbol)
893{
894 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Rusty Russell24da1cb2007-07-15 23:41:46 -0700896 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500897 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 BUG();
899 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700900 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902EXPORT_SYMBOL(__symbol_put);
903
904void symbol_put_addr(void *addr)
905{
Trent Piepho5e376612006-05-15 09:44:06 -0700906 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trent Piepho5e376612006-05-15 09:44:06 -0700908 if (core_kernel_text((unsigned long)addr))
909 return;
910
911 if (!(modaddr = module_text_address((unsigned long)addr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 BUG();
Trent Piepho5e376612006-05-15 09:44:06 -0700913 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915EXPORT_SYMBOL_GPL(symbol_put_addr);
916
917static ssize_t show_refcnt(struct module_attribute *mattr,
918 struct module *mod, char *buffer)
919{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400920 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900924 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .show = show_refcnt,
926};
927
Al Virof6a57032006-10-18 01:47:25 -0400928void module_put(struct module *module)
929{
930 if (module) {
931 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030932 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400933 /* Maybe they're waiting for us to drop reference? */
934 if (unlikely(!module_is_live(module)))
935 wake_up_process(module->waiter);
936 put_cpu();
937 }
938}
939EXPORT_SYMBOL(module_put);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800942static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 /* We don't know the usage count, or what modules are using. */
945 seq_printf(m, " - -");
946}
947
948static inline void module_unload_free(struct module *mod)
949{
950}
951
952static inline int use_module(struct module *a, struct module *b)
953{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500954 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static inline void module_unload_init(struct module *mod)
958{
959}
960#endif /* CONFIG_MODULE_UNLOAD */
961
Kay Sievers1f717402006-11-24 12:15:25 +0100962static ssize_t show_initstate(struct module_attribute *mattr,
963 struct module *mod, char *buffer)
964{
965 const char *state = "unknown";
966
967 switch (mod->state) {
968 case MODULE_STATE_LIVE:
969 state = "live";
970 break;
971 case MODULE_STATE_COMING:
972 state = "coming";
973 break;
974 case MODULE_STATE_GOING:
975 state = "going";
976 break;
977 }
978 return sprintf(buffer, "%s\n", state);
979}
980
981static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900982 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100983 .show = show_initstate,
984};
985
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800986static struct module_attribute *modinfo_attrs[] = {
987 &modinfo_version,
988 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100989 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800990#ifdef CONFIG_MODULE_UNLOAD
991 &refcnt,
992#endif
993 NULL,
994};
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static const char vermagic[] = VERMAGIC_STRING;
997
Linus Torvalds826e4502008-05-04 17:04:16 -0700998static int try_to_force_load(struct module *mod, const char *symname)
999{
1000#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001001 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -07001002 printk("%s: no version for \"%s\" found: kernel tainted.\n",
1003 mod->name, symname);
1004 add_taint_module(mod, TAINT_FORCED_MODULE);
1005 return 0;
1006#else
1007 return -ENOEXEC;
1008#endif
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#ifdef CONFIG_MODVERSIONS
1012static int check_version(Elf_Shdr *sechdrs,
1013 unsigned int versindex,
1014 const char *symname,
1015 struct module *mod,
1016 const unsigned long *crc)
1017{
1018 unsigned int i, num_versions;
1019 struct modversion_info *versions;
1020
1021 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1022 if (!crc)
1023 return 1;
1024
Rusty Russella5dd6972008-05-09 16:24:21 +10001025 /* No versions at all? modprobe --force does this. */
1026 if (versindex == 0)
1027 return try_to_force_load(mod, symname) == 0;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 versions = (void *) sechdrs[versindex].sh_addr;
1030 num_versions = sechdrs[versindex].sh_size
1031 / sizeof(struct modversion_info);
1032
1033 for (i = 0; i < num_versions; i++) {
1034 if (strcmp(versions[i].name, symname) != 0)
1035 continue;
1036
1037 if (versions[i].crc == *crc)
1038 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 DEBUGP("Found checksum %lX vs module %lX\n",
1040 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001041 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001043
Rusty Russella5dd6972008-05-09 16:24:21 +10001044 printk(KERN_WARNING "%s: no symbol version for %s\n",
1045 mod->name, symname);
1046 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001047
1048bad_version:
1049 printk("%s: disagrees about version of symbol %s\n",
1050 mod->name, symname);
1051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1055 unsigned int versindex,
1056 struct module *mod)
1057{
1058 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Tim Abbott414fd312008-12-05 19:03:56 -05001060 if (!find_symbol("struct_module", NULL, &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001062 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Rusty Russell91e37a72008-05-09 16:25:28 +10001065/* First part is kernel version, which we ignore if module has crcs. */
1066static inline int same_magic(const char *amagic, const char *bmagic,
1067 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Rusty Russell91e37a72008-05-09 16:25:28 +10001069 if (has_crcs) {
1070 amagic += strcspn(amagic, " ");
1071 bmagic += strcspn(bmagic, " ");
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return strcmp(amagic, bmagic) == 0;
1074}
1075#else
1076static inline int check_version(Elf_Shdr *sechdrs,
1077 unsigned int versindex,
1078 const char *symname,
1079 struct module *mod,
1080 const unsigned long *crc)
1081{
1082 return 1;
1083}
1084
1085static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1086 unsigned int versindex,
1087 struct module *mod)
1088{
1089 return 1;
1090}
1091
Rusty Russell91e37a72008-05-09 16:25:28 +10001092static inline int same_magic(const char *amagic, const char *bmagic,
1093 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 return strcmp(amagic, bmagic) == 0;
1096}
1097#endif /* CONFIG_MODVERSIONS */
1098
1099/* Resolve a symbol for this module. I.e. if we find one, record usage.
1100 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001101static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1102 unsigned int versindex,
1103 const char *name,
1104 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001107 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 const unsigned long *crc;
1109
Tim Abbott414fd312008-12-05 19:03:56 -05001110 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001111 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001112 /* use_module can fail due to OOM,
1113 or module initialization or unloading */
1114 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1116 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001117 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Tim Abbott414fd312008-12-05 19:03:56 -05001119 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
1123 * /sys/module/foo/sections stuff
1124 * J. Corbet <corbet@lwn.net>
1125 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001126#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001127struct module_sect_attr
1128{
1129 struct module_attribute mattr;
1130 char *name;
1131 unsigned long address;
1132};
1133
1134struct module_sect_attrs
1135{
1136 struct attribute_group grp;
1137 unsigned int nsections;
1138 struct module_sect_attr attrs[0];
1139};
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static ssize_t module_sect_show(struct module_attribute *mattr,
1142 struct module *mod, char *buf)
1143{
1144 struct module_sect_attr *sattr =
1145 container_of(mattr, struct module_sect_attr, mattr);
1146 return sprintf(buf, "0x%lx\n", sattr->address);
1147}
1148
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001149static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1150{
Rusty Russella58730c2008-03-13 09:03:44 +00001151 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001152
1153 for (section = 0; section < sect_attrs->nsections; section++)
1154 kfree(sect_attrs->attrs[section].name);
1155 kfree(sect_attrs);
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static void add_sect_attrs(struct module *mod, unsigned int nsect,
1159 char *secstrings, Elf_Shdr *sechdrs)
1160{
1161 unsigned int nloaded = 0, i, size[2];
1162 struct module_sect_attrs *sect_attrs;
1163 struct module_sect_attr *sattr;
1164 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Count loaded sections and allocate structures */
1167 for (i = 0; i < nsect; i++)
1168 if (sechdrs[i].sh_flags & SHF_ALLOC)
1169 nloaded++;
1170 size[0] = ALIGN(sizeof(*sect_attrs)
1171 + nloaded * sizeof(sect_attrs->attrs[0]),
1172 sizeof(sect_attrs->grp.attrs[0]));
1173 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001174 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1175 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return;
1177
1178 /* Setup section attributes. */
1179 sect_attrs->grp.name = "sections";
1180 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1181
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001182 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sattr = &sect_attrs->attrs[0];
1184 gattr = &sect_attrs->grp.attrs[0];
1185 for (i = 0; i < nsect; i++) {
1186 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1187 continue;
1188 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001189 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1190 GFP_KERNEL);
1191 if (sattr->name == NULL)
1192 goto out;
1193 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sattr->mattr.show = module_sect_show;
1195 sattr->mattr.store = NULL;
1196 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sattr->mattr.attr.mode = S_IRUGO;
1198 *(gattr++) = &(sattr++)->mattr.attr;
1199 }
1200 *gattr = NULL;
1201
1202 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1203 goto out;
1204
1205 mod->sect_attrs = sect_attrs;
1206 return;
1207 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001208 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211static void remove_sect_attrs(struct module *mod)
1212{
1213 if (mod->sect_attrs) {
1214 sysfs_remove_group(&mod->mkobj.kobj,
1215 &mod->sect_attrs->grp);
1216 /* We are positive that no one is using any sect attrs
1217 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001218 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 mod->sect_attrs = NULL;
1220 }
1221}
1222
Roland McGrath6d760132007-10-16 23:26:40 -07001223/*
1224 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1225 */
1226
1227struct module_notes_attrs {
1228 struct kobject *dir;
1229 unsigned int notes;
1230 struct bin_attribute attrs[0];
1231};
1232
1233static ssize_t module_notes_read(struct kobject *kobj,
1234 struct bin_attribute *bin_attr,
1235 char *buf, loff_t pos, size_t count)
1236{
1237 /*
1238 * The caller checked the pos and count against our size.
1239 */
1240 memcpy(buf, bin_attr->private + pos, count);
1241 return count;
1242}
1243
1244static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1245 unsigned int i)
1246{
1247 if (notes_attrs->dir) {
1248 while (i-- > 0)
1249 sysfs_remove_bin_file(notes_attrs->dir,
1250 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001251 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001252 }
1253 kfree(notes_attrs);
1254}
1255
1256static void add_notes_attrs(struct module *mod, unsigned int nsect,
1257 char *secstrings, Elf_Shdr *sechdrs)
1258{
1259 unsigned int notes, loaded, i;
1260 struct module_notes_attrs *notes_attrs;
1261 struct bin_attribute *nattr;
1262
1263 /* Count notes sections and allocate structures. */
1264 notes = 0;
1265 for (i = 0; i < nsect; i++)
1266 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1267 (sechdrs[i].sh_type == SHT_NOTE))
1268 ++notes;
1269
1270 if (notes == 0)
1271 return;
1272
1273 notes_attrs = kzalloc(sizeof(*notes_attrs)
1274 + notes * sizeof(notes_attrs->attrs[0]),
1275 GFP_KERNEL);
1276 if (notes_attrs == NULL)
1277 return;
1278
1279 notes_attrs->notes = notes;
1280 nattr = &notes_attrs->attrs[0];
1281 for (loaded = i = 0; i < nsect; ++i) {
1282 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1283 continue;
1284 if (sechdrs[i].sh_type == SHT_NOTE) {
1285 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1286 nattr->attr.mode = S_IRUGO;
1287 nattr->size = sechdrs[i].sh_size;
1288 nattr->private = (void *) sechdrs[i].sh_addr;
1289 nattr->read = module_notes_read;
1290 ++nattr;
1291 }
1292 ++loaded;
1293 }
1294
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001295 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001296 if (!notes_attrs->dir)
1297 goto out;
1298
1299 for (i = 0; i < notes; ++i)
1300 if (sysfs_create_bin_file(notes_attrs->dir,
1301 &notes_attrs->attrs[i]))
1302 goto out;
1303
1304 mod->notes_attrs = notes_attrs;
1305 return;
1306
1307 out:
1308 free_notes_attrs(notes_attrs, i);
1309}
1310
1311static void remove_notes_attrs(struct module *mod)
1312{
1313 if (mod->notes_attrs)
1314 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1320 char *sectstrings, Elf_Shdr *sechdrs)
1321{
1322}
1323
1324static inline void remove_sect_attrs(struct module *mod)
1325{
1326}
Roland McGrath6d760132007-10-16 23:26:40 -07001327
1328static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1329 char *sectstrings, Elf_Shdr *sechdrs)
1330{
1331}
1332
1333static inline void remove_notes_attrs(struct module *mod)
1334{
1335}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001336#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Randy Dunlapef665c12007-02-13 15:19:06 -08001338#ifdef CONFIG_SYSFS
1339int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001340{
1341 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001342 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001343 int error = 0;
1344 int i;
1345
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001346 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1347 (ARRAY_SIZE(modinfo_attrs) + 1)),
1348 GFP_KERNEL);
1349 if (!mod->modinfo_attrs)
1350 return -ENOMEM;
1351
1352 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001353 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1354 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001355 (attr->test && attr->test(mod))) {
1356 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001357 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1358 ++temp_attr;
1359 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001360 }
1361 return error;
1362}
1363
Randy Dunlapef665c12007-02-13 15:19:06 -08001364void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001365{
1366 struct module_attribute *attr;
1367 int i;
1368
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001369 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1370 /* pick a field to test for end of list */
1371 if (!attr->attr.name)
1372 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001373 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001374 if (attr->free)
1375 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001376 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001377 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Randy Dunlapef665c12007-02-13 15:19:06 -08001380int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001383 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001385 if (!module_sysfs_initialized) {
1386 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001387 mod->name);
1388 err = -EINVAL;
1389 goto out;
1390 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001391
1392 kobj = kset_find_obj(module_kset, mod->name);
1393 if (kobj) {
1394 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1395 kobject_put(kobj);
1396 err = -EINVAL;
1397 goto out;
1398 }
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001401
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001402 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1403 mod->mkobj.kobj.kset = module_kset;
1404 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1405 "%s", mod->name);
1406 if (err)
1407 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001408
Kay Sievers97c146e2007-11-29 23:46:11 +01001409 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001410out:
1411 return err;
1412}
1413
Randy Dunlapef665c12007-02-13 15:19:06 -08001414int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001415 struct kernel_param *kparam,
1416 unsigned int num_params)
1417{
1418 int err;
1419
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001420 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001421 if (!mod->holders_dir) {
1422 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001423 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001424 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 err = module_param_sysfs_setup(mod, kparam, num_params);
1427 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001428 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Matt Domschc988d2b2005-06-23 22:05:15 -07001430 err = module_add_modinfo_attrs(mod);
1431 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001432 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001433
Kay Sieverse17e0f52006-11-24 12:15:25 +01001434 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
1436
Kay Sieverse17e0f52006-11-24 12:15:25 +01001437out_unreg_param:
1438 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001439out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001440 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001441out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001442 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return err;
1444}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001445
1446static void mod_sysfs_fini(struct module *mod)
1447{
1448 kobject_put(&mod->mkobj.kobj);
1449}
1450
1451#else /* CONFIG_SYSFS */
1452
1453static void mod_sysfs_fini(struct module *mod)
1454{
1455}
1456
1457#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459static void mod_kobject_remove(struct module *mod)
1460{
Matt Domschc988d2b2005-06-23 22:05:15 -07001461 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001463 kobject_put(mod->mkobj.drivers_dir);
1464 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001465 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468/*
1469 * unlink the module with the whole machine is stopped with interrupts off
1470 * - this defends against kallsyms not taking locks
1471 */
1472static int __unlink_module(void *_mod)
1473{
1474 struct module *mod = _mod;
1475 list_del(&mod->list);
1476 return 0;
1477}
1478
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001479/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480static void free_module(struct module *mod)
1481{
1482 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001483 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001484 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 remove_sect_attrs(mod);
1486 mod_kobject_remove(mod);
1487
1488 /* Arch-specific cleanup. */
1489 module_arch_cleanup(mod);
1490
1491 /* Module unload stuff */
1492 module_unload_free(mod);
1493
Rusty Russelle180a6b2009-03-31 13:05:29 -06001494 /* Free any allocated parameters. */
1495 destroy_params(mod->kp, mod->num_kp);
1496
Steven Rostedtfed19392008-08-14 22:47:19 -04001497 /* release any pointers to mcount in this module */
1498 ftrace_release(mod->module_core, mod->core_size);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* This may be NULL, but that's OK */
1501 module_free(mod, mod->module_init);
1502 kfree(mod->args);
1503 if (mod->percpu)
1504 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301505#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1506 if (mod->refptr)
1507 percpu_modfree(mod->refptr);
1508#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001509 /* Free lock-classes: */
1510 lockdep_free_key_range(mod->module_core, mod->core_size);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* Finally, free the core (containing the module structure) */
1513 module_free(mod, mod->module_core);
1514}
1515
1516void *__symbol_get(const char *symbol)
1517{
1518 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001519 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rusty Russell24da1cb2007-07-15 23:41:46 -07001521 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001522 sym = find_symbol(symbol, &owner, NULL, true, true);
1523 if (sym && strong_try_module_get(owner))
1524 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001525 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Tim Abbott414fd312008-12-05 19:03:56 -05001527 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529EXPORT_SYMBOL_GPL(__symbol_get);
1530
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001531/*
1532 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001533 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001534 */
1535static int verify_export_symbols(struct module *mod)
1536{
Rusty Russellb2111042008-05-01 21:15:00 -05001537 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001538 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001539 const struct kernel_symbol *s;
1540 struct {
1541 const struct kernel_symbol *sym;
1542 unsigned int num;
1543 } arr[] = {
1544 { mod->syms, mod->num_syms },
1545 { mod->gpl_syms, mod->num_gpl_syms },
1546 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001547#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001548 { mod->unused_syms, mod->num_unused_syms },
1549 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001550#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001551 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001552
Rusty Russellb2111042008-05-01 21:15:00 -05001553 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1554 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001555 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001556 printk(KERN_ERR
1557 "%s: exports duplicate symbol %s"
1558 " (owned by %s)\n",
1559 mod->name, s->name, module_name(owner));
1560 return -ENOEXEC;
1561 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001562 }
Rusty Russellb2111042008-05-01 21:15:00 -05001563 }
1564 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001565}
1566
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001567/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568static int simplify_symbols(Elf_Shdr *sechdrs,
1569 unsigned int symindex,
1570 const char *strtab,
1571 unsigned int versindex,
1572 unsigned int pcpuindex,
1573 struct module *mod)
1574{
1575 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1576 unsigned long secbase;
1577 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1578 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001579 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 for (i = 1; i < n; i++) {
1582 switch (sym[i].st_shndx) {
1583 case SHN_COMMON:
1584 /* We compiled with -fno-common. These are not
1585 supposed to happen. */
1586 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1587 printk("%s: please compile with -fno-common\n",
1588 mod->name);
1589 ret = -ENOEXEC;
1590 break;
1591
1592 case SHN_ABS:
1593 /* Don't need to do anything */
1594 DEBUGP("Absolute symbol: 0x%08lx\n",
1595 (long)sym[i].st_value);
1596 break;
1597
1598 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001599 ksym = resolve_symbol(sechdrs, versindex,
1600 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001602 if (ksym) {
1603 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001605 }
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* Ok if weak. */
1608 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1609 break;
1610
1611 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1612 mod->name, strtab + sym[i].st_name);
1613 ret = -ENOENT;
1614 break;
1615
1616 default:
1617 /* Divert to percpu allocation if a percpu var. */
1618 if (sym[i].st_shndx == pcpuindex)
1619 secbase = (unsigned long)mod->percpu;
1620 else
1621 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1622 sym[i].st_value += secbase;
1623 break;
1624 }
1625 }
1626
1627 return ret;
1628}
1629
Helge Deller088af9a2008-12-31 12:31:18 +01001630/* Additional bytes needed by arch in front of individual sections */
1631unsigned int __weak arch_mod_section_prepend(struct module *mod,
1632 unsigned int section)
1633{
1634 /* default implementation just returns zero */
1635 return 0;
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001639static long get_offset(struct module *mod, unsigned int *size,
1640 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 long ret;
1643
Helge Deller088af9a2008-12-31 12:31:18 +01001644 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1646 *size = ret + sechdr->sh_size;
1647 return ret;
1648}
1649
1650/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1651 might -- code, read-only data, read-write data, small data. Tally
1652 sizes, and place the offsets into sh_entsize fields: high bit means it
1653 belongs in init. */
1654static void layout_sections(struct module *mod,
1655 const Elf_Ehdr *hdr,
1656 Elf_Shdr *sechdrs,
1657 const char *secstrings)
1658{
1659 static unsigned long const masks[][2] = {
1660 /* NOTE: all executable code must be the first section
1661 * in this array; otherwise modify the text_size
1662 * finder in the two loops below */
1663 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1664 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1665 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1666 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1667 };
1668 unsigned int m, i;
1669
1670 for (i = 0; i < hdr->e_shnum; i++)
1671 sechdrs[i].sh_entsize = ~0UL;
1672
1673 DEBUGP("Core section allocation order:\n");
1674 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1675 for (i = 0; i < hdr->e_shnum; ++i) {
1676 Elf_Shdr *s = &sechdrs[i];
1677
1678 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1679 || (s->sh_flags & masks[m][1])
1680 || s->sh_entsize != ~0UL
1681 || strncmp(secstrings + s->sh_name,
1682 ".init", 5) == 0)
1683 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001684 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 DEBUGP("\t%s\n", secstrings + s->sh_name);
1686 }
1687 if (m == 0)
1688 mod->core_text_size = mod->core_size;
1689 }
1690
1691 DEBUGP("Init section allocation order:\n");
1692 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1693 for (i = 0; i < hdr->e_shnum; ++i) {
1694 Elf_Shdr *s = &sechdrs[i];
1695
1696 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1697 || (s->sh_flags & masks[m][1])
1698 || s->sh_entsize != ~0UL
1699 || strncmp(secstrings + s->sh_name,
1700 ".init", 5) != 0)
1701 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001702 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 | INIT_OFFSET_MASK);
1704 DEBUGP("\t%s\n", secstrings + s->sh_name);
1705 }
1706 if (m == 0)
1707 mod->init_text_size = mod->init_size;
1708 }
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static void set_license(struct module *mod, const char *license)
1712{
1713 if (!license)
1714 license = "unspecified";
1715
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001716 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001717 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001718 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001719 "kernel.\n", mod->name, license);
1720 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722}
1723
1724/* Parse tag=value strings from .modinfo section */
1725static char *next_string(char *string, unsigned long *secsize)
1726{
1727 /* Skip non-zero chars */
1728 while (string[0]) {
1729 string++;
1730 if ((*secsize)-- <= 1)
1731 return NULL;
1732 }
1733
1734 /* Skip any zero padding. */
1735 while (!string[0]) {
1736 string++;
1737 if ((*secsize)-- <= 1)
1738 return NULL;
1739 }
1740 return string;
1741}
1742
1743static char *get_modinfo(Elf_Shdr *sechdrs,
1744 unsigned int info,
1745 const char *tag)
1746{
1747 char *p;
1748 unsigned int taglen = strlen(tag);
1749 unsigned long size = sechdrs[info].sh_size;
1750
1751 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1752 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1753 return p + taglen + 1;
1754 }
1755 return NULL;
1756}
1757
Matt Domschc988d2b2005-06-23 22:05:15 -07001758static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1759 unsigned int infoindex)
1760{
1761 struct module_attribute *attr;
1762 int i;
1763
1764 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1765 if (attr->setup)
1766 attr->setup(mod,
1767 get_modinfo(sechdrs,
1768 infoindex,
1769 attr->attr.name));
1770 }
1771}
Matt Domschc988d2b2005-06-23 22:05:15 -07001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001774
1775/* lookup symbol in given range of kernel_symbols */
1776static const struct kernel_symbol *lookup_symbol(const char *name,
1777 const struct kernel_symbol *start,
1778 const struct kernel_symbol *stop)
1779{
1780 const struct kernel_symbol *ks = start;
1781 for (; ks < stop; ks++)
1782 if (strcmp(ks->name, name) == 0)
1783 return ks;
1784 return NULL;
1785}
1786
Tim Abbottca4787b2009-01-05 08:40:10 -06001787static int is_exported(const char *name, unsigned long value,
1788 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Tim Abbottca4787b2009-01-05 08:40:10 -06001790 const struct kernel_symbol *ks;
1791 if (!mod)
1792 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001793 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001794 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1795 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798/* As per nm */
1799static char elf_type(const Elf_Sym *sym,
1800 Elf_Shdr *sechdrs,
1801 const char *secstrings,
1802 struct module *mod)
1803{
1804 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1805 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1806 return 'v';
1807 else
1808 return 'w';
1809 }
1810 if (sym->st_shndx == SHN_UNDEF)
1811 return 'U';
1812 if (sym->st_shndx == SHN_ABS)
1813 return 'a';
1814 if (sym->st_shndx >= SHN_LORESERVE)
1815 return '?';
1816 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1817 return 't';
1818 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1819 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1820 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1821 return 'r';
1822 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1823 return 'g';
1824 else
1825 return 'd';
1826 }
1827 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1828 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1829 return 's';
1830 else
1831 return 'b';
1832 }
1833 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1834 ".debug", strlen(".debug")) == 0)
1835 return 'n';
1836 return '?';
1837}
1838
1839static void add_kallsyms(struct module *mod,
1840 Elf_Shdr *sechdrs,
1841 unsigned int symindex,
1842 unsigned int strindex,
1843 const char *secstrings)
1844{
1845 unsigned int i;
1846
1847 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1848 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1849 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1850
1851 /* Set types up while we still have access to sections. */
1852 for (i = 0; i < mod->num_symtab; i++)
1853 mod->symtab[i].st_info
1854 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1855}
1856#else
1857static inline void add_kallsyms(struct module *mod,
1858 Elf_Shdr *sechdrs,
1859 unsigned int symindex,
1860 unsigned int strindex,
1861 const char *secstrings)
1862{
1863}
1864#endif /* CONFIG_KALLSYMS */
1865
Jason Barone9d376f2009-02-05 11:51:38 -05001866static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001867{
Jason Barone9d376f2009-02-05 11:51:38 -05001868#ifdef CONFIG_DYNAMIC_DEBUG
1869 if (ddebug_add_module(debug, num, debug->modname))
1870 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1871 debug->modname);
1872#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001873}
Jason Baron346e15b2008-08-12 16:46:19 -04001874
Rusty Russell3a642e92008-07-22 19:24:28 -05001875static void *module_alloc_update_bounds(unsigned long size)
1876{
1877 void *ret = module_alloc(size);
1878
1879 if (ret) {
1880 /* Update module bounds. */
1881 if ((unsigned long)ret < module_addr_min)
1882 module_addr_min = (unsigned long)ret;
1883 if ((unsigned long)ret + size > module_addr_max)
1884 module_addr_max = (unsigned long)ret + size;
1885 }
1886 return ret;
1887}
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889/* Allocate and load the module: note that size of section 0 is always
1890 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001891static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 unsigned long len,
1893 const char __user *uargs)
1894{
1895 Elf_Ehdr *hdr;
1896 Elf_Shdr *sechdrs;
1897 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001898 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001899 unsigned int i;
1900 unsigned int symindex = 0;
1901 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001902 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russelle180a6b2009-03-31 13:05:29 -06001903 unsigned int num_mcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct module *mod;
1905 long err = 0;
1906 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001907 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001908 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1911 umod, len, uargs);
1912 if (len < sizeof(*hdr))
1913 return ERR_PTR(-ENOEXEC);
1914
1915 /* Suck in entire file: we'll want most of it. */
1916 /* vmalloc barfs on "unusual" numbers. Check here */
1917 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1918 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001919
1920 /* Create stop_machine threads since the error path relies on
1921 * a non-failing stop_machine call. */
1922 err = stop_machine_create();
1923 if (err)
1924 goto free_hdr;
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (copy_from_user(hdr, umod, len) != 0) {
1927 err = -EFAULT;
1928 goto free_hdr;
1929 }
1930
1931 /* Sanity checks against insmoding binaries or wrong arch,
1932 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001933 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 || hdr->e_type != ET_REL
1935 || !elf_check_arch(hdr)
1936 || hdr->e_shentsize != sizeof(*sechdrs)) {
1937 err = -ENOEXEC;
1938 goto free_hdr;
1939 }
1940
1941 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1942 goto truncated;
1943
1944 /* Convenience variables */
1945 sechdrs = (void *)hdr + hdr->e_shoff;
1946 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1947 sechdrs[0].sh_addr = 0;
1948
1949 for (i = 1; i < hdr->e_shnum; i++) {
1950 if (sechdrs[i].sh_type != SHT_NOBITS
1951 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1952 goto truncated;
1953
1954 /* Mark all sections sh_addr with their address in the
1955 temporary image. */
1956 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1957
1958 /* Internal symbols and strings. */
1959 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1960 symindex = i;
1961 strindex = sechdrs[i].sh_link;
1962 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1963 }
1964#ifndef CONFIG_MODULE_UNLOAD
1965 /* Don't load .exit sections */
1966 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1967 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1968#endif
1969 }
1970
1971 modindex = find_sec(hdr, sechdrs, secstrings,
1972 ".gnu.linkonce.this_module");
1973 if (!modindex) {
1974 printk(KERN_WARNING "No module found in object\n");
1975 err = -ENOEXEC;
1976 goto free_hdr;
1977 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001978 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 mod = (void *)sechdrs[modindex].sh_addr;
1980
1981 if (symindex == 0) {
1982 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1983 mod->name);
1984 err = -ENOEXEC;
1985 goto free_hdr;
1986 }
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1989 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1990 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1991
Rusty Russellea01e792008-03-13 09:02:17 +00001992 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001994 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995#ifdef CONFIG_KALLSYMS
1996 /* Keep symbol and string tables for decoding later. */
1997 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1998 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1999#endif
2000
2001 /* Check module struct version now, before we try to use module. */
2002 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2003 err = -ENOEXEC;
2004 goto free_hdr;
2005 }
2006
2007 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2008 /* This is allowed: modprobe --force will invalidate it. */
2009 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002010 err = try_to_force_load(mod, "magic");
2011 if (err)
2012 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002013 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2015 mod->name, modmagic, vermagic);
2016 err = -ENOEXEC;
2017 goto free_hdr;
2018 }
2019
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002020 staging = get_modinfo(sechdrs, infoindex, "staging");
2021 if (staging) {
2022 add_taint_module(mod, TAINT_CRAP);
2023 printk(KERN_WARNING "%s: module is from the staging directory,"
2024 " the quality is unknown, you have been warned.\n",
2025 mod->name);
2026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002029 args = strndup_user(uargs, ~0UL >> 1);
2030 if (IS_ERR(args)) {
2031 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto free_hdr;
2033 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (find_module(mod->name)) {
2036 err = -EEXIST;
2037 goto free_mod;
2038 }
2039
2040 mod->state = MODULE_STATE_COMING;
2041
2042 /* Allow arches to frob section contents and sizes. */
2043 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2044 if (err < 0)
2045 goto free_mod;
2046
2047 if (pcpuindex) {
2048 /* We have a special allocation for this section. */
2049 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002050 sechdrs[pcpuindex].sh_addralign,
2051 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (!percpu) {
2053 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002054 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2057 mod->percpu = percpu;
2058 }
2059
2060 /* Determine total sizes, and put offsets in sh_entsize. For now
2061 this is done generically; there doesn't appear to be any
2062 special cases for the architectures. */
2063 layout_sections(mod, hdr, sechdrs, secstrings);
2064
2065 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002066 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!ptr) {
2068 err = -ENOMEM;
2069 goto free_percpu;
2070 }
2071 memset(ptr, 0, mod->core_size);
2072 mod->module_core = ptr;
2073
Rusty Russell3a642e92008-07-22 19:24:28 -05002074 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!ptr && mod->init_size) {
2076 err = -ENOMEM;
2077 goto free_core;
2078 }
2079 memset(ptr, 0, mod->init_size);
2080 mod->module_init = ptr;
2081
2082 /* Transfer each section which specifies SHF_ALLOC */
2083 DEBUGP("final section addresses:\n");
2084 for (i = 0; i < hdr->e_shnum; i++) {
2085 void *dest;
2086
2087 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2088 continue;
2089
2090 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2091 dest = mod->module_init
2092 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2093 else
2094 dest = mod->module_core + sechdrs[i].sh_entsize;
2095
2096 if (sechdrs[i].sh_type != SHT_NOBITS)
2097 memcpy(dest, (void *)sechdrs[i].sh_addr,
2098 sechdrs[i].sh_size);
2099 /* Update sh_addr to point to copy in image. */
2100 sechdrs[i].sh_addr = (unsigned long)dest;
2101 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2102 }
2103 /* Module has been moved. */
2104 mod = (void *)sechdrs[modindex].sh_addr;
2105
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002106#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2107 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2108 mod->name);
2109 if (!mod->refptr) {
2110 err = -ENOMEM;
2111 goto free_init;
2112 }
2113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* Now we've moved module, initialize linked lists, etc. */
2115 module_unload_init(mod);
2116
Kay Sievers97c146e2007-11-29 23:46:11 +01002117 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002118 err = mod_sysfs_init(mod);
2119 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002120 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /* Set up license info based on the info section */
2123 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2124
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002125 /*
2126 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2127 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2128 * using GPL-only symbols it needs.
2129 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002130 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002131 add_taint(TAINT_PROPRIETARY_MODULE);
2132
2133 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002134 if (strcmp(mod->name, "driverloader") == 0)
2135 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002136
Matt Domschc988d2b2005-06-23 22:05:15 -07002137 /* Set up MODINFO_ATTR fields */
2138 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Fix up syms, so that st_value is a pointer to location. */
2141 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2142 mod);
2143 if (err < 0)
2144 goto cleanup;
2145
Rusty Russell5e458cc2008-10-22 10:00:13 -05002146 /* Now we've got everything in the final locations, we can
2147 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002148 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2149 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002150 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2151 sizeof(*mod->syms), &mod->num_syms);
2152 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2153 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2154 sizeof(*mod->gpl_syms),
2155 &mod->num_gpl_syms);
2156 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2157 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2158 "__ksymtab_gpl_future",
2159 sizeof(*mod->gpl_future_syms),
2160 &mod->num_gpl_future_syms);
2161 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2162 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002164#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002165 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2166 "__ksymtab_unused",
2167 sizeof(*mod->unused_syms),
2168 &mod->num_unused_syms);
2169 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2170 "__kcrctab_unused");
2171 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2172 "__ksymtab_unused_gpl",
2173 sizeof(*mod->unused_gpl_syms),
2174 &mod->num_unused_gpl_syms);
2175 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2176 "__kcrctab_unused_gpl");
2177#endif
2178
2179#ifdef CONFIG_MARKERS
2180 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2181 sizeof(*mod->markers), &mod->num_markers);
2182#endif
2183#ifdef CONFIG_TRACEPOINTS
2184 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2185 "__tracepoints",
2186 sizeof(*mod->tracepoints),
2187 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002188#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002191 if ((mod->num_syms && !mod->crcs)
2192 || (mod->num_gpl_syms && !mod->gpl_crcs)
2193 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002194#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002195 || (mod->num_unused_syms && !mod->unused_crcs)
2196 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002197#endif
2198 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002199 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2200 err = try_to_force_load(mod, "nocrc");
2201 if (err)
2202 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204#endif
2205
2206 /* Now do relocations. */
2207 for (i = 1; i < hdr->e_shnum; i++) {
2208 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2209 unsigned int info = sechdrs[i].sh_info;
2210
2211 /* Not a valid relocation section? */
2212 if (info >= hdr->e_shnum)
2213 continue;
2214
2215 /* Don't bother with non-allocated sections */
2216 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2217 continue;
2218
2219 if (sechdrs[i].sh_type == SHT_REL)
2220 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2221 else if (sechdrs[i].sh_type == SHT_RELA)
2222 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2223 mod);
2224 if (err < 0)
2225 goto cleanup;
2226 }
2227
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002228 /* Find duplicate symbols */
2229 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002230 if (err < 0)
2231 goto cleanup;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002234 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2235 sizeof(*mod->extable), &mod->num_exentries);
2236 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 /* Finally, copy percpu area over. */
2239 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2240 sechdrs[pcpuindex].sh_size);
2241
2242 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2243
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002244 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002245 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002246 unsigned int num_debug;
2247
Rusty Russell5e458cc2008-10-22 10:00:13 -05002248 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2249 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002250 if (debug)
2251 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002252 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002253
Steven Rostedtfed19392008-08-14 22:47:19 -04002254 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002255 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2256 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002257 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 err = module_finalize(hdr, sechdrs, mod);
2260 if (err < 0)
2261 goto cleanup;
2262
Thomas Koeller378bac82005-09-06 15:17:11 -07002263 /* flush the icache in correct context */
2264 old_fs = get_fs();
2265 set_fs(KERNEL_DS);
2266
2267 /*
2268 * Flush the instruction cache, since we've played with text.
2269 * Do it before processing of module parameters, so the module
2270 * can provide parameter accessor functions of its own.
2271 */
2272 if (mod->module_init)
2273 flush_icache_range((unsigned long)mod->module_init,
2274 (unsigned long)mod->module_init
2275 + mod->init_size);
2276 flush_icache_range((unsigned long)mod->module_core,
2277 (unsigned long)mod->module_core + mod->core_size);
2278
2279 set_fs(old_fs);
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002282 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002283 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2284 mod->name);
2285
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002286 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002287 * info during argument parsing. Noone should access us, since
2288 * strong_try_module_get() will fail.
2289 * lockdep/oops can run asynchronous, so use the RCU list insertion
2290 * function to insert in a way safe to concurrent readers.
2291 * The mutex protects against concurrent writers.
2292 */
2293 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002294
Rusty Russelle180a6b2009-03-31 13:05:29 -06002295 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002297 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Rusty Russelle180a6b2009-03-31 13:05:29 -06002299 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002301 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002303 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* Get rid of temporary copy */
2306 vfree(hdr);
2307
Heiko Carstens9e018922008-12-22 12:36:31 +01002308 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /* Done! */
2310 return mod;
2311
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002312 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002313 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 module_arch_cleanup(mod);
2315 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002316 kobject_del(&mod->mkobj.kobj);
2317 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002318 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002319 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302321#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002322 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302323 percpu_modfree(mod->refptr);
2324#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002325 module_free(mod, mod->module_init);
2326 free_core:
2327 module_free(mod, mod->module_core);
2328 /* mod will be freed with core. Don't access it beyond this line! */
2329 free_percpu:
2330 if (percpu)
2331 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 free_mod:
2333 kfree(args);
2334 free_hdr:
2335 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002336 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002337 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 truncated:
2340 printk(KERN_ERR "Module len %lu truncated\n", len);
2341 err = -ENOEXEC;
2342 goto free_hdr;
2343}
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002346SYSCALL_DEFINE3(init_module, void __user *, umod,
2347 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 struct module *mod;
2350 int ret = 0;
2351
2352 /* Must have permission */
2353 if (!capable(CAP_SYS_MODULE))
2354 return -EPERM;
2355
2356 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002357 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return -EINTR;
2359
2360 /* Do all the hard work */
2361 mod = load_module(umod, len, uargs);
2362 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002363 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return PTR_ERR(mod);
2365 }
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002368 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Alan Sterne041c682006-03-27 01:16:30 -08002370 blocking_notifier_call_chain(&module_notify_list,
2371 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /* Start the module */
2374 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002375 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (ret < 0) {
2377 /* Init routine failed: abort. Try to protect us from
2378 buggy refcounters. */
2379 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002380 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002381 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002382 blocking_notifier_call_chain(&module_notify_list,
2383 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002384 mutex_lock(&module_mutex);
2385 free_module(mod);
2386 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002387 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return ret;
2389 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002390 if (ret > 0) {
2391 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2392 "it should follow 0/-E convention\n"
2393 KERN_WARNING "%s: loading module anyway...\n",
2394 __func__, mod->name, ret,
2395 __func__);
2396 dump_stack();
2397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Rusty Russell6c5db222008-03-10 11:43:52 -07002399 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002401 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002402 blocking_notifier_call_chain(&module_notify_list,
2403 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002404
2405 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* Drop initial reference. */
2407 module_put(mod);
2408 module_free(mod, mod->module_init);
2409 mod->module_init = NULL;
2410 mod->init_size = 0;
2411 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002412 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 return 0;
2415}
2416
2417static inline int within(unsigned long addr, void *start, unsigned long size)
2418{
2419 return ((void *)addr >= start && (void *)addr < start + size);
2420}
2421
2422#ifdef CONFIG_KALLSYMS
2423/*
2424 * This ignores the intensely annoying "mapping symbols" found
2425 * in ARM ELF files: $a, $t and $d.
2426 */
2427static inline int is_arm_mapping_symbol(const char *str)
2428{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002429 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 && (str[2] == '\0' || str[2] == '.');
2431}
2432
2433static const char *get_ksymbol(struct module *mod,
2434 unsigned long addr,
2435 unsigned long *size,
2436 unsigned long *offset)
2437{
2438 unsigned int i, best = 0;
2439 unsigned long nextval;
2440
2441 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002442 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002444 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2446
2447 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002448 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 for (i = 1; i < mod->num_symtab; i++) {
2450 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2451 continue;
2452
2453 /* We ignore unnamed symbols: they're uninformative
2454 * and inserted at a whim. */
2455 if (mod->symtab[i].st_value <= addr
2456 && mod->symtab[i].st_value > mod->symtab[best].st_value
2457 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2458 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2459 best = i;
2460 if (mod->symtab[i].st_value > addr
2461 && mod->symtab[i].st_value < nextval
2462 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2463 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2464 nextval = mod->symtab[i].st_value;
2465 }
2466
2467 if (!best)
2468 return NULL;
2469
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002470 if (size)
2471 *size = nextval - mod->symtab[best].st_value;
2472 if (offset)
2473 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return mod->strtab + mod->symtab[best].st_name;
2475}
2476
Rusty Russell6dd06c92008-01-29 17:13:22 -05002477/* For kallsyms to ask for address resolution. NULL means not found. Careful
2478 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002479const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002480 unsigned long *size,
2481 unsigned long *offset,
2482 char **modname,
2483 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
2485 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002486 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Rusty Russellcb2a5202008-01-14 00:55:03 -08002488 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002489 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002490 if (within_module_init(addr, mod) ||
2491 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002492 if (modname)
2493 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002494 ret = get_ksymbol(mod, addr, size, offset);
2495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002498 /* Make a copy in here where it's safe */
2499 if (ret) {
2500 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2501 ret = namebuf;
2502 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002503 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002507int lookup_module_symbol_name(unsigned long addr, char *symname)
2508{
2509 struct module *mod;
2510
Rusty Russellcb2a5202008-01-14 00:55:03 -08002511 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002512 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002513 if (within_module_init(addr, mod) ||
2514 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002515 const char *sym;
2516
2517 sym = get_ksymbol(mod, addr, NULL, NULL);
2518 if (!sym)
2519 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002520 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002521 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002522 return 0;
2523 }
2524 }
2525out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002526 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002527 return -ERANGE;
2528}
2529
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002530int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2531 unsigned long *offset, char *modname, char *name)
2532{
2533 struct module *mod;
2534
Rusty Russellcb2a5202008-01-14 00:55:03 -08002535 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002536 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002537 if (within_module_init(addr, mod) ||
2538 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002539 const char *sym;
2540
2541 sym = get_ksymbol(mod, addr, size, offset);
2542 if (!sym)
2543 goto out;
2544 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002545 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002546 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002547 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002548 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002549 return 0;
2550 }
2551 }
2552out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002553 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002554 return -ERANGE;
2555}
2556
Alexey Dobriyanea078902007-05-08 00:28:39 -07002557int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2558 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 struct module *mod;
2561
Rusty Russellcb2a5202008-01-14 00:55:03 -08002562 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002563 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (symnum < mod->num_symtab) {
2565 *value = mod->symtab[symnum].st_value;
2566 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002567 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002568 KSYM_NAME_LEN);
2569 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002570 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002571 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574 symnum -= mod->num_symtab;
2575 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002576 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002577 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580static unsigned long mod_find_symname(struct module *mod, const char *name)
2581{
2582 unsigned int i;
2583
2584 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002585 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2586 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return mod->symtab[i].st_value;
2588 return 0;
2589}
2590
2591/* Look for this name: can be of form module:name. */
2592unsigned long module_kallsyms_lookup_name(const char *name)
2593{
2594 struct module *mod;
2595 char *colon;
2596 unsigned long ret = 0;
2597
2598 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002599 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if ((colon = strchr(name, ':')) != NULL) {
2601 *colon = '\0';
2602 if ((mod = find_module(name)) != NULL)
2603 ret = mod_find_symname(mod, colon+1);
2604 *colon = ':';
2605 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002606 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if ((ret = mod_find_symname(mod, name)) != 0)
2608 break;
2609 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002610 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return ret;
2612}
2613#endif /* CONFIG_KALLSYMS */
2614
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002615static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002616{
2617 int bx = 0;
2618
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002619 if (mod->taints ||
2620 mod->state == MODULE_STATE_GOING ||
2621 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002622 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002623 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002624 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002625 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002626 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002627 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002628 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002629 /*
2630 * TAINT_FORCED_RMMOD: could be added.
2631 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2632 * apply to modules.
2633 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002634
2635 /* Show a - for module-is-being-unloaded */
2636 if (mod->state == MODULE_STATE_GOING)
2637 buf[bx++] = '-';
2638 /* Show a + for module-is-being-loaded */
2639 if (mod->state == MODULE_STATE_COMING)
2640 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002641 buf[bx++] = ')';
2642 }
2643 buf[bx] = '\0';
2644
2645 return buf;
2646}
2647
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002648#ifdef CONFIG_PROC_FS
2649/* Called by the /proc file system to return a list of modules. */
2650static void *m_start(struct seq_file *m, loff_t *pos)
2651{
2652 mutex_lock(&module_mutex);
2653 return seq_list_start(&modules, *pos);
2654}
2655
2656static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2657{
2658 return seq_list_next(p, &modules, pos);
2659}
2660
2661static void m_stop(struct seq_file *m, void *p)
2662{
2663 mutex_unlock(&module_mutex);
2664}
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666static int m_show(struct seq_file *m, void *p)
2667{
2668 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002669 char buf[8];
2670
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002671 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 mod->name, mod->init_size + mod->core_size);
2673 print_unload_info(m, mod);
2674
2675 /* Informative for users. */
2676 seq_printf(m, " %s",
2677 mod->state == MODULE_STATE_GOING ? "Unloading":
2678 mod->state == MODULE_STATE_COMING ? "Loading":
2679 "Live");
2680 /* Used by oprofile and other similar tools. */
2681 seq_printf(m, " 0x%p", mod->module_core);
2682
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002683 /* Taints info */
2684 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002685 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 seq_printf(m, "\n");
2688 return 0;
2689}
2690
2691/* Format: modulename size refcount deps address
2692
2693 Where refcount is a number or -, and deps is a comma-separated list
2694 of depends or -.
2695*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002696static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 .start = m_start,
2698 .next = m_next,
2699 .stop = m_stop,
2700 .show = m_show
2701};
2702
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002703static int modules_open(struct inode *inode, struct file *file)
2704{
2705 return seq_open(file, &modules_op);
2706}
2707
2708static const struct file_operations proc_modules_operations = {
2709 .open = modules_open,
2710 .read = seq_read,
2711 .llseek = seq_lseek,
2712 .release = seq_release,
2713};
2714
2715static int __init proc_modules_init(void)
2716{
2717 proc_create("modules", 0, NULL, &proc_modules_operations);
2718 return 0;
2719}
2720module_init(proc_modules_init);
2721#endif
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723/* Given an address, look for it in the module exception tables. */
2724const struct exception_table_entry *search_module_extables(unsigned long addr)
2725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 const struct exception_table_entry *e = NULL;
2727 struct module *mod;
2728
Rusty Russell24da1cb2007-07-15 23:41:46 -07002729 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002730 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (mod->num_exentries == 0)
2732 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 e = search_extable(mod->extable,
2735 mod->extable + mod->num_exentries - 1,
2736 addr);
2737 if (e)
2738 break;
2739 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002740 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002743 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return e;
2745}
2746
Ingo Molnar4d435f92006-07-03 00:24:24 -07002747/*
Rusty Russelle6104992009-03-31 13:05:31 -06002748 * is_module_address - is this address inside a module?
2749 * @addr: the address to check.
2750 *
2751 * See is_module_text_address() if you simply want to see if the address
2752 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002753 */
Rusty Russelle6104992009-03-31 13:05:31 -06002754bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002755{
Rusty Russelle6104992009-03-31 13:05:31 -06002756 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002757
Rusty Russell24da1cb2007-07-15 23:41:46 -07002758 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002759 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002760 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002761
Rusty Russelle6104992009-03-31 13:05:31 -06002762 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002763}
2764
Rusty Russelle6104992009-03-31 13:05:31 -06002765/*
2766 * __module_address - get the module which contains an address.
2767 * @addr: the address.
2768 *
2769 * Must be called with preempt disabled or module mutex held so that
2770 * module doesn't get freed during this.
2771 */
2772__notrace_funcgraph struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct module *mod;
2775
Rusty Russell3a642e92008-07-22 19:24:28 -05002776 if (addr < module_addr_min || addr > module_addr_max)
2777 return NULL;
2778
Andi Kleend72b3752008-08-30 10:09:00 +02002779 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002780 if (within_module_core(addr, mod)
2781 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return mod;
2783 return NULL;
2784}
2785
Rusty Russelle6104992009-03-31 13:05:31 -06002786/*
2787 * is_module_text_address - is this address inside module code?
2788 * @addr: the address to check.
2789 *
2790 * See is_module_address() if you simply want to see if the address is
2791 * anywhere in a module. See kernel_text_address() for testing if an
2792 * address corresponds to kernel or module code.
2793 */
2794bool is_module_text_address(unsigned long addr)
2795{
2796 bool ret;
2797
2798 preempt_disable();
2799 ret = __module_text_address(addr) != NULL;
2800 preempt_enable();
2801
2802 return ret;
2803}
2804
2805/*
2806 * __module_text_address - get the module whose code contains an address.
2807 * @addr: the address.
2808 *
2809 * Must be called with preempt disabled or module mutex held so that
2810 * module doesn't get freed during this.
2811 */
2812struct module *__module_text_address(unsigned long addr)
2813{
2814 struct module *mod = __module_address(addr);
2815 if (mod) {
2816 /* Make sure it's within the text section. */
2817 if (!within(addr, mod->module_init, mod->init_text_size)
2818 && !within(addr, mod->module_core, mod->core_text_size))
2819 mod = NULL;
2820 }
2821 return mod;
2822}
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824struct module *module_text_address(unsigned long addr)
2825{
2826 struct module *mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Rusty Russell24da1cb2007-07-15 23:41:46 -07002828 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 mod = __module_text_address(addr);
Rusty Russell24da1cb2007-07-15 23:41:46 -07002830 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 return mod;
2833}
2834
2835/* Don't grab lock, we're oopsing. */
2836void print_modules(void)
2837{
2838 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002839 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002842 /* Most callers should already have preempt disabled, but make sure */
2843 preempt_disable();
2844 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002845 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002846 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002847 if (last_unloaded_module[0])
2848 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 printk("\n");
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852#ifdef CONFIG_MODVERSIONS
2853/* Generate the signature for struct module here, too, for modversions. */
2854void struct_module(struct module *mod) { return; }
2855EXPORT_SYMBOL(struct_module);
2856#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002857
2858#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002859void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002860{
2861 struct module *mod;
2862
2863 mutex_lock(&module_mutex);
2864 list_for_each_entry(mod, &modules, list)
2865 if (!mod->taints)
2866 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002867 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002868 mutex_unlock(&module_mutex);
2869}
2870#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002871
2872#ifdef CONFIG_TRACEPOINTS
2873void module_update_tracepoints(void)
2874{
2875 struct module *mod;
2876
2877 mutex_lock(&module_mutex);
2878 list_for_each_entry(mod, &modules, list)
2879 if (!mod->taints)
2880 tracepoint_update_probe_range(mod->tracepoints,
2881 mod->tracepoints + mod->num_tracepoints);
2882 mutex_unlock(&module_mutex);
2883}
2884
2885/*
2886 * Returns 0 if current not found.
2887 * Returns 1 if current found.
2888 */
2889int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2890{
2891 struct module *iter_mod;
2892 int found = 0;
2893
2894 mutex_lock(&module_mutex);
2895 list_for_each_entry(iter_mod, &modules, list) {
2896 if (!iter_mod->taints) {
2897 /*
2898 * Sorted module list
2899 */
2900 if (iter_mod < iter->module)
2901 continue;
2902 else if (iter_mod > iter->module)
2903 iter->tracepoint = NULL;
2904 found = tracepoint_get_iter_range(&iter->tracepoint,
2905 iter_mod->tracepoints,
2906 iter_mod->tracepoints
2907 + iter_mod->num_tracepoints);
2908 if (found) {
2909 iter->module = iter_mod;
2910 break;
2911 }
2912 }
2913 }
2914 mutex_unlock(&module_mutex);
2915 return found;
2916}
2917#endif