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