blob: 5fd00766a4dc696e776f684693858e249e8858dd [file] [log] [blame]
Arjan van de Venf71d20e2006-06-28 04:26:45 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/moduleloader.h>
21#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070022#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040023#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070024#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070025#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/vmalloc.h>
28#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040029#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/seq_file.h>
31#include <linux/syscalls.h>
32#include <linux/fcntl.h>
33#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080034#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/cpu.h>
36#include <linux/moduleparam.h>
37#include <linux/errno.h>
38#include <linux/err.h>
39#include <linux/vermagic.h>
40#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040041#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/stop_machine.h>
43#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070044#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080045#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020046#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020049#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080050#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040051#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040052#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080053#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090054#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#if 0
57#define DEBUGP printk
58#else
59#define DEBUGP(fmt , a...)
60#endif
61
62#ifndef ARCH_SHF_SMALL
63#define ARCH_SHF_SMALL 0
64#endif
65
66/* If this is set, the section belongs in the init part of the module */
67#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
68
Rusty Russell24da1cb2007-07-15 23:41:46 -070069/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020070 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050071DEFINE_MUTEX(module_mutex);
72EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static LIST_HEAD(modules);
74
Rusty Russellc9a3ba52008-01-29 17:13:18 -050075/* Waiting for a module to finish initializing? */
76static DECLARE_WAIT_QUEUE_HEAD(module_wq);
77
Alan Sterne041c682006-03-27 01:16:30 -080078static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Rusty Russelle6104992009-03-31 13:05:31 -060080/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050081static unsigned long module_addr_min = -1UL, module_addr_max = 0;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083int register_module_notifier(struct notifier_block * nb)
84{
Alan Sterne041c682006-03-27 01:16:30 -080085 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87EXPORT_SYMBOL(register_module_notifier);
88
89int unregister_module_notifier(struct notifier_block * nb)
90{
Alan Sterne041c682006-03-27 01:16:30 -080091 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93EXPORT_SYMBOL(unregister_module_notifier);
94
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -080095/* We require a truly strong try_module_get(): 0 means failure due to
96 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static inline int strong_try_module_get(struct module *mod)
98{
99 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500100 return -EBUSY;
101 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500103 else
104 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700107static inline void add_taint_module(struct module *mod, unsigned flag)
108{
109 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700110 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700111}
112
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200113/*
114 * A thread that wants to hold a reference to a module only while it
115 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117void __module_put_and_exit(struct module *mod, long code)
118{
119 module_put(mod);
120 do_exit(code);
121}
122EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Find a module section: 0 means not found. */
125static unsigned int find_sec(Elf_Ehdr *hdr,
126 Elf_Shdr *sechdrs,
127 const char *secstrings,
128 const char *name)
129{
130 unsigned int i;
131
132 for (i = 1; i < hdr->e_shnum; i++)
133 /* Alloc bit cleared means "ignore it." */
134 if ((sechdrs[i].sh_flags & SHF_ALLOC)
135 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
136 return i;
137 return 0;
138}
139
Rusty Russell5e458cc2008-10-22 10:00:13 -0500140/* Find a module section, or NULL. */
141static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
142 const char *secstrings, const char *name)
143{
144 /* Section 0 has sh_addr 0. */
145 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
146}
147
148/* Find a module section, or NULL. Fill in number of "objects" in section. */
149static void *section_objs(Elf_Ehdr *hdr,
150 Elf_Shdr *sechdrs,
151 const char *secstrings,
152 const char *name,
153 size_t object_size,
154 unsigned int *num)
155{
156 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
157
158 /* Section 0 has sh_addr 0 and sh_size 0. */
159 *num = sechdrs[sec].sh_size / object_size;
160 return (void *)sechdrs[sec].sh_addr;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* Provided by the linker */
164extern const struct kernel_symbol __start___ksymtab[];
165extern const struct kernel_symbol __stop___ksymtab[];
166extern const struct kernel_symbol __start___ksymtab_gpl[];
167extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800168extern const struct kernel_symbol __start___ksymtab_gpl_future[];
169extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700170extern const struct kernel_symbol __start___ksymtab_gpl_future[];
171extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172extern const unsigned long __start___kcrctab[];
173extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800174extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500175#ifdef CONFIG_UNUSED_SYMBOLS
176extern const struct kernel_symbol __start___ksymtab_unused[];
177extern const struct kernel_symbol __stop___ksymtab_unused[];
178extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
179extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700180extern const unsigned long __start___kcrctab_unused[];
181extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500182#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184#ifndef CONFIG_MODVERSIONS
185#define symversion(base, idx) NULL
186#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800187#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#endif
189
Rusty Russelldafd0942008-07-22 19:24:25 -0500190static bool each_symbol_in_section(const struct symsearch *arr,
191 unsigned int arrsize,
192 struct module *owner,
193 bool (*fn)(const struct symsearch *syms,
194 struct module *owner,
195 unsigned int symnum, void *data),
196 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100197{
Rusty Russelldafd0942008-07-22 19:24:25 -0500198 unsigned int i, j;
199
200 for (j = 0; j < arrsize; j++) {
201 for (i = 0; i < arr[j].stop - arr[j].start; i++)
202 if (fn(&arr[j], owner, i, data))
203 return true;
204 }
205
206 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100207}
208
Rusty Russelldafd0942008-07-22 19:24:25 -0500209/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500210bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
211 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700212{
Rusty Russelldafd0942008-07-22 19:24:25 -0500213 struct module *mod;
214 const struct symsearch arr[] = {
215 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
216 NOT_GPL_ONLY, false },
217 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
218 __start___kcrctab_gpl,
219 GPL_ONLY, false },
220 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
221 __start___kcrctab_gpl_future,
222 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500223#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500224 { __start___ksymtab_unused, __stop___ksymtab_unused,
225 __start___kcrctab_unused,
226 NOT_GPL_ONLY, true },
227 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
228 __start___kcrctab_unused_gpl,
229 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500230#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500231 };
232
233 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
234 return true;
235
Andi Kleend72b3752008-08-30 10:09:00 +0200236 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500237 struct symsearch arr[] = {
238 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
239 NOT_GPL_ONLY, false },
240 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
241 mod->gpl_crcs,
242 GPL_ONLY, false },
243 { mod->gpl_future_syms,
244 mod->gpl_future_syms + mod->num_gpl_future_syms,
245 mod->gpl_future_crcs,
246 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500247#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500248 { mod->unused_syms,
249 mod->unused_syms + mod->num_unused_syms,
250 mod->unused_crcs,
251 NOT_GPL_ONLY, true },
252 { mod->unused_gpl_syms,
253 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
254 mod->unused_gpl_crcs,
255 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500256#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500257 };
258
259 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
260 return true;
261 }
262 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700263}
Tim Abbottc6b37802008-12-05 19:03:59 -0500264EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700265
Rusty Russelldafd0942008-07-22 19:24:25 -0500266struct find_symbol_arg {
267 /* Input */
268 const char *name;
269 bool gplok;
270 bool warn;
271
272 /* Output */
273 struct module *owner;
274 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500275 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500276};
277
278static bool find_symbol_in_section(const struct symsearch *syms,
279 struct module *owner,
280 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500281{
Rusty Russelldafd0942008-07-22 19:24:25 -0500282 struct find_symbol_arg *fsa = data;
283
284 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
285 return false;
286
287 if (!fsa->gplok) {
288 if (syms->licence == GPL_ONLY)
289 return false;
290 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
291 printk(KERN_WARNING "Symbol %s is being used "
292 "by a non-GPL module, which will not "
293 "be allowed in the future\n", fsa->name);
294 printk(KERN_WARNING "Please see the file "
295 "Documentation/feature-removal-schedule.txt "
296 "in the kernel source tree for more details.\n");
297 }
298 }
299
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500300#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500301 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500302 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500303 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500304 printk(KERN_WARNING
305 "This symbol will go away in the future.\n");
306 printk(KERN_WARNING
307 "Please evalute if this is the right api to use and if "
308 "it really is, submit a report the linux kernel "
309 "mailinglist together with submitting your code for "
310 "inclusion.\n");
311 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500312#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500313
314 fsa->owner = owner;
315 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500316 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500317 return true;
318}
319
Tim Abbott414fd312008-12-05 19:03:56 -0500320/* Find a symbol and return it, along with, (optional) crc and
321 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500322const struct kernel_symbol *find_symbol(const char *name,
323 struct module **owner,
324 const unsigned long **crc,
325 bool gplok,
326 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Rusty Russelldafd0942008-07-22 19:24:25 -0500328 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Rusty Russelldafd0942008-07-22 19:24:25 -0500330 fsa.name = name;
331 fsa.gplok = gplok;
332 fsa.warn = warn;
333
334 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500335 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500336 *owner = fsa.owner;
337 if (crc)
338 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500339 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500343 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Tim Abbottc6b37802008-12-05 19:03:59 -0500345EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500348struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct module *mod;
351
352 list_for_each_entry(mod, &modules, list) {
353 if (strcmp(mod->name, name) == 0)
354 return mod;
355 }
356 return NULL;
357}
Tim Abbottc6b37802008-12-05 19:03:59 -0500358EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900361
362#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
363
364static void *percpu_modalloc(unsigned long size, unsigned long align,
365 const char *name)
366{
367 void *ptr;
368
369 if (align > PAGE_SIZE) {
370 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
371 name, align, PAGE_SIZE);
372 align = PAGE_SIZE;
373 }
374
Tejun Heoedcb4632009-03-06 14:33:59 +0900375 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900376 if (!ptr)
377 printk(KERN_WARNING
378 "Could not allocate %lu bytes percpu data\n", size);
379 return ptr;
380}
381
382static void percpu_modfree(void *freeme)
383{
384 free_percpu(freeme);
385}
386
387#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/* Number of blocks used and allocated. */
390static unsigned int pcpu_num_used, pcpu_num_allocated;
391/* Size of each block. -ve means used. */
392static int *pcpu_size;
393
394static int split_block(unsigned int i, unsigned short size)
395{
396 /* Reallocation required? */
397 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700398 int *new;
399
400 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
401 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!new)
403 return 0;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 pcpu_size = new;
407 }
408
409 /* Insert a new subblock */
410 memmove(&pcpu_size[i+1], &pcpu_size[i],
411 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
412 pcpu_num_used++;
413
414 pcpu_size[i+1] -= size;
415 pcpu_size[i] = size;
416 return 1;
417}
418
419static inline unsigned int block_size(int val)
420{
421 if (val < 0)
422 return -val;
423 return val;
424}
425
Rusty Russell842bbaa2005-08-01 21:11:47 -0700426static void *percpu_modalloc(unsigned long size, unsigned long align,
427 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 unsigned long extra;
430 unsigned int i;
431 void *ptr;
432
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200433 if (align > PAGE_SIZE) {
434 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
435 name, align, PAGE_SIZE);
436 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 ptr = __per_cpu_start;
440 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
441 /* Extra for alignment requirement. */
442 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
443 BUG_ON(i == 0 && extra != 0);
444
445 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
446 continue;
447
448 /* Transfer extra to previous block. */
449 if (pcpu_size[i-1] < 0)
450 pcpu_size[i-1] -= extra;
451 else
452 pcpu_size[i-1] += extra;
453 pcpu_size[i] -= extra;
454 ptr += extra;
455
456 /* Split block if warranted */
457 if (pcpu_size[i] - size > sizeof(unsigned long))
458 if (!split_block(i, size))
459 return NULL;
460
461 /* Mark allocated */
462 pcpu_size[i] = -pcpu_size[i];
463 return ptr;
464 }
465
466 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
467 size);
468 return NULL;
469}
470
471static void percpu_modfree(void *freeme)
472{
473 unsigned int i;
474 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
475
476 /* First entry is core kernel percpu data. */
477 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
478 if (ptr == freeme) {
479 pcpu_size[i] = -pcpu_size[i];
480 goto free;
481 }
482 }
483 BUG();
484
485 free:
486 /* Merge with previous? */
487 if (pcpu_size[i-1] >= 0) {
488 pcpu_size[i-1] += pcpu_size[i];
489 pcpu_num_used--;
490 memmove(&pcpu_size[i], &pcpu_size[i+1],
491 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
492 i--;
493 }
494 /* Merge with next? */
495 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
496 pcpu_size[i] += pcpu_size[i+1];
497 pcpu_num_used--;
498 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
499 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
500 }
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int percpu_modinit(void)
504{
505 pcpu_num_used = 2;
506 pcpu_num_allocated = 2;
507 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
508 GFP_KERNEL);
509 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200510 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Free room. */
512 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
513 if (pcpu_size[1] < 0) {
514 printk(KERN_ERR "No per-cpu room for modules.\n");
515 pcpu_num_used = 1;
516 }
517
518 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700519}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900521
Tejun Heofbf59bc2009-02-20 16:29:08 +0900522#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
523
Tejun Heo6b588c12009-02-20 16:29:07 +0900524static unsigned int find_pcpusec(Elf_Ehdr *hdr,
525 Elf_Shdr *sechdrs,
526 const char *secstrings)
527{
528 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
529}
530
531static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
532{
533 int cpu;
534
535 for_each_possible_cpu(cpu)
536 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900540
Rusty Russell842bbaa2005-08-01 21:11:47 -0700541static inline void *percpu_modalloc(unsigned long size, unsigned long align,
542 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 return NULL;
545}
546static inline void percpu_modfree(void *pcpuptr)
547{
548 BUG();
549}
550static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
551 Elf_Shdr *sechdrs,
552 const char *secstrings)
553{
554 return 0;
555}
556static inline void percpu_modcopy(void *pcpudst, const void *src,
557 unsigned long size)
558{
559 /* pcpusec should be 0, and size of that section should be 0. */
560 BUG_ON(size != 0);
561}
Tejun Heo6b588c12009-02-20 16:29:07 +0900562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#endif /* CONFIG_SMP */
564
Matt Domschc988d2b2005-06-23 22:05:15 -0700565#define MODINFO_ATTR(field) \
566static void setup_modinfo_##field(struct module *mod, const char *s) \
567{ \
568 mod->field = kstrdup(s, GFP_KERNEL); \
569} \
570static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
571 struct module *mod, char *buffer) \
572{ \
573 return sprintf(buffer, "%s\n", mod->field); \
574} \
575static int modinfo_##field##_exists(struct module *mod) \
576{ \
577 return mod->field != NULL; \
578} \
579static void free_modinfo_##field(struct module *mod) \
580{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700581 kfree(mod->field); \
582 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700583} \
584static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900585 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700586 .show = show_modinfo_##field, \
587 .setup = setup_modinfo_##field, \
588 .test = modinfo_##field##_exists, \
589 .free = free_modinfo_##field, \
590};
591
592MODINFO_ATTR(version);
593MODINFO_ATTR(srcversion);
594
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100595static char last_unloaded_module[MODULE_NAME_LEN+1];
596
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800597#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/* Init the unload section of the module. */
599static void module_unload_init(struct module *mod)
600{
Eric Dumazet720eba32009-02-03 13:31:36 +1030601 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030604 for_each_possible_cpu(cpu)
605 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030607 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Backwards compatibility macros put refcount during init. */
609 mod->waiter = current;
610}
611
612/* modules using other modules */
613struct module_use
614{
615 struct list_head list;
616 struct module *module_which_uses;
617};
618
619/* Does a already use b? */
620static int already_uses(struct module *a, struct module *b)
621{
622 struct module_use *use;
623
624 list_for_each_entry(use, &b->modules_which_use_me, list) {
625 if (use->module_which_uses == a) {
626 DEBUGP("%s uses %s!\n", a->name, b->name);
627 return 1;
628 }
629 }
630 DEBUGP("%s does not use %s!\n", a->name, b->name);
631 return 0;
632}
633
634/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500635int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500638 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (b == NULL || already_uses(a, b)) return 1;
641
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500642 /* If we're interrupted or time out, we fail. */
643 if (wait_event_interruptible_timeout(
644 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
645 30 * HZ) <= 0) {
646 printk("%s: gave up waiting for init of module %s.\n",
647 a->name, b->name);
648 return 0;
649 }
650
651 /* If strong_try_module_get() returned a different error, we fail. */
652 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654
655 DEBUGP("Allocating new usage for %s.\n", a->name);
656 use = kmalloc(sizeof(*use), GFP_ATOMIC);
657 if (!use) {
658 printk("%s: out of memory loading\n", a->name);
659 module_put(b);
660 return 0;
661 }
662
663 use->module_which_uses = a;
664 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100665 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 1;
667}
Tim Abbottc6b37802008-12-05 19:03:59 -0500668EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/* Clear the unload stuff of the module. */
671static void module_unload_free(struct module *mod)
672{
673 struct module *i;
674
675 list_for_each_entry(i, &modules, list) {
676 struct module_use *use;
677
678 list_for_each_entry(use, &i->modules_which_use_me, list) {
679 if (use->module_which_uses == mod) {
680 DEBUGP("%s unusing %s\n", mod->name, i->name);
681 module_put(i);
682 list_del(&use->list);
683 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100684 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* There can be at most one match. */
686 break;
687 }
688 }
689 }
690}
691
692#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800693static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 int ret = (flags & O_TRUNC);
696 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800697 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return ret;
699}
700#else
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 return 0;
704}
705#endif /* CONFIG_MODULE_FORCE_UNLOAD */
706
707struct stopref
708{
709 struct module *mod;
710 int flags;
711 int *forced;
712};
713
714/* Whole machine is stopped with interrupts off when this runs. */
715static int __try_stop_module(void *_sref)
716{
717 struct stopref *sref = _sref;
718
Rusty Russellda39ba52008-07-22 19:24:25 -0500719 /* If it's not unused, quit unless we're forcing. */
720 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800721 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EWOULDBLOCK;
723 }
724
725 /* Mark it as dying. */
726 sref->mod->state = MODULE_STATE_GOING;
727 return 0;
728}
729
730static int try_stop_module(struct module *mod, int flags, int *forced)
731{
Rusty Russellda39ba52008-07-22 19:24:25 -0500732 if (flags & O_NONBLOCK) {
733 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500735 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500736 } else {
737 /* We don't need to stop the machine for this. */
738 mod->state = MODULE_STATE_GOING;
739 synchronize_sched();
740 return 0;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744unsigned int module_refcount(struct module *mod)
745{
Eric Dumazet720eba32009-02-03 13:31:36 +1030746 unsigned int total = 0;
747 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Eric Dumazet720eba32009-02-03 13:31:36 +1030749 for_each_possible_cpu(cpu)
750 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return total;
752}
753EXPORT_SYMBOL(module_refcount);
754
755/* This exists whether we can unload or not */
756static void free_module(struct module *mod);
757
758static void wait_for_zero_refcount(struct module *mod)
759{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500760 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800761 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 for (;;) {
763 DEBUGP("Looking at refcount...\n");
764 set_current_state(TASK_UNINTERRUPTIBLE);
765 if (module_refcount(mod) == 0)
766 break;
767 schedule();
768 }
769 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800770 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100773SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
774 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800777 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int ret, forced = 0;
779
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800780 if (!capable(CAP_SYS_MODULE))
781 return -EPERM;
782
783 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
784 return -EFAULT;
785 name[MODULE_NAME_LEN-1] = '\0';
786
Heiko Carstens9e018922008-12-22 12:36:31 +0100787 /* Create stop_machine threads since free_module relies on
788 * a non-failing stop_machine call. */
789 ret = stop_machine_create();
790 if (ret)
791 return ret;
792
793 if (mutex_lock_interruptible(&module_mutex) != 0) {
794 ret = -EINTR;
795 goto out_stop;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 mod = find_module(name);
799 if (!mod) {
800 ret = -ENOENT;
801 goto out;
802 }
803
804 if (!list_empty(&mod->modules_which_use_me)) {
805 /* Other modules depend on us: get rid of them first. */
806 ret = -EWOULDBLOCK;
807 goto out;
808 }
809
810 /* Doing init or already dying? */
811 if (mod->state != MODULE_STATE_LIVE) {
812 /* FIXME: if (force), slam module count and wake up
813 waiter --RR */
814 DEBUGP("%s already dying\n", mod->name);
815 ret = -EBUSY;
816 goto out;
817 }
818
819 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700820 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800821 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!forced) {
823 /* This module can't be removed */
824 ret = -EBUSY;
825 goto out;
826 }
827 }
828
829 /* Set this up before setting mod->state */
830 mod->waiter = current;
831
832 /* Stop the machine so refcounts can't move and disable module. */
833 ret = try_stop_module(mod, flags, &forced);
834 if (ret != 0)
835 goto out;
836
837 /* Never wait if forced. */
838 if (!forced && module_refcount(mod) != 0)
839 wait_for_zero_refcount(mod);
840
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200841 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200843 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200845 blocking_notifier_call_chain(&module_notify_list,
846 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800847 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200848 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100849 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500850 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500851 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 free_module(mod);
853
854 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800855 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100856out_stop:
857 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ret;
859}
860
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800861static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct module_use *use;
864 int printed_something = 0;
865
866 seq_printf(m, " %u ", module_refcount(mod));
867
868 /* Always include a trailing , so userspace can differentiate
869 between this and the old multi-field proc format. */
870 list_for_each_entry(use, &mod->modules_which_use_me, list) {
871 printed_something = 1;
872 seq_printf(m, "%s,", use->module_which_uses->name);
873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (mod->init != NULL && mod->exit == NULL) {
876 printed_something = 1;
877 seq_printf(m, "[permanent],");
878 }
879
880 if (!printed_something)
881 seq_printf(m, "-");
882}
883
884void __symbol_put(const char *symbol)
885{
886 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Rusty Russell24da1cb2007-07-15 23:41:46 -0700888 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500889 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 BUG();
891 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700892 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894EXPORT_SYMBOL(__symbol_put);
895
896void symbol_put_addr(void *addr)
897{
Trent Piepho5e376612006-05-15 09:44:06 -0700898 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Trent Piepho5e376612006-05-15 09:44:06 -0700900 if (core_kernel_text((unsigned long)addr))
901 return;
902
Rusty Russella6e6abd2009-03-31 13:05:31 -0600903 /* module_text_address is safe here: we're supposed to have reference
904 * to module from symbol_get, so it can't go away. */
905 modaddr = __module_text_address((unsigned long)addr);
906 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700907 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909EXPORT_SYMBOL_GPL(symbol_put_addr);
910
911static ssize_t show_refcnt(struct module_attribute *mattr,
912 struct module *mod, char *buffer)
913{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400914 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900918 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 .show = show_refcnt,
920};
921
Al Virof6a57032006-10-18 01:47:25 -0400922void module_put(struct module *module)
923{
924 if (module) {
925 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030926 local_dec(__module_ref_addr(module, cpu));
Al Virof6a57032006-10-18 01:47:25 -0400927 /* Maybe they're waiting for us to drop reference? */
928 if (unlikely(!module_is_live(module)))
929 wake_up_process(module->waiter);
930 put_cpu();
931 }
932}
933EXPORT_SYMBOL(module_put);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800936static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 /* We don't know the usage count, or what modules are using. */
939 seq_printf(m, " - -");
940}
941
942static inline void module_unload_free(struct module *mod)
943{
944}
945
Tim Abbottc6b37802008-12-05 19:03:59 -0500946int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500948 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Tim Abbottc6b37802008-12-05 19:03:59 -0500950EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952static inline void module_unload_init(struct module *mod)
953{
954}
955#endif /* CONFIG_MODULE_UNLOAD */
956
Kay Sievers1f717402006-11-24 12:15:25 +0100957static ssize_t show_initstate(struct module_attribute *mattr,
958 struct module *mod, char *buffer)
959{
960 const char *state = "unknown";
961
962 switch (mod->state) {
963 case MODULE_STATE_LIVE:
964 state = "live";
965 break;
966 case MODULE_STATE_COMING:
967 state = "coming";
968 break;
969 case MODULE_STATE_GOING:
970 state = "going";
971 break;
972 }
973 return sprintf(buffer, "%s\n", state);
974}
975
976static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +0900977 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +0100978 .show = show_initstate,
979};
980
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800981static struct module_attribute *modinfo_attrs[] = {
982 &modinfo_version,
983 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +0100984 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800985#ifdef CONFIG_MODULE_UNLOAD
986 &refcnt,
987#endif
988 NULL,
989};
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static const char vermagic[] = VERMAGIC_STRING;
992
Linus Torvalds826e4502008-05-04 17:04:16 -0700993static int try_to_force_load(struct module *mod, const char *symname)
994{
995#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -0700996 if (!test_taint(TAINT_FORCED_MODULE))
Linus Torvalds826e4502008-05-04 17:04:16 -0700997 printk("%s: no version for \"%s\" found: kernel tainted.\n",
998 mod->name, symname);
999 add_taint_module(mod, TAINT_FORCED_MODULE);
1000 return 0;
1001#else
1002 return -ENOEXEC;
1003#endif
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#ifdef CONFIG_MODVERSIONS
1007static int check_version(Elf_Shdr *sechdrs,
1008 unsigned int versindex,
1009 const char *symname,
1010 struct module *mod,
1011 const unsigned long *crc)
1012{
1013 unsigned int i, num_versions;
1014 struct modversion_info *versions;
1015
1016 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1017 if (!crc)
1018 return 1;
1019
Rusty Russella5dd6972008-05-09 16:24:21 +10001020 /* No versions at all? modprobe --force does this. */
1021 if (versindex == 0)
1022 return try_to_force_load(mod, symname) == 0;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 versions = (void *) sechdrs[versindex].sh_addr;
1025 num_versions = sechdrs[versindex].sh_size
1026 / sizeof(struct modversion_info);
1027
1028 for (i = 0; i < num_versions; i++) {
1029 if (strcmp(versions[i].name, symname) != 0)
1030 continue;
1031
1032 if (versions[i].crc == *crc)
1033 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 DEBUGP("Found checksum %lX vs module %lX\n",
1035 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001036 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001038
Rusty Russella5dd6972008-05-09 16:24:21 +10001039 printk(KERN_WARNING "%s: no symbol version for %s\n",
1040 mod->name, symname);
1041 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001042
1043bad_version:
1044 printk("%s: disagrees about version of symbol %s\n",
1045 mod->name, symname);
1046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1050 unsigned int versindex,
1051 struct module *mod)
1052{
1053 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Tim Abbott414fd312008-12-05 19:03:56 -05001055 if (!find_symbol("struct_module", NULL, &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 BUG();
Rusty Russellad9546c2008-05-01 21:14:59 -05001057 return check_version(sechdrs, versindex, "struct_module", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Rusty Russell91e37a72008-05-09 16:25:28 +10001060/* First part is kernel version, which we ignore if module has crcs. */
1061static inline int same_magic(const char *amagic, const char *bmagic,
1062 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Rusty Russell91e37a72008-05-09 16:25:28 +10001064 if (has_crcs) {
1065 amagic += strcspn(amagic, " ");
1066 bmagic += strcspn(bmagic, " ");
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return strcmp(amagic, bmagic) == 0;
1069}
1070#else
1071static inline int check_version(Elf_Shdr *sechdrs,
1072 unsigned int versindex,
1073 const char *symname,
1074 struct module *mod,
1075 const unsigned long *crc)
1076{
1077 return 1;
1078}
1079
1080static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1081 unsigned int versindex,
1082 struct module *mod)
1083{
1084 return 1;
1085}
1086
Rusty Russell91e37a72008-05-09 16:25:28 +10001087static inline int same_magic(const char *amagic, const char *bmagic,
1088 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 return strcmp(amagic, bmagic) == 0;
1091}
1092#endif /* CONFIG_MODVERSIONS */
1093
1094/* Resolve a symbol for this module. I.e. if we find one, record usage.
1095 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001096static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1097 unsigned int versindex,
1098 const char *name,
1099 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001102 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 const unsigned long *crc;
1104
Tim Abbott414fd312008-12-05 19:03:56 -05001105 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001106 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001107 /* use_module can fail due to OOM,
1108 or module initialization or unloading */
1109 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1111 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001112 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Tim Abbott414fd312008-12-05 19:03:56 -05001114 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117/*
1118 * /sys/module/foo/sections stuff
1119 * J. Corbet <corbet@lwn.net>
1120 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001121#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001122struct module_sect_attr
1123{
1124 struct module_attribute mattr;
1125 char *name;
1126 unsigned long address;
1127};
1128
1129struct module_sect_attrs
1130{
1131 struct attribute_group grp;
1132 unsigned int nsections;
1133 struct module_sect_attr attrs[0];
1134};
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136static ssize_t module_sect_show(struct module_attribute *mattr,
1137 struct module *mod, char *buf)
1138{
1139 struct module_sect_attr *sattr =
1140 container_of(mattr, struct module_sect_attr, mattr);
1141 return sprintf(buf, "0x%lx\n", sattr->address);
1142}
1143
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001144static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1145{
Rusty Russella58730c2008-03-13 09:03:44 +00001146 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001147
1148 for (section = 0; section < sect_attrs->nsections; section++)
1149 kfree(sect_attrs->attrs[section].name);
1150 kfree(sect_attrs);
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static void add_sect_attrs(struct module *mod, unsigned int nsect,
1154 char *secstrings, Elf_Shdr *sechdrs)
1155{
1156 unsigned int nloaded = 0, i, size[2];
1157 struct module_sect_attrs *sect_attrs;
1158 struct module_sect_attr *sattr;
1159 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /* Count loaded sections and allocate structures */
1162 for (i = 0; i < nsect; i++)
1163 if (sechdrs[i].sh_flags & SHF_ALLOC)
1164 nloaded++;
1165 size[0] = ALIGN(sizeof(*sect_attrs)
1166 + nloaded * sizeof(sect_attrs->attrs[0]),
1167 sizeof(sect_attrs->grp.attrs[0]));
1168 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001169 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1170 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return;
1172
1173 /* Setup section attributes. */
1174 sect_attrs->grp.name = "sections";
1175 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1176
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001177 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 sattr = &sect_attrs->attrs[0];
1179 gattr = &sect_attrs->grp.attrs[0];
1180 for (i = 0; i < nsect; i++) {
1181 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1182 continue;
1183 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001184 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1185 GFP_KERNEL);
1186 if (sattr->name == NULL)
1187 goto out;
1188 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 sattr->mattr.show = module_sect_show;
1190 sattr->mattr.store = NULL;
1191 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sattr->mattr.attr.mode = S_IRUGO;
1193 *(gattr++) = &(sattr++)->mattr.attr;
1194 }
1195 *gattr = NULL;
1196
1197 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1198 goto out;
1199
1200 mod->sect_attrs = sect_attrs;
1201 return;
1202 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001203 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static void remove_sect_attrs(struct module *mod)
1207{
1208 if (mod->sect_attrs) {
1209 sysfs_remove_group(&mod->mkobj.kobj,
1210 &mod->sect_attrs->grp);
1211 /* We are positive that no one is using any sect attrs
1212 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001213 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 mod->sect_attrs = NULL;
1215 }
1216}
1217
Roland McGrath6d760132007-10-16 23:26:40 -07001218/*
1219 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1220 */
1221
1222struct module_notes_attrs {
1223 struct kobject *dir;
1224 unsigned int notes;
1225 struct bin_attribute attrs[0];
1226};
1227
1228static ssize_t module_notes_read(struct kobject *kobj,
1229 struct bin_attribute *bin_attr,
1230 char *buf, loff_t pos, size_t count)
1231{
1232 /*
1233 * The caller checked the pos and count against our size.
1234 */
1235 memcpy(buf, bin_attr->private + pos, count);
1236 return count;
1237}
1238
1239static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1240 unsigned int i)
1241{
1242 if (notes_attrs->dir) {
1243 while (i-- > 0)
1244 sysfs_remove_bin_file(notes_attrs->dir,
1245 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001246 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001247 }
1248 kfree(notes_attrs);
1249}
1250
1251static void add_notes_attrs(struct module *mod, unsigned int nsect,
1252 char *secstrings, Elf_Shdr *sechdrs)
1253{
1254 unsigned int notes, loaded, i;
1255 struct module_notes_attrs *notes_attrs;
1256 struct bin_attribute *nattr;
1257
1258 /* Count notes sections and allocate structures. */
1259 notes = 0;
1260 for (i = 0; i < nsect; i++)
1261 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1262 (sechdrs[i].sh_type == SHT_NOTE))
1263 ++notes;
1264
1265 if (notes == 0)
1266 return;
1267
1268 notes_attrs = kzalloc(sizeof(*notes_attrs)
1269 + notes * sizeof(notes_attrs->attrs[0]),
1270 GFP_KERNEL);
1271 if (notes_attrs == NULL)
1272 return;
1273
1274 notes_attrs->notes = notes;
1275 nattr = &notes_attrs->attrs[0];
1276 for (loaded = i = 0; i < nsect; ++i) {
1277 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1278 continue;
1279 if (sechdrs[i].sh_type == SHT_NOTE) {
1280 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1281 nattr->attr.mode = S_IRUGO;
1282 nattr->size = sechdrs[i].sh_size;
1283 nattr->private = (void *) sechdrs[i].sh_addr;
1284 nattr->read = module_notes_read;
1285 ++nattr;
1286 }
1287 ++loaded;
1288 }
1289
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001290 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001291 if (!notes_attrs->dir)
1292 goto out;
1293
1294 for (i = 0; i < notes; ++i)
1295 if (sysfs_create_bin_file(notes_attrs->dir,
1296 &notes_attrs->attrs[i]))
1297 goto out;
1298
1299 mod->notes_attrs = notes_attrs;
1300 return;
1301
1302 out:
1303 free_notes_attrs(notes_attrs, i);
1304}
1305
1306static void remove_notes_attrs(struct module *mod)
1307{
1308 if (mod->notes_attrs)
1309 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1315 char *sectstrings, Elf_Shdr *sechdrs)
1316{
1317}
1318
1319static inline void remove_sect_attrs(struct module *mod)
1320{
1321}
Roland McGrath6d760132007-10-16 23:26:40 -07001322
1323static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1324 char *sectstrings, Elf_Shdr *sechdrs)
1325{
1326}
1327
1328static inline void remove_notes_attrs(struct module *mod)
1329{
1330}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001331#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Randy Dunlapef665c12007-02-13 15:19:06 -08001333#ifdef CONFIG_SYSFS
1334int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001335{
1336 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001337 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001338 int error = 0;
1339 int i;
1340
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001341 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1342 (ARRAY_SIZE(modinfo_attrs) + 1)),
1343 GFP_KERNEL);
1344 if (!mod->modinfo_attrs)
1345 return -ENOMEM;
1346
1347 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001348 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1349 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001350 (attr->test && attr->test(mod))) {
1351 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001352 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1353 ++temp_attr;
1354 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001355 }
1356 return error;
1357}
1358
Randy Dunlapef665c12007-02-13 15:19:06 -08001359void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001360{
1361 struct module_attribute *attr;
1362 int i;
1363
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001364 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1365 /* pick a field to test for end of list */
1366 if (!attr->attr.name)
1367 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001368 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001369 if (attr->free)
1370 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001371 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001372 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Randy Dunlapef665c12007-02-13 15:19:06 -08001375int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001378 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001380 if (!module_sysfs_initialized) {
1381 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001382 mod->name);
1383 err = -EINVAL;
1384 goto out;
1385 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001386
1387 kobj = kset_find_obj(module_kset, mod->name);
1388 if (kobj) {
1389 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1390 kobject_put(kobj);
1391 err = -EINVAL;
1392 goto out;
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001396
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001397 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1398 mod->mkobj.kobj.kset = module_kset;
1399 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1400 "%s", mod->name);
1401 if (err)
1402 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001403
Kay Sievers97c146e2007-11-29 23:46:11 +01001404 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001405out:
1406 return err;
1407}
1408
Randy Dunlapef665c12007-02-13 15:19:06 -08001409int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001410 struct kernel_param *kparam,
1411 unsigned int num_params)
1412{
1413 int err;
1414
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001415 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001416 if (!mod->holders_dir) {
1417 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001418 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001419 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 err = module_param_sysfs_setup(mod, kparam, num_params);
1422 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001423 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Matt Domschc988d2b2005-06-23 22:05:15 -07001425 err = module_add_modinfo_attrs(mod);
1426 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001427 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001428
Kay Sieverse17e0f52006-11-24 12:15:25 +01001429 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431
Kay Sieverse17e0f52006-11-24 12:15:25 +01001432out_unreg_param:
1433 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001434out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001435 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001436out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001437 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return err;
1439}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001440
1441static void mod_sysfs_fini(struct module *mod)
1442{
1443 kobject_put(&mod->mkobj.kobj);
1444}
1445
1446#else /* CONFIG_SYSFS */
1447
1448static void mod_sysfs_fini(struct module *mod)
1449{
1450}
1451
1452#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454static void mod_kobject_remove(struct module *mod)
1455{
Matt Domschc988d2b2005-06-23 22:05:15 -07001456 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001458 kobject_put(mod->mkobj.drivers_dir);
1459 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001460 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
1463/*
1464 * unlink the module with the whole machine is stopped with interrupts off
1465 * - this defends against kallsyms not taking locks
1466 */
1467static int __unlink_module(void *_mod)
1468{
1469 struct module *mod = _mod;
1470 list_del(&mod->list);
1471 return 0;
1472}
1473
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001474/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475static void free_module(struct module *mod)
1476{
1477 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001478 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001479 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 remove_sect_attrs(mod);
1481 mod_kobject_remove(mod);
1482
1483 /* Arch-specific cleanup. */
1484 module_arch_cleanup(mod);
1485
1486 /* Module unload stuff */
1487 module_unload_free(mod);
1488
Rusty Russelle180a6b2009-03-31 13:05:29 -06001489 /* Free any allocated parameters. */
1490 destroy_params(mod->kp, mod->num_kp);
1491
Steven Rostedtfed19392008-08-14 22:47:19 -04001492 /* release any pointers to mcount in this module */
1493 ftrace_release(mod->module_core, mod->core_size);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /* This may be NULL, but that's OK */
1496 module_free(mod, mod->module_init);
1497 kfree(mod->args);
1498 if (mod->percpu)
1499 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301500#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1501 if (mod->refptr)
1502 percpu_modfree(mod->refptr);
1503#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001504 /* Free lock-classes: */
1505 lockdep_free_key_range(mod->module_core, mod->core_size);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* Finally, free the core (containing the module structure) */
1508 module_free(mod, mod->module_core);
1509}
1510
1511void *__symbol_get(const char *symbol)
1512{
1513 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001514 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Rusty Russell24da1cb2007-07-15 23:41:46 -07001516 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001517 sym = find_symbol(symbol, &owner, NULL, true, true);
1518 if (sym && strong_try_module_get(owner))
1519 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001520 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Tim Abbott414fd312008-12-05 19:03:56 -05001522 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524EXPORT_SYMBOL_GPL(__symbol_get);
1525
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001526/*
1527 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001528 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001529 */
1530static int verify_export_symbols(struct module *mod)
1531{
Rusty Russellb2111042008-05-01 21:15:00 -05001532 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001533 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001534 const struct kernel_symbol *s;
1535 struct {
1536 const struct kernel_symbol *sym;
1537 unsigned int num;
1538 } arr[] = {
1539 { mod->syms, mod->num_syms },
1540 { mod->gpl_syms, mod->num_gpl_syms },
1541 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001542#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001543 { mod->unused_syms, mod->num_unused_syms },
1544 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001545#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001546 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001547
Rusty Russellb2111042008-05-01 21:15:00 -05001548 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1549 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001550 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001551 printk(KERN_ERR
1552 "%s: exports duplicate symbol %s"
1553 " (owned by %s)\n",
1554 mod->name, s->name, module_name(owner));
1555 return -ENOEXEC;
1556 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001557 }
Rusty Russellb2111042008-05-01 21:15:00 -05001558 }
1559 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001560}
1561
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001562/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563static int simplify_symbols(Elf_Shdr *sechdrs,
1564 unsigned int symindex,
1565 const char *strtab,
1566 unsigned int versindex,
1567 unsigned int pcpuindex,
1568 struct module *mod)
1569{
1570 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1571 unsigned long secbase;
1572 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1573 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001574 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 for (i = 1; i < n; i++) {
1577 switch (sym[i].st_shndx) {
1578 case SHN_COMMON:
1579 /* We compiled with -fno-common. These are not
1580 supposed to happen. */
1581 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1582 printk("%s: please compile with -fno-common\n",
1583 mod->name);
1584 ret = -ENOEXEC;
1585 break;
1586
1587 case SHN_ABS:
1588 /* Don't need to do anything */
1589 DEBUGP("Absolute symbol: 0x%08lx\n",
1590 (long)sym[i].st_value);
1591 break;
1592
1593 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001594 ksym = resolve_symbol(sechdrs, versindex,
1595 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001597 if (ksym) {
1598 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001600 }
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* Ok if weak. */
1603 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1604 break;
1605
1606 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1607 mod->name, strtab + sym[i].st_name);
1608 ret = -ENOENT;
1609 break;
1610
1611 default:
1612 /* Divert to percpu allocation if a percpu var. */
1613 if (sym[i].st_shndx == pcpuindex)
1614 secbase = (unsigned long)mod->percpu;
1615 else
1616 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1617 sym[i].st_value += secbase;
1618 break;
1619 }
1620 }
1621
1622 return ret;
1623}
1624
Helge Deller088af9a2008-12-31 12:31:18 +01001625/* Additional bytes needed by arch in front of individual sections */
1626unsigned int __weak arch_mod_section_prepend(struct module *mod,
1627 unsigned int section)
1628{
1629 /* default implementation just returns zero */
1630 return 0;
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001634static long get_offset(struct module *mod, unsigned int *size,
1635 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 long ret;
1638
Helge Deller088af9a2008-12-31 12:31:18 +01001639 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1641 *size = ret + sechdr->sh_size;
1642 return ret;
1643}
1644
1645/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1646 might -- code, read-only data, read-write data, small data. Tally
1647 sizes, and place the offsets into sh_entsize fields: high bit means it
1648 belongs in init. */
1649static void layout_sections(struct module *mod,
1650 const Elf_Ehdr *hdr,
1651 Elf_Shdr *sechdrs,
1652 const char *secstrings)
1653{
1654 static unsigned long const masks[][2] = {
1655 /* NOTE: all executable code must be the first section
1656 * in this array; otherwise modify the text_size
1657 * finder in the two loops below */
1658 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1659 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1660 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1661 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1662 };
1663 unsigned int m, i;
1664
1665 for (i = 0; i < hdr->e_shnum; i++)
1666 sechdrs[i].sh_entsize = ~0UL;
1667
1668 DEBUGP("Core section allocation order:\n");
1669 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1670 for (i = 0; i < hdr->e_shnum; ++i) {
1671 Elf_Shdr *s = &sechdrs[i];
1672
1673 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1674 || (s->sh_flags & masks[m][1])
1675 || s->sh_entsize != ~0UL
1676 || strncmp(secstrings + s->sh_name,
1677 ".init", 5) == 0)
1678 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001679 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 DEBUGP("\t%s\n", secstrings + s->sh_name);
1681 }
1682 if (m == 0)
1683 mod->core_text_size = mod->core_size;
1684 }
1685
1686 DEBUGP("Init section allocation order:\n");
1687 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1688 for (i = 0; i < hdr->e_shnum; ++i) {
1689 Elf_Shdr *s = &sechdrs[i];
1690
1691 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1692 || (s->sh_flags & masks[m][1])
1693 || s->sh_entsize != ~0UL
1694 || strncmp(secstrings + s->sh_name,
1695 ".init", 5) != 0)
1696 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001697 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 | INIT_OFFSET_MASK);
1699 DEBUGP("\t%s\n", secstrings + s->sh_name);
1700 }
1701 if (m == 0)
1702 mod->init_text_size = mod->init_size;
1703 }
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706static void set_license(struct module *mod, const char *license)
1707{
1708 if (!license)
1709 license = "unspecified";
1710
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001711 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001712 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001713 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001714 "kernel.\n", mod->name, license);
1715 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717}
1718
1719/* Parse tag=value strings from .modinfo section */
1720static char *next_string(char *string, unsigned long *secsize)
1721{
1722 /* Skip non-zero chars */
1723 while (string[0]) {
1724 string++;
1725 if ((*secsize)-- <= 1)
1726 return NULL;
1727 }
1728
1729 /* Skip any zero padding. */
1730 while (!string[0]) {
1731 string++;
1732 if ((*secsize)-- <= 1)
1733 return NULL;
1734 }
1735 return string;
1736}
1737
1738static char *get_modinfo(Elf_Shdr *sechdrs,
1739 unsigned int info,
1740 const char *tag)
1741{
1742 char *p;
1743 unsigned int taglen = strlen(tag);
1744 unsigned long size = sechdrs[info].sh_size;
1745
1746 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1747 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1748 return p + taglen + 1;
1749 }
1750 return NULL;
1751}
1752
Matt Domschc988d2b2005-06-23 22:05:15 -07001753static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1754 unsigned int infoindex)
1755{
1756 struct module_attribute *attr;
1757 int i;
1758
1759 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1760 if (attr->setup)
1761 attr->setup(mod,
1762 get_modinfo(sechdrs,
1763 infoindex,
1764 attr->attr.name));
1765 }
1766}
Matt Domschc988d2b2005-06-23 22:05:15 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001769
1770/* lookup symbol in given range of kernel_symbols */
1771static const struct kernel_symbol *lookup_symbol(const char *name,
1772 const struct kernel_symbol *start,
1773 const struct kernel_symbol *stop)
1774{
1775 const struct kernel_symbol *ks = start;
1776 for (; ks < stop; ks++)
1777 if (strcmp(ks->name, name) == 0)
1778 return ks;
1779 return NULL;
1780}
1781
Tim Abbottca4787b2009-01-05 08:40:10 -06001782static int is_exported(const char *name, unsigned long value,
1783 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Tim Abbottca4787b2009-01-05 08:40:10 -06001785 const struct kernel_symbol *ks;
1786 if (!mod)
1787 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001788 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001789 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1790 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793/* As per nm */
1794static char elf_type(const Elf_Sym *sym,
1795 Elf_Shdr *sechdrs,
1796 const char *secstrings,
1797 struct module *mod)
1798{
1799 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1800 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1801 return 'v';
1802 else
1803 return 'w';
1804 }
1805 if (sym->st_shndx == SHN_UNDEF)
1806 return 'U';
1807 if (sym->st_shndx == SHN_ABS)
1808 return 'a';
1809 if (sym->st_shndx >= SHN_LORESERVE)
1810 return '?';
1811 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1812 return 't';
1813 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1814 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1815 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1816 return 'r';
1817 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1818 return 'g';
1819 else
1820 return 'd';
1821 }
1822 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1823 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1824 return 's';
1825 else
1826 return 'b';
1827 }
1828 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1829 ".debug", strlen(".debug")) == 0)
1830 return 'n';
1831 return '?';
1832}
1833
1834static void add_kallsyms(struct module *mod,
1835 Elf_Shdr *sechdrs,
1836 unsigned int symindex,
1837 unsigned int strindex,
1838 const char *secstrings)
1839{
1840 unsigned int i;
1841
1842 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1843 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1844 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1845
1846 /* Set types up while we still have access to sections. */
1847 for (i = 0; i < mod->num_symtab; i++)
1848 mod->symtab[i].st_info
1849 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1850}
1851#else
1852static inline void add_kallsyms(struct module *mod,
1853 Elf_Shdr *sechdrs,
1854 unsigned int symindex,
1855 unsigned int strindex,
1856 const char *secstrings)
1857{
1858}
1859#endif /* CONFIG_KALLSYMS */
1860
Jason Barone9d376f2009-02-05 11:51:38 -05001861static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001862{
Jason Barone9d376f2009-02-05 11:51:38 -05001863#ifdef CONFIG_DYNAMIC_DEBUG
1864 if (ddebug_add_module(debug, num, debug->modname))
1865 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1866 debug->modname);
1867#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001868}
Jason Baron346e15b2008-08-12 16:46:19 -04001869
Rusty Russell3a642e92008-07-22 19:24:28 -05001870static void *module_alloc_update_bounds(unsigned long size)
1871{
1872 void *ret = module_alloc(size);
1873
1874 if (ret) {
1875 /* Update module bounds. */
1876 if ((unsigned long)ret < module_addr_min)
1877 module_addr_min = (unsigned long)ret;
1878 if ((unsigned long)ret + size > module_addr_max)
1879 module_addr_max = (unsigned long)ret + size;
1880 }
1881 return ret;
1882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884/* Allocate and load the module: note that size of section 0 is always
1885 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001886static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 unsigned long len,
1888 const char __user *uargs)
1889{
1890 Elf_Ehdr *hdr;
1891 Elf_Shdr *sechdrs;
1892 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001893 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001894 unsigned int i;
1895 unsigned int symindex = 0;
1896 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001897 unsigned int modindex, versindex, infoindex, pcpuindex;
Rusty Russelle180a6b2009-03-31 13:05:29 -06001898 unsigned int num_mcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct module *mod;
1900 long err = 0;
1901 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Rusty Russell5e458cc2008-10-22 10:00:13 -05001902 unsigned long *mseg;
Thomas Koeller378bac82005-09-06 15:17:11 -07001903 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1906 umod, len, uargs);
1907 if (len < sizeof(*hdr))
1908 return ERR_PTR(-ENOEXEC);
1909
1910 /* Suck in entire file: we'll want most of it. */
1911 /* vmalloc barfs on "unusual" numbers. Check here */
1912 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1913 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001914
1915 /* Create stop_machine threads since the error path relies on
1916 * a non-failing stop_machine call. */
1917 err = stop_machine_create();
1918 if (err)
1919 goto free_hdr;
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (copy_from_user(hdr, umod, len) != 0) {
1922 err = -EFAULT;
1923 goto free_hdr;
1924 }
1925
1926 /* Sanity checks against insmoding binaries or wrong arch,
1927 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001928 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 || hdr->e_type != ET_REL
1930 || !elf_check_arch(hdr)
1931 || hdr->e_shentsize != sizeof(*sechdrs)) {
1932 err = -ENOEXEC;
1933 goto free_hdr;
1934 }
1935
1936 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1937 goto truncated;
1938
1939 /* Convenience variables */
1940 sechdrs = (void *)hdr + hdr->e_shoff;
1941 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1942 sechdrs[0].sh_addr = 0;
1943
1944 for (i = 1; i < hdr->e_shnum; i++) {
1945 if (sechdrs[i].sh_type != SHT_NOBITS
1946 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1947 goto truncated;
1948
1949 /* Mark all sections sh_addr with their address in the
1950 temporary image. */
1951 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1952
1953 /* Internal symbols and strings. */
1954 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1955 symindex = i;
1956 strindex = sechdrs[i].sh_link;
1957 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1958 }
1959#ifndef CONFIG_MODULE_UNLOAD
1960 /* Don't load .exit sections */
1961 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1962 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1963#endif
1964 }
1965
1966 modindex = find_sec(hdr, sechdrs, secstrings,
1967 ".gnu.linkonce.this_module");
1968 if (!modindex) {
1969 printk(KERN_WARNING "No module found in object\n");
1970 err = -ENOEXEC;
1971 goto free_hdr;
1972 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05001973 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 mod = (void *)sechdrs[modindex].sh_addr;
1975
1976 if (symindex == 0) {
1977 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1978 mod->name);
1979 err = -ENOEXEC;
1980 goto free_hdr;
1981 }
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1984 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1985 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1986
Rusty Russellea01e792008-03-13 09:02:17 +00001987 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00001989 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#ifdef CONFIG_KALLSYMS
1991 /* Keep symbol and string tables for decoding later. */
1992 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1993 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1994#endif
1995
1996 /* Check module struct version now, before we try to use module. */
1997 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1998 err = -ENOEXEC;
1999 goto free_hdr;
2000 }
2001
2002 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2003 /* This is allowed: modprobe --force will invalidate it. */
2004 if (!modmagic) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002005 err = try_to_force_load(mod, "magic");
2006 if (err)
2007 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002008 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2010 mod->name, modmagic, vermagic);
2011 err = -ENOEXEC;
2012 goto free_hdr;
2013 }
2014
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002015 staging = get_modinfo(sechdrs, infoindex, "staging");
2016 if (staging) {
2017 add_taint_module(mod, TAINT_CRAP);
2018 printk(KERN_WARNING "%s: module is from the staging directory,"
2019 " the quality is unknown, you have been warned.\n",
2020 mod->name);
2021 }
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002024 args = strndup_user(uargs, ~0UL >> 1);
2025 if (IS_ERR(args)) {
2026 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto free_hdr;
2028 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (find_module(mod->name)) {
2031 err = -EEXIST;
2032 goto free_mod;
2033 }
2034
2035 mod->state = MODULE_STATE_COMING;
2036
2037 /* Allow arches to frob section contents and sizes. */
2038 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2039 if (err < 0)
2040 goto free_mod;
2041
2042 if (pcpuindex) {
2043 /* We have a special allocation for this section. */
2044 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002045 sechdrs[pcpuindex].sh_addralign,
2046 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (!percpu) {
2048 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002049 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2052 mod->percpu = percpu;
2053 }
2054
2055 /* Determine total sizes, and put offsets in sh_entsize. For now
2056 this is done generically; there doesn't appear to be any
2057 special cases for the architectures. */
2058 layout_sections(mod, hdr, sechdrs, secstrings);
2059
2060 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002061 ptr = module_alloc_update_bounds(mod->core_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!ptr) {
2063 err = -ENOMEM;
2064 goto free_percpu;
2065 }
2066 memset(ptr, 0, mod->core_size);
2067 mod->module_core = ptr;
2068
Rusty Russell3a642e92008-07-22 19:24:28 -05002069 ptr = module_alloc_update_bounds(mod->init_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (!ptr && mod->init_size) {
2071 err = -ENOMEM;
2072 goto free_core;
2073 }
2074 memset(ptr, 0, mod->init_size);
2075 mod->module_init = ptr;
2076
2077 /* Transfer each section which specifies SHF_ALLOC */
2078 DEBUGP("final section addresses:\n");
2079 for (i = 0; i < hdr->e_shnum; i++) {
2080 void *dest;
2081
2082 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2083 continue;
2084
2085 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2086 dest = mod->module_init
2087 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2088 else
2089 dest = mod->module_core + sechdrs[i].sh_entsize;
2090
2091 if (sechdrs[i].sh_type != SHT_NOBITS)
2092 memcpy(dest, (void *)sechdrs[i].sh_addr,
2093 sechdrs[i].sh_size);
2094 /* Update sh_addr to point to copy in image. */
2095 sechdrs[i].sh_addr = (unsigned long)dest;
2096 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2097 }
2098 /* Module has been moved. */
2099 mod = (void *)sechdrs[modindex].sh_addr;
2100
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002101#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2102 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2103 mod->name);
2104 if (!mod->refptr) {
2105 err = -ENOMEM;
2106 goto free_init;
2107 }
2108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /* Now we've moved module, initialize linked lists, etc. */
2110 module_unload_init(mod);
2111
Kay Sievers97c146e2007-11-29 23:46:11 +01002112 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002113 err = mod_sysfs_init(mod);
2114 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002115 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* Set up license info based on the info section */
2118 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2119
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002120 /*
2121 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2122 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2123 * using GPL-only symbols it needs.
2124 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002125 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002126 add_taint(TAINT_PROPRIETARY_MODULE);
2127
2128 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002129 if (strcmp(mod->name, "driverloader") == 0)
2130 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002131
Matt Domschc988d2b2005-06-23 22:05:15 -07002132 /* Set up MODINFO_ATTR fields */
2133 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Fix up syms, so that st_value is a pointer to location. */
2136 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2137 mod);
2138 if (err < 0)
2139 goto cleanup;
2140
Rusty Russell5e458cc2008-10-22 10:00:13 -05002141 /* Now we've got everything in the final locations, we can
2142 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002143 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2144 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002145 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2146 sizeof(*mod->syms), &mod->num_syms);
2147 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2148 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2149 sizeof(*mod->gpl_syms),
2150 &mod->num_gpl_syms);
2151 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2152 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2153 "__ksymtab_gpl_future",
2154 sizeof(*mod->gpl_future_syms),
2155 &mod->num_gpl_future_syms);
2156 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2157 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002159#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002160 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2161 "__ksymtab_unused",
2162 sizeof(*mod->unused_syms),
2163 &mod->num_unused_syms);
2164 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2165 "__kcrctab_unused");
2166 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2167 "__ksymtab_unused_gpl",
2168 sizeof(*mod->unused_gpl_syms),
2169 &mod->num_unused_gpl_syms);
2170 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2171 "__kcrctab_unused_gpl");
2172#endif
2173
2174#ifdef CONFIG_MARKERS
2175 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2176 sizeof(*mod->markers), &mod->num_markers);
2177#endif
2178#ifdef CONFIG_TRACEPOINTS
2179 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2180 "__tracepoints",
2181 sizeof(*mod->tracepoints),
2182 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002183#endif
Arjan van de Venf71d20e2006-06-28 04:26:45 -07002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002186 if ((mod->num_syms && !mod->crcs)
2187 || (mod->num_gpl_syms && !mod->gpl_crcs)
2188 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002189#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002190 || (mod->num_unused_syms && !mod->unused_crcs)
2191 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002192#endif
2193 ) {
Linus Torvalds826e4502008-05-04 17:04:16 -07002194 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2195 err = try_to_force_load(mod, "nocrc");
2196 if (err)
2197 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199#endif
2200
2201 /* Now do relocations. */
2202 for (i = 1; i < hdr->e_shnum; i++) {
2203 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2204 unsigned int info = sechdrs[i].sh_info;
2205
2206 /* Not a valid relocation section? */
2207 if (info >= hdr->e_shnum)
2208 continue;
2209
2210 /* Don't bother with non-allocated sections */
2211 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2212 continue;
2213
2214 if (sechdrs[i].sh_type == SHT_REL)
2215 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2216 else if (sechdrs[i].sh_type == SHT_RELA)
2217 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2218 mod);
2219 if (err < 0)
2220 goto cleanup;
2221 }
2222
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002223 /* Find duplicate symbols */
2224 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002225 if (err < 0)
2226 goto cleanup;
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002229 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2230 sizeof(*mod->extable), &mod->num_exentries);
2231 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 /* Finally, copy percpu area over. */
2234 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2235 sechdrs[pcpuindex].sh_size);
2236
2237 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2238
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002239 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002240 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002241 unsigned int num_debug;
2242
Rusty Russell5e458cc2008-10-22 10:00:13 -05002243 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2244 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002245 if (debug)
2246 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002247 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002248
Steven Rostedtfed19392008-08-14 22:47:19 -04002249 /* sechdrs[0].sh_size is always zero */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002250 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2251 sizeof(*mseg), &num_mcount);
Steven Rostedt31e88902008-11-14 16:21:19 -08002252 ftrace_init_module(mod, mseg, mseg + num_mcount);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 err = module_finalize(hdr, sechdrs, mod);
2255 if (err < 0)
2256 goto cleanup;
2257
Thomas Koeller378bac82005-09-06 15:17:11 -07002258 /* flush the icache in correct context */
2259 old_fs = get_fs();
2260 set_fs(KERNEL_DS);
2261
2262 /*
2263 * Flush the instruction cache, since we've played with text.
2264 * Do it before processing of module parameters, so the module
2265 * can provide parameter accessor functions of its own.
2266 */
2267 if (mod->module_init)
2268 flush_icache_range((unsigned long)mod->module_init,
2269 (unsigned long)mod->module_init
2270 + mod->init_size);
2271 flush_icache_range((unsigned long)mod->module_core,
2272 (unsigned long)mod->module_core + mod->core_size);
2273
2274 set_fs(old_fs);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002277 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002278 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2279 mod->name);
2280
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002281 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002282 * info during argument parsing. Noone should access us, since
2283 * strong_try_module_get() will fail.
2284 * lockdep/oops can run asynchronous, so use the RCU list insertion
2285 * function to insert in a way safe to concurrent readers.
2286 * The mutex protects against concurrent writers.
2287 */
2288 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002289
Rusty Russelle180a6b2009-03-31 13:05:29 -06002290 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002292 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Rusty Russelle180a6b2009-03-31 13:05:29 -06002294 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002296 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002298 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /* Get rid of temporary copy */
2301 vfree(hdr);
2302
Heiko Carstens9e018922008-12-22 12:36:31 +01002303 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /* Done! */
2305 return mod;
2306
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002307 unlink:
Rusty Russell9b1a4d32008-07-28 12:16:30 -05002308 stop_machine(__unlink_module, mod, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 module_arch_cleanup(mod);
2310 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002311 kobject_del(&mod->mkobj.kobj);
2312 kobject_put(&mod->mkobj.kobj);
Steven Rostedtfed19392008-08-14 22:47:19 -04002313 ftrace_release(mod->module_core, mod->core_size);
Kay Sievers97c146e2007-11-29 23:46:11 +01002314 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302316#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002317 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302318 percpu_modfree(mod->refptr);
2319#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002320 module_free(mod, mod->module_init);
2321 free_core:
2322 module_free(mod, mod->module_core);
2323 /* mod will be freed with core. Don't access it beyond this line! */
2324 free_percpu:
2325 if (percpu)
2326 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 free_mod:
2328 kfree(args);
2329 free_hdr:
2330 vfree(hdr);
Heiko Carstens9e018922008-12-22 12:36:31 +01002331 stop_machine_destroy();
Jayachandran C6fe2e702006-01-06 00:19:54 -08002332 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 truncated:
2335 printk(KERN_ERR "Module len %lu truncated\n", len);
2336 err = -ENOEXEC;
2337 goto free_hdr;
2338}
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002341SYSCALL_DEFINE3(init_module, void __user *, umod,
2342 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 struct module *mod;
2345 int ret = 0;
2346
2347 /* Must have permission */
2348 if (!capable(CAP_SYS_MODULE))
2349 return -EPERM;
2350
2351 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002352 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return -EINTR;
2354
2355 /* Do all the hard work */
2356 mod = load_module(umod, len, uargs);
2357 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002358 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return PTR_ERR(mod);
2360 }
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002363 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Alan Sterne041c682006-03-27 01:16:30 -08002365 blocking_notifier_call_chain(&module_notify_list,
2366 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 /* Start the module */
2369 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002370 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (ret < 0) {
2372 /* Init routine failed: abort. Try to protect us from
2373 buggy refcounters. */
2374 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002375 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002376 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002377 blocking_notifier_call_chain(&module_notify_list,
2378 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002379 mutex_lock(&module_mutex);
2380 free_module(mod);
2381 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002382 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return ret;
2384 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002385 if (ret > 0) {
2386 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2387 "it should follow 0/-E convention\n"
2388 KERN_WARNING "%s: loading module anyway...\n",
2389 __func__, mod->name, ret,
2390 __func__);
2391 dump_stack();
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Rusty Russell6c5db222008-03-10 11:43:52 -07002394 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002396 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002397 blocking_notifier_call_chain(&module_notify_list,
2398 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002399
2400 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* Drop initial reference. */
2402 module_put(mod);
2403 module_free(mod, mod->module_init);
2404 mod->module_init = NULL;
2405 mod->init_size = 0;
2406 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002407 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 return 0;
2410}
2411
2412static inline int within(unsigned long addr, void *start, unsigned long size)
2413{
2414 return ((void *)addr >= start && (void *)addr < start + size);
2415}
2416
2417#ifdef CONFIG_KALLSYMS
2418/*
2419 * This ignores the intensely annoying "mapping symbols" found
2420 * in ARM ELF files: $a, $t and $d.
2421 */
2422static inline int is_arm_mapping_symbol(const char *str)
2423{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002424 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 && (str[2] == '\0' || str[2] == '.');
2426}
2427
2428static const char *get_ksymbol(struct module *mod,
2429 unsigned long addr,
2430 unsigned long *size,
2431 unsigned long *offset)
2432{
2433 unsigned int i, best = 0;
2434 unsigned long nextval;
2435
2436 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002437 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002439 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2441
2442 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002443 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 for (i = 1; i < mod->num_symtab; i++) {
2445 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2446 continue;
2447
2448 /* We ignore unnamed symbols: they're uninformative
2449 * and inserted at a whim. */
2450 if (mod->symtab[i].st_value <= addr
2451 && mod->symtab[i].st_value > mod->symtab[best].st_value
2452 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2453 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2454 best = i;
2455 if (mod->symtab[i].st_value > addr
2456 && mod->symtab[i].st_value < nextval
2457 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2458 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2459 nextval = mod->symtab[i].st_value;
2460 }
2461
2462 if (!best)
2463 return NULL;
2464
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002465 if (size)
2466 *size = nextval - mod->symtab[best].st_value;
2467 if (offset)
2468 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return mod->strtab + mod->symtab[best].st_name;
2470}
2471
Rusty Russell6dd06c92008-01-29 17:13:22 -05002472/* For kallsyms to ask for address resolution. NULL means not found. Careful
2473 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002474const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002475 unsigned long *size,
2476 unsigned long *offset,
2477 char **modname,
2478 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002481 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Rusty Russellcb2a5202008-01-14 00:55:03 -08002483 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002484 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002485 if (within_module_init(addr, mod) ||
2486 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002487 if (modname)
2488 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002489 ret = get_ksymbol(mod, addr, size, offset);
2490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002493 /* Make a copy in here where it's safe */
2494 if (ret) {
2495 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2496 ret = namebuf;
2497 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002498 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002502int lookup_module_symbol_name(unsigned long addr, char *symname)
2503{
2504 struct module *mod;
2505
Rusty Russellcb2a5202008-01-14 00:55:03 -08002506 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002507 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002508 if (within_module_init(addr, mod) ||
2509 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002510 const char *sym;
2511
2512 sym = get_ksymbol(mod, addr, NULL, NULL);
2513 if (!sym)
2514 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002515 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002516 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002517 return 0;
2518 }
2519 }
2520out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002521 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002522 return -ERANGE;
2523}
2524
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002525int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2526 unsigned long *offset, char *modname, char *name)
2527{
2528 struct module *mod;
2529
Rusty Russellcb2a5202008-01-14 00:55:03 -08002530 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002531 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002532 if (within_module_init(addr, mod) ||
2533 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002534 const char *sym;
2535
2536 sym = get_ksymbol(mod, addr, size, offset);
2537 if (!sym)
2538 goto out;
2539 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002540 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002541 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002542 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002543 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002544 return 0;
2545 }
2546 }
2547out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002548 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002549 return -ERANGE;
2550}
2551
Alexey Dobriyanea078902007-05-08 00:28:39 -07002552int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2553 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 struct module *mod;
2556
Rusty Russellcb2a5202008-01-14 00:55:03 -08002557 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002558 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (symnum < mod->num_symtab) {
2560 *value = mod->symtab[symnum].st_value;
2561 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002562 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002563 KSYM_NAME_LEN);
2564 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002565 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002566 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569 symnum -= mod->num_symtab;
2570 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002571 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002572 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
2575static unsigned long mod_find_symname(struct module *mod, const char *name)
2576{
2577 unsigned int i;
2578
2579 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002580 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2581 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return mod->symtab[i].st_value;
2583 return 0;
2584}
2585
2586/* Look for this name: can be of form module:name. */
2587unsigned long module_kallsyms_lookup_name(const char *name)
2588{
2589 struct module *mod;
2590 char *colon;
2591 unsigned long ret = 0;
2592
2593 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002594 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if ((colon = strchr(name, ':')) != NULL) {
2596 *colon = '\0';
2597 if ((mod = find_module(name)) != NULL)
2598 ret = mod_find_symname(mod, colon+1);
2599 *colon = ':';
2600 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002601 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if ((ret = mod_find_symname(mod, name)) != 0)
2603 break;
2604 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002605 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return ret;
2607}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002608
2609int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2610 struct module *, unsigned long),
2611 void *data)
2612{
2613 struct module *mod;
2614 unsigned int i;
2615 int ret;
2616
2617 list_for_each_entry(mod, &modules, list) {
2618 for (i = 0; i < mod->num_symtab; i++) {
2619 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2620 mod, mod->symtab[i].st_value);
2621 if (ret != 0)
2622 return ret;
2623 }
2624 }
2625 return 0;
2626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627#endif /* CONFIG_KALLSYMS */
2628
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002629static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002630{
2631 int bx = 0;
2632
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002633 if (mod->taints ||
2634 mod->state == MODULE_STATE_GOING ||
2635 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002636 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002637 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002638 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002639 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002640 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002641 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002642 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002643 /*
2644 * TAINT_FORCED_RMMOD: could be added.
2645 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2646 * apply to modules.
2647 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002648
2649 /* Show a - for module-is-being-unloaded */
2650 if (mod->state == MODULE_STATE_GOING)
2651 buf[bx++] = '-';
2652 /* Show a + for module-is-being-loaded */
2653 if (mod->state == MODULE_STATE_COMING)
2654 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002655 buf[bx++] = ')';
2656 }
2657 buf[bx] = '\0';
2658
2659 return buf;
2660}
2661
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002662#ifdef CONFIG_PROC_FS
2663/* Called by the /proc file system to return a list of modules. */
2664static void *m_start(struct seq_file *m, loff_t *pos)
2665{
2666 mutex_lock(&module_mutex);
2667 return seq_list_start(&modules, *pos);
2668}
2669
2670static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2671{
2672 return seq_list_next(p, &modules, pos);
2673}
2674
2675static void m_stop(struct seq_file *m, void *p)
2676{
2677 mutex_unlock(&module_mutex);
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680static int m_show(struct seq_file *m, void *p)
2681{
2682 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002683 char buf[8];
2684
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002685 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 mod->name, mod->init_size + mod->core_size);
2687 print_unload_info(m, mod);
2688
2689 /* Informative for users. */
2690 seq_printf(m, " %s",
2691 mod->state == MODULE_STATE_GOING ? "Unloading":
2692 mod->state == MODULE_STATE_COMING ? "Loading":
2693 "Live");
2694 /* Used by oprofile and other similar tools. */
2695 seq_printf(m, " 0x%p", mod->module_core);
2696
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002697 /* Taints info */
2698 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002699 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 seq_printf(m, "\n");
2702 return 0;
2703}
2704
2705/* Format: modulename size refcount deps address
2706
2707 Where refcount is a number or -, and deps is a comma-separated list
2708 of depends or -.
2709*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002710static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 .start = m_start,
2712 .next = m_next,
2713 .stop = m_stop,
2714 .show = m_show
2715};
2716
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002717static int modules_open(struct inode *inode, struct file *file)
2718{
2719 return seq_open(file, &modules_op);
2720}
2721
2722static const struct file_operations proc_modules_operations = {
2723 .open = modules_open,
2724 .read = seq_read,
2725 .llseek = seq_lseek,
2726 .release = seq_release,
2727};
2728
2729static int __init proc_modules_init(void)
2730{
2731 proc_create("modules", 0, NULL, &proc_modules_operations);
2732 return 0;
2733}
2734module_init(proc_modules_init);
2735#endif
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737/* Given an address, look for it in the module exception tables. */
2738const struct exception_table_entry *search_module_extables(unsigned long addr)
2739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 const struct exception_table_entry *e = NULL;
2741 struct module *mod;
2742
Rusty Russell24da1cb2007-07-15 23:41:46 -07002743 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002744 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (mod->num_exentries == 0)
2746 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 e = search_extable(mod->extable,
2749 mod->extable + mod->num_exentries - 1,
2750 addr);
2751 if (e)
2752 break;
2753 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002754 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002757 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return e;
2759}
2760
Ingo Molnar4d435f92006-07-03 00:24:24 -07002761/*
Rusty Russelle6104992009-03-31 13:05:31 -06002762 * is_module_address - is this address inside a module?
2763 * @addr: the address to check.
2764 *
2765 * See is_module_text_address() if you simply want to see if the address
2766 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002767 */
Rusty Russelle6104992009-03-31 13:05:31 -06002768bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002769{
Rusty Russelle6104992009-03-31 13:05:31 -06002770 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002771
Rusty Russell24da1cb2007-07-15 23:41:46 -07002772 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002773 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002774 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002775
Rusty Russelle6104992009-03-31 13:05:31 -06002776 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002777}
2778
Rusty Russelle6104992009-03-31 13:05:31 -06002779/*
2780 * __module_address - get the module which contains an address.
2781 * @addr: the address.
2782 *
2783 * Must be called with preempt disabled or module mutex held so that
2784 * module doesn't get freed during this.
2785 */
2786__notrace_funcgraph struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct module *mod;
2789
Rusty Russell3a642e92008-07-22 19:24:28 -05002790 if (addr < module_addr_min || addr > module_addr_max)
2791 return NULL;
2792
Andi Kleend72b3752008-08-30 10:09:00 +02002793 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002794 if (within_module_core(addr, mod)
2795 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return mod;
2797 return NULL;
2798}
Tim Abbottc6b37802008-12-05 19:03:59 -05002799EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Rusty Russelle6104992009-03-31 13:05:31 -06002801/*
2802 * is_module_text_address - is this address inside module code?
2803 * @addr: the address to check.
2804 *
2805 * See is_module_address() if you simply want to see if the address is
2806 * anywhere in a module. See kernel_text_address() for testing if an
2807 * address corresponds to kernel or module code.
2808 */
2809bool is_module_text_address(unsigned long addr)
2810{
2811 bool ret;
2812
2813 preempt_disable();
2814 ret = __module_text_address(addr) != NULL;
2815 preempt_enable();
2816
2817 return ret;
2818}
2819
2820/*
2821 * __module_text_address - get the module whose code contains an address.
2822 * @addr: the address.
2823 *
2824 * Must be called with preempt disabled or module mutex held so that
2825 * module doesn't get freed during this.
2826 */
2827struct module *__module_text_address(unsigned long addr)
2828{
2829 struct module *mod = __module_address(addr);
2830 if (mod) {
2831 /* Make sure it's within the text section. */
2832 if (!within(addr, mod->module_init, mod->init_text_size)
2833 && !within(addr, mod->module_core, mod->core_text_size))
2834 mod = NULL;
2835 }
2836 return mod;
2837}
Tim Abbottc6b37802008-12-05 19:03:59 -05002838EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840/* Don't grab lock, we're oopsing. */
2841void print_modules(void)
2842{
2843 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002844 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 printk("Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002847 /* Most callers should already have preempt disabled, but make sure */
2848 preempt_disable();
2849 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002850 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002851 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002852 if (last_unloaded_module[0])
2853 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 printk("\n");
2855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857#ifdef CONFIG_MODVERSIONS
2858/* Generate the signature for struct module here, too, for modversions. */
2859void struct_module(struct module *mod) { return; }
2860EXPORT_SYMBOL(struct_module);
2861#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002862
2863#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002864void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002865{
2866 struct module *mod;
2867
2868 mutex_lock(&module_mutex);
2869 list_for_each_entry(mod, &modules, list)
2870 if (!mod->taints)
2871 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002872 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002873 mutex_unlock(&module_mutex);
2874}
2875#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002876
2877#ifdef CONFIG_TRACEPOINTS
2878void module_update_tracepoints(void)
2879{
2880 struct module *mod;
2881
2882 mutex_lock(&module_mutex);
2883 list_for_each_entry(mod, &modules, list)
2884 if (!mod->taints)
2885 tracepoint_update_probe_range(mod->tracepoints,
2886 mod->tracepoints + mod->num_tracepoints);
2887 mutex_unlock(&module_mutex);
2888}
2889
2890/*
2891 * Returns 0 if current not found.
2892 * Returns 1 if current found.
2893 */
2894int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2895{
2896 struct module *iter_mod;
2897 int found = 0;
2898
2899 mutex_lock(&module_mutex);
2900 list_for_each_entry(iter_mod, &modules, list) {
2901 if (!iter_mod->taints) {
2902 /*
2903 * Sorted module list
2904 */
2905 if (iter_mod < iter->module)
2906 continue;
2907 else if (iter_mod > iter->module)
2908 iter->tracepoint = NULL;
2909 found = tracepoint_get_iter_range(&iter->tracepoint,
2910 iter_mod->tracepoints,
2911 iter_mod->tracepoints
2912 + iter_mod->num_tracepoints);
2913 if (found) {
2914 iter->module = iter_mod;
2915 break;
2916 }
2917 }
2918 }
2919 mutex_unlock(&module_mutex);
2920 return found;
2921}
2922#endif