File: /usr/src/linux/kernel/module.c
1 #include <linux/config.h>
2 #include <linux/mm.h>
3 #include <linux/module.h>
4 #include <asm/module.h>
5 #include <asm/uaccess.h>
6 #include <linux/vmalloc.h>
7 #include <linux/smp_lock.h>
8 #include <asm/pgalloc.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/kmod.h>
12
13 /*
14 * Originally by Anonymous (as far as I know...)
15 * Linux version by Bas Laarhoven <bas@vimec.nl>
16 * 0.99.14 version by Jon Tombs <jon@gtex02.us.es>,
17 * Heavily modified by Bjorn Ekwall <bj0rn@blox.se> May 1994 (C)
18 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
19 * Add MOD_INITIALIZING Keith Owens <kaos@ocs.com.au> Nov 1999
20 * Add kallsyms support, Keith Owens <kaos@ocs.com.au> Apr 2000
21 * Add asm/module support, IA64 has special requirements. Keith Owens <kaos@ocs.com.au> Sep 2000
22 * Fix assorted bugs in module verification. Keith Owens <kaos@ocs.com.au> Sep 2000
23 * Fix sys_init_module race, Andrew Morton <andrewm@uow.edu.au> Oct 2000
24 * http://www.uwsg.iu.edu/hypermail/linux/kernel/0008.3/0379.html
25 * Replace xxx_module_symbol with inter_module_xxx. Keith Owens <kaos@ocs.com.au> Oct 2000
26 * Add a module list lock for kernel fault race fixing. Alan Cox <alan@redhat.com>
27 *
28 * This source is covered by the GNU GPL, the same as all kernel sources.
29 */
30
31 #if defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS)
32
33 extern struct module_symbol __start___ksymtab[];
34 extern struct module_symbol __stop___ksymtab[];
35
36 extern const struct exception_table_entry __start___ex_table[];
37 extern const struct exception_table_entry __stop___ex_table[];
38
39 extern const char __start___kallsyms[] __attribute__ ((weak));
40 extern const char __stop___kallsyms[] __attribute__ ((weak));
41
42 struct module kernel_module =
43 {
44 size_of_struct: sizeof(struct module),
45 name: "",
46 uc: {ATOMIC_INIT(1)},
47 flags: MOD_RUNNING,
48 syms: __start___ksymtab,
49 ex_table_start: __start___ex_table,
50 ex_table_end: __stop___ex_table,
51 kallsyms_start: __start___kallsyms,
52 kallsyms_end: __stop___kallsyms,
53 };
54
55 struct module *module_list = &kernel_module;
56
57 #endif /* defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS) */
58
59 /* inter_module functions are always available, even when the kernel is
60 * compiled without modules. Consumers of inter_module_xxx routines
61 * will always work, even when both are built into the kernel, this
62 * approach removes lots of #ifdefs in mainline code.
63 */
64
65 static struct list_head ime_list = LIST_HEAD_INIT(ime_list);
66 static spinlock_t ime_lock = SPIN_LOCK_UNLOCKED;
67 static int kmalloc_failed;
68
69 /*
70 * This lock prevents modifications that might race the kernel fault
71 * fixups. It does not prevent reader walks that the modules code
72 * does. The kernel lock does that.
73 *
74 * Since vmalloc fault fixups occur in any context this lock is taken
75 * irqsave at all times.
76 */
77
78 spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
79
80 /**
81 * inter_module_register - register a new set of inter module data.
82 * @im_name: an arbitrary string to identify the data, must be unique
83 * @owner: module that is registering the data, always use THIS_MODULE
84 * @userdata: pointer to arbitrary userdata to be registered
85 *
86 * Description: Check that the im_name has not already been registered,
87 * complain if it has. For new data, add it to the inter_module_entry
88 * list.
89 */
90 void inter_module_register(const char *im_name, struct module *owner, const void *userdata)
91 {
92 struct list_head *tmp;
93 struct inter_module_entry *ime, *ime_new;
94
95 if (!(ime_new = kmalloc(sizeof(*ime), GFP_KERNEL))) {
96 /* Overloaded kernel, not fatal */
97 printk(KERN_ERR
98 "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n",
99 im_name);
100 kmalloc_failed = 1;
101 return;
102 }
103 memset(ime_new, 0, sizeof(*ime_new));
104 ime_new->im_name = im_name;
105 ime_new->owner = owner;
106 ime_new->userdata = userdata;
107
108 spin_lock(&ime_lock);
109 list_for_each(tmp, &ime_list) {
110 ime = list_entry(tmp, struct inter_module_entry, list);
111 if (strcmp(ime->im_name, im_name) == 0) {
112 spin_unlock(&ime_lock);
113 kfree(ime_new);
114 /* Program logic error, fatal */
115 printk(KERN_ERR "inter_module_register: duplicate im_name '%s'", im_name);
116 BUG();
117 }
118 }
119 list_add(&(ime_new->list), &ime_list);
120 spin_unlock(&ime_lock);
121 }
122
123 /**
124 * inter_module_unregister - unregister a set of inter module data.
125 * @im_name: an arbitrary string to identify the data, must be unique
126 *
127 * Description: Check that the im_name has been registered, complain if
128 * it has not. For existing data, remove it from the
129 * inter_module_entry list.
130 */
131 void inter_module_unregister(const char *im_name)
132 {
133 struct list_head *tmp;
134 struct inter_module_entry *ime;
135
136 spin_lock(&ime_lock);
137 list_for_each(tmp, &ime_list) {
138 ime = list_entry(tmp, struct inter_module_entry, list);
139 if (strcmp(ime->im_name, im_name) == 0) {
140 list_del(&(ime->list));
141 spin_unlock(&ime_lock);
142 kfree(ime);
143 return;
144 }
145 }
146 spin_unlock(&ime_lock);
147 if (kmalloc_failed) {
148 printk(KERN_ERR
149 "inter_module_unregister: no entry for '%s', "
150 "probably caused by previous kmalloc failure\n",
151 im_name);
152 return;
153 }
154 else {
155 /* Program logic error, fatal */
156 printk(KERN_ERR "inter_module_unregister: no entry for '%s'", im_name);
157 BUG();
158 }
159 }
160
161 /**
162 * inter_module_get - return arbitrary userdata from another module.
163 * @im_name: an arbitrary string to identify the data, must be unique
164 *
165 * Description: If the im_name has not been registered, return NULL.
166 * Try to increment the use count on the owning module, if that fails
167 * then return NULL. Otherwise return the userdata.
168 */
169 const void *inter_module_get(const char *im_name)
170 {
171 struct list_head *tmp;
172 struct inter_module_entry *ime;
173 const void *result = NULL;
174
175 spin_lock(&ime_lock);
176 list_for_each(tmp, &ime_list) {
177 ime = list_entry(tmp, struct inter_module_entry, list);
178 if (strcmp(ime->im_name, im_name) == 0) {
179 if (try_inc_mod_count(ime->owner))
180 result = ime->userdata;
181 break;
182 }
183 }
184 spin_unlock(&ime_lock);
185 return(result);
186 }
187
188 /**
189 * inter_module_get_request - im get with automatic request_module.
190 * @im_name: an arbitrary string to identify the data, must be unique
191 * @modname: module that is expected to register im_name
192 *
193 * Description: If inter_module_get fails, do request_module then retry.
194 */
195 const void *inter_module_get_request(const char *im_name, const char *modname)
196 {
197 const void *result = inter_module_get(im_name);
198 if (!result) {
199 request_module(modname);
200 result = inter_module_get(im_name);
201 }
202 return(result);
203 }
204
205 /**
206 * inter_module_put - release use of data from another module.
207 * @im_name: an arbitrary string to identify the data, must be unique
208 *
209 * Description: If the im_name has not been registered, complain,
210 * otherwise decrement the use count on the owning module.
211 */
212 void inter_module_put(const char *im_name)
213 {
214 struct list_head *tmp;
215 struct inter_module_entry *ime;
216
217 spin_lock(&ime_lock);
218 list_for_each(tmp, &ime_list) {
219 ime = list_entry(tmp, struct inter_module_entry, list);
220 if (strcmp(ime->im_name, im_name) == 0) {
221 if (ime->owner)
222 __MOD_DEC_USE_COUNT(ime->owner);
223 spin_unlock(&ime_lock);
224 return;
225 }
226 }
227 spin_unlock(&ime_lock);
228 printk(KERN_ERR "inter_module_put: no entry for '%s'", im_name);
229 BUG();
230 }
231
232
233 #if defined(CONFIG_MODULES) /* The rest of the source */
234
235 static long get_mod_name(const char *user_name, char **buf);
236 static void put_mod_name(char *buf);
237 struct module *find_module(const char *name);
238 void free_module(struct module *, int tag_freed);
239
240
241 /*
242 * Called at boot time
243 */
244
245 void __init init_modules(void)
246 {
247 kernel_module.nsyms = __stop___ksymtab - __start___ksymtab;
248
249 arch_init_modules(&kernel_module);
250 }
251
252 /*
253 * Copy the name of a module from user space.
254 */
255
256 static inline long
257 get_mod_name(const char *user_name, char **buf)
258 {
259 unsigned long page;
260 long retval;
261
262 page = __get_free_page(GFP_KERNEL);
263 if (!page)
264 return -ENOMEM;
265
266 retval = strncpy_from_user((char *)page, user_name, PAGE_SIZE);
267 if (retval > 0) {
268 if (retval < PAGE_SIZE) {
269 *buf = (char *)page;
270 return retval;
271 }
272 retval = -ENAMETOOLONG;
273 } else if (!retval)
274 retval = -EINVAL;
275
276 free_page(page);
277 return retval;
278 }
279
280 static inline void
281 put_mod_name(char *buf)
282 {
283 free_page((unsigned long)buf);
284 }
285
286 /*
287 * Allocate space for a module.
288 */
289
290 asmlinkage unsigned long
291 sys_create_module(const char *name_user, size_t size)
292 {
293 char *name;
294 long namelen, error;
295 struct module *mod;
296 unsigned long flags;
297
298 if (!capable(CAP_SYS_MODULE))
299 return -EPERM;
300 lock_kernel();
301 if ((namelen = get_mod_name(name_user, &name)) < 0) {
302 error = namelen;
303 goto err0;
304 }
305 if (size < sizeof(struct module)+namelen) {
306 error = -EINVAL;
307 goto err1;
308 }
309 if (find_module(name) != NULL) {
310 error = -EEXIST;
311 goto err1;
312 }
313 if ((mod = (struct module *)module_map(size)) == NULL) {
314 error = -ENOMEM;
315 goto err1;
316 }
317
318 memset(mod, 0, sizeof(*mod));
319 mod->size_of_struct = sizeof(*mod);
320 mod->name = (char *)(mod + 1);
321 mod->size = size;
322 memcpy((char*)(mod+1), name, namelen+1);
323
324 put_mod_name(name);
325
326 spin_lock_irqsave(&modlist_lock, flags);
327 mod->next = module_list;
328 module_list = mod; /* link it in */
329 spin_unlock_irqrestore(&modlist_lock, flags);
330
331 error = (long) mod;
332 goto err0;
333 err1:
334 put_mod_name(name);
335 err0:
336 unlock_kernel();
337 return error;
338 }
339
340 /*
341 * Initialize a module.
342 */
343
344 asmlinkage long
345 sys_init_module(const char *name_user, struct module *mod_user)
346 {
347 struct module mod_tmp, *mod;
348 char *name, *n_name, *name_tmp = NULL;
349 long namelen, n_namelen, i, error;
350 unsigned long mod_user_size;
351 struct module_ref *dep;
352
353 if (!capable(CAP_SYS_MODULE))
354 return -EPERM;
355 lock_kernel();
356 if ((namelen = get_mod_name(name_user, &name)) < 0) {
357 error = namelen;
358 goto err0;
359 }
360 if ((mod = find_module(name)) == NULL) {
361 error = -ENOENT;
362 goto err1;
363 }
364
365 /* Check module header size. We allow a bit of slop over the
366 size we are familiar with to cope with a version of insmod
367 for a newer kernel. But don't over do it. */
368 if ((error = get_user(mod_user_size, &mod_user->size_of_struct)) != 0)
369 goto err1;
370 if (mod_user_size < (unsigned long)&((struct module *)0L)->persist_start
371 || mod_user_size > sizeof(struct module) + 16*sizeof(void*)) {
372 printk(KERN_ERR "init_module: Invalid module header size.\n"
373 KERN_ERR "A new version of the modutils is likely "
374 "needed.\n");
375 error = -EINVAL;
376 goto err1;
377 }
378
379 /* Hold the current contents while we play with the user's idea
380 of righteousness. */
381 mod_tmp = *mod;
382 name_tmp = kmalloc(strlen(mod->name) + 1, GFP_KERNEL); /* Where's kstrdup()? */
383 if (name_tmp == NULL) {
384 error = -ENOMEM;
385 goto err1;
386 }
387 strcpy(name_tmp, mod->name);
388
389 error = copy_from_user(mod, mod_user, mod_user_size);
390 if (error) {
391 error = -EFAULT;
392 goto err2;
393 }
394
395 /* Sanity check the size of the module. */
396 error = -EINVAL;
397
398 if (mod->size > mod_tmp.size) {
399 printk(KERN_ERR "init_module: Size of initialized module "
400 "exceeds size of created module.\n");
401 goto err2;
402 }
403
404 /* Make sure all interesting pointers are sane. */
405
406 if (!mod_bound(mod->name, namelen, mod)) {
407 printk(KERN_ERR "init_module: mod->name out of bounds.\n");
408 goto err2;
409 }
410 if (mod->nsyms && !mod_bound(mod->syms, mod->nsyms, mod)) {
411 printk(KERN_ERR "init_module: mod->syms out of bounds.\n");
412 goto err2;
413 }
414 if (mod->ndeps && !mod_bound(mod->deps, mod->ndeps, mod)) {
415 printk(KERN_ERR "init_module: mod->deps out of bounds.\n");
416 goto err2;
417 }
418 if (mod->init && !mod_bound(mod->init, 0, mod)) {
419 printk(KERN_ERR "init_module: mod->init out of bounds.\n");
420 goto err2;
421 }
422 if (mod->cleanup && !mod_bound(mod->cleanup, 0, mod)) {
423 printk(KERN_ERR "init_module: mod->cleanup out of bounds.\n");
424 goto err2;
425 }
426 if (mod->ex_table_start > mod->ex_table_end
427 || (mod->ex_table_start &&
428 !((unsigned long)mod->ex_table_start >= ((unsigned long)mod + mod->size_of_struct)
429 && ((unsigned long)mod->ex_table_end
430 < (unsigned long)mod + mod->size)))
431 || (((unsigned long)mod->ex_table_start
432 - (unsigned long)mod->ex_table_end)
433 % sizeof(struct exception_table_entry))) {
434 printk(KERN_ERR "init_module: mod->ex_table_* invalid.\n");
435 goto err2;
436 }
437 if (mod->flags & ~MOD_AUTOCLEAN) {
438 printk(KERN_ERR "init_module: mod->flags invalid.\n");
439 goto err2;
440 }
441 if (mod_member_present(mod, can_unload)
442 && mod->can_unload && !mod_bound(mod->can_unload, 0, mod)) {
443 printk(KERN_ERR "init_module: mod->can_unload out of bounds.\n");
444 goto err2;
445 }
446 if (mod_member_present(mod, kallsyms_end)) {
447 if (mod->kallsyms_end &&
448 (!mod_bound(mod->kallsyms_start, 0, mod) ||
449 !mod_bound(mod->kallsyms_end, 0, mod))) {
450 printk(KERN_ERR "init_module: mod->kallsyms out of bounds.\n");
451 goto err2;
452 }
453 if (mod->kallsyms_start > mod->kallsyms_end) {
454 printk(KERN_ERR "init_module: mod->kallsyms invalid.\n");
455 goto err2;
456 }
457 }
458 if (mod_member_present(mod, archdata_end)) {
459 if (mod->archdata_end &&
460 (!mod_bound(mod->archdata_start, 0, mod) ||
461 !mod_bound(mod->archdata_end, 0, mod))) {
462 printk(KERN_ERR "init_module: mod->archdata out of bounds.\n");
463 goto err2;
464 }
465 if (mod->archdata_start > mod->archdata_end) {
466 printk(KERN_ERR "init_module: mod->archdata invalid.\n");
467 goto err2;
468 }
469 }
470 if (mod_member_present(mod, kernel_data) && mod->kernel_data) {
471 printk(KERN_ERR "init_module: mod->kernel_data must be zero.\n");
472 goto err2;
473 }
474
475 /* Check that the user isn't doing something silly with the name. */
476
477 if ((n_namelen = get_mod_name(mod->name - (unsigned long)mod
478 + (unsigned long)mod_user,
479 &n_name)) < 0) {
480 printk(KERN_ERR "init_module: get_mod_name failure.\n");
481 error = n_namelen;
482 goto err2;
483 }
484 if (namelen != n_namelen || strcmp(n_name, mod_tmp.name) != 0) {
485 printk(KERN_ERR "init_module: changed module name to "
486 "`%s' from `%s'\n",
487 n_name, mod_tmp.name);
488 goto err3;
489 }
490
491 /* Ok, that's about all the sanity we can stomach; copy the rest. */
492
493 if (copy_from_user((char *)mod+mod_user_size,
494 (char *)mod_user+mod_user_size,
495 mod->size-mod_user_size)) {
496 error = -EFAULT;
497 goto err3;
498 }
499
500 if (module_arch_init(mod))
501 goto err3;
502
503 /* On some machines it is necessary to do something here
504 to make the I and D caches consistent. */
505 flush_icache_range((unsigned long)mod, (unsigned long)mod + mod->size);
506
507 mod->next = mod_tmp.next;
508 mod->refs = NULL;
509
510 /* Sanity check the module's dependents */
511 for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
512 struct module *o, *d = dep->dep;
513
514 /* Make sure the indicated dependencies are really modules. */
515 if (d == mod) {
516 printk(KERN_ERR "init_module: self-referential "
517 "dependency in mod->deps.\n");
518 goto err3;
519 }
520
521 /* Scan the current modules for this dependency */
522 for (o = module_list; o != &kernel_module && o != d; o = o->next)
523 ;
524
525 if (o != d) {
526 printk(KERN_ERR "init_module: found dependency that is "
527 "(no longer?) a module.\n");
528 goto err3;
529 }
530 }
531
532 /* Update module references. */
533 for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
534 struct module *d = dep->dep;
535
536 dep->ref = mod;
537 dep->next_ref = d->refs;
538 d->refs = dep;
539 /* Being referenced by a dependent module counts as a
540 use as far as kmod is concerned. */
541 d->flags |= MOD_USED_ONCE;
542 }
543
544 /* Free our temporary memory. */
545 put_mod_name(n_name);
546 put_mod_name(name);
547
548 /* Initialize the module. */
549 atomic_set(&mod->uc.usecount,1);
550 mod->flags |= MOD_INITIALIZING;
551 if (mod->init && (error = mod->init()) != 0) {
552 atomic_set(&mod->uc.usecount,0);
553 mod->flags &= ~MOD_INITIALIZING;
554 if (error > 0) /* Buggy module */
555 error = -EBUSY;
556 goto err0;
557 }
558 atomic_dec(&mod->uc.usecount);
559
560 /* And set it running. */
561 mod->flags = (mod->flags | MOD_RUNNING) & ~MOD_INITIALIZING;
562 error = 0;
563 goto err0;
564
565 err3:
566 put_mod_name(n_name);
567 err2:
568 *mod = mod_tmp;
569 strcpy((char *)mod->name, name_tmp); /* We know there is room for this */
570 err1:
571 put_mod_name(name);
572 err0:
573 unlock_kernel();
574 kfree(name_tmp);
575 return error;
576 }
577
578 static spinlock_t unload_lock = SPIN_LOCK_UNLOCKED;
579 int try_inc_mod_count(struct module *mod)
580 {
581 int res = 1;
582 if (mod) {
583 spin_lock(&unload_lock);
584 if (mod->flags & MOD_DELETED)
585 res = 0;
586 else
587 __MOD_INC_USE_COUNT(mod);
588 spin_unlock(&unload_lock);
589 }
590 return res;
591 }
592
593 asmlinkage long
594 sys_delete_module(const char *name_user)
595 {
596 struct module *mod, *next;
597 char *name;
598 long error;
599 int something_changed;
600
601 if (!capable(CAP_SYS_MODULE))
602 return -EPERM;
603
604 lock_kernel();
605 if (name_user) {
606 if ((error = get_mod_name(name_user, &name)) < 0)
607 goto out;
608 error = -ENOENT;
609 if ((mod = find_module(name)) == NULL) {
610 put_mod_name(name);
611 goto out;
612 }
613 put_mod_name(name);
614 error = -EBUSY;
615 if (mod->refs != NULL)
616 goto out;
617
618 spin_lock(&unload_lock);
619 if (!__MOD_IN_USE(mod)) {
620 mod->flags |= MOD_DELETED;
621 spin_unlock(&unload_lock);
622 free_module(mod, 0);
623 error = 0;
624 } else {
625 spin_unlock(&unload_lock);
626 }
627 goto out;
628 }
629
630 /* Do automatic reaping */
631 restart:
632 something_changed = 0;
633
634 for (mod = module_list; mod != &kernel_module; mod = next) {
635 next = mod->next;
636 spin_lock(&unload_lock);
637 if (mod->refs == NULL
638 && (mod->flags & MOD_AUTOCLEAN)
639 && (mod->flags & MOD_RUNNING)
640 && !(mod->flags & MOD_DELETED)
641 && (mod->flags & MOD_USED_ONCE)
642 && !__MOD_IN_USE(mod)) {
643 if ((mod->flags & MOD_VISITED)
644 && !(mod->flags & MOD_JUST_FREED)) {
645 spin_unlock(&unload_lock);
646 mod->flags &= ~MOD_VISITED;
647 } else {
648 mod->flags |= MOD_DELETED;
649 spin_unlock(&unload_lock);
650 free_module(mod, 1);
651 something_changed = 1;
652 }
653 } else {
654 spin_unlock(&unload_lock);
655 }
656 }
657
658 if (something_changed)
659 goto restart;
660
661 for (mod = module_list; mod != &kernel_module; mod = mod->next)
662 mod->flags &= ~MOD_JUST_FREED;
663
664 error = 0;
665 out:
666 unlock_kernel();
667 return error;
668 }
669
670 /* Query various bits about modules. */
671
672 static int
673 qm_modules(char *buf, size_t bufsize, size_t *ret)
674 {
675 struct module *mod;
676 size_t nmod, space, len;
677
678 nmod = space = 0;
679
680 for (mod=module_list; mod != &kernel_module; mod=mod->next, ++nmod) {
681 len = strlen(mod->name)+1;
682 if (len > bufsize)
683 goto calc_space_needed;
684 if (copy_to_user(buf, mod->name, len))
685 return -EFAULT;
686 buf += len;
687 bufsize -= len;
688 space += len;
689 }
690
691 if (put_user(nmod, ret))
692 return -EFAULT;
693 else
694 return 0;
695
696 calc_space_needed:
697 space += len;
698 while ((mod = mod->next) != &kernel_module)
699 space += strlen(mod->name)+1;
700
701 if (put_user(space, ret))
702 return -EFAULT;
703 else
704 return -ENOSPC;
705 }
706
707 static int
708 qm_deps(struct module *mod, char *buf, size_t bufsize, size_t *ret)
709 {
710 size_t i, space, len;
711
712 if (mod == &kernel_module)
713 return -EINVAL;
714 if (!MOD_CAN_QUERY(mod))
715 if (put_user(0, ret))
716 return -EFAULT;
717 else
718 return 0;
719
720 space = 0;
721 for (i = 0; i < mod->ndeps; ++i) {
722 const char *dep_name = mod->deps[i].dep->name;
723
724 len = strlen(dep_name)+1;
725 if (len > bufsize)
726 goto calc_space_needed;
727 if (copy_to_user(buf, dep_name, len))
728 return -EFAULT;
729 buf += len;
730 bufsize -= len;
731 space += len;
732 }
733
734 if (put_user(i, ret))
735 return -EFAULT;
736 else
737 return 0;
738
739 calc_space_needed:
740 space += len;
741 while (++i < mod->ndeps)
742 space += strlen(mod->deps[i].dep->name)+1;
743
744 if (put_user(space, ret))
745 return -EFAULT;
746 else
747 return -ENOSPC;
748 }
749
750 static int
751 qm_refs(struct module *mod, char *buf, size_t bufsize, size_t *ret)
752 {
753 size_t nrefs, space, len;
754 struct module_ref *ref;
755
756 if (mod == &kernel_module)
757 return -EINVAL;
758 if (!MOD_CAN_QUERY(mod))
759 if (put_user(0, ret))
760 return -EFAULT;
761 else
762 return 0;
763
764 space = 0;
765 for (nrefs = 0, ref = mod->refs; ref ; ++nrefs, ref = ref->next_ref) {
766 const char *ref_name = ref->ref->name;
767
768 len = strlen(ref_name)+1;
769 if (len > bufsize)
770 goto calc_space_needed;
771 if (copy_to_user(buf, ref_name, len))
772 return -EFAULT;
773 buf += len;
774 bufsize -= len;
775 space += len;
776 }
777
778 if (put_user(nrefs, ret))
779 return -EFAULT;
780 else
781 return 0;
782
783 calc_space_needed:
784 space += len;
785 while ((ref = ref->next_ref) != NULL)
786 space += strlen(ref->ref->name)+1;
787
788 if (put_user(space, ret))
789 return -EFAULT;
790 else
791 return -ENOSPC;
792 }
793
794 static int
795 qm_symbols(struct module *mod, char *buf, size_t bufsize, size_t *ret)
796 {
797 size_t i, space, len;
798 struct module_symbol *s;
799 char *strings;
800 unsigned long *vals;
801
802 if (!MOD_CAN_QUERY(mod))
803 if (put_user(0, ret))
804 return -EFAULT;
805 else
806 return 0;
807
808 space = mod->nsyms * 2*sizeof(void *);
809
810 i = len = 0;
811 s = mod->syms;
812
813 if (space > bufsize)
814 goto calc_space_needed;
815
816 if (!access_ok(VERIFY_WRITE, buf, space))
817 return -EFAULT;
818
819 bufsize -= space;
820 vals = (unsigned long *)buf;
821 strings = buf+space;
822
823 for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
824 len = strlen(s->name)+1;
825 if (len > bufsize)
826 goto calc_space_needed;
827
828 if (copy_to_user(strings, s->name, len)
829 || __put_user(s->value, vals+0)
830 || __put_user(space, vals+1))
831 return -EFAULT;
832
833 strings += len;
834 bufsize -= len;
835 space += len;
836 }
837 if (put_user(i, ret))
838 return -EFAULT;
839 else
840 return 0;
841
842 calc_space_needed:
843 for (; i < mod->nsyms; ++i, ++s)
844 space += strlen(s->name)+1;
845
846 if (put_user(space, ret))
847 return -EFAULT;
848 else
849 return -ENOSPC;
850 }
851
852 static int
853 qm_info(struct module *mod, char *buf, size_t bufsize, size_t *ret)
854 {
855 int error = 0;
856
857 if (mod == &kernel_module)
858 return -EINVAL;
859
860 if (sizeof(struct module_info) <= bufsize) {
861 struct module_info info;
862 info.addr = (unsigned long)mod;
863 info.size = mod->size;
864 info.flags = mod->flags;
865
866 /* usecount is one too high here - report appropriately to
867 compensate for locking */
868 info.usecount = (mod_member_present(mod, can_unload)
869 && mod->can_unload ? -1 : atomic_read(&mod->uc.usecount)-1);
870
871 if (copy_to_user(buf, &info, sizeof(struct module_info)))
872 return -EFAULT;
873 } else
874 error = -ENOSPC;
875
876 if (put_user(sizeof(struct module_info), ret))
877 return -EFAULT;
878
879 return error;
880 }
881
882 asmlinkage long
883 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
884 size_t *ret)
885 {
886 struct module *mod;
887 int err;
888
889 lock_kernel();
890 if (name_user == NULL)
891 mod = &kernel_module;
892 else {
893 long namelen;
894 char *name;
895
896 if ((namelen = get_mod_name(name_user, &name)) < 0) {
897 err = namelen;
898 goto out;
899 }
900 err = -ENOENT;
901 if ((mod = find_module(name)) == NULL) {
902 put_mod_name(name);
903 goto out;
904 }
905 put_mod_name(name);
906 }
907
908 /* __MOD_ touches the flags. We must avoid that */
909
910 atomic_inc(&mod->uc.usecount);
911
912 switch (which)
913 {
914 case 0:
915 err = 0;
916 break;
917 case QM_MODULES:
918 err = qm_modules(buf, bufsize, ret);
919 break;
920 case QM_DEPS:
921 err = qm_deps(mod, buf, bufsize, ret);
922 break;
923 case QM_REFS:
924 err = qm_refs(mod, buf, bufsize, ret);
925 break;
926 case QM_SYMBOLS:
927 err = qm_symbols(mod, buf, bufsize, ret);
928 break;
929 case QM_INFO:
930 err = qm_info(mod, buf, bufsize, ret);
931 break;
932 default:
933 err = -EINVAL;
934 break;
935 }
936 atomic_dec(&mod->uc.usecount);
937
938 out:
939 unlock_kernel();
940 return err;
941 }
942
943 /*
944 * Copy the kernel symbol table to user space. If the argument is
945 * NULL, just return the size of the table.
946 *
947 * This call is obsolete. New programs should use query_module+QM_SYMBOLS
948 * which does not arbitrarily limit the length of symbols.
949 */
950
951 asmlinkage long
952 sys_get_kernel_syms(struct kernel_sym *table)
953 {
954 struct module *mod;
955 int i;
956 struct kernel_sym ksym;
957
958 lock_kernel();
959 for (mod = module_list, i = 0; mod; mod = mod->next) {
960 /* include the count for the module name! */
961 i += mod->nsyms + 1;
962 }
963
964 if (table == NULL)
965 goto out;
966
967 /* So that we don't give the user our stack content */
968 memset (&ksym, 0, sizeof (ksym));
969
970 for (mod = module_list, i = 0; mod; mod = mod->next) {
971 struct module_symbol *msym;
972 unsigned int j;
973
974 if (!MOD_CAN_QUERY(mod))
975 continue;
976
977 /* magic: write module info as a pseudo symbol */
978 ksym.value = (unsigned long)mod;
979 ksym.name[0] = '#';
980 strncpy(ksym.name+1, mod->name, sizeof(ksym.name)-1);
981 ksym.name[sizeof(ksym.name)-1] = '\0';
982
983 if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
984 goto out;
985 ++i, ++table;
986
987 if (mod->nsyms == 0)
988 continue;
989
990 for (j = 0, msym = mod->syms; j < mod->nsyms; ++j, ++msym) {
991 ksym.value = msym->value;
992 strncpy(ksym.name, msym->name, sizeof(ksym.name));
993 ksym.name[sizeof(ksym.name)-1] = '\0';
994
995 if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
996 goto out;
997 ++i, ++table;
998 }
999 }
1000 out:
1001 unlock_kernel();
1002 return i;
1003 }
1004
1005 /*
1006 * Look for a module by name, ignoring modules marked for deletion.
1007 */
1008
1009 struct module *
1010 find_module(const char *name)
1011 {
1012 struct module *mod;
1013
1014 for (mod = module_list; mod ; mod = mod->next) {
1015 if (mod->flags & MOD_DELETED)
1016 continue;
1017 if (!strcmp(mod->name, name))
1018 break;
1019 }
1020
1021 return mod;
1022 }
1023
1024 /*
1025 * Free the given module.
1026 */
1027
1028 void
1029 free_module(struct module *mod, int tag_freed)
1030 {
1031 struct module_ref *dep;
1032 unsigned i;
1033 unsigned long flags;
1034
1035 /* Let the module clean up. */
1036
1037 if (mod->flags & MOD_RUNNING)
1038 {
1039 if(mod->cleanup)
1040 mod->cleanup();
1041 mod->flags &= ~MOD_RUNNING;
1042 }
1043
1044 /* Remove the module from the dependency lists. */
1045
1046 for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
1047 struct module_ref **pp;
1048 for (pp = &dep->dep->refs; *pp != dep; pp = &(*pp)->next_ref)
1049 continue;
1050 *pp = dep->next_ref;
1051 if (tag_freed && dep->dep->refs == NULL)
1052 dep->dep->flags |= MOD_JUST_FREED;
1053 }
1054
1055 /* And from the main module list. */
1056
1057 spin_lock_irqsave(&modlist_lock, flags);
1058 if (mod == module_list) {
1059 module_list = mod->next;
1060 } else {
1061 struct module *p;
1062 for (p = module_list; p->next != mod; p = p->next)
1063 continue;
1064 p->next = mod->next;
1065 }
1066 spin_unlock_irqrestore(&modlist_lock, flags);
1067
1068 /* And free the memory. */
1069
1070 module_unmap(mod);
1071 }
1072
1073 /*
1074 * Called by the /proc file system to return a current list of modules.
1075 */
1076
1077 int get_module_list(char *p)
1078 {
1079 size_t left = PAGE_SIZE;
1080 struct module *mod;
1081 char tmpstr[64];
1082 struct module_ref *ref;
1083
1084 for (mod = module_list; mod != &kernel_module; mod = mod->next) {
1085 long len;
1086 const char *q;
1087
1088 #define safe_copy_str(str, len) \
1089 do { \
1090 if (left < len) \
1091 goto fini; \
1092 memcpy(p, str, len); p += len, left -= len; \
1093 } while (0)
1094 #define safe_copy_cstr(str) safe_copy_str(str, sizeof(str)-1)
1095
1096 len = strlen(mod->name);
1097 safe_copy_str(mod->name, len);
1098
1099 if ((len = 20 - len) > 0) {
1100 if (left < len)
1101 goto fini;
1102 memset(p, ' ', len);
1103 p += len;
1104 left -= len;
1105 }
1106
1107 len = sprintf(tmpstr, "%8lu", mod->size);
1108 safe_copy_str(tmpstr, len);
1109
1110 if (mod->flags & MOD_RUNNING) {
1111 len = sprintf(tmpstr, "%4ld",
1112 (mod_member_present(mod, can_unload)
1113 && mod->can_unload
1114 ? -1L : (long)atomic_read(&mod->uc.usecount)));
1115 safe_copy_str(tmpstr, len);
1116 }
1117
1118 if (mod->flags & MOD_DELETED)
1119 safe_copy_cstr(" (deleted)");
1120 else if (mod->flags & MOD_RUNNING) {
1121 if (mod->flags & MOD_AUTOCLEAN)
1122 safe_copy_cstr(" (autoclean)");
1123 if (!(mod->flags & MOD_USED_ONCE))
1124 safe_copy_cstr(" (unused)");
1125 }
1126 else if (mod->flags & MOD_INITIALIZING)
1127 safe_copy_cstr(" (initializing)");
1128 else
1129 safe_copy_cstr(" (uninitialized)");
1130
1131 if ((ref = mod->refs) != NULL) {
1132 safe_copy_cstr(" [");
1133 while (1) {
1134 q = ref->ref->name;
1135 len = strlen(q);
1136 safe_copy_str(q, len);
1137
1138 if ((ref = ref->next_ref) != NULL)
1139 safe_copy_cstr(" ");
1140 else
1141 break;
1142 }
1143 safe_copy_cstr("]");
1144 }
1145 safe_copy_cstr("\n");
1146
1147 #undef safe_copy_str
1148 #undef safe_copy_cstr
1149 }
1150
1151 fini:
1152 return PAGE_SIZE - left;
1153 }
1154
1155 /*
1156 * Called by the /proc file system to return a current list of ksyms.
1157 */
1158
1159 int
1160 get_ksyms_list(char *buf, char **start, off_t offset, int length)
1161 {
1162 struct module *mod;
1163 char *p = buf;
1164 int len = 0; /* code from net/ipv4/proc.c */
1165 off_t pos = 0;
1166 off_t begin = 0;
1167
1168 for (mod = module_list; mod; mod = mod->next) {
1169 unsigned i;
1170 struct module_symbol *sym;
1171
1172 if (!MOD_CAN_QUERY(mod))
1173 continue;
1174
1175 for (i = mod->nsyms, sym = mod->syms; i > 0; --i, ++sym) {
1176 p = buf + len;
1177 if (*mod->name) {
1178 len += sprintf(p, "%0*lx %s\t[%s]\n",
1179 (int)(2*sizeof(void*)),
1180 sym->value, sym->name,
1181 mod->name);
1182 } else {
1183 len += sprintf(p, "%0*lx %s\n",
1184 (int)(2*sizeof(void*)),
1185 sym->value, sym->name);
1186 }
1187 pos = begin + len;
1188 if (pos < offset) {
1189 len = 0;
1190 begin = pos;
1191 }
1192 pos = begin + len;
1193 if (pos > offset+length)
1194 goto leave_the_loop;
1195 }
1196 }
1197 leave_the_loop:
1198 *start = buf + (offset - begin);
1199 len -= (offset - begin);
1200 if (len > length)
1201 len = length;
1202 return len;
1203 }
1204
1205 #else /* CONFIG_MODULES */
1206
1207 /* Dummy syscalls for people who don't want modules */
1208
1209 asmlinkage unsigned long
1210 sys_create_module(const char *name_user, size_t size)
1211 {
1212 return -ENOSYS;
1213 }
1214
1215 asmlinkage long
1216 sys_init_module(const char *name_user, struct module *mod_user)
1217 {
1218 return -ENOSYS;
1219 }
1220
1221 asmlinkage long
1222 sys_delete_module(const char *name_user)
1223 {
1224 return -ENOSYS;
1225 }
1226
1227 asmlinkage long
1228 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
1229 size_t *ret)
1230 {
1231 /* Let the program know about the new interface. Not that
1232 it'll do them much good. */
1233 if (which == 0)
1234 return 0;
1235
1236 return -ENOSYS;
1237 }
1238
1239 asmlinkage long
1240 sys_get_kernel_syms(struct kernel_sym *table)
1241 {
1242 return -ENOSYS;
1243 }
1244
1245 int try_inc_mod_count(struct module *mod)
1246 {
1247 return 1;
1248 }
1249
1250 #endif /* CONFIG_MODULES */
1251