linux-2.1.110pre2: Minor cleanups for kernel/module.c

Horst von Brand (vonbrand@sleipnir.valparaiso.cl)
Sun, 19 Jul 1998 00:16:29 -0500


This improves readability and also gives (totally inconsecuential ;-) space
and time savings. Compiles and boots, seems to be running fine here.

--- linux/kernel/module.c.dist-2.1.110pre1 Sun Jun 7 13:37:42 1998
+++ linux/kernel/module.c Sun Jul 19 00:09:33 1998
@@ -10,6 +10,7 @@
#include <linux/smp.h>
#include <linux/smp_lock.h>
#include <asm/pgtable.h>
+#include <linux/init.h>

/*
* Originally by Anonymous (as far as I know...)
@@ -17,6 +18,7 @@
* 0.99.14 version by Jon Tombs <jon@gtex02.us.es>,
* Heavily modified by Bjorn Ekwall <bj0rn@blox.se> May 1994 (C)
* Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
+ * Cleanups by Horst von Brand <vonbrand@sleipnir.valparaiso.cl> Jul 1998
*
* This source is covered by the GNU GPL, the same as all kernel sources.
*/
@@ -61,7 +63,7 @@
* Called at boot time
*/

-void init_modules(void)
+__initfunc(void init_modules(void))
{
kernel_module.nsyms = __stop___ksymtab - __start___ksymtab;

@@ -122,23 +124,23 @@
lock_kernel();
if (!capable(CAP_SYS_MODULE)) {
error = -EPERM;
- goto err0;
+ goto out;
}
if ((namelen = get_mod_name(name_user, &name)) < 0) {
error = namelen;
- goto err0;
+ goto out;
}
- if (size < sizeof(struct module)+namelen) {
+ if (size < sizeof(struct module) + namelen) {
error = -EINVAL;
- goto err1;
+ goto put_out;
}
if (find_module(name) != NULL) {
error = -EEXIST;
- goto err1;
+ goto put_out;
}
if ((mod = (struct module *)module_map(size)) == NULL) {
error = -ENOMEM;
- goto err1;
+ goto put_out;
}

memset(mod, 0, sizeof(*mod));
@@ -146,17 +148,14 @@
mod->next = module_list;
mod->name = (char *)(mod + 1);
mod->size = size;
- memcpy((char*)(mod+1), name, namelen+1);
-
- put_mod_name(name);
-
+ memcpy((char*)(mod + 1), name, namelen + 1);
module_list = mod; /* link it in */

error = (long) mod;
- goto err0;
-err1:
+
+put_out:
put_mod_name(name);
-err0:
+out:
unlock_kernel();
return error;
}
@@ -192,7 +191,7 @@
if ((error = get_user(mod_user_size, &mod_user->size_of_struct)) != 0)
goto err1;
if (mod_user_size < (unsigned long)&((struct module *)0L)->persist_start
- || mod_user_size > sizeof(struct module) + 16*sizeof(void*)) {
+ || mod_user_size > sizeof(struct module) + 16 * sizeof(void*)) {
printk(KERN_ERR "init_module: Invalid module header size.\n"
KERN_ERR "A new version of the modutils is likely "
"needed.\n");
@@ -221,8 +220,8 @@

/* Make sure all interesting pointers are sane. */

-#define bound(p, n, m) ((unsigned long)(p) >= (unsigned long)(m+1) && \
- (unsigned long)((p)+(n)) <= (unsigned long)(m) + (m)->size)
+#define bound(p, n, m) ((unsigned long)(p) >= (unsigned long)(m + 1) && \
+ (unsigned long)((p) + (n)) <= (unsigned long)(m) + (m)->size)

if (!bound(mod->name, namelen, mod)) {
printk(KERN_ERR "init_module: mod->name out of bounds.\n");
@@ -246,7 +245,7 @@
}
if (mod->ex_table_start > mod->ex_table_end
|| (mod->ex_table_start &&
- !((unsigned long)mod->ex_table_start >= (unsigned long)(mod+1)
+ !((unsigned long)mod->ex_table_start >= (unsigned long)(mod + 1)
&& ((unsigned long)mod->ex_table_end
< (unsigned long)mod + mod->size)))
|| (((unsigned long)mod->ex_table_start
@@ -290,7 +289,7 @@

/* Ok, that's about all the sanity we can stomach; copy the rest. */

- if (copy_from_user(mod+1, mod_user+1, mod->size-sizeof(*mod))) {
+ if (copy_from_user(mod + 1, mod_user + 1, mod->size-sizeof(*mod))) {
error = -EFAULT;
goto err3;
}
@@ -374,50 +373,56 @@
goto out;
if (error == 0) {
error = -EINVAL;
- put_mod_name(name);
- goto out;
+ goto put_out;
}
- error = -ENOENT;
+
if ((mod = find_module(name)) == NULL) {
- put_mod_name(name);
- goto out;
+ error = -ENOENT;
+ goto put_out;
+ }
+
+ if (mod->refs != NULL || __MOD_IN_USE(mod)) {
+ error = -EBUSY;
+ goto put_out;
}
- put_mod_name(name);
- error = -EBUSY;
- if (mod->refs != NULL || __MOD_IN_USE(mod))
- goto out;

free_module(mod, 0);
error = 0;
- goto out;
+ goto put_out;
}

+#define MASK (MOD_AUTOCLEAN | MOD_RUNNING | MOD_DELETED | MOD_USED_ONCE)
+#define VALUE (MOD_AUTOCLEAN | MOD_RUNNING | MOD_USED_ONCE)
+
/* Do automatic reaping */
-restart:
- something_changed = 0;
- for (mod = module_list; mod != &kernel_module; mod = next) {
- next = mod->next;
- if (mod->refs == NULL
- && (mod->flags & MOD_AUTOCLEAN)
- && (mod->flags & MOD_RUNNING)
- && !(mod->flags & MOD_DELETED)
- && (mod->flags & MOD_USED_ONCE)
- && !__MOD_IN_USE(mod)) {
- if ((mod->flags & MOD_VISITED)
- && !(mod->flags & MOD_JUST_FREED)) {
- mod->flags &= ~MOD_VISITED;
- } else {
- free_module(mod, 1);
- something_changed = 1;
+ do {
+ something_changed = 0;
+ for (mod = module_list; mod != &kernel_module; mod = next) {
+ next = mod->next;
+ if (mod->refs == NULL
+ && ((mod->flags & MASK) == VALUE)
+ && !__MOD_IN_USE(mod)) {
+ if ((mod->flags & MOD_VISITED)
+ && !(mod->flags & MOD_JUST_FREED)) {
+ mod->flags &= ~MOD_VISITED;
+ } else {
+ free_module(mod, 1);
+ something_changed++;
+ }
}
}
- }
- if (something_changed)
- goto restart;
+ } while (something_changed);
for (mod = module_list; mod != &kernel_module; mod = mod->next)
mod->flags &= ~MOD_JUST_FREED;
error = 0;
-out:
+ goto out;
+
+#undef MASK
+#undef VALUE
+
+ put_out:
+ put_mod_name(name);
+ out:
unlock_kernel();
return error;
}
@@ -433,7 +438,7 @@
nmod = space = 0;

for (mod=module_list; mod != &kernel_module; mod=mod->next, ++nmod) {
- len = strlen(mod->name)+1;
+ len = strlen(mod->name) + 1;
if (len > bufsize)
goto calc_space_needed;
if (copy_to_user(buf, mod->name, len))
@@ -451,7 +456,7 @@
calc_space_needed:
space += len;
while ((mod = mod->next) != &kernel_module)
- space += strlen(mod->name)+1;
+ space += strlen(mod->name) + 1;

if (put_user(space, ret))
return -EFAULT;
@@ -476,7 +481,7 @@
for (i = 0; i < mod->ndeps; ++i) {
const char *dep_name = mod->deps[i].dep->name;

- len = strlen(dep_name)+1;
+ len = strlen(dep_name) + 1;
if (len > bufsize)
goto calc_space_needed;
if (copy_to_user(buf, dep_name, len))
@@ -494,7 +499,7 @@
calc_space_needed:
space += len;
while (++i < mod->ndeps)
- space += strlen(mod->deps[i].dep->name)+1;
+ space += strlen(mod->deps[i].dep->name) + 1;

if (put_user(space, ret))
return -EFAULT;
@@ -520,7 +525,7 @@
for (nrefs = 0, ref = mod->refs; ref ; ++nrefs, ref = ref->next_ref) {
const char *ref_name = ref->ref->name;

- len = strlen(ref_name)+1;
+ len = strlen(ref_name) + 1;
if (len > bufsize)
goto calc_space_needed;
if (copy_to_user(buf, ref_name, len))
@@ -538,7 +543,7 @@
calc_space_needed:
space += len;
while ((ref = ref->next_ref) != NULL)
- space += strlen(ref->ref->name)+1;
+ space += strlen(ref->ref->name) + 1;

if (put_user(space, ret))
return -EFAULT;
@@ -573,16 +578,16 @@

bufsize -= space;
vals = (unsigned long *)buf;
- strings = buf+space;
+ strings = buf + space;

for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
- len = strlen(s->name)+1;
+ len = strlen(s->name) + 1;
if (len > bufsize)
goto calc_space_needed;

if (copy_to_user(strings, s->name, len)
- || __put_user(s->value, vals+0)
- || __put_user(space, vals+1))
+ || __put_user(s->value, vals + 0)
+ || __put_user(space, vals + 1))
return -EFAULT;

strings += len;
@@ -597,7 +602,7 @@

calc_space_needed:
for (; i < mod->nsyms; ++i, ++s)
- space += strlen(s->name)+1;
+ space += strlen(s->name) + 1;

if (put_user(space, ret))
return -EFAULT;
@@ -720,14 +725,14 @@
struct module_symbol *msym;
unsigned int j;

- if ((mod->flags & (MOD_RUNNING|MOD_DELETED)) != MOD_RUNNING)
+ if ((mod->flags & (MOD_RUNNING | MOD_DELETED)) != MOD_RUNNING)
continue;

/* magic: write module info as a pseudo symbol */
ksym.value = (unsigned long)mod;
ksym.name[0] = '#';
- strncpy(ksym.name+1, mod->name, sizeof(ksym.name)-1);
- ksym.name[sizeof(ksym.name)-1] = '\0';
+ strncpy(ksym.name + 1, mod->name, sizeof(ksym.name) - 1);
+ ksym.name[sizeof(ksym.name) - 1] = '\0';

if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
goto out;
@@ -739,7 +744,7 @@
for (j = 0, msym = mod->syms; j < mod->nsyms; ++j, ++msym) {
ksym.value = msym->value;
strncpy(ksym.name, msym->name, sizeof(ksym.name));
- ksym.name[sizeof(ksym.name)-1] = '\0';
+ ksym.name[sizeof(ksym.name) - 1] = '\0';

if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
goto out;
@@ -838,7 +843,7 @@
goto fini; \
memcpy(p, str, len); p += len, left -= len; \
} while (0)
-#define safe_copy_cstr(str) safe_copy_str(str, sizeof(str)-1)
+#define safe_copy_cstr(str) safe_copy_str(str, sizeof(str) - 1)

len = strlen(mod->name);
safe_copy_str(mod->name, len);
@@ -920,12 +925,12 @@
p = buf + len;
if (*mod->name) {
len += sprintf(p, "%0*lx %s\t[%s]\n",
- (int)(2*sizeof(void*)),
+ (int)(2 * sizeof(void*)),
sym->value, sym->name,
mod->name);
} else {
len += sprintf(p, "%0*lx %s\n",
- (int)(2*sizeof(void*)),
+ (int)(2 * sizeof(void*)),
sym->value, sym->name);
}
pos = begin + len;
@@ -934,7 +939,7 @@
begin = pos;
}
pos = begin + len;
- if (pos > offset+length)
+ if (pos > offset + length)
goto leave_the_loop;
}
}

-- 
Horst von Brand                             vonbrand@sleipnir.valparaiso.cl
Casilla 9G, Viņa del Mar, Chile                               +56 32 672616

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html