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