1 #include <linux/config.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>
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
27 * This source is covered by the GNU GPL, the same as all kernel sources.
30 #if defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS)
32 extern struct module_symbol __start___ksymtab
[];
33 extern struct module_symbol __stop___ksymtab
[];
35 extern const struct exception_table_entry __start___ex_table
[];
36 extern const struct exception_table_entry __stop___ex_table
[];
38 extern const char __start___kallsyms
[] __attribute__ ((weak
));
39 extern const char __stop___kallsyms
[] __attribute__ ((weak
));
41 static struct module kernel_module
=
43 size_of_struct
: sizeof(struct module
),
47 syms
: __start___ksymtab
,
48 ex_table_start
: __start___ex_table
,
49 ex_table_end
: __stop___ex_table
,
50 kallsyms_start
: __start___kallsyms
,
51 kallsyms_end
: __stop___kallsyms
,
54 struct module
*module_list
= &kernel_module
;
56 #endif /* defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS) */
58 /* inter_module functions are always available, even when the kernel is
59 * compiled without modules. Consumers of inter_module_xxx routines
60 * will always work, even when both are built into the kernel, this
61 * approach removes lots of #ifdefs in mainline code.
64 static struct list_head ime_list
= LIST_HEAD_INIT(ime_list
);
65 static spinlock_t ime_lock
= SPIN_LOCK_UNLOCKED
;
66 static int kmalloc_failed
;
69 * inter_module_register - register a new set of inter module data.
70 * @im_name: an arbitrary string to identify the data, must be unique
71 * @owner: module that is registering the data, always use THIS_MODULE
72 * @userdata: pointer to arbitrary userdata to be registered
74 * Description: Check that the im_name has not already been registered,
75 * complain if it has. For new data, add it to the inter_module_entry
78 void inter_module_register(const char *im_name
, struct module
*owner
, const void *userdata
)
80 struct list_head
*tmp
;
81 struct inter_module_entry
*ime
, *ime_new
;
83 if (!(ime_new
= kmalloc(sizeof(*ime
), GFP_KERNEL
))) {
84 /* Overloaded kernel, not fatal */
86 "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n",
91 memset(ime_new
, 0, sizeof(*ime_new
));
92 ime_new
->im_name
= im_name
;
93 ime_new
->owner
= owner
;
94 ime_new
->userdata
= userdata
;
97 list_for_each(tmp
, &ime_list
) {
98 ime
= list_entry(tmp
, struct inter_module_entry
, list
);
99 if (strcmp(ime
->im_name
, im_name
) == 0) {
100 spin_unlock(&ime_lock
);
102 /* Program logic error, fatal */
103 printk(KERN_ERR
"inter_module_register: duplicate im_name '%s'", im_name
);
107 list_add(&(ime_new
->list
), &ime_list
);
108 spin_unlock(&ime_lock
);
112 * inter_module_unregister - unregister a set of inter module data.
113 * @im_name: an arbitrary string to identify the data, must be unique
115 * Description: Check that the im_name has been registered, complain if
116 * it has not. For existing data, remove it from the
117 * inter_module_entry list.
119 void inter_module_unregister(const char *im_name
)
121 struct list_head
*tmp
;
122 struct inter_module_entry
*ime
;
124 spin_lock(&ime_lock
);
125 list_for_each(tmp
, &ime_list
) {
126 ime
= list_entry(tmp
, struct inter_module_entry
, list
);
127 if (strcmp(ime
->im_name
, im_name
) == 0) {
128 list_del(&(ime
->list
));
129 spin_unlock(&ime_lock
);
134 spin_unlock(&ime_lock
);
135 if (kmalloc_failed
) {
137 "inter_module_unregister: no entry for '%s', "
138 "probably caused by previous kmalloc failure\n",
143 /* Program logic error, fatal */
144 printk(KERN_ERR
"inter_module_unregister: no entry for '%s'", im_name
);
150 * inter_module_get - return arbitrary userdata from another module.
151 * @im_name: an arbitrary string to identify the data, must be unique
153 * Description: If the im_name has not been registered, return NULL.
154 * Try to increment the use count on the owning module, if that fails
155 * then return NULL. Otherwise return the userdata.
157 const void *inter_module_get(const char *im_name
)
159 struct list_head
*tmp
;
160 struct inter_module_entry
*ime
;
161 const void *result
= NULL
;
163 spin_lock(&ime_lock
);
164 list_for_each(tmp
, &ime_list
) {
165 ime
= list_entry(tmp
, struct inter_module_entry
, list
);
166 if (strcmp(ime
->im_name
, im_name
) == 0) {
167 if (try_inc_mod_count(ime
->owner
))
168 result
= ime
->userdata
;
172 spin_unlock(&ime_lock
);
177 * inter_module_get_request - im get with automatic request_module.
178 * @im_name: an arbitrary string to identify the data, must be unique
179 * @modname: module that is expected to register im_name
181 * Description: If inter_module_get fails, do request_module then retry.
183 const void *inter_module_get_request(const char *im_name
, const char *modname
)
185 const void *result
= inter_module_get(im_name
);
187 request_module(modname
);
188 result
= inter_module_get(im_name
);
194 * inter_module_put - release use of data from another module.
195 * @im_name: an arbitrary string to identify the data, must be unique
197 * Description: If the im_name has not been registered, complain,
198 * otherwise decrement the use count on the owning module.
200 void inter_module_put(const char *im_name
)
202 struct list_head
*tmp
;
203 struct inter_module_entry
*ime
;
205 spin_lock(&ime_lock
);
206 list_for_each(tmp
, &ime_list
) {
207 ime
= list_entry(tmp
, struct inter_module_entry
, list
);
208 if (strcmp(ime
->im_name
, im_name
) == 0) {
210 __MOD_DEC_USE_COUNT(ime
->owner
);
211 spin_unlock(&ime_lock
);
215 spin_unlock(&ime_lock
);
216 printk(KERN_ERR
"inter_module_put: no entry for '%s'", im_name
);
221 #if defined(CONFIG_MODULES) /* The rest of the source */
223 static long get_mod_name(const char *user_name
, char **buf
);
224 static void put_mod_name(char *buf
);
225 struct module
*find_module(const char *name
);
226 void free_module(struct module
*, int tag_freed
);
230 * Called at boot time
233 void __init
init_modules(void)
235 kernel_module
.nsyms
= __stop___ksymtab
- __start___ksymtab
;
238 __asm__("stq $29,%0" : "=m"(kernel_module
.gp
));
243 * Copy the name of a module from user space.
247 get_mod_name(const char *user_name
, char **buf
)
252 page
= __get_free_page(GFP_KERNEL
);
256 retval
= strncpy_from_user((char *)page
, user_name
, PAGE_SIZE
);
258 if (retval
< PAGE_SIZE
) {
262 retval
= -ENAMETOOLONG
;
271 put_mod_name(char *buf
)
273 free_page((unsigned long)buf
);
277 * Allocate space for a module.
280 asmlinkage
unsigned long
281 sys_create_module(const char *name_user
, size_t size
)
287 if (!capable(CAP_SYS_MODULE
))
290 if ((namelen
= get_mod_name(name_user
, &name
)) < 0) {
294 if (size
< sizeof(struct module
)+namelen
) {
298 if (find_module(name
) != NULL
) {
302 if ((mod
= (struct module
*)module_map(size
)) == NULL
) {
307 memset(mod
, 0, sizeof(*mod
));
308 mod
->size_of_struct
= sizeof(*mod
);
309 mod
->next
= module_list
;
310 mod
->name
= (char *)(mod
+ 1);
312 memcpy((char*)(mod
+1), name
, namelen
+1);
316 module_list
= mod
; /* link it in */
328 * Initialize a module.
332 sys_init_module(const char *name_user
, struct module
*mod_user
)
334 struct module mod_tmp
, *mod
;
335 char *name
, *n_name
, *name_tmp
= NULL
;
336 long namelen
, n_namelen
, i
, error
;
337 unsigned long mod_user_size
;
338 struct module_ref
*dep
;
340 if (!capable(CAP_SYS_MODULE
))
343 if ((namelen
= get_mod_name(name_user
, &name
)) < 0) {
347 if ((mod
= find_module(name
)) == NULL
) {
352 /* Check module header size. We allow a bit of slop over the
353 size we are familiar with to cope with a version of insmod
354 for a newer kernel. But don't over do it. */
355 if ((error
= get_user(mod_user_size
, &mod_user
->size_of_struct
)) != 0)
357 if (mod_user_size
< (unsigned long)&((struct module
*)0L)->persist_start
358 || mod_user_size
> sizeof(struct module
) + 16*sizeof(void*)) {
359 printk(KERN_ERR
"init_module: Invalid module header size.\n"
360 KERN_ERR
"A new version of the modutils is likely "
366 /* Hold the current contents while we play with the user's idea
369 name_tmp
= kmalloc(strlen(mod
->name
) + 1, GFP_KERNEL
); /* Where's kstrdup()? */
370 if (name_tmp
== NULL
) {
374 strcpy(name_tmp
, mod
->name
);
376 error
= copy_from_user(mod
, mod_user
, mod_user_size
);
382 /* Sanity check the size of the module. */
385 if (mod
->size
> mod_tmp
.size
) {
386 printk(KERN_ERR
"init_module: Size of initialized module "
387 "exceeds size of created module.\n");
391 /* Make sure all interesting pointers are sane. */
393 if (!mod_bound(mod
->name
, namelen
, mod
)) {
394 printk(KERN_ERR
"init_module: mod->name out of bounds.\n");
397 if (mod
->nsyms
&& !mod_bound(mod
->syms
, mod
->nsyms
, mod
)) {
398 printk(KERN_ERR
"init_module: mod->syms out of bounds.\n");
401 if (mod
->ndeps
&& !mod_bound(mod
->deps
, mod
->ndeps
, mod
)) {
402 printk(KERN_ERR
"init_module: mod->deps out of bounds.\n");
405 if (mod
->init
&& !mod_bound(mod
->init
, 0, mod
)) {
406 printk(KERN_ERR
"init_module: mod->init out of bounds.\n");
409 if (mod
->cleanup
&& !mod_bound(mod
->cleanup
, 0, mod
)) {
410 printk(KERN_ERR
"init_module: mod->cleanup out of bounds.\n");
413 if (mod
->ex_table_start
> mod
->ex_table_end
414 || (mod
->ex_table_start
&&
415 !((unsigned long)mod
->ex_table_start
>= ((unsigned long)mod
+ mod
->size_of_struct
)
416 && ((unsigned long)mod
->ex_table_end
417 < (unsigned long)mod
+ mod
->size
)))
418 || (((unsigned long)mod
->ex_table_start
419 - (unsigned long)mod
->ex_table_end
)
420 % sizeof(struct exception_table_entry
))) {
421 printk(KERN_ERR
"init_module: mod->ex_table_* invalid.\n");
424 if (mod
->flags
& ~MOD_AUTOCLEAN
) {
425 printk(KERN_ERR
"init_module: mod->flags invalid.\n");
429 if (!mod_bound(mod
->gp
- 0x8000, 0, mod
)) {
430 printk(KERN_ERR
"init_module: mod->gp out of bounds.\n");
434 if (mod_member_present(mod
, can_unload
)
435 && mod
->can_unload
&& !mod_bound(mod
->can_unload
, 0, mod
)) {
436 printk(KERN_ERR
"init_module: mod->can_unload out of bounds.\n");
439 if (mod_member_present(mod
, kallsyms_end
)) {
440 if (mod
->kallsyms_end
&&
441 (!mod_bound(mod
->kallsyms_start
, 0, mod
) ||
442 !mod_bound(mod
->kallsyms_end
, 0, mod
))) {
443 printk(KERN_ERR
"init_module: mod->kallsyms out of bounds.\n");
446 if (mod
->kallsyms_start
> mod
->kallsyms_end
) {
447 printk(KERN_ERR
"init_module: mod->kallsyms invalid.\n");
451 if (mod_member_present(mod
, archdata_end
)) {
452 if (mod
->archdata_end
&&
453 (!mod_bound(mod
->archdata_start
, 0, mod
) ||
454 !mod_bound(mod
->archdata_end
, 0, mod
))) {
455 printk(KERN_ERR
"init_module: mod->archdata out of bounds.\n");
458 if (mod
->archdata_start
> mod
->archdata_end
) {
459 printk(KERN_ERR
"init_module: mod->archdata invalid.\n");
463 if (mod_member_present(mod
, kernel_data
) && mod
->kernel_data
) {
464 printk(KERN_ERR
"init_module: mod->kernel_data must be zero.\n");
468 /* Check that the user isn't doing something silly with the name. */
470 if ((n_namelen
= get_mod_name(mod
->name
- (unsigned long)mod
471 + (unsigned long)mod_user
,
473 printk(KERN_ERR
"init_module: get_mod_name failure.\n");
477 if (namelen
!= n_namelen
|| strcmp(n_name
, mod_tmp
.name
) != 0) {
478 printk(KERN_ERR
"init_module: changed module name to "
480 n_name
, mod_tmp
.name
);
484 /* Ok, that's about all the sanity we can stomach; copy the rest. */
486 if (copy_from_user((char *)mod
+mod_user_size
,
487 (char *)mod_user
+mod_user_size
,
488 mod
->size
-mod_user_size
)) {
493 if (module_arch_init(mod
))
496 /* On some machines it is necessary to do something here
497 to make the I and D caches consistent. */
498 flush_icache_range((unsigned long)mod
, (unsigned long)mod
+ mod
->size
);
500 mod
->next
= mod_tmp
.next
;
503 /* Sanity check the module's dependents */
504 for (i
= 0, dep
= mod
->deps
; i
< mod
->ndeps
; ++i
, ++dep
) {
505 struct module
*o
, *d
= dep
->dep
;
507 /* Make sure the indicated dependencies are really modules. */
509 printk(KERN_ERR
"init_module: self-referential "
510 "dependency in mod->deps.\n");
514 /* Scan the current modules for this dependency */
515 for (o
= module_list
; o
!= &kernel_module
&& o
!= d
; o
= o
->next
)
519 printk(KERN_ERR
"init_module: found dependency that is "
520 "(no longer?) a module.\n");
525 /* Update module references. */
526 for (i
= 0, dep
= mod
->deps
; i
< mod
->ndeps
; ++i
, ++dep
) {
527 struct module
*d
= dep
->dep
;
530 dep
->next_ref
= d
->refs
;
532 /* Being referenced by a dependent module counts as a
533 use as far as kmod is concerned. */
534 d
->flags
|= MOD_USED_ONCE
;
537 /* Free our temporary memory. */
538 put_mod_name(n_name
);
541 /* Initialize the module. */
542 mod
->flags
|= MOD_INITIALIZING
;
543 atomic_set(&mod
->uc
.usecount
,1);
544 if (mod
->init
&& (error
= mod
->init()) != 0) {
545 atomic_set(&mod
->uc
.usecount
,0);
546 mod
->flags
&= ~MOD_INITIALIZING
;
547 if (error
> 0) /* Buggy module */
551 atomic_dec(&mod
->uc
.usecount
);
553 /* And set it running. */
554 mod
->flags
= (mod
->flags
| MOD_RUNNING
) & ~MOD_INITIALIZING
;
559 put_mod_name(n_name
);
562 strcpy((char *)mod
->name
, name_tmp
); /* We know there is room for this */
571 static spinlock_t unload_lock
= SPIN_LOCK_UNLOCKED
;
572 int try_inc_mod_count(struct module
*mod
)
576 spin_lock(&unload_lock
);
577 if (mod
->flags
& MOD_DELETED
)
580 __MOD_INC_USE_COUNT(mod
);
581 spin_unlock(&unload_lock
);
587 sys_delete_module(const char *name_user
)
589 struct module
*mod
, *next
;
592 int something_changed
;
594 if (!capable(CAP_SYS_MODULE
))
599 if ((error
= get_mod_name(name_user
, &name
)) < 0)
607 if ((mod
= find_module(name
)) == NULL
) {
613 if (mod
->refs
!= NULL
)
616 spin_lock(&unload_lock
);
617 if (!__MOD_IN_USE(mod
)) {
618 mod
->flags
|= MOD_DELETED
;
619 spin_unlock(&unload_lock
);
623 spin_unlock(&unload_lock
);
628 /* Do automatic reaping */
630 something_changed
= 0;
631 for (mod
= module_list
; mod
!= &kernel_module
; mod
= next
) {
633 spin_lock(&unload_lock
);
634 if (mod
->refs
== NULL
635 && (mod
->flags
& MOD_AUTOCLEAN
)
636 && (mod
->flags
& MOD_RUNNING
)
637 && !(mod
->flags
& MOD_DELETED
)
638 && (mod
->flags
& MOD_USED_ONCE
)
639 && !__MOD_IN_USE(mod
)) {
640 if ((mod
->flags
& MOD_VISITED
)
641 && !(mod
->flags
& MOD_JUST_FREED
)) {
642 spin_unlock(&unload_lock
);
643 mod
->flags
&= ~MOD_VISITED
;
645 mod
->flags
|= MOD_DELETED
;
646 spin_unlock(&unload_lock
);
648 something_changed
= 1;
651 spin_unlock(&unload_lock
);
654 if (something_changed
)
656 for (mod
= module_list
; mod
!= &kernel_module
; mod
= mod
->next
)
657 mod
->flags
&= ~MOD_JUST_FREED
;
664 /* Query various bits about modules. */
667 qm_modules(char *buf
, size_t bufsize
, size_t *ret
)
670 size_t nmod
, space
, len
;
674 for (mod
=module_list
; mod
!= &kernel_module
; mod
=mod
->next
, ++nmod
) {
675 len
= strlen(mod
->name
)+1;
677 goto calc_space_needed
;
678 if (copy_to_user(buf
, mod
->name
, len
))
685 if (put_user(nmod
, ret
))
692 while ((mod
= mod
->next
) != &kernel_module
)
693 space
+= strlen(mod
->name
)+1;
695 if (put_user(space
, ret
))
702 qm_deps(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
704 size_t i
, space
, len
;
706 if (mod
== &kernel_module
)
708 if (!MOD_CAN_QUERY(mod
))
709 if (put_user(0, ret
))
715 for (i
= 0; i
< mod
->ndeps
; ++i
) {
716 const char *dep_name
= mod
->deps
[i
].dep
->name
;
718 len
= strlen(dep_name
)+1;
720 goto calc_space_needed
;
721 if (copy_to_user(buf
, dep_name
, len
))
728 if (put_user(i
, ret
))
735 while (++i
< mod
->ndeps
)
736 space
+= strlen(mod
->deps
[i
].dep
->name
)+1;
738 if (put_user(space
, ret
))
745 qm_refs(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
747 size_t nrefs
, space
, len
;
748 struct module_ref
*ref
;
750 if (mod
== &kernel_module
)
752 if (!MOD_CAN_QUERY(mod
))
753 if (put_user(0, ret
))
759 for (nrefs
= 0, ref
= mod
->refs
; ref
; ++nrefs
, ref
= ref
->next_ref
) {
760 const char *ref_name
= ref
->ref
->name
;
762 len
= strlen(ref_name
)+1;
764 goto calc_space_needed
;
765 if (copy_to_user(buf
, ref_name
, len
))
772 if (put_user(nrefs
, ret
))
779 while ((ref
= ref
->next_ref
) != NULL
)
780 space
+= strlen(ref
->ref
->name
)+1;
782 if (put_user(space
, ret
))
789 qm_symbols(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
791 size_t i
, space
, len
;
792 struct module_symbol
*s
;
796 if (!MOD_CAN_QUERY(mod
))
797 if (put_user(0, ret
))
802 space
= mod
->nsyms
* 2*sizeof(void *);
808 goto calc_space_needed
;
810 if (!access_ok(VERIFY_WRITE
, buf
, space
))
814 vals
= (unsigned long *)buf
;
817 for (; i
< mod
->nsyms
; ++i
, ++s
, vals
+= 2) {
818 len
= strlen(s
->name
)+1;
820 goto calc_space_needed
;
822 if (copy_to_user(strings
, s
->name
, len
)
823 || __put_user(s
->value
, vals
+0)
824 || __put_user(space
, vals
+1))
832 if (put_user(i
, ret
))
838 for (; i
< mod
->nsyms
; ++i
, ++s
)
839 space
+= strlen(s
->name
)+1;
841 if (put_user(space
, ret
))
848 qm_info(struct module
*mod
, char *buf
, size_t bufsize
, size_t *ret
)
852 if (mod
== &kernel_module
)
855 if (sizeof(struct module_info
) <= bufsize
) {
856 struct module_info info
;
857 info
.addr
= (unsigned long)mod
;
858 info
.size
= mod
->size
;
859 info
.flags
= mod
->flags
;
860 info
.usecount
= (mod_member_present(mod
, can_unload
)
861 && mod
->can_unload
? -1 : atomic_read(&mod
->uc
.usecount
));
863 if (copy_to_user(buf
, &info
, sizeof(struct module_info
)))
868 if (put_user(sizeof(struct module_info
), ret
))
875 sys_query_module(const char *name_user
, int which
, char *buf
, size_t bufsize
,
882 if (name_user
== NULL
)
883 mod
= &kernel_module
;
888 if ((namelen
= get_mod_name(name_user
, &name
)) < 0) {
894 mod
= &kernel_module
;
895 else if ((mod
= find_module(name
)) == NULL
) {
908 err
= qm_modules(buf
, bufsize
, ret
);
911 err
= qm_deps(mod
, buf
, bufsize
, ret
);
914 err
= qm_refs(mod
, buf
, bufsize
, ret
);
917 err
= qm_symbols(mod
, buf
, bufsize
, ret
);
920 err
= qm_info(mod
, buf
, bufsize
, ret
);
932 * Copy the kernel symbol table to user space. If the argument is
933 * NULL, just return the size of the table.
935 * This call is obsolete. New programs should use query_module+QM_SYMBOLS
936 * which does not arbitrarily limit the length of symbols.
940 sys_get_kernel_syms(struct kernel_sym
*table
)
944 struct kernel_sym ksym
;
947 for (mod
= module_list
, i
= 0; mod
; mod
= mod
->next
) {
948 /* include the count for the module name! */
955 /* So that we don't give the user our stack content */
956 memset (&ksym
, 0, sizeof (ksym
));
958 for (mod
= module_list
, i
= 0; mod
; mod
= mod
->next
) {
959 struct module_symbol
*msym
;
962 if (!MOD_CAN_QUERY(mod
))
965 /* magic: write module info as a pseudo symbol */
966 ksym
.value
= (unsigned long)mod
;
968 strncpy(ksym
.name
+1, mod
->name
, sizeof(ksym
.name
)-1);
969 ksym
.name
[sizeof(ksym
.name
)-1] = '\0';
971 if (copy_to_user(table
, &ksym
, sizeof(ksym
)) != 0)
978 for (j
= 0, msym
= mod
->syms
; j
< mod
->nsyms
; ++j
, ++msym
) {
979 ksym
.value
= msym
->value
;
980 strncpy(ksym
.name
, msym
->name
, sizeof(ksym
.name
));
981 ksym
.name
[sizeof(ksym
.name
)-1] = '\0';
983 if (copy_to_user(table
, &ksym
, sizeof(ksym
)) != 0)
994 * Look for a module by name, ignoring modules marked for deletion.
998 find_module(const char *name
)
1002 for (mod
= module_list
; mod
; mod
= mod
->next
) {
1003 if (mod
->flags
& MOD_DELETED
)
1005 if (!strcmp(mod
->name
, name
))
1013 * Free the given module.
1017 free_module(struct module
*mod
, int tag_freed
)
1019 struct module_ref
*dep
;
1022 /* Let the module clean up. */
1024 if (mod
->flags
& MOD_RUNNING
)
1028 mod
->flags
&= ~MOD_RUNNING
;
1031 /* Remove the module from the dependency lists. */
1033 for (i
= 0, dep
= mod
->deps
; i
< mod
->ndeps
; ++i
, ++dep
) {
1034 struct module_ref
**pp
;
1035 for (pp
= &dep
->dep
->refs
; *pp
!= dep
; pp
= &(*pp
)->next_ref
)
1037 *pp
= dep
->next_ref
;
1038 if (tag_freed
&& dep
->dep
->refs
== NULL
)
1039 dep
->dep
->flags
|= MOD_JUST_FREED
;
1042 /* And from the main module list. */
1044 if (mod
== module_list
) {
1045 module_list
= mod
->next
;
1048 for (p
= module_list
; p
->next
!= mod
; p
= p
->next
)
1050 p
->next
= mod
->next
;
1053 /* And free the memory. */
1059 * Called by the /proc file system to return a current list of modules.
1062 int get_module_list(char *p
)
1064 size_t left
= PAGE_SIZE
;
1067 struct module_ref
*ref
;
1069 for (mod
= module_list
; mod
!= &kernel_module
; mod
= mod
->next
) {
1073 #define safe_copy_str(str, len) \
1077 memcpy(p, str, len); p += len, left -= len; \
1079 #define safe_copy_cstr(str) safe_copy_str(str, sizeof(str)-1)
1081 len
= strlen(mod
->name
);
1082 safe_copy_str(mod
->name
, len
);
1084 if ((len
= 20 - len
) > 0) {
1087 memset(p
, ' ', len
);
1092 len
= sprintf(tmpstr
, "%8lu", mod
->size
);
1093 safe_copy_str(tmpstr
, len
);
1095 if (mod
->flags
& MOD_RUNNING
) {
1096 len
= sprintf(tmpstr
, "%4ld",
1097 (mod_member_present(mod
, can_unload
)
1099 ? -1L : (long)atomic_read(&mod
->uc
.usecount
)));
1100 safe_copy_str(tmpstr
, len
);
1103 if (mod
->flags
& MOD_DELETED
)
1104 safe_copy_cstr(" (deleted)");
1105 else if (mod
->flags
& MOD_RUNNING
) {
1106 if (mod
->flags
& MOD_AUTOCLEAN
)
1107 safe_copy_cstr(" (autoclean)");
1108 if (!(mod
->flags
& MOD_USED_ONCE
))
1109 safe_copy_cstr(" (unused)");
1111 else if (mod
->flags
& MOD_INITIALIZING
)
1112 safe_copy_cstr(" (initializing)");
1114 safe_copy_cstr(" (uninitialized)");
1116 if ((ref
= mod
->refs
) != NULL
) {
1117 safe_copy_cstr(" [");
1121 safe_copy_str(q
, len
);
1123 if ((ref
= ref
->next_ref
) != NULL
)
1124 safe_copy_cstr(" ");
1128 safe_copy_cstr("]");
1130 safe_copy_cstr("\n");
1132 #undef safe_copy_str
1133 #undef safe_copy_cstr
1137 return PAGE_SIZE
- left
;
1141 * Called by the /proc file system to return a current list of ksyms.
1145 get_ksyms_list(char *buf
, char **start
, off_t offset
, int length
)
1149 int len
= 0; /* code from net/ipv4/proc.c */
1153 for (mod
= module_list
; mod
; mod
= mod
->next
) {
1155 struct module_symbol
*sym
;
1157 if (!MOD_CAN_QUERY(mod
))
1160 for (i
= mod
->nsyms
, sym
= mod
->syms
; i
> 0; --i
, ++sym
) {
1163 len
+= sprintf(p
, "%0*lx %s\t[%s]\n",
1164 (int)(2*sizeof(void*)),
1165 sym
->value
, sym
->name
,
1168 len
+= sprintf(p
, "%0*lx %s\n",
1169 (int)(2*sizeof(void*)),
1170 sym
->value
, sym
->name
);
1178 if (pos
> offset
+length
)
1179 goto leave_the_loop
;
1183 *start
= buf
+ (offset
- begin
);
1184 len
-= (offset
- begin
);
1190 #else /* CONFIG_MODULES */
1192 /* Dummy syscalls for people who don't want modules */
1194 asmlinkage
unsigned long
1195 sys_create_module(const char *name_user
, size_t size
)
1201 sys_init_module(const char *name_user
, struct module
*mod_user
)
1207 sys_delete_module(const char *name_user
)
1213 sys_query_module(const char *name_user
, int which
, char *buf
, size_t bufsize
,
1216 /* Let the program know about the new interface. Not that
1217 it'll do them much good. */
1225 sys_get_kernel_syms(struct kernel_sym
*table
)
1230 int try_inc_mod_count(struct module
*mod
)
1235 #endif /* CONFIG_MODULES */