]>
Commit | Line | Data |
---|---|---|
f71d20e9 | 1 | /* |
1da177e4 LT |
2 | 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 | */ | |
1da177e4 LT |
19 | #include <linux/module.h> |
20 | #include <linux/moduleloader.h> | |
6d723736 | 21 | #include <linux/ftrace_event.h> |
1da177e4 | 22 | #include <linux/init.h> |
ae84e324 | 23 | #include <linux/kallsyms.h> |
3b5d5c6b | 24 | #include <linux/fs.h> |
6d760133 | 25 | #include <linux/sysfs.h> |
9f158333 | 26 | #include <linux/kernel.h> |
1da177e4 LT |
27 | #include <linux/slab.h> |
28 | #include <linux/vmalloc.h> | |
29 | #include <linux/elf.h> | |
3b5d5c6b | 30 | #include <linux/proc_fs.h> |
1da177e4 LT |
31 | #include <linux/seq_file.h> |
32 | #include <linux/syscalls.h> | |
33 | #include <linux/fcntl.h> | |
34 | #include <linux/rcupdate.h> | |
c59ede7b | 35 | #include <linux/capability.h> |
1da177e4 LT |
36 | #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> | |
f6a57033 | 42 | #include <linux/sched.h> |
1da177e4 LT |
43 | #include <linux/stop_machine.h> |
44 | #include <linux/device.h> | |
c988d2b2 | 45 | #include <linux/string.h> |
97d1f15b | 46 | #include <linux/mutex.h> |
d72b3751 | 47 | #include <linux/rculist.h> |
1da177e4 | 48 | #include <asm/uaccess.h> |
1da177e4 | 49 | #include <asm/cacheflush.h> |
b817f6fe | 50 | #include <linux/license.h> |
6d762394 | 51 | #include <asm/sections.h> |
97e1c18e | 52 | #include <linux/tracepoint.h> |
90d595fe | 53 | #include <linux/ftrace.h> |
22a9d645 | 54 | #include <linux/async.h> |
fbf59bc9 | 55 | #include <linux/percpu.h> |
4f2294b6 | 56 | #include <linux/kmemleak.h> |
1da177e4 LT |
57 | |
58 | #if 0 | |
59 | #define DEBUGP printk | |
60 | #else | |
61 | #define DEBUGP(fmt , a...) | |
62 | #endif | |
63 | ||
64 | #ifndef ARCH_SHF_SMALL | |
65 | #define ARCH_SHF_SMALL 0 | |
66 | #endif | |
67 | ||
68 | /* If this is set, the section belongs in the init part of the module */ | |
69 | #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) | |
70 | ||
24da1cbf | 71 | /* List of modules, protected by module_mutex or preempt_disable |
d72b3751 | 72 | * (delete uses stop_machine/add uses RCU list operations). */ |
c6b37801 TA |
73 | DEFINE_MUTEX(module_mutex); |
74 | EXPORT_SYMBOL_GPL(module_mutex); | |
1da177e4 LT |
75 | static LIST_HEAD(modules); |
76 | ||
19e4529e SR |
77 | /* Block module loading/unloading? */ |
78 | int modules_disabled = 0; | |
79 | ||
c9a3ba55 RR |
80 | /* Waiting for a module to finish initializing? */ |
81 | static DECLARE_WAIT_QUEUE_HEAD(module_wq); | |
82 | ||
e041c683 | 83 | static BLOCKING_NOTIFIER_HEAD(module_notify_list); |
1da177e4 | 84 | |
e610499e | 85 | /* Bounds of module allocation, for speeding __module_address */ |
3a642e99 RR |
86 | static unsigned long module_addr_min = -1UL, module_addr_max = 0; |
87 | ||
1da177e4 LT |
88 | int register_module_notifier(struct notifier_block * nb) |
89 | { | |
e041c683 | 90 | return blocking_notifier_chain_register(&module_notify_list, nb); |
1da177e4 LT |
91 | } |
92 | EXPORT_SYMBOL(register_module_notifier); | |
93 | ||
94 | int unregister_module_notifier(struct notifier_block * nb) | |
95 | { | |
e041c683 | 96 | return blocking_notifier_chain_unregister(&module_notify_list, nb); |
1da177e4 LT |
97 | } |
98 | EXPORT_SYMBOL(unregister_module_notifier); | |
99 | ||
9a4b9708 ML |
100 | /* We require a truly strong try_module_get(): 0 means failure due to |
101 | ongoing or failed initialization etc. */ | |
1da177e4 LT |
102 | static inline int strong_try_module_get(struct module *mod) |
103 | { | |
104 | if (mod && mod->state == MODULE_STATE_COMING) | |
c9a3ba55 RR |
105 | return -EBUSY; |
106 | if (try_module_get(mod)) | |
1da177e4 | 107 | return 0; |
c9a3ba55 RR |
108 | else |
109 | return -ENOENT; | |
1da177e4 LT |
110 | } |
111 | ||
fa3ba2e8 FM |
112 | static inline void add_taint_module(struct module *mod, unsigned flag) |
113 | { | |
114 | add_taint(flag); | |
25ddbb18 | 115 | mod->taints |= (1U << flag); |
fa3ba2e8 FM |
116 | } |
117 | ||
02a3e59a RD |
118 | /* |
119 | * A thread that wants to hold a reference to a module only while it | |
120 | * is running can call this to safely exit. nfsd and lockd use this. | |
1da177e4 LT |
121 | */ |
122 | void __module_put_and_exit(struct module *mod, long code) | |
123 | { | |
124 | module_put(mod); | |
125 | do_exit(code); | |
126 | } | |
127 | EXPORT_SYMBOL(__module_put_and_exit); | |
22a8bdeb | 128 | |
1da177e4 LT |
129 | /* Find a module section: 0 means not found. */ |
130 | static unsigned int find_sec(Elf_Ehdr *hdr, | |
131 | Elf_Shdr *sechdrs, | |
132 | const char *secstrings, | |
133 | const char *name) | |
134 | { | |
135 | unsigned int i; | |
136 | ||
137 | for (i = 1; i < hdr->e_shnum; i++) | |
138 | /* Alloc bit cleared means "ignore it." */ | |
139 | if ((sechdrs[i].sh_flags & SHF_ALLOC) | |
140 | && strcmp(secstrings+sechdrs[i].sh_name, name) == 0) | |
141 | return i; | |
142 | return 0; | |
143 | } | |
144 | ||
5e458cc0 RR |
145 | /* Find a module section, or NULL. */ |
146 | static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs, | |
147 | const char *secstrings, const char *name) | |
148 | { | |
149 | /* Section 0 has sh_addr 0. */ | |
150 | return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr; | |
151 | } | |
152 | ||
153 | /* Find a module section, or NULL. Fill in number of "objects" in section. */ | |
154 | static void *section_objs(Elf_Ehdr *hdr, | |
155 | Elf_Shdr *sechdrs, | |
156 | const char *secstrings, | |
157 | const char *name, | |
158 | size_t object_size, | |
159 | unsigned int *num) | |
160 | { | |
161 | unsigned int sec = find_sec(hdr, sechdrs, secstrings, name); | |
162 | ||
163 | /* Section 0 has sh_addr 0 and sh_size 0. */ | |
164 | *num = sechdrs[sec].sh_size / object_size; | |
165 | return (void *)sechdrs[sec].sh_addr; | |
166 | } | |
167 | ||
1da177e4 LT |
168 | /* Provided by the linker */ |
169 | extern const struct kernel_symbol __start___ksymtab[]; | |
170 | extern const struct kernel_symbol __stop___ksymtab[]; | |
171 | extern const struct kernel_symbol __start___ksymtab_gpl[]; | |
172 | extern const struct kernel_symbol __stop___ksymtab_gpl[]; | |
9f28bb7e GKH |
173 | extern const struct kernel_symbol __start___ksymtab_gpl_future[]; |
174 | extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; | |
f71d20e9 AV |
175 | extern const struct kernel_symbol __start___ksymtab_gpl_future[]; |
176 | extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; | |
1da177e4 LT |
177 | extern const unsigned long __start___kcrctab[]; |
178 | extern const unsigned long __start___kcrctab_gpl[]; | |
9f28bb7e | 179 | extern const unsigned long __start___kcrctab_gpl_future[]; |
f7f5b675 DV |
180 | #ifdef CONFIG_UNUSED_SYMBOLS |
181 | extern const struct kernel_symbol __start___ksymtab_unused[]; | |
182 | extern const struct kernel_symbol __stop___ksymtab_unused[]; | |
183 | extern const struct kernel_symbol __start___ksymtab_unused_gpl[]; | |
184 | extern const struct kernel_symbol __stop___ksymtab_unused_gpl[]; | |
f71d20e9 AV |
185 | extern const unsigned long __start___kcrctab_unused[]; |
186 | extern const unsigned long __start___kcrctab_unused_gpl[]; | |
f7f5b675 | 187 | #endif |
1da177e4 LT |
188 | |
189 | #ifndef CONFIG_MODVERSIONS | |
190 | #define symversion(base, idx) NULL | |
191 | #else | |
f83ca9fe | 192 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) |
1da177e4 LT |
193 | #endif |
194 | ||
dafd0940 RR |
195 | static bool each_symbol_in_section(const struct symsearch *arr, |
196 | unsigned int arrsize, | |
197 | struct module *owner, | |
198 | bool (*fn)(const struct symsearch *syms, | |
199 | struct module *owner, | |
200 | unsigned int symnum, void *data), | |
201 | void *data) | |
ad9546c9 | 202 | { |
dafd0940 | 203 | unsigned int i, j; |
ad9546c9 | 204 | |
dafd0940 RR |
205 | for (j = 0; j < arrsize; j++) { |
206 | for (i = 0; i < arr[j].stop - arr[j].start; i++) | |
207 | if (fn(&arr[j], owner, i, data)) | |
208 | return true; | |
f71d20e9 | 209 | } |
dafd0940 RR |
210 | |
211 | return false; | |
ad9546c9 RR |
212 | } |
213 | ||
dafd0940 | 214 | /* Returns true as soon as fn returns true, otherwise false. */ |
c6b37801 TA |
215 | bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner, |
216 | unsigned int symnum, void *data), void *data) | |
ad9546c9 RR |
217 | { |
218 | struct module *mod; | |
ad9546c9 RR |
219 | const struct symsearch arr[] = { |
220 | { __start___ksymtab, __stop___ksymtab, __start___kcrctab, | |
dafd0940 | 221 | NOT_GPL_ONLY, false }, |
ad9546c9 | 222 | { __start___ksymtab_gpl, __stop___ksymtab_gpl, |
dafd0940 RR |
223 | __start___kcrctab_gpl, |
224 | GPL_ONLY, false }, | |
ad9546c9 | 225 | { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future, |
dafd0940 RR |
226 | __start___kcrctab_gpl_future, |
227 | WILL_BE_GPL_ONLY, false }, | |
f7f5b675 | 228 | #ifdef CONFIG_UNUSED_SYMBOLS |
ad9546c9 | 229 | { __start___ksymtab_unused, __stop___ksymtab_unused, |
dafd0940 RR |
230 | __start___kcrctab_unused, |
231 | NOT_GPL_ONLY, true }, | |
ad9546c9 | 232 | { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl, |
dafd0940 RR |
233 | __start___kcrctab_unused_gpl, |
234 | GPL_ONLY, true }, | |
f7f5b675 | 235 | #endif |
ad9546c9 | 236 | }; |
f71d20e9 | 237 | |
dafd0940 RR |
238 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data)) |
239 | return true; | |
f71d20e9 | 240 | |
d72b3751 | 241 | list_for_each_entry_rcu(mod, &modules, list) { |
ad9546c9 RR |
242 | struct symsearch arr[] = { |
243 | { mod->syms, mod->syms + mod->num_syms, mod->crcs, | |
dafd0940 | 244 | NOT_GPL_ONLY, false }, |
ad9546c9 | 245 | { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms, |
dafd0940 RR |
246 | mod->gpl_crcs, |
247 | GPL_ONLY, false }, | |
ad9546c9 RR |
248 | { mod->gpl_future_syms, |
249 | mod->gpl_future_syms + mod->num_gpl_future_syms, | |
dafd0940 RR |
250 | mod->gpl_future_crcs, |
251 | WILL_BE_GPL_ONLY, false }, | |
f7f5b675 | 252 | #ifdef CONFIG_UNUSED_SYMBOLS |
ad9546c9 RR |
253 | { mod->unused_syms, |
254 | mod->unused_syms + mod->num_unused_syms, | |
dafd0940 RR |
255 | mod->unused_crcs, |
256 | NOT_GPL_ONLY, true }, | |
ad9546c9 RR |
257 | { mod->unused_gpl_syms, |
258 | mod->unused_gpl_syms + mod->num_unused_gpl_syms, | |
dafd0940 RR |
259 | mod->unused_gpl_crcs, |
260 | GPL_ONLY, true }, | |
f7f5b675 | 261 | #endif |
ad9546c9 RR |
262 | }; |
263 | ||
dafd0940 RR |
264 | if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data)) |
265 | return true; | |
266 | } | |
267 | return false; | |
268 | } | |
c6b37801 | 269 | EXPORT_SYMBOL_GPL(each_symbol); |
dafd0940 RR |
270 | |
271 | struct find_symbol_arg { | |
272 | /* Input */ | |
273 | const char *name; | |
274 | bool gplok; | |
275 | bool warn; | |
276 | ||
277 | /* Output */ | |
278 | struct module *owner; | |
279 | const unsigned long *crc; | |
414fd31b | 280 | const struct kernel_symbol *sym; |
dafd0940 RR |
281 | }; |
282 | ||
283 | static bool find_symbol_in_section(const struct symsearch *syms, | |
284 | struct module *owner, | |
285 | unsigned int symnum, void *data) | |
286 | { | |
287 | struct find_symbol_arg *fsa = data; | |
288 | ||
289 | if (strcmp(syms->start[symnum].name, fsa->name) != 0) | |
290 | return false; | |
291 | ||
292 | if (!fsa->gplok) { | |
293 | if (syms->licence == GPL_ONLY) | |
294 | return false; | |
295 | if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) { | |
296 | printk(KERN_WARNING "Symbol %s is being used " | |
297 | "by a non-GPL module, which will not " | |
298 | "be allowed in the future\n", fsa->name); | |
299 | printk(KERN_WARNING "Please see the file " | |
300 | "Documentation/feature-removal-schedule.txt " | |
301 | "in the kernel source tree for more details.\n"); | |
9f28bb7e | 302 | } |
1da177e4 | 303 | } |
ad9546c9 | 304 | |
f7f5b675 | 305 | #ifdef CONFIG_UNUSED_SYMBOLS |
dafd0940 RR |
306 | if (syms->unused && fsa->warn) { |
307 | printk(KERN_WARNING "Symbol %s is marked as UNUSED, " | |
308 | "however this module is using it.\n", fsa->name); | |
309 | printk(KERN_WARNING | |
310 | "This symbol will go away in the future.\n"); | |
311 | printk(KERN_WARNING | |
312 | "Please evalute if this is the right api to use and if " | |
313 | "it really is, submit a report the linux kernel " | |
314 | "mailinglist together with submitting your code for " | |
315 | "inclusion.\n"); | |
316 | } | |
f7f5b675 | 317 | #endif |
dafd0940 RR |
318 | |
319 | fsa->owner = owner; | |
320 | fsa->crc = symversion(syms->crcs, symnum); | |
414fd31b | 321 | fsa->sym = &syms->start[symnum]; |
dafd0940 RR |
322 | return true; |
323 | } | |
324 | ||
414fd31b TA |
325 | /* Find a symbol and return it, along with, (optional) crc and |
326 | * (optional) module which owns it */ | |
c6b37801 TA |
327 | const struct kernel_symbol *find_symbol(const char *name, |
328 | struct module **owner, | |
329 | const unsigned long **crc, | |
330 | bool gplok, | |
331 | bool warn) | |
dafd0940 RR |
332 | { |
333 | struct find_symbol_arg fsa; | |
334 | ||
335 | fsa.name = name; | |
336 | fsa.gplok = gplok; | |
337 | fsa.warn = warn; | |
338 | ||
339 | if (each_symbol(find_symbol_in_section, &fsa)) { | |
340 | if (owner) | |
341 | *owner = fsa.owner; | |
342 | if (crc) | |
343 | *crc = fsa.crc; | |
414fd31b | 344 | return fsa.sym; |
dafd0940 RR |
345 | } |
346 | ||
1da177e4 | 347 | DEBUGP("Failed to find symbol %s\n", name); |
414fd31b | 348 | return NULL; |
1da177e4 | 349 | } |
c6b37801 | 350 | EXPORT_SYMBOL_GPL(find_symbol); |
1da177e4 | 351 | |
1da177e4 | 352 | /* Search for module by name: must hold module_mutex. */ |
c6b37801 | 353 | struct module *find_module(const char *name) |
1da177e4 LT |
354 | { |
355 | struct module *mod; | |
356 | ||
357 | list_for_each_entry(mod, &modules, list) { | |
358 | if (strcmp(mod->name, name) == 0) | |
359 | return mod; | |
360 | } | |
361 | return NULL; | |
362 | } | |
c6b37801 | 363 | EXPORT_SYMBOL_GPL(find_module); |
1da177e4 LT |
364 | |
365 | #ifdef CONFIG_SMP | |
fbf59bc9 TH |
366 | |
367 | #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA | |
368 | ||
369 | static void *percpu_modalloc(unsigned long size, unsigned long align, | |
370 | const char *name) | |
371 | { | |
372 | void *ptr; | |
373 | ||
374 | if (align > PAGE_SIZE) { | |
375 | printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", | |
376 | name, align, PAGE_SIZE); | |
377 | align = PAGE_SIZE; | |
378 | } | |
379 | ||
edcb4639 | 380 | ptr = __alloc_reserved_percpu(size, align); |
fbf59bc9 TH |
381 | if (!ptr) |
382 | printk(KERN_WARNING | |
383 | "Could not allocate %lu bytes percpu data\n", size); | |
384 | return ptr; | |
385 | } | |
386 | ||
387 | static void percpu_modfree(void *freeme) | |
388 | { | |
389 | free_percpu(freeme); | |
390 | } | |
391 | ||
392 | #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */ | |
393 | ||
1da177e4 LT |
394 | /* Number of blocks used and allocated. */ |
395 | static unsigned int pcpu_num_used, pcpu_num_allocated; | |
396 | /* Size of each block. -ve means used. */ | |
397 | static int *pcpu_size; | |
398 | ||
399 | static int split_block(unsigned int i, unsigned short size) | |
400 | { | |
401 | /* Reallocation required? */ | |
402 | if (pcpu_num_used + 1 > pcpu_num_allocated) { | |
6d4f9c55 PE |
403 | int *new; |
404 | ||
405 | new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2, | |
406 | GFP_KERNEL); | |
1da177e4 LT |
407 | if (!new) |
408 | return 0; | |
409 | ||
1da177e4 | 410 | pcpu_num_allocated *= 2; |
1da177e4 LT |
411 | pcpu_size = new; |
412 | } | |
413 | ||
414 | /* Insert a new subblock */ | |
415 | memmove(&pcpu_size[i+1], &pcpu_size[i], | |
416 | sizeof(pcpu_size[0]) * (pcpu_num_used - i)); | |
417 | pcpu_num_used++; | |
418 | ||
419 | pcpu_size[i+1] -= size; | |
420 | pcpu_size[i] = size; | |
421 | return 1; | |
422 | } | |
423 | ||
424 | static inline unsigned int block_size(int val) | |
425 | { | |
426 | if (val < 0) | |
427 | return -val; | |
428 | return val; | |
429 | } | |
430 | ||
842bbaaa RR |
431 | static void *percpu_modalloc(unsigned long size, unsigned long align, |
432 | const char *name) | |
1da177e4 LT |
433 | { |
434 | unsigned long extra; | |
435 | unsigned int i; | |
436 | void *ptr; | |
4f2294b6 | 437 | int cpu; |
1da177e4 | 438 | |
b6e3590f JF |
439 | if (align > PAGE_SIZE) { |
440 | printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n", | |
441 | name, align, PAGE_SIZE); | |
442 | align = PAGE_SIZE; | |
842bbaaa | 443 | } |
1da177e4 LT |
444 | |
445 | ptr = __per_cpu_start; | |
446 | for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { | |
447 | /* Extra for alignment requirement. */ | |
448 | extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr; | |
449 | BUG_ON(i == 0 && extra != 0); | |
450 | ||
451 | if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size) | |
452 | continue; | |
453 | ||
454 | /* Transfer extra to previous block. */ | |
455 | if (pcpu_size[i-1] < 0) | |
456 | pcpu_size[i-1] -= extra; | |
457 | else | |
458 | pcpu_size[i-1] += extra; | |
459 | pcpu_size[i] -= extra; | |
460 | ptr += extra; | |
461 | ||
462 | /* Split block if warranted */ | |
463 | if (pcpu_size[i] - size > sizeof(unsigned long)) | |
464 | if (!split_block(i, size)) | |
465 | return NULL; | |
466 | ||
4f2294b6 CM |
467 | /* add the per-cpu scanning areas */ |
468 | for_each_possible_cpu(cpu) | |
469 | kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0, | |
470 | GFP_KERNEL); | |
471 | ||
1da177e4 LT |
472 | /* Mark allocated */ |
473 | pcpu_size[i] = -pcpu_size[i]; | |
474 | return ptr; | |
475 | } | |
476 | ||
477 | printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n", | |
478 | size); | |
479 | return NULL; | |
480 | } | |
481 | ||
482 | static void percpu_modfree(void *freeme) | |
483 | { | |
484 | unsigned int i; | |
485 | void *ptr = __per_cpu_start + block_size(pcpu_size[0]); | |
4f2294b6 | 486 | int cpu; |
1da177e4 LT |
487 | |
488 | /* First entry is core kernel percpu data. */ | |
489 | for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { | |
490 | if (ptr == freeme) { | |
491 | pcpu_size[i] = -pcpu_size[i]; | |
492 | goto free; | |
493 | } | |
494 | } | |
495 | BUG(); | |
496 | ||
497 | free: | |
4f2294b6 CM |
498 | /* remove the per-cpu scanning areas */ |
499 | for_each_possible_cpu(cpu) | |
500 | kmemleak_free(freeme + per_cpu_offset(cpu)); | |
501 | ||
1da177e4 LT |
502 | /* Merge with previous? */ |
503 | if (pcpu_size[i-1] >= 0) { | |
504 | pcpu_size[i-1] += pcpu_size[i]; | |
505 | pcpu_num_used--; | |
506 | memmove(&pcpu_size[i], &pcpu_size[i+1], | |
507 | (pcpu_num_used - i) * sizeof(pcpu_size[0])); | |
508 | i--; | |
509 | } | |
510 | /* Merge with next? */ | |
511 | if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) { | |
512 | pcpu_size[i] += pcpu_size[i+1]; | |
513 | pcpu_num_used--; | |
514 | memmove(&pcpu_size[i+1], &pcpu_size[i+2], | |
515 | (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0])); | |
516 | } | |
517 | } | |
518 | ||
1da177e4 LT |
519 | static int percpu_modinit(void) |
520 | { | |
521 | pcpu_num_used = 2; | |
522 | pcpu_num_allocated = 2; | |
523 | pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated, | |
524 | GFP_KERNEL); | |
525 | /* Static in-kernel percpu data (used). */ | |
b00742d3 | 526 | pcpu_size[0] = -(__per_cpu_end-__per_cpu_start); |
1da177e4 LT |
527 | /* Free room. */ |
528 | pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0]; | |
529 | if (pcpu_size[1] < 0) { | |
530 | printk(KERN_ERR "No per-cpu room for modules.\n"); | |
531 | pcpu_num_used = 1; | |
532 | } | |
533 | ||
534 | return 0; | |
22a8bdeb | 535 | } |
1da177e4 | 536 | __initcall(percpu_modinit); |
6b588c18 | 537 | |
fbf59bc9 TH |
538 | #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */ |
539 | ||
6b588c18 TH |
540 | static unsigned int find_pcpusec(Elf_Ehdr *hdr, |
541 | Elf_Shdr *sechdrs, | |
542 | const char *secstrings) | |
543 | { | |
544 | return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); | |
545 | } | |
546 | ||
547 | static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size) | |
548 | { | |
549 | int cpu; | |
550 | ||
551 | for_each_possible_cpu(cpu) | |
552 | memcpy(pcpudest + per_cpu_offset(cpu), from, size); | |
553 | } | |
554 | ||
1da177e4 | 555 | #else /* ... !CONFIG_SMP */ |
6b588c18 | 556 | |
842bbaaa RR |
557 | static inline void *percpu_modalloc(unsigned long size, unsigned long align, |
558 | const char *name) | |
1da177e4 LT |
559 | { |
560 | return NULL; | |
561 | } | |
562 | static inline void percpu_modfree(void *pcpuptr) | |
563 | { | |
564 | BUG(); | |
565 | } | |
566 | static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, | |
567 | Elf_Shdr *sechdrs, | |
568 | const char *secstrings) | |
569 | { | |
570 | return 0; | |
571 | } | |
572 | static inline void percpu_modcopy(void *pcpudst, const void *src, | |
573 | unsigned long size) | |
574 | { | |
575 | /* pcpusec should be 0, and size of that section should be 0. */ | |
576 | BUG_ON(size != 0); | |
577 | } | |
6b588c18 | 578 | |
1da177e4 LT |
579 | #endif /* CONFIG_SMP */ |
580 | ||
c988d2b2 MD |
581 | #define MODINFO_ATTR(field) \ |
582 | static void setup_modinfo_##field(struct module *mod, const char *s) \ | |
583 | { \ | |
584 | mod->field = kstrdup(s, GFP_KERNEL); \ | |
585 | } \ | |
586 | static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ | |
587 | struct module *mod, char *buffer) \ | |
588 | { \ | |
589 | return sprintf(buffer, "%s\n", mod->field); \ | |
590 | } \ | |
591 | static int modinfo_##field##_exists(struct module *mod) \ | |
592 | { \ | |
593 | return mod->field != NULL; \ | |
594 | } \ | |
595 | static void free_modinfo_##field(struct module *mod) \ | |
596 | { \ | |
22a8bdeb DW |
597 | kfree(mod->field); \ |
598 | mod->field = NULL; \ | |
c988d2b2 MD |
599 | } \ |
600 | static struct module_attribute modinfo_##field = { \ | |
7b595756 | 601 | .attr = { .name = __stringify(field), .mode = 0444 }, \ |
c988d2b2 MD |
602 | .show = show_modinfo_##field, \ |
603 | .setup = setup_modinfo_##field, \ | |
604 | .test = modinfo_##field##_exists, \ | |
605 | .free = free_modinfo_##field, \ | |
606 | }; | |
607 | ||
608 | MODINFO_ATTR(version); | |
609 | MODINFO_ATTR(srcversion); | |
610 | ||
e14af7ee AV |
611 | static char last_unloaded_module[MODULE_NAME_LEN+1]; |
612 | ||
03e88ae1 | 613 | #ifdef CONFIG_MODULE_UNLOAD |
1da177e4 LT |
614 | /* Init the unload section of the module. */ |
615 | static void module_unload_init(struct module *mod) | |
616 | { | |
720eba31 | 617 | int cpu; |
1da177e4 LT |
618 | |
619 | INIT_LIST_HEAD(&mod->modules_which_use_me); | |
720eba31 ED |
620 | for_each_possible_cpu(cpu) |
621 | local_set(__module_ref_addr(mod, cpu), 0); | |
1da177e4 | 622 | /* Hold reference count during initialization. */ |
720eba31 | 623 | local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1); |
1da177e4 LT |
624 | /* Backwards compatibility macros put refcount during init. */ |
625 | mod->waiter = current; | |
626 | } | |
627 | ||
628 | /* modules using other modules */ | |
629 | struct module_use | |
630 | { | |
631 | struct list_head list; | |
632 | struct module *module_which_uses; | |
633 | }; | |
634 | ||
635 | /* Does a already use b? */ | |
636 | static int already_uses(struct module *a, struct module *b) | |
637 | { | |
638 | struct module_use *use; | |
639 | ||
640 | list_for_each_entry(use, &b->modules_which_use_me, list) { | |
641 | if (use->module_which_uses == a) { | |
642 | DEBUGP("%s uses %s!\n", a->name, b->name); | |
643 | return 1; | |
644 | } | |
645 | } | |
646 | DEBUGP("%s does not use %s!\n", a->name, b->name); | |
647 | return 0; | |
648 | } | |
649 | ||
650 | /* Module a uses b */ | |
c6b37801 | 651 | int use_module(struct module *a, struct module *b) |
1da177e4 LT |
652 | { |
653 | struct module_use *use; | |
c9a3ba55 | 654 | int no_warn, err; |
270a6c4c | 655 | |
1da177e4 LT |
656 | if (b == NULL || already_uses(a, b)) return 1; |
657 | ||
c9a3ba55 RR |
658 | /* If we're interrupted or time out, we fail. */ |
659 | if (wait_event_interruptible_timeout( | |
660 | module_wq, (err = strong_try_module_get(b)) != -EBUSY, | |
661 | 30 * HZ) <= 0) { | |
662 | printk("%s: gave up waiting for init of module %s.\n", | |
663 | a->name, b->name); | |
664 | return 0; | |
665 | } | |
666 | ||
667 | /* If strong_try_module_get() returned a different error, we fail. */ | |
668 | if (err) | |
1da177e4 LT |
669 | return 0; |
670 | ||
671 | DEBUGP("Allocating new usage for %s.\n", a->name); | |
672 | use = kmalloc(sizeof(*use), GFP_ATOMIC); | |
673 | if (!use) { | |
674 | printk("%s: out of memory loading\n", a->name); | |
675 | module_put(b); | |
676 | return 0; | |
677 | } | |
678 | ||
679 | use->module_which_uses = a; | |
680 | list_add(&use->list, &b->modules_which_use_me); | |
270a6c4c | 681 | no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name); |
1da177e4 LT |
682 | return 1; |
683 | } | |
c6b37801 | 684 | EXPORT_SYMBOL_GPL(use_module); |
1da177e4 LT |
685 | |
686 | /* Clear the unload stuff of the module. */ | |
687 | static void module_unload_free(struct module *mod) | |
688 | { | |
689 | struct module *i; | |
690 | ||
691 | list_for_each_entry(i, &modules, list) { | |
692 | struct module_use *use; | |
693 | ||
694 | list_for_each_entry(use, &i->modules_which_use_me, list) { | |
695 | if (use->module_which_uses == mod) { | |
696 | DEBUGP("%s unusing %s\n", mod->name, i->name); | |
697 | module_put(i); | |
698 | list_del(&use->list); | |
699 | kfree(use); | |
270a6c4c | 700 | sysfs_remove_link(i->holders_dir, mod->name); |
1da177e4 LT |
701 | /* There can be at most one match. */ |
702 | break; | |
703 | } | |
704 | } | |
705 | } | |
706 | } | |
707 | ||
708 | #ifdef CONFIG_MODULE_FORCE_UNLOAD | |
fb169793 | 709 | static inline int try_force_unload(unsigned int flags) |
1da177e4 LT |
710 | { |
711 | int ret = (flags & O_TRUNC); | |
712 | if (ret) | |
fb169793 | 713 | add_taint(TAINT_FORCED_RMMOD); |
1da177e4 LT |
714 | return ret; |
715 | } | |
716 | #else | |
fb169793 | 717 | static inline int try_force_unload(unsigned int flags) |
1da177e4 LT |
718 | { |
719 | return 0; | |
720 | } | |
721 | #endif /* CONFIG_MODULE_FORCE_UNLOAD */ | |
722 | ||
723 | struct stopref | |
724 | { | |
725 | struct module *mod; | |
726 | int flags; | |
727 | int *forced; | |
728 | }; | |
729 | ||
730 | /* Whole machine is stopped with interrupts off when this runs. */ | |
731 | static int __try_stop_module(void *_sref) | |
732 | { | |
733 | struct stopref *sref = _sref; | |
734 | ||
da39ba5e RR |
735 | /* If it's not unused, quit unless we're forcing. */ |
736 | if (module_refcount(sref->mod) != 0) { | |
fb169793 | 737 | if (!(*sref->forced = try_force_unload(sref->flags))) |
1da177e4 LT |
738 | return -EWOULDBLOCK; |
739 | } | |
740 | ||
741 | /* Mark it as dying. */ | |
742 | sref->mod->state = MODULE_STATE_GOING; | |
743 | return 0; | |
744 | } | |
745 | ||
746 | static int try_stop_module(struct module *mod, int flags, int *forced) | |
747 | { | |
da39ba5e RR |
748 | if (flags & O_NONBLOCK) { |
749 | struct stopref sref = { mod, flags, forced }; | |
1da177e4 | 750 | |
9b1a4d38 | 751 | return stop_machine(__try_stop_module, &sref, NULL); |
da39ba5e RR |
752 | } else { |
753 | /* We don't need to stop the machine for this. */ | |
754 | mod->state = MODULE_STATE_GOING; | |
755 | synchronize_sched(); | |
756 | return 0; | |
757 | } | |
1da177e4 LT |
758 | } |
759 | ||
760 | unsigned int module_refcount(struct module *mod) | |
761 | { | |
720eba31 ED |
762 | unsigned int total = 0; |
763 | int cpu; | |
1da177e4 | 764 | |
720eba31 ED |
765 | for_each_possible_cpu(cpu) |
766 | total += local_read(__module_ref_addr(mod, cpu)); | |
1da177e4 LT |
767 | return total; |
768 | } | |
769 | EXPORT_SYMBOL(module_refcount); | |
770 | ||
771 | /* This exists whether we can unload or not */ | |
772 | static void free_module(struct module *mod); | |
773 | ||
774 | static void wait_for_zero_refcount(struct module *mod) | |
775 | { | |
a6550207 | 776 | /* Since we might sleep for some time, release the mutex first */ |
6389a385 | 777 | mutex_unlock(&module_mutex); |
1da177e4 LT |
778 | for (;;) { |
779 | DEBUGP("Looking at refcount...\n"); | |
780 | set_current_state(TASK_UNINTERRUPTIBLE); | |
781 | if (module_refcount(mod) == 0) | |
782 | break; | |
783 | schedule(); | |
784 | } | |
785 | current->state = TASK_RUNNING; | |
6389a385 | 786 | mutex_lock(&module_mutex); |
1da177e4 LT |
787 | } |
788 | ||
17da2bd9 HC |
789 | SYSCALL_DEFINE2(delete_module, const char __user *, name_user, |
790 | unsigned int, flags) | |
1da177e4 LT |
791 | { |
792 | struct module *mod; | |
dfff0a06 | 793 | char name[MODULE_NAME_LEN]; |
1da177e4 LT |
794 | int ret, forced = 0; |
795 | ||
3d43321b | 796 | if (!capable(CAP_SYS_MODULE) || modules_disabled) |
dfff0a06 GKH |
797 | return -EPERM; |
798 | ||
799 | if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) | |
800 | return -EFAULT; | |
801 | name[MODULE_NAME_LEN-1] = '\0'; | |
802 | ||
9e01892c HC |
803 | /* Create stop_machine threads since free_module relies on |
804 | * a non-failing stop_machine call. */ | |
805 | ret = stop_machine_create(); | |
806 | if (ret) | |
807 | return ret; | |
808 | ||
809 | if (mutex_lock_interruptible(&module_mutex) != 0) { | |
810 | ret = -EINTR; | |
811 | goto out_stop; | |
812 | } | |
1da177e4 LT |
813 | |
814 | mod = find_module(name); | |
815 | if (!mod) { | |
816 | ret = -ENOENT; | |
817 | goto out; | |
818 | } | |
819 | ||
820 | if (!list_empty(&mod->modules_which_use_me)) { | |
821 | /* Other modules depend on us: get rid of them first. */ | |
822 | ret = -EWOULDBLOCK; | |
823 | goto out; | |
824 | } | |
825 | ||
826 | /* Doing init or already dying? */ | |
827 | if (mod->state != MODULE_STATE_LIVE) { | |
828 | /* FIXME: if (force), slam module count and wake up | |
829 | waiter --RR */ | |
830 | DEBUGP("%s already dying\n", mod->name); | |
831 | ret = -EBUSY; | |
832 | goto out; | |
833 | } | |
834 | ||
835 | /* If it has an init func, it must have an exit func to unload */ | |
af49d924 | 836 | if (mod->init && !mod->exit) { |
fb169793 | 837 | forced = try_force_unload(flags); |
1da177e4 LT |
838 | if (!forced) { |
839 | /* This module can't be removed */ | |
840 | ret = -EBUSY; | |
841 | goto out; | |
842 | } | |
843 | } | |
844 | ||
845 | /* Set this up before setting mod->state */ | |
846 | mod->waiter = current; | |
847 | ||
848 | /* Stop the machine so refcounts can't move and disable module. */ | |
849 | ret = try_stop_module(mod, flags, &forced); | |
850 | if (ret != 0) | |
851 | goto out; | |
852 | ||
853 | /* Never wait if forced. */ | |
854 | if (!forced && module_refcount(mod) != 0) | |
855 | wait_for_zero_refcount(mod); | |
856 | ||
df4b565e | 857 | mutex_unlock(&module_mutex); |
1da177e4 | 858 | /* Final destruction now noone is using it. */ |
df4b565e | 859 | if (mod->exit != NULL) |
1da177e4 | 860 | mod->exit(); |
df4b565e PO |
861 | blocking_notifier_call_chain(&module_notify_list, |
862 | MODULE_STATE_GOING, mod); | |
22a9d645 | 863 | async_synchronize_full(); |
df4b565e | 864 | mutex_lock(&module_mutex); |
e14af7ee | 865 | /* Store the name of the last unloaded module for diagnostic purposes */ |
efa5345e | 866 | strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); |
e9d376f0 | 867 | ddebug_remove_module(mod->name); |
1da177e4 LT |
868 | free_module(mod); |
869 | ||
870 | out: | |
6389a385 | 871 | mutex_unlock(&module_mutex); |
9e01892c HC |
872 | out_stop: |
873 | stop_machine_destroy(); | |
1da177e4 LT |
874 | return ret; |
875 | } | |
876 | ||
d1e99d7a | 877 | static inline void print_unload_info(struct seq_file *m, struct module *mod) |
1da177e4 LT |
878 | { |
879 | struct module_use *use; | |
880 | int printed_something = 0; | |
881 | ||
882 | seq_printf(m, " %u ", module_refcount(mod)); | |
883 | ||
884 | /* Always include a trailing , so userspace can differentiate | |
885 | between this and the old multi-field proc format. */ | |
886 | list_for_each_entry(use, &mod->modules_which_use_me, list) { | |
887 | printed_something = 1; | |
888 | seq_printf(m, "%s,", use->module_which_uses->name); | |
889 | } | |
890 | ||
1da177e4 LT |
891 | if (mod->init != NULL && mod->exit == NULL) { |
892 | printed_something = 1; | |
893 | seq_printf(m, "[permanent],"); | |
894 | } | |
895 | ||
896 | if (!printed_something) | |
897 | seq_printf(m, "-"); | |
898 | } | |
899 | ||
900 | void __symbol_put(const char *symbol) | |
901 | { | |
902 | struct module *owner; | |
1da177e4 | 903 | |
24da1cbf | 904 | preempt_disable(); |
414fd31b | 905 | if (!find_symbol(symbol, &owner, NULL, true, false)) |
1da177e4 LT |
906 | BUG(); |
907 | module_put(owner); | |
24da1cbf | 908 | preempt_enable(); |
1da177e4 LT |
909 | } |
910 | EXPORT_SYMBOL(__symbol_put); | |
911 | ||
7d1d16e4 | 912 | /* Note this assumes addr is a function, which it currently always is. */ |
1da177e4 LT |
913 | void symbol_put_addr(void *addr) |
914 | { | |
5e376613 | 915 | struct module *modaddr; |
7d1d16e4 | 916 | unsigned long a = (unsigned long)dereference_function_descriptor(addr); |
1da177e4 | 917 | |
7d1d16e4 | 918 | if (core_kernel_text(a)) |
5e376613 | 919 | return; |
1da177e4 | 920 | |
a6e6abd5 RR |
921 | /* module_text_address is safe here: we're supposed to have reference |
922 | * to module from symbol_get, so it can't go away. */ | |
7d1d16e4 | 923 | modaddr = __module_text_address(a); |
a6e6abd5 | 924 | BUG_ON(!modaddr); |
5e376613 | 925 | module_put(modaddr); |
1da177e4 LT |
926 | } |
927 | EXPORT_SYMBOL_GPL(symbol_put_addr); | |
928 | ||
929 | static ssize_t show_refcnt(struct module_attribute *mattr, | |
930 | struct module *mod, char *buffer) | |
931 | { | |
256e2fdf | 932 | return sprintf(buffer, "%u\n", module_refcount(mod)); |
1da177e4 LT |
933 | } |
934 | ||
935 | static struct module_attribute refcnt = { | |
7b595756 | 936 | .attr = { .name = "refcnt", .mode = 0444 }, |
1da177e4 LT |
937 | .show = show_refcnt, |
938 | }; | |
939 | ||
f6a57033 AV |
940 | void module_put(struct module *module) |
941 | { | |
942 | if (module) { | |
943 | unsigned int cpu = get_cpu(); | |
720eba31 | 944 | local_dec(__module_ref_addr(module, cpu)); |
f6a57033 AV |
945 | /* Maybe they're waiting for us to drop reference? */ |
946 | if (unlikely(!module_is_live(module))) | |
947 | wake_up_process(module->waiter); | |
948 | put_cpu(); | |
949 | } | |
950 | } | |
951 | EXPORT_SYMBOL(module_put); | |
952 | ||
1da177e4 | 953 | #else /* !CONFIG_MODULE_UNLOAD */ |
d1e99d7a | 954 | static inline void print_unload_info(struct seq_file *m, struct module *mod) |
1da177e4 LT |
955 | { |
956 | /* We don't know the usage count, or what modules are using. */ | |
957 | seq_printf(m, " - -"); | |
958 | } | |
959 | ||
960 | static inline void module_unload_free(struct module *mod) | |
961 | { | |
962 | } | |
963 | ||
c6b37801 | 964 | int use_module(struct module *a, struct module *b) |
1da177e4 | 965 | { |
c9a3ba55 | 966 | return strong_try_module_get(b) == 0; |
1da177e4 | 967 | } |
c6b37801 | 968 | EXPORT_SYMBOL_GPL(use_module); |
1da177e4 LT |
969 | |
970 | static inline void module_unload_init(struct module *mod) | |
971 | { | |
972 | } | |
973 | #endif /* CONFIG_MODULE_UNLOAD */ | |
974 | ||
1f71740a KS |
975 | static ssize_t show_initstate(struct module_attribute *mattr, |
976 | struct module *mod, char *buffer) | |
977 | { | |
978 | const char *state = "unknown"; | |
979 | ||
980 | switch (mod->state) { | |
981 | case MODULE_STATE_LIVE: | |
982 | state = "live"; | |
983 | break; | |
984 | case MODULE_STATE_COMING: | |
985 | state = "coming"; | |
986 | break; | |
987 | case MODULE_STATE_GOING: | |
988 | state = "going"; | |
989 | break; | |
990 | } | |
991 | return sprintf(buffer, "%s\n", state); | |
992 | } | |
993 | ||
994 | static struct module_attribute initstate = { | |
7b595756 | 995 | .attr = { .name = "initstate", .mode = 0444 }, |
1f71740a KS |
996 | .show = show_initstate, |
997 | }; | |
998 | ||
03e88ae1 GKH |
999 | static struct module_attribute *modinfo_attrs[] = { |
1000 | &modinfo_version, | |
1001 | &modinfo_srcversion, | |
1f71740a | 1002 | &initstate, |
03e88ae1 GKH |
1003 | #ifdef CONFIG_MODULE_UNLOAD |
1004 | &refcnt, | |
1005 | #endif | |
1006 | NULL, | |
1007 | }; | |
1008 | ||
1da177e4 LT |
1009 | static const char vermagic[] = VERMAGIC_STRING; |
1010 | ||
c6e665c8 | 1011 | static int try_to_force_load(struct module *mod, const char *reason) |
826e4506 LT |
1012 | { |
1013 | #ifdef CONFIG_MODULE_FORCE_LOAD | |
25ddbb18 | 1014 | if (!test_taint(TAINT_FORCED_MODULE)) |
c6e665c8 RR |
1015 | printk(KERN_WARNING "%s: %s: kernel tainted.\n", |
1016 | mod->name, reason); | |
826e4506 LT |
1017 | add_taint_module(mod, TAINT_FORCED_MODULE); |
1018 | return 0; | |
1019 | #else | |
1020 | return -ENOEXEC; | |
1021 | #endif | |
1022 | } | |
1023 | ||
1da177e4 LT |
1024 | #ifdef CONFIG_MODVERSIONS |
1025 | static int check_version(Elf_Shdr *sechdrs, | |
1026 | unsigned int versindex, | |
1027 | const char *symname, | |
1028 | struct module *mod, | |
1029 | const unsigned long *crc) | |
1030 | { | |
1031 | unsigned int i, num_versions; | |
1032 | struct modversion_info *versions; | |
1033 | ||
1034 | /* Exporting module didn't supply crcs? OK, we're already tainted. */ | |
1035 | if (!crc) | |
1036 | return 1; | |
1037 | ||
a5dd6970 RR |
1038 | /* No versions at all? modprobe --force does this. */ |
1039 | if (versindex == 0) | |
1040 | return try_to_force_load(mod, symname) == 0; | |
1041 | ||
1da177e4 LT |
1042 | versions = (void *) sechdrs[versindex].sh_addr; |
1043 | num_versions = sechdrs[versindex].sh_size | |
1044 | / sizeof(struct modversion_info); | |
1045 | ||
1046 | for (i = 0; i < num_versions; i++) { | |
1047 | if (strcmp(versions[i].name, symname) != 0) | |
1048 | continue; | |
1049 | ||
1050 | if (versions[i].crc == *crc) | |
1051 | return 1; | |
1da177e4 LT |
1052 | DEBUGP("Found checksum %lX vs module %lX\n", |
1053 | *crc, versions[i].crc); | |
826e4506 | 1054 | goto bad_version; |
1da177e4 | 1055 | } |
826e4506 | 1056 | |
a5dd6970 RR |
1057 | printk(KERN_WARNING "%s: no symbol version for %s\n", |
1058 | mod->name, symname); | |
1059 | return 0; | |
826e4506 LT |
1060 | |
1061 | bad_version: | |
1062 | printk("%s: disagrees about version of symbol %s\n", | |
1063 | mod->name, symname); | |
1064 | return 0; | |
1da177e4 LT |
1065 | } |
1066 | ||
1067 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, | |
1068 | unsigned int versindex, | |
1069 | struct module *mod) | |
1070 | { | |
1071 | const unsigned long *crc; | |
1da177e4 | 1072 | |
6560dc16 MF |
1073 | if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, |
1074 | &crc, true, false)) | |
1da177e4 | 1075 | BUG(); |
8c8ef42a | 1076 | return check_version(sechdrs, versindex, "module_layout", mod, crc); |
1da177e4 LT |
1077 | } |
1078 | ||
91e37a79 RR |
1079 | /* First part is kernel version, which we ignore if module has crcs. */ |
1080 | static inline int same_magic(const char *amagic, const char *bmagic, | |
1081 | bool has_crcs) | |
1da177e4 | 1082 | { |
91e37a79 RR |
1083 | if (has_crcs) { |
1084 | amagic += strcspn(amagic, " "); | |
1085 | bmagic += strcspn(bmagic, " "); | |
1086 | } | |
1da177e4 LT |
1087 | return strcmp(amagic, bmagic) == 0; |
1088 | } | |
1089 | #else | |
1090 | static inline int check_version(Elf_Shdr *sechdrs, | |
1091 | unsigned int versindex, | |
1092 | const char *symname, | |
1093 | struct module *mod, | |
1094 | const unsigned long *crc) | |
1095 | { | |
1096 | return 1; | |
1097 | } | |
1098 | ||
1099 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, | |
1100 | unsigned int versindex, | |
1101 | struct module *mod) | |
1102 | { | |
1103 | return 1; | |
1104 | } | |
1105 | ||
91e37a79 RR |
1106 | static inline int same_magic(const char *amagic, const char *bmagic, |
1107 | bool has_crcs) | |
1da177e4 LT |
1108 | { |
1109 | return strcmp(amagic, bmagic) == 0; | |
1110 | } | |
1111 | #endif /* CONFIG_MODVERSIONS */ | |
1112 | ||
1113 | /* Resolve a symbol for this module. I.e. if we find one, record usage. | |
1114 | Must be holding module_mutex. */ | |
414fd31b TA |
1115 | static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, |
1116 | unsigned int versindex, | |
1117 | const char *name, | |
1118 | struct module *mod) | |
1da177e4 LT |
1119 | { |
1120 | struct module *owner; | |
414fd31b | 1121 | const struct kernel_symbol *sym; |
1da177e4 LT |
1122 | const unsigned long *crc; |
1123 | ||
414fd31b | 1124 | sym = find_symbol(name, &owner, &crc, |
25ddbb18 | 1125 | !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); |
414fd31b TA |
1126 | /* use_module can fail due to OOM, |
1127 | or module initialization or unloading */ | |
1128 | if (sym) { | |
1da177e4 LT |
1129 | if (!check_version(sechdrs, versindex, name, mod, crc) || |
1130 | !use_module(mod, owner)) | |
414fd31b | 1131 | sym = NULL; |
1da177e4 | 1132 | } |
414fd31b | 1133 | return sym; |
1da177e4 LT |
1134 | } |
1135 | ||
1da177e4 LT |
1136 | /* |
1137 | * /sys/module/foo/sections stuff | |
1138 | * J. Corbet <corbet@lwn.net> | |
1139 | */ | |
120fc3d7 | 1140 | #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS) |
a58730c4 RR |
1141 | struct module_sect_attr |
1142 | { | |
1143 | struct module_attribute mattr; | |
1144 | char *name; | |
1145 | unsigned long address; | |
1146 | }; | |
1147 | ||
1148 | struct module_sect_attrs | |
1149 | { | |
1150 | struct attribute_group grp; | |
1151 | unsigned int nsections; | |
1152 | struct module_sect_attr attrs[0]; | |
1153 | }; | |
1154 | ||
1da177e4 LT |
1155 | static ssize_t module_sect_show(struct module_attribute *mattr, |
1156 | struct module *mod, char *buf) | |
1157 | { | |
1158 | struct module_sect_attr *sattr = | |
1159 | container_of(mattr, struct module_sect_attr, mattr); | |
1160 | return sprintf(buf, "0x%lx\n", sattr->address); | |
1161 | } | |
1162 | ||
04b1db9f IN |
1163 | static void free_sect_attrs(struct module_sect_attrs *sect_attrs) |
1164 | { | |
a58730c4 | 1165 | unsigned int section; |
04b1db9f IN |
1166 | |
1167 | for (section = 0; section < sect_attrs->nsections; section++) | |
1168 | kfree(sect_attrs->attrs[section].name); | |
1169 | kfree(sect_attrs); | |
1170 | } | |
1171 | ||
1da177e4 LT |
1172 | static void add_sect_attrs(struct module *mod, unsigned int nsect, |
1173 | char *secstrings, Elf_Shdr *sechdrs) | |
1174 | { | |
1175 | unsigned int nloaded = 0, i, size[2]; | |
1176 | struct module_sect_attrs *sect_attrs; | |
1177 | struct module_sect_attr *sattr; | |
1178 | struct attribute **gattr; | |
22a8bdeb | 1179 | |
1da177e4 LT |
1180 | /* Count loaded sections and allocate structures */ |
1181 | for (i = 0; i < nsect; i++) | |
1182 | if (sechdrs[i].sh_flags & SHF_ALLOC) | |
1183 | nloaded++; | |
1184 | size[0] = ALIGN(sizeof(*sect_attrs) | |
1185 | + nloaded * sizeof(sect_attrs->attrs[0]), | |
1186 | sizeof(sect_attrs->grp.attrs[0])); | |
1187 | size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); | |
04b1db9f IN |
1188 | sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); |
1189 | if (sect_attrs == NULL) | |
1da177e4 LT |
1190 | return; |
1191 | ||
1192 | /* Setup section attributes. */ | |
1193 | sect_attrs->grp.name = "sections"; | |
1194 | sect_attrs->grp.attrs = (void *)sect_attrs + size[0]; | |
1195 | ||
04b1db9f | 1196 | sect_attrs->nsections = 0; |
1da177e4 LT |
1197 | sattr = §_attrs->attrs[0]; |
1198 | gattr = §_attrs->grp.attrs[0]; | |
1199 | for (i = 0; i < nsect; i++) { | |
1200 | if (! (sechdrs[i].sh_flags & SHF_ALLOC)) | |
1201 | continue; | |
1202 | sattr->address = sechdrs[i].sh_addr; | |
04b1db9f IN |
1203 | sattr->name = kstrdup(secstrings + sechdrs[i].sh_name, |
1204 | GFP_KERNEL); | |
1205 | if (sattr->name == NULL) | |
1206 | goto out; | |
1207 | sect_attrs->nsections++; | |
1da177e4 LT |
1208 | sattr->mattr.show = module_sect_show; |
1209 | sattr->mattr.store = NULL; | |
1210 | sattr->mattr.attr.name = sattr->name; | |
1da177e4 LT |
1211 | sattr->mattr.attr.mode = S_IRUGO; |
1212 | *(gattr++) = &(sattr++)->mattr.attr; | |
1213 | } | |
1214 | *gattr = NULL; | |
1215 | ||
1216 | if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) | |
1217 | goto out; | |
1218 | ||
1219 | mod->sect_attrs = sect_attrs; | |
1220 | return; | |
1221 | out: | |
04b1db9f | 1222 | free_sect_attrs(sect_attrs); |
1da177e4 LT |
1223 | } |
1224 | ||
1225 | static void remove_sect_attrs(struct module *mod) | |
1226 | { | |
1227 | if (mod->sect_attrs) { | |
1228 | sysfs_remove_group(&mod->mkobj.kobj, | |
1229 | &mod->sect_attrs->grp); | |
1230 | /* We are positive that no one is using any sect attrs | |
1231 | * at this point. Deallocate immediately. */ | |
04b1db9f | 1232 | free_sect_attrs(mod->sect_attrs); |
1da177e4 LT |
1233 | mod->sect_attrs = NULL; |
1234 | } | |
1235 | } | |
1236 | ||
6d760133 RM |
1237 | /* |
1238 | * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. | |
1239 | */ | |
1240 | ||
1241 | struct module_notes_attrs { | |
1242 | struct kobject *dir; | |
1243 | unsigned int notes; | |
1244 | struct bin_attribute attrs[0]; | |
1245 | }; | |
1246 | ||
1247 | static ssize_t module_notes_read(struct kobject *kobj, | |
1248 | struct bin_attribute *bin_attr, | |
1249 | char *buf, loff_t pos, size_t count) | |
1250 | { | |
1251 | /* | |
1252 | * The caller checked the pos and count against our size. | |
1253 | */ | |
1254 | memcpy(buf, bin_attr->private + pos, count); | |
1255 | return count; | |
1256 | } | |
1257 | ||
1258 | static void free_notes_attrs(struct module_notes_attrs *notes_attrs, | |
1259 | unsigned int i) | |
1260 | { | |
1261 | if (notes_attrs->dir) { | |
1262 | while (i-- > 0) | |
1263 | sysfs_remove_bin_file(notes_attrs->dir, | |
1264 | ¬es_attrs->attrs[i]); | |
e9432093 | 1265 | kobject_put(notes_attrs->dir); |
6d760133 RM |
1266 | } |
1267 | kfree(notes_attrs); | |
1268 | } | |
1269 | ||
1270 | static void add_notes_attrs(struct module *mod, unsigned int nsect, | |
1271 | char *secstrings, Elf_Shdr *sechdrs) | |
1272 | { | |
1273 | unsigned int notes, loaded, i; | |
1274 | struct module_notes_attrs *notes_attrs; | |
1275 | struct bin_attribute *nattr; | |
1276 | ||
ea6bff36 IM |
1277 | /* failed to create section attributes, so can't create notes */ |
1278 | if (!mod->sect_attrs) | |
1279 | return; | |
1280 | ||
6d760133 RM |
1281 | /* Count notes sections and allocate structures. */ |
1282 | notes = 0; | |
1283 | for (i = 0; i < nsect; i++) | |
1284 | if ((sechdrs[i].sh_flags & SHF_ALLOC) && | |
1285 | (sechdrs[i].sh_type == SHT_NOTE)) | |
1286 | ++notes; | |
1287 | ||
1288 | if (notes == 0) | |
1289 | return; | |
1290 | ||
1291 | notes_attrs = kzalloc(sizeof(*notes_attrs) | |
1292 | + notes * sizeof(notes_attrs->attrs[0]), | |
1293 | GFP_KERNEL); | |
1294 | if (notes_attrs == NULL) | |
1295 | return; | |
1296 | ||
1297 | notes_attrs->notes = notes; | |
1298 | nattr = ¬es_attrs->attrs[0]; | |
1299 | for (loaded = i = 0; i < nsect; ++i) { | |
1300 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | |
1301 | continue; | |
1302 | if (sechdrs[i].sh_type == SHT_NOTE) { | |
1303 | nattr->attr.name = mod->sect_attrs->attrs[loaded].name; | |
1304 | nattr->attr.mode = S_IRUGO; | |
1305 | nattr->size = sechdrs[i].sh_size; | |
1306 | nattr->private = (void *) sechdrs[i].sh_addr; | |
1307 | nattr->read = module_notes_read; | |
1308 | ++nattr; | |
1309 | } | |
1310 | ++loaded; | |
1311 | } | |
1312 | ||
4ff6abff | 1313 | notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj); |
6d760133 RM |
1314 | if (!notes_attrs->dir) |
1315 | goto out; | |
1316 | ||
1317 | for (i = 0; i < notes; ++i) | |
1318 | if (sysfs_create_bin_file(notes_attrs->dir, | |
1319 | ¬es_attrs->attrs[i])) | |
1320 | goto out; | |
1321 | ||
1322 | mod->notes_attrs = notes_attrs; | |
1323 | return; | |
1324 | ||
1325 | out: | |
1326 | free_notes_attrs(notes_attrs, i); | |
1327 | } | |
1328 | ||
1329 | static void remove_notes_attrs(struct module *mod) | |
1330 | { | |
1331 | if (mod->notes_attrs) | |
1332 | free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); | |
1333 | } | |
1334 | ||
1da177e4 | 1335 | #else |
04b1db9f | 1336 | |
1da177e4 LT |
1337 | static inline void add_sect_attrs(struct module *mod, unsigned int nsect, |
1338 | char *sectstrings, Elf_Shdr *sechdrs) | |
1339 | { | |
1340 | } | |
1341 | ||
1342 | static inline void remove_sect_attrs(struct module *mod) | |
1343 | { | |
1344 | } | |
6d760133 RM |
1345 | |
1346 | static inline void add_notes_attrs(struct module *mod, unsigned int nsect, | |
1347 | char *sectstrings, Elf_Shdr *sechdrs) | |
1348 | { | |
1349 | } | |
1350 | ||
1351 | static inline void remove_notes_attrs(struct module *mod) | |
1352 | { | |
1353 | } | |
120fc3d7 | 1354 | #endif |
1da177e4 | 1355 | |
ef665c1a RD |
1356 | #ifdef CONFIG_SYSFS |
1357 | int module_add_modinfo_attrs(struct module *mod) | |
c988d2b2 MD |
1358 | { |
1359 | struct module_attribute *attr; | |
03e88ae1 | 1360 | struct module_attribute *temp_attr; |
c988d2b2 MD |
1361 | int error = 0; |
1362 | int i; | |
1363 | ||
03e88ae1 GKH |
1364 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * |
1365 | (ARRAY_SIZE(modinfo_attrs) + 1)), | |
1366 | GFP_KERNEL); | |
1367 | if (!mod->modinfo_attrs) | |
1368 | return -ENOMEM; | |
1369 | ||
1370 | temp_attr = mod->modinfo_attrs; | |
c988d2b2 MD |
1371 | for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { |
1372 | if (!attr->test || | |
03e88ae1 GKH |
1373 | (attr->test && attr->test(mod))) { |
1374 | memcpy(temp_attr, attr, sizeof(*temp_attr)); | |
03e88ae1 GKH |
1375 | error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); |
1376 | ++temp_attr; | |
1377 | } | |
c988d2b2 MD |
1378 | } |
1379 | return error; | |
1380 | } | |
1381 | ||
ef665c1a | 1382 | void module_remove_modinfo_attrs(struct module *mod) |
c988d2b2 MD |
1383 | { |
1384 | struct module_attribute *attr; | |
1385 | int i; | |
1386 | ||
03e88ae1 GKH |
1387 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { |
1388 | /* pick a field to test for end of list */ | |
1389 | if (!attr->attr.name) | |
1390 | break; | |
c988d2b2 | 1391 | sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); |
03e88ae1 GKH |
1392 | if (attr->free) |
1393 | attr->free(mod); | |
c988d2b2 | 1394 | } |
03e88ae1 | 1395 | kfree(mod->modinfo_attrs); |
c988d2b2 | 1396 | } |
1da177e4 | 1397 | |
ef665c1a | 1398 | int mod_sysfs_init(struct module *mod) |
1da177e4 LT |
1399 | { |
1400 | int err; | |
6494a93d | 1401 | struct kobject *kobj; |
1da177e4 | 1402 | |
823bccfc GKH |
1403 | if (!module_sysfs_initialized) { |
1404 | printk(KERN_ERR "%s: module sysfs not initialized\n", | |
1cc5f714 ES |
1405 | mod->name); |
1406 | err = -EINVAL; | |
1407 | goto out; | |
1408 | } | |
6494a93d GKH |
1409 | |
1410 | kobj = kset_find_obj(module_kset, mod->name); | |
1411 | if (kobj) { | |
1412 | printk(KERN_ERR "%s: module is already loaded\n", mod->name); | |
1413 | kobject_put(kobj); | |
1414 | err = -EINVAL; | |
1415 | goto out; | |
1416 | } | |
1417 | ||
1da177e4 | 1418 | mod->mkobj.mod = mod; |
e17e0f51 | 1419 | |
ac3c8141 GKH |
1420 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); |
1421 | mod->mkobj.kobj.kset = module_kset; | |
1422 | err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, | |
1423 | "%s", mod->name); | |
1424 | if (err) | |
1425 | kobject_put(&mod->mkobj.kobj); | |
270a6c4c | 1426 | |
97c146ef | 1427 | /* delay uevent until full sysfs population */ |
270a6c4c KS |
1428 | out: |
1429 | return err; | |
1430 | } | |
1431 | ||
ef665c1a | 1432 | int mod_sysfs_setup(struct module *mod, |
270a6c4c KS |
1433 | struct kernel_param *kparam, |
1434 | unsigned int num_params) | |
1435 | { | |
1436 | int err; | |
1437 | ||
4ff6abff | 1438 | mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); |
240936e1 AM |
1439 | if (!mod->holders_dir) { |
1440 | err = -ENOMEM; | |
270a6c4c | 1441 | goto out_unreg; |
240936e1 | 1442 | } |
270a6c4c | 1443 | |
1da177e4 LT |
1444 | err = module_param_sysfs_setup(mod, kparam, num_params); |
1445 | if (err) | |
270a6c4c | 1446 | goto out_unreg_holders; |
1da177e4 | 1447 | |
c988d2b2 MD |
1448 | err = module_add_modinfo_attrs(mod); |
1449 | if (err) | |
e17e0f51 | 1450 | goto out_unreg_param; |
c988d2b2 | 1451 | |
e17e0f51 | 1452 | kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); |
1da177e4 LT |
1453 | return 0; |
1454 | ||
e17e0f51 KS |
1455 | out_unreg_param: |
1456 | module_param_sysfs_remove(mod); | |
270a6c4c | 1457 | out_unreg_holders: |
78a2d906 | 1458 | kobject_put(mod->holders_dir); |
270a6c4c | 1459 | out_unreg: |
e17e0f51 | 1460 | kobject_put(&mod->mkobj.kobj); |
1da177e4 LT |
1461 | return err; |
1462 | } | |
34e4e2fe DL |
1463 | |
1464 | static void mod_sysfs_fini(struct module *mod) | |
1465 | { | |
1466 | kobject_put(&mod->mkobj.kobj); | |
1467 | } | |
1468 | ||
1469 | #else /* CONFIG_SYSFS */ | |
1470 | ||
1471 | static void mod_sysfs_fini(struct module *mod) | |
1472 | { | |
1473 | } | |
1474 | ||
1475 | #endif /* CONFIG_SYSFS */ | |
1da177e4 LT |
1476 | |
1477 | static void mod_kobject_remove(struct module *mod) | |
1478 | { | |
c988d2b2 | 1479 | module_remove_modinfo_attrs(mod); |
1da177e4 | 1480 | module_param_sysfs_remove(mod); |
78a2d906 GKH |
1481 | kobject_put(mod->mkobj.drivers_dir); |
1482 | kobject_put(mod->holders_dir); | |
34e4e2fe | 1483 | mod_sysfs_fini(mod); |
1da177e4 LT |
1484 | } |
1485 | ||
1486 | /* | |
1487 | * unlink the module with the whole machine is stopped with interrupts off | |
1488 | * - this defends against kallsyms not taking locks | |
1489 | */ | |
1490 | static int __unlink_module(void *_mod) | |
1491 | { | |
1492 | struct module *mod = _mod; | |
1493 | list_del(&mod->list); | |
1494 | return 0; | |
1495 | } | |
1496 | ||
02a3e59a | 1497 | /* Free a module, remove from lists, etc (must hold module_mutex). */ |
1da177e4 LT |
1498 | static void free_module(struct module *mod) |
1499 | { | |
1500 | /* Delete from various lists */ | |
9b1a4d38 | 1501 | stop_machine(__unlink_module, mod, NULL); |
6d760133 | 1502 | remove_notes_attrs(mod); |
1da177e4 LT |
1503 | remove_sect_attrs(mod); |
1504 | mod_kobject_remove(mod); | |
1505 | ||
1506 | /* Arch-specific cleanup. */ | |
1507 | module_arch_cleanup(mod); | |
1508 | ||
1509 | /* Module unload stuff */ | |
1510 | module_unload_free(mod); | |
1511 | ||
e180a6b7 RR |
1512 | /* Free any allocated parameters. */ |
1513 | destroy_params(mod->kp, mod->num_kp); | |
1514 | ||
1da177e4 LT |
1515 | /* This may be NULL, but that's OK */ |
1516 | module_free(mod, mod->module_init); | |
1517 | kfree(mod->args); | |
1518 | if (mod->percpu) | |
1519 | percpu_modfree(mod->percpu); | |
720eba31 ED |
1520 | #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) |
1521 | if (mod->refptr) | |
1522 | percpu_modfree(mod->refptr); | |
1523 | #endif | |
fbb9ce95 IM |
1524 | /* Free lock-classes: */ |
1525 | lockdep_free_key_range(mod->module_core, mod->core_size); | |
1526 | ||
1da177e4 LT |
1527 | /* Finally, free the core (containing the module structure) */ |
1528 | module_free(mod, mod->module_core); | |
1529 | } | |
1530 | ||
1531 | void *__symbol_get(const char *symbol) | |
1532 | { | |
1533 | struct module *owner; | |
414fd31b | 1534 | const struct kernel_symbol *sym; |
1da177e4 | 1535 | |
24da1cbf | 1536 | preempt_disable(); |
414fd31b TA |
1537 | sym = find_symbol(symbol, &owner, NULL, true, true); |
1538 | if (sym && strong_try_module_get(owner)) | |
1539 | sym = NULL; | |
24da1cbf | 1540 | preempt_enable(); |
1da177e4 | 1541 | |
414fd31b | 1542 | return sym ? (void *)sym->value : NULL; |
1da177e4 LT |
1543 | } |
1544 | EXPORT_SYMBOL_GPL(__symbol_get); | |
1545 | ||
eea8b54d AN |
1546 | /* |
1547 | * Ensure that an exported symbol [global namespace] does not already exist | |
02a3e59a | 1548 | * in the kernel or in some other module's exported symbol table. |
eea8b54d AN |
1549 | */ |
1550 | static int verify_export_symbols(struct module *mod) | |
1551 | { | |
b211104d | 1552 | unsigned int i; |
eea8b54d | 1553 | struct module *owner; |
b211104d RR |
1554 | const struct kernel_symbol *s; |
1555 | struct { | |
1556 | const struct kernel_symbol *sym; | |
1557 | unsigned int num; | |
1558 | } arr[] = { | |
1559 | { mod->syms, mod->num_syms }, | |
1560 | { mod->gpl_syms, mod->num_gpl_syms }, | |
1561 | { mod->gpl_future_syms, mod->num_gpl_future_syms }, | |
f7f5b675 | 1562 | #ifdef CONFIG_UNUSED_SYMBOLS |
b211104d RR |
1563 | { mod->unused_syms, mod->num_unused_syms }, |
1564 | { mod->unused_gpl_syms, mod->num_unused_gpl_syms }, | |
f7f5b675 | 1565 | #endif |
b211104d | 1566 | }; |
eea8b54d | 1567 | |
b211104d RR |
1568 | for (i = 0; i < ARRAY_SIZE(arr); i++) { |
1569 | for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { | |
414fd31b | 1570 | if (find_symbol(s->name, &owner, NULL, true, false)) { |
b211104d RR |
1571 | printk(KERN_ERR |
1572 | "%s: exports duplicate symbol %s" | |
1573 | " (owned by %s)\n", | |
1574 | mod->name, s->name, module_name(owner)); | |
1575 | return -ENOEXEC; | |
1576 | } | |
eea8b54d | 1577 | } |
b211104d RR |
1578 | } |
1579 | return 0; | |
eea8b54d AN |
1580 | } |
1581 | ||
9a4b9708 | 1582 | /* Change all symbols so that st_value encodes the pointer directly. */ |
1da177e4 LT |
1583 | static int simplify_symbols(Elf_Shdr *sechdrs, |
1584 | unsigned int symindex, | |
1585 | const char *strtab, | |
1586 | unsigned int versindex, | |
1587 | unsigned int pcpuindex, | |
1588 | struct module *mod) | |
1589 | { | |
1590 | Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr; | |
1591 | unsigned long secbase; | |
1592 | unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); | |
1593 | int ret = 0; | |
414fd31b | 1594 | const struct kernel_symbol *ksym; |
1da177e4 LT |
1595 | |
1596 | for (i = 1; i < n; i++) { | |
1597 | switch (sym[i].st_shndx) { | |
1598 | case SHN_COMMON: | |
1599 | /* We compiled with -fno-common. These are not | |
1600 | supposed to happen. */ | |
1601 | DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name); | |
1602 | printk("%s: please compile with -fno-common\n", | |
1603 | mod->name); | |
1604 | ret = -ENOEXEC; | |
1605 | break; | |
1606 | ||
1607 | case SHN_ABS: | |
1608 | /* Don't need to do anything */ | |
1609 | DEBUGP("Absolute symbol: 0x%08lx\n", | |
1610 | (long)sym[i].st_value); | |
1611 | break; | |
1612 | ||
1613 | case SHN_UNDEF: | |
414fd31b TA |
1614 | ksym = resolve_symbol(sechdrs, versindex, |
1615 | strtab + sym[i].st_name, mod); | |
1da177e4 | 1616 | /* Ok if resolved. */ |
414fd31b TA |
1617 | if (ksym) { |
1618 | sym[i].st_value = ksym->value; | |
1da177e4 | 1619 | break; |
414fd31b TA |
1620 | } |
1621 | ||
1da177e4 LT |
1622 | /* Ok if weak. */ |
1623 | if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) | |
1624 | break; | |
1625 | ||
1626 | printk(KERN_WARNING "%s: Unknown symbol %s\n", | |
1627 | mod->name, strtab + sym[i].st_name); | |
1628 | ret = -ENOENT; | |
1629 | break; | |
1630 | ||
1631 | default: | |
1632 | /* Divert to percpu allocation if a percpu var. */ | |
1633 | if (sym[i].st_shndx == pcpuindex) | |
1634 | secbase = (unsigned long)mod->percpu; | |
1635 | else | |
1636 | secbase = sechdrs[sym[i].st_shndx].sh_addr; | |
1637 | sym[i].st_value += secbase; | |
1638 | break; | |
1639 | } | |
1640 | } | |
1641 | ||
1642 | return ret; | |
1643 | } | |
1644 | ||
088af9a6 HD |
1645 | /* Additional bytes needed by arch in front of individual sections */ |
1646 | unsigned int __weak arch_mod_section_prepend(struct module *mod, | |
1647 | unsigned int section) | |
1648 | { | |
1649 | /* default implementation just returns zero */ | |
1650 | return 0; | |
1651 | } | |
1652 | ||
1da177e4 | 1653 | /* Update size with this section: return offset. */ |
088af9a6 HD |
1654 | static long get_offset(struct module *mod, unsigned int *size, |
1655 | Elf_Shdr *sechdr, unsigned int section) | |
1da177e4 LT |
1656 | { |
1657 | long ret; | |
1658 | ||
088af9a6 | 1659 | *size += arch_mod_section_prepend(mod, section); |
1da177e4 LT |
1660 | ret = ALIGN(*size, sechdr->sh_addralign ?: 1); |
1661 | *size = ret + sechdr->sh_size; | |
1662 | return ret; | |
1663 | } | |
1664 | ||
1665 | /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld | |
1666 | might -- code, read-only data, read-write data, small data. Tally | |
1667 | sizes, and place the offsets into sh_entsize fields: high bit means it | |
1668 | belongs in init. */ | |
1669 | static void layout_sections(struct module *mod, | |
1670 | const Elf_Ehdr *hdr, | |
1671 | Elf_Shdr *sechdrs, | |
1672 | const char *secstrings) | |
1673 | { | |
1674 | static unsigned long const masks[][2] = { | |
1675 | /* NOTE: all executable code must be the first section | |
1676 | * in this array; otherwise modify the text_size | |
1677 | * finder in the two loops below */ | |
1678 | { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, | |
1679 | { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, | |
1680 | { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, | |
1681 | { ARCH_SHF_SMALL | SHF_ALLOC, 0 } | |
1682 | }; | |
1683 | unsigned int m, i; | |
1684 | ||
1685 | for (i = 0; i < hdr->e_shnum; i++) | |
1686 | sechdrs[i].sh_entsize = ~0UL; | |
1687 | ||
1688 | DEBUGP("Core section allocation order:\n"); | |
1689 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { | |
1690 | for (i = 0; i < hdr->e_shnum; ++i) { | |
1691 | Elf_Shdr *s = &sechdrs[i]; | |
1692 | ||
1693 | if ((s->sh_flags & masks[m][0]) != masks[m][0] | |
1694 | || (s->sh_flags & masks[m][1]) | |
1695 | || s->sh_entsize != ~0UL | |
49502677 | 1696 | || strstarts(secstrings + s->sh_name, ".init")) |
1da177e4 | 1697 | continue; |
088af9a6 | 1698 | s->sh_entsize = get_offset(mod, &mod->core_size, s, i); |
1da177e4 LT |
1699 | DEBUGP("\t%s\n", secstrings + s->sh_name); |
1700 | } | |
1701 | if (m == 0) | |
1702 | mod->core_text_size = mod->core_size; | |
1703 | } | |
1704 | ||
1705 | DEBUGP("Init section allocation order:\n"); | |
1706 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { | |
1707 | for (i = 0; i < hdr->e_shnum; ++i) { | |
1708 | Elf_Shdr *s = &sechdrs[i]; | |
1709 | ||
1710 | if ((s->sh_flags & masks[m][0]) != masks[m][0] | |
1711 | || (s->sh_flags & masks[m][1]) | |
1712 | || s->sh_entsize != ~0UL | |
49502677 | 1713 | || !strstarts(secstrings + s->sh_name, ".init")) |
1da177e4 | 1714 | continue; |
088af9a6 | 1715 | s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) |
1da177e4 LT |
1716 | | INIT_OFFSET_MASK); |
1717 | DEBUGP("\t%s\n", secstrings + s->sh_name); | |
1718 | } | |
1719 | if (m == 0) | |
1720 | mod->init_text_size = mod->init_size; | |
1721 | } | |
1722 | } | |
1723 | ||
1da177e4 LT |
1724 | static void set_license(struct module *mod, const char *license) |
1725 | { | |
1726 | if (!license) | |
1727 | license = "unspecified"; | |
1728 | ||
fa3ba2e8 | 1729 | if (!license_is_gpl_compatible(license)) { |
25ddbb18 | 1730 | if (!test_taint(TAINT_PROPRIETARY_MODULE)) |
1d4d2627 | 1731 | printk(KERN_WARNING "%s: module license '%s' taints " |
fa3ba2e8 FM |
1732 | "kernel.\n", mod->name, license); |
1733 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | |
1da177e4 LT |
1734 | } |
1735 | } | |
1736 | ||
1737 | /* Parse tag=value strings from .modinfo section */ | |
1738 | static char *next_string(char *string, unsigned long *secsize) | |
1739 | { | |
1740 | /* Skip non-zero chars */ | |
1741 | while (string[0]) { | |
1742 | string++; | |
1743 | if ((*secsize)-- <= 1) | |
1744 | return NULL; | |
1745 | } | |
1746 | ||
1747 | /* Skip any zero padding. */ | |
1748 | while (!string[0]) { | |
1749 | string++; | |
1750 | if ((*secsize)-- <= 1) | |
1751 | return NULL; | |
1752 | } | |
1753 | return string; | |
1754 | } | |
1755 | ||
1756 | static char *get_modinfo(Elf_Shdr *sechdrs, | |
1757 | unsigned int info, | |
1758 | const char *tag) | |
1759 | { | |
1760 | char *p; | |
1761 | unsigned int taglen = strlen(tag); | |
1762 | unsigned long size = sechdrs[info].sh_size; | |
1763 | ||
1764 | for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) { | |
1765 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') | |
1766 | return p + taglen + 1; | |
1767 | } | |
1768 | return NULL; | |
1769 | } | |
1770 | ||
c988d2b2 MD |
1771 | static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, |
1772 | unsigned int infoindex) | |
1773 | { | |
1774 | struct module_attribute *attr; | |
1775 | int i; | |
1776 | ||
1777 | for (i = 0; (attr = modinfo_attrs[i]); i++) { | |
1778 | if (attr->setup) | |
1779 | attr->setup(mod, | |
1780 | get_modinfo(sechdrs, | |
1781 | infoindex, | |
1782 | attr->attr.name)); | |
1783 | } | |
1784 | } | |
c988d2b2 | 1785 | |
1da177e4 | 1786 | #ifdef CONFIG_KALLSYMS |
15bba37d WC |
1787 | |
1788 | /* lookup symbol in given range of kernel_symbols */ | |
1789 | static const struct kernel_symbol *lookup_symbol(const char *name, | |
1790 | const struct kernel_symbol *start, | |
1791 | const struct kernel_symbol *stop) | |
1792 | { | |
1793 | const struct kernel_symbol *ks = start; | |
1794 | for (; ks < stop; ks++) | |
1795 | if (strcmp(ks->name, name) == 0) | |
1796 | return ks; | |
1797 | return NULL; | |
1798 | } | |
1799 | ||
ca4787b7 TA |
1800 | static int is_exported(const char *name, unsigned long value, |
1801 | const struct module *mod) | |
1da177e4 | 1802 | { |
ca4787b7 TA |
1803 | const struct kernel_symbol *ks; |
1804 | if (!mod) | |
1805 | ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); | |
3fd6805f | 1806 | else |
ca4787b7 TA |
1807 | ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); |
1808 | return ks != NULL && ks->value == value; | |
1da177e4 LT |
1809 | } |
1810 | ||
1811 | /* As per nm */ | |
1812 | static char elf_type(const Elf_Sym *sym, | |
1813 | Elf_Shdr *sechdrs, | |
1814 | const char *secstrings, | |
1815 | struct module *mod) | |
1816 | { | |
1817 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { | |
1818 | if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) | |
1819 | return 'v'; | |
1820 | else | |
1821 | return 'w'; | |
1822 | } | |
1823 | if (sym->st_shndx == SHN_UNDEF) | |
1824 | return 'U'; | |
1825 | if (sym->st_shndx == SHN_ABS) | |
1826 | return 'a'; | |
1827 | if (sym->st_shndx >= SHN_LORESERVE) | |
1828 | return '?'; | |
1829 | if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) | |
1830 | return 't'; | |
1831 | if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC | |
1832 | && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { | |
1833 | if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) | |
1834 | return 'r'; | |
1835 | else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) | |
1836 | return 'g'; | |
1837 | else | |
1838 | return 'd'; | |
1839 | } | |
1840 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { | |
1841 | if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) | |
1842 | return 's'; | |
1843 | else | |
1844 | return 'b'; | |
1845 | } | |
49502677 | 1846 | if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug")) |
1da177e4 LT |
1847 | return 'n'; |
1848 | return '?'; | |
1849 | } | |
1850 | ||
1851 | static void add_kallsyms(struct module *mod, | |
1852 | Elf_Shdr *sechdrs, | |
1853 | unsigned int symindex, | |
1854 | unsigned int strindex, | |
1855 | const char *secstrings) | |
1856 | { | |
1857 | unsigned int i; | |
1858 | ||
1859 | mod->symtab = (void *)sechdrs[symindex].sh_addr; | |
1860 | mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); | |
1861 | mod->strtab = (void *)sechdrs[strindex].sh_addr; | |
1862 | ||
1863 | /* Set types up while we still have access to sections. */ | |
1864 | for (i = 0; i < mod->num_symtab; i++) | |
1865 | mod->symtab[i].st_info | |
1866 | = elf_type(&mod->symtab[i], sechdrs, secstrings, mod); | |
1867 | } | |
1868 | #else | |
1869 | static inline void add_kallsyms(struct module *mod, | |
1870 | Elf_Shdr *sechdrs, | |
1871 | unsigned int symindex, | |
1872 | unsigned int strindex, | |
1873 | const char *secstrings) | |
1874 | { | |
1875 | } | |
1876 | #endif /* CONFIG_KALLSYMS */ | |
1877 | ||
e9d376f0 | 1878 | static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num) |
346e15be | 1879 | { |
e9d376f0 JB |
1880 | #ifdef CONFIG_DYNAMIC_DEBUG |
1881 | if (ddebug_add_module(debug, num, debug->modname)) | |
1882 | printk(KERN_ERR "dynamic debug error adding module: %s\n", | |
1883 | debug->modname); | |
1884 | #endif | |
5e458cc0 | 1885 | } |
346e15be | 1886 | |
3a642e99 RR |
1887 | static void *module_alloc_update_bounds(unsigned long size) |
1888 | { | |
1889 | void *ret = module_alloc(size); | |
1890 | ||
1891 | if (ret) { | |
1892 | /* Update module bounds. */ | |
1893 | if ((unsigned long)ret < module_addr_min) | |
1894 | module_addr_min = (unsigned long)ret; | |
1895 | if ((unsigned long)ret + size > module_addr_max) | |
1896 | module_addr_max = (unsigned long)ret + size; | |
1897 | } | |
1898 | return ret; | |
1899 | } | |
1900 | ||
4f2294b6 CM |
1901 | #ifdef CONFIG_DEBUG_KMEMLEAK |
1902 | static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, | |
1903 | Elf_Shdr *sechdrs, char *secstrings) | |
1904 | { | |
1905 | unsigned int i; | |
1906 | ||
1907 | /* only scan the sections containing data */ | |
1908 | kmemleak_scan_area(mod->module_core, (unsigned long)mod - | |
1909 | (unsigned long)mod->module_core, | |
1910 | sizeof(struct module), GFP_KERNEL); | |
1911 | ||
1912 | for (i = 1; i < hdr->e_shnum; i++) { | |
1913 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | |
1914 | continue; | |
1915 | if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0 | |
1916 | && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0) | |
1917 | continue; | |
1918 | ||
1919 | kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr - | |
1920 | (unsigned long)mod->module_core, | |
1921 | sechdrs[i].sh_size, GFP_KERNEL); | |
1922 | } | |
1923 | } | |
1924 | #else | |
1925 | static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, | |
1926 | Elf_Shdr *sechdrs, char *secstrings) | |
1927 | { | |
1928 | } | |
1929 | #endif | |
1930 | ||
1da177e4 LT |
1931 | /* Allocate and load the module: note that size of section 0 is always |
1932 | zero, and we rely on this for optional sections. */ | |
ffb4ba76 | 1933 | static noinline struct module *load_module(void __user *umod, |
1da177e4 LT |
1934 | unsigned long len, |
1935 | const char __user *uargs) | |
1936 | { | |
1937 | Elf_Ehdr *hdr; | |
1938 | Elf_Shdr *sechdrs; | |
1939 | char *secstrings, *args, *modmagic, *strtab = NULL; | |
061b1bd3 | 1940 | char *staging; |
84860f99 AM |
1941 | unsigned int i; |
1942 | unsigned int symindex = 0; | |
1943 | unsigned int strindex = 0; | |
5e458cc0 | 1944 | unsigned int modindex, versindex, infoindex, pcpuindex; |
1da177e4 LT |
1945 | struct module *mod; |
1946 | long err = 0; | |
1947 | void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ | |
378bac82 | 1948 | mm_segment_t old_fs; |
1da177e4 LT |
1949 | |
1950 | DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", | |
1951 | umod, len, uargs); | |
1952 | if (len < sizeof(*hdr)) | |
1953 | return ERR_PTR(-ENOEXEC); | |
1954 | ||
1955 | /* Suck in entire file: we'll want most of it. */ | |
1956 | /* vmalloc barfs on "unusual" numbers. Check here */ | |
1957 | if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) | |
1958 | return ERR_PTR(-ENOMEM); | |
9e01892c | 1959 | |
1da177e4 LT |
1960 | if (copy_from_user(hdr, umod, len) != 0) { |
1961 | err = -EFAULT; | |
1962 | goto free_hdr; | |
1963 | } | |
1964 | ||
1965 | /* Sanity checks against insmoding binaries or wrong arch, | |
1966 | weird elf version */ | |
c4ea6fcf | 1967 | if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 |
1da177e4 LT |
1968 | || hdr->e_type != ET_REL |
1969 | || !elf_check_arch(hdr) | |
1970 | || hdr->e_shentsize != sizeof(*sechdrs)) { | |
1971 | err = -ENOEXEC; | |
1972 | goto free_hdr; | |
1973 | } | |
1974 | ||
1975 | if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) | |
1976 | goto truncated; | |
1977 | ||
1978 | /* Convenience variables */ | |
1979 | sechdrs = (void *)hdr + hdr->e_shoff; | |
1980 | secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; | |
1981 | sechdrs[0].sh_addr = 0; | |
1982 | ||
1983 | for (i = 1; i < hdr->e_shnum; i++) { | |
1984 | if (sechdrs[i].sh_type != SHT_NOBITS | |
1985 | && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) | |
1986 | goto truncated; | |
1987 | ||
1988 | /* Mark all sections sh_addr with their address in the | |
1989 | temporary image. */ | |
1990 | sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset; | |
1991 | ||
1992 | /* Internal symbols and strings. */ | |
1993 | if (sechdrs[i].sh_type == SHT_SYMTAB) { | |
1994 | symindex = i; | |
1995 | strindex = sechdrs[i].sh_link; | |
1996 | strtab = (char *)hdr + sechdrs[strindex].sh_offset; | |
1997 | } | |
1998 | #ifndef CONFIG_MODULE_UNLOAD | |
1999 | /* Don't load .exit sections */ | |
49502677 | 2000 | if (strstarts(secstrings+sechdrs[i].sh_name, ".exit")) |
1da177e4 LT |
2001 | sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; |
2002 | #endif | |
2003 | } | |
2004 | ||
2005 | modindex = find_sec(hdr, sechdrs, secstrings, | |
2006 | ".gnu.linkonce.this_module"); | |
2007 | if (!modindex) { | |
2008 | printk(KERN_WARNING "No module found in object\n"); | |
2009 | err = -ENOEXEC; | |
2010 | goto free_hdr; | |
2011 | } | |
5e458cc0 | 2012 | /* This is temporary: point mod into copy of data. */ |
1da177e4 LT |
2013 | mod = (void *)sechdrs[modindex].sh_addr; |
2014 | ||
2015 | if (symindex == 0) { | |
2016 | printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", | |
2017 | mod->name); | |
2018 | err = -ENOEXEC; | |
2019 | goto free_hdr; | |
2020 | } | |
2021 | ||
1da177e4 LT |
2022 | versindex = find_sec(hdr, sechdrs, secstrings, "__versions"); |
2023 | infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo"); | |
2024 | pcpuindex = find_pcpusec(hdr, sechdrs, secstrings); | |
2025 | ||
ea01e798 | 2026 | /* Don't keep modinfo and version sections. */ |
1da177e4 | 2027 | sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; |
ea01e798 | 2028 | sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC; |
1da177e4 LT |
2029 | #ifdef CONFIG_KALLSYMS |
2030 | /* Keep symbol and string tables for decoding later. */ | |
2031 | sechdrs[symindex].sh_flags |= SHF_ALLOC; | |
2032 | sechdrs[strindex].sh_flags |= SHF_ALLOC; | |
2033 | #endif | |
2034 | ||
2035 | /* Check module struct version now, before we try to use module. */ | |
2036 | if (!check_modstruct_version(sechdrs, versindex, mod)) { | |
2037 | err = -ENOEXEC; | |
2038 | goto free_hdr; | |
2039 | } | |
2040 | ||
2041 | modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); | |
2042 | /* This is allowed: modprobe --force will invalidate it. */ | |
2043 | if (!modmagic) { | |
c6e665c8 | 2044 | err = try_to_force_load(mod, "bad vermagic"); |
826e4506 LT |
2045 | if (err) |
2046 | goto free_hdr; | |
91e37a79 | 2047 | } else if (!same_magic(modmagic, vermagic, versindex)) { |
1da177e4 LT |
2048 | printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", |
2049 | mod->name, modmagic, vermagic); | |
2050 | err = -ENOEXEC; | |
2051 | goto free_hdr; | |
2052 | } | |
2053 | ||
061b1bd3 GKH |
2054 | staging = get_modinfo(sechdrs, infoindex, "staging"); |
2055 | if (staging) { | |
2056 | add_taint_module(mod, TAINT_CRAP); | |
2057 | printk(KERN_WARNING "%s: module is from the staging directory," | |
2058 | " the quality is unknown, you have been warned.\n", | |
2059 | mod->name); | |
2060 | } | |
2061 | ||
1da177e4 | 2062 | /* Now copy in args */ |
24277dda DA |
2063 | args = strndup_user(uargs, ~0UL >> 1); |
2064 | if (IS_ERR(args)) { | |
2065 | err = PTR_ERR(args); | |
1da177e4 LT |
2066 | goto free_hdr; |
2067 | } | |
8e08b756 | 2068 | |
1da177e4 LT |
2069 | if (find_module(mod->name)) { |
2070 | err = -EEXIST; | |
2071 | goto free_mod; | |
2072 | } | |
2073 | ||
2074 | mod->state = MODULE_STATE_COMING; | |
2075 | ||
2076 | /* Allow arches to frob section contents and sizes. */ | |
2077 | err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod); | |
2078 | if (err < 0) | |
2079 | goto free_mod; | |
2080 | ||
2081 | if (pcpuindex) { | |
2082 | /* We have a special allocation for this section. */ | |
2083 | percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, | |
842bbaaa RR |
2084 | sechdrs[pcpuindex].sh_addralign, |
2085 | mod->name); | |
1da177e4 LT |
2086 | if (!percpu) { |
2087 | err = -ENOMEM; | |
6e2b7574 | 2088 | goto free_mod; |
1da177e4 LT |
2089 | } |
2090 | sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; | |
2091 | mod->percpu = percpu; | |
2092 | } | |
2093 | ||
2094 | /* Determine total sizes, and put offsets in sh_entsize. For now | |
2095 | this is done generically; there doesn't appear to be any | |
2096 | special cases for the architectures. */ | |
2097 | layout_sections(mod, hdr, sechdrs, secstrings); | |
2098 | ||
2099 | /* Do the allocs. */ | |
3a642e99 | 2100 | ptr = module_alloc_update_bounds(mod->core_size); |
4f2294b6 CM |
2101 | /* |
2102 | * The pointer to this block is stored in the module structure | |
2103 | * which is inside the block. Just mark it as not being a | |
2104 | * leak. | |
2105 | */ | |
2106 | kmemleak_not_leak(ptr); | |
1da177e4 LT |
2107 | if (!ptr) { |
2108 | err = -ENOMEM; | |
2109 | goto free_percpu; | |
2110 | } | |
2111 | memset(ptr, 0, mod->core_size); | |
2112 | mod->module_core = ptr; | |
2113 | ||
3a642e99 | 2114 | ptr = module_alloc_update_bounds(mod->init_size); |
4f2294b6 CM |
2115 | /* |
2116 | * The pointer to this block is stored in the module structure | |
2117 | * which is inside the block. This block doesn't need to be | |
2118 | * scanned as it contains data and code that will be freed | |
2119 | * after the module is initialized. | |
2120 | */ | |
2121 | kmemleak_ignore(ptr); | |
1da177e4 LT |
2122 | if (!ptr && mod->init_size) { |
2123 | err = -ENOMEM; | |
2124 | goto free_core; | |
2125 | } | |
2126 | memset(ptr, 0, mod->init_size); | |
2127 | mod->module_init = ptr; | |
2128 | ||
2129 | /* Transfer each section which specifies SHF_ALLOC */ | |
2130 | DEBUGP("final section addresses:\n"); | |
2131 | for (i = 0; i < hdr->e_shnum; i++) { | |
2132 | void *dest; | |
2133 | ||
2134 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | |
2135 | continue; | |
2136 | ||
2137 | if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK) | |
2138 | dest = mod->module_init | |
2139 | + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK); | |
2140 | else | |
2141 | dest = mod->module_core + sechdrs[i].sh_entsize; | |
2142 | ||
2143 | if (sechdrs[i].sh_type != SHT_NOBITS) | |
2144 | memcpy(dest, (void *)sechdrs[i].sh_addr, | |
2145 | sechdrs[i].sh_size); | |
2146 | /* Update sh_addr to point to copy in image. */ | |
2147 | sechdrs[i].sh_addr = (unsigned long)dest; | |
2148 | DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name); | |
2149 | } | |
2150 | /* Module has been moved. */ | |
2151 | mod = (void *)sechdrs[modindex].sh_addr; | |
4f2294b6 | 2152 | kmemleak_load_module(mod, hdr, sechdrs, secstrings); |
1da177e4 | 2153 | |
6e2b7574 MH |
2154 | #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) |
2155 | mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t), | |
2156 | mod->name); | |
2157 | if (!mod->refptr) { | |
2158 | err = -ENOMEM; | |
2159 | goto free_init; | |
2160 | } | |
2161 | #endif | |
1da177e4 LT |
2162 | /* Now we've moved module, initialize linked lists, etc. */ |
2163 | module_unload_init(mod); | |
2164 | ||
97c146ef | 2165 | /* add kobject, so we can reference it. */ |
d58ae678 AM |
2166 | err = mod_sysfs_init(mod); |
2167 | if (err) | |
97c146ef | 2168 | goto free_unload; |
270a6c4c | 2169 | |
1da177e4 LT |
2170 | /* Set up license info based on the info section */ |
2171 | set_license(mod, get_modinfo(sechdrs, infoindex, "license")); | |
2172 | ||
9b37ccfc PR |
2173 | /* |
2174 | * ndiswrapper is under GPL by itself, but loads proprietary modules. | |
2175 | * Don't use add_taint_module(), as it would prevent ndiswrapper from | |
2176 | * using GPL-only symbols it needs. | |
2177 | */ | |
fa3ba2e8 | 2178 | if (strcmp(mod->name, "ndiswrapper") == 0) |
9b37ccfc PR |
2179 | add_taint(TAINT_PROPRIETARY_MODULE); |
2180 | ||
2181 | /* driverloader was caught wrongly pretending to be under GPL */ | |
fa3ba2e8 FM |
2182 | if (strcmp(mod->name, "driverloader") == 0) |
2183 | add_taint_module(mod, TAINT_PROPRIETARY_MODULE); | |
9841d61d | 2184 | |
c988d2b2 MD |
2185 | /* Set up MODINFO_ATTR fields */ |
2186 | setup_modinfo(mod, sechdrs, infoindex); | |
c988d2b2 | 2187 | |
1da177e4 LT |
2188 | /* Fix up syms, so that st_value is a pointer to location. */ |
2189 | err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex, | |
2190 | mod); | |
2191 | if (err < 0) | |
2192 | goto cleanup; | |
2193 | ||
5e458cc0 RR |
2194 | /* Now we've got everything in the final locations, we can |
2195 | * find optional sections. */ | |
e180a6b7 RR |
2196 | mod->kp = section_objs(hdr, sechdrs, secstrings, "__param", |
2197 | sizeof(*mod->kp), &mod->num_kp); | |
5e458cc0 RR |
2198 | mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab", |
2199 | sizeof(*mod->syms), &mod->num_syms); | |
2200 | mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab"); | |
2201 | mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl", | |
2202 | sizeof(*mod->gpl_syms), | |
2203 | &mod->num_gpl_syms); | |
2204 | mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl"); | |
2205 | mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings, | |
2206 | "__ksymtab_gpl_future", | |
2207 | sizeof(*mod->gpl_future_syms), | |
2208 | &mod->num_gpl_future_syms); | |
2209 | mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings, | |
2210 | "__kcrctab_gpl_future"); | |
1da177e4 | 2211 | |
f7f5b675 | 2212 | #ifdef CONFIG_UNUSED_SYMBOLS |
5e458cc0 RR |
2213 | mod->unused_syms = section_objs(hdr, sechdrs, secstrings, |
2214 | "__ksymtab_unused", | |
2215 | sizeof(*mod->unused_syms), | |
2216 | &mod->num_unused_syms); | |
2217 | mod->unused_crcs = section_addr(hdr, sechdrs, secstrings, | |
2218 | "__kcrctab_unused"); | |
2219 | mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings, | |
2220 | "__ksymtab_unused_gpl", | |
2221 | sizeof(*mod->unused_gpl_syms), | |
2222 | &mod->num_unused_gpl_syms); | |
2223 | mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings, | |
2224 | "__kcrctab_unused_gpl"); | |
2225 | #endif | |
b99b87f7 PO |
2226 | #ifdef CONFIG_CONSTRUCTORS |
2227 | mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors", | |
2228 | sizeof(*mod->ctors), &mod->num_ctors); | |
2229 | #endif | |
5e458cc0 RR |
2230 | |
2231 | #ifdef CONFIG_MARKERS | |
2232 | mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers", | |
2233 | sizeof(*mod->markers), &mod->num_markers); | |
2234 | #endif | |
2235 | #ifdef CONFIG_TRACEPOINTS | |
2236 | mod->tracepoints = section_objs(hdr, sechdrs, secstrings, | |
2237 | "__tracepoints", | |
2238 | sizeof(*mod->tracepoints), | |
2239 | &mod->num_tracepoints); | |
f7f5b675 | 2240 | #endif |
6d723736 SR |
2241 | #ifdef CONFIG_EVENT_TRACING |
2242 | mod->trace_events = section_objs(hdr, sechdrs, secstrings, | |
2243 | "_ftrace_events", | |
2244 | sizeof(*mod->trace_events), | |
2245 | &mod->num_trace_events); | |
2246 | #endif | |
93eb677d SR |
2247 | #ifdef CONFIG_FTRACE_MCOUNT_RECORD |
2248 | /* sechdrs[0].sh_size is always zero */ | |
2249 | mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings, | |
2250 | "__mcount_loc", | |
2251 | sizeof(*mod->ftrace_callsites), | |
2252 | &mod->num_ftrace_callsites); | |
2253 | #endif | |
1da177e4 | 2254 | #ifdef CONFIG_MODVERSIONS |
5e458cc0 RR |
2255 | if ((mod->num_syms && !mod->crcs) |
2256 | || (mod->num_gpl_syms && !mod->gpl_crcs) | |
2257 | || (mod->num_gpl_future_syms && !mod->gpl_future_crcs) | |
f7f5b675 | 2258 | #ifdef CONFIG_UNUSED_SYMBOLS |
5e458cc0 RR |
2259 | || (mod->num_unused_syms && !mod->unused_crcs) |
2260 | || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) | |
f7f5b675 DV |
2261 | #endif |
2262 | ) { | |
c6e665c8 RR |
2263 | err = try_to_force_load(mod, |
2264 | "no versions for exported symbols"); | |
826e4506 LT |
2265 | if (err) |
2266 | goto cleanup; | |
1da177e4 LT |
2267 | } |
2268 | #endif | |
2269 | ||
2270 | /* Now do relocations. */ | |
2271 | for (i = 1; i < hdr->e_shnum; i++) { | |
2272 | const char *strtab = (char *)sechdrs[strindex].sh_addr; | |
2273 | unsigned int info = sechdrs[i].sh_info; | |
2274 | ||
2275 | /* Not a valid relocation section? */ | |
2276 | if (info >= hdr->e_shnum) | |
2277 | continue; | |
2278 | ||
2279 | /* Don't bother with non-allocated sections */ | |
2280 | if (!(sechdrs[info].sh_flags & SHF_ALLOC)) | |
2281 | continue; | |
2282 | ||
2283 | if (sechdrs[i].sh_type == SHT_REL) | |
2284 | err = apply_relocate(sechdrs, strtab, symindex, i,mod); | |
2285 | else if (sechdrs[i].sh_type == SHT_RELA) | |
2286 | err = apply_relocate_add(sechdrs, strtab, symindex, i, | |
2287 | mod); | |
2288 | if (err < 0) | |
2289 | goto cleanup; | |
2290 | } | |
2291 | ||
eea8b54d AN |
2292 | /* Find duplicate symbols */ |
2293 | err = verify_export_symbols(mod); | |
eea8b54d AN |
2294 | if (err < 0) |
2295 | goto cleanup; | |
2296 | ||
1da177e4 | 2297 | /* Set up and sort exception table */ |
5e458cc0 RR |
2298 | mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table", |
2299 | sizeof(*mod->extable), &mod->num_exentries); | |
2300 | sort_extable(mod->extable, mod->extable + mod->num_exentries); | |
1da177e4 LT |
2301 | |
2302 | /* Finally, copy percpu area over. */ | |
2303 | percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, | |
2304 | sechdrs[pcpuindex].sh_size); | |
2305 | ||
2306 | add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); | |
2307 | ||
97e1c18e | 2308 | if (!mod->taints) { |
e9d376f0 | 2309 | struct _ddebug *debug; |
5e458cc0 RR |
2310 | unsigned int num_debug; |
2311 | ||
5e458cc0 RR |
2312 | debug = section_objs(hdr, sechdrs, secstrings, "__verbose", |
2313 | sizeof(*debug), &num_debug); | |
e9d376f0 JB |
2314 | if (debug) |
2315 | dynamic_debug_setup(debug, num_debug); | |
97e1c18e | 2316 | } |
90d595fe | 2317 | |
1da177e4 LT |
2318 | err = module_finalize(hdr, sechdrs, mod); |
2319 | if (err < 0) | |
2320 | goto cleanup; | |
2321 | ||
378bac82 TK |
2322 | /* flush the icache in correct context */ |
2323 | old_fs = get_fs(); | |
2324 | set_fs(KERNEL_DS); | |
2325 | ||
2326 | /* | |
2327 | * Flush the instruction cache, since we've played with text. | |
2328 | * Do it before processing of module parameters, so the module | |
2329 | * can provide parameter accessor functions of its own. | |
2330 | */ | |
2331 | if (mod->module_init) | |
2332 | flush_icache_range((unsigned long)mod->module_init, | |
2333 | (unsigned long)mod->module_init | |
2334 | + mod->init_size); | |
2335 | flush_icache_range((unsigned long)mod->module_core, | |
2336 | (unsigned long)mod->module_core + mod->core_size); | |
2337 | ||
2338 | set_fs(old_fs); | |
2339 | ||
1da177e4 | 2340 | mod->args = args; |
5e458cc0 | 2341 | if (section_addr(hdr, sechdrs, secstrings, "__obsparm")) |
8d3b33f6 RR |
2342 | printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", |
2343 | mod->name); | |
2344 | ||
bb9d3d56 | 2345 | /* Now sew it into the lists so we can get lockdep and oops |
d72b3751 AK |
2346 | * info during argument parsing. Noone should access us, since |
2347 | * strong_try_module_get() will fail. | |
2348 | * lockdep/oops can run asynchronous, so use the RCU list insertion | |
2349 | * function to insert in a way safe to concurrent readers. | |
2350 | * The mutex protects against concurrent writers. | |
2351 | */ | |
2352 | list_add_rcu(&mod->list, &modules); | |
bb9d3d56 | 2353 | |
e180a6b7 | 2354 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); |
1da177e4 | 2355 | if (err < 0) |
bb9d3d56 | 2356 | goto unlink; |
1da177e4 | 2357 | |
e180a6b7 | 2358 | err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); |
1da177e4 | 2359 | if (err < 0) |
bb9d3d56 | 2360 | goto unlink; |
1da177e4 | 2361 | add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); |
ea6bff36 | 2362 | add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); |
1da177e4 LT |
2363 | |
2364 | /* Get rid of temporary copy */ | |
2365 | vfree(hdr); | |
2366 | ||
2367 | /* Done! */ | |
2368 | return mod; | |
2369 | ||
bb9d3d56 | 2370 | unlink: |
e91defa2 RR |
2371 | /* Unlink carefully: kallsyms could be walking list. */ |
2372 | list_del_rcu(&mod->list); | |
2373 | synchronize_sched(); | |
1da177e4 LT |
2374 | module_arch_cleanup(mod); |
2375 | cleanup: | |
97c146ef KS |
2376 | kobject_del(&mod->mkobj.kobj); |
2377 | kobject_put(&mod->mkobj.kobj); | |
2378 | free_unload: | |
1da177e4 | 2379 | module_unload_free(mod); |
6e2b7574 | 2380 | #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) |
b10153fe | 2381 | free_init: |
6e2b7574 MH |
2382 | percpu_modfree(mod->refptr); |
2383 | #endif | |
1da177e4 LT |
2384 | module_free(mod, mod->module_init); |
2385 | free_core: | |
2386 | module_free(mod, mod->module_core); | |
6e2b7574 | 2387 | /* mod will be freed with core. Don't access it beyond this line! */ |
1da177e4 LT |
2388 | free_percpu: |
2389 | if (percpu) | |
2390 | percpu_modfree(percpu); | |
2391 | free_mod: | |
2392 | kfree(args); | |
2393 | free_hdr: | |
2394 | vfree(hdr); | |
6fe2e70b | 2395 | return ERR_PTR(err); |
1da177e4 LT |
2396 | |
2397 | truncated: | |
2398 | printk(KERN_ERR "Module len %lu truncated\n", len); | |
2399 | err = -ENOEXEC; | |
2400 | goto free_hdr; | |
2401 | } | |
2402 | ||
b99b87f7 PO |
2403 | /* Call module constructors. */ |
2404 | static void do_mod_ctors(struct module *mod) | |
2405 | { | |
2406 | #ifdef CONFIG_CONSTRUCTORS | |
2407 | unsigned long i; | |
2408 | ||
2409 | for (i = 0; i < mod->num_ctors; i++) | |
2410 | mod->ctors[i](); | |
2411 | #endif | |
2412 | } | |
2413 | ||
1da177e4 | 2414 | /* This is where the real work happens */ |
17da2bd9 HC |
2415 | SYSCALL_DEFINE3(init_module, void __user *, umod, |
2416 | unsigned long, len, const char __user *, uargs) | |
1da177e4 LT |
2417 | { |
2418 | struct module *mod; | |
2419 | int ret = 0; | |
2420 | ||
2421 | /* Must have permission */ | |
3d43321b | 2422 | if (!capable(CAP_SYS_MODULE) || modules_disabled) |
1da177e4 LT |
2423 | return -EPERM; |
2424 | ||
2425 | /* Only one module load at a time, please */ | |
6389a385 | 2426 | if (mutex_lock_interruptible(&module_mutex) != 0) |
1da177e4 LT |
2427 | return -EINTR; |
2428 | ||
2429 | /* Do all the hard work */ | |
2430 | mod = load_module(umod, len, uargs); | |
2431 | if (IS_ERR(mod)) { | |
6389a385 | 2432 | mutex_unlock(&module_mutex); |
1da177e4 LT |
2433 | return PTR_ERR(mod); |
2434 | } | |
2435 | ||
1da177e4 | 2436 | /* Drop lock so they can recurse */ |
6389a385 | 2437 | mutex_unlock(&module_mutex); |
1da177e4 | 2438 | |
e041c683 AS |
2439 | blocking_notifier_call_chain(&module_notify_list, |
2440 | MODULE_STATE_COMING, mod); | |
1da177e4 | 2441 | |
b99b87f7 | 2442 | do_mod_ctors(mod); |
1da177e4 LT |
2443 | /* Start the module */ |
2444 | if (mod->init != NULL) | |
59f9415f | 2445 | ret = do_one_initcall(mod->init); |
1da177e4 LT |
2446 | if (ret < 0) { |
2447 | /* Init routine failed: abort. Try to protect us from | |
2448 | buggy refcounters. */ | |
2449 | mod->state = MODULE_STATE_GOING; | |
fbd568a3 | 2450 | synchronize_sched(); |
af49d924 | 2451 | module_put(mod); |
df4b565e PO |
2452 | blocking_notifier_call_chain(&module_notify_list, |
2453 | MODULE_STATE_GOING, mod); | |
af49d924 RR |
2454 | mutex_lock(&module_mutex); |
2455 | free_module(mod); | |
2456 | mutex_unlock(&module_mutex); | |
c9a3ba55 | 2457 | wake_up(&module_wq); |
1da177e4 LT |
2458 | return ret; |
2459 | } | |
e24e2e64 | 2460 | if (ret > 0) { |
ad361c98 JP |
2461 | printk(KERN_WARNING |
2462 | "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n" | |
2463 | "%s: loading module anyway...\n", | |
e24e2e64 AD |
2464 | __func__, mod->name, ret, |
2465 | __func__); | |
2466 | dump_stack(); | |
2467 | } | |
1da177e4 | 2468 | |
6c5db22d | 2469 | /* Now it's a first class citizen! Wake up anyone waiting for it. */ |
1da177e4 | 2470 | mod->state = MODULE_STATE_LIVE; |
6c5db22d | 2471 | wake_up(&module_wq); |
0deddf43 MH |
2472 | blocking_notifier_call_chain(&module_notify_list, |
2473 | MODULE_STATE_LIVE, mod); | |
6c5db22d | 2474 | |
d6de2c80 LT |
2475 | /* We need to finish all async code before the module init sequence is done */ |
2476 | async_synchronize_full(); | |
2477 | ||
6c5db22d | 2478 | mutex_lock(&module_mutex); |
1da177e4 LT |
2479 | /* Drop initial reference. */ |
2480 | module_put(mod); | |
ad6561df | 2481 | trim_init_extable(mod); |
1da177e4 LT |
2482 | module_free(mod, mod->module_init); |
2483 | mod->module_init = NULL; | |
2484 | mod->init_size = 0; | |
2485 | mod->init_text_size = 0; | |
6389a385 | 2486 | mutex_unlock(&module_mutex); |
1da177e4 LT |
2487 | |
2488 | return 0; | |
2489 | } | |
2490 | ||
2491 | static inline int within(unsigned long addr, void *start, unsigned long size) | |
2492 | { | |
2493 | return ((void *)addr >= start && (void *)addr < start + size); | |
2494 | } | |
2495 | ||
2496 | #ifdef CONFIG_KALLSYMS | |
2497 | /* | |
2498 | * This ignores the intensely annoying "mapping symbols" found | |
2499 | * in ARM ELF files: $a, $t and $d. | |
2500 | */ | |
2501 | static inline int is_arm_mapping_symbol(const char *str) | |
2502 | { | |
22a8bdeb | 2503 | return str[0] == '$' && strchr("atd", str[1]) |
1da177e4 LT |
2504 | && (str[2] == '\0' || str[2] == '.'); |
2505 | } | |
2506 | ||
2507 | static const char *get_ksymbol(struct module *mod, | |
2508 | unsigned long addr, | |
2509 | unsigned long *size, | |
2510 | unsigned long *offset) | |
2511 | { | |
2512 | unsigned int i, best = 0; | |
2513 | unsigned long nextval; | |
2514 | ||
2515 | /* At worse, next value is at end of module */ | |
a06f6211 | 2516 | if (within_module_init(addr, mod)) |
1da177e4 | 2517 | nextval = (unsigned long)mod->module_init+mod->init_text_size; |
22a8bdeb | 2518 | else |
1da177e4 LT |
2519 | nextval = (unsigned long)mod->module_core+mod->core_text_size; |
2520 | ||
2521 | /* Scan for closest preceeding symbol, and next symbol. (ELF | |
22a8bdeb | 2522 | starts real symbols at 1). */ |
1da177e4 LT |
2523 | for (i = 1; i < mod->num_symtab; i++) { |
2524 | if (mod->symtab[i].st_shndx == SHN_UNDEF) | |
2525 | continue; | |
2526 | ||
2527 | /* We ignore unnamed symbols: they're uninformative | |
2528 | * and inserted at a whim. */ | |
2529 | if (mod->symtab[i].st_value <= addr | |
2530 | && mod->symtab[i].st_value > mod->symtab[best].st_value | |
2531 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' | |
2532 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) | |
2533 | best = i; | |
2534 | if (mod->symtab[i].st_value > addr | |
2535 | && mod->symtab[i].st_value < nextval | |
2536 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' | |
2537 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) | |
2538 | nextval = mod->symtab[i].st_value; | |
2539 | } | |
2540 | ||
2541 | if (!best) | |
2542 | return NULL; | |
2543 | ||
ffb45122 AD |
2544 | if (size) |
2545 | *size = nextval - mod->symtab[best].st_value; | |
2546 | if (offset) | |
2547 | *offset = addr - mod->symtab[best].st_value; | |
1da177e4 LT |
2548 | return mod->strtab + mod->symtab[best].st_name; |
2549 | } | |
2550 | ||
6dd06c9f RR |
2551 | /* For kallsyms to ask for address resolution. NULL means not found. Careful |
2552 | * not to lock to avoid deadlock on oopses, simply disable preemption. */ | |
92dfc9dc | 2553 | const char *module_address_lookup(unsigned long addr, |
6dd06c9f RR |
2554 | unsigned long *size, |
2555 | unsigned long *offset, | |
2556 | char **modname, | |
2557 | char *namebuf) | |
1da177e4 LT |
2558 | { |
2559 | struct module *mod; | |
cb2a5205 | 2560 | const char *ret = NULL; |
1da177e4 | 2561 | |
cb2a5205 | 2562 | preempt_disable(); |
d72b3751 | 2563 | list_for_each_entry_rcu(mod, &modules, list) { |
a06f6211 MH |
2564 | if (within_module_init(addr, mod) || |
2565 | within_module_core(addr, mod)) { | |
ffc50891 FBH |
2566 | if (modname) |
2567 | *modname = mod->name; | |
cb2a5205 RR |
2568 | ret = get_ksymbol(mod, addr, size, offset); |
2569 | break; | |
1da177e4 LT |
2570 | } |
2571 | } | |
6dd06c9f RR |
2572 | /* Make a copy in here where it's safe */ |
2573 | if (ret) { | |
2574 | strncpy(namebuf, ret, KSYM_NAME_LEN - 1); | |
2575 | ret = namebuf; | |
2576 | } | |
cb2a5205 | 2577 | preempt_enable(); |
92dfc9dc | 2578 | return ret; |
1da177e4 LT |
2579 | } |
2580 | ||
9d65cb4a AD |
2581 | int lookup_module_symbol_name(unsigned long addr, char *symname) |
2582 | { | |
2583 | struct module *mod; | |
2584 | ||
cb2a5205 | 2585 | preempt_disable(); |
d72b3751 | 2586 | list_for_each_entry_rcu(mod, &modules, list) { |
a06f6211 MH |
2587 | if (within_module_init(addr, mod) || |
2588 | within_module_core(addr, mod)) { | |
9d65cb4a AD |
2589 | const char *sym; |
2590 | ||
2591 | sym = get_ksymbol(mod, addr, NULL, NULL); | |
2592 | if (!sym) | |
2593 | goto out; | |
9281acea | 2594 | strlcpy(symname, sym, KSYM_NAME_LEN); |
cb2a5205 | 2595 | preempt_enable(); |
9d65cb4a AD |
2596 | return 0; |
2597 | } | |
2598 | } | |
2599 | out: | |
cb2a5205 | 2600 | preempt_enable(); |
9d65cb4a AD |
2601 | return -ERANGE; |
2602 | } | |
2603 | ||
a5c43dae AD |
2604 | int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, |
2605 | unsigned long *offset, char *modname, char *name) | |
2606 | { | |
2607 | struct module *mod; | |
2608 | ||
cb2a5205 | 2609 | preempt_disable(); |
d72b3751 | 2610 | list_for_each_entry_rcu(mod, &modules, list) { |
a06f6211 MH |
2611 | if (within_module_init(addr, mod) || |
2612 | within_module_core(addr, mod)) { | |
a5c43dae AD |
2613 | const char *sym; |
2614 | ||
2615 | sym = get_ksymbol(mod, addr, size, offset); | |
2616 | if (!sym) | |
2617 | goto out; | |
2618 | if (modname) | |
9281acea | 2619 | strlcpy(modname, mod->name, MODULE_NAME_LEN); |
a5c43dae | 2620 | if (name) |
9281acea | 2621 | strlcpy(name, sym, KSYM_NAME_LEN); |
cb2a5205 | 2622 | preempt_enable(); |
a5c43dae AD |
2623 | return 0; |
2624 | } | |
2625 | } | |
2626 | out: | |
cb2a5205 | 2627 | preempt_enable(); |
a5c43dae AD |
2628 | return -ERANGE; |
2629 | } | |
2630 | ||
ea07890a AD |
2631 | int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, |
2632 | char *name, char *module_name, int *exported) | |
1da177e4 LT |
2633 | { |
2634 | struct module *mod; | |
2635 | ||
cb2a5205 | 2636 | preempt_disable(); |
d72b3751 | 2637 | list_for_each_entry_rcu(mod, &modules, list) { |
1da177e4 LT |
2638 | if (symnum < mod->num_symtab) { |
2639 | *value = mod->symtab[symnum].st_value; | |
2640 | *type = mod->symtab[symnum].st_info; | |
098c5eea | 2641 | strlcpy(name, mod->strtab + mod->symtab[symnum].st_name, |
9281acea TH |
2642 | KSYM_NAME_LEN); |
2643 | strlcpy(module_name, mod->name, MODULE_NAME_LEN); | |
ca4787b7 | 2644 | *exported = is_exported(name, *value, mod); |
cb2a5205 | 2645 | preempt_enable(); |
ea07890a | 2646 | return 0; |
1da177e4 LT |
2647 | } |
2648 | symnum -= mod->num_symtab; | |
2649 | } | |
cb2a5205 | 2650 | preempt_enable(); |
ea07890a | 2651 | return -ERANGE; |
1da177e4 LT |
2652 | } |
2653 | ||
2654 | static unsigned long mod_find_symname(struct module *mod, const char *name) | |
2655 | { | |
2656 | unsigned int i; | |
2657 | ||
2658 | for (i = 0; i < mod->num_symtab; i++) | |
54e8ce46 KO |
2659 | if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 && |
2660 | mod->symtab[i].st_info != 'U') | |
1da177e4 LT |
2661 | return mod->symtab[i].st_value; |
2662 | return 0; | |
2663 | } | |
2664 | ||
2665 | /* Look for this name: can be of form module:name. */ | |
2666 | unsigned long module_kallsyms_lookup_name(const char *name) | |
2667 | { | |
2668 | struct module *mod; | |
2669 | char *colon; | |
2670 | unsigned long ret = 0; | |
2671 | ||
2672 | /* Don't lock: we're in enough trouble already. */ | |
cb2a5205 | 2673 | preempt_disable(); |
1da177e4 LT |
2674 | if ((colon = strchr(name, ':')) != NULL) { |
2675 | *colon = '\0'; | |
2676 | if ((mod = find_module(name)) != NULL) | |
2677 | ret = mod_find_symname(mod, colon+1); | |
2678 | *colon = ':'; | |
2679 | } else { | |
d72b3751 | 2680 | list_for_each_entry_rcu(mod, &modules, list) |
1da177e4 LT |
2681 | if ((ret = mod_find_symname(mod, name)) != 0) |
2682 | break; | |
2683 | } | |
cb2a5205 | 2684 | preempt_enable(); |
1da177e4 LT |
2685 | return ret; |
2686 | } | |
75a66614 AK |
2687 | |
2688 | int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, | |
2689 | struct module *, unsigned long), | |
2690 | void *data) | |
2691 | { | |
2692 | struct module *mod; | |
2693 | unsigned int i; | |
2694 | int ret; | |
2695 | ||
2696 | list_for_each_entry(mod, &modules, list) { | |
2697 | for (i = 0; i < mod->num_symtab; i++) { | |
2698 | ret = fn(data, mod->strtab + mod->symtab[i].st_name, | |
2699 | mod, mod->symtab[i].st_value); | |
2700 | if (ret != 0) | |
2701 | return ret; | |
2702 | } | |
2703 | } | |
2704 | return 0; | |
2705 | } | |
1da177e4 LT |
2706 | #endif /* CONFIG_KALLSYMS */ |
2707 | ||
21aa9280 | 2708 | static char *module_flags(struct module *mod, char *buf) |
fa3ba2e8 FM |
2709 | { |
2710 | int bx = 0; | |
2711 | ||
21aa9280 AV |
2712 | if (mod->taints || |
2713 | mod->state == MODULE_STATE_GOING || | |
2714 | mod->state == MODULE_STATE_COMING) { | |
fa3ba2e8 | 2715 | buf[bx++] = '('; |
25ddbb18 | 2716 | if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE)) |
fa3ba2e8 | 2717 | buf[bx++] = 'P'; |
25ddbb18 | 2718 | if (mod->taints & (1 << TAINT_FORCED_MODULE)) |
fa3ba2e8 | 2719 | buf[bx++] = 'F'; |
26e9a397 | 2720 | if (mod->taints & (1 << TAINT_CRAP)) |
061b1bd3 | 2721 | buf[bx++] = 'C'; |
fa3ba2e8 FM |
2722 | /* |
2723 | * TAINT_FORCED_RMMOD: could be added. | |
2724 | * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't | |
2725 | * apply to modules. | |
2726 | */ | |
21aa9280 AV |
2727 | |
2728 | /* Show a - for module-is-being-unloaded */ | |
2729 | if (mod->state == MODULE_STATE_GOING) | |
2730 | buf[bx++] = '-'; | |
2731 | /* Show a + for module-is-being-loaded */ | |
2732 | if (mod->state == MODULE_STATE_COMING) | |
2733 | buf[bx++] = '+'; | |
fa3ba2e8 FM |
2734 | buf[bx++] = ')'; |
2735 | } | |
2736 | buf[bx] = '\0'; | |
2737 | ||
2738 | return buf; | |
2739 | } | |
2740 | ||
3b5d5c6b AD |
2741 | #ifdef CONFIG_PROC_FS |
2742 | /* Called by the /proc file system to return a list of modules. */ | |
2743 | static void *m_start(struct seq_file *m, loff_t *pos) | |
2744 | { | |
2745 | mutex_lock(&module_mutex); | |
2746 | return seq_list_start(&modules, *pos); | |
2747 | } | |
2748 | ||
2749 | static void *m_next(struct seq_file *m, void *p, loff_t *pos) | |
2750 | { | |
2751 | return seq_list_next(p, &modules, pos); | |
2752 | } | |
2753 | ||
2754 | static void m_stop(struct seq_file *m, void *p) | |
2755 | { | |
2756 | mutex_unlock(&module_mutex); | |
2757 | } | |
2758 | ||
1da177e4 LT |
2759 | static int m_show(struct seq_file *m, void *p) |
2760 | { | |
2761 | struct module *mod = list_entry(p, struct module, list); | |
fa3ba2e8 FM |
2762 | char buf[8]; |
2763 | ||
2f0f2a33 | 2764 | seq_printf(m, "%s %u", |
1da177e4 LT |
2765 | mod->name, mod->init_size + mod->core_size); |
2766 | print_unload_info(m, mod); | |
2767 | ||
2768 | /* Informative for users. */ | |
2769 | seq_printf(m, " %s", | |
2770 | mod->state == MODULE_STATE_GOING ? "Unloading": | |
2771 | mod->state == MODULE_STATE_COMING ? "Loading": | |
2772 | "Live"); | |
2773 | /* Used by oprofile and other similar tools. */ | |
2774 | seq_printf(m, " 0x%p", mod->module_core); | |
2775 | ||
fa3ba2e8 FM |
2776 | /* Taints info */ |
2777 | if (mod->taints) | |
21aa9280 | 2778 | seq_printf(m, " %s", module_flags(mod, buf)); |
fa3ba2e8 | 2779 | |
1da177e4 LT |
2780 | seq_printf(m, "\n"); |
2781 | return 0; | |
2782 | } | |
2783 | ||
2784 | /* Format: modulename size refcount deps address | |
2785 | ||
2786 | Where refcount is a number or -, and deps is a comma-separated list | |
2787 | of depends or -. | |
2788 | */ | |
3b5d5c6b | 2789 | static const struct seq_operations modules_op = { |
1da177e4 LT |
2790 | .start = m_start, |
2791 | .next = m_next, | |
2792 | .stop = m_stop, | |
2793 | .show = m_show | |
2794 | }; | |
2795 | ||
3b5d5c6b AD |
2796 | static int modules_open(struct inode *inode, struct file *file) |
2797 | { | |
2798 | return seq_open(file, &modules_op); | |
2799 | } | |
2800 | ||
2801 | static const struct file_operations proc_modules_operations = { | |
2802 | .open = modules_open, | |
2803 | .read = seq_read, | |
2804 | .llseek = seq_lseek, | |
2805 | .release = seq_release, | |
2806 | }; | |
2807 | ||
2808 | static int __init proc_modules_init(void) | |
2809 | { | |
2810 | proc_create("modules", 0, NULL, &proc_modules_operations); | |
2811 | return 0; | |
2812 | } | |
2813 | module_init(proc_modules_init); | |
2814 | #endif | |
2815 | ||
1da177e4 LT |
2816 | /* Given an address, look for it in the module exception tables. */ |
2817 | const struct exception_table_entry *search_module_extables(unsigned long addr) | |
2818 | { | |
1da177e4 LT |
2819 | const struct exception_table_entry *e = NULL; |
2820 | struct module *mod; | |
2821 | ||
24da1cbf | 2822 | preempt_disable(); |
d72b3751 | 2823 | list_for_each_entry_rcu(mod, &modules, list) { |
1da177e4 LT |
2824 | if (mod->num_exentries == 0) |
2825 | continue; | |
22a8bdeb | 2826 | |
1da177e4 LT |
2827 | e = search_extable(mod->extable, |
2828 | mod->extable + mod->num_exentries - 1, | |
2829 | addr); | |
2830 | if (e) | |
2831 | break; | |
2832 | } | |
24da1cbf | 2833 | preempt_enable(); |
1da177e4 LT |
2834 | |
2835 | /* Now, if we found one, we are running inside it now, hence | |
22a8bdeb | 2836 | we cannot unload the module, hence no refcnt needed. */ |
1da177e4 LT |
2837 | return e; |
2838 | } | |
2839 | ||
4d435f9d | 2840 | /* |
e610499e RR |
2841 | * is_module_address - is this address inside a module? |
2842 | * @addr: the address to check. | |
2843 | * | |
2844 | * See is_module_text_address() if you simply want to see if the address | |
2845 | * is code (not data). | |
4d435f9d | 2846 | */ |
e610499e | 2847 | bool is_module_address(unsigned long addr) |
4d435f9d | 2848 | { |
e610499e | 2849 | bool ret; |
4d435f9d | 2850 | |
24da1cbf | 2851 | preempt_disable(); |
e610499e | 2852 | ret = __module_address(addr) != NULL; |
24da1cbf | 2853 | preempt_enable(); |
4d435f9d | 2854 | |
e610499e | 2855 | return ret; |
4d435f9d IM |
2856 | } |
2857 | ||
e610499e RR |
2858 | /* |
2859 | * __module_address - get the module which contains an address. | |
2860 | * @addr: the address. | |
2861 | * | |
2862 | * Must be called with preempt disabled or module mutex held so that | |
2863 | * module doesn't get freed during this. | |
2864 | */ | |
714f83d5 | 2865 | struct module *__module_address(unsigned long addr) |
1da177e4 LT |
2866 | { |
2867 | struct module *mod; | |
2868 | ||
3a642e99 RR |
2869 | if (addr < module_addr_min || addr > module_addr_max) |
2870 | return NULL; | |
2871 | ||
d72b3751 | 2872 | list_for_each_entry_rcu(mod, &modules, list) |
e610499e RR |
2873 | if (within_module_core(addr, mod) |
2874 | || within_module_init(addr, mod)) | |
1da177e4 LT |
2875 | return mod; |
2876 | return NULL; | |
2877 | } | |
c6b37801 | 2878 | EXPORT_SYMBOL_GPL(__module_address); |
1da177e4 | 2879 | |
e610499e RR |
2880 | /* |
2881 | * is_module_text_address - is this address inside module code? | |
2882 | * @addr: the address to check. | |
2883 | * | |
2884 | * See is_module_address() if you simply want to see if the address is | |
2885 | * anywhere in a module. See kernel_text_address() for testing if an | |
2886 | * address corresponds to kernel or module code. | |
2887 | */ | |
2888 | bool is_module_text_address(unsigned long addr) | |
2889 | { | |
2890 | bool ret; | |
2891 | ||
2892 | preempt_disable(); | |
2893 | ret = __module_text_address(addr) != NULL; | |
2894 | preempt_enable(); | |
2895 | ||
2896 | return ret; | |
2897 | } | |
2898 | ||
2899 | /* | |
2900 | * __module_text_address - get the module whose code contains an address. | |
2901 | * @addr: the address. | |
2902 | * | |
2903 | * Must be called with preempt disabled or module mutex held so that | |
2904 | * module doesn't get freed during this. | |
2905 | */ | |
2906 | struct module *__module_text_address(unsigned long addr) | |
2907 | { | |
2908 | struct module *mod = __module_address(addr); | |
2909 | if (mod) { | |
2910 | /* Make sure it's within the text section. */ | |
2911 | if (!within(addr, mod->module_init, mod->init_text_size) | |
2912 | && !within(addr, mod->module_core, mod->core_text_size)) | |
2913 | mod = NULL; | |
2914 | } | |
2915 | return mod; | |
2916 | } | |
c6b37801 | 2917 | EXPORT_SYMBOL_GPL(__module_text_address); |
e610499e | 2918 | |
1da177e4 LT |
2919 | /* Don't grab lock, we're oopsing. */ |
2920 | void print_modules(void) | |
2921 | { | |
2922 | struct module *mod; | |
2bc2d61a | 2923 | char buf[8]; |
1da177e4 | 2924 | |
b231125a | 2925 | printk(KERN_DEFAULT "Modules linked in:"); |
d72b3751 AK |
2926 | /* Most callers should already have preempt disabled, but make sure */ |
2927 | preempt_disable(); | |
2928 | list_for_each_entry_rcu(mod, &modules, list) | |
21aa9280 | 2929 | printk(" %s%s", mod->name, module_flags(mod, buf)); |
d72b3751 | 2930 | preempt_enable(); |
e14af7ee AV |
2931 | if (last_unloaded_module[0]) |
2932 | printk(" [last unloaded: %s]", last_unloaded_module); | |
1da177e4 LT |
2933 | printk("\n"); |
2934 | } | |
2935 | ||
1da177e4 | 2936 | #ifdef CONFIG_MODVERSIONS |
8c8ef42a RR |
2937 | /* Generate the signature for all relevant module structures here. |
2938 | * If these change, we don't want to try to parse the module. */ | |
2939 | void module_layout(struct module *mod, | |
2940 | struct modversion_info *ver, | |
2941 | struct kernel_param *kp, | |
2942 | struct kernel_symbol *ks, | |
2943 | struct marker *marker, | |
2944 | struct tracepoint *tp) | |
2945 | { | |
2946 | } | |
2947 | EXPORT_SYMBOL(module_layout); | |
1da177e4 | 2948 | #endif |
8256e47c MD |
2949 | |
2950 | #ifdef CONFIG_MARKERS | |
fb40bd78 | 2951 | void module_update_markers(void) |
8256e47c MD |
2952 | { |
2953 | struct module *mod; | |
2954 | ||
2955 | mutex_lock(&module_mutex); | |
2956 | list_for_each_entry(mod, &modules, list) | |
2957 | if (!mod->taints) | |
2958 | marker_update_probe_range(mod->markers, | |
fb40bd78 | 2959 | mod->markers + mod->num_markers); |
8256e47c MD |
2960 | mutex_unlock(&module_mutex); |
2961 | } | |
2962 | #endif | |
97e1c18e MD |
2963 | |
2964 | #ifdef CONFIG_TRACEPOINTS | |
2965 | void module_update_tracepoints(void) | |
2966 | { | |
2967 | struct module *mod; | |
2968 | ||
2969 | mutex_lock(&module_mutex); | |
2970 | list_for_each_entry(mod, &modules, list) | |
2971 | if (!mod->taints) | |
2972 | tracepoint_update_probe_range(mod->tracepoints, | |
2973 | mod->tracepoints + mod->num_tracepoints); | |
2974 | mutex_unlock(&module_mutex); | |
2975 | } | |
2976 | ||
2977 | /* | |
2978 | * Returns 0 if current not found. | |
2979 | * Returns 1 if current found. | |
2980 | */ | |
2981 | int module_get_iter_tracepoints(struct tracepoint_iter *iter) | |
2982 | { | |
2983 | struct module *iter_mod; | |
2984 | int found = 0; | |
2985 | ||
2986 | mutex_lock(&module_mutex); | |
2987 | list_for_each_entry(iter_mod, &modules, list) { | |
2988 | if (!iter_mod->taints) { | |
2989 | /* | |
2990 | * Sorted module list | |
2991 | */ | |
2992 | if (iter_mod < iter->module) | |
2993 | continue; | |
2994 | else if (iter_mod > iter->module) | |
2995 | iter->tracepoint = NULL; | |
2996 | found = tracepoint_get_iter_range(&iter->tracepoint, | |
2997 | iter_mod->tracepoints, | |
2998 | iter_mod->tracepoints | |
2999 | + iter_mod->num_tracepoints); | |
3000 | if (found) { | |
3001 | iter->module = iter_mod; | |
3002 | break; | |
3003 | } | |
3004 | } | |
3005 | } | |
3006 | mutex_unlock(&module_mutex); | |
3007 | return found; | |
3008 | } | |
3009 | #endif |