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