blob: b1821438694e0c26fe9637872647c62e564866ce [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>
Steven Rostedt6d723732009-04-10 14:53:50 -040021#include <linux/ftrace_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Alexey Dobriyanae84e322007-05-08 00:28:38 -070023#include <linux/kallsyms.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040024#include <linux/fs.h>
Roland McGrath6d760132007-10-16 23:26:40 -070025#include <linux/sysfs.h>
Randy Dunlap9f158332005-09-13 01:25:16 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +040030#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/seq_file.h>
32#include <linux/syscalls.h>
33#include <linux/fcntl.h>
34#include <linux/rcupdate.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/cpu.h>
37#include <linux/moduleparam.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/vermagic.h>
41#include <linux/notifier.h>
Al Virof6a57032006-10-18 01:47:25 -040042#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/stop_machine.h>
44#include <linux/device.h>
Matt Domschc988d2b2005-06-23 22:05:15 -070045#include <linux/string.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080046#include <linux/mutex.h>
Andi Kleend72b3752008-08-30 10:09:00 +020047#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/cacheflush.h>
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020050#include <linux/license.h>
Christoph Lameter6d762392008-02-08 04:18:42 -080051#include <asm/sections.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040052#include <linux/tracepoint.h>
Steven Rostedt90d595f2008-08-14 15:45:09 -040053#include <linux/ftrace.h>
Arjan van de Ven22a9d642009-01-07 08:45:46 -080054#include <linux/async.h>
Tejun Heofbf59bc2009-02-20 16:29:08 +090055#include <linux/percpu.h>
Catalin Marinas4f2294b2009-06-11 13:23:20 +010056#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Li Zefan7ead8b82009-08-17 16:56:28 +080058#define CREATE_TRACE_POINTS
59#include <trace/events/module.h>
60
61EXPORT_TRACEPOINT_SYMBOL(module_get);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#if 0
64#define DEBUGP printk
65#else
66#define DEBUGP(fmt , a...)
67#endif
68
69#ifndef ARCH_SHF_SMALL
70#define ARCH_SHF_SMALL 0
71#endif
72
73/* If this is set, the section belongs in the init part of the module */
74#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
75
Rusty Russell24da1cb2007-07-15 23:41:46 -070076/* List of modules, protected by module_mutex or preempt_disable
Andi Kleend72b3752008-08-30 10:09:00 +020077 * (delete uses stop_machine/add uses RCU list operations). */
Tim Abbottc6b37802008-12-05 19:03:59 -050078DEFINE_MUTEX(module_mutex);
79EXPORT_SYMBOL_GPL(module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static LIST_HEAD(modules);
81
Stephen Rothwell19e45292009-04-14 17:27:18 +100082/* Block module loading/unloading? */
83int modules_disabled = 0;
84
Rusty Russellc9a3ba52008-01-29 17:13:18 -050085/* Waiting for a module to finish initializing? */
86static DECLARE_WAIT_QUEUE_HEAD(module_wq);
87
Alan Sterne041c682006-03-27 01:16:30 -080088static BLOCKING_NOTIFIER_HEAD(module_notify_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Rusty Russelle6104992009-03-31 13:05:31 -060090/* Bounds of module allocation, for speeding __module_address */
Rusty Russell3a642e92008-07-22 19:24:28 -050091static unsigned long module_addr_min = -1UL, module_addr_max = 0;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093int register_module_notifier(struct notifier_block * nb)
94{
Alan Sterne041c682006-03-27 01:16:30 -080095 return blocking_notifier_chain_register(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97EXPORT_SYMBOL(register_module_notifier);
98
99int unregister_module_notifier(struct notifier_block * nb)
100{
Alan Sterne041c682006-03-27 01:16:30 -0800101 return blocking_notifier_chain_unregister(&module_notify_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103EXPORT_SYMBOL(unregister_module_notifier);
104
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -0800105/* We require a truly strong try_module_get(): 0 means failure due to
106 ongoing or failed initialization etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static inline int strong_try_module_get(struct module *mod)
108{
109 if (mod && mod->state == MODULE_STATE_COMING)
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500110 return -EBUSY;
111 if (try_module_get(mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500113 else
114 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700117static inline void add_taint_module(struct module *mod, unsigned flag)
118{
119 add_taint(flag);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700120 mod->taints |= (1U << flag);
Florin Malitafa3ba2e82006-10-11 01:21:48 -0700121}
122
Robert P. J. Day02a3e592007-05-09 07:26:28 +0200123/*
124 * A thread that wants to hold a reference to a module only while it
125 * is running can call this to safely exit. nfsd and lockd use this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127void __module_put_and_exit(struct module *mod, long code)
128{
129 module_put(mod);
130 do_exit(code);
131}
132EXPORT_SYMBOL(__module_put_and_exit);
Daniel Walker22a8bde2007-10-18 03:06:07 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* Find a module section: 0 means not found. */
135static unsigned int find_sec(Elf_Ehdr *hdr,
136 Elf_Shdr *sechdrs,
137 const char *secstrings,
138 const char *name)
139{
140 unsigned int i;
141
142 for (i = 1; i < hdr->e_shnum; i++)
143 /* Alloc bit cleared means "ignore it." */
144 if ((sechdrs[i].sh_flags & SHF_ALLOC)
145 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
146 return i;
147 return 0;
148}
149
Rusty Russell5e458cc2008-10-22 10:00:13 -0500150/* Find a module section, or NULL. */
151static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
152 const char *secstrings, const char *name)
153{
154 /* Section 0 has sh_addr 0. */
155 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
156}
157
158/* Find a module section, or NULL. Fill in number of "objects" in section. */
159static void *section_objs(Elf_Ehdr *hdr,
160 Elf_Shdr *sechdrs,
161 const char *secstrings,
162 const char *name,
163 size_t object_size,
164 unsigned int *num)
165{
166 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
167
168 /* Section 0 has sh_addr 0 and sh_size 0. */
169 *num = sechdrs[sec].sh_size / object_size;
170 return (void *)sechdrs[sec].sh_addr;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* Provided by the linker */
174extern const struct kernel_symbol __start___ksymtab[];
175extern const struct kernel_symbol __stop___ksymtab[];
176extern const struct kernel_symbol __start___ksymtab_gpl[];
177extern const struct kernel_symbol __stop___ksymtab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800178extern const struct kernel_symbol __start___ksymtab_gpl_future[];
179extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700180extern const struct kernel_symbol __start___ksymtab_gpl_future[];
181extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182extern const unsigned long __start___kcrctab[];
183extern const unsigned long __start___kcrctab_gpl[];
Greg Kroah-Hartman9f28bb72006-03-20 13:17:13 -0800184extern const unsigned long __start___kcrctab_gpl_future[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500185#ifdef CONFIG_UNUSED_SYMBOLS
186extern const struct kernel_symbol __start___ksymtab_unused[];
187extern const struct kernel_symbol __stop___ksymtab_unused[];
188extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
189extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700190extern const unsigned long __start___kcrctab_unused[];
191extern const unsigned long __start___kcrctab_unused_gpl[];
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194#ifndef CONFIG_MODVERSIONS
195#define symversion(base, idx) NULL
196#else
Andrew Mortonf83ca9f2006-03-28 01:56:20 -0800197#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#endif
199
Rusty Russelldafd0942008-07-22 19:24:25 -0500200static bool each_symbol_in_section(const struct symsearch *arr,
201 unsigned int arrsize,
202 struct module *owner,
203 bool (*fn)(const struct symsearch *syms,
204 struct module *owner,
205 unsigned int symnum, void *data),
206 void *data)
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100207{
Rusty Russelldafd0942008-07-22 19:24:25 -0500208 unsigned int i, j;
209
210 for (j = 0; j < arrsize; j++) {
211 for (i = 0; i < arr[j].stop - arr[j].start; i++)
212 if (fn(&arr[j], owner, i, data))
213 return true;
214 }
215
216 return false;
Sam Ravnborg3fd68052006-02-08 21:16:45 +0100217}
218
Rusty Russelldafd0942008-07-22 19:24:25 -0500219/* Returns true as soon as fn returns true, otherwise false. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500220bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
221 unsigned int symnum, void *data), void *data)
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700222{
Rusty Russelldafd0942008-07-22 19:24:25 -0500223 struct module *mod;
224 const struct symsearch arr[] = {
225 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
226 NOT_GPL_ONLY, false },
227 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
228 __start___kcrctab_gpl,
229 GPL_ONLY, false },
230 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
231 __start___kcrctab_gpl_future,
232 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500233#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500234 { __start___ksymtab_unused, __stop___ksymtab_unused,
235 __start___kcrctab_unused,
236 NOT_GPL_ONLY, true },
237 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
238 __start___kcrctab_unused_gpl,
239 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500240#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500241 };
242
243 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
244 return true;
245
Andi Kleend72b3752008-08-30 10:09:00 +0200246 list_for_each_entry_rcu(mod, &modules, list) {
Rusty Russelldafd0942008-07-22 19:24:25 -0500247 struct symsearch arr[] = {
248 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
249 NOT_GPL_ONLY, false },
250 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
251 mod->gpl_crcs,
252 GPL_ONLY, false },
253 { mod->gpl_future_syms,
254 mod->gpl_future_syms + mod->num_gpl_future_syms,
255 mod->gpl_future_crcs,
256 WILL_BE_GPL_ONLY, false },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500257#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500258 { mod->unused_syms,
259 mod->unused_syms + mod->num_unused_syms,
260 mod->unused_crcs,
261 NOT_GPL_ONLY, true },
262 { mod->unused_gpl_syms,
263 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
264 mod->unused_gpl_crcs,
265 GPL_ONLY, true },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500266#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500267 };
268
269 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
270 return true;
271 }
272 return false;
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700273}
Tim Abbottc6b37802008-12-05 19:03:59 -0500274EXPORT_SYMBOL_GPL(each_symbol);
Arjan van de Venf71d20e2006-06-28 04:26:45 -0700275
Rusty Russelldafd0942008-07-22 19:24:25 -0500276struct find_symbol_arg {
277 /* Input */
278 const char *name;
279 bool gplok;
280 bool warn;
281
282 /* Output */
283 struct module *owner;
284 const unsigned long *crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500285 const struct kernel_symbol *sym;
Rusty Russelldafd0942008-07-22 19:24:25 -0500286};
287
288static bool find_symbol_in_section(const struct symsearch *syms,
289 struct module *owner,
290 unsigned int symnum, void *data)
Rusty Russellad9546c2008-05-01 21:14:59 -0500291{
Rusty Russelldafd0942008-07-22 19:24:25 -0500292 struct find_symbol_arg *fsa = data;
293
294 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
295 return false;
296
297 if (!fsa->gplok) {
298 if (syms->licence == GPL_ONLY)
299 return false;
300 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
301 printk(KERN_WARNING "Symbol %s is being used "
302 "by a non-GPL module, which will not "
303 "be allowed in the future\n", fsa->name);
304 printk(KERN_WARNING "Please see the file "
305 "Documentation/feature-removal-schedule.txt "
306 "in the kernel source tree for more details.\n");
307 }
308 }
309
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500310#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russelldafd0942008-07-22 19:24:25 -0500311 if (syms->unused && fsa->warn) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500312 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
Rusty Russelldafd0942008-07-22 19:24:25 -0500313 "however this module is using it.\n", fsa->name);
Rusty Russellad9546c2008-05-01 21:14:59 -0500314 printk(KERN_WARNING
315 "This symbol will go away in the future.\n");
316 printk(KERN_WARNING
317 "Please evalute if this is the right api to use and if "
318 "it really is, submit a report the linux kernel "
319 "mailinglist together with submitting your code for "
320 "inclusion.\n");
321 }
Denys Vlasenkof7f5b672008-07-22 19:24:26 -0500322#endif
Rusty Russelldafd0942008-07-22 19:24:25 -0500323
324 fsa->owner = owner;
325 fsa->crc = symversion(syms->crcs, symnum);
Tim Abbott414fd312008-12-05 19:03:56 -0500326 fsa->sym = &syms->start[symnum];
Rusty Russellad9546c2008-05-01 21:14:59 -0500327 return true;
328}
329
Tim Abbott414fd312008-12-05 19:03:56 -0500330/* Find a symbol and return it, along with, (optional) crc and
331 * (optional) module which owns it */
Tim Abbottc6b37802008-12-05 19:03:59 -0500332const struct kernel_symbol *find_symbol(const char *name,
333 struct module **owner,
334 const unsigned long **crc,
335 bool gplok,
336 bool warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Rusty Russelldafd0942008-07-22 19:24:25 -0500338 struct find_symbol_arg fsa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Rusty Russelldafd0942008-07-22 19:24:25 -0500340 fsa.name = name;
341 fsa.gplok = gplok;
342 fsa.warn = warn;
343
344 if (each_symbol(find_symbol_in_section, &fsa)) {
Rusty Russellad9546c2008-05-01 21:14:59 -0500345 if (owner)
Rusty Russelldafd0942008-07-22 19:24:25 -0500346 *owner = fsa.owner;
347 if (crc)
348 *crc = fsa.crc;
Tim Abbott414fd312008-12-05 19:03:56 -0500349 return fsa.sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Rusty Russellad9546c2008-05-01 21:14:59 -0500351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 DEBUGP("Failed to find symbol %s\n", name);
Tim Abbott414fd312008-12-05 19:03:56 -0500353 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Tim Abbottc6b37802008-12-05 19:03:59 -0500355EXPORT_SYMBOL_GPL(find_symbol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* Search for module by name: must hold module_mutex. */
Tim Abbottc6b37802008-12-05 19:03:59 -0500358struct module *find_module(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct module *mod;
361
362 list_for_each_entry(mod, &modules, list) {
363 if (strcmp(mod->name, name) == 0)
364 return mod;
365 }
366 return NULL;
367}
Tim Abbottc6b37802008-12-05 19:03:59 -0500368EXPORT_SYMBOL_GPL(find_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370#ifdef CONFIG_SMP
Tejun Heofbf59bc2009-02-20 16:29:08 +0900371
372#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
373
374static void *percpu_modalloc(unsigned long size, unsigned long align,
375 const char *name)
376{
377 void *ptr;
378
379 if (align > PAGE_SIZE) {
380 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
381 name, align, PAGE_SIZE);
382 align = PAGE_SIZE;
383 }
384
Tejun Heoedcb4632009-03-06 14:33:59 +0900385 ptr = __alloc_reserved_percpu(size, align);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900386 if (!ptr)
387 printk(KERN_WARNING
388 "Could not allocate %lu bytes percpu data\n", size);
389 return ptr;
390}
391
392static void percpu_modfree(void *freeme)
393{
394 free_percpu(freeme);
395}
396
397#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/* Number of blocks used and allocated. */
400static unsigned int pcpu_num_used, pcpu_num_allocated;
401/* Size of each block. -ve means used. */
402static int *pcpu_size;
403
404static int split_block(unsigned int i, unsigned short size)
405{
406 /* Reallocation required? */
407 if (pcpu_num_used + 1 > pcpu_num_allocated) {
Pekka Enberg6d4f9c52007-05-08 00:24:58 -0700408 int *new;
409
410 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
411 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!new)
413 return 0;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 pcpu_num_allocated *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 pcpu_size = new;
417 }
418
419 /* Insert a new subblock */
420 memmove(&pcpu_size[i+1], &pcpu_size[i],
421 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
422 pcpu_num_used++;
423
424 pcpu_size[i+1] -= size;
425 pcpu_size[i] = size;
426 return 1;
427}
428
429static inline unsigned int block_size(int val)
430{
431 if (val < 0)
432 return -val;
433 return val;
434}
435
Rusty Russell842bbaa2005-08-01 21:11:47 -0700436static void *percpu_modalloc(unsigned long size, unsigned long align,
437 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 unsigned long extra;
440 unsigned int i;
441 void *ptr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100442 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +0200444 if (align > PAGE_SIZE) {
445 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
446 name, align, PAGE_SIZE);
447 align = PAGE_SIZE;
Rusty Russell842bbaa2005-08-01 21:11:47 -0700448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 ptr = __per_cpu_start;
451 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
452 /* Extra for alignment requirement. */
453 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
454 BUG_ON(i == 0 && extra != 0);
455
456 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
457 continue;
458
459 /* Transfer extra to previous block. */
460 if (pcpu_size[i-1] < 0)
461 pcpu_size[i-1] -= extra;
462 else
463 pcpu_size[i-1] += extra;
464 pcpu_size[i] -= extra;
465 ptr += extra;
466
467 /* Split block if warranted */
468 if (pcpu_size[i] - size > sizeof(unsigned long))
469 if (!split_block(i, size))
470 return NULL;
471
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100472 /* add the per-cpu scanning areas */
473 for_each_possible_cpu(cpu)
474 kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0,
475 GFP_KERNEL);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* Mark allocated */
478 pcpu_size[i] = -pcpu_size[i];
479 return ptr;
480 }
481
482 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
483 size);
484 return NULL;
485}
486
487static void percpu_modfree(void *freeme)
488{
489 unsigned int i;
490 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100491 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* First entry is core kernel percpu data. */
494 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
495 if (ptr == freeme) {
496 pcpu_size[i] = -pcpu_size[i];
497 goto free;
498 }
499 }
500 BUG();
501
502 free:
Catalin Marinas4f2294b2009-06-11 13:23:20 +0100503 /* remove the per-cpu scanning areas */
504 for_each_possible_cpu(cpu)
505 kmemleak_free(freeme + per_cpu_offset(cpu));
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Merge with previous? */
508 if (pcpu_size[i-1] >= 0) {
509 pcpu_size[i-1] += pcpu_size[i];
510 pcpu_num_used--;
511 memmove(&pcpu_size[i], &pcpu_size[i+1],
512 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
513 i--;
514 }
515 /* Merge with next? */
516 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
517 pcpu_size[i] += pcpu_size[i+1];
518 pcpu_num_used--;
519 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
520 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
521 }
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524static int percpu_modinit(void)
525{
526 pcpu_num_used = 2;
527 pcpu_num_allocated = 2;
528 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
529 GFP_KERNEL);
530 /* Static in-kernel percpu data (used). */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +0200531 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Free room. */
533 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
534 if (pcpu_size[1] < 0) {
535 printk(KERN_ERR "No per-cpu room for modules.\n");
536 pcpu_num_used = 1;
537 }
538
539 return 0;
Daniel Walker22a8bde2007-10-18 03:06:07 -0700540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541__initcall(percpu_modinit);
Tejun Heo6b588c12009-02-20 16:29:07 +0900542
Tejun Heofbf59bc2009-02-20 16:29:08 +0900543#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
544
Tejun Heo6b588c12009-02-20 16:29:07 +0900545static unsigned int find_pcpusec(Elf_Ehdr *hdr,
546 Elf_Shdr *sechdrs,
547 const char *secstrings)
548{
549 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
550}
551
552static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
553{
554 int cpu;
555
556 for_each_possible_cpu(cpu)
557 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#else /* ... !CONFIG_SMP */
Tejun Heo6b588c12009-02-20 16:29:07 +0900561
Rusty Russell842bbaa2005-08-01 21:11:47 -0700562static inline void *percpu_modalloc(unsigned long size, unsigned long align,
563 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 return NULL;
566}
567static inline void percpu_modfree(void *pcpuptr)
568{
569 BUG();
570}
571static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
572 Elf_Shdr *sechdrs,
573 const char *secstrings)
574{
575 return 0;
576}
577static inline void percpu_modcopy(void *pcpudst, const void *src,
578 unsigned long size)
579{
580 /* pcpusec should be 0, and size of that section should be 0. */
581 BUG_ON(size != 0);
582}
Tejun Heo6b588c12009-02-20 16:29:07 +0900583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#endif /* CONFIG_SMP */
585
Matt Domschc988d2b2005-06-23 22:05:15 -0700586#define MODINFO_ATTR(field) \
587static void setup_modinfo_##field(struct module *mod, const char *s) \
588{ \
589 mod->field = kstrdup(s, GFP_KERNEL); \
590} \
591static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
592 struct module *mod, char *buffer) \
593{ \
594 return sprintf(buffer, "%s\n", mod->field); \
595} \
596static int modinfo_##field##_exists(struct module *mod) \
597{ \
598 return mod->field != NULL; \
599} \
600static void free_modinfo_##field(struct module *mod) \
601{ \
Daniel Walker22a8bde2007-10-18 03:06:07 -0700602 kfree(mod->field); \
603 mod->field = NULL; \
Matt Domschc988d2b2005-06-23 22:05:15 -0700604} \
605static struct module_attribute modinfo_##field = { \
Tejun Heo7b595752007-06-14 03:45:17 +0900606 .attr = { .name = __stringify(field), .mode = 0444 }, \
Matt Domschc988d2b2005-06-23 22:05:15 -0700607 .show = show_modinfo_##field, \
608 .setup = setup_modinfo_##field, \
609 .test = modinfo_##field##_exists, \
610 .free = free_modinfo_##field, \
611};
612
613MODINFO_ATTR(version);
614MODINFO_ATTR(srcversion);
615
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100616static char last_unloaded_module[MODULE_NAME_LEN+1];
617
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -0800618#ifdef CONFIG_MODULE_UNLOAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/* Init the unload section of the module. */
620static void module_unload_init(struct module *mod)
621{
Eric Dumazet720eba32009-02-03 13:31:36 +1030622 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 INIT_LIST_HEAD(&mod->modules_which_use_me);
Eric Dumazet720eba32009-02-03 13:31:36 +1030625 for_each_possible_cpu(cpu)
626 local_set(__module_ref_addr(mod, cpu), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Hold reference count during initialization. */
Eric Dumazet720eba32009-02-03 13:31:36 +1030628 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Backwards compatibility macros put refcount during init. */
630 mod->waiter = current;
631}
632
633/* modules using other modules */
634struct module_use
635{
636 struct list_head list;
637 struct module *module_which_uses;
638};
639
640/* Does a already use b? */
641static int already_uses(struct module *a, struct module *b)
642{
643 struct module_use *use;
644
645 list_for_each_entry(use, &b->modules_which_use_me, list) {
646 if (use->module_which_uses == a) {
647 DEBUGP("%s uses %s!\n", a->name, b->name);
648 return 1;
649 }
650 }
651 DEBUGP("%s does not use %s!\n", a->name, b->name);
652 return 0;
653}
654
655/* Module a uses b */
Tim Abbottc6b37802008-12-05 19:03:59 -0500656int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct module_use *use;
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500659 int no_warn, err;
Kay Sievers270a6c42007-01-18 13:26:15 +0100660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (b == NULL || already_uses(a, b)) return 1;
662
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500663 /* If we're interrupted or time out, we fail. */
664 if (wait_event_interruptible_timeout(
665 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
666 30 * HZ) <= 0) {
667 printk("%s: gave up waiting for init of module %s.\n",
668 a->name, b->name);
669 return 0;
670 }
671
672 /* If strong_try_module_get() returned a different error, we fail. */
673 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675
676 DEBUGP("Allocating new usage for %s.\n", a->name);
677 use = kmalloc(sizeof(*use), GFP_ATOMIC);
678 if (!use) {
679 printk("%s: out of memory loading\n", a->name);
680 module_put(b);
681 return 0;
682 }
683
684 use->module_which_uses = a;
685 list_add(&use->list, &b->modules_which_use_me);
Kay Sievers270a6c42007-01-18 13:26:15 +0100686 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 1;
688}
Tim Abbottc6b37802008-12-05 19:03:59 -0500689EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/* Clear the unload stuff of the module. */
692static void module_unload_free(struct module *mod)
693{
694 struct module *i;
695
696 list_for_each_entry(i, &modules, list) {
697 struct module_use *use;
698
699 list_for_each_entry(use, &i->modules_which_use_me, list) {
700 if (use->module_which_uses == mod) {
701 DEBUGP("%s unusing %s\n", mod->name, i->name);
702 module_put(i);
703 list_del(&use->list);
704 kfree(use);
Kay Sievers270a6c42007-01-18 13:26:15 +0100705 sysfs_remove_link(i->holders_dir, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* There can be at most one match. */
707 break;
708 }
709 }
710 }
711}
712
713#ifdef CONFIG_MODULE_FORCE_UNLOAD
Akinobu Mitafb169792006-01-08 01:04:29 -0800714static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 int ret = (flags & O_TRUNC);
717 if (ret)
Akinobu Mitafb169792006-01-08 01:04:29 -0800718 add_taint(TAINT_FORCED_RMMOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return ret;
720}
721#else
Akinobu Mitafb169792006-01-08 01:04:29 -0800722static inline int try_force_unload(unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 return 0;
725}
726#endif /* CONFIG_MODULE_FORCE_UNLOAD */
727
728struct stopref
729{
730 struct module *mod;
731 int flags;
732 int *forced;
733};
734
735/* Whole machine is stopped with interrupts off when this runs. */
736static int __try_stop_module(void *_sref)
737{
738 struct stopref *sref = _sref;
739
Rusty Russellda39ba52008-07-22 19:24:25 -0500740 /* If it's not unused, quit unless we're forcing. */
741 if (module_refcount(sref->mod) != 0) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800742 if (!(*sref->forced = try_force_unload(sref->flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -EWOULDBLOCK;
744 }
745
746 /* Mark it as dying. */
747 sref->mod->state = MODULE_STATE_GOING;
748 return 0;
749}
750
751static int try_stop_module(struct module *mod, int flags, int *forced)
752{
Rusty Russellda39ba52008-07-22 19:24:25 -0500753 if (flags & O_NONBLOCK) {
754 struct stopref sref = { mod, flags, forced };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500756 return stop_machine(__try_stop_module, &sref, NULL);
Rusty Russellda39ba52008-07-22 19:24:25 -0500757 } else {
758 /* We don't need to stop the machine for this. */
759 mod->state = MODULE_STATE_GOING;
760 synchronize_sched();
761 return 0;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765unsigned int module_refcount(struct module *mod)
766{
Eric Dumazet720eba32009-02-03 13:31:36 +1030767 unsigned int total = 0;
768 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Eric Dumazet720eba32009-02-03 13:31:36 +1030770 for_each_possible_cpu(cpu)
771 total += local_read(__module_ref_addr(mod, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return total;
773}
774EXPORT_SYMBOL(module_refcount);
775
776/* This exists whether we can unload or not */
777static void free_module(struct module *mod);
778
779static void wait_for_zero_refcount(struct module *mod)
780{
Matthew Wilcoxa6550202008-02-26 10:47:18 -0500781 /* Since we might sleep for some time, release the mutex first */
Ashutosh Naik6389a382006-03-23 03:00:46 -0800782 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 for (;;) {
784 DEBUGP("Looking at refcount...\n");
785 set_current_state(TASK_UNINTERRUPTIBLE);
786 if (module_refcount(mod) == 0)
787 break;
788 schedule();
789 }
790 current->state = TASK_RUNNING;
Ashutosh Naik6389a382006-03-23 03:00:46 -0800791 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Heiko Carstens17da2bd2009-01-14 14:14:10 +0100794SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
795 unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct module *mod;
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800798 char name[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int ret, forced = 0;
800
Kees Cook3d433212009-04-02 15:49:29 -0700801 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Greg Kroah-Hartmandfff0a02007-02-23 14:54:57 -0800802 return -EPERM;
803
804 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
805 return -EFAULT;
806 name[MODULE_NAME_LEN-1] = '\0';
807
Heiko Carstens9e018922008-12-22 12:36:31 +0100808 /* Create stop_machine threads since free_module relies on
809 * a non-failing stop_machine call. */
810 ret = stop_machine_create();
811 if (ret)
812 return ret;
813
814 if (mutex_lock_interruptible(&module_mutex) != 0) {
815 ret = -EINTR;
816 goto out_stop;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 mod = find_module(name);
820 if (!mod) {
821 ret = -ENOENT;
822 goto out;
823 }
824
825 if (!list_empty(&mod->modules_which_use_me)) {
826 /* Other modules depend on us: get rid of them first. */
827 ret = -EWOULDBLOCK;
828 goto out;
829 }
830
831 /* Doing init or already dying? */
832 if (mod->state != MODULE_STATE_LIVE) {
833 /* FIXME: if (force), slam module count and wake up
834 waiter --RR */
835 DEBUGP("%s already dying\n", mod->name);
836 ret = -EBUSY;
837 goto out;
838 }
839
840 /* If it has an init func, it must have an exit func to unload */
Rusty Russellaf49d922007-10-16 23:26:27 -0700841 if (mod->init && !mod->exit) {
Akinobu Mitafb169792006-01-08 01:04:29 -0800842 forced = try_force_unload(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!forced) {
844 /* This module can't be removed */
845 ret = -EBUSY;
846 goto out;
847 }
848 }
849
850 /* Set this up before setting mod->state */
851 mod->waiter = current;
852
853 /* Stop the machine so refcounts can't move and disable module. */
854 ret = try_stop_module(mod, flags, &forced);
855 if (ret != 0)
856 goto out;
857
858 /* Never wait if forced. */
859 if (!forced && module_refcount(mod) != 0)
860 wait_for_zero_refcount(mod);
861
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200862 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Final destruction now noone is using it. */
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200864 if (mod->exit != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 mod->exit();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200866 blocking_notifier_call_chain(&module_notify_list,
867 MODULE_STATE_GOING, mod);
Arjan van de Ven22a9d642009-01-07 08:45:46 -0800868 async_synchronize_full();
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +0200869 mutex_lock(&module_mutex);
Arjan van de Vene14af7e2008-01-25 21:08:33 +0100870 /* Store the name of the last unloaded module for diagnostic purposes */
Rusty Russellefa53452008-01-29 17:13:20 -0500871 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
Jason Barone9d376f2009-02-05 11:51:38 -0500872 ddebug_remove_module(mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 free_module(mod);
874
875 out:
Ashutosh Naik6389a382006-03-23 03:00:46 -0800876 mutex_unlock(&module_mutex);
Heiko Carstens9e018922008-12-22 12:36:31 +0100877out_stop:
878 stop_machine_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return ret;
880}
881
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800882static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct module_use *use;
885 int printed_something = 0;
886
887 seq_printf(m, " %u ", module_refcount(mod));
888
889 /* Always include a trailing , so userspace can differentiate
890 between this and the old multi-field proc format. */
891 list_for_each_entry(use, &mod->modules_which_use_me, list) {
892 printed_something = 1;
893 seq_printf(m, "%s,", use->module_which_uses->name);
894 }
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (mod->init != NULL && mod->exit == NULL) {
897 printed_something = 1;
898 seq_printf(m, "[permanent],");
899 }
900
901 if (!printed_something)
902 seq_printf(m, "-");
903}
904
905void __symbol_put(const char *symbol)
906{
907 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Rusty Russell24da1cb2007-07-15 23:41:46 -0700909 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -0500910 if (!find_symbol(symbol, &owner, NULL, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 BUG();
912 module_put(owner);
Rusty Russell24da1cb2007-07-15 23:41:46 -0700913 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915EXPORT_SYMBOL(__symbol_put);
916
917void symbol_put_addr(void *addr)
918{
Trent Piepho5e376612006-05-15 09:44:06 -0700919 struct module *modaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Trent Piepho5e376612006-05-15 09:44:06 -0700921 if (core_kernel_text((unsigned long)addr))
922 return;
923
Rusty Russella6e6abd2009-03-31 13:05:31 -0600924 /* module_text_address is safe here: we're supposed to have reference
925 * to module from symbol_get, so it can't go away. */
926 modaddr = __module_text_address((unsigned long)addr);
927 BUG_ON(!modaddr);
Trent Piepho5e376612006-05-15 09:44:06 -0700928 module_put(modaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930EXPORT_SYMBOL_GPL(symbol_put_addr);
931
932static ssize_t show_refcnt(struct module_attribute *mattr,
933 struct module *mod, char *buffer)
934{
Alexey Dobriyan256e2fd2007-08-06 23:47:45 +0400935 return sprintf(buffer, "%u\n", module_refcount(mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938static struct module_attribute refcnt = {
Tejun Heo7b595752007-06-14 03:45:17 +0900939 .attr = { .name = "refcnt", .mode = 0444 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .show = show_refcnt,
941};
942
Al Virof6a57032006-10-18 01:47:25 -0400943void module_put(struct module *module)
944{
945 if (module) {
946 unsigned int cpu = get_cpu();
Eric Dumazet720eba32009-02-03 13:31:36 +1030947 local_dec(__module_ref_addr(module, cpu));
Li Zefan7ead8b82009-08-17 16:56:28 +0800948 trace_module_put(module, _RET_IP_,
949 local_read(__module_ref_addr(module, cpu)));
Al Virof6a57032006-10-18 01:47:25 -0400950 /* Maybe they're waiting for us to drop reference? */
951 if (unlikely(!module_is_live(module)))
952 wake_up_process(module->waiter);
953 put_cpu();
954 }
955}
956EXPORT_SYMBOL(module_put);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#else /* !CONFIG_MODULE_UNLOAD */
Jianjun Kongd1e99d72008-12-08 14:26:29 +0800959static inline void print_unload_info(struct seq_file *m, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 /* We don't know the usage count, or what modules are using. */
962 seq_printf(m, " - -");
963}
964
965static inline void module_unload_free(struct module *mod)
966{
967}
968
Tim Abbottc6b37802008-12-05 19:03:59 -0500969int use_module(struct module *a, struct module *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Rusty Russellc9a3ba52008-01-29 17:13:18 -0500971 return strong_try_module_get(b) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Tim Abbottc6b37802008-12-05 19:03:59 -0500973EXPORT_SYMBOL_GPL(use_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975static inline void module_unload_init(struct module *mod)
976{
977}
978#endif /* CONFIG_MODULE_UNLOAD */
979
Kay Sievers1f717402006-11-24 12:15:25 +0100980static ssize_t show_initstate(struct module_attribute *mattr,
981 struct module *mod, char *buffer)
982{
983 const char *state = "unknown";
984
985 switch (mod->state) {
986 case MODULE_STATE_LIVE:
987 state = "live";
988 break;
989 case MODULE_STATE_COMING:
990 state = "coming";
991 break;
992 case MODULE_STATE_GOING:
993 state = "going";
994 break;
995 }
996 return sprintf(buffer, "%s\n", state);
997}
998
999static struct module_attribute initstate = {
Tejun Heo7b595752007-06-14 03:45:17 +09001000 .attr = { .name = "initstate", .mode = 0444 },
Kay Sievers1f717402006-11-24 12:15:25 +01001001 .show = show_initstate,
1002};
1003
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001004static struct module_attribute *modinfo_attrs[] = {
1005 &modinfo_version,
1006 &modinfo_srcversion,
Kay Sievers1f717402006-11-24 12:15:25 +01001007 &initstate,
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001008#ifdef CONFIG_MODULE_UNLOAD
1009 &refcnt,
1010#endif
1011 NULL,
1012};
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014static const char vermagic[] = VERMAGIC_STRING;
1015
Rusty Russellc6e665c2009-03-31 13:05:33 -06001016static int try_to_force_load(struct module *mod, const char *reason)
Linus Torvalds826e4502008-05-04 17:04:16 -07001017{
1018#ifdef CONFIG_MODULE_FORCE_LOAD
Andi Kleen25ddbb12008-10-15 22:01:41 -07001019 if (!test_taint(TAINT_FORCED_MODULE))
Rusty Russellc6e665c2009-03-31 13:05:33 -06001020 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1021 mod->name, reason);
Linus Torvalds826e4502008-05-04 17:04:16 -07001022 add_taint_module(mod, TAINT_FORCED_MODULE);
1023 return 0;
1024#else
1025 return -ENOEXEC;
1026#endif
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029#ifdef CONFIG_MODVERSIONS
1030static int check_version(Elf_Shdr *sechdrs,
1031 unsigned int versindex,
1032 const char *symname,
1033 struct module *mod,
1034 const unsigned long *crc)
1035{
1036 unsigned int i, num_versions;
1037 struct modversion_info *versions;
1038
1039 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1040 if (!crc)
1041 return 1;
1042
Rusty Russella5dd6972008-05-09 16:24:21 +10001043 /* No versions at all? modprobe --force does this. */
1044 if (versindex == 0)
1045 return try_to_force_load(mod, symname) == 0;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 versions = (void *) sechdrs[versindex].sh_addr;
1048 num_versions = sechdrs[versindex].sh_size
1049 / sizeof(struct modversion_info);
1050
1051 for (i = 0; i < num_versions; i++) {
1052 if (strcmp(versions[i].name, symname) != 0)
1053 continue;
1054
1055 if (versions[i].crc == *crc)
1056 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 DEBUGP("Found checksum %lX vs module %lX\n",
1058 *crc, versions[i].crc);
Linus Torvalds826e4502008-05-04 17:04:16 -07001059 goto bad_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Linus Torvalds826e4502008-05-04 17:04:16 -07001061
Rusty Russella5dd6972008-05-09 16:24:21 +10001062 printk(KERN_WARNING "%s: no symbol version for %s\n",
1063 mod->name, symname);
1064 return 0;
Linus Torvalds826e4502008-05-04 17:04:16 -07001065
1066bad_version:
1067 printk("%s: disagrees about version of symbol %s\n",
1068 mod->name, symname);
1069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1073 unsigned int versindex,
1074 struct module *mod)
1075{
1076 const unsigned long *crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Mike Frysinger6560dc12009-07-23 23:42:08 +09301078 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1079 &crc, true, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 BUG();
Rusty Russell8c8ef422009-03-31 13:05:34 -06001081 return check_version(sechdrs, versindex, "module_layout", mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Rusty Russell91e37a72008-05-09 16:25:28 +10001084/* First part is kernel version, which we ignore if module has crcs. */
1085static inline int same_magic(const char *amagic, const char *bmagic,
1086 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Rusty Russell91e37a72008-05-09 16:25:28 +10001088 if (has_crcs) {
1089 amagic += strcspn(amagic, " ");
1090 bmagic += strcspn(bmagic, " ");
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return strcmp(amagic, bmagic) == 0;
1093}
1094#else
1095static inline int check_version(Elf_Shdr *sechdrs,
1096 unsigned int versindex,
1097 const char *symname,
1098 struct module *mod,
1099 const unsigned long *crc)
1100{
1101 return 1;
1102}
1103
1104static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1105 unsigned int versindex,
1106 struct module *mod)
1107{
1108 return 1;
1109}
1110
Rusty Russell91e37a72008-05-09 16:25:28 +10001111static inline int same_magic(const char *amagic, const char *bmagic,
1112 bool has_crcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 return strcmp(amagic, bmagic) == 0;
1115}
1116#endif /* CONFIG_MODVERSIONS */
1117
1118/* Resolve a symbol for this module. I.e. if we find one, record usage.
1119 Must be holding module_mutex. */
Tim Abbott414fd312008-12-05 19:03:56 -05001120static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1121 unsigned int versindex,
1122 const char *name,
1123 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001126 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 const unsigned long *crc;
1128
Tim Abbott414fd312008-12-05 19:03:56 -05001129 sym = find_symbol(name, &owner, &crc,
Andi Kleen25ddbb12008-10-15 22:01:41 -07001130 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
Tim Abbott414fd312008-12-05 19:03:56 -05001131 /* use_module can fail due to OOM,
1132 or module initialization or unloading */
1133 if (sym) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1135 !use_module(mod, owner))
Tim Abbott414fd312008-12-05 19:03:56 -05001136 sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
Tim Abbott414fd312008-12-05 19:03:56 -05001138 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
1142 * /sys/module/foo/sections stuff
1143 * J. Corbet <corbet@lwn.net>
1144 */
Kay Sievers120fc3d2008-02-21 00:33:20 +01001145#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
Rusty Russella58730c2008-03-13 09:03:44 +00001146struct module_sect_attr
1147{
1148 struct module_attribute mattr;
1149 char *name;
1150 unsigned long address;
1151};
1152
1153struct module_sect_attrs
1154{
1155 struct attribute_group grp;
1156 unsigned int nsections;
1157 struct module_sect_attr attrs[0];
1158};
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160static ssize_t module_sect_show(struct module_attribute *mattr,
1161 struct module *mod, char *buf)
1162{
1163 struct module_sect_attr *sattr =
1164 container_of(mattr, struct module_sect_attr, mattr);
1165 return sprintf(buf, "0x%lx\n", sattr->address);
1166}
1167
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001168static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1169{
Rusty Russella58730c2008-03-13 09:03:44 +00001170 unsigned int section;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001171
1172 for (section = 0; section < sect_attrs->nsections; section++)
1173 kfree(sect_attrs->attrs[section].name);
1174 kfree(sect_attrs);
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177static void add_sect_attrs(struct module *mod, unsigned int nsect,
1178 char *secstrings, Elf_Shdr *sechdrs)
1179{
1180 unsigned int nloaded = 0, i, size[2];
1181 struct module_sect_attrs *sect_attrs;
1182 struct module_sect_attr *sattr;
1183 struct attribute **gattr;
Daniel Walker22a8bde2007-10-18 03:06:07 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* Count loaded sections and allocate structures */
1186 for (i = 0; i < nsect; i++)
1187 if (sechdrs[i].sh_flags & SHF_ALLOC)
1188 nloaded++;
1189 size[0] = ALIGN(sizeof(*sect_attrs)
1190 + nloaded * sizeof(sect_attrs->attrs[0]),
1191 sizeof(sect_attrs->grp.attrs[0]));
1192 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001193 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1194 if (sect_attrs == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return;
1196
1197 /* Setup section attributes. */
1198 sect_attrs->grp.name = "sections";
1199 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1200
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001201 sect_attrs->nsections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sattr = &sect_attrs->attrs[0];
1203 gattr = &sect_attrs->grp.attrs[0];
1204 for (i = 0; i < nsect; i++) {
1205 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1206 continue;
1207 sattr->address = sechdrs[i].sh_addr;
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001208 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1209 GFP_KERNEL);
1210 if (sattr->name == NULL)
1211 goto out;
1212 sect_attrs->nsections++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 sattr->mattr.show = module_sect_show;
1214 sattr->mattr.store = NULL;
1215 sattr->mattr.attr.name = sattr->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sattr->mattr.attr.mode = S_IRUGO;
1217 *(gattr++) = &(sattr++)->mattr.attr;
1218 }
1219 *gattr = NULL;
1220
1221 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1222 goto out;
1223
1224 mod->sect_attrs = sect_attrs;
1225 return;
1226 out:
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001227 free_sect_attrs(sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static void remove_sect_attrs(struct module *mod)
1231{
1232 if (mod->sect_attrs) {
1233 sysfs_remove_group(&mod->mkobj.kobj,
1234 &mod->sect_attrs->grp);
1235 /* We are positive that no one is using any sect attrs
1236 * at this point. Deallocate immediately. */
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001237 free_sect_attrs(mod->sect_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 mod->sect_attrs = NULL;
1239 }
1240}
1241
Roland McGrath6d760132007-10-16 23:26:40 -07001242/*
1243 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1244 */
1245
1246struct module_notes_attrs {
1247 struct kobject *dir;
1248 unsigned int notes;
1249 struct bin_attribute attrs[0];
1250};
1251
1252static ssize_t module_notes_read(struct kobject *kobj,
1253 struct bin_attribute *bin_attr,
1254 char *buf, loff_t pos, size_t count)
1255{
1256 /*
1257 * The caller checked the pos and count against our size.
1258 */
1259 memcpy(buf, bin_attr->private + pos, count);
1260 return count;
1261}
1262
1263static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1264 unsigned int i)
1265{
1266 if (notes_attrs->dir) {
1267 while (i-- > 0)
1268 sysfs_remove_bin_file(notes_attrs->dir,
1269 &notes_attrs->attrs[i]);
Alexey Dobriyane9432092008-09-23 23:51:11 +04001270 kobject_put(notes_attrs->dir);
Roland McGrath6d760132007-10-16 23:26:40 -07001271 }
1272 kfree(notes_attrs);
1273}
1274
1275static void add_notes_attrs(struct module *mod, unsigned int nsect,
1276 char *secstrings, Elf_Shdr *sechdrs)
1277{
1278 unsigned int notes, loaded, i;
1279 struct module_notes_attrs *notes_attrs;
1280 struct bin_attribute *nattr;
1281
1282 /* Count notes sections and allocate structures. */
1283 notes = 0;
1284 for (i = 0; i < nsect; i++)
1285 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1286 (sechdrs[i].sh_type == SHT_NOTE))
1287 ++notes;
1288
1289 if (notes == 0)
1290 return;
1291
1292 notes_attrs = kzalloc(sizeof(*notes_attrs)
1293 + notes * sizeof(notes_attrs->attrs[0]),
1294 GFP_KERNEL);
1295 if (notes_attrs == NULL)
1296 return;
1297
1298 notes_attrs->notes = notes;
1299 nattr = &notes_attrs->attrs[0];
1300 for (loaded = i = 0; i < nsect; ++i) {
1301 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1302 continue;
1303 if (sechdrs[i].sh_type == SHT_NOTE) {
1304 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1305 nattr->attr.mode = S_IRUGO;
1306 nattr->size = sechdrs[i].sh_size;
1307 nattr->private = (void *) sechdrs[i].sh_addr;
1308 nattr->read = module_notes_read;
1309 ++nattr;
1310 }
1311 ++loaded;
1312 }
1313
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001314 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
Roland McGrath6d760132007-10-16 23:26:40 -07001315 if (!notes_attrs->dir)
1316 goto out;
1317
1318 for (i = 0; i < notes; ++i)
1319 if (sysfs_create_bin_file(notes_attrs->dir,
1320 &notes_attrs->attrs[i]))
1321 goto out;
1322
1323 mod->notes_attrs = notes_attrs;
1324 return;
1325
1326 out:
1327 free_notes_attrs(notes_attrs, i);
1328}
1329
1330static void remove_notes_attrs(struct module *mod)
1331{
1332 if (mod->notes_attrs)
1333 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336#else
Ian S. Nelson04b1db92006-09-29 02:01:31 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1339 char *sectstrings, Elf_Shdr *sechdrs)
1340{
1341}
1342
1343static inline void remove_sect_attrs(struct module *mod)
1344{
1345}
Roland McGrath6d760132007-10-16 23:26:40 -07001346
1347static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1348 char *sectstrings, Elf_Shdr *sechdrs)
1349{
1350}
1351
1352static inline void remove_notes_attrs(struct module *mod)
1353{
1354}
Kay Sievers120fc3d2008-02-21 00:33:20 +01001355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Randy Dunlapef665c12007-02-13 15:19:06 -08001357#ifdef CONFIG_SYSFS
1358int module_add_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001359{
1360 struct module_attribute *attr;
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001361 struct module_attribute *temp_attr;
Matt Domschc988d2b2005-06-23 22:05:15 -07001362 int error = 0;
1363 int i;
1364
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001365 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1366 (ARRAY_SIZE(modinfo_attrs) + 1)),
1367 GFP_KERNEL);
1368 if (!mod->modinfo_attrs)
1369 return -ENOMEM;
1370
1371 temp_attr = mod->modinfo_attrs;
Matt Domschc988d2b2005-06-23 22:05:15 -07001372 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1373 if (!attr->test ||
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001374 (attr->test && attr->test(mod))) {
1375 memcpy(temp_attr, attr, sizeof(*temp_attr));
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001376 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1377 ++temp_attr;
1378 }
Matt Domschc988d2b2005-06-23 22:05:15 -07001379 }
1380 return error;
1381}
1382
Randy Dunlapef665c12007-02-13 15:19:06 -08001383void module_remove_modinfo_attrs(struct module *mod)
Matt Domschc988d2b2005-06-23 22:05:15 -07001384{
1385 struct module_attribute *attr;
1386 int i;
1387
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001388 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1389 /* pick a field to test for end of list */
1390 if (!attr->attr.name)
1391 break;
Matt Domschc988d2b2005-06-23 22:05:15 -07001392 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001393 if (attr->free)
1394 attr->free(mod);
Matt Domschc988d2b2005-06-23 22:05:15 -07001395 }
Greg Kroah-Hartman03e88ae12006-02-16 13:50:23 -08001396 kfree(mod->modinfo_attrs);
Matt Domschc988d2b2005-06-23 22:05:15 -07001397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Randy Dunlapef665c12007-02-13 15:19:06 -08001399int mod_sysfs_init(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 int err;
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001402 struct kobject *kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -07001404 if (!module_sysfs_initialized) {
1405 printk(KERN_ERR "%s: module sysfs not initialized\n",
Ed Swierk1cc5f712006-09-25 16:25:36 -07001406 mod->name);
1407 err = -EINVAL;
1408 goto out;
1409 }
Greg Kroah-Hartman6494a932008-01-27 15:38:40 -08001410
1411 kobj = kset_find_obj(module_kset, mod->name);
1412 if (kobj) {
1413 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1414 kobject_put(kobj);
1415 err = -EINVAL;
1416 goto out;
1417 }
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 mod->mkobj.mod = mod;
Kay Sieverse17e0f52006-11-24 12:15:25 +01001420
Greg Kroah-Hartmanac3c8142007-12-17 23:05:35 -07001421 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1422 mod->mkobj.kobj.kset = module_kset;
1423 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1424 "%s", mod->name);
1425 if (err)
1426 kobject_put(&mod->mkobj.kobj);
Kay Sievers270a6c42007-01-18 13:26:15 +01001427
Kay Sievers97c146e2007-11-29 23:46:11 +01001428 /* delay uevent until full sysfs population */
Kay Sievers270a6c42007-01-18 13:26:15 +01001429out:
1430 return err;
1431}
1432
Randy Dunlapef665c12007-02-13 15:19:06 -08001433int mod_sysfs_setup(struct module *mod,
Kay Sievers270a6c42007-01-18 13:26:15 +01001434 struct kernel_param *kparam,
1435 unsigned int num_params)
1436{
1437 int err;
1438
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001439 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
Akinobu Mita240936e2007-04-26 00:12:09 -07001440 if (!mod->holders_dir) {
1441 err = -ENOMEM;
Kay Sievers270a6c42007-01-18 13:26:15 +01001442 goto out_unreg;
Akinobu Mita240936e2007-04-26 00:12:09 -07001443 }
Kay Sievers270a6c42007-01-18 13:26:15 +01001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 err = module_param_sysfs_setup(mod, kparam, num_params);
1446 if (err)
Kay Sievers270a6c42007-01-18 13:26:15 +01001447 goto out_unreg_holders;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Matt Domschc988d2b2005-06-23 22:05:15 -07001449 err = module_add_modinfo_attrs(mod);
1450 if (err)
Kay Sieverse17e0f52006-11-24 12:15:25 +01001451 goto out_unreg_param;
Matt Domschc988d2b2005-06-23 22:05:15 -07001452
Kay Sieverse17e0f52006-11-24 12:15:25 +01001453 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455
Kay Sieverse17e0f52006-11-24 12:15:25 +01001456out_unreg_param:
1457 module_param_sysfs_remove(mod);
Kay Sievers270a6c42007-01-18 13:26:15 +01001458out_unreg_holders:
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001459 kobject_put(mod->holders_dir);
Kay Sievers270a6c42007-01-18 13:26:15 +01001460out_unreg:
Kay Sieverse17e0f52006-11-24 12:15:25 +01001461 kobject_put(&mod->mkobj.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return err;
1463}
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001464
1465static void mod_sysfs_fini(struct module *mod)
1466{
1467 kobject_put(&mod->mkobj.kobj);
1468}
1469
1470#else /* CONFIG_SYSFS */
1471
1472static void mod_sysfs_fini(struct module *mod)
1473{
1474}
1475
1476#endif /* CONFIG_SYSFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478static void mod_kobject_remove(struct module *mod)
1479{
Matt Domschc988d2b2005-06-23 22:05:15 -07001480 module_remove_modinfo_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 module_param_sysfs_remove(mod);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -08001482 kobject_put(mod->mkobj.drivers_dir);
1483 kobject_put(mod->holders_dir);
Denis V. Lunev34e4e2f2008-05-20 13:59:48 +04001484 mod_sysfs_fini(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/*
1488 * unlink the module with the whole machine is stopped with interrupts off
1489 * - this defends against kallsyms not taking locks
1490 */
1491static int __unlink_module(void *_mod)
1492{
1493 struct module *mod = _mod;
1494 list_del(&mod->list);
1495 return 0;
1496}
1497
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001498/* Free a module, remove from lists, etc (must hold module_mutex). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499static void free_module(struct module *mod)
1500{
Li Zefan7ead8b82009-08-17 16:56:28 +08001501 trace_module_free(mod);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* Delete from various lists */
Rusty Russell9b1a4d32008-07-28 12:16:30 -05001504 stop_machine(__unlink_module, mod, NULL);
Roland McGrath6d760132007-10-16 23:26:40 -07001505 remove_notes_attrs(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 remove_sect_attrs(mod);
1507 mod_kobject_remove(mod);
1508
1509 /* Arch-specific cleanup. */
1510 module_arch_cleanup(mod);
1511
1512 /* Module unload stuff */
1513 module_unload_free(mod);
1514
Rusty Russelle180a6b2009-03-31 13:05:29 -06001515 /* Free any allocated parameters. */
1516 destroy_params(mod->kp, mod->num_kp);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 /* This may be NULL, but that's OK */
1519 module_free(mod, mod->module_init);
1520 kfree(mod->args);
1521 if (mod->percpu)
1522 percpu_modfree(mod->percpu);
Eric Dumazet720eba32009-02-03 13:31:36 +10301523#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1524 if (mod->refptr)
1525 percpu_modfree(mod->refptr);
1526#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001527 /* Free lock-classes: */
1528 lockdep_free_key_range(mod->module_core, mod->core_size);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* Finally, free the core (containing the module structure) */
1531 module_free(mod, mod->module_core);
1532}
1533
1534void *__symbol_get(const char *symbol)
1535{
1536 struct module *owner;
Tim Abbott414fd312008-12-05 19:03:56 -05001537 const struct kernel_symbol *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Rusty Russell24da1cb2007-07-15 23:41:46 -07001539 preempt_disable();
Tim Abbott414fd312008-12-05 19:03:56 -05001540 sym = find_symbol(symbol, &owner, NULL, true, true);
1541 if (sym && strong_try_module_get(owner))
1542 sym = NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07001543 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Tim Abbott414fd312008-12-05 19:03:56 -05001545 return sym ? (void *)sym->value : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547EXPORT_SYMBOL_GPL(__symbol_get);
1548
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001549/*
1550 * Ensure that an exported symbol [global namespace] does not already exist
Robert P. J. Day02a3e592007-05-09 07:26:28 +02001551 * in the kernel or in some other module's exported symbol table.
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001552 */
1553static int verify_export_symbols(struct module *mod)
1554{
Rusty Russellb2111042008-05-01 21:15:00 -05001555 unsigned int i;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001556 struct module *owner;
Rusty Russellb2111042008-05-01 21:15:00 -05001557 const struct kernel_symbol *s;
1558 struct {
1559 const struct kernel_symbol *sym;
1560 unsigned int num;
1561 } arr[] = {
1562 { mod->syms, mod->num_syms },
1563 { mod->gpl_syms, mod->num_gpl_syms },
1564 { mod->gpl_future_syms, mod->num_gpl_future_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001565#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russellb2111042008-05-01 21:15:00 -05001566 { mod->unused_syms, mod->num_unused_syms },
1567 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05001568#endif
Rusty Russellb2111042008-05-01 21:15:00 -05001569 };
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001570
Rusty Russellb2111042008-05-01 21:15:00 -05001571 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1572 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
Tim Abbott414fd312008-12-05 19:03:56 -05001573 if (find_symbol(s->name, &owner, NULL, true, false)) {
Rusty Russellb2111042008-05-01 21:15:00 -05001574 printk(KERN_ERR
1575 "%s: exports duplicate symbol %s"
1576 " (owned by %s)\n",
1577 mod->name, s->name, module_name(owner));
1578 return -ENOEXEC;
1579 }
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001580 }
Rusty Russellb2111042008-05-01 21:15:00 -05001581 }
1582 return 0;
Ashutosh Naikeea8b542006-01-08 01:04:25 -08001583}
1584
Matti Linnanvuori9a4b9702007-11-08 08:37:38 -08001585/* Change all symbols so that st_value encodes the pointer directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static int simplify_symbols(Elf_Shdr *sechdrs,
1587 unsigned int symindex,
1588 const char *strtab,
1589 unsigned int versindex,
1590 unsigned int pcpuindex,
1591 struct module *mod)
1592{
1593 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1594 unsigned long secbase;
1595 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1596 int ret = 0;
Tim Abbott414fd312008-12-05 19:03:56 -05001597 const struct kernel_symbol *ksym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 for (i = 1; i < n; i++) {
1600 switch (sym[i].st_shndx) {
1601 case SHN_COMMON:
1602 /* We compiled with -fno-common. These are not
1603 supposed to happen. */
1604 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1605 printk("%s: please compile with -fno-common\n",
1606 mod->name);
1607 ret = -ENOEXEC;
1608 break;
1609
1610 case SHN_ABS:
1611 /* Don't need to do anything */
1612 DEBUGP("Absolute symbol: 0x%08lx\n",
1613 (long)sym[i].st_value);
1614 break;
1615
1616 case SHN_UNDEF:
Tim Abbott414fd312008-12-05 19:03:56 -05001617 ksym = resolve_symbol(sechdrs, versindex,
1618 strtab + sym[i].st_name, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /* Ok if resolved. */
Tim Abbott414fd312008-12-05 19:03:56 -05001620 if (ksym) {
1621 sym[i].st_value = ksym->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 break;
Tim Abbott414fd312008-12-05 19:03:56 -05001623 }
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 /* Ok if weak. */
1626 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1627 break;
1628
1629 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1630 mod->name, strtab + sym[i].st_name);
1631 ret = -ENOENT;
1632 break;
1633
1634 default:
1635 /* Divert to percpu allocation if a percpu var. */
1636 if (sym[i].st_shndx == pcpuindex)
1637 secbase = (unsigned long)mod->percpu;
1638 else
1639 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1640 sym[i].st_value += secbase;
1641 break;
1642 }
1643 }
1644
1645 return ret;
1646}
1647
Helge Deller088af9a2008-12-31 12:31:18 +01001648/* Additional bytes needed by arch in front of individual sections */
1649unsigned int __weak arch_mod_section_prepend(struct module *mod,
1650 unsigned int section)
1651{
1652 /* default implementation just returns zero */
1653 return 0;
1654}
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656/* Update size with this section: return offset. */
Helge Deller088af9a2008-12-31 12:31:18 +01001657static long get_offset(struct module *mod, unsigned int *size,
1658 Elf_Shdr *sechdr, unsigned int section)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 long ret;
1661
Helge Deller088af9a2008-12-31 12:31:18 +01001662 *size += arch_mod_section_prepend(mod, section);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1664 *size = ret + sechdr->sh_size;
1665 return ret;
1666}
1667
1668/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1669 might -- code, read-only data, read-write data, small data. Tally
1670 sizes, and place the offsets into sh_entsize fields: high bit means it
1671 belongs in init. */
1672static void layout_sections(struct module *mod,
1673 const Elf_Ehdr *hdr,
1674 Elf_Shdr *sechdrs,
1675 const char *secstrings)
1676{
1677 static unsigned long const masks[][2] = {
1678 /* NOTE: all executable code must be the first section
1679 * in this array; otherwise modify the text_size
1680 * finder in the two loops below */
1681 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1682 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1683 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1684 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1685 };
1686 unsigned int m, i;
1687
1688 for (i = 0; i < hdr->e_shnum; i++)
1689 sechdrs[i].sh_entsize = ~0UL;
1690
1691 DEBUGP("Core section allocation order:\n");
1692 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1693 for (i = 0; i < hdr->e_shnum; ++i) {
1694 Elf_Shdr *s = &sechdrs[i];
1695
1696 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1697 || (s->sh_flags & masks[m][1])
1698 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001699 || strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001701 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 DEBUGP("\t%s\n", secstrings + s->sh_name);
1703 }
1704 if (m == 0)
1705 mod->core_text_size = mod->core_size;
1706 }
1707
1708 DEBUGP("Init section allocation order:\n");
1709 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1710 for (i = 0; i < hdr->e_shnum; ++i) {
1711 Elf_Shdr *s = &sechdrs[i];
1712
1713 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1714 || (s->sh_flags & masks[m][1])
1715 || s->sh_entsize != ~0UL
Rusty Russell49502672009-03-31 13:05:36 -06001716 || !strstarts(secstrings + s->sh_name, ".init"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 continue;
Helge Deller088af9a2008-12-31 12:31:18 +01001718 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 | INIT_OFFSET_MASK);
1720 DEBUGP("\t%s\n", secstrings + s->sh_name);
1721 }
1722 if (m == 0)
1723 mod->init_text_size = mod->init_size;
1724 }
1725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static void set_license(struct module *mod, const char *license)
1728{
1729 if (!license)
1730 license = "unspecified";
1731
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001732 if (!license_is_gpl_compatible(license)) {
Andi Kleen25ddbb12008-10-15 22:01:41 -07001733 if (!test_taint(TAINT_PROPRIETARY_MODULE))
Jan Dittmer1d4d2622006-10-28 10:38:38 -07001734 printk(KERN_WARNING "%s: module license '%s' taints "
Florin Malitafa3ba2e82006-10-11 01:21:48 -07001735 "kernel.\n", mod->name, license);
1736 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738}
1739
1740/* Parse tag=value strings from .modinfo section */
1741static char *next_string(char *string, unsigned long *secsize)
1742{
1743 /* Skip non-zero chars */
1744 while (string[0]) {
1745 string++;
1746 if ((*secsize)-- <= 1)
1747 return NULL;
1748 }
1749
1750 /* Skip any zero padding. */
1751 while (!string[0]) {
1752 string++;
1753 if ((*secsize)-- <= 1)
1754 return NULL;
1755 }
1756 return string;
1757}
1758
1759static char *get_modinfo(Elf_Shdr *sechdrs,
1760 unsigned int info,
1761 const char *tag)
1762{
1763 char *p;
1764 unsigned int taglen = strlen(tag);
1765 unsigned long size = sechdrs[info].sh_size;
1766
1767 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1768 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1769 return p + taglen + 1;
1770 }
1771 return NULL;
1772}
1773
Matt Domschc988d2b2005-06-23 22:05:15 -07001774static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1775 unsigned int infoindex)
1776{
1777 struct module_attribute *attr;
1778 int i;
1779
1780 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1781 if (attr->setup)
1782 attr->setup(mod,
1783 get_modinfo(sechdrs,
1784 infoindex,
1785 attr->attr.name));
1786 }
1787}
Matt Domschc988d2b2005-06-23 22:05:15 -07001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#ifdef CONFIG_KALLSYMS
WANG Cong15bba372008-07-24 15:41:48 +01001790
1791/* lookup symbol in given range of kernel_symbols */
1792static const struct kernel_symbol *lookup_symbol(const char *name,
1793 const struct kernel_symbol *start,
1794 const struct kernel_symbol *stop)
1795{
1796 const struct kernel_symbol *ks = start;
1797 for (; ks < stop; ks++)
1798 if (strcmp(ks->name, name) == 0)
1799 return ks;
1800 return NULL;
1801}
1802
Tim Abbottca4787b2009-01-05 08:40:10 -06001803static int is_exported(const char *name, unsigned long value,
1804 const struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Tim Abbottca4787b2009-01-05 08:40:10 -06001806 const struct kernel_symbol *ks;
1807 if (!mod)
1808 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
Sam Ravnborg3fd68052006-02-08 21:16:45 +01001809 else
Tim Abbottca4787b2009-01-05 08:40:10 -06001810 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1811 return ks != NULL && ks->value == value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814/* As per nm */
1815static char elf_type(const Elf_Sym *sym,
1816 Elf_Shdr *sechdrs,
1817 const char *secstrings,
1818 struct module *mod)
1819{
1820 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1821 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1822 return 'v';
1823 else
1824 return 'w';
1825 }
1826 if (sym->st_shndx == SHN_UNDEF)
1827 return 'U';
1828 if (sym->st_shndx == SHN_ABS)
1829 return 'a';
1830 if (sym->st_shndx >= SHN_LORESERVE)
1831 return '?';
1832 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1833 return 't';
1834 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1835 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1836 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1837 return 'r';
1838 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1839 return 'g';
1840 else
1841 return 'd';
1842 }
1843 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1844 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1845 return 's';
1846 else
1847 return 'b';
1848 }
Rusty Russell49502672009-03-31 13:05:36 -06001849 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return 'n';
1851 return '?';
1852}
1853
1854static void add_kallsyms(struct module *mod,
1855 Elf_Shdr *sechdrs,
1856 unsigned int symindex,
1857 unsigned int strindex,
1858 const char *secstrings)
1859{
1860 unsigned int i;
1861
1862 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1863 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1864 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1865
1866 /* Set types up while we still have access to sections. */
1867 for (i = 0; i < mod->num_symtab; i++)
1868 mod->symtab[i].st_info
1869 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1870}
1871#else
1872static inline void add_kallsyms(struct module *mod,
1873 Elf_Shdr *sechdrs,
1874 unsigned int symindex,
1875 unsigned int strindex,
1876 const char *secstrings)
1877{
1878}
1879#endif /* CONFIG_KALLSYMS */
1880
Jason Barone9d376f2009-02-05 11:51:38 -05001881static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
Rusty Russell5e458cc2008-10-22 10:00:13 -05001882{
Jason Barone9d376f2009-02-05 11:51:38 -05001883#ifdef CONFIG_DYNAMIC_DEBUG
1884 if (ddebug_add_module(debug, num, debug->modname))
1885 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1886 debug->modname);
1887#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05001888}
Jason Baron346e15b2008-08-12 16:46:19 -04001889
Rusty Russell3a642e92008-07-22 19:24:28 -05001890static void *module_alloc_update_bounds(unsigned long size)
1891{
1892 void *ret = module_alloc(size);
1893
1894 if (ret) {
1895 /* Update module bounds. */
1896 if ((unsigned long)ret < module_addr_min)
1897 module_addr_min = (unsigned long)ret;
1898 if ((unsigned long)ret + size > module_addr_max)
1899 module_addr_max = (unsigned long)ret + size;
1900 }
1901 return ret;
1902}
1903
Catalin Marinas4f2294b2009-06-11 13:23:20 +01001904#ifdef CONFIG_DEBUG_KMEMLEAK
1905static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1906 Elf_Shdr *sechdrs, char *secstrings)
1907{
1908 unsigned int i;
1909
1910 /* only scan the sections containing data */
1911 kmemleak_scan_area(mod->module_core, (unsigned long)mod -
1912 (unsigned long)mod->module_core,
1913 sizeof(struct module), GFP_KERNEL);
1914
1915 for (i = 1; i < hdr->e_shnum; i++) {
1916 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1917 continue;
1918 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1919 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1920 continue;
1921
1922 kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr -
1923 (unsigned long)mod->module_core,
1924 sechdrs[i].sh_size, GFP_KERNEL);
1925 }
1926}
1927#else
1928static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1929 Elf_Shdr *sechdrs, char *secstrings)
1930{
1931}
1932#endif
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/* Allocate and load the module: note that size of section 0 is always
1935 zero, and we rely on this for optional sections. */
Linus Torvaldsffb4ba72008-08-25 11:10:26 -07001936static noinline struct module *load_module(void __user *umod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 unsigned long len,
1938 const char __user *uargs)
1939{
1940 Elf_Ehdr *hdr;
1941 Elf_Shdr *sechdrs;
1942 char *secstrings, *args, *modmagic, *strtab = NULL;
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07001943 char *staging;
Andrew Morton84860f92006-06-28 04:26:46 -07001944 unsigned int i;
1945 unsigned int symindex = 0;
1946 unsigned int strindex = 0;
Rusty Russell5e458cc2008-10-22 10:00:13 -05001947 unsigned int modindex, versindex, infoindex, pcpuindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct module *mod;
1949 long err = 0;
1950 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
Thomas Koeller378bac82005-09-06 15:17:11 -07001951 mm_segment_t old_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1954 umod, len, uargs);
1955 if (len < sizeof(*hdr))
1956 return ERR_PTR(-ENOEXEC);
1957
1958 /* Suck in entire file: we'll want most of it. */
1959 /* vmalloc barfs on "unusual" numbers. Check here */
1960 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1961 return ERR_PTR(-ENOMEM);
Heiko Carstens9e018922008-12-22 12:36:31 +01001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (copy_from_user(hdr, umod, len) != 0) {
1964 err = -EFAULT;
1965 goto free_hdr;
1966 }
1967
1968 /* Sanity checks against insmoding binaries or wrong arch,
1969 weird elf version */
Cyrill Gorcunovc4ea6fc2008-05-14 16:27:29 -07001970 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 || hdr->e_type != ET_REL
1972 || !elf_check_arch(hdr)
1973 || hdr->e_shentsize != sizeof(*sechdrs)) {
1974 err = -ENOEXEC;
1975 goto free_hdr;
1976 }
1977
1978 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1979 goto truncated;
1980
1981 /* Convenience variables */
1982 sechdrs = (void *)hdr + hdr->e_shoff;
1983 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1984 sechdrs[0].sh_addr = 0;
1985
1986 for (i = 1; i < hdr->e_shnum; i++) {
1987 if (sechdrs[i].sh_type != SHT_NOBITS
1988 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1989 goto truncated;
1990
1991 /* Mark all sections sh_addr with their address in the
1992 temporary image. */
1993 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1994
1995 /* Internal symbols and strings. */
1996 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1997 symindex = i;
1998 strindex = sechdrs[i].sh_link;
1999 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2000 }
2001#ifndef CONFIG_MODULE_UNLOAD
2002 /* Don't load .exit sections */
Rusty Russell49502672009-03-31 13:05:36 -06002003 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2005#endif
2006 }
2007
2008 modindex = find_sec(hdr, sechdrs, secstrings,
2009 ".gnu.linkonce.this_module");
2010 if (!modindex) {
2011 printk(KERN_WARNING "No module found in object\n");
2012 err = -ENOEXEC;
2013 goto free_hdr;
2014 }
Rusty Russell5e458cc2008-10-22 10:00:13 -05002015 /* This is temporary: point mod into copy of data. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 mod = (void *)sechdrs[modindex].sh_addr;
2017
2018 if (symindex == 0) {
2019 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2020 mod->name);
2021 err = -ENOEXEC;
2022 goto free_hdr;
2023 }
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2026 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2027 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2028
Rusty Russellea01e792008-03-13 09:02:17 +00002029 /* Don't keep modinfo and version sections. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Rusty Russellea01e792008-03-13 09:02:17 +00002031 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#ifdef CONFIG_KALLSYMS
2033 /* Keep symbol and string tables for decoding later. */
2034 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2035 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2036#endif
2037
2038 /* Check module struct version now, before we try to use module. */
2039 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2040 err = -ENOEXEC;
2041 goto free_hdr;
2042 }
2043
2044 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2045 /* This is allowed: modprobe --force will invalidate it. */
2046 if (!modmagic) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002047 err = try_to_force_load(mod, "bad vermagic");
Linus Torvalds826e4502008-05-04 17:04:16 -07002048 if (err)
2049 goto free_hdr;
Rusty Russell91e37a72008-05-09 16:25:28 +10002050 } else if (!same_magic(modmagic, vermagic, versindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2052 mod->name, modmagic, vermagic);
2053 err = -ENOEXEC;
2054 goto free_hdr;
2055 }
2056
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002057 staging = get_modinfo(sechdrs, infoindex, "staging");
2058 if (staging) {
2059 add_taint_module(mod, TAINT_CRAP);
2060 printk(KERN_WARNING "%s: module is from the staging directory,"
2061 " the quality is unknown, you have been warned.\n",
2062 mod->name);
2063 }
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* Now copy in args */
Davi Arnaut24277dd2006-03-24 03:18:43 -08002066 args = strndup_user(uargs, ~0UL >> 1);
2067 if (IS_ERR(args)) {
2068 err = PTR_ERR(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 goto free_hdr;
2070 }
Andrew Morton8e08b752006-02-07 12:58:45 -08002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (find_module(mod->name)) {
2073 err = -EEXIST;
2074 goto free_mod;
2075 }
2076
2077 mod->state = MODULE_STATE_COMING;
2078
2079 /* Allow arches to frob section contents and sizes. */
2080 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2081 if (err < 0)
2082 goto free_mod;
2083
2084 if (pcpuindex) {
2085 /* We have a special allocation for this section. */
2086 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
Rusty Russell842bbaa2005-08-01 21:11:47 -07002087 sechdrs[pcpuindex].sh_addralign,
2088 mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (!percpu) {
2090 err = -ENOMEM;
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002091 goto free_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2094 mod->percpu = percpu;
2095 }
2096
2097 /* Determine total sizes, and put offsets in sh_entsize. For now
2098 this is done generically; there doesn't appear to be any
2099 special cases for the architectures. */
2100 layout_sections(mod, hdr, sechdrs, secstrings);
2101
2102 /* Do the allocs. */
Rusty Russell3a642e92008-07-22 19:24:28 -05002103 ptr = module_alloc_update_bounds(mod->core_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002104 /*
2105 * The pointer to this block is stored in the module structure
2106 * which is inside the block. Just mark it as not being a
2107 * leak.
2108 */
2109 kmemleak_not_leak(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (!ptr) {
2111 err = -ENOMEM;
2112 goto free_percpu;
2113 }
2114 memset(ptr, 0, mod->core_size);
2115 mod->module_core = ptr;
2116
Rusty Russell3a642e92008-07-22 19:24:28 -05002117 ptr = module_alloc_update_bounds(mod->init_size);
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002118 /*
2119 * The pointer to this block is stored in the module structure
2120 * which is inside the block. This block doesn't need to be
2121 * scanned as it contains data and code that will be freed
2122 * after the module is initialized.
2123 */
2124 kmemleak_ignore(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (!ptr && mod->init_size) {
2126 err = -ENOMEM;
2127 goto free_core;
2128 }
2129 memset(ptr, 0, mod->init_size);
2130 mod->module_init = ptr;
2131
2132 /* Transfer each section which specifies SHF_ALLOC */
2133 DEBUGP("final section addresses:\n");
2134 for (i = 0; i < hdr->e_shnum; i++) {
2135 void *dest;
2136
2137 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2138 continue;
2139
2140 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2141 dest = mod->module_init
2142 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2143 else
2144 dest = mod->module_core + sechdrs[i].sh_entsize;
2145
2146 if (sechdrs[i].sh_type != SHT_NOBITS)
2147 memcpy(dest, (void *)sechdrs[i].sh_addr,
2148 sechdrs[i].sh_size);
2149 /* Update sh_addr to point to copy in image. */
2150 sechdrs[i].sh_addr = (unsigned long)dest;
2151 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2152 }
2153 /* Module has been moved. */
2154 mod = (void *)sechdrs[modindex].sh_addr;
Catalin Marinas4f2294b2009-06-11 13:23:20 +01002155 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002157#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2158 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2159 mod->name);
2160 if (!mod->refptr) {
2161 err = -ENOMEM;
2162 goto free_init;
2163 }
2164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* Now we've moved module, initialize linked lists, etc. */
2166 module_unload_init(mod);
2167
Kay Sievers97c146e2007-11-29 23:46:11 +01002168 /* add kobject, so we can reference it. */
Akinobu Mitad58ae672007-10-16 23:30:27 -07002169 err = mod_sysfs_init(mod);
2170 if (err)
Kay Sievers97c146e2007-11-29 23:46:11 +01002171 goto free_unload;
Kay Sievers270a6c42007-01-18 13:26:15 +01002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* Set up license info based on the info section */
2174 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2175
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002176 /*
2177 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2178 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2179 * using GPL-only symbols it needs.
2180 */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002181 if (strcmp(mod->name, "ndiswrapper") == 0)
Pavel Roskin9b37ccf2008-02-28 17:11:02 -05002182 add_taint(TAINT_PROPRIETARY_MODULE);
2183
2184 /* driverloader was caught wrongly pretending to be under GPL */
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002185 if (strcmp(mod->name, "driverloader") == 0)
2186 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
Dave Jones9841d61d2006-01-08 01:03:41 -08002187
Matt Domschc988d2b2005-06-23 22:05:15 -07002188 /* Set up MODINFO_ATTR fields */
2189 setup_modinfo(mod, sechdrs, infoindex);
Matt Domschc988d2b2005-06-23 22:05:15 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* Fix up syms, so that st_value is a pointer to location. */
2192 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2193 mod);
2194 if (err < 0)
2195 goto cleanup;
2196
Rusty Russell5e458cc2008-10-22 10:00:13 -05002197 /* Now we've got everything in the final locations, we can
2198 * find optional sections. */
Rusty Russelle180a6b2009-03-31 13:05:29 -06002199 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2200 sizeof(*mod->kp), &mod->num_kp);
Rusty Russell5e458cc2008-10-22 10:00:13 -05002201 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2202 sizeof(*mod->syms), &mod->num_syms);
2203 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2204 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2205 sizeof(*mod->gpl_syms),
2206 &mod->num_gpl_syms);
2207 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2208 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2209 "__ksymtab_gpl_future",
2210 sizeof(*mod->gpl_future_syms),
2211 &mod->num_gpl_future_syms);
2212 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2213 "__kcrctab_gpl_future");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002215#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002216 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2217 "__ksymtab_unused",
2218 sizeof(*mod->unused_syms),
2219 &mod->num_unused_syms);
2220 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2221 "__kcrctab_unused");
2222 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2223 "__ksymtab_unused_gpl",
2224 sizeof(*mod->unused_gpl_syms),
2225 &mod->num_unused_gpl_syms);
2226 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2227 "__kcrctab_unused_gpl");
2228#endif
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002229#ifdef CONFIG_CONSTRUCTORS
2230 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2231 sizeof(*mod->ctors), &mod->num_ctors);
2232#endif
Rusty Russell5e458cc2008-10-22 10:00:13 -05002233
2234#ifdef CONFIG_MARKERS
2235 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2236 sizeof(*mod->markers), &mod->num_markers);
2237#endif
2238#ifdef CONFIG_TRACEPOINTS
2239 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2240 "__tracepoints",
2241 sizeof(*mod->tracepoints),
2242 &mod->num_tracepoints);
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002243#endif
Steven Rostedt6d723732009-04-10 14:53:50 -04002244#ifdef CONFIG_EVENT_TRACING
2245 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2246 "_ftrace_events",
2247 sizeof(*mod->trace_events),
2248 &mod->num_trace_events);
2249#endif
Steven Rostedt93eb6772009-04-15 13:24:06 -04002250#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2251 /* sechdrs[0].sh_size is always zero */
2252 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2253 "__mcount_loc",
2254 sizeof(*mod->ftrace_callsites),
2255 &mod->num_ftrace_callsites);
2256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257#ifdef CONFIG_MODVERSIONS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002258 if ((mod->num_syms && !mod->crcs)
2259 || (mod->num_gpl_syms && !mod->gpl_crcs)
2260 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002261#ifdef CONFIG_UNUSED_SYMBOLS
Rusty Russell5e458cc2008-10-22 10:00:13 -05002262 || (mod->num_unused_syms && !mod->unused_crcs)
2263 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
Denys Vlasenkof7f5b672008-07-22 19:24:26 -05002264#endif
2265 ) {
Rusty Russellc6e665c2009-03-31 13:05:33 -06002266 err = try_to_force_load(mod,
2267 "no versions for exported symbols");
Linus Torvalds826e4502008-05-04 17:04:16 -07002268 if (err)
2269 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271#endif
2272
2273 /* Now do relocations. */
2274 for (i = 1; i < hdr->e_shnum; i++) {
2275 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2276 unsigned int info = sechdrs[i].sh_info;
2277
2278 /* Not a valid relocation section? */
2279 if (info >= hdr->e_shnum)
2280 continue;
2281
2282 /* Don't bother with non-allocated sections */
2283 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2284 continue;
2285
2286 if (sechdrs[i].sh_type == SHT_REL)
2287 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2288 else if (sechdrs[i].sh_type == SHT_RELA)
2289 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2290 mod);
2291 if (err < 0)
2292 goto cleanup;
2293 }
2294
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002295 /* Find duplicate symbols */
2296 err = verify_export_symbols(mod);
Ashutosh Naikeea8b542006-01-08 01:04:25 -08002297 if (err < 0)
2298 goto cleanup;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* Set up and sort exception table */
Rusty Russell5e458cc2008-10-22 10:00:13 -05002301 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2302 sizeof(*mod->extable), &mod->num_exentries);
2303 sort_extable(mod->extable, mod->extable + mod->num_exentries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* Finally, copy percpu area over. */
2306 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2307 sechdrs[pcpuindex].sh_size);
2308
2309 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2310
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002311 if (!mod->taints) {
Jason Barone9d376f2009-02-05 11:51:38 -05002312 struct _ddebug *debug;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002313 unsigned int num_debug;
2314
Rusty Russell5e458cc2008-10-22 10:00:13 -05002315 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2316 sizeof(*debug), &num_debug);
Jason Barone9d376f2009-02-05 11:51:38 -05002317 if (debug)
2318 dynamic_debug_setup(debug, num_debug);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002319 }
Steven Rostedt90d595f2008-08-14 15:45:09 -04002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 err = module_finalize(hdr, sechdrs, mod);
2322 if (err < 0)
2323 goto cleanup;
2324
Thomas Koeller378bac82005-09-06 15:17:11 -07002325 /* flush the icache in correct context */
2326 old_fs = get_fs();
2327 set_fs(KERNEL_DS);
2328
2329 /*
2330 * Flush the instruction cache, since we've played with text.
2331 * Do it before processing of module parameters, so the module
2332 * can provide parameter accessor functions of its own.
2333 */
2334 if (mod->module_init)
2335 flush_icache_range((unsigned long)mod->module_init,
2336 (unsigned long)mod->module_init
2337 + mod->init_size);
2338 flush_icache_range((unsigned long)mod->module_core,
2339 (unsigned long)mod->module_core + mod->core_size);
2340
2341 set_fs(old_fs);
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 mod->args = args;
Rusty Russell5e458cc2008-10-22 10:00:13 -05002344 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
Rusty Russell8d3b33f2006-03-25 03:07:05 -08002345 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2346 mod->name);
2347
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002348 /* Now sew it into the lists so we can get lockdep and oops
Andi Kleend72b3752008-08-30 10:09:00 +02002349 * info during argument parsing. Noone should access us, since
2350 * strong_try_module_get() will fail.
2351 * lockdep/oops can run asynchronous, so use the RCU list insertion
2352 * function to insert in a way safe to concurrent readers.
2353 * The mutex protects against concurrent writers.
2354 */
2355 list_add_rcu(&mod->list, &modules);
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002356
Rusty Russelle180a6b2009-03-31 13:05:29 -06002357 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002359 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Rusty Russelle180a6b2009-03-31 13:05:29 -06002361 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (err < 0)
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002363 goto unlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Roland McGrath6d760132007-10-16 23:26:40 -07002365 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 /* Get rid of temporary copy */
2368 vfree(hdr);
2369
Li Zefan7ead8b82009-08-17 16:56:28 +08002370 trace_module_load(mod);
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 /* Done! */
2373 return mod;
2374
Rusty Russellbb9d3d52008-01-29 17:13:21 -05002375 unlink:
Rusty Russelle91defa2009-03-31 13:05:35 -06002376 /* Unlink carefully: kallsyms could be walking list. */
2377 list_del_rcu(&mod->list);
2378 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 module_arch_cleanup(mod);
2380 cleanup:
Kay Sievers97c146e2007-11-29 23:46:11 +01002381 kobject_del(&mod->mkobj.kobj);
2382 kobject_put(&mod->mkobj.kobj);
2383 free_unload:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 module_unload_free(mod);
Eric Dumazet720eba32009-02-03 13:31:36 +10302385#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
Américo Wangb10153f2009-03-25 00:07:19 +08002386 free_init:
Eric Dumazet720eba32009-02-03 13:31:36 +10302387 percpu_modfree(mod->refptr);
2388#endif
Masami Hiramatsu6e2b7572009-03-16 18:13:36 -04002389 module_free(mod, mod->module_init);
2390 free_core:
2391 module_free(mod, mod->module_core);
2392 /* mod will be freed with core. Don't access it beyond this line! */
2393 free_percpu:
2394 if (percpu)
2395 percpu_modfree(percpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 free_mod:
2397 kfree(args);
2398 free_hdr:
2399 vfree(hdr);
Jayachandran C6fe2e702006-01-06 00:19:54 -08002400 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 truncated:
2403 printk(KERN_ERR "Module len %lu truncated\n", len);
2404 err = -ENOEXEC;
2405 goto free_hdr;
2406}
2407
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002408/* Call module constructors. */
2409static void do_mod_ctors(struct module *mod)
2410{
2411#ifdef CONFIG_CONSTRUCTORS
2412 unsigned long i;
2413
2414 for (i = 0; i < mod->num_ctors; i++)
2415 mod->ctors[i]();
2416#endif
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/* This is where the real work happens */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002420SYSCALL_DEFINE3(init_module, void __user *, umod,
2421 unsigned long, len, const char __user *, uargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
2423 struct module *mod;
2424 int ret = 0;
2425
2426 /* Must have permission */
Kees Cook3d433212009-04-02 15:49:29 -07002427 if (!capable(CAP_SYS_MODULE) || modules_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return -EPERM;
2429
2430 /* Only one module load at a time, please */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002431 if (mutex_lock_interruptible(&module_mutex) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return -EINTR;
2433
2434 /* Do all the hard work */
2435 mod = load_module(umod, len, uargs);
2436 if (IS_ERR(mod)) {
Ashutosh Naik6389a382006-03-23 03:00:46 -08002437 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return PTR_ERR(mod);
2439 }
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 /* Drop lock so they can recurse */
Ashutosh Naik6389a382006-03-23 03:00:46 -08002442 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Alan Sterne041c682006-03-27 01:16:30 -08002444 blocking_notifier_call_chain(&module_notify_list,
2445 MODULE_STATE_COMING, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Peter Oberparleiterb99b87f2009-06-17 16:28:03 -07002447 do_mod_ctors(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Start the module */
2449 if (mod->init != NULL)
Arjan van de Ven59f94152008-07-30 12:49:02 -07002450 ret = do_one_initcall(mod->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (ret < 0) {
2452 /* Init routine failed: abort. Try to protect us from
2453 buggy refcounters. */
2454 mod->state = MODULE_STATE_GOING;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002455 synchronize_sched();
Rusty Russellaf49d922007-10-16 23:26:27 -07002456 module_put(mod);
Peter Oberparleiterdf4b5652008-04-21 14:34:31 +02002457 blocking_notifier_call_chain(&module_notify_list,
2458 MODULE_STATE_GOING, mod);
Rusty Russellaf49d922007-10-16 23:26:27 -07002459 mutex_lock(&module_mutex);
2460 free_module(mod);
2461 mutex_unlock(&module_mutex);
Rusty Russellc9a3ba52008-01-29 17:13:18 -05002462 wake_up(&module_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return ret;
2464 }
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002465 if (ret > 0) {
Joe Perchesad361c92009-07-06 13:05:40 -07002466 printk(KERN_WARNING
2467"%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2468"%s: loading module anyway...\n",
Alexey Dobriyane24e2e62008-03-10 11:43:53 -07002469 __func__, mod->name, ret,
2470 __func__);
2471 dump_stack();
2472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Rusty Russell6c5db222008-03-10 11:43:52 -07002474 /* Now it's a first class citizen! Wake up anyone waiting for it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 mod->state = MODULE_STATE_LIVE;
Rusty Russell6c5db222008-03-10 11:43:52 -07002476 wake_up(&module_wq);
Masami Hiramatsu0deddf432009-01-06 14:41:54 -08002477 blocking_notifier_call_chain(&module_notify_list,
2478 MODULE_STATE_LIVE, mod);
Rusty Russell6c5db222008-03-10 11:43:52 -07002479
Linus Torvaldsd6de2c82009-04-10 12:17:41 -07002480 /* We need to finish all async code before the module init sequence is done */
2481 async_synchronize_full();
2482
Rusty Russell6c5db222008-03-10 11:43:52 -07002483 mutex_lock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /* Drop initial reference. */
2485 module_put(mod);
Rusty Russellad6561d2009-06-12 21:47:03 -06002486 trim_init_extable(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 module_free(mod, mod->module_init);
2488 mod->module_init = NULL;
2489 mod->init_size = 0;
2490 mod->init_text_size = 0;
Ashutosh Naik6389a382006-03-23 03:00:46 -08002491 mutex_unlock(&module_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 return 0;
2494}
2495
2496static inline int within(unsigned long addr, void *start, unsigned long size)
2497{
2498 return ((void *)addr >= start && (void *)addr < start + size);
2499}
2500
2501#ifdef CONFIG_KALLSYMS
2502/*
2503 * This ignores the intensely annoying "mapping symbols" found
2504 * in ARM ELF files: $a, $t and $d.
2505 */
2506static inline int is_arm_mapping_symbol(const char *str)
2507{
Daniel Walker22a8bde2007-10-18 03:06:07 -07002508 return str[0] == '$' && strchr("atd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 && (str[2] == '\0' || str[2] == '.');
2510}
2511
2512static const char *get_ksymbol(struct module *mod,
2513 unsigned long addr,
2514 unsigned long *size,
2515 unsigned long *offset)
2516{
2517 unsigned int i, best = 0;
2518 unsigned long nextval;
2519
2520 /* At worse, next value is at end of module */
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002521 if (within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 nextval = (unsigned long)mod->module_init+mod->init_text_size;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002523 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2525
2526 /* Scan for closest preceeding symbol, and next symbol. (ELF
Daniel Walker22a8bde2007-10-18 03:06:07 -07002527 starts real symbols at 1). */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 for (i = 1; i < mod->num_symtab; i++) {
2529 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2530 continue;
2531
2532 /* We ignore unnamed symbols: they're uninformative
2533 * and inserted at a whim. */
2534 if (mod->symtab[i].st_value <= addr
2535 && mod->symtab[i].st_value > mod->symtab[best].st_value
2536 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2537 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2538 best = i;
2539 if (mod->symtab[i].st_value > addr
2540 && mod->symtab[i].st_value < nextval
2541 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2542 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2543 nextval = mod->symtab[i].st_value;
2544 }
2545
2546 if (!best)
2547 return NULL;
2548
Alexey Dobriyanffb45122007-05-08 00:28:41 -07002549 if (size)
2550 *size = nextval - mod->symtab[best].st_value;
2551 if (offset)
2552 *offset = addr - mod->symtab[best].st_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return mod->strtab + mod->symtab[best].st_name;
2554}
2555
Rusty Russell6dd06c92008-01-29 17:13:22 -05002556/* For kallsyms to ask for address resolution. NULL means not found. Careful
2557 * not to lock to avoid deadlock on oopses, simply disable preemption. */
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002558const char *module_address_lookup(unsigned long addr,
Rusty Russell6dd06c92008-01-29 17:13:22 -05002559 unsigned long *size,
2560 unsigned long *offset,
2561 char **modname,
2562 char *namebuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 struct module *mod;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002565 const char *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Rusty Russellcb2a5202008-01-14 00:55:03 -08002567 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002568 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002569 if (within_module_init(addr, mod) ||
2570 within_module_core(addr, mod)) {
Franck Bui-Huuffc50892006-10-03 01:13:48 -07002571 if (modname)
2572 *modname = mod->name;
Rusty Russellcb2a5202008-01-14 00:55:03 -08002573 ret = get_ksymbol(mod, addr, size, offset);
2574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576 }
Rusty Russell6dd06c92008-01-29 17:13:22 -05002577 /* Make a copy in here where it's safe */
2578 if (ret) {
2579 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2580 ret = namebuf;
2581 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002582 preempt_enable();
Andrew Morton92dfc9d2008-02-08 04:18:43 -08002583 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002586int lookup_module_symbol_name(unsigned long addr, char *symname)
2587{
2588 struct module *mod;
2589
Rusty Russellcb2a5202008-01-14 00:55:03 -08002590 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002591 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002592 if (within_module_init(addr, mod) ||
2593 within_module_core(addr, mod)) {
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002594 const char *sym;
2595
2596 sym = get_ksymbol(mod, addr, NULL, NULL);
2597 if (!sym)
2598 goto out;
Tejun Heo9281ace2007-07-17 04:03:51 -07002599 strlcpy(symname, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002600 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002601 return 0;
2602 }
2603 }
2604out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002605 preempt_enable();
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -07002606 return -ERANGE;
2607}
2608
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002609int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2610 unsigned long *offset, char *modname, char *name)
2611{
2612 struct module *mod;
2613
Rusty Russellcb2a5202008-01-14 00:55:03 -08002614 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002615 list_for_each_entry_rcu(mod, &modules, list) {
Masami Hiramatsua06f6212009-01-06 14:41:49 -08002616 if (within_module_init(addr, mod) ||
2617 within_module_core(addr, mod)) {
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002618 const char *sym;
2619
2620 sym = get_ksymbol(mod, addr, size, offset);
2621 if (!sym)
2622 goto out;
2623 if (modname)
Tejun Heo9281ace2007-07-17 04:03:51 -07002624 strlcpy(modname, mod->name, MODULE_NAME_LEN);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002625 if (name)
Tejun Heo9281ace2007-07-17 04:03:51 -07002626 strlcpy(name, sym, KSYM_NAME_LEN);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002627 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002628 return 0;
2629 }
2630 }
2631out:
Rusty Russellcb2a5202008-01-14 00:55:03 -08002632 preempt_enable();
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07002633 return -ERANGE;
2634}
2635
Alexey Dobriyanea078902007-05-08 00:28:39 -07002636int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2637 char *name, char *module_name, int *exported)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 struct module *mod;
2640
Rusty Russellcb2a5202008-01-14 00:55:03 -08002641 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002642 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (symnum < mod->num_symtab) {
2644 *value = mod->symtab[symnum].st_value;
2645 *type = mod->symtab[symnum].st_info;
Andreas Gruenbacher098c5ee2006-07-14 00:24:04 -07002646 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
Tejun Heo9281ace2007-07-17 04:03:51 -07002647 KSYM_NAME_LEN);
2648 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
Tim Abbottca4787b2009-01-05 08:40:10 -06002649 *exported = is_exported(name, *value, mod);
Rusty Russellcb2a5202008-01-14 00:55:03 -08002650 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653 symnum -= mod->num_symtab;
2654 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002655 preempt_enable();
Alexey Dobriyanea078902007-05-08 00:28:39 -07002656 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659static unsigned long mod_find_symname(struct module *mod, const char *name)
2660{
2661 unsigned int i;
2662
2663 for (i = 0; i < mod->num_symtab; i++)
Keith Owens54e8ce42006-02-03 03:03:53 -08002664 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2665 mod->symtab[i].st_info != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return mod->symtab[i].st_value;
2667 return 0;
2668}
2669
2670/* Look for this name: can be of form module:name. */
2671unsigned long module_kallsyms_lookup_name(const char *name)
2672{
2673 struct module *mod;
2674 char *colon;
2675 unsigned long ret = 0;
2676
2677 /* Don't lock: we're in enough trouble already. */
Rusty Russellcb2a5202008-01-14 00:55:03 -08002678 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if ((colon = strchr(name, ':')) != NULL) {
2680 *colon = '\0';
2681 if ((mod = find_module(name)) != NULL)
2682 ret = mod_find_symname(mod, colon+1);
2683 *colon = ':';
2684 } else {
Andi Kleend72b3752008-08-30 10:09:00 +02002685 list_for_each_entry_rcu(mod, &modules, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if ((ret = mod_find_symname(mod, name)) != 0)
2687 break;
2688 }
Rusty Russellcb2a5202008-01-14 00:55:03 -08002689 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return ret;
2691}
Anders Kaseorg75a66612008-12-05 19:03:58 -05002692
2693int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2694 struct module *, unsigned long),
2695 void *data)
2696{
2697 struct module *mod;
2698 unsigned int i;
2699 int ret;
2700
2701 list_for_each_entry(mod, &modules, list) {
2702 for (i = 0; i < mod->num_symtab; i++) {
2703 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2704 mod, mod->symtab[i].st_value);
2705 if (ret != 0)
2706 return ret;
2707 }
2708 }
2709 return 0;
2710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711#endif /* CONFIG_KALLSYMS */
2712
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002713static char *module_flags(struct module *mod, char *buf)
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002714{
2715 int bx = 0;
2716
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002717 if (mod->taints ||
2718 mod->state == MODULE_STATE_GOING ||
2719 mod->state == MODULE_STATE_COMING) {
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002720 buf[bx++] = '(';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002721 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002722 buf[bx++] = 'P';
Andi Kleen25ddbb12008-10-15 22:01:41 -07002723 if (mod->taints & (1 << TAINT_FORCED_MODULE))
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002724 buf[bx++] = 'F';
Linus Torvalds26e9a392008-10-17 09:50:12 -07002725 if (mod->taints & (1 << TAINT_CRAP))
Greg Kroah-Hartman061b1bd2008-09-24 14:46:44 -07002726 buf[bx++] = 'C';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002727 /*
2728 * TAINT_FORCED_RMMOD: could be added.
2729 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2730 * apply to modules.
2731 */
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002732
2733 /* Show a - for module-is-being-unloaded */
2734 if (mod->state == MODULE_STATE_GOING)
2735 buf[bx++] = '-';
2736 /* Show a + for module-is-being-loaded */
2737 if (mod->state == MODULE_STATE_COMING)
2738 buf[bx++] = '+';
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002739 buf[bx++] = ')';
2740 }
2741 buf[bx] = '\0';
2742
2743 return buf;
2744}
2745
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002746#ifdef CONFIG_PROC_FS
2747/* Called by the /proc file system to return a list of modules. */
2748static void *m_start(struct seq_file *m, loff_t *pos)
2749{
2750 mutex_lock(&module_mutex);
2751 return seq_list_start(&modules, *pos);
2752}
2753
2754static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2755{
2756 return seq_list_next(p, &modules, pos);
2757}
2758
2759static void m_stop(struct seq_file *m, void *p)
2760{
2761 mutex_unlock(&module_mutex);
2762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764static int m_show(struct seq_file *m, void *p)
2765{
2766 struct module *mod = list_entry(p, struct module, list);
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002767 char buf[8];
2768
Denys Vlasenko2f0f2a32008-07-22 19:24:27 -05002769 seq_printf(m, "%s %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 mod->name, mod->init_size + mod->core_size);
2771 print_unload_info(m, mod);
2772
2773 /* Informative for users. */
2774 seq_printf(m, " %s",
2775 mod->state == MODULE_STATE_GOING ? "Unloading":
2776 mod->state == MODULE_STATE_COMING ? "Loading":
2777 "Live");
2778 /* Used by oprofile and other similar tools. */
2779 seq_printf(m, " 0x%p", mod->module_core);
2780
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002781 /* Taints info */
2782 if (mod->taints)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002783 seq_printf(m, " %s", module_flags(mod, buf));
Florin Malitafa3ba2e82006-10-11 01:21:48 -07002784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 seq_printf(m, "\n");
2786 return 0;
2787}
2788
2789/* Format: modulename size refcount deps address
2790
2791 Where refcount is a number or -, and deps is a comma-separated list
2792 of depends or -.
2793*/
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002794static const struct seq_operations modules_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .start = m_start,
2796 .next = m_next,
2797 .stop = m_stop,
2798 .show = m_show
2799};
2800
Alexey Dobriyan3b5d5c62008-10-06 13:19:27 +04002801static int modules_open(struct inode *inode, struct file *file)
2802{
2803 return seq_open(file, &modules_op);
2804}
2805
2806static const struct file_operations proc_modules_operations = {
2807 .open = modules_open,
2808 .read = seq_read,
2809 .llseek = seq_lseek,
2810 .release = seq_release,
2811};
2812
2813static int __init proc_modules_init(void)
2814{
2815 proc_create("modules", 0, NULL, &proc_modules_operations);
2816 return 0;
2817}
2818module_init(proc_modules_init);
2819#endif
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821/* Given an address, look for it in the module exception tables. */
2822const struct exception_table_entry *search_module_extables(unsigned long addr)
2823{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 const struct exception_table_entry *e = NULL;
2825 struct module *mod;
2826
Rusty Russell24da1cb2007-07-15 23:41:46 -07002827 preempt_disable();
Andi Kleend72b3752008-08-30 10:09:00 +02002828 list_for_each_entry_rcu(mod, &modules, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (mod->num_exentries == 0)
2830 continue;
Daniel Walker22a8bde2007-10-18 03:06:07 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 e = search_extable(mod->extable,
2833 mod->extable + mod->num_exentries - 1,
2834 addr);
2835 if (e)
2836 break;
2837 }
Rusty Russell24da1cb2007-07-15 23:41:46 -07002838 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 /* Now, if we found one, we are running inside it now, hence
Daniel Walker22a8bde2007-10-18 03:06:07 -07002841 we cannot unload the module, hence no refcnt needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return e;
2843}
2844
Ingo Molnar4d435f92006-07-03 00:24:24 -07002845/*
Rusty Russelle6104992009-03-31 13:05:31 -06002846 * is_module_address - is this address inside a module?
2847 * @addr: the address to check.
2848 *
2849 * See is_module_text_address() if you simply want to see if the address
2850 * is code (not data).
Ingo Molnar4d435f92006-07-03 00:24:24 -07002851 */
Rusty Russelle6104992009-03-31 13:05:31 -06002852bool is_module_address(unsigned long addr)
Ingo Molnar4d435f92006-07-03 00:24:24 -07002853{
Rusty Russelle6104992009-03-31 13:05:31 -06002854 bool ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002855
Rusty Russell24da1cb2007-07-15 23:41:46 -07002856 preempt_disable();
Rusty Russelle6104992009-03-31 13:05:31 -06002857 ret = __module_address(addr) != NULL;
Rusty Russell24da1cb2007-07-15 23:41:46 -07002858 preempt_enable();
Ingo Molnar4d435f92006-07-03 00:24:24 -07002859
Rusty Russelle6104992009-03-31 13:05:31 -06002860 return ret;
Ingo Molnar4d435f92006-07-03 00:24:24 -07002861}
2862
Rusty Russelle6104992009-03-31 13:05:31 -06002863/*
2864 * __module_address - get the module which contains an address.
2865 * @addr: the address.
2866 *
2867 * Must be called with preempt disabled or module mutex held so that
2868 * module doesn't get freed during this.
2869 */
Linus Torvalds714f83d2009-04-05 11:04:19 -07002870struct module *__module_address(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 struct module *mod;
2873
Rusty Russell3a642e92008-07-22 19:24:28 -05002874 if (addr < module_addr_min || addr > module_addr_max)
2875 return NULL;
2876
Andi Kleend72b3752008-08-30 10:09:00 +02002877 list_for_each_entry_rcu(mod, &modules, list)
Rusty Russelle6104992009-03-31 13:05:31 -06002878 if (within_module_core(addr, mod)
2879 || within_module_init(addr, mod))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return mod;
2881 return NULL;
2882}
Tim Abbottc6b37802008-12-05 19:03:59 -05002883EXPORT_SYMBOL_GPL(__module_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Rusty Russelle6104992009-03-31 13:05:31 -06002885/*
2886 * is_module_text_address - is this address inside module code?
2887 * @addr: the address to check.
2888 *
2889 * See is_module_address() if you simply want to see if the address is
2890 * anywhere in a module. See kernel_text_address() for testing if an
2891 * address corresponds to kernel or module code.
2892 */
2893bool is_module_text_address(unsigned long addr)
2894{
2895 bool ret;
2896
2897 preempt_disable();
2898 ret = __module_text_address(addr) != NULL;
2899 preempt_enable();
2900
2901 return ret;
2902}
2903
2904/*
2905 * __module_text_address - get the module whose code contains an address.
2906 * @addr: the address.
2907 *
2908 * Must be called with preempt disabled or module mutex held so that
2909 * module doesn't get freed during this.
2910 */
2911struct module *__module_text_address(unsigned long addr)
2912{
2913 struct module *mod = __module_address(addr);
2914 if (mod) {
2915 /* Make sure it's within the text section. */
2916 if (!within(addr, mod->module_init, mod->init_text_size)
2917 && !within(addr, mod->module_core, mod->core_text_size))
2918 mod = NULL;
2919 }
2920 return mod;
2921}
Tim Abbottc6b37802008-12-05 19:03:59 -05002922EXPORT_SYMBOL_GPL(__module_text_address);
Rusty Russelle6104992009-03-31 13:05:31 -06002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924/* Don't grab lock, we're oopsing. */
2925void print_modules(void)
2926{
2927 struct module *mod;
Randy Dunlap2bc2d612006-10-02 02:17:02 -07002928 char buf[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Linus Torvaldsb2311252009-06-16 11:07:14 -07002930 printk(KERN_DEFAULT "Modules linked in:");
Andi Kleend72b3752008-08-30 10:09:00 +02002931 /* Most callers should already have preempt disabled, but make sure */
2932 preempt_disable();
2933 list_for_each_entry_rcu(mod, &modules, list)
Arjan van de Ven21aa9282008-01-25 21:08:33 +01002934 printk(" %s%s", mod->name, module_flags(mod, buf));
Andi Kleend72b3752008-08-30 10:09:00 +02002935 preempt_enable();
Arjan van de Vene14af7e2008-01-25 21:08:33 +01002936 if (last_unloaded_module[0])
2937 printk(" [last unloaded: %s]", last_unloaded_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 printk("\n");
2939}
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941#ifdef CONFIG_MODVERSIONS
Rusty Russell8c8ef422009-03-31 13:05:34 -06002942/* Generate the signature for all relevant module structures here.
2943 * If these change, we don't want to try to parse the module. */
2944void module_layout(struct module *mod,
2945 struct modversion_info *ver,
2946 struct kernel_param *kp,
2947 struct kernel_symbol *ks,
2948 struct marker *marker,
2949 struct tracepoint *tp)
2950{
2951}
2952EXPORT_SYMBOL(module_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953#endif
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002954
2955#ifdef CONFIG_MARKERS
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002956void module_update_markers(void)
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002957{
2958 struct module *mod;
2959
2960 mutex_lock(&module_mutex);
2961 list_for_each_entry(mod, &modules, list)
2962 if (!mod->taints)
2963 marker_update_probe_range(mod->markers,
Mathieu Desnoyersfb40bd72008-02-13 15:03:37 -08002964 mod->markers + mod->num_markers);
Mathieu Desnoyers8256e472007-10-18 23:41:06 -07002965 mutex_unlock(&module_mutex);
2966}
2967#endif
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04002968
2969#ifdef CONFIG_TRACEPOINTS
2970void module_update_tracepoints(void)
2971{
2972 struct module *mod;
2973
2974 mutex_lock(&module_mutex);
2975 list_for_each_entry(mod, &modules, list)
2976 if (!mod->taints)
2977 tracepoint_update_probe_range(mod->tracepoints,
2978 mod->tracepoints + mod->num_tracepoints);
2979 mutex_unlock(&module_mutex);
2980}
2981
2982/*
2983 * Returns 0 if current not found.
2984 * Returns 1 if current found.
2985 */
2986int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2987{
2988 struct module *iter_mod;
2989 int found = 0;
2990
2991 mutex_lock(&module_mutex);
2992 list_for_each_entry(iter_mod, &modules, list) {
2993 if (!iter_mod->taints) {
2994 /*
2995 * Sorted module list
2996 */
2997 if (iter_mod < iter->module)
2998 continue;
2999 else if (iter_mod > iter->module)
3000 iter->tracepoint = NULL;
3001 found = tracepoint_get_iter_range(&iter->tracepoint,
3002 iter_mod->tracepoints,
3003 iter_mod->tracepoints
3004 + iter_mod->num_tracepoints);
3005 if (found) {
3006 iter->module = iter_mod;
3007 break;
3008 }
3009 }
3010 }
3011 mutex_unlock(&module_mutex);
3012 return found;
3013}
3014#endif